2 * Copyright (c) 2004-2005, 2007, 2010, 2012-2014
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 // #include <config.h>
20 #include <sys/types.h>
30 #endif /* STDC_HEADERS */
33 #ifdef HAVE_PSTAT_GETPROC
34 # include <sys/param.h>
35 # include <sys/pstat.h>
39 # define NAMLEN(dirent) strlen((dirent)->d_name)
41 # define dirent direct
42 # define NAMLEN(dirent) (dirent)->d_namlen
43 # ifdef HAVE_SYS_NDIR_H
44 # include <sys/ndir.h>
46 # ifdef HAVE_SYS_DIR_H
59 #if defined(HAVE_FCNTL_CLOSEM) && !defined(HAVE_DIRFD)
60 # define closefrom closefrom_fallback
64 closefrom_close(int fd)
67 /* Avoid potential libdispatch crash when we close its fds. */
68 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
75 * Close all file descriptors greater than or equal to lowfd.
76 * This is the expensive (fallback) method.
79 closefrom_fallback(int lowfd)
84 * Fall back on sysconf() or getdtablesize(). We avoid checking
85 * resource limits since it is possible to open a file descriptor
86 * and then drop the rlimit such that it is below the open fd.
89 maxfd = sysconf(_SC_OPEN_MAX);
91 maxfd = getdtablesize();
92 #endif /* HAVE_SYSCONF */
96 for (fd = lowfd; fd < maxfd; fd++)
101 * Close all file descriptors greater than or equal to lowfd.
102 * We try the fast way first, falling back on the slow method.
104 #if defined(HAVE_FCNTL_CLOSEM)
108 if (fcntl(lowfd, F_CLOSEM, 0) == -1)
109 closefrom_fallback(lowfd);
111 #elif defined(HAVE_PSTAT_GETPROC)
115 struct pst_status pstat;
118 if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1) {
119 for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
122 closefrom_fallback(lowfd);
125 #elif defined(HAVE_DIRFD)
127 closefrom_procfs(int lowfd)
132 int *fd_array = NULL;
133 int fd_array_used = 0;
134 int fd_array_size = 0;
138 /* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */
139 # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
142 path = "/proc/self/fd";
144 dirp = opendir(path);
148 while ((dent = readdir(dirp)) != NULL) {
152 fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
153 if (errstr != NULL || fd == dirfd(dirp))
156 if (fd_array_used >= fd_array_size) {
159 if (fd_array_size > 0)
164 ptr = reallocarray(fd_array, fd_array_size, sizeof(int));
172 fd_array[fd_array_used++] = fd;
175 for (i = 0; i < fd_array_used; i++)
176 closefrom_close(fd_array[i]);
179 (void)closedir(dirp);
187 if (closefrom_procfs(lowfd) == 0)
190 closefrom_fallback(lowfd);
192 #endif /* HAVE_FCNTL_CLOSEM */