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