blob: 5d1691ace4192003a1449749998e3732fc83fc44 [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
Victor Stinner5572ba72011-05-26 14:10:08 +02003#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
4# define _GNU_SOURCE
Gregory P. Smith51ee2702010-12-13 07:59:39 +00005#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00006#include <unistd.h>
Gregory P. Smith51ee2702010-12-13 07:59:39 +00007#include <fcntl.h>
Gregory P. Smith8facece2012-01-21 14:01:08 -08008#ifdef HAVE_SYS_TYPES_H
9#include <sys/types.h>
10#endif
Gregory P. Smithf3751ef2019-10-12 13:24:56 -070011#if defined(HAVE_SYS_STAT_H)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080012#include <sys/stat.h>
13#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080014#ifdef HAVE_SYS_SYSCALL_H
15#include <sys/syscall.h>
16#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -070017#if defined(HAVE_SYS_RESOURCE_H)
18#include <sys/resource.h>
19#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080020#ifdef HAVE_DIRENT_H
21#include <dirent.h>
22#endif
Patrick McLean2b2ead72019-09-12 10:15:44 -070023#ifdef HAVE_GRP_H
24#include <grp.h>
25#endif /* HAVE_GRP_H */
26
27#include "posixmodule.h"
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000028
Gregory P. Smith3015fb82018-11-12 22:01:22 -080029#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -080030# include <sanitizer/msan_interface.h>
31#endif
32
Xavier de Gayec716f182016-06-15 11:35:29 +020033#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
Gregory P. Smithefeb9da2014-04-14 13:31:21 -070034# include <sys/linux-syscalls.h>
35# define SYS_getdents64 __NR_getdents64
36#endif
37
Jakub Kulík6f9bc722018-12-31 03:16:40 +010038#if defined(__sun) && defined(__SVR4)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080039/* 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. Smith8facece2012-01-21 14:01:08 -080043/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080044# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080045# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080046# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080047#endif
48
Gregory P. Smith4842efc2012-01-21 21:01:24 -080049#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. Smithfb94c5f2010-03-14 06:49:55 +000054
Patrick McLean2b2ead72019-09-12 10:15:44 -070055#ifdef NGROUPS_MAX
56#define MAX_GROUPS NGROUPS_MAX
57#else
58#define MAX_GROUPS 64
59#endif
60
Victor Stinnerdaf45552013-08-28 00:53:59 +020061#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000062
Dino Viehland5a7d2e12019-09-10 12:01:20 +010063typedef struct {
64 PyObject* disable;
65 PyObject* enable;
66 PyObject* isenabled;
67} _posixsubprocessstate;
68
69static struct PyModuleDef _posixsubprocessmodule;
70
Hai Shif707d942020-03-16 21:15:01 +080071static inline _posixsubprocessstate*
72get_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. Smithfb94c5f2010-03-14 06:49:55 +000080
Martin Panterafdd5132015-11-30 02:21:41 +000081/* If gc was disabled, call gc.enable(). Return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050082static int
Martin Panterafdd5132015-11-30 02:21:41 +000083_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000084{
85 PyObject *result;
Martin Panterafdd5132015-11-30 02:21:41 +000086 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020087
Martin Panterafdd5132015-11-30 02:21:41 +000088 if (need_to_reenable_gc) {
89 PyErr_Fetch(&exctype, &val, &tb);
Petr Viktorinffd97532020-02-11 17:46:57 +010090 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +010091 gc_module, _posixsubprocessstate_global->enable);
Martin Panterafdd5132015-11-30 02:21:41 +000092 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. Smithfb94c5f2010-03-14 06:49:55 +0000100 return 0;
101}
102
103
Gregory P. Smith8facece2012-01-21 14:01:08 -0800104/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500105static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200106_pos_int_from_ascii(const char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800107{
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. Smith4842efc2012-01-21 21:01:24 -0800119#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 Peterson91eef982012-01-22 20:04:46 -0500126static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +0200127_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800128{
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 Stinnerdaf45552013-08-28 00:53:59 +0200134 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800135 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. Smith8facece2012-01-21 14:01:08 -0800142/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500143static int
144_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800145{
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300146 Py_ssize_t seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800147 long prev_fd = -1;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300148 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. Smithd0a5b1c2015-11-15 21:15:26 -0800155 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300156 /* Negative, overflow, unsorted, too big for a fd. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800157 return 1;
158 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800159 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800160 }
161 return 0;
162}
163
164
165/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500166static int
167_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800168{
169 /* Binary search. */
170 Py_ssize_t search_min = 0;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300171 Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800172 if (search_max < 0)
173 return 0;
174 do {
175 long middle = (search_min + search_max) / 2;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300176 long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
Gregory P. Smith8facece2012-01-21 14:01:08 -0800177 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 Stinnerdaf45552013-08-28 00:53:59 +0200187static int
188make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
189{
190 Py_ssize_t i, len;
191
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300192 len = PyTuple_GET_SIZE(py_fds_to_keep);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200193 for (i = 0; i < len; ++i) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300194 PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200195 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 Izbyshevc1e46e92018-02-06 09:09:34 +0300204 if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200205 return -1;
206 }
207 return 0;
208}
209
Gregory P. Smith8facece2012-01-21 14:01:08 -0800210
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700211/* 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 */
214static long
215safe_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. Smithf9681772015-04-25 23:43:34 -0700223#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. Smithd4dcb702014-06-01 13:18:28 -0700230#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. Smith8facece2012-01-21 14:01:08 -0800246 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500247static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700248_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800249{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700250 long end_fd = safe_get_max_fd();
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300251 Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800252 Py_ssize_t keep_seq_idx;
253 int fd_num;
254 /* As py_fds_to_keep is sorted we can loop through the list closing
luzpaza5293b42017-11-05 07:37:50 -0600255 * fds in between any in the keep list falling within our range. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800256 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300257 PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800258 int keep_fd = PyLong_AsLong(py_keep_fd);
259 if (keep_fd < start_fd)
260 continue;
261 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200262 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800263 }
264 start_fd = keep_fd + 1;
265 }
266 if (start_fd <= end_fd) {
Victor Stinnere6f8abd2020-04-24 12:06:58 +0200267#if defined(__FreeBSD__)
268 /* Any errors encountered while closing file descriptors are ignored */
269 closefrom(start_fd);
270#else
Gregory P. Smith8facece2012-01-21 14:01:08 -0800271 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
Victor Stinnere6f8abd2020-04-24 12:06:58 +0200272 /* Ignore errors */
273 (void)close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800274 }
Victor Stinnere6f8abd2020-04-24 12:06:58 +0200275#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800276 }
277}
278
279
280#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
281/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
282 * only to read a directory of short file descriptor number names. The kernel
283 * will return an error if we didn't give it enough space. Highly Unlikely.
284 * This structure is very old and stable: It will not change unless the kernel
285 * chooses to break compatibility with all existing binaries. Highly Unlikely.
286 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800287struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700288 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800289 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800290 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800291 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800292 char d_name[256]; /* Filename (null-terminated) */
293};
294
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700295/* Close all open file descriptors in the range from start_fd and higher
296 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800297 *
298 * This version is async signal safe as it does not make any unsafe C library
299 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
300 * to resort to making a kernel system call directly but this is the ONLY api
301 * available that does no harm. opendir/readdir/closedir perform memory
302 * allocation and locking so while they usually work they are not guaranteed
303 * to (especially if you have replaced your malloc implementation). A version
304 * of this function that uses those can be found in the _maybe_unsafe variant.
305 *
306 * This is Linux specific because that is all I am ready to test it on. It
307 * should be easy to add OS specific dirent or dirent64 structures and modify
308 * it with some cpp #define magic to work on other OSes as well if you want.
309 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500310static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700311_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312{
313 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200314
Victor Stinner160e8192015-03-30 02:18:31 +0200315 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800316 if (fd_dir_fd == -1) {
317 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700318 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800319 return;
320 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800321 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800322 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800323 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
324 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800325 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800326 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800327 int offset;
Gregory P. Smith3015fb82018-11-12 22:01:22 -0800328#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -0800329 __msan_unpoison(buffer, bytes);
330#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800331 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
332 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800333 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800334 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
335 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700336 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800337 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200338 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800339 }
340 }
341 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200342 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800343 }
344}
345
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700346#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800347
348#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
349
350
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700351/* Close all open file descriptors from start_fd and higher.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300352 * Do not close any in the sorted py_fds_to_keep tuple.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800353 *
354 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800355 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800356 * likely to ever cause a problem is opendir() as it performs an internal
357 * malloc(). Practically this should not be a problem. The Java VM makes the
358 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
359 *
360 * readdir_r() is not used because it provides no benefit. It is typically
361 * implemented as readdir() followed by memcpy(). See also:
362 * http://womble.decadent.org.uk/readdir_r-advisory.html
363 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500364static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700365_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800366{
367 DIR *proc_fd_dir;
368#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700369 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800370 ++start_fd;
371 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800372 /* Close our lowest fd before we call opendir so that it is likely to
373 * reuse that fd otherwise we might close opendir's file descriptor in
374 * our loop. This trick assumes that fd's are allocated on a lowest
375 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200376 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800377 ++start_fd;
378#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800379
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800380#if defined(__FreeBSD__)
381 if (!_is_fdescfs_mounted_on_dev_fd())
382 proc_fd_dir = NULL;
383 else
384#endif
385 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800386 if (!proc_fd_dir) {
387 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700388 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800389 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800390 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800391#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800392 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800393#else
394 int fd_used_by_opendir = start_fd - 1;
395#endif
396 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800397 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800398 int fd;
399 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
400 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700401 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800402 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200403 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800404 }
405 errno = 0;
406 }
407 if (errno) {
408 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700409 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800410 }
411 closedir(proc_fd_dir);
412 }
413}
414
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700415#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800416
417#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
418
419
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000420/*
421 * This function is code executed in the child process immediately after fork
422 * to set things up and call exec().
423 *
424 * All of the code in this function must only use async-signal-safe functions,
425 * listed at `man 7 signal` or
426 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
427 *
428 * This restriction is documented at
429 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
430 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500431static void
432child_exec(char *const exec_array[],
433 char *const argv[],
434 char *const envp[],
435 const char *cwd,
436 int p2cread, int p2cwrite,
437 int c2pread, int c2pwrite,
438 int errread, int errwrite,
439 int errpipe_read, int errpipe_write,
440 int close_fds, int restore_signals,
441 int call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700442 int call_setgid, gid_t gid,
443 int call_setgroups, size_t groups_size, const gid_t *groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700444 int call_setuid, uid_t uid, int child_umask,
Benjamin Peterson91eef982012-01-22 20:04:46 -0500445 PyObject *py_fds_to_keep,
446 PyObject *preexec_fn,
447 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000448{
Victor Stinner185fd332015-04-01 18:35:53 +0200449 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000450 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000451 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000452 /* Buffer large enough to hold a hex integer. We can't malloc. */
453 char hex_errno[sizeof(saved_errno)*2+1];
454
Victor Stinnerdaf45552013-08-28 00:53:59 +0200455 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
456 goto error;
457
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000458 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200459 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000460 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200461 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000462 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200463 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000464 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000465 POSIX_CALL(close(errpipe_read));
466
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200467 /* When duping fds, if there arises a situation where one of the fds is
468 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
Gregory P. Smithce344102018-09-10 17:46:22 -0700469 if (c2pwrite == 0) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200470 POSIX_CALL(c2pwrite = dup(c2pwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700471 /* issue32270 */
472 if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
473 goto error;
474 }
475 }
476 while (errwrite == 0 || errwrite == 1) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200477 POSIX_CALL(errwrite = dup(errwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700478 /* issue32270 */
479 if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
480 goto error;
481 }
482 }
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200483
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000484 /* Dup fds for child.
485 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
486 would be a no-op (issue #10806). */
487 if (p2cread == 0) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300488 if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200489 goto error;
490 }
491 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000492 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200493
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000494 if (c2pwrite == 1) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300495 if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200496 goto error;
497 }
498 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000499 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200500
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000501 if (errwrite == 2) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300502 if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200503 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000504 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200505 else if (errwrite != -1)
506 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000507
Gregory P. Smithce344102018-09-10 17:46:22 -0700508 /* We no longer manually close p2cread, c2pwrite, and errwrite here as
509 * _close_open_fds takes care when it is not already non-inheritable. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000510
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000511 if (cwd)
512 POSIX_CALL(chdir(cwd));
513
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700514 if (child_umask >= 0)
515 umask(child_umask); /* umask() always succeeds. */
516
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000517 if (restore_signals)
518 _Py_RestoreSignals();
519
520#ifdef HAVE_SETSID
521 if (call_setsid)
522 POSIX_CALL(setsid());
523#endif
524
Patrick McLean2b2ead72019-09-12 10:15:44 -0700525#ifdef HAVE_SETGROUPS
526 if (call_setgroups)
527 POSIX_CALL(setgroups(groups_size, groups));
528#endif /* HAVE_SETGROUPS */
529
530#ifdef HAVE_SETREGID
531 if (call_setgid)
532 POSIX_CALL(setregid(gid, gid));
533#endif /* HAVE_SETREGID */
534
535#ifdef HAVE_SETREUID
536 if (call_setuid)
537 POSIX_CALL(setreuid(uid, uid));
538#endif /* HAVE_SETREUID */
539
540
Gregory P. Smith5591b022012-10-10 03:34:47 -0700541 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000542 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
543 /* This is where the user has asked us to deadlock their program. */
544 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
545 if (result == NULL) {
546 /* Stringifying the exception or traceback would involve
547 * memory allocation and thus potential for deadlock.
548 * We've already faced potential deadlock by calling back
549 * into Python in the first place, so it probably doesn't
550 * matter but we avoid it to minimize the possibility. */
551 err_msg = "Exception occurred in preexec_fn.";
552 errno = 0; /* We don't want to report an OSError. */
553 goto error;
554 }
555 /* Py_DECREF(result); - We're about to exec so why bother? */
556 }
557
Charles-François Natali249cdc32013-08-25 18:24:45 +0200558 /* close FDs after executing preexec_fn, which might open FDs */
559 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200560 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700561 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200562 }
563
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000564 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
565 /* given the executable_list generated by Lib/subprocess.py. */
566 saved_errno = 0;
567 for (i = 0; exec_array[i] != NULL; ++i) {
568 const char *executable = exec_array[i];
569 if (envp) {
570 execve(executable, argv, envp);
571 } else {
572 execv(executable, argv);
573 }
574 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
575 saved_errno = errno;
576 }
577 }
578 /* Report the first exec error, not the last. */
579 if (saved_errno)
580 errno = saved_errno;
581
582error:
583 saved_errno = errno;
584 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800585 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200586 less than PIPEBUF and we cannot do anything about an error anyways.
587 Use _Py_write_noraise() to retry write() if it is interrupted by a
588 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000589 if (saved_errno) {
590 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200591 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000592 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300593 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000594 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000595 saved_errno /= 16;
596 }
Victor Stinner185fd332015-04-01 18:35:53 +0200597 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
598 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700599 if (!reached_preexec) {
600 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200601 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700602 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000603 /* We can't call strerror(saved_errno). It is not async signal safe.
604 * The parent process will look the error message up. */
605 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200606 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
607 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000608 }
609}
610
611
612static PyObject *
613subprocess_fork_exec(PyObject* self, PyObject *args)
614{
615 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200616 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000617 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000618 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000619 PyObject *preexec_fn_args_tuple = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700620 PyObject *groups_list;
621 PyObject *uid_object, *gid_object;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000622 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
623 int errpipe_read, errpipe_write, close_fds, restore_signals;
624 int call_setsid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700625 int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
626 uid_t uid;
627 gid_t gid, *groups = NULL;
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700628 int child_umask;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000629 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000630 const char *cwd;
631 pid_t pid;
632 int need_to_reenable_gc = 0;
633 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700634 Py_ssize_t arg_num, num_groups = 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200635 int need_after_fork = 0;
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700636 int saved_errno = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000637
638 if (!PyArg_ParseTuple(
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700639 args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300640 &process_args, &executable_list,
641 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000642 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000643 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
644 &errread, &errwrite, &errpipe_read, &errpipe_write,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700645 &restore_signals, &call_setsid,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700646 &gid_object, &groups_list, &uid_object, &child_umask,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700647 &preexec_fn))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000648 return NULL;
649
Christian Heimes98d90f72019-08-27 23:36:56 +0200650 if ((preexec_fn != Py_None) &&
Victor Stinnerbe793732020-03-13 18:15:33 +0100651 (PyInterpreterState_Get() != PyInterpreterState_Main())) {
Christian Heimes98d90f72019-08-27 23:36:56 +0200652 PyErr_SetString(PyExc_RuntimeError,
653 "preexec_fn not supported within subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -0700654 return NULL;
655 }
656
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800657 if (close_fds && errpipe_write < 3) { /* precondition */
658 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
659 return NULL;
660 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800661 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
662 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000663 return NULL;
664 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000665
Victor Stinner252346a2020-05-01 11:33:44 +0200666 PyInterpreterState *interp = PyInterpreterState_Get();
667 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
668 if (config->_isolated_interpreter) {
669 PyErr_SetString(PyExc_RuntimeError,
670 "subprocess not supported for isolated subinterpreters");
671 return NULL;
672 }
673
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000674 /* We need to call gc.disable() when we'll be calling preexec_fn */
675 if (preexec_fn != Py_None) {
676 PyObject *result;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200677
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000678 gc_module = PyImport_ImportModule("gc");
679 if (gc_module == NULL)
680 return NULL;
Petr Viktorinffd97532020-02-11 17:46:57 +0100681 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100682 gc_module, _posixsubprocessstate_global->isenabled);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000683 if (result == NULL) {
684 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000685 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000686 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000687 need_to_reenable_gc = PyObject_IsTrue(result);
688 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000689 if (need_to_reenable_gc == -1) {
690 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000691 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000692 }
Petr Viktorinffd97532020-02-11 17:46:57 +0100693 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100694 gc_module, _posixsubprocessstate_global->disable);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000695 if (result == NULL) {
696 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000697 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000698 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000699 Py_DECREF(result);
700 }
701
702 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200703 if (!exec_array)
704 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000705
706 /* Convert args and env into appropriate arguments for exec() */
707 /* These conversions are done in the parent process to avoid allocating
708 or freeing memory in the child process. */
709 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000710 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000711 /* Equivalent to: */
712 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000713 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200714 if (fast_args == NULL)
715 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000716 num_args = PySequence_Fast_GET_SIZE(fast_args);
717 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000718 if (converted_args == NULL)
719 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000720 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000721 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300722 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
723 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
724 goto cleanup;
725 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000726 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000727 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
728 goto cleanup;
729 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
730 }
731
732 argv = _PySequence_BytesToCharpArray(converted_args);
733 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000734 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000735 if (!argv)
736 goto cleanup;
737 }
738
739 if (env_list != Py_None) {
740 envp = _PySequence_BytesToCharpArray(env_list);
741 if (!envp)
742 goto cleanup;
743 }
744
Victor Stinner0e59cc32010-04-16 23:49:32 +0000745 if (cwd_obj != Py_None) {
746 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
747 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000748 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000749 } else {
750 cwd = NULL;
751 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000752 }
753
Patrick McLean2b2ead72019-09-12 10:15:44 -0700754 if (groups_list != Py_None) {
755#ifdef HAVE_SETGROUPS
756 Py_ssize_t i;
757 unsigned long gid;
758
759 if (!PyList_Check(groups_list)) {
760 PyErr_SetString(PyExc_TypeError,
761 "setgroups argument must be a list");
762 goto cleanup;
763 }
764 num_groups = PySequence_Size(groups_list);
765
766 if (num_groups < 0)
767 goto cleanup;
768
769 if (num_groups > MAX_GROUPS) {
770 PyErr_SetString(PyExc_ValueError, "too many groups");
771 goto cleanup;
772 }
773
774 if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
775 PyErr_SetString(PyExc_MemoryError,
776 "failed to allocate memory for group list");
777 goto cleanup;
778 }
779
780 for (i = 0; i < num_groups; i++) {
781 PyObject *elem;
782 elem = PySequence_GetItem(groups_list, i);
783 if (!elem)
784 goto cleanup;
785 if (!PyLong_Check(elem)) {
786 PyErr_SetString(PyExc_TypeError,
787 "groups must be integers");
788 Py_DECREF(elem);
789 goto cleanup;
790 } else {
791 /* In posixmodule.c UnsignedLong is used as a fallback value
792 * if the value provided does not fit in a Long. Since we are
793 * already doing the bounds checking on the Python side, we
794 * can go directly to an UnsignedLong here. */
795 if (!_Py_Gid_Converter(elem, &gid)) {
796 Py_DECREF(elem);
797 PyErr_SetString(PyExc_ValueError, "invalid group id");
798 goto cleanup;
799 }
800 groups[i] = gid;
801 }
802 Py_DECREF(elem);
803 }
804 call_setgroups = 1;
805
806#else /* HAVE_SETGROUPS */
807 PyErr_BadInternalCall();
808 goto cleanup;
809#endif /* HAVE_SETGROUPS */
810 }
811
812 if (gid_object != Py_None) {
813#ifdef HAVE_SETREGID
814 if (!_Py_Gid_Converter(gid_object, &gid))
815 goto cleanup;
816
817 call_setgid = 1;
818
819#else /* HAVE_SETREGID */
820 PyErr_BadInternalCall();
821 goto cleanup;
822#endif /* HAVE_SETREUID */
823 }
824
825 if (uid_object != Py_None) {
826#ifdef HAVE_SETREUID
827 if (!_Py_Uid_Converter(uid_object, &uid))
828 goto cleanup;
829
830 call_setuid = 1;
831
832#else /* HAVE_SETREUID */
833 PyErr_BadInternalCall();
834 goto cleanup;
835#endif /* HAVE_SETREUID */
836 }
837
Gregory P. Smith163468a2017-05-29 10:03:41 -0700838 /* This must be the last thing done before fork() because we do not
839 * want to call PyOS_BeforeFork() if there is any chance of another
840 * error leading to the cleanup: code without calling fork(). */
841 if (preexec_fn != Py_None) {
842 preexec_fn_args_tuple = PyTuple_New(0);
843 if (!preexec_fn_args_tuple)
844 goto cleanup;
845 PyOS_BeforeFork();
846 need_after_fork = 1;
847 }
848
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000849 pid = fork();
850 if (pid == 0) {
851 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000852 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000853 * Code from here to _exit() must only use async-signal-safe functions,
854 * listed at `man 7 signal` or
855 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
856 */
857
858 if (preexec_fn != Py_None) {
859 /* We'll be calling back into Python later so we need to do this.
860 * This call may not be async-signal-safe but neither is calling
861 * back into Python. The user asked us to use hope as a strategy
862 * to avoid deadlock... */
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200863 PyOS_AfterFork_Child();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000864 }
865
866 child_exec(exec_array, argv, envp, cwd,
867 p2cread, p2cwrite, c2pread, c2pwrite,
868 errread, errwrite, errpipe_read, errpipe_write,
869 close_fds, restore_signals, call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700870 call_setgid, gid, call_setgroups, num_groups, groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700871 call_setuid, uid, child_umask,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800872 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000873 _exit(255);
874 return NULL; /* Dead code to avoid a potential compiler warning. */
875 }
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700876 /* Parent (original) process */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000877 if (pid == -1) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700878 /* Capture errno for the exception. */
879 saved_errno = errno;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000880 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000881
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700882 Py_XDECREF(cwd_obj2);
883
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200884 if (need_after_fork)
885 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000886 if (envp)
887 _Py_FreeCharPArray(envp);
888 if (argv)
889 _Py_FreeCharPArray(argv);
890 _Py_FreeCharPArray(exec_array);
891
892 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +0000893 if (_enable_gc(need_to_reenable_gc, gc_module)) {
894 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000895 }
Christian Heimes0d3350d2020-06-12 18:18:43 +0200896 PyMem_RawFree(groups);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000897 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000898 Py_XDECREF(gc_module);
899
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700900 if (pid == -1) {
901 errno = saved_errno;
902 /* We can't call this above as PyOS_AfterFork_Parent() calls back
903 * into Python code which would see the unreturned error. */
904 PyErr_SetFromErrno(PyExc_OSError);
905 return NULL; /* fork() failed. */
906 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000907
908 return PyLong_FromPid(pid);
909
910cleanup:
911 if (envp)
912 _Py_FreeCharPArray(envp);
913 if (argv)
914 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200915 if (exec_array)
916 _Py_FreeCharPArray(exec_array);
Patrick McLean2b2ead72019-09-12 10:15:44 -0700917
918 PyMem_RawFree(groups);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000919 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000920 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000921 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +0000922 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000923 Py_XDECREF(gc_module);
924 return NULL;
925}
926
927
928PyDoc_STRVAR(subprocess_fork_exec_doc,
Orivej Desh77abf232019-09-20 17:01:10 +0000929"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000930 p2cread, p2cwrite, c2pread, c2pwrite,\n\
931 errread, errwrite, errpipe_read, errpipe_write,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -0700932 restore_signals, call_setsid,\n\
Orivej Desh77abf232019-09-20 17:01:10 +0000933 gid, groups_list, uid,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -0700934 preexec_fn)\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000935\n\
936Forks a child process, closes parent file descriptors as appropriate in the\n\
937child and dups the few that are needed before calling exec() in the child\n\
938process.\n\
939\n\
Orivej Desh77abf232019-09-20 17:01:10 +0000940If close_fds is true, close file descriptors 3 and higher, except those listed\n\
941in the sorted tuple pass_fds.\n\
942\n\
943The preexec_fn, if supplied, will be called immediately before closing file\n\
944descriptors and exec.\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000945WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
946 It may trigger infrequent, difficult to debug deadlocks.\n\
947\n\
948If an error occurs in the child process before the exec, it is\n\
949serialized and written to the errpipe_write fd per subprocess.py.\n\
950\n\
951Returns: the child process's PID.\n\
952\n\
953Raises: Only on an error in the parent process.\n\
954");
955
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000956/* module level code ********************************************************/
957
958PyDoc_STRVAR(module_doc,
959"A POSIX helper for the subprocess module.");
960
961
962static PyMethodDef module_methods[] = {
963 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000964 {NULL, NULL} /* sentinel */
965};
966
967
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100968static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Hai Shif707d942020-03-16 21:15:01 +0800969 Py_VISIT(get_posixsubprocess_state(m)->disable);
970 Py_VISIT(get_posixsubprocess_state(m)->enable);
971 Py_VISIT(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100972 return 0;
973}
974
975static int _posixsubprocess_clear(PyObject *m) {
Hai Shif707d942020-03-16 21:15:01 +0800976 Py_CLEAR(get_posixsubprocess_state(m)->disable);
977 Py_CLEAR(get_posixsubprocess_state(m)->enable);
978 Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100979 return 0;
980}
981
982static void _posixsubprocess_free(void *m) {
983 _posixsubprocess_clear((PyObject *)m);
984}
985
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000986static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -0600987 PyModuleDef_HEAD_INIT,
988 "_posixsubprocess",
989 module_doc,
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100990 sizeof(_posixsubprocessstate),
luzpaza5293b42017-11-05 07:37:50 -0600991 module_methods,
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100992 NULL,
993 _posixsubprocess_traverse,
994 _posixsubprocess_clear,
995 _posixsubprocess_free,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000996};
997
998PyMODINIT_FUNC
999PyInit__posixsubprocess(void)
1000{
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001001 PyObject* m;
1002
1003 m = PyState_FindModule(&_posixsubprocessmodule);
1004 if (m != NULL) {
1005 Py_INCREF(m);
1006 return m;
1007 }
1008
1009 m = PyModule_Create(&_posixsubprocessmodule);
1010 if (m == NULL) {
1011 return NULL;
1012 }
1013
Hai Shif707d942020-03-16 21:15:01 +08001014 get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
1015 get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
1016 get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001017
1018 PyState_AddModule(m, &_posixsubprocessmodule);
1019 return m;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001020}