Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1 | /* Authors: Gregory P. Smith & Jeffrey Yasskin */ |
| 2 | #include "Python.h" |
Victor Stinner | 5572ba7 | 2011-05-26 14:10:08 +0200 | [diff] [blame] | 3 | #if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE) |
| 4 | # define _GNU_SOURCE |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 5 | #endif |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 6 | #include <unistd.h> |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 7 | #include <fcntl.h> |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 8 | #ifdef HAVE_SYS_TYPES_H |
| 9 | #include <sys/types.h> |
| 10 | #endif |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 11 | #if defined(HAVE_SYS_STAT_H) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 12 | #include <sys/stat.h> |
| 13 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 14 | #ifdef HAVE_SYS_SYSCALL_H |
| 15 | #include <sys/syscall.h> |
| 16 | #endif |
Gregory P. Smith | f968177 | 2015-04-25 23:43:34 -0700 | [diff] [blame] | 17 | #if defined(HAVE_SYS_RESOURCE_H) |
| 18 | #include <sys/resource.h> |
| 19 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 20 | #ifdef HAVE_DIRENT_H |
| 21 | #include <dirent.h> |
| 22 | #endif |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 23 | #ifdef HAVE_GRP_H |
| 24 | #include <grp.h> |
| 25 | #endif /* HAVE_GRP_H */ |
| 26 | |
| 27 | #include "posixmodule.h" |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 28 | |
Gregory P. Smith | 3015fb8 | 2018-11-12 22:01:22 -0800 | [diff] [blame] | 29 | #ifdef _Py_MEMORY_SANITIZER |
Gregory P. Smith | 1584a00 | 2018-11-12 12:07:14 -0800 | [diff] [blame] | 30 | # include <sanitizer/msan_interface.h> |
| 31 | #endif |
| 32 | |
Xavier de Gaye | c716f18 | 2016-06-15 11:35:29 +0200 | [diff] [blame] | 33 | #if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64) |
Gregory P. Smith | efeb9da | 2014-04-14 13:31:21 -0700 | [diff] [blame] | 34 | # include <sys/linux-syscalls.h> |
| 35 | # define SYS_getdents64 __NR_getdents64 |
| 36 | #endif |
| 37 | |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 38 | #if defined(__sun) && defined(__SVR4) |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 39 | /* readdir64 is used to work around Solaris 9 bug 6395699. */ |
| 40 | # define readdir readdir64 |
| 41 | # define dirent dirent64 |
| 42 | # if !defined(HAVE_DIRFD) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 43 | /* Some versions of Solaris lack dirfd(). */ |
Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 44 | # define dirfd(dirp) ((dirp)->dd_fd) |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 45 | # define HAVE_DIRFD |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 46 | # endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 47 | #endif |
| 48 | |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 49 | #if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__)) |
| 50 | # define FD_DIR "/dev/fd" |
| 51 | #else |
| 52 | # define FD_DIR "/proc/self/fd" |
| 53 | #endif |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 54 | |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 55 | #ifdef NGROUPS_MAX |
| 56 | #define MAX_GROUPS NGROUPS_MAX |
| 57 | #else |
| 58 | #define MAX_GROUPS 64 |
| 59 | #endif |
| 60 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 61 | #define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 62 | |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 63 | typedef struct { |
| 64 | PyObject* disable; |
| 65 | PyObject* enable; |
| 66 | PyObject* isenabled; |
| 67 | } _posixsubprocessstate; |
| 68 | |
| 69 | static struct PyModuleDef _posixsubprocessmodule; |
| 70 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 71 | static inline _posixsubprocessstate* |
| 72 | get_posixsubprocess_state(PyObject *module) |
| 73 | { |
| 74 | void *state = PyModule_GetState(module); |
| 75 | assert(state != NULL); |
| 76 | return (_posixsubprocessstate *)state; |
| 77 | } |
| 78 | |
| 79 | #define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule)) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 80 | |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 81 | /* If gc was disabled, call gc.enable(). Return 0 on success. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 82 | static int |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 83 | _enable_gc(int need_to_reenable_gc, PyObject *gc_module) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 84 | { |
| 85 | PyObject *result; |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 86 | PyObject *exctype, *val, *tb; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 87 | |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 88 | if (need_to_reenable_gc) { |
| 89 | PyErr_Fetch(&exctype, &val, &tb); |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 90 | result = PyObject_CallMethodNoArgs( |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 91 | gc_module, _posixsubprocessstate_global->enable); |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 92 | if (exctype != NULL) { |
| 93 | PyErr_Restore(exctype, val, tb); |
| 94 | } |
| 95 | if (result == NULL) { |
| 96 | return 1; |
| 97 | } |
| 98 | Py_DECREF(result); |
| 99 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 104 | /* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 105 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 106 | _pos_int_from_ascii(const char *name) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 107 | { |
| 108 | int num = 0; |
| 109 | while (*name >= '0' && *name <= '9') { |
| 110 | num = num * 10 + (*name - '0'); |
| 111 | ++name; |
| 112 | } |
| 113 | if (*name) |
| 114 | return -1; /* Non digit found, not a number. */ |
| 115 | return num; |
| 116 | } |
| 117 | |
| 118 | |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 119 | #if defined(__FreeBSD__) |
| 120 | /* When /dev/fd isn't mounted it is often a static directory populated |
| 121 | * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD. |
| 122 | * NetBSD and OpenBSD have a /proc fs available (though not necessarily |
| 123 | * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs |
| 124 | * that properly supports /dev/fd. |
| 125 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 126 | static int |
Ross Lagerwall | 7f4fdb2 | 2012-03-07 20:06:33 +0200 | [diff] [blame] | 127 | _is_fdescfs_mounted_on_dev_fd(void) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 128 | { |
| 129 | struct stat dev_stat; |
| 130 | struct stat dev_fd_stat; |
| 131 | if (stat("/dev", &dev_stat) != 0) |
| 132 | return 0; |
| 133 | if (stat(FD_DIR, &dev_fd_stat) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 134 | return 0; |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 135 | if (dev_stat.st_dev == dev_fd_stat.st_dev) |
| 136 | return 0; /* / == /dev == /dev/fd means it is static. #fail */ |
| 137 | return 1; |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 142 | /* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 143 | static int |
| 144 | _sanity_check_python_fd_sequence(PyObject *fd_sequence) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 145 | { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 146 | Py_ssize_t seq_idx; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 147 | long prev_fd = -1; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 148 | for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) { |
| 149 | PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx); |
| 150 | long iter_fd; |
| 151 | if (!PyLong_Check(py_fd)) { |
| 152 | return 1; |
| 153 | } |
| 154 | iter_fd = PyLong_AsLong(py_fd); |
Gregory P. Smith | d0a5b1c | 2015-11-15 21:15:26 -0800 | [diff] [blame] | 155 | if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 156 | /* Negative, overflow, unsorted, too big for a fd. */ |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 157 | return 1; |
| 158 | } |
Gregory P. Smith | d0a5b1c | 2015-11-15 21:15:26 -0800 | [diff] [blame] | 159 | prev_fd = iter_fd; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /* Is fd found in the sorted Python Sequence? */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 166 | static int |
| 167 | _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 168 | { |
| 169 | /* Binary search. */ |
| 170 | Py_ssize_t search_min = 0; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 171 | Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 172 | if (search_max < 0) |
| 173 | return 0; |
| 174 | do { |
| 175 | long middle = (search_min + search_max) / 2; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 176 | long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle)); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 177 | if (fd == middle_fd) |
| 178 | return 1; |
| 179 | if (fd > middle_fd) |
| 180 | search_min = middle + 1; |
| 181 | else |
| 182 | search_max = middle - 1; |
| 183 | } while (search_min <= search_max); |
| 184 | return 0; |
| 185 | } |
| 186 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 187 | static int |
| 188 | make_inheritable(PyObject *py_fds_to_keep, int errpipe_write) |
| 189 | { |
| 190 | Py_ssize_t i, len; |
| 191 | |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 192 | len = PyTuple_GET_SIZE(py_fds_to_keep); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 193 | for (i = 0; i < len; ++i) { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 194 | PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 195 | long fd = PyLong_AsLong(fdobj); |
| 196 | assert(!PyErr_Occurred()); |
| 197 | assert(0 <= fd && fd <= INT_MAX); |
| 198 | if (fd == errpipe_write) { |
| 199 | /* errpipe_write is part of py_fds_to_keep. It must be closed at |
| 200 | exec(), but kept open in the child process until exec() is |
| 201 | called. */ |
| 202 | continue; |
| 203 | } |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 204 | if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 205 | return -1; |
| 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 210 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 211 | /* Get the maximum file descriptor that could be opened by this process. |
| 212 | * This function is async signal safe for use between fork() and exec(). |
| 213 | */ |
| 214 | static long |
| 215 | safe_get_max_fd(void) |
| 216 | { |
| 217 | long local_max_fd; |
| 218 | #if defined(__NetBSD__) |
| 219 | local_max_fd = fcntl(0, F_MAXFD); |
| 220 | if (local_max_fd >= 0) |
| 221 | return local_max_fd; |
| 222 | #endif |
Gregory P. Smith | f968177 | 2015-04-25 23:43:34 -0700 | [diff] [blame] | 223 | #if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__) |
| 224 | struct rlimit rl; |
| 225 | /* Not on the POSIX async signal safe functions list but likely |
| 226 | * safe. TODO - Someone should audit OpenBSD to make sure. */ |
| 227 | if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) |
| 228 | return (long) rl.rlim_max; |
| 229 | #endif |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 230 | #ifdef _SC_OPEN_MAX |
| 231 | local_max_fd = sysconf(_SC_OPEN_MAX); |
| 232 | if (local_max_fd == -1) |
| 233 | #endif |
| 234 | local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */ |
| 235 | return local_max_fd; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* Close all file descriptors in the range from start_fd and higher |
| 240 | * except for those in py_fds_to_keep. If the range defined by |
| 241 | * [start_fd, safe_get_max_fd()) is large this will take a long |
| 242 | * time as it calls close() on EVERY possible fd. |
| 243 | * |
| 244 | * It isn't possible to know for sure what the max fd to go up to |
| 245 | * is for processes with the capability of raising their maximum. |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 246 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 247 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 248 | _close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 249 | { |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 250 | long end_fd = safe_get_max_fd(); |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 251 | Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 252 | Py_ssize_t keep_seq_idx; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 253 | /* As py_fds_to_keep is sorted we can loop through the list closing |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 254 | * fds in between any in the keep list falling within our range. */ |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 255 | for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 256 | PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 257 | int keep_fd = PyLong_AsLong(py_keep_fd); |
| 258 | if (keep_fd < start_fd) |
| 259 | continue; |
Kyle Evans | c230fde | 2020-10-11 13:54:11 -0500 | [diff] [blame^] | 260 | _Py_closerange(start_fd, keep_fd - 1); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 261 | start_fd = keep_fd + 1; |
| 262 | } |
| 263 | if (start_fd <= end_fd) { |
Kyle Evans | c230fde | 2020-10-11 13:54:11 -0500 | [diff] [blame^] | 264 | _Py_closerange(start_fd, end_fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) |
| 270 | /* It doesn't matter if d_name has room for NAME_MAX chars; we're using this |
| 271 | * only to read a directory of short file descriptor number names. The kernel |
| 272 | * will return an error if we didn't give it enough space. Highly Unlikely. |
| 273 | * This structure is very old and stable: It will not change unless the kernel |
| 274 | * chooses to break compatibility with all existing binaries. Highly Unlikely. |
| 275 | */ |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 276 | struct linux_dirent64 { |
Gregory P. Smith | 58f07a9 | 2012-06-05 13:26:39 -0700 | [diff] [blame] | 277 | unsigned long long d_ino; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 278 | long long d_off; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 279 | unsigned short d_reclen; /* Length of this linux_dirent */ |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 280 | unsigned char d_type; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 281 | char d_name[256]; /* Filename (null-terminated) */ |
| 282 | }; |
| 283 | |
Gregory P. Smith | a26987a | 2014-06-01 13:46:36 -0700 | [diff] [blame] | 284 | /* Close all open file descriptors in the range from start_fd and higher |
| 285 | * Do not close any in the sorted py_fds_to_keep list. |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 286 | * |
| 287 | * This version is async signal safe as it does not make any unsafe C library |
| 288 | * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced |
| 289 | * to resort to making a kernel system call directly but this is the ONLY api |
| 290 | * available that does no harm. opendir/readdir/closedir perform memory |
| 291 | * allocation and locking so while they usually work they are not guaranteed |
| 292 | * to (especially if you have replaced your malloc implementation). A version |
| 293 | * of this function that uses those can be found in the _maybe_unsafe variant. |
| 294 | * |
| 295 | * This is Linux specific because that is all I am ready to test it on. It |
| 296 | * should be easy to add OS specific dirent or dirent64 structures and modify |
| 297 | * it with some cpp #define magic to work on other OSes as well if you want. |
| 298 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 299 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 300 | _close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 301 | { |
| 302 | int fd_dir_fd; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 303 | |
Victor Stinner | 160e819 | 2015-03-30 02:18:31 +0200 | [diff] [blame] | 304 | fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 305 | if (fd_dir_fd == -1) { |
| 306 | /* No way to get a list of open fds. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 307 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 308 | return; |
| 309 | } else { |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 310 | char buffer[sizeof(struct linux_dirent64)]; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 311 | int bytes; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 312 | while ((bytes = syscall(SYS_getdents64, fd_dir_fd, |
| 313 | (struct linux_dirent64 *)buffer, |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 314 | sizeof(buffer))) > 0) { |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 315 | struct linux_dirent64 *entry; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 316 | int offset; |
Gregory P. Smith | 3015fb8 | 2018-11-12 22:01:22 -0800 | [diff] [blame] | 317 | #ifdef _Py_MEMORY_SANITIZER |
Gregory P. Smith | 1584a00 | 2018-11-12 12:07:14 -0800 | [diff] [blame] | 318 | __msan_unpoison(buffer, bytes); |
| 319 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 320 | for (offset = 0; offset < bytes; offset += entry->d_reclen) { |
| 321 | int fd; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 322 | entry = (struct linux_dirent64 *)(buffer + offset); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 323 | if ((fd = _pos_int_from_ascii(entry->d_name)) < 0) |
| 324 | continue; /* Not a number. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 325 | if (fd != fd_dir_fd && fd >= start_fd && |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 326 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 327 | close(fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 331 | close(fd_dir_fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 335 | #define _close_open_fds _close_open_fds_safe |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 336 | |
| 337 | #else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ |
| 338 | |
| 339 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 340 | /* Close all open file descriptors from start_fd and higher. |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 341 | * Do not close any in the sorted py_fds_to_keep tuple. |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 342 | * |
| 343 | * This function violates the strict use of async signal safe functions. :( |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 344 | * It calls opendir(), readdir() and closedir(). Of these, the one most |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 345 | * likely to ever cause a problem is opendir() as it performs an internal |
| 346 | * malloc(). Practically this should not be a problem. The Java VM makes the |
| 347 | * same calls between fork and exec in its own UNIXProcess_md.c implementation. |
| 348 | * |
| 349 | * readdir_r() is not used because it provides no benefit. It is typically |
| 350 | * implemented as readdir() followed by memcpy(). See also: |
| 351 | * http://womble.decadent.org.uk/readdir_r-advisory.html |
| 352 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 353 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 354 | _close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 355 | { |
| 356 | DIR *proc_fd_dir; |
| 357 | #ifndef HAVE_DIRFD |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 358 | while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) { |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 359 | ++start_fd; |
| 360 | } |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 361 | /* Close our lowest fd before we call opendir so that it is likely to |
| 362 | * reuse that fd otherwise we might close opendir's file descriptor in |
| 363 | * our loop. This trick assumes that fd's are allocated on a lowest |
| 364 | * available basis. */ |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 365 | close(start_fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 366 | ++start_fd; |
| 367 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 368 | |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 369 | #if defined(__FreeBSD__) |
| 370 | if (!_is_fdescfs_mounted_on_dev_fd()) |
| 371 | proc_fd_dir = NULL; |
| 372 | else |
| 373 | #endif |
| 374 | proc_fd_dir = opendir(FD_DIR); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 375 | if (!proc_fd_dir) { |
| 376 | /* No way to get a list of open fds. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 377 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 378 | } else { |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 379 | struct dirent *dir_entry; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 380 | #ifdef HAVE_DIRFD |
Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 381 | int fd_used_by_opendir = dirfd(proc_fd_dir); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 382 | #else |
| 383 | int fd_used_by_opendir = start_fd - 1; |
| 384 | #endif |
| 385 | errno = 0; |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 386 | while ((dir_entry = readdir(proc_fd_dir))) { |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 387 | int fd; |
| 388 | if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0) |
| 389 | continue; /* Not a number. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 390 | if (fd != fd_used_by_opendir && fd >= start_fd && |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 391 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 392 | close(fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 393 | } |
| 394 | errno = 0; |
| 395 | } |
| 396 | if (errno) { |
| 397 | /* readdir error, revert behavior. Highly Unlikely. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 398 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 399 | } |
| 400 | closedir(proc_fd_dir); |
| 401 | } |
| 402 | } |
| 403 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 404 | #define _close_open_fds _close_open_fds_maybe_unsafe |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 405 | |
| 406 | #endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ |
| 407 | |
| 408 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 409 | /* |
| 410 | * This function is code executed in the child process immediately after fork |
| 411 | * to set things up and call exec(). |
| 412 | * |
| 413 | * All of the code in this function must only use async-signal-safe functions, |
| 414 | * listed at `man 7 signal` or |
| 415 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 416 | * |
| 417 | * This restriction is documented at |
| 418 | * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html. |
| 419 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 420 | static void |
| 421 | child_exec(char *const exec_array[], |
| 422 | char *const argv[], |
| 423 | char *const envp[], |
| 424 | const char *cwd, |
| 425 | int p2cread, int p2cwrite, |
| 426 | int c2pread, int c2pwrite, |
| 427 | int errread, int errwrite, |
| 428 | int errpipe_read, int errpipe_write, |
| 429 | int close_fds, int restore_signals, |
| 430 | int call_setsid, |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 431 | int call_setgid, gid_t gid, |
| 432 | int call_setgroups, size_t groups_size, const gid_t *groups, |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 433 | int call_setuid, uid_t uid, int child_umask, |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 434 | PyObject *py_fds_to_keep, |
| 435 | PyObject *preexec_fn, |
| 436 | PyObject *preexec_fn_args_tuple) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 437 | { |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 438 | int i, saved_errno, reached_preexec = 0; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 439 | PyObject *result; |
Gregory P. Smith | 14affb8 | 2010-12-22 05:22:17 +0000 | [diff] [blame] | 440 | const char* err_msg = ""; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 441 | /* Buffer large enough to hold a hex integer. We can't malloc. */ |
| 442 | char hex_errno[sizeof(saved_errno)*2+1]; |
| 443 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 444 | if (make_inheritable(py_fds_to_keep, errpipe_write) < 0) |
| 445 | goto error; |
| 446 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 447 | /* Close parent's pipe ends. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 448 | if (p2cwrite != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 449 | POSIX_CALL(close(p2cwrite)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 450 | if (c2pread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 451 | POSIX_CALL(close(c2pread)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 452 | if (errread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 453 | POSIX_CALL(close(errread)); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 454 | POSIX_CALL(close(errpipe_read)); |
| 455 | |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 456 | /* When duping fds, if there arises a situation where one of the fds is |
| 457 | either 0, 1 or 2, it is possible that it is overwritten (#12607). */ |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 458 | if (c2pwrite == 0) { |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 459 | POSIX_CALL(c2pwrite = dup(c2pwrite)); |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 460 | /* issue32270 */ |
| 461 | if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) { |
| 462 | goto error; |
| 463 | } |
| 464 | } |
| 465 | while (errwrite == 0 || errwrite == 1) { |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 466 | POSIX_CALL(errwrite = dup(errwrite)); |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 467 | /* issue32270 */ |
| 468 | if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) { |
| 469 | goto error; |
| 470 | } |
| 471 | } |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 472 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 473 | /* Dup fds for child. |
| 474 | dup2() removes the CLOEXEC flag but we must do it ourselves if dup2() |
| 475 | would be a no-op (issue #10806). */ |
| 476 | if (p2cread == 0) { |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 477 | if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 478 | goto error; |
| 479 | } |
| 480 | else if (p2cread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 481 | POSIX_CALL(dup2(p2cread, 0)); /* stdin */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 482 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 483 | if (c2pwrite == 1) { |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 484 | if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 485 | goto error; |
| 486 | } |
| 487 | else if (c2pwrite != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 488 | POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 489 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 490 | if (errwrite == 2) { |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 491 | if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 492 | goto error; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 493 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 494 | else if (errwrite != -1) |
| 495 | POSIX_CALL(dup2(errwrite, 2)); /* stderr */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 496 | |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 497 | /* We no longer manually close p2cread, c2pwrite, and errwrite here as |
| 498 | * _close_open_fds takes care when it is not already non-inheritable. */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 499 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 500 | if (cwd) |
| 501 | POSIX_CALL(chdir(cwd)); |
| 502 | |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 503 | if (child_umask >= 0) |
| 504 | umask(child_umask); /* umask() always succeeds. */ |
| 505 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 506 | if (restore_signals) |
| 507 | _Py_RestoreSignals(); |
| 508 | |
| 509 | #ifdef HAVE_SETSID |
| 510 | if (call_setsid) |
| 511 | POSIX_CALL(setsid()); |
| 512 | #endif |
| 513 | |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 514 | #ifdef HAVE_SETGROUPS |
| 515 | if (call_setgroups) |
| 516 | POSIX_CALL(setgroups(groups_size, groups)); |
| 517 | #endif /* HAVE_SETGROUPS */ |
| 518 | |
| 519 | #ifdef HAVE_SETREGID |
| 520 | if (call_setgid) |
| 521 | POSIX_CALL(setregid(gid, gid)); |
| 522 | #endif /* HAVE_SETREGID */ |
| 523 | |
| 524 | #ifdef HAVE_SETREUID |
| 525 | if (call_setuid) |
| 526 | POSIX_CALL(setreuid(uid, uid)); |
| 527 | #endif /* HAVE_SETREUID */ |
| 528 | |
| 529 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 530 | reached_preexec = 1; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 531 | if (preexec_fn != Py_None && preexec_fn_args_tuple) { |
| 532 | /* This is where the user has asked us to deadlock their program. */ |
| 533 | result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL); |
| 534 | if (result == NULL) { |
| 535 | /* Stringifying the exception or traceback would involve |
| 536 | * memory allocation and thus potential for deadlock. |
| 537 | * We've already faced potential deadlock by calling back |
| 538 | * into Python in the first place, so it probably doesn't |
| 539 | * matter but we avoid it to minimize the possibility. */ |
| 540 | err_msg = "Exception occurred in preexec_fn."; |
| 541 | errno = 0; /* We don't want to report an OSError. */ |
| 542 | goto error; |
| 543 | } |
| 544 | /* Py_DECREF(result); - We're about to exec so why bother? */ |
| 545 | } |
| 546 | |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 547 | /* close FDs after executing preexec_fn, which might open FDs */ |
| 548 | if (close_fds) { |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 549 | /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 550 | _close_open_fds(3, py_fds_to_keep); |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 553 | /* This loop matches the Lib/os.py _execvpe()'s PATH search when */ |
| 554 | /* given the executable_list generated by Lib/subprocess.py. */ |
| 555 | saved_errno = 0; |
| 556 | for (i = 0; exec_array[i] != NULL; ++i) { |
| 557 | const char *executable = exec_array[i]; |
| 558 | if (envp) { |
| 559 | execve(executable, argv, envp); |
| 560 | } else { |
| 561 | execv(executable, argv); |
| 562 | } |
| 563 | if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) { |
| 564 | saved_errno = errno; |
| 565 | } |
| 566 | } |
| 567 | /* Report the first exec error, not the last. */ |
| 568 | if (saved_errno) |
| 569 | errno = saved_errno; |
| 570 | |
| 571 | error: |
| 572 | saved_errno = errno; |
| 573 | /* Report the posix error to our parent process. */ |
Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 574 | /* We ignore all write() return values as the total size of our writes is |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 575 | less than PIPEBUF and we cannot do anything about an error anyways. |
| 576 | Use _Py_write_noraise() to retry write() if it is interrupted by a |
| 577 | signal (fails with EINTR). */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 578 | if (saved_errno) { |
| 579 | char *cur; |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 580 | _Py_write_noraise(errpipe_write, "OSError:", 8); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 581 | cur = hex_errno + sizeof(hex_errno); |
Serhiy Storchaka | 5ae4f49 | 2016-09-27 22:03:51 +0300 | [diff] [blame] | 582 | while (saved_errno != 0 && cur != hex_errno) { |
Gregory P. Smith | 4dff6f6 | 2015-04-25 23:42:38 +0000 | [diff] [blame] | 583 | *--cur = Py_hexdigits[saved_errno % 16]; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 584 | saved_errno /= 16; |
| 585 | } |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 586 | _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur); |
| 587 | _Py_write_noraise(errpipe_write, ":", 1); |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 588 | if (!reached_preexec) { |
| 589 | /* Indicate to the parent that the error happened before exec(). */ |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 590 | _Py_write_noraise(errpipe_write, "noexec", 6); |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 591 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 592 | /* We can't call strerror(saved_errno). It is not async signal safe. |
| 593 | * The parent process will look the error message up. */ |
| 594 | } else { |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 595 | _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18); |
| 596 | _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg)); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | static PyObject * |
| 602 | subprocess_fork_exec(PyObject* self, PyObject *args) |
| 603 | { |
| 604 | PyObject *gc_module = NULL; |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 605 | PyObject *executable_list, *py_fds_to_keep; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 606 | PyObject *env_list, *preexec_fn; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 607 | PyObject *process_args, *converted_args = NULL, *fast_args = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 608 | PyObject *preexec_fn_args_tuple = NULL; |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 609 | PyObject *groups_list; |
| 610 | PyObject *uid_object, *gid_object; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 611 | int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite; |
| 612 | int errpipe_read, errpipe_write, close_fds, restore_signals; |
| 613 | int call_setsid; |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 614 | int call_setgid = 0, call_setgroups = 0, call_setuid = 0; |
| 615 | uid_t uid; |
| 616 | gid_t gid, *groups = NULL; |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 617 | int child_umask; |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 618 | PyObject *cwd_obj, *cwd_obj2; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 619 | const char *cwd; |
| 620 | pid_t pid; |
| 621 | int need_to_reenable_gc = 0; |
| 622 | char *const *exec_array, *const *argv = NULL, *const *envp = NULL; |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 623 | Py_ssize_t arg_num, num_groups = 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 624 | int need_after_fork = 0; |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 625 | int saved_errno = 0; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 626 | |
| 627 | if (!PyArg_ParseTuple( |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 628 | args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec", |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 629 | &process_args, &executable_list, |
| 630 | &close_fds, &PyTuple_Type, &py_fds_to_keep, |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 631 | &cwd_obj, &env_list, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 632 | &p2cread, &p2cwrite, &c2pread, &c2pwrite, |
| 633 | &errread, &errwrite, &errpipe_read, &errpipe_write, |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 634 | &restore_signals, &call_setsid, |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 635 | &gid_object, &groups_list, &uid_object, &child_umask, |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 636 | &preexec_fn)) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 637 | return NULL; |
| 638 | |
Christian Heimes | 98d90f7 | 2019-08-27 23:36:56 +0200 | [diff] [blame] | 639 | if ((preexec_fn != Py_None) && |
Victor Stinner | be79373 | 2020-03-13 18:15:33 +0100 | [diff] [blame] | 640 | (PyInterpreterState_Get() != PyInterpreterState_Main())) { |
Christian Heimes | 98d90f7 | 2019-08-27 23:36:56 +0200 | [diff] [blame] | 641 | PyErr_SetString(PyExc_RuntimeError, |
| 642 | "preexec_fn not supported within subinterpreters"); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 643 | return NULL; |
| 644 | } |
| 645 | |
Gregory P. Smith | 361e30c | 2013-12-01 00:12:24 -0800 | [diff] [blame] | 646 | if (close_fds && errpipe_write < 3) { /* precondition */ |
| 647 | PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3"); |
| 648 | return NULL; |
| 649 | } |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 650 | if (_sanity_check_python_fd_sequence(py_fds_to_keep)) { |
| 651 | PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep"); |
Gregory P. Smith | d4cc7bf | 2010-12-04 11:22:11 +0000 | [diff] [blame] | 652 | return NULL; |
| 653 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 654 | |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 655 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 656 | const PyConfig *config = _PyInterpreterState_GetConfig(interp); |
| 657 | if (config->_isolated_interpreter) { |
| 658 | PyErr_SetString(PyExc_RuntimeError, |
| 659 | "subprocess not supported for isolated subinterpreters"); |
| 660 | return NULL; |
| 661 | } |
| 662 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 663 | /* We need to call gc.disable() when we'll be calling preexec_fn */ |
| 664 | if (preexec_fn != Py_None) { |
| 665 | PyObject *result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 666 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 667 | gc_module = PyImport_ImportModule("gc"); |
| 668 | if (gc_module == NULL) |
| 669 | return NULL; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 670 | result = PyObject_CallMethodNoArgs( |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 671 | gc_module, _posixsubprocessstate_global->isenabled); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 672 | if (result == NULL) { |
| 673 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 674 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 675 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 676 | need_to_reenable_gc = PyObject_IsTrue(result); |
| 677 | Py_DECREF(result); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 678 | if (need_to_reenable_gc == -1) { |
| 679 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 680 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 681 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 682 | result = PyObject_CallMethodNoArgs( |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 683 | gc_module, _posixsubprocessstate_global->disable); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 684 | if (result == NULL) { |
| 685 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 686 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 687 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 688 | Py_DECREF(result); |
| 689 | } |
| 690 | |
| 691 | exec_array = _PySequence_BytesToCharpArray(executable_list); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 692 | if (!exec_array) |
| 693 | goto cleanup; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 694 | |
| 695 | /* Convert args and env into appropriate arguments for exec() */ |
| 696 | /* These conversions are done in the parent process to avoid allocating |
| 697 | or freeing memory in the child process. */ |
| 698 | if (process_args != Py_None) { |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 699 | Py_ssize_t num_args; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 700 | /* Equivalent to: */ |
| 701 | /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */ |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 702 | fast_args = PySequence_Fast(process_args, "argv must be a tuple"); |
Stefan Krah | db579d7 | 2012-08-20 14:36:47 +0200 | [diff] [blame] | 703 | if (fast_args == NULL) |
| 704 | goto cleanup; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 705 | num_args = PySequence_Fast_GET_SIZE(fast_args); |
| 706 | converted_args = PyTuple_New(num_args); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 707 | if (converted_args == NULL) |
| 708 | goto cleanup; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 709 | for (arg_num = 0; arg_num < num_args; ++arg_num) { |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 710 | PyObject *borrowed_arg, *converted_arg; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 711 | if (PySequence_Fast_GET_SIZE(fast_args) != num_args) { |
| 712 | PyErr_SetString(PyExc_RuntimeError, "args changed during iteration"); |
| 713 | goto cleanup; |
| 714 | } |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 715 | borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 716 | if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0) |
| 717 | goto cleanup; |
| 718 | PyTuple_SET_ITEM(converted_args, arg_num, converted_arg); |
| 719 | } |
| 720 | |
| 721 | argv = _PySequence_BytesToCharpArray(converted_args); |
| 722 | Py_CLEAR(converted_args); |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 723 | Py_CLEAR(fast_args); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 724 | if (!argv) |
| 725 | goto cleanup; |
| 726 | } |
| 727 | |
| 728 | if (env_list != Py_None) { |
| 729 | envp = _PySequence_BytesToCharpArray(env_list); |
| 730 | if (!envp) |
| 731 | goto cleanup; |
| 732 | } |
| 733 | |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 734 | if (cwd_obj != Py_None) { |
| 735 | if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0) |
| 736 | goto cleanup; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 737 | cwd = PyBytes_AsString(cwd_obj2); |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 738 | } else { |
| 739 | cwd = NULL; |
| 740 | cwd_obj2 = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 743 | if (groups_list != Py_None) { |
| 744 | #ifdef HAVE_SETGROUPS |
| 745 | Py_ssize_t i; |
| 746 | unsigned long gid; |
| 747 | |
| 748 | if (!PyList_Check(groups_list)) { |
| 749 | PyErr_SetString(PyExc_TypeError, |
| 750 | "setgroups argument must be a list"); |
| 751 | goto cleanup; |
| 752 | } |
| 753 | num_groups = PySequence_Size(groups_list); |
| 754 | |
| 755 | if (num_groups < 0) |
| 756 | goto cleanup; |
| 757 | |
| 758 | if (num_groups > MAX_GROUPS) { |
| 759 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 760 | goto cleanup; |
| 761 | } |
| 762 | |
| 763 | if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) { |
| 764 | PyErr_SetString(PyExc_MemoryError, |
| 765 | "failed to allocate memory for group list"); |
| 766 | goto cleanup; |
| 767 | } |
| 768 | |
| 769 | for (i = 0; i < num_groups; i++) { |
| 770 | PyObject *elem; |
| 771 | elem = PySequence_GetItem(groups_list, i); |
| 772 | if (!elem) |
| 773 | goto cleanup; |
| 774 | if (!PyLong_Check(elem)) { |
| 775 | PyErr_SetString(PyExc_TypeError, |
| 776 | "groups must be integers"); |
| 777 | Py_DECREF(elem); |
| 778 | goto cleanup; |
| 779 | } else { |
| 780 | /* In posixmodule.c UnsignedLong is used as a fallback value |
| 781 | * if the value provided does not fit in a Long. Since we are |
| 782 | * already doing the bounds checking on the Python side, we |
| 783 | * can go directly to an UnsignedLong here. */ |
| 784 | if (!_Py_Gid_Converter(elem, &gid)) { |
| 785 | Py_DECREF(elem); |
| 786 | PyErr_SetString(PyExc_ValueError, "invalid group id"); |
| 787 | goto cleanup; |
| 788 | } |
| 789 | groups[i] = gid; |
| 790 | } |
| 791 | Py_DECREF(elem); |
| 792 | } |
| 793 | call_setgroups = 1; |
| 794 | |
| 795 | #else /* HAVE_SETGROUPS */ |
| 796 | PyErr_BadInternalCall(); |
| 797 | goto cleanup; |
| 798 | #endif /* HAVE_SETGROUPS */ |
| 799 | } |
| 800 | |
| 801 | if (gid_object != Py_None) { |
| 802 | #ifdef HAVE_SETREGID |
| 803 | if (!_Py_Gid_Converter(gid_object, &gid)) |
| 804 | goto cleanup; |
| 805 | |
| 806 | call_setgid = 1; |
| 807 | |
| 808 | #else /* HAVE_SETREGID */ |
| 809 | PyErr_BadInternalCall(); |
| 810 | goto cleanup; |
| 811 | #endif /* HAVE_SETREUID */ |
| 812 | } |
| 813 | |
| 814 | if (uid_object != Py_None) { |
| 815 | #ifdef HAVE_SETREUID |
| 816 | if (!_Py_Uid_Converter(uid_object, &uid)) |
| 817 | goto cleanup; |
| 818 | |
| 819 | call_setuid = 1; |
| 820 | |
| 821 | #else /* HAVE_SETREUID */ |
| 822 | PyErr_BadInternalCall(); |
| 823 | goto cleanup; |
| 824 | #endif /* HAVE_SETREUID */ |
| 825 | } |
| 826 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 827 | /* This must be the last thing done before fork() because we do not |
| 828 | * want to call PyOS_BeforeFork() if there is any chance of another |
| 829 | * error leading to the cleanup: code without calling fork(). */ |
| 830 | if (preexec_fn != Py_None) { |
| 831 | preexec_fn_args_tuple = PyTuple_New(0); |
| 832 | if (!preexec_fn_args_tuple) |
| 833 | goto cleanup; |
| 834 | PyOS_BeforeFork(); |
| 835 | need_after_fork = 1; |
| 836 | } |
| 837 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 838 | pid = fork(); |
| 839 | if (pid == 0) { |
| 840 | /* Child process */ |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 841 | /* |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 842 | * Code from here to _exit() must only use async-signal-safe functions, |
| 843 | * listed at `man 7 signal` or |
| 844 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 845 | */ |
| 846 | |
| 847 | if (preexec_fn != Py_None) { |
| 848 | /* We'll be calling back into Python later so we need to do this. |
| 849 | * This call may not be async-signal-safe but neither is calling |
| 850 | * back into Python. The user asked us to use hope as a strategy |
| 851 | * to avoid deadlock... */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 852 | PyOS_AfterFork_Child(); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | child_exec(exec_array, argv, envp, cwd, |
| 856 | p2cread, p2cwrite, c2pread, c2pwrite, |
| 857 | errread, errwrite, errpipe_read, errpipe_write, |
| 858 | close_fds, restore_signals, call_setsid, |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 859 | call_setgid, gid, call_setgroups, num_groups, groups, |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 860 | call_setuid, uid, child_umask, |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 861 | py_fds_to_keep, preexec_fn, preexec_fn_args_tuple); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 862 | _exit(255); |
| 863 | return NULL; /* Dead code to avoid a potential compiler warning. */ |
| 864 | } |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 865 | /* Parent (original) process */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 866 | if (pid == -1) { |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 867 | /* Capture errno for the exception. */ |
| 868 | saved_errno = errno; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 869 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 870 | |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 871 | Py_XDECREF(cwd_obj2); |
| 872 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 873 | if (need_after_fork) |
| 874 | PyOS_AfterFork_Parent(); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 875 | if (envp) |
| 876 | _Py_FreeCharPArray(envp); |
| 877 | if (argv) |
| 878 | _Py_FreeCharPArray(argv); |
| 879 | _Py_FreeCharPArray(exec_array); |
| 880 | |
| 881 | /* Reenable gc in the parent process (or if fork failed). */ |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 882 | if (_enable_gc(need_to_reenable_gc, gc_module)) { |
| 883 | pid = -1; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 884 | } |
Christian Heimes | 0d3350d | 2020-06-12 18:18:43 +0200 | [diff] [blame] | 885 | PyMem_RawFree(groups); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 886 | Py_XDECREF(preexec_fn_args_tuple); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 887 | Py_XDECREF(gc_module); |
| 888 | |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 889 | if (pid == -1) { |
| 890 | errno = saved_errno; |
| 891 | /* We can't call this above as PyOS_AfterFork_Parent() calls back |
| 892 | * into Python code which would see the unreturned error. */ |
| 893 | PyErr_SetFromErrno(PyExc_OSError); |
| 894 | return NULL; /* fork() failed. */ |
| 895 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 896 | |
| 897 | return PyLong_FromPid(pid); |
| 898 | |
| 899 | cleanup: |
| 900 | if (envp) |
| 901 | _Py_FreeCharPArray(envp); |
| 902 | if (argv) |
| 903 | _Py_FreeCharPArray(argv); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 904 | if (exec_array) |
| 905 | _Py_FreeCharPArray(exec_array); |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 906 | |
| 907 | PyMem_RawFree(groups); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 908 | Py_XDECREF(converted_args); |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 909 | Py_XDECREF(fast_args); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 910 | Py_XDECREF(preexec_fn_args_tuple); |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 911 | _enable_gc(need_to_reenable_gc, gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 912 | Py_XDECREF(gc_module); |
| 913 | return NULL; |
| 914 | } |
| 915 | |
| 916 | |
| 917 | PyDoc_STRVAR(subprocess_fork_exec_doc, |
Orivej Desh | 77abf23 | 2019-09-20 17:01:10 +0000 | [diff] [blame] | 918 | "fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 919 | p2cread, p2cwrite, c2pread, c2pwrite,\n\ |
| 920 | errread, errwrite, errpipe_read, errpipe_write,\n\ |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 921 | restore_signals, call_setsid,\n\ |
Orivej Desh | 77abf23 | 2019-09-20 17:01:10 +0000 | [diff] [blame] | 922 | gid, groups_list, uid,\n\ |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 923 | preexec_fn)\n\ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 924 | \n\ |
| 925 | Forks a child process, closes parent file descriptors as appropriate in the\n\ |
| 926 | child and dups the few that are needed before calling exec() in the child\n\ |
| 927 | process.\n\ |
| 928 | \n\ |
Orivej Desh | 77abf23 | 2019-09-20 17:01:10 +0000 | [diff] [blame] | 929 | If close_fds is true, close file descriptors 3 and higher, except those listed\n\ |
| 930 | in the sorted tuple pass_fds.\n\ |
| 931 | \n\ |
| 932 | The preexec_fn, if supplied, will be called immediately before closing file\n\ |
| 933 | descriptors and exec.\n\ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 934 | WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\ |
| 935 | It may trigger infrequent, difficult to debug deadlocks.\n\ |
| 936 | \n\ |
| 937 | If an error occurs in the child process before the exec, it is\n\ |
| 938 | serialized and written to the errpipe_write fd per subprocess.py.\n\ |
| 939 | \n\ |
| 940 | Returns: the child process's PID.\n\ |
| 941 | \n\ |
| 942 | Raises: Only on an error in the parent process.\n\ |
| 943 | "); |
| 944 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 945 | /* module level code ********************************************************/ |
| 946 | |
| 947 | PyDoc_STRVAR(module_doc, |
| 948 | "A POSIX helper for the subprocess module."); |
| 949 | |
| 950 | |
| 951 | static PyMethodDef module_methods[] = { |
| 952 | {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc}, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 953 | {NULL, NULL} /* sentinel */ |
| 954 | }; |
| 955 | |
| 956 | |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 957 | static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 958 | Py_VISIT(get_posixsubprocess_state(m)->disable); |
| 959 | Py_VISIT(get_posixsubprocess_state(m)->enable); |
| 960 | Py_VISIT(get_posixsubprocess_state(m)->isenabled); |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | static int _posixsubprocess_clear(PyObject *m) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 965 | Py_CLEAR(get_posixsubprocess_state(m)->disable); |
| 966 | Py_CLEAR(get_posixsubprocess_state(m)->enable); |
| 967 | Py_CLEAR(get_posixsubprocess_state(m)->isenabled); |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | static void _posixsubprocess_free(void *m) { |
| 972 | _posixsubprocess_clear((PyObject *)m); |
| 973 | } |
| 974 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 975 | static struct PyModuleDef _posixsubprocessmodule = { |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 976 | PyModuleDef_HEAD_INIT, |
| 977 | "_posixsubprocess", |
| 978 | module_doc, |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 979 | sizeof(_posixsubprocessstate), |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 980 | module_methods, |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 981 | NULL, |
| 982 | _posixsubprocess_traverse, |
| 983 | _posixsubprocess_clear, |
| 984 | _posixsubprocess_free, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 985 | }; |
| 986 | |
| 987 | PyMODINIT_FUNC |
| 988 | PyInit__posixsubprocess(void) |
| 989 | { |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 990 | PyObject* m; |
| 991 | |
| 992 | m = PyState_FindModule(&_posixsubprocessmodule); |
| 993 | if (m != NULL) { |
| 994 | Py_INCREF(m); |
| 995 | return m; |
| 996 | } |
| 997 | |
| 998 | m = PyModule_Create(&_posixsubprocessmodule); |
| 999 | if (m == NULL) { |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 1003 | get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable"); |
| 1004 | get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable"); |
| 1005 | get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled"); |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 1006 | |
| 1007 | PyState_AddModule(m, &_posixsubprocessmodule); |
| 1008 | return m; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1009 | } |