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 #include <sys/types.h>
34 #ifdef HAVE_PSTAT_GETPROC
35 # include <sys/pstat.h>
42 #ifndef _POSIX_OPEN_MAX
43 # define _POSIX_OPEN_MAX 20
47 * Close all file descriptors greater than or equal to lowfd.
48 * This is the expensive (fallback) method.
51 closefrom_fallback(int lowfd)
56 * Fall back on sysconf(_SC_OPEN_MAX). We avoid checking
57 * resource limits since it is possible to open a file descriptor
58 * and then drop the rlimit such that it is below the open fd.
60 maxfd = sysconf(_SC_OPEN_MAX);
62 maxfd = _POSIX_OPEN_MAX;
64 for (fd = lowfd; fd < maxfd; fd++) {
66 /* Avoid potential libdispatch crash when we close its fds. */
67 (void) fcntl((int) fd, F_SETFD, FD_CLOEXEC);
69 (void) close((int) fd);
75 * Close all file descriptors greater than or equal to lowfd.
76 * We try the fast way first, falling back on the slow method.
81 #if defined(HAVE_PSTAT_GETPROC)
82 struct pst_status pstat;
83 #elif defined(HAVE_DIRFD)
88 /* Try the fast method first, if possible. */
89 #if defined(HAVE_FCNTL_CLOSEM)
90 if (fcntl(lowfd, F_CLOSEM, 0) != -1)
93 #if defined(HAVE_PSTAT_GETPROC)
95 * EOVERFLOW is not a fatal error for the fields we use.
96 * See the "EOVERFLOW Error" section of pstat_getvminfo(3).
98 if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1 ||
102 for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
106 #elif defined(HAVE_DIRFD)
107 /* Use /proc/self/fd (or /dev/fd on macOS) if it exists. */
109 path = _PATH_DEV "fd";
111 path = "/proc/self/fd";
113 if ((dirp = opendir(path)) != NULL) {
115 while ((dent = readdir(dirp)) != NULL) {
117 int fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
118 if (errstr == NULL && fd != dirfd(dirp)) {
120 /* Avoid potential libdispatch crash when we close its fds. */
121 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
127 (void) closedir(dirp);
130 #endif /* HAVE_DIRFD */
132 /* Do things the slow way. */
133 closefrom_fallback(lowfd);