From f70a80b840e2ee10eb8fd304298f3f5e8c5620a4 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 31 Dec 2015 01:50:50 +0100 Subject: fix DAEMONISE_KEEP_FDS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/unistd/daemonise.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/unistd/daemonise.c b/src/unistd/daemonise.c index e7ae471..c305542 100644 --- a/src/unistd/daemonise.c +++ b/src/unistd/daemonise.c @@ -226,9 +226,10 @@ int daemonise(const char* name, int flags, ...) if (flags & DAEMONISE_KEEP_FDS) { va_start(args, flags); - while (va_arg(args, int) >= 0) + while ((fd = va_arg(args, int)) >= 0) if ((fd > 2) && (keepmax < fd)) keepmax = fd; + fd = -1; va_end(args); keep = calloc((size_t)keepmax + 1, sizeof(char)); t (keep == NULL); @@ -246,8 +247,8 @@ int daemonise(const char* name, int flags, ...) keep[fd] = 1; break; } - va_end(args); fd = -1; + va_end(args); } /* We assume that the maximum file descriptor is not extremely large. * We also assume the number of file descriptors too keep is very small, @@ -261,7 +262,7 @@ int daemonise(const char* name, int flags, ...) for (i = 3; (rlim_t)i < rlimit.rlim_cur; i++) /* File descriptors with numbers above and including * `rlimit.rlim_cur` cannot be created. They cause EBADF. */ - if ((keep == NULL) || (keep[i] == 0)) + if ((i > keepmax) || (keep[i] == 0)) close(i); } free(keep), keep = NULL; -- cgit v1.2.3-70-g09d2