From: Sami Kerola Date: Sat, 31 Oct 2015 17:29:02 +0000 (+0000) Subject: lib/blkdev: add open_blkdev_or_file() function X-Git-Tag: v2.28-rc1~248^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8fe1e638fa81d88e1e405f6221c7a592284c9b23;p=thirdparty%2Futil-linux.git lib/blkdev: add open_blkdev_or_file() function Purpose of this function is to open a path that is potentially pointing to a block device or file without races. The function also proper open(3) flags are used to check the device is not busy, and finally warning is been printed if a block device happens to be misaligned. Signed-off-by: Sami Kerola --- diff --git a/include/blkdev.h b/include/blkdev.h index c994795a2a..46fb2cef05 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 struct stat *st, 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 5f495822ee..a57b3672bf 100644 --- a/lib/blkdev.c +++ b/lib/blkdev.c @@ -25,9 +25,15 @@ # include #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,24 @@ int blkdev_is_misaligned(int fd) #endif } +int open_blkdev_or_file(const struct stat *st, const char *name, const int oflag) +{ + int fd; + + if (S_ISBLK(st->st_mode)) { + fd = open(name, oflag | O_EXCL); + } else + fd = open(name, oflag); + if (-1 < fd && !is_same_inode(fd, st)) { + close(fd); + errno = EBADFD; + return -1; + } + if (-1 < fd && S_ISBLK(st->st_mode) && blkdev_is_misaligned(fd)) + warnx(_("warning: %s is misaligned"), name); + return fd; +} + int blkdev_is_cdrom(int fd) { #ifdef CDROM_GET_CAPABILITY