From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matt Wilson Subject: [PATCH] xend: fix file descriptor leak in pci utilities Date: Fri, 30 Aug 2013 12:21:56 -0700 Message-ID: <1377890516-3235-1-git-send-email-msw@linux.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VFUGu-0005TT-VF for xen-devel@lists.xenproject.org; Fri, 30 Aug 2013 19:22:17 +0000 Received: by mail-pa0-f51.google.com with SMTP id lf1so2703875pab.24 for ; Fri, 30 Aug 2013 12:22:13 -0700 (PDT) List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: Ian Campbell , Ian Jackson , Xi Xiong , Matt Wilson , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org From: Xi Xiong A file descriptor leak was detected after creating multiple domUs with pass-through PCI devices. This patch fixes the issue. Signed-off-by: Xi Xiong Reviewed-by: Matt Wilson [msw: adjusted commit message] Cc: Ian Jackson Cc: Stefano Stabellini Cc: Ian Campbell Signed-off-by: Matt Wilson --- tools/python/xen/util/pci.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/python/xen/util/pci.py b/tools/python/xen/util/pci.py index 98bea1a..792fb69 100644 --- a/tools/python/xen/util/pci.py +++ b/tools/python/xen/util/pci.py @@ -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 -- 1.7.9.5