|
Lines 46-51
Link Here
|
| 46 |
# include <ndir.h> |
46 |
# include <ndir.h> |
| 47 |
# endif |
47 |
# endif |
| 48 |
#endif |
48 |
#endif |
|
|
49 |
#if !defined(HAVE_FCNTL_CLOSEM) && \ |
| 50 |
defined(HAVE_LIBPROC_H) && \ |
| 51 |
HAVE_DECL_PROC_PIDINFO |
| 52 |
#include <libproc.h> |
| 53 |
#endif |
| 49 |
|
54 |
|
| 50 |
#ifndef OPEN_MAX |
55 |
#ifndef OPEN_MAX |
| 51 |
# define OPEN_MAX 256 |
56 |
# define OPEN_MAX 256 |
|
Lines 55-75
Link Here
|
| 55 |
__unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $"; |
60 |
__unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $"; |
| 56 |
#endif /* lint */ |
61 |
#endif /* lint */ |
| 57 |
|
62 |
|
|
|
63 |
#ifndef HAVE_FCNTL_CLOSEM |
| 58 |
/* |
64 |
/* |
| 59 |
* Close all file descriptors greater than or equal to lowfd. |
65 |
* Close all file descriptors greater than or equal to lowfd. |
| 60 |
*/ |
66 |
*/ |
|
|
67 |
static void |
| 68 |
closefrom_fallback(int lowfd) |
| 69 |
{ |
| 70 |
long fd, maxfd; |
| 71 |
|
| 72 |
/* |
| 73 |
* Fall back on sysconf() or getdtablesize(). We avoid checking |
| 74 |
* resource limits since it is possible to open a file descriptor |
| 75 |
* and then drop the rlimit such that it is below the open fd. |
| 76 |
*/ |
| 77 |
#ifdef HAVE_SYSCONF |
| 78 |
maxfd = sysconf(_SC_OPEN_MAX); |
| 79 |
#else |
| 80 |
maxfd = getdtablesize(); |
| 81 |
#endif /* HAVE_SYSCONF */ |
| 82 |
if (maxfd < 0) |
| 83 |
maxfd = OPEN_MAX; |
| 84 |
|
| 85 |
for (fd = lowfd; fd < maxfd; fd++) |
| 86 |
(void) close((int) fd); |
| 87 |
} |
| 88 |
#endif /* HAVE_FCNTL_CLOSEM */ |
| 89 |
|
| 61 |
#ifdef HAVE_FCNTL_CLOSEM |
90 |
#ifdef HAVE_FCNTL_CLOSEM |
| 62 |
void |
91 |
void |
| 63 |
closefrom(int lowfd) |
92 |
closefrom(int lowfd) |
| 64 |
{ |
93 |
{ |
| 65 |
(void) fcntl(lowfd, F_CLOSEM, 0); |
94 |
(void) fcntl(lowfd, F_CLOSEM, 0); |
| 66 |
} |
95 |
} |
| 67 |
#else |
96 |
#elif defined(HAVE_LIBPROC_H) && HAVE_DECL_PROC_PIDINFO |
| 68 |
void |
97 |
void |
| 69 |
closefrom(int lowfd) |
98 |
closefrom(int lowfd) |
| 70 |
{ |
99 |
{ |
| 71 |
long fd, maxfd; |
100 |
int bufsize, try, i, n, fd; |
| 72 |
#if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID) |
101 |
pid_t pid = getpid(); |
|
|
102 |
struct proc_fdinfo *fdinfo_buf = NULL; |
| 103 |
|
| 104 |
/* |
| 105 |
* Obtain map of open fds. |
| 106 |
* |
| 107 |
* Be prepared to retry a few times in case the table size changes |
| 108 |
* between the size query and the actual attempt to fetch it. |
| 109 |
*/ |
| 110 |
for (try = 0; try < 3; try++) { |
| 111 |
/* Obtain size */ |
| 112 |
bufsize = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
| 113 |
if (bufsize == 0) |
| 114 |
return; /* no fds, really? */ |
| 115 |
else if (bufsize == -1) |
| 116 |
break; |
| 117 |
if ((fdinfo_buf = malloc(bufsize)) == NULL) |
| 118 |
break; |
| 119 |
if ((bufsize = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, |
| 120 |
fdinfo_buf, bufsize)) == NULL) { |
| 121 |
/* table size changed? retry... */ |
| 122 |
free(fdinfo_buf); |
| 123 |
fdinfo_buf = NULL; |
| 124 |
} |
| 125 |
break; |
| 126 |
} |
| 127 |
/* If unable to obtain the fd map, then fall back to brute force */ |
| 128 |
if (fdinfo_buf == NULL) |
| 129 |
closefrom_fallback(lowfd); |
| 130 |
/* Close fds from map */ |
| 131 |
for (i = 0, n = bufsize / PROC_PIDLISTFD_SIZE; i < n; ++i) { |
| 132 |
fd = fdinfo_buf[i].proc_fd; |
| 133 |
if (fd >= lowfd) close(fd); |
| 134 |
} |
| 135 |
free(fdinfo_buf); |
| 136 |
} |
| 137 |
#elif defined(HAVE_DIRFD) && defined(HAVE_PROC_PID) |
| 138 |
void |
| 139 |
closefrom(int lowfd) |
| 140 |
{ |
| 141 |
long fd; |
| 73 |
char fdpath[PATH_MAX], *endp; |
142 |
char fdpath[PATH_MAX], *endp; |
| 74 |
struct dirent *dent; |
143 |
struct dirent *dent; |
| 75 |
DIR *dirp; |
144 |
DIR *dirp; |
|
Lines 85-109
closefrom(int lowfd)
Link Here
|
| 85 |
(void) close((int) fd); |
154 |
(void) close((int) fd); |
| 86 |
} |
155 |
} |
| 87 |
(void) closedir(dirp); |
156 |
(void) closedir(dirp); |
| 88 |
} else |
157 |
return; |
| 89 |
#endif |
|
|
| 90 |
{ |
| 91 |
/* |
| 92 |
* Fall back on sysconf() or getdtablesize(). We avoid checking |
| 93 |
* resource limits since it is possible to open a file descriptor |
| 94 |
* and then drop the rlimit such that it is below the open fd. |
| 95 |
*/ |
| 96 |
#ifdef HAVE_SYSCONF |
| 97 |
maxfd = sysconf(_SC_OPEN_MAX); |
| 98 |
#else |
| 99 |
maxfd = getdtablesize(); |
| 100 |
#endif /* HAVE_SYSCONF */ |
| 101 |
if (maxfd < 0) |
| 102 |
maxfd = OPEN_MAX; |
| 103 |
|
| 104 |
for (fd = lowfd; fd < maxfd; fd++) |
| 105 |
(void) close((int) fd); |
| 106 |
} |
158 |
} |
|
|
159 |
/* /proc/$$/fd strategy failed, fall back to brute force closure */ |
| 160 |
closefrom_fallback(lowfd); |
| 161 |
} |
| 162 |
#else |
| 163 |
void |
| 164 |
closefrom(int lowfd) |
| 165 |
{ |
| 166 |
closefrom_fallback(lowfd); |
| 107 |
} |
167 |
} |
| 108 |
#endif /* !HAVE_FCNTL_CLOSEM */ |
168 |
#endif /* !HAVE_FCNTL_CLOSEM */ |
| 109 |
#endif /* HAVE_CLOSEFROM */ |
169 |
#endif /* HAVE_CLOSEFROM */ |