portable: release 0.90
[got-portable.git] / compat / closefrom.c
blobe024679deee4fc0821648c0095946edd4c4ab55e
1 /*
2 * Copyright (c) 2004-2005 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef HAVE_CLOSEFROM
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #ifdef HAVE_FCNTL_H
24 # include <fcntl.h>
25 #endif
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <string.h>
30 #include <unistd.h>
31 #ifdef HAVE_DIRENT_H
32 # include <dirent.h>
33 # define NAMLEN(dirent) strlen((dirent)->d_name)
34 #else
35 # define dirent direct
36 # define NAMLEN(dirent) (dirent)->d_namlen
37 # ifdef HAVE_SYS_NDIR_H
38 # include <sys/ndir.h>
39 # endif
40 # ifdef HAVE_SYS_DIR_H
41 # include <sys/dir.h>
42 # endif
43 # ifdef HAVE_NDIR_H
44 # include <ndir.h>
45 # endif
46 #endif
48 #ifndef OPEN_MAX
49 # define OPEN_MAX 256
50 #endif
52 #if 0
53 __unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $";
54 #endif /* lint */
57 * Close all file descriptors greater than or equal to lowfd.
59 #ifdef HAVE_FCNTL_CLOSEM
60 void
61 closefrom(int lowfd)
63 (void) fcntl(lowfd, F_CLOSEM, 0);
65 #else
66 void
67 closefrom(int lowfd)
69 long fd, maxfd;
70 #if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
71 char fdpath[PATH_MAX], *endp;
72 struct dirent *dent;
73 DIR *dirp;
74 int len;
76 /* Check for a /proc/$$/fd directory. */
77 len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
78 if (len > 0 && (size_t)len <= sizeof(fdpath) && (dirp = opendir(fdpath))) {
79 while ((dent = readdir(dirp)) != NULL) {
80 fd = strtol(dent->d_name, &endp, 10);
81 if (dent->d_name != endp && *endp == '\0' &&
82 fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
83 (void) close((int) fd);
85 (void) closedir(dirp);
86 } else
87 #endif
90 * Fall back on sysconf() or getdtablesize(). We avoid checking
91 * resource limits since it is possible to open a file descriptor
92 * and then drop the rlimit such that it is below the open fd.
94 #ifdef HAVE_SYSCONF
95 maxfd = sysconf(_SC_OPEN_MAX);
96 #else
97 maxfd = getdtablesize();
98 #endif /* HAVE_SYSCONF */
99 if (maxfd < 0)
100 maxfd = OPEN_MAX;
102 for (fd = lowfd; fd < maxfd; fd++)
103 (void) close((int) fd);
106 #endif /* !HAVE_FCNTL_CLOSEM */
107 #endif /* HAVE_CLOSEFROM */