From fd3cb908415b6ac1018b9ff9a78eb9105fab68df Mon Sep 17 00:00:00 2001 From: Tilghman Lesher Date: Mon, 12 Jan 2009 23:06:12 +0000 Subject: Some platforms (notably, the BSDs) have a more efficient implementation called closefrom(3). git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@168522 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- main/app.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'main/app.c') diff --git a/main/app.c b/main/app.c index d3d3b3784..bda021d56 100644 --- a/main/app.c +++ b/main/app.c @@ -33,6 +33,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include #include /* added this to allow to compile, sorry! */ #include +#include /* for getrlimit(2) */ +#include /* for getrlimit(2) */ +#include /* for closefrom(3) */ #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */ #include "asterisk/channel.h" @@ -1839,18 +1842,24 @@ int ast_get_encoded_str(const char *stream, char *result, size_t result_size) void ast_close_fds_above_n(int n) { +#ifdef HAVE_CLOSEFROM + closefrom(n + 1); +#else int x, null; + struct rlimit rl; + getrlimit(RLIMIT_NOFILE, &rl); null = open("/dev/null", O_RDONLY); - for (x = n + 1; x <= (null >= 8192 ? null : 8192); x++) { + for (x = n + 1; x < rl.rlim_max; x++) { if (x != null) { /* Side effect of dup2 is that it closes any existing fd without error. * This prevents valgrind and other debugging tools from sending up * false error reports. */ - dup2(null, x); + while (dup2(null, x) < 0 && errno == EINTR); close(x); } } close(null); +#endif } int ast_safe_fork(int stop_reaper) -- cgit v1.2.3