1 Subject: [PATCH] Do not close fds on OS X
3 The OS X version of sudo sets the close-on-exec flag for file descriptors
4 to be closed rather than actually closing them. It uses an opendir to
5 /dev/fd instead of /proc/self/fd to enumerate the open fd values if
6 possible rather than blindly trying them all.
8 closefrom.c | 12 ++++++++++--
9 1 file changed, 10 insertions(+), 2 deletions(-)
11 diff --git a/closefrom.c b/closefrom.c
12 index 68da392d..514311e5 100644
15 @@ -79,7 +79,11 @@ closefrom_fallback(lowfd)
18 for (fd = lowfd; fd < maxfd; fd++)
20 + (void) fcntl((int) fd, F_SETFD, 1);
22 (void) close((int) fd);
27 @@ -105,13 +109,17 @@ closefrom(lowfd)
31 - /* Use /proc/self/fd directory if it exists. */
32 - if ((dirp = opendir("/proc/self/fd")) != NULL) {
33 + /* Use /dev/fd directory if it exists. */
34 + if ((dirp = opendir("/dev/fd")) != NULL) {
35 while ((dent = readdir(dirp)) != NULL) {
36 fd = strtol(dent->d_name, &endp, 10);
37 if (dent->d_name != endp && *endp == '\0' &&
38 fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
40 + (void) fcntl((int) fd, F_SETFD, 1);
42 (void) close((int) fd);
45 (void) closedir(dirp);