aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/fdisk.c46
-rw-r--r--util-linux/fdisk_osf.c2
2 files changed, 29 insertions, 19 deletions
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 57d4d653e..50ab48dab 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -36,6 +36,18 @@
36#define LINUX_LVM 0x8e 36#define LINUX_LVM 0x8e
37#define LINUX_RAID 0xfd 37#define LINUX_RAID 0xfd
38 38
39
40enum {
41 OPT_b = 1 << 0,
42 OPT_C = 1 << 1,
43 OPT_H = 1 << 2,
44 OPT_l = 1 << 3,
45 OPT_S = 1 << 4,
46 OPT_u = 1 << 5,
47 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
48};
49
50
39/* Used for sector numbers. Today's disk sizes make it necessary */ 51/* Used for sector numbers. Today's disk sizes make it necessary */
40typedef unsigned long long ullong; 52typedef unsigned long long ullong;
41 53
@@ -366,6 +378,13 @@ static ullong bb_BLKGETSIZE_sectors(int fd)
366 s |= (sector >> 2) & 0xc0; \ 378 s |= (sector >> 2) & 0xc0; \
367 } while (0) 379 } while (0)
368 380
381static void
382close_dev_fd(void)
383{
384 /* Not really closing, but making sure it is open, and to harmless place */
385 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
386}
387
369#if ENABLE_FEATURE_FDISK_WRITABLE 388#if ENABLE_FEATURE_FDISK_WRITABLE
370/* read line; return 0 or first printable char */ 389/* read line; return 0 or first printable char */
371static int 390static int
@@ -660,7 +679,7 @@ static ullong total_number_of_sectors;
660static void fdisk_fatal(const char *why) 679static void fdisk_fatal(const char *why)
661{ 680{
662 if (listing) { 681 if (listing) {
663 close(dev_fd); 682 close_dev_fd();
664 longjmp(listingbuf, 1); 683 longjmp(listingbuf, 1);
665 } 684 }
666 bb_error_msg_and_die(why, disk_device); 685 bb_error_msg_and_die(why, disk_device);
@@ -1280,12 +1299,12 @@ static int get_boot(void)
1280 return 1; 1299 return 1;
1281 fdisk_fatal(unable_to_open); 1300 fdisk_fatal(unable_to_open);
1282 } 1301 }
1283 xmove_fd(fd, dev_fd);
1284 printf("'%s' is opened for read only\n", disk_device); 1302 printf("'%s' is opened for read only\n", disk_device);
1285 } 1303 }
1304 xmove_fd(fd, dev_fd);
1286 if (512 != read(dev_fd, MBRbuffer, 512)) { 1305 if (512 != read(dev_fd, MBRbuffer, 512)) {
1287 if (what == TRY_ONLY) { 1306 if (what == TRY_ONLY) {
1288 close(dev_fd); 1307 close_dev_fd();
1289 return 1; 1308 return 1;
1290 } 1309 }
1291 fdisk_fatal(unable_to_read); 1310 fdisk_fatal(unable_to_read);
@@ -2466,7 +2485,7 @@ reread_partition_table(int leave)
2466 2485
2467 if (leave) { 2486 if (leave) {
2468 if (ENABLE_FEATURE_CLEAN_UP) 2487 if (ENABLE_FEATURE_CLEAN_UP)
2469 close(dev_fd); 2488 close_dev_fd();
2470 exit(i != 0); 2489 exit(i != 0);
2471 } 2490 }
2472} 2491}
@@ -2599,7 +2618,7 @@ xselect(void)
2599 break; 2618 break;
2600 case 'q': 2619 case 'q':
2601 if (ENABLE_FEATURE_CLEAN_UP) 2620 if (ENABLE_FEATURE_CLEAN_UP)
2602 close(dev_fd); 2621 close_dev_fd();
2603 bb_putchar('\n'); 2622 bb_putchar('\n');
2604 exit(0); 2623 exit(0);
2605 case 'r': 2624 case 'r':
@@ -2708,7 +2727,7 @@ open_list_and_close(const char *device, int user_specified)
2708#endif 2727#endif
2709 } 2728 }
2710 ret: 2729 ret:
2711 close(dev_fd); 2730 close_dev_fd();
2712} 2731}
2713 2732
2714/* for fdisk -l: try all things in /proc/partitions 2733/* for fdisk -l: try all things in /proc/partitions
@@ -2758,19 +2777,9 @@ int fdisk_main(int argc, char **argv)
2758 * 2777 *
2759 * Options -C, -H, -S set the geometry. 2778 * Options -C, -H, -S set the geometry.
2760 */ 2779 */
2761 enum {
2762 OPT_b = 1 << 0,
2763 OPT_C = 1 << 1,
2764 OPT_H = 1 << 2,
2765 OPT_l = 1 << 3,
2766 OPT_S = 1 << 4,
2767 OPT_u = 1 << 5,
2768 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2769 };
2770
2771 INIT_G(); 2780 INIT_G();
2772 2781
2773 close(dev_fd); /* just in case */ 2782 close_dev_fd(); /* needed: fd 3 must not stay closed */
2774 2783
2775 opt_complementary = "b+:C+:H+:S+"; /* numeric params */ 2784 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
2776 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"), 2785 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
@@ -2933,7 +2942,8 @@ int fdisk_main(int argc, char **argv)
2933 list_table(0); 2942 list_table(0);
2934 break; 2943 break;
2935 case 'q': 2944 case 'q':
2936 close(dev_fd); 2945 if (ENABLE_FEATURE_CLEAN_UP)
2946 close_dev_fd();
2937 bb_putchar('\n'); 2947 bb_putchar('\n');
2938 return 0; 2948 return 0;
2939 case 's': 2949 case 's':
diff --git a/util-linux/fdisk_osf.c b/util-linux/fdisk_osf.c
index ba01a7f0c..e281ea5d9 100644
--- a/util-linux/fdisk_osf.c
+++ b/util-linux/fdisk_osf.c
@@ -414,7 +414,7 @@ bsd_select(void)
414 break; 414 break;
415 case 'q': 415 case 'q':
416 if (ENABLE_FEATURE_CLEAN_UP) 416 if (ENABLE_FEATURE_CLEAN_UP)
417 close(dev_fd); 417 close_dev_fd();
418 exit(EXIT_SUCCESS); 418 exit(EXIT_SUCCESS);
419 case 'r': 419 case 'r':
420 return; 420 return;