]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdformat: fix block device open race
authorSami Kerola <kerolasa@iki.fi>
Sat, 31 Oct 2015 18:34:36 +0000 (18:34 +0000)
committerSami Kerola <kerolasa@iki.fi>
Sun, 22 Nov 2015 20:56:38 +0000 (20:56 +0000)
This fix makes fdformat also to detect if block device is busy, and warn if
the device is misaligned.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
disk-utils/fdformat.c

index e7d43da8f6b10ee188a7e0f42695487815272901..66e1bb90d6d3b26caa83141f9f5720397782a730 100644 (file)
@@ -12,6 +12,7 @@
 #include <unistd.h>
 
 #include "c.h"
+#include "blkdev.h"
 #include "strutils.h"
 #include "closestream.h"
 #include "nls.h"
@@ -221,10 +222,7 @@ int main(int argc, char **argv)
        if (!S_ISBLK(st.st_mode))
                /* do not test major - perhaps this was an USB floppy */
                errx(EXIT_FAILURE, _("%s: not a block device"), argv[0]);
-       if (access(argv[0], W_OK) < 0)
-               err(EXIT_FAILURE, _("cannot access file %s"), argv[0]);
-
-       ctrl = open(argv[0], O_RDWR);
+       ctrl = open_blkdev_or_file(&st, argv[0], O_RDWR);
        if (ctrl < 0)
                err(EXIT_FAILURE, _("cannot open %s"), argv[0]);
        if (ioctl(ctrl, FDGETPRM, (long) &param) < 0)