From 7c77f7803fc3ab41abd294d6b1cdc9f18aeea8b8 Mon Sep 17 00:00:00 2001 From: tilghman Date: Sun, 6 Dec 2009 07:01:06 +0000 Subject: Move implementation of closefrom(3) from app.c to strcompat.c git-svn-id: http://svn.digium.com/svn/asterisk/trunk@233358 f38db490-d61c-443f-a65b-d21fe96a405b --- main/strcompat.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'main/strcompat.c') diff --git a/main/strcompat.c b/main/strcompat.c index ff123713e..b1d8b0d1d 100644 --- a/main/strcompat.c +++ b/main/strcompat.c @@ -22,6 +22,10 @@ #include "asterisk.h" #include +#include /* for getrlimit(2) */ +#include /* for getrlimit(2) */ +#include /* for opendir(3) */ +#include /* for opendir(3) */ #ifndef HAVE_STRSEP char *strsep(char **str, const char *delims) @@ -399,3 +403,33 @@ int ffsll(long long n) } #endif +#ifndef HAVE_CLOSEFROM +void closefrom(int n) +{ + long x; + struct rlimit rl; + DIR *dir; + char path[16], *result; + struct dirent *entry; + + snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid()); + if ((dir = opendir(path))) { + while ((entry = readdir(dir))) { + /* Skip . and .. */ + if (entry->d_name[0] == '.') { + continue; + } + if ((x = strtol(entry->d_name, &result, 10)) && x >= n) { + close(x); + } + } + closedir(dir); + } else { + getrlimit(RLIMIT_NOFILE, &rl); + for (x = n; x < rl.rlim_cur; x++) { + close(x); + } + } +} +#endif + -- cgit v1.2.3