From b5e17ec1d828e07639e9c3db5aa30a70be7ae4d4 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 2 Nov 2011 18:31:00 +0100 Subject: [PATCH] Rewrite SetupLogging to not leak the fd. Thanks to Steve Grubb for advice on this. --- src/util-daemon.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/util-daemon.c b/src/util-daemon.c index 1606cde64f..e8b0890616 100644 --- a/src/util-daemon.c +++ b/src/util-daemon.c @@ -79,18 +79,23 @@ static void WaitForChild (pid_t pid) { * \brief Close stdin, stdout, stderr.Redirect logging info to syslog * */ -static void SetupLogging () { - int fd0, fd1, fd2; - +static void SetupLogging (void) { /* Close stdin, stdout, stderr */ close(0); close(1); close(2); /* Redirect stdin, stdout, stderr to /dev/null */ - fd0 = open("/dev/null", O_RDWR); - fd1 = dup(0); - fd2 = dup(0); + int fd = open("/dev/null", O_RDWR); + if (fd < 0) + return; + if (dup2(fd, 0) < 0) + return; + if (dup2(fd, 1) < 0) + return; + if (dup2(fd, 2) < 0) + return; + close(fd); } /**