|
24 | 24 | #include <libgen.h>
|
25 | 25 | #include <stdlib.h>
|
26 | 26 | #include <termios.h>
|
| 27 | +#include <sys/syscall.h> |
| 28 | +#include <dirent.h> |
| 29 | +#include <ctype.h> |
27 | 30 |
|
28 | 31 | /* from pfind.c */
|
29 | 32 | extern char *pfind(const char *name, char *const envp[]);
|
30 | 33 |
|
| 34 | +static int sys_close_range_wrapper(unsigned int from_fd_inclusive) { |
| 35 | + // Use fast `close_range` (https://man7.org/linux/man-pages/man2/close_range.2.html) if available. |
| 36 | + // Cannot call `close_range` from libc, as it may be unavailable in older libc. |
| 37 | +# if defined(__linux__) && defined(SYS_close_range) && defined(CLOSE_RANGE_UNSHARE) |
| 38 | + return syscall(SYS_close_range, from_fd_inclusive, ~0U, CLOSE_RANGE_UNSHARE); |
| 39 | +# else |
| 40 | + errno = ENOSYS; |
| 41 | + return -1; |
| 42 | +# endif |
| 43 | +} |
| 44 | + |
| 45 | +static int close_all_fds_using_parsing(unsigned int from_fd_inclusive) { |
| 46 | + // If `opendir` is implemented using a file descriptor, we may close it accidentally. |
| 47 | + // Let's close a few lowest file descriptors, in hope that `opendir` will use it. |
| 48 | + int lowest_fds_to_close = 2; |
| 49 | + for (int i = 0; i < lowest_fds_to_close; i++) { |
| 50 | + close(from_fd_inclusive + i); |
| 51 | + } |
| 52 | + |
| 53 | +#if defined(__APPLE__) |
| 54 | +#define FD_DIR "/dev/fd" |
| 55 | +#else |
| 56 | +#define FD_DIR "/proc/self/fd" |
| 57 | +#endif |
| 58 | + |
| 59 | + DIR *dirp = opendir(FD_DIR); |
| 60 | + if (dirp == NULL) return -1; |
| 61 | + |
| 62 | + struct dirent *direntp; |
| 63 | + |
| 64 | + while ((direntp = readdir(dirp)) != NULL) { |
| 65 | + if (isdigit(direntp->d_name[0])) { |
| 66 | + int fd = strtol(direntp->d_name, NULL, 10); |
| 67 | + if (fd >= from_fd_inclusive + lowest_fds_to_close && fd != dirfd(dirp)) { |
| 68 | + close(fd); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + closedir(dirp); |
| 74 | + |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +static void close_all_fds_fallback(unsigned int from_fd_inclusive) { |
| 79 | + int fdlimit = sysconf(_SC_OPEN_MAX); |
| 80 | + if (fdlimit == -1) fdlimit = 65535; // arbitrary default, just in case |
| 81 | + for (int fd = from_fd_inclusive; fd < fdlimit; fd++) { |
| 82 | + close(fd); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +static void close_all_fds() { |
| 87 | + unsigned int from_fd = STDERR_FILENO + 1; |
| 88 | + if (sys_close_range_wrapper(from_fd) == 0) return; |
| 89 | + if (close_all_fds_using_parsing(from_fd) == 0) return; |
| 90 | + close_all_fds_fallback(from_fd); |
| 91 | +} |
| 92 | + |
31 | 93 | pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const char *dirpath, int channels[3],
|
32 | 94 | const char *pts_name, int fdm, int console) {
|
33 | 95 | int pipe2[2];
|
@@ -107,14 +169,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
|
107 | 169 | }
|
108 | 170 |
|
109 | 171 | /* Close all the fd's in the child */
|
110 |
| - { |
111 |
| - int fdlimit = sysconf(_SC_OPEN_MAX); |
112 |
| - int fd = 3; |
113 |
| - |
114 |
| - while (fd < fdlimit) { |
115 |
| - close(fd++); |
116 |
| - } |
117 |
| - } |
| 172 | + close_all_fds(); |
118 | 173 |
|
119 | 174 | if (envp && envp[0]) {
|
120 | 175 | execve(full_path, argv, envp);
|
|
0 commit comments