From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>
Subject: [PATCH 07/12] include/blkdev: add open_blkdev_or_file() function
Date: Sat, 31 Oct 2015 19:21:19 +0000 [thread overview]
Message-ID: <1446319284-24778-8-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1446319284-24778-1-git-send-email-kerolasa@iki.fi>
Purpose of this function is to open a path that is potentially pointing to a
block device or file without races, and checking ensuring block device is
not busy when path points to such.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
include/blkdev.h | 3 +++
lib/blkdev.c | 27 +++++++++++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/include/blkdev.h b/include/blkdev.h
index c994795..fde2dbf 100644
--- a/include/blkdev.h
+++ b/include/blkdev.h
@@ -97,6 +97,9 @@ struct hd_geometry {
/* are we working with block device? */
int is_blkdev(int fd);
+/* open block device or file */
+int open_blkdev_or_file(const char *name, const int oflag);
+
/* Determine size in bytes */
off_t blkdev_find_size (int fd);
diff --git a/lib/blkdev.c b/lib/blkdev.c
index 5f49582..da8d835 100644
--- a/lib/blkdev.c
+++ b/lib/blkdev.c
@@ -25,9 +25,15 @@
# include <sys/disk.h>
#endif
+#ifndef EBADFD
+# define EBADFD 77 /* File descriptor in bad state */
+#endif
+
#include "blkdev.h"
#include "c.h"
#include "linux_version.h"
+#include "fileutils.h"
+#include "nls.h"
static long
blkdev_valid_offset (int fd, off_t offset) {
@@ -254,6 +260,27 @@ int blkdev_is_misaligned(int fd)
#endif
}
+int open_blkdev_or_file(const char *name, const int oflag)
+{
+ int fd;
+ struct stat ftype;
+
+ if (stat(name, &ftype) < 0)
+ return -1;
+ if (S_ISBLK(ftype.st_mode)) {
+ fd = open(name, oflag | O_EXCL);
+ } else
+ fd = open(name, oflag);
+ if (-1 < fd && !is_same_inode(fd, name)) {
+ close(fd);
+ errno = EBADFD;
+ return -1;
+ }
+ if (-1 < fd && S_ISBLK(ftype.st_mode) && blkdev_is_misaligned(fd))
+ warnx(_("warning: %s is misaligned"), name);
+ return fd;
+}
+
int blkdev_is_cdrom(int fd)
{
#ifdef CDROM_GET_CAPABILITY
--
2.6.2
next prev parent reply other threads:[~2015-10-31 19:21 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-31 19:21 [PATCH 00/12] pull: race fixes Sami Kerola
2015-10-31 19:21 ` [PATCH 01/12] login: fix stat(2) race Sami Kerola
2015-10-31 19:21 ` [PATCH 02/12] swapon: fix stat(3) and open(3) race Sami Kerola
2015-10-31 19:21 ` [PATCH 03/12] fsck: retire stat(3) when access(3) does better job Sami Kerola
2015-10-31 19:21 ` [PATCH 04/12] agetty: fix chown(3), chmod(3) and open(3) race Sami Kerola
2015-11-09 9:01 ` Karel Zak
2015-10-31 19:21 ` [PATCH 05/12] script, hwclock: check file exist with access(3) rather than stat(3) Sami Kerola
2015-11-09 8:43 ` Karel Zak
2015-10-31 19:21 ` [PATCH 06/12] lib/fileutils: add is_same_inode() check Sami Kerola
2015-11-09 8:54 ` Karel Zak
2015-10-31 19:21 ` Sami Kerola [this message]
2015-11-09 8:51 ` [PATCH 07/12] include/blkdev: add open_blkdev_or_file() function Karel Zak
2015-11-22 21:27 ` Sami Kerola
2015-10-31 19:21 ` [PATCH 08/12] mkfs.bfs: fix block device open race Sami Kerola
2015-11-09 8:48 ` Karel Zak
2015-11-22 21:24 ` Sami Kerola
2015-10-31 19:21 ` [PATCH 09/12] mkfs.minix: " Sami Kerola
2015-10-31 19:21 ` [PATCH 10/12] mkswap: " Sami Kerola
2015-10-31 19:21 ` [PATCH 11/12] mesg: fix multiple races Sami Kerola
2015-10-31 19:21 ` [PATCH 12/12] fdformat: fix block device open race Sami Kerola
2015-11-09 8:58 ` [PATCH 00/12] pull: race fixes Karel Zak
2015-12-02 9:54 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1446319284-24778-8-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox