Skip to contentSkip to conversation

xend: fix file descriptor leak in pci utilities

PATCH New Matt Wilson 1 participant 1 message Aug 30, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xend: fix file descriptor leak in pci utilities

From: Xi Xiong <[email protected]>

A file descriptor leak was detected after creating multiple domUs with pass-through PCI devices. This patch fixes the issue.

[msw: adjusted commit message]
Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/python/xen/util/pci.py |    6 ++++++
 1 file changed, 6 insertions(+)
tools/python/xen/util/pci.py +6 -0
index 98bea1a..792fb69 100644
@@ -968,18 +968,22 @@ class PciDevice:
         ttl = 480; # 3840 bytes, minimum 8 bytes per capability
         pos = 0x100
 
+        fd = None
         try:
             fd = os.open(path, os.O_RDONLY)
             os.lseek(fd, pos, 0)
             h = os.read(fd, 4)
             if len(h) == 0: # MMCONF is not enabled?
+                os.close(fd)
                 return 0
             header = struct.unpack('I', h)[0]
             if header == 0 or header == -1:
+                os.close(fd)
                 return 0
 
             while ttl > 0:
                 if (header & 0x0000ffff) == cap:
+                    os.close(fd)
                     return pos
                 pos = (header >> 20) & 0xffc
                 if pos < 0x100:
@@ -989,6 +993,8 @@ class PciDevice:
                 ttl = ttl - 1
             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 0
Signature · 1 line
1.7.9.5