debuggers.hg

changeset 22831:3a89585d77b1

xend: pci.py: fix open file descriptor leak

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>
author Kouya Shimura <kouya@jp.fujitsu.com>
date Thu Jan 20 16:41:23 2011 +0000 (2011-01-20)
parents 0592d6ca9177
children 6ee4b87d1863
files tools/python/xen/util/pci.py
line diff
     1.1 --- a/tools/python/xen/util/pci.py	Thu Jan 20 16:32:33 2011 +0000
     1.2 +++ b/tools/python/xen/util/pci.py	Thu Jan 20 16:41:23 2011 +0000
     1.3 @@ -922,10 +922,12 @@ class PciDevice:
     1.4          pos = PCI_CAPABILITY_LIST
     1.5  
     1.6          try:
     1.7 +            fd = None
     1.8              fd = os.open(path, os.O_RDONLY)
     1.9              os.lseek(fd, PCI_STATUS, 0)
    1.10              status = struct.unpack('H', os.read(fd, 2))[0]
    1.11              if (status & 0x10) == 0:
    1.12 +                os.close(fd)
    1.13                  # The device doesn't support PCI_STATUS_CAP_LIST
    1.14                  return 0
    1.15  
    1.16 @@ -952,6 +954,8 @@ class PciDevice:
    1.17  
    1.18              os.close(fd)
    1.19          except OSError, (errno, strerr):
    1.20 +            if fd is not None:
    1.21 +                os.close(fd)
    1.22              raise PciDeviceParseError(('Error when accessing sysfs: %s (%d)' %
    1.23                  (strerr, errno)))
    1.24          return pos