]> xenbits.xensource.com Git - xen.git/commitdiff
xend: pci.py: fix open file descriptor leak
authorKouya Shimura <kouya@jp.fujitsu.com>
Thu, 20 Jan 2011 16:41:23 +0000 (16:41 +0000)
committerKouya Shimura <kouya@jp.fujitsu.com>
Thu, 20 Jan 2011 16:41:23 +0000 (16:41 +0000)
I got the following error:
    $ xm pci-list-assignable-devices
    Error: [Errno 24] Too many open files

Signed-off-by: Kouya Shimura <kouya@jp.fujitsu.com>
tools/python/xen/util/pci.py

index 2fad278aa66478cb5c47217a3e61ba4c6d1b450d..b889d1816e4562b0f70a3dedc6508fdf54fa29ed 100644 (file)
@@ -922,10 +922,12 @@ class PciDevice:
         pos = PCI_CAPABILITY_LIST
 
         try:
+            fd = None
             fd = os.open(path, os.O_RDONLY)
             os.lseek(fd, PCI_STATUS, 0)
             status = struct.unpack('H', os.read(fd, 2))[0]
             if (status & 0x10) == 0:
+                os.close(fd)
                 # The device doesn't support PCI_STATUS_CAP_LIST
                 return 0
 
@@ -952,6 +954,8 @@ class PciDevice:
 
             os.close(fd)
         except OSError, (errno, strerr):
+            if fd is not None:
+                os.close(fd)
             raise PciDeviceParseError(('Error when accessing sysfs: %s (%d)' %
                 (strerr, errno)))
         return pos