[asterisk-commits] tilghman: branch 1.6.2 r256483 - /branches/1.6.2/main/app.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 8 17:03:02 CDT 2010


Author: tilghman
Date: Thu Apr  8 17:03:00 2010
New Revision: 256483

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=256483
Log:
Backport /proc/%d/fd method of closing file descriptors to 1.6.2.

Modified:
    branches/1.6.2/main/app.c

Modified: branches/1.6.2/main/app.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.2/main/app.c?view=diff&rev=256483&r1=256482&r2=256483
==============================================================================
--- branches/1.6.2/main/app.c (original)
+++ branches/1.6.2/main/app.c Thu Apr  8 17:03:00 2010
@@ -36,6 +36,10 @@
 #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 @@
 #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
 }
 




More information about the asterisk-commits mailing list