[PATCH] fdisk: eliminate redundant call to open()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

The command

  fdisk -l /dev/sda

opens a redundant file descriptor to file /dev/sda.  The redundant file
descriptor is also not closed.  Using strace on the command gives:

  [ Locale initializations snipped ]

open("/dev/sda", O_RDONLY)              = 3
uname({sys="Linux", node="tux", ...})   = 0
ioctl(3, BLKSSZGET, 0x7fff060fe6ac)     = 0
lseek(3, 512, SEEK_SET)                 = 512
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512
fstat(3, {st_mode=S_IFBLK|0660, st_rdev=makedev(8, 0), ...}) = 0
ioctl(3, BLKGETSIZE64, 0x7fff060fe770)  = 0
ioctl(3, BLKSSZGET, 0x7fff060fe77c)     = 0
ioctl(3, BLKSSZGET, 0x7fff060fe6ac)     = 0
lseek(3, 250059349504, SEEK_SET)        = 250059349504
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512
close(3)                                = 0
open("/dev/sda", O_RDONLY)              = 3
open("/dev/sda", O_RDONLY)              = 4
read(4, "\372\353!\1\264\1LILO\26\10\7DpL\0\0\0\0\326-\374K\3551\232\37\201\0\200`\6"..., 512) = 512
fadvise64(4, 0, 0, POSIX_FADV_RANDOM)   = 0
fstat(4, {st_mode=S_IFBLK|0660, st_rdev=makedev(8, 0), ...}) = 0
uname({sys="Linux", node="tux", ...})   = 0
ioctl(4, BLKGETSIZE64, 0x61e110)        = 0
ioctl(4, CDROM_GET_CAPABILITY or SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, 0) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(4, 0x127a, 0x7fff060fe66c)        = 0
ioctl(4, 0x1278, 0x7fff060fe66c)        = 0
ioctl(4, 0x1279, 0x7fff060fe66c)        = 0
ioctl(4, 0x127b, 0x7fff060fe66c)        = 0
ioctl(4, BLKSSZGET, 0x61e128)           = 0
ioctl(4, BLKSSZGET, 0x7fff060fe72c)     = 0
fstat(4, {st_mode=S_IFBLK|0660, st_rdev=makedev(8, 0), ...}) = 0
ioctl(4, 0x301, 0x7fff060fe710)         = 0
ioctl(4, BLKGETSIZE64, 0x7fff060fe6f8)  = 0
lseek(4, 0, SEEK_SET)                   = 0
read(4, "\372\353!\1\264\1LILO\26\10\7DpL\0\0\0\0\326-\374K\3551\232\37\201\0\200`\6"..., 8192) = 8192
open("/home/fidori/utiltmp/share/locale/en_US/LC_MESSAGES/util-linux-ng.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/fidori/utiltmp/share/locale/en/LC_MESSAGES/util-linux-ng.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff8d6d13000
write(1, "\n"..., 1)                    = 1
write(1, "Disk /dev/sda: 250.1 GB, 25005935"..., 44) = 44
write(1, "255 heads, 63 sectors/track, 3040"..., 70) = 70
write(1, "Units = sectors of 1 * 512 = 512 "..., 39) = 39
write(1, "Sector size (logical/physical): 5"..., 54) = 54
write(1, "I/O size (minimum/optimal): 512 b"..., 50) = 50
write(1, "Disk identifier: 0x1f9a31ed\n"..., 28) = 28
write(1, "\n"..., 1)                    = 1
write(1, "   Device Boot      Start        "..., 63) = 63
write(1, "/dev/sda1              63    1955"..., 62) = 62
write(1, "/dev/sda2        19551105   48839"..., 62) = 62
close(4)                                = 0
exit_group(0)

The first open()/close() pair is executed in gpt_warning().  After that, the
file /dev/sda is opened two times, giving file descriptors 3 and 4.  Descriptor
4 is handled and is closed at the end, but descriptor 3 is never used or
closed.  The open() call giving descriptor 3 is executed in function try().
The function try() calls get_boot(), which opens the file again, giving
descriptor 4.

Here is a patch to avoid opening the file in get_boot() if it was called with
the argument try_only.  This should not break any existing functionality,
because the only place where get_boot() is called with argument try_only is in
funtion try(), and only in case the file was already opened.


>From dc3dc7bed9691fb7aec17a135904f231bc3c0adc Mon Sep 17 00:00:00 2001
From: Markus Rinne <markus.ka.rinne@xxxxxxxxx>
Date: Tue, 21 Sep 2010 20:02:26 +0300
Subject: [PATCH] fdisk: eliminate redundant call to open()

Don't use open() in get_boot() if it's called with an argument try_only,
because the file has already been opened by the caller.

Signed-off-by: Markus Rinne <markus.ka.rinne@xxxxxxxxx>
---
 fdisk/fdisk.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index 6829321..1f841d1 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -1213,14 +1213,14 @@ get_boot(enum action what) {
 	if (what == create_empty_dos)
 		goto got_dos_table;		/* skip reading disk */
 
-	if ((fd = open(disk_device, type_open)) < 0) {
-	    if ((fd = open(disk_device, O_RDONLY)) < 0) {
-		if (what == try_only)
-		    return 1;
-		fatal(unable_to_open);
-	    } else
-		printf(_("You will not be able to write "
-			 "the partition table.\n"));
+	if (what != try_only) {
+		if ((fd = open(disk_device, type_open)) < 0) {
+			if ((fd = open(disk_device, O_RDONLY)) < 0)
+				fatal(unable_to_open);
+			else
+				printf(_("You will not be able to write "
+					    "the partition table.\n"));
+		}
 	}
 
 	if (512 != read(fd, MBRbuffer, 512)) {
-- 
1.7.1

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


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux