From: Alex Jia <ajia(a)redhat.com>
The codes hasn't close a pipe decriptor statuspipe[0] before exiting,
moreover, should also close all of opening fds on error path to
avoid fd leaks.
In addition, I think other fds leak doesen't belong to libvirtd,
so this patch hasn't fixed them.
Detected by valgrind. Leaks introduced in commit 4296cea.
* daemon/libvirtd.c: fix fd leak on libvirt daemon.
* How to reproduce?
% service libvirtd stop
% valgrind -v --track-fds=yes /usr/sbin/libvirtd --daemon
* Actual valgrind result:
==16804== FILE DESCRIPTORS: 7 open at exit.
==16804== Open file descriptor 7:
==16804== at 0x321FAD8B87: pipe (in /lib64/libc-2.12.so)
==16804== by 0x41F34D: daemonForkIntoBackground (libvirtd.c:186)
==16804== by 0x4207A0: main (libvirtd.c:1420)
Signed-off-by: Alex Jia <ajia(a)redhat.com>
---
daemon/libvirtd.c | 14 ++++++++++----
1 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index d7a03d7..b05f126 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -184,7 +184,7 @@ static int daemonForkIntoBackground(const char *argv0)
{
int statuspipe[2];
if (pipe(statuspipe) < 0)
- return -1;
+ goto error;
int pid = fork();
switch (pid) {
@@ -219,7 +219,7 @@ static int daemonForkIntoBackground(const char *argv0)
case 0:
return statuspipe[1];
case -1:
- return -1;
+ goto error;
default:
_exit(0);
}
@@ -232,7 +232,7 @@ static int daemonForkIntoBackground(const char *argv0)
}
case -1:
- return -1;
+ goto error;
default:
{
@@ -243,7 +243,7 @@ static int daemonForkIntoBackground(const char *argv0)
/* We wait to make sure the first child forked successfully */
if (virPidWait(pid, NULL) < 0)
- return -1;
+ goto error;
/* Now block until the second child initializes successfully */
again:
@@ -257,9 +257,15 @@ static int daemonForkIntoBackground(const char *argv0)
"--daemon for more info.\n"), argv0,
virDaemonErrTypeToString(status));
}
+ VIR_FORCE_CLOSE(statuspipe[0]);
_exit(ret == 1 && status == 0 ? 0 : 1);
}
}
+
+error:
+ VIR_FORCE_CLOSE(statuspipe[0]);
+ VIR_FORCE_CLOSE(statuspipe[1]);
+ return -1;
}
--
1.7.1