svn commit: trunk/busybox/util-linux

vda at busybox.net vda at busybox.net
Thu Apr 17 01:52:29 UTC 2008


Author: vda
Date: 2008-04-16 18:52:28 -0700 (Wed, 16 Apr 2008)
New Revision: 21751

Log:
fdisk: fix a case where we can inadvertently close /proc/partitions fd early



Modified:
   trunk/busybox/util-linux/fdisk.c
   trunk/busybox/util-linux/fdisk_osf.c


Changeset:
Modified: trunk/busybox/util-linux/fdisk.c
===================================================================
--- trunk/busybox/util-linux/fdisk.c	2008-04-17 00:12:10 UTC (rev 21750)
+++ trunk/busybox/util-linux/fdisk.c	2008-04-17 01:52:28 UTC (rev 21751)
@@ -36,6 +36,18 @@
 #define LINUX_LVM       0x8e
 #define LINUX_RAID      0xfd
 
+
+enum {
+	OPT_b = 1 << 0,
+	OPT_C = 1 << 1,
+	OPT_H = 1 << 2,
+	OPT_l = 1 << 3,
+	OPT_S = 1 << 4,
+	OPT_u = 1 << 5,
+	OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
+};
+
+
 /* Used for sector numbers. Today's disk sizes make it necessary */
 typedef unsigned long long ullong;
 
@@ -366,6 +378,13 @@
 		s |= (sector >> 2) & 0xc0;   \
 	} while (0)
 
+static void
+close_dev_fd(void)
+{
+	/* Not really closing, but making sure it is open, and to harmless place */
+	xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
+}
+
 #if ENABLE_FEATURE_FDISK_WRITABLE
 /* read line; return 0 or first printable char */
 static int
@@ -660,7 +679,7 @@
 static void fdisk_fatal(const char *why)
 {
 	if (listing) {
-		close(dev_fd);
+		close_dev_fd();
 		longjmp(listingbuf, 1);
 	}
 	bb_error_msg_and_die(why, disk_device);
@@ -1280,12 +1299,12 @@
 				return 1;
 			fdisk_fatal(unable_to_open);
 		}
-		xmove_fd(fd, dev_fd);
 		printf("'%s' is opened for read only\n", disk_device);
 	}
+	xmove_fd(fd, dev_fd);
 	if (512 != read(dev_fd, MBRbuffer, 512)) {
 		if (what == TRY_ONLY) {
-			close(dev_fd);
+			close_dev_fd();
 			return 1;
 		}
 		fdisk_fatal(unable_to_read);
@@ -2466,7 +2485,7 @@
 
 	if (leave) {
 		if (ENABLE_FEATURE_CLEAN_UP)
-			close(dev_fd);
+			close_dev_fd();
 		exit(i != 0);
 	}
 }
@@ -2599,7 +2618,7 @@
 			break;
 		case 'q':
 			if (ENABLE_FEATURE_CLEAN_UP)
-				close(dev_fd);
+				close_dev_fd();
 			bb_putchar('\n');
 			exit(0);
 		case 'r':
@@ -2708,7 +2727,7 @@
 #endif
 	}
  ret:
-	close(dev_fd);
+	close_dev_fd();
 }
 
 /* for fdisk -l: try all things in /proc/partitions
@@ -2758,19 +2777,9 @@
 	 *
 	 * Options -C, -H, -S set the geometry.
 	 */
-	enum {
-		OPT_b = 1 << 0,
-		OPT_C = 1 << 1,
-		OPT_H = 1 << 2,
-		OPT_l = 1 << 3,
-		OPT_S = 1 << 4,
-		OPT_u = 1 << 5,
-		OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
-	};
-
 	INIT_G();
 
-	close(dev_fd); /* just in case */
+	close_dev_fd(); /* needed: fd 3 must not stay closed */
 
 	opt_complementary = "b+:C+:H+:S+"; /* numeric params */
 	opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
@@ -2933,7 +2942,8 @@
 			list_table(0);
 			break;
 		case 'q':
-			close(dev_fd);
+			if (ENABLE_FEATURE_CLEAN_UP)
+				close_dev_fd();
 			bb_putchar('\n');
 			return 0;
 		case 's':

Modified: trunk/busybox/util-linux/fdisk_osf.c
===================================================================
--- trunk/busybox/util-linux/fdisk_osf.c	2008-04-17 00:12:10 UTC (rev 21750)
+++ trunk/busybox/util-linux/fdisk_osf.c	2008-04-17 01:52:28 UTC (rev 21751)
@@ -414,7 +414,7 @@
 			break;
 		case 'q':
 			if (ENABLE_FEATURE_CLEAN_UP)
-				close(dev_fd);
+				close_dev_fd();
 			exit(EXIT_SUCCESS);
 		case 'r':
 			return;




More information about the busybox-cvs mailing list