Let mkswap, mkfs.bfs, mkfs.minix open with O_EXCL if used on block devices to prevent writing to the device even if they are busy. Signed-off-by: Matthias Koenig <mkoenig@xxxxxxx> --- disk-utils/mkfs.bfs.c | 2 +- disk-utils/mkfs.minix.c | 9 ++++++--- disk-utils/mkswap.c | 12 ++++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/disk-utils/mkfs.bfs.c b/disk-utils/mkfs.bfs.c index 8221f3b..557cde4 100644 --- a/disk-utils/mkfs.bfs.c +++ b/disk-utils/mkfs.bfs.c @@ -170,7 +170,7 @@ main(int argc, char *argv[]) { if (!S_ISBLK(statbuf.st_mode)) fatal(_("%s is not a block special device"), device); - fd = open(device, O_RDWR); + fd = open(device, O_RDWR | O_EXCL); if (fd == -1) { perror(device); fatal(_("cannot open %s"), device); diff --git a/disk-utils/mkfs.minix.c b/disk-utils/mkfs.minix.c index 92a6789..8a81e7c 100644 --- a/disk-utils/mkfs.minix.c +++ b/disk-utils/mkfs.minix.c @@ -699,11 +699,14 @@ main(int argc, char ** argv) { tmp += dirsize; *(short *)tmp = 2; strcpy(tmp+2,".badblocks"); - DEV = open(device_name,O_RDWR ); + if (stat(device_name, &statbuf) < 0) + die(_("unable to stat %s")); + if (S_ISBLK(statbuf.st_mode)) + DEV = open(device_name,O_RDWR | O_EXCL); + else + DEV = open(device_name,O_RDWR); if (DEV<0) die(_("unable to open %s")); - if (fstat(DEV,&statbuf)<0) - die(_("unable to stat %s")); if (!S_ISBLK(statbuf.st_mode)) check=0; else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c index fdd3e0d..c2fc342 100644 --- a/disk-utils/mkswap.c +++ b/disk-utils/mkswap.c @@ -660,8 +660,16 @@ main(int argc, char ** argv) { usage(); } - DEV = open(device_name,O_RDWR); - if (DEV < 0 || fstat(DEV, &statbuf) < 0) { + if (stat(device_name, &statbuf) < 0) { + perror(device_name); + exit(EXIT_FAILURE); + } + if (S_ISBLK(statbuf.st_mode)) + DEV = open(device_name, O_RDWR | O_EXCL); + else + DEV = open(device_name, O_RDWR); + + if (DEV < 0) { perror(device_name); exit(1); } -- 1.5.0.4.GIT - To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html