diff options
Diffstat (limited to 'libasyncns')
-rw-r--r-- | libasyncns/asyncns.c | 23 |
1 files 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; |