]> err.no Git - util-linux/commitdiff
disk-utils: let mkfs tools open with O_EXCL
authorMatthias Koenig <mkoenig@suse.de>
Wed, 18 Jul 2007 14:15:46 +0000 (16:15 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 27 Jul 2007 11:39:29 +0000 (13:39 +0200)
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 (mounted).

Unfortunately, O_EXCL has zero effect for 2.4 kernels where
in-kernel code doesn't use O_EXCL-like access locks. (Tested
on RHEL3.)

Signed-off-by: Matthias Koenig <mkoenig@suse.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/mkfs.bfs.c
disk-utils/mkfs.minix.c
disk-utils/mkswap.c

index 8221f3b7e6282a5c4a825d6d7a2d03f92630974c..557cde48098feb437b1282a417c42314412159c1 100644 (file)
@@ -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);
index 92a6789a75d86d0c8a2a4e7b89afe5a489c20292..8a81e7cadc750866d30a3756023e95f378264639 100644 (file)
@@ -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)
index 1fd1f185bd040971fc44a26b62ee907222dd99e2..6af1ff7bb51b779ca7fb213b171132a91ba58bc2 100644 (file)
@@ -640,8 +640,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);
        }