mkswap: fix block device open race

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2015-10-31 18:10:00 +00:00
parent 8a4eb983d8
commit 80c320fac1
No known key found for this signature in database
GPG Key ID: A9553245FDE9B739
1 changed files with 6 additions and 15 deletions

View 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)