|
21 | 21 | #include <sys/ioctl.h>
|
22 | 22 | #include <fcntl.h>
|
23 | 23 | #include <sys/wait.h>
|
| 24 | +#include <sys/syscall.h> |
| 25 | +#include <dirent.h> |
| 26 | +#include <ctype.h> |
24 | 27 |
|
25 | 28 | #include "exec_pty.h"
|
26 | 29 |
|
@@ -53,6 +56,65 @@ void restore_signals() {
|
53 | 56 | restore_signal(SIGQUIT);
|
54 | 57 | }
|
55 | 58 |
|
| 59 | +static int sys_close_range_wrapper(unsigned int from_fd_inclusive) { |
| 60 | + // Use fast `close_range` (https://man7.org/linux/man-pages/man2/close_range.2.html) if available. |
| 61 | + // Cannot call `close_range` from libc, as it may be unavailable in older libc. |
| 62 | +# if defined(__linux__) && defined(SYS_close_range) && defined(CLOSE_RANGE_UNSHARE) |
| 63 | + return syscall(SYS_close_range, from_fd_inclusive, ~0U, CLOSE_RANGE_UNSHARE); |
| 64 | +# else |
| 65 | + return -1; |
| 66 | +# endif |
| 67 | +} |
| 68 | + |
| 69 | +static int close_all_fds_using_parsing(unsigned int from_fd_inclusive) { |
| 70 | + // If `opendir` is implemented using a file descriptor, we may close it accidentally. |
| 71 | + // Let's close a few lowest file descriptors, in hope that `opendir` will use it. |
| 72 | + int lowest_fds_to_close = 2; |
| 73 | + for (int i = 0; i < lowest_fds_to_close; i++) { |
| 74 | + close(from_fd_inclusive + i); |
| 75 | + } |
| 76 | + |
| 77 | +#if defined(_ALLBSD_SOURCE) |
| 78 | +#define FD_DIR "/dev/fd" |
| 79 | +#else |
| 80 | +#define FD_DIR "/proc/self/fd" |
| 81 | +#endif |
| 82 | + |
| 83 | + DIR *dirp = opendir(FD_DIR); |
| 84 | + if (dirp == NULL) return -1; |
| 85 | + |
| 86 | + struct dirent *direntp; |
| 87 | + |
| 88 | + while ((direntp = readdir(dirp)) != NULL) { |
| 89 | + int fd; |
| 90 | + if (isdigit(direntp->d_name[0])) { |
| 91 | + fd = strtol(direntp->d_name, NULL, 10); |
| 92 | + if (fd >= from_fd_inclusive + lowest_fds_to_close && fd != dirfd(dirp)) { |
| 93 | + close(fd); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + closedir(dirp); |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +static void close_all_fds_fallback(unsigned int from_fd_inclusive) { |
| 104 | + int fdlimit = sysconf(_SC_OPEN_MAX); |
| 105 | + if (fdlimit == -1) fdlimit = 65535; // arbitrary default, just in case |
| 106 | + for (int fd = from_fd_inclusive; fd < fdlimit; fd++) { |
| 107 | + close(fd); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +static void close_all_fds() { |
| 112 | + unsigned int from_fd = STDERR_FILENO + 1; |
| 113 | + if (sys_close_range_wrapper(from_fd) == 0) return; |
| 114 | + if (close_all_fds_using_parsing(from_fd) == 0) return; |
| 115 | + close_all_fds_fallback(from_fd); |
| 116 | +} |
| 117 | + |
56 | 118 | pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const char *dirpath,
|
57 | 119 | const char *pts_name, int fdm, const char *err_pts_name, int err_fdm, int console)
|
58 | 120 | {
|
@@ -122,13 +184,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
|
122 | 184 | if (console && err_fds >= 0) close(err_fds);
|
123 | 185 |
|
124 | 186 | /* Close all the fd's in the child */
|
125 |
| - { |
126 |
| - int fdlimit = sysconf(_SC_OPEN_MAX); |
127 |
| - int fd = 3; |
128 |
| - |
129 |
| - while (fd < fdlimit) |
130 |
| - close(fd++); |
131 |
| - } |
| 187 | + close_all_fds(); |
132 | 188 |
|
133 | 189 | restore_signals();
|
134 | 190 |
|
|
0 commit comments