blob: d08c47980e9c6a926d8989dd46de620ee4abb3ac [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
Kyle Evans79925792020-10-13 15:04:44 -05003#include "pycore_fileutils.h"
Victor Stinner5572ba72011-05-26 14:10:08 +02004#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
Gregory P. Smith51ee2702010-12-13 07:59:39 +00006#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00007#include <unistd.h>
Gregory P. Smith51ee2702010-12-13 07:59:39 +00008#include <fcntl.h>
Gregory P. Smith8facece2012-01-21 14:01:08 -08009#ifdef HAVE_SYS_TYPES_H
10#include <sys/types.h>
11#endif
Gregory P. Smithf3751ef2019-10-12 13:24:56 -070012#if defined(HAVE_SYS_STAT_H)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080013#include <sys/stat.h>
14#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080015#ifdef HAVE_SYS_SYSCALL_H
16#include <sys/syscall.h>
17#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -070018#if defined(HAVE_SYS_RESOURCE_H)
19#include <sys/resource.h>
20#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080021#ifdef HAVE_DIRENT_H
22#include <dirent.h>
23#endif
Patrick McLean2b2ead72019-09-12 10:15:44 -070024#ifdef HAVE_GRP_H
25#include <grp.h>
26#endif /* HAVE_GRP_H */
27
28#include "posixmodule.h"
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000029
Gregory P. Smith3015fb82018-11-12 22:01:22 -080030#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -080031# include <sanitizer/msan_interface.h>
32#endif
33
Xavier de Gayec716f182016-06-15 11:35:29 +020034#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
Gregory P. Smithefeb9da2014-04-14 13:31:21 -070035# include <sys/linux-syscalls.h>
36# define SYS_getdents64 __NR_getdents64
37#endif
38
Jakub Kulík6f9bc722018-12-31 03:16:40 +010039#if defined(__sun) && defined(__SVR4)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080040/* readdir64 is used to work around Solaris 9 bug 6395699. */
41# define readdir readdir64
42# define dirent dirent64
43# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080044/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080045# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080046# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080047# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080048#endif
49
Gregory P. Smith4842efc2012-01-21 21:01:24 -080050#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
51# define FD_DIR "/dev/fd"
52#else
53# define FD_DIR "/proc/self/fd"
54#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000055
Patrick McLean2b2ead72019-09-12 10:15:44 -070056#ifdef NGROUPS_MAX
57#define MAX_GROUPS NGROUPS_MAX
58#else
59#define MAX_GROUPS 64
60#endif
61
Victor Stinnerdaf45552013-08-28 00:53:59 +020062#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000063
Dino Viehland5a7d2e12019-09-10 12:01:20 +010064typedef struct {
65 PyObject* disable;
66 PyObject* enable;
67 PyObject* isenabled;
68} _posixsubprocessstate;
69
70static struct PyModuleDef _posixsubprocessmodule;
71
Hai Shif707d942020-03-16 21:15:01 +080072static inline _posixsubprocessstate*
73get_posixsubprocess_state(PyObject *module)
74{
75 void *state = PyModule_GetState(module);
76 assert(state != NULL);
77 return (_posixsubprocessstate *)state;
78}
79
80#define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000081
Martin Panterafdd5132015-11-30 02:21:41 +000082/* If gc was disabled, call gc.enable(). Return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050083static int
Martin Panterafdd5132015-11-30 02:21:41 +000084_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000085{
86 PyObject *result;
Martin Panterafdd5132015-11-30 02:21:41 +000087 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020088
Martin Panterafdd5132015-11-30 02:21:41 +000089 if (need_to_reenable_gc) {
90 PyErr_Fetch(&exctype, &val, &tb);
Petr Viktorinffd97532020-02-11 17:46:57 +010091 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +010092 gc_module, _posixsubprocessstate_global->enable);
Martin Panterafdd5132015-11-30 02:21:41 +000093 if (exctype != NULL) {
94 PyErr_Restore(exctype, val, tb);
95 }
96 if (result == NULL) {
97 return 1;
98 }
99 Py_DECREF(result);
100 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000101 return 0;
102}
103
104
Gregory P. Smith8facece2012-01-21 14:01:08 -0800105/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500106static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200107_pos_int_from_ascii(const char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800108{
109 int num = 0;
110 while (*name >= '0' && *name <= '9') {
111 num = num * 10 + (*name - '0');
112 ++name;
113 }
114 if (*name)
115 return -1; /* Non digit found, not a number. */
116 return num;
117}
118
119
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800120#if defined(__FreeBSD__)
121/* When /dev/fd isn't mounted it is often a static directory populated
122 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
123 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
124 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
125 * that properly supports /dev/fd.
126 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500127static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +0200128_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800129{
130 struct stat dev_stat;
131 struct stat dev_fd_stat;
132 if (stat("/dev", &dev_stat) != 0)
133 return 0;
134 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200135 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800136 if (dev_stat.st_dev == dev_fd_stat.st_dev)
137 return 0; /* / == /dev == /dev/fd means it is static. #fail */
138 return 1;
139}
140#endif
141
142
Gregory P. Smith8facece2012-01-21 14:01:08 -0800143/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500144static int
145_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800146{
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300147 Py_ssize_t seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800148 long prev_fd = -1;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300149 for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) {
150 PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx);
151 long iter_fd;
152 if (!PyLong_Check(py_fd)) {
153 return 1;
154 }
155 iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800156 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300157 /* Negative, overflow, unsorted, too big for a fd. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800158 return 1;
159 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800160 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800161 }
162 return 0;
163}
164
165
166/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500167static int
168_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800169{
170 /* Binary search. */
171 Py_ssize_t search_min = 0;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300172 Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800173 if (search_max < 0)
174 return 0;
175 do {
176 long middle = (search_min + search_max) / 2;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300177 long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
Gregory P. Smith8facece2012-01-21 14:01:08 -0800178 if (fd == middle_fd)
179 return 1;
180 if (fd > middle_fd)
181 search_min = middle + 1;
182 else
183 search_max = middle - 1;
184 } while (search_min <= search_max);
185 return 0;
186}
187
Victor Stinnerdaf45552013-08-28 00:53:59 +0200188static int
189make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
190{
191 Py_ssize_t i, len;
192
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300193 len = PyTuple_GET_SIZE(py_fds_to_keep);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200194 for (i = 0; i < len; ++i) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300195 PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200196 long fd = PyLong_AsLong(fdobj);
197 assert(!PyErr_Occurred());
198 assert(0 <= fd && fd <= INT_MAX);
199 if (fd == errpipe_write) {
200 /* errpipe_write is part of py_fds_to_keep. It must be closed at
201 exec(), but kept open in the child process until exec() is
202 called. */
203 continue;
204 }
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300205 if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200206 return -1;
207 }
208 return 0;
209}
210
Gregory P. Smith8facece2012-01-21 14:01:08 -0800211
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700212/* Get the maximum file descriptor that could be opened by this process.
213 * This function is async signal safe for use between fork() and exec().
214 */
215static long
216safe_get_max_fd(void)
217{
218 long local_max_fd;
219#if defined(__NetBSD__)
220 local_max_fd = fcntl(0, F_MAXFD);
221 if (local_max_fd >= 0)
222 return local_max_fd;
223#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700224#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
225 struct rlimit rl;
226 /* Not on the POSIX async signal safe functions list but likely
227 * safe. TODO - Someone should audit OpenBSD to make sure. */
228 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
229 return (long) rl.rlim_max;
230#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700231#ifdef _SC_OPEN_MAX
232 local_max_fd = sysconf(_SC_OPEN_MAX);
233 if (local_max_fd == -1)
234#endif
235 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
236 return local_max_fd;
237}
238
239
240/* Close all file descriptors in the range from start_fd and higher
241 * except for those in py_fds_to_keep. If the range defined by
242 * [start_fd, safe_get_max_fd()) is large this will take a long
243 * time as it calls close() on EVERY possible fd.
244 *
245 * It isn't possible to know for sure what the max fd to go up to
246 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800247 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500248static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700249_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800250{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700251 long end_fd = safe_get_max_fd();
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300252 Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800253 Py_ssize_t keep_seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800254 /* 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;
Kyle Evansc230fde2020-10-11 13:54:11 -0500261 _Py_closerange(start_fd, keep_fd - 1);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800262 start_fd = keep_fd + 1;
263 }
264 if (start_fd <= end_fd) {
Kyle Evansc230fde2020-10-11 13:54:11 -0500265 _Py_closerange(start_fd, end_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800266 }
267}
268
269
270#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
271/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
272 * only to read a directory of short file descriptor number names. The kernel
273 * will return an error if we didn't give it enough space. Highly Unlikely.
274 * This structure is very old and stable: It will not change unless the kernel
275 * chooses to break compatibility with all existing binaries. Highly Unlikely.
276 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800277struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700278 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800279 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800280 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800281 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800282 char d_name[256]; /* Filename (null-terminated) */
283};
284
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700285/* Close all open file descriptors in the range from start_fd and higher
286 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800287 *
288 * This version is async signal safe as it does not make any unsafe C library
289 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
290 * to resort to making a kernel system call directly but this is the ONLY api
291 * available that does no harm. opendir/readdir/closedir perform memory
292 * allocation and locking so while they usually work they are not guaranteed
293 * to (especially if you have replaced your malloc implementation). A version
294 * of this function that uses those can be found in the _maybe_unsafe variant.
295 *
296 * This is Linux specific because that is all I am ready to test it on. It
297 * should be easy to add OS specific dirent or dirent64 structures and modify
298 * it with some cpp #define magic to work on other OSes as well if you want.
299 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500300static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700301_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800302{
303 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200304
Victor Stinner160e8192015-03-30 02:18:31 +0200305 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800306 if (fd_dir_fd == -1) {
307 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700308 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800309 return;
310 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800311 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800313 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
314 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800315 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800316 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800317 int offset;
Gregory P. Smith3015fb82018-11-12 22:01:22 -0800318#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -0800319 __msan_unpoison(buffer, bytes);
320#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800321 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
322 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800323 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800324 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
325 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700326 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800327 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200328 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329 }
330 }
331 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200332 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800333 }
334}
335
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700336#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800337
338#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
339
340
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700341/* Close all open file descriptors from start_fd and higher.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300342 * Do not close any in the sorted py_fds_to_keep tuple.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800343 *
344 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800345 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800346 * likely to ever cause a problem is opendir() as it performs an internal
347 * malloc(). Practically this should not be a problem. The Java VM makes the
348 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
349 *
350 * readdir_r() is not used because it provides no benefit. It is typically
351 * implemented as readdir() followed by memcpy(). See also:
352 * http://womble.decadent.org.uk/readdir_r-advisory.html
353 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500354static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700355_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800356{
357 DIR *proc_fd_dir;
358#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700359 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800360 ++start_fd;
361 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800362 /* Close our lowest fd before we call opendir so that it is likely to
363 * reuse that fd otherwise we might close opendir's file descriptor in
364 * our loop. This trick assumes that fd's are allocated on a lowest
365 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200366 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800367 ++start_fd;
368#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800369
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800370#if defined(__FreeBSD__)
371 if (!_is_fdescfs_mounted_on_dev_fd())
372 proc_fd_dir = NULL;
373 else
374#endif
375 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800376 if (!proc_fd_dir) {
377 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700378 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800379 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800380 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800381#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800382 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800383#else
384 int fd_used_by_opendir = start_fd - 1;
385#endif
386 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800387 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800388 int fd;
389 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
390 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700391 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800392 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200393 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800394 }
395 errno = 0;
396 }
397 if (errno) {
398 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700399 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800400 }
401 closedir(proc_fd_dir);
402 }
403}
404
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700405#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800406
407#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
408
409
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000410/*
411 * This function is code executed in the child process immediately after fork
412 * to set things up and call exec().
413 *
414 * All of the code in this function must only use async-signal-safe functions,
415 * listed at `man 7 signal` or
416 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
417 *
418 * This restriction is documented at
419 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
420 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500421static void
422child_exec(char *const exec_array[],
423 char *const argv[],
424 char *const envp[],
425 const char *cwd,
426 int p2cread, int p2cwrite,
427 int c2pread, int c2pwrite,
428 int errread, int errwrite,
429 int errpipe_read, int errpipe_write,
430 int close_fds, int restore_signals,
431 int call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700432 int call_setgid, gid_t gid,
433 int call_setgroups, size_t groups_size, const gid_t *groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700434 int call_setuid, uid_t uid, int child_umask,
Benjamin Peterson91eef982012-01-22 20:04:46 -0500435 PyObject *py_fds_to_keep,
436 PyObject *preexec_fn,
437 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000438{
Victor Stinner185fd332015-04-01 18:35:53 +0200439 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000440 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000441 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000442 /* Buffer large enough to hold a hex integer. We can't malloc. */
443 char hex_errno[sizeof(saved_errno)*2+1];
444
Victor Stinnerdaf45552013-08-28 00:53:59 +0200445 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
446 goto error;
447
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000448 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200449 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000450 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200451 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000452 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200453 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000454 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000455 POSIX_CALL(close(errpipe_read));
456
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200457 /* When duping fds, if there arises a situation where one of the fds is
458 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
Gregory P. Smithce344102018-09-10 17:46:22 -0700459 if (c2pwrite == 0) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200460 POSIX_CALL(c2pwrite = dup(c2pwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700461 /* issue32270 */
462 if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
463 goto error;
464 }
465 }
466 while (errwrite == 0 || errwrite == 1) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200467 POSIX_CALL(errwrite = dup(errwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700468 /* issue32270 */
469 if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
470 goto error;
471 }
472 }
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200473
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000474 /* Dup fds for child.
475 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
476 would be a no-op (issue #10806). */
477 if (p2cread == 0) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300478 if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200479 goto error;
480 }
481 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000482 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200483
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000484 if (c2pwrite == 1) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300485 if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200486 goto error;
487 }
488 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000489 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200490
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000491 if (errwrite == 2) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300492 if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200493 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000494 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200495 else if (errwrite != -1)
496 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000497
Gregory P. Smithce344102018-09-10 17:46:22 -0700498 /* We no longer manually close p2cread, c2pwrite, and errwrite here as
499 * _close_open_fds takes care when it is not already non-inheritable. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000500
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000501 if (cwd)
502 POSIX_CALL(chdir(cwd));
503
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700504 if (child_umask >= 0)
505 umask(child_umask); /* umask() always succeeds. */
506
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000507 if (restore_signals)
508 _Py_RestoreSignals();
509
510#ifdef HAVE_SETSID
511 if (call_setsid)
512 POSIX_CALL(setsid());
513#endif
514
Patrick McLean2b2ead72019-09-12 10:15:44 -0700515#ifdef HAVE_SETGROUPS
516 if (call_setgroups)
517 POSIX_CALL(setgroups(groups_size, groups));
518#endif /* HAVE_SETGROUPS */
519
520#ifdef HAVE_SETREGID
521 if (call_setgid)
522 POSIX_CALL(setregid(gid, gid));
523#endif /* HAVE_SETREGID */
524
525#ifdef HAVE_SETREUID
526 if (call_setuid)
527 POSIX_CALL(setreuid(uid, uid));
528#endif /* HAVE_SETREUID */
529
530
Gregory P. Smith5591b022012-10-10 03:34:47 -0700531 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000532 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
533 /* This is where the user has asked us to deadlock their program. */
534 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
535 if (result == NULL) {
536 /* Stringifying the exception or traceback would involve
537 * memory allocation and thus potential for deadlock.
538 * We've already faced potential deadlock by calling back
539 * into Python in the first place, so it probably doesn't
540 * matter but we avoid it to minimize the possibility. */
541 err_msg = "Exception occurred in preexec_fn.";
542 errno = 0; /* We don't want to report an OSError. */
543 goto error;
544 }
545 /* Py_DECREF(result); - We're about to exec so why bother? */
546 }
547
Charles-François Natali249cdc32013-08-25 18:24:45 +0200548 /* close FDs after executing preexec_fn, which might open FDs */
549 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200550 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700551 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200552 }
553
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000554 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
555 /* given the executable_list generated by Lib/subprocess.py. */
556 saved_errno = 0;
557 for (i = 0; exec_array[i] != NULL; ++i) {
558 const char *executable = exec_array[i];
559 if (envp) {
560 execve(executable, argv, envp);
561 } else {
562 execv(executable, argv);
563 }
564 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
565 saved_errno = errno;
566 }
567 }
568 /* Report the first exec error, not the last. */
569 if (saved_errno)
570 errno = saved_errno;
571
572error:
573 saved_errno = errno;
574 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800575 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200576 less than PIPEBUF and we cannot do anything about an error anyways.
577 Use _Py_write_noraise() to retry write() if it is interrupted by a
578 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000579 if (saved_errno) {
580 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200581 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000582 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300583 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000584 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000585 saved_errno /= 16;
586 }
Victor Stinner185fd332015-04-01 18:35:53 +0200587 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
588 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700589 if (!reached_preexec) {
590 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200591 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700592 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000593 /* We can't call strerror(saved_errno). It is not async signal safe.
594 * The parent process will look the error message up. */
595 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200596 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
597 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000598 }
599}
600
601
602static PyObject *
603subprocess_fork_exec(PyObject* self, PyObject *args)
604{
605 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200606 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000607 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000608 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000609 PyObject *preexec_fn_args_tuple = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700610 PyObject *groups_list;
611 PyObject *uid_object, *gid_object;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000612 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
613 int errpipe_read, errpipe_write, close_fds, restore_signals;
614 int call_setsid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700615 int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
616 uid_t uid;
617 gid_t gid, *groups = NULL;
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700618 int child_umask;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000619 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000620 const char *cwd;
621 pid_t pid;
622 int need_to_reenable_gc = 0;
623 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700624 Py_ssize_t arg_num, num_groups = 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200625 int need_after_fork = 0;
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700626 int saved_errno = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000627
628 if (!PyArg_ParseTuple(
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700629 args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300630 &process_args, &executable_list,
631 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000632 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000633 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
634 &errread, &errwrite, &errpipe_read, &errpipe_write,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700635 &restore_signals, &call_setsid,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700636 &gid_object, &groups_list, &uid_object, &child_umask,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700637 &preexec_fn))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000638 return NULL;
639
Christian Heimes98d90f72019-08-27 23:36:56 +0200640 if ((preexec_fn != Py_None) &&
Victor Stinnerbe793732020-03-13 18:15:33 +0100641 (PyInterpreterState_Get() != PyInterpreterState_Main())) {
Christian Heimes98d90f72019-08-27 23:36:56 +0200642 PyErr_SetString(PyExc_RuntimeError,
643 "preexec_fn not supported within subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -0700644 return NULL;
645 }
646
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800647 if (close_fds && errpipe_write < 3) { /* precondition */
648 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
649 return NULL;
650 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800651 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
652 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000653 return NULL;
654 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000655
Victor Stinner252346a2020-05-01 11:33:44 +0200656 PyInterpreterState *interp = PyInterpreterState_Get();
657 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
658 if (config->_isolated_interpreter) {
659 PyErr_SetString(PyExc_RuntimeError,
660 "subprocess not supported for isolated subinterpreters");
661 return NULL;
662 }
663
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000664 /* We need to call gc.disable() when we'll be calling preexec_fn */
665 if (preexec_fn != Py_None) {
666 PyObject *result;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200667
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000668 gc_module = PyImport_ImportModule("gc");
669 if (gc_module == NULL)
670 return NULL;
Petr Viktorinffd97532020-02-11 17:46:57 +0100671 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100672 gc_module, _posixsubprocessstate_global->isenabled);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000673 if (result == NULL) {
674 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000675 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000676 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000677 need_to_reenable_gc = PyObject_IsTrue(result);
678 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000679 if (need_to_reenable_gc == -1) {
680 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000681 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000682 }
Petr Viktorinffd97532020-02-11 17:46:57 +0100683 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100684 gc_module, _posixsubprocessstate_global->disable);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000685 if (result == NULL) {
686 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000687 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000688 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000689 Py_DECREF(result);
690 }
691
692 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200693 if (!exec_array)
694 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000695
696 /* Convert args and env into appropriate arguments for exec() */
697 /* These conversions are done in the parent process to avoid allocating
698 or freeing memory in the child process. */
699 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000700 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000701 /* Equivalent to: */
702 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000703 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200704 if (fast_args == NULL)
705 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000706 num_args = PySequence_Fast_GET_SIZE(fast_args);
707 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000708 if (converted_args == NULL)
709 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000710 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000711 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300712 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
713 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
714 goto cleanup;
715 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000716 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000717 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
718 goto cleanup;
719 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
720 }
721
722 argv = _PySequence_BytesToCharpArray(converted_args);
723 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000724 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000725 if (!argv)
726 goto cleanup;
727 }
728
729 if (env_list != Py_None) {
730 envp = _PySequence_BytesToCharpArray(env_list);
731 if (!envp)
732 goto cleanup;
733 }
734
Victor Stinner0e59cc32010-04-16 23:49:32 +0000735 if (cwd_obj != Py_None) {
736 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
737 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000738 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000739 } else {
740 cwd = NULL;
741 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000742 }
743
Patrick McLean2b2ead72019-09-12 10:15:44 -0700744 if (groups_list != Py_None) {
745#ifdef HAVE_SETGROUPS
746 Py_ssize_t i;
747 unsigned long gid;
748
749 if (!PyList_Check(groups_list)) {
750 PyErr_SetString(PyExc_TypeError,
751 "setgroups argument must be a list");
752 goto cleanup;
753 }
754 num_groups = PySequence_Size(groups_list);
755
756 if (num_groups < 0)
757 goto cleanup;
758
759 if (num_groups > MAX_GROUPS) {
760 PyErr_SetString(PyExc_ValueError, "too many groups");
761 goto cleanup;
762 }
763
764 if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
765 PyErr_SetString(PyExc_MemoryError,
766 "failed to allocate memory for group list");
767 goto cleanup;
768 }
769
770 for (i = 0; i < num_groups; i++) {
771 PyObject *elem;
772 elem = PySequence_GetItem(groups_list, i);
773 if (!elem)
774 goto cleanup;
775 if (!PyLong_Check(elem)) {
776 PyErr_SetString(PyExc_TypeError,
777 "groups must be integers");
778 Py_DECREF(elem);
779 goto cleanup;
780 } else {
781 /* In posixmodule.c UnsignedLong is used as a fallback value
782 * if the value provided does not fit in a Long. Since we are
783 * already doing the bounds checking on the Python side, we
784 * can go directly to an UnsignedLong here. */
785 if (!_Py_Gid_Converter(elem, &gid)) {
786 Py_DECREF(elem);
787 PyErr_SetString(PyExc_ValueError, "invalid group id");
788 goto cleanup;
789 }
790 groups[i] = gid;
791 }
792 Py_DECREF(elem);
793 }
794 call_setgroups = 1;
795
796#else /* HAVE_SETGROUPS */
797 PyErr_BadInternalCall();
798 goto cleanup;
799#endif /* HAVE_SETGROUPS */
800 }
801
802 if (gid_object != Py_None) {
803#ifdef HAVE_SETREGID
804 if (!_Py_Gid_Converter(gid_object, &gid))
805 goto cleanup;
806
807 call_setgid = 1;
808
809#else /* HAVE_SETREGID */
810 PyErr_BadInternalCall();
811 goto cleanup;
812#endif /* HAVE_SETREUID */
813 }
814
815 if (uid_object != Py_None) {
816#ifdef HAVE_SETREUID
817 if (!_Py_Uid_Converter(uid_object, &uid))
818 goto cleanup;
819
820 call_setuid = 1;
821
822#else /* HAVE_SETREUID */
823 PyErr_BadInternalCall();
824 goto cleanup;
825#endif /* HAVE_SETREUID */
826 }
827
Gregory P. Smith163468a2017-05-29 10:03:41 -0700828 /* This must be the last thing done before fork() because we do not
829 * want to call PyOS_BeforeFork() if there is any chance of another
830 * error leading to the cleanup: code without calling fork(). */
831 if (preexec_fn != Py_None) {
832 preexec_fn_args_tuple = PyTuple_New(0);
833 if (!preexec_fn_args_tuple)
834 goto cleanup;
835 PyOS_BeforeFork();
836 need_after_fork = 1;
837 }
838
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000839 pid = fork();
840 if (pid == 0) {
841 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000842 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000843 * Code from here to _exit() must only use async-signal-safe functions,
844 * listed at `man 7 signal` or
845 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
846 */
847
848 if (preexec_fn != Py_None) {
849 /* We'll be calling back into Python later so we need to do this.
850 * This call may not be async-signal-safe but neither is calling
851 * back into Python. The user asked us to use hope as a strategy
852 * to avoid deadlock... */
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200853 PyOS_AfterFork_Child();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000854 }
855
856 child_exec(exec_array, argv, envp, cwd,
857 p2cread, p2cwrite, c2pread, c2pwrite,
858 errread, errwrite, errpipe_read, errpipe_write,
859 close_fds, restore_signals, call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700860 call_setgid, gid, call_setgroups, num_groups, groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700861 call_setuid, uid, child_umask,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800862 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000863 _exit(255);
864 return NULL; /* Dead code to avoid a potential compiler warning. */
865 }
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700866 /* Parent (original) process */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000867 if (pid == -1) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700868 /* Capture errno for the exception. */
869 saved_errno = errno;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000870 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000871
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700872 Py_XDECREF(cwd_obj2);
873
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200874 if (need_after_fork)
875 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000876 if (envp)
877 _Py_FreeCharPArray(envp);
878 if (argv)
879 _Py_FreeCharPArray(argv);
880 _Py_FreeCharPArray(exec_array);
881
882 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +0000883 if (_enable_gc(need_to_reenable_gc, gc_module)) {
884 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000885 }
Christian Heimes0d3350d2020-06-12 18:18:43 +0200886 PyMem_RawFree(groups);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000887 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000888 Py_XDECREF(gc_module);
889
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700890 if (pid == -1) {
891 errno = saved_errno;
892 /* We can't call this above as PyOS_AfterFork_Parent() calls back
893 * into Python code which would see the unreturned error. */
894 PyErr_SetFromErrno(PyExc_OSError);
895 return NULL; /* fork() failed. */
896 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000897
898 return PyLong_FromPid(pid);
899
900cleanup:
901 if (envp)
902 _Py_FreeCharPArray(envp);
903 if (argv)
904 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200905 if (exec_array)
906 _Py_FreeCharPArray(exec_array);
Patrick McLean2b2ead72019-09-12 10:15:44 -0700907
908 PyMem_RawFree(groups);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000909 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000910 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000911 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +0000912 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000913 Py_XDECREF(gc_module);
914 return NULL;
915}
916
917
918PyDoc_STRVAR(subprocess_fork_exec_doc,
Orivej Desh77abf232019-09-20 17:01:10 +0000919"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000920 p2cread, p2cwrite, c2pread, c2pwrite,\n\
921 errread, errwrite, errpipe_read, errpipe_write,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -0700922 restore_signals, call_setsid,\n\
Orivej Desh77abf232019-09-20 17:01:10 +0000923 gid, groups_list, uid,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -0700924 preexec_fn)\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000925\n\
926Forks a child process, closes parent file descriptors as appropriate in the\n\
927child and dups the few that are needed before calling exec() in the child\n\
928process.\n\
929\n\
Orivej Desh77abf232019-09-20 17:01:10 +0000930If close_fds is true, close file descriptors 3 and higher, except those listed\n\
931in the sorted tuple pass_fds.\n\
932\n\
933The preexec_fn, if supplied, will be called immediately before closing file\n\
934descriptors and exec.\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000935WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
936 It may trigger infrequent, difficult to debug deadlocks.\n\
937\n\
938If an error occurs in the child process before the exec, it is\n\
939serialized and written to the errpipe_write fd per subprocess.py.\n\
940\n\
941Returns: the child process's PID.\n\
942\n\
943Raises: Only on an error in the parent process.\n\
944");
945
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000946/* module level code ********************************************************/
947
948PyDoc_STRVAR(module_doc,
949"A POSIX helper for the subprocess module.");
950
951
952static PyMethodDef module_methods[] = {
953 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000954 {NULL, NULL} /* sentinel */
955};
956
957
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100958static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Hai Shif707d942020-03-16 21:15:01 +0800959 Py_VISIT(get_posixsubprocess_state(m)->disable);
960 Py_VISIT(get_posixsubprocess_state(m)->enable);
961 Py_VISIT(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100962 return 0;
963}
964
965static int _posixsubprocess_clear(PyObject *m) {
Hai Shif707d942020-03-16 21:15:01 +0800966 Py_CLEAR(get_posixsubprocess_state(m)->disable);
967 Py_CLEAR(get_posixsubprocess_state(m)->enable);
968 Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100969 return 0;
970}
971
972static void _posixsubprocess_free(void *m) {
973 _posixsubprocess_clear((PyObject *)m);
974}
975
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000976static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -0600977 PyModuleDef_HEAD_INIT,
978 "_posixsubprocess",
979 module_doc,
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100980 sizeof(_posixsubprocessstate),
luzpaza5293b42017-11-05 07:37:50 -0600981 module_methods,
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100982 NULL,
983 _posixsubprocess_traverse,
984 _posixsubprocess_clear,
985 _posixsubprocess_free,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000986};
987
988PyMODINIT_FUNC
989PyInit__posixsubprocess(void)
990{
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100991 PyObject* m;
992
993 m = PyState_FindModule(&_posixsubprocessmodule);
994 if (m != NULL) {
995 Py_INCREF(m);
996 return m;
997 }
998
999 m = PyModule_Create(&_posixsubprocessmodule);
1000 if (m == NULL) {
1001 return NULL;
1002 }
1003
Hai Shif707d942020-03-16 21:15:01 +08001004 get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
1005 get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
1006 get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001007
1008 PyState_AddModule(m, &_posixsubprocessmodule);
1009 return m;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001010}