2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004-2005, 2007, 2010, 2012-2015, 2017-2018
5 * Todd C. Miller <Todd.Miller@sudo.ws>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
22 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
27 #ifndef HAVE_CLOSEFROM
29 #include <sys/types.h>
35 #ifdef HAVE_PSTAT_GETPROC
36 # include <sys/pstat.h>
43 #ifndef _POSIX_OPEN_MAX
44 # define _POSIX_OPEN_MAX 20
48 * Close all file descriptors greater than or equal to lowfd.
49 * This is the expensive (fallback) method.
52 closefrom_fallback(int lowfd)
57 * Fall back on sysconf(_SC_OPEN_MAX). We avoid checking
58 * resource limits since it is possible to open a file descriptor
59 * and then drop the rlimit such that it is below the open fd.
61 maxfd = sysconf(_SC_OPEN_MAX);
63 maxfd = _POSIX_OPEN_MAX;
65 for (fd = lowfd; fd < maxfd; fd++) {
67 /* Avoid potential libdispatch crash when we close its fds. */
68 (void) fcntl((int) fd, F_SETFD, FD_CLOEXEC);
70 (void) close((int) fd);
76 * Close all file descriptors greater than or equal to lowfd.
77 * We try the fast way first, falling back on the slow method.
82 #if defined(HAVE_PSTAT_GETPROC)
83 struct pst_status pstat;
84 #elif defined(HAVE_DIRFD)
89 /* Try the fast method first, if possible. */
90 #if defined(HAVE_FCNTL_CLOSEM)
91 if (fcntl(lowfd, F_CLOSEM, 0) != -1)
94 #if defined(HAVE_PSTAT_GETPROC)
96 * EOVERFLOW is not a fatal error for the fields we use.
97 * See the "EOVERFLOW Error" section of pstat_getvminfo(3).
99 if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1 ||
100 errno == EOVERFLOW) {
103 for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
107 #elif defined(HAVE_DIRFD)
108 /* Use /proc/self/fd (or /dev/fd on macOS) if it exists. */
110 path = _PATH_DEV "fd";
112 path = "/proc/self/fd";
114 if ((dirp = opendir(path)) != NULL) {
116 while ((dent = readdir(dirp)) != NULL) {
118 int fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
119 if (errstr == NULL && fd != dirfd(dirp)) {
121 /* Avoid potential libdispatch crash when we close its fds. */
122 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
128 (void) closedir(dirp);
131 #endif /* HAVE_DIRFD */
133 /* Do things the slow way. */
134 closefrom_fallback(lowfd);
137 #endif /* HAVE_CLOSEFROM */