]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mkswap: fix block device open race
authorSami Kerola <kerolasa@iki.fi>
Sat, 31 Oct 2015 18:10:00 +0000 (18:10 +0000)
committerSami Kerola <kerolasa@iki.fi>
Sun, 22 Nov 2015 20:56:38 +0000 (20:56 +0000)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
disk-utils/mkswap.c

index c7166f7313d9b9623531f08688ba18e30939a1b7..c559e60e4431fc76bbc36cbb865d03421d0825d3 100644 (file)
@@ -240,23 +240,14 @@ static void open_device(struct mkswap_control *ctl)
 
        if (stat(ctl->devname, &ctl->devstat) < 0)
                err(EXIT_FAILURE, _("stat of %s failed"), ctl->devname);
-
-       if (S_ISBLK(ctl->devstat.st_mode))
-               ctl->fd = open(ctl->devname, O_RDWR | O_EXCL);
-       else {
-               if (ctl->check) {
-                       ctl->check = 0;
-                       warnx(_("warning: checking bad blocks from swap file is not supported: %s"),
-                               ctl->devname);
-               }
-               ctl->fd = open(ctl->devname, O_RDWR);
-       }
+       ctl->fd = open_blkdev_or_file(&ctl->devstat, ctl->devname, O_RDWR);
        if (ctl->fd < 0)
                err(EXIT_FAILURE, _("cannot open %s"), ctl->devname);
-
-       if (S_ISBLK(ctl->devstat.st_mode))
-               if (blkdev_is_misaligned(ctl->fd))
-                       warnx(_("warning: %s is misaligned"), ctl->devname);
+       if (ctl->check && S_ISREG(ctl->devstat.st_mode)) {
+               ctl->check = 0;
+               warnx(_("warning: checking bad blocks from swap file is not supported: %s"),
+                      ctl->devname);
+       }
 }
 
 static void wipe_device(struct mkswap_control *ctl)