aboutsummaryrefslogtreecommitdiffstats
path: root/main/app.c
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2010-04-08 22:03:00 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2010-04-08 22:03:00 +0000
commit5db8e96ebeb4ed4889689e4da0ff70d14b470cd8 (patch)
tree03aef995a13619ade4c4b52483565d396eb3e544 /main/app.c
parentc4cc60d6da228b5d2f7cefa6e34649b09cf46fb2 (diff)
Backport /proc/%d/fd method of closing file descriptors to 1.6.2.
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.6.2@256483 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/app.c')
-rw-r--r--main/app.c47
1 files changed, 34 insertions, 13 deletions
diff --git a/main/app.c b/main/app.c
index 2aa76edef..eb26a9bab 100644
--- a/main/app.c
+++ b/main/app.c
@@ -36,6 +36,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <sys/time.h> /* for getrlimit(2) */
#include <sys/resource.h> /* for getrlimit(2) */
#include <stdlib.h> /* for closefrom(3) */
+#ifndef HAVE_CLOSEFROM
+#include <sys/types.h>
+#include <dirent.h> /* for opendir(3) */
+#endif
#ifdef HAVE_CAP
#include <sys/capability.h>
#endif /* HAVE_CAP */
@@ -1984,20 +1988,37 @@ void ast_close_fds_above_n(int n)
#ifdef HAVE_CLOSEFROM
closefrom(n + 1);
#else
- int x, null;
+ long x, null;
struct rlimit rl;
- getrlimit(RLIMIT_NOFILE, &rl);
- null = open("/dev/null", O_RDONLY);
- for (x = n + 1; x < rl.rlim_cur; 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. */
- while (dup2(null, x) < 0 && errno == EINTR);
- close(x);
- }
- }
- close(null);
+ 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);
+ null = open("/dev/null", O_RDONLY);
+ for (x = n + 1; x < rl.rlim_cur; 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. */
+ while (dup2(null, x) < 0 && errno == EINTR);
+ close(x);
+ }
+ }
+ close(null);
+ }
#endif
}