From 81d74c9df801174012a2fb7aebc8569db85faf0c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 15 Oct 2009 03:33:57 +0200 Subject: fix loop exit in close_allv() --- libasyncns/asyncns.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/libasyncns/asyncns.c b/libasyncns/asyncns.c index 13a21f2..0161400 100644 --- a/libasyncns/asyncns.c +++ b/libasyncns/asyncns.c @@ -214,7 +214,7 @@ static char *strndup(const char *s, size_t l) { static int close_allv(const int except_fds[]) { struct rlimit rl; - int fd; + int fd, maxfd; #ifdef __linux__ @@ -284,18 +284,23 @@ static int close_allv(const int except_fds[]) { #endif - if (getrlimit(RLIMIT_NOFILE, &rl) < 0) - return -1; - - for (fd = 0; fd < (int) rl.rlim_max; fd++) { - int i; + if (getrlimit(RLIMIT_NOFILE, &rl) > 0) + maxfd = (int) rl.rlim_max; + else + maxfd = sysconf(_SC_OPEN_MAX); - if (fd <= 3) - continue; + for (fd = 3; fd < maxfd; fd++) { + int i, found; + found = 0; for (i = 0; except_fds[i] >= 0; i++) - if (except_fds[i] == fd) + if (except_fds[i] == fd) { + found = 1; continue; + } + + if (found) + continue; if (close(fd) < 0 && errno != EBADF) return -1; -- cgit