From c38bf687bbcffe29c64e881ee078f95fbe5ebe34 Mon Sep 17 00:00:00 2001
From: Markus Armbruster <armbru@redhat.com>
Date: Wed, 18 Jan 2012 10:38:38 +0100
Subject: [PATCH 44/52] os-posix: Plug fd leak in qemu_create_pidfile()

RH-Author: Markus Armbruster <armbru@redhat.com>
Message-id: <1326883126-22053-45-git-send-email-armbru@redhat.com>
Patchwork-id: 36605
O-Subject: [RHEL-6.3 PATCH qemu-kvm 44/52] os-posix: Plug fd leak in qemu_create_pidfile()
Bugzilla: 758194
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Amit Shah <amit.shah@redhat.com>

Spotted by Coverity.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
(cherry picked from commit 1bbd1592c89a433e1a0501d582652d54d367ca53)

Conflicts:

	os-posix.c
---
 osdep.c |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 osdep.c |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/osdep.c b/osdep.c
index cd82f8f..81a82a6 100644
--- a/osdep.c
+++ b/osdep.c
@@ -149,15 +149,20 @@ int qemu_create_pidfile(const char *filename)
     int fd;
 
     fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
-    if (fd == -1)
+    if (fd == -1) {
         return -1;
-
-    if (lockf(fd, F_TLOCK, 0) == -1)
-        return -1;
-
+    }
+    if (lockf(fd, F_TLOCK, 0) == -1) {
+        close(fd);
+       return -1;
+    }
     len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
-    if (write(fd, buffer, len) != len)
+    if (write(fd, buffer, len) != len) {
+        close(fd);
         return -1;
+    }
+
+    close(fd);
 #else
     HANDLE file;
     DWORD flags;
-- 
1.7.7.5

