blob: aeb10f9ecfe47c9c3000f3457b7a2507dec18f53 [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. Smith4842efc2012-01-21 21:01:24 -080011#if defined(HAVE_SYS_STAT_H) && defined(__FreeBSD__)
12#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
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000023
Xavier de Gayec716f182016-06-15 11:35:29 +020024#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
Gregory P. Smithefeb9da2014-04-14 13:31:21 -070025# include <sys/linux-syscalls.h>
26# define SYS_getdents64 __NR_getdents64
27#endif
28
Gregory P. Smithe3f78482012-01-21 15:16:17 -080029#if defined(sun)
30/* readdir64 is used to work around Solaris 9 bug 6395699. */
31# define readdir readdir64
32# define dirent dirent64
33# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080034/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080035# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080036# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080037# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080038#endif
39
Gregory P. Smith4842efc2012-01-21 21:01:24 -080040#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
41# define FD_DIR "/dev/fd"
42#else
43# define FD_DIR "/proc/self/fd"
44#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000045
Victor Stinnerdaf45552013-08-28 00:53:59 +020046#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000047
48
Martin Panterafdd5132015-11-30 02:21:41 +000049/* If gc was disabled, call gc.enable(). Return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050050static int
Martin Panterafdd5132015-11-30 02:21:41 +000051_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000052{
53 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020054 _Py_IDENTIFIER(enable);
Martin Panterafdd5132015-11-30 02:21:41 +000055 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020056
Martin Panterafdd5132015-11-30 02:21:41 +000057 if (need_to_reenable_gc) {
58 PyErr_Fetch(&exctype, &val, &tb);
59 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
60 if (exctype != NULL) {
61 PyErr_Restore(exctype, val, tb);
62 }
63 if (result == NULL) {
64 return 1;
65 }
66 Py_DECREF(result);
67 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000068 return 0;
69}
70
71
Gregory P. Smith8facece2012-01-21 14:01:08 -080072/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050073static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020074_pos_int_from_ascii(const char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080075{
76 int num = 0;
77 while (*name >= '0' && *name <= '9') {
78 num = num * 10 + (*name - '0');
79 ++name;
80 }
81 if (*name)
82 return -1; /* Non digit found, not a number. */
83 return num;
84}
85
86
Gregory P. Smith4842efc2012-01-21 21:01:24 -080087#if defined(__FreeBSD__)
88/* When /dev/fd isn't mounted it is often a static directory populated
89 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
90 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
91 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
92 * that properly supports /dev/fd.
93 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050094static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020095_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080096{
97 struct stat dev_stat;
98 struct stat dev_fd_stat;
99 if (stat("/dev", &dev_stat) != 0)
100 return 0;
101 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200102 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800103 if (dev_stat.st_dev == dev_fd_stat.st_dev)
104 return 0; /* / == /dev == /dev/fd means it is static. #fail */
105 return 1;
106}
107#endif
108
109
Gregory P. Smith8facece2012-01-21 14:01:08 -0800110/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500111static int
112_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800113{
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300114 Py_ssize_t seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800115 long prev_fd = -1;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300116 for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) {
117 PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx);
118 long iter_fd;
119 if (!PyLong_Check(py_fd)) {
120 return 1;
121 }
122 iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800123 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300124 /* Negative, overflow, unsorted, too big for a fd. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800125 return 1;
126 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800127 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800128 }
129 return 0;
130}
131
132
133/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500134static int
135_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800136{
137 /* Binary search. */
138 Py_ssize_t search_min = 0;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300139 Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800140 if (search_max < 0)
141 return 0;
142 do {
143 long middle = (search_min + search_max) / 2;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300144 long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
Gregory P. Smith8facece2012-01-21 14:01:08 -0800145 if (fd == middle_fd)
146 return 1;
147 if (fd > middle_fd)
148 search_min = middle + 1;
149 else
150 search_max = middle - 1;
151 } while (search_min <= search_max);
152 return 0;
153}
154
Victor Stinnerdaf45552013-08-28 00:53:59 +0200155static int
156make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
157{
158 Py_ssize_t i, len;
159
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300160 len = PyTuple_GET_SIZE(py_fds_to_keep);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200161 for (i = 0; i < len; ++i) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300162 PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200163 long fd = PyLong_AsLong(fdobj);
164 assert(!PyErr_Occurred());
165 assert(0 <= fd && fd <= INT_MAX);
166 if (fd == errpipe_write) {
167 /* errpipe_write is part of py_fds_to_keep. It must be closed at
168 exec(), but kept open in the child process until exec() is
169 called. */
170 continue;
171 }
Miss Islington (bot)2bb0bfa2018-02-05 22:31:22 -0800172 if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200173 return -1;
174 }
175 return 0;
176}
177
Gregory P. Smith8facece2012-01-21 14:01:08 -0800178
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700179/* Get the maximum file descriptor that could be opened by this process.
180 * This function is async signal safe for use between fork() and exec().
181 */
182static long
183safe_get_max_fd(void)
184{
185 long local_max_fd;
186#if defined(__NetBSD__)
187 local_max_fd = fcntl(0, F_MAXFD);
188 if (local_max_fd >= 0)
189 return local_max_fd;
190#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700191#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
192 struct rlimit rl;
193 /* Not on the POSIX async signal safe functions list but likely
194 * safe. TODO - Someone should audit OpenBSD to make sure. */
195 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
196 return (long) rl.rlim_max;
197#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700198#ifdef _SC_OPEN_MAX
199 local_max_fd = sysconf(_SC_OPEN_MAX);
200 if (local_max_fd == -1)
201#endif
202 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
203 return local_max_fd;
204}
205
206
207/* Close all file descriptors in the range from start_fd and higher
208 * except for those in py_fds_to_keep. If the range defined by
209 * [start_fd, safe_get_max_fd()) is large this will take a long
210 * time as it calls close() on EVERY possible fd.
211 *
212 * It isn't possible to know for sure what the max fd to go up to
213 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800214 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500215static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700216_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800217{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700218 long end_fd = safe_get_max_fd();
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300219 Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800220 Py_ssize_t keep_seq_idx;
221 int fd_num;
222 /* As py_fds_to_keep is sorted we can loop through the list closing
luzpaza5293b42017-11-05 07:37:50 -0600223 * fds in between any in the keep list falling within our range. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800224 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300225 PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800226 int keep_fd = PyLong_AsLong(py_keep_fd);
227 if (keep_fd < start_fd)
228 continue;
229 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200230 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800231 }
232 start_fd = keep_fd + 1;
233 }
234 if (start_fd <= end_fd) {
235 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200236 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800237 }
238 }
239}
240
241
242#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
243/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
244 * only to read a directory of short file descriptor number names. The kernel
245 * will return an error if we didn't give it enough space. Highly Unlikely.
246 * This structure is very old and stable: It will not change unless the kernel
247 * chooses to break compatibility with all existing binaries. Highly Unlikely.
248 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800249struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700250 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800251 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800252 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800253 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800254 char d_name[256]; /* Filename (null-terminated) */
255};
256
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700257/* Close all open file descriptors in the range from start_fd and higher
258 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800259 *
260 * This version is async signal safe as it does not make any unsafe C library
261 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
262 * to resort to making a kernel system call directly but this is the ONLY api
263 * available that does no harm. opendir/readdir/closedir perform memory
264 * allocation and locking so while they usually work they are not guaranteed
265 * to (especially if you have replaced your malloc implementation). A version
266 * of this function that uses those can be found in the _maybe_unsafe variant.
267 *
268 * This is Linux specific because that is all I am ready to test it on. It
269 * should be easy to add OS specific dirent or dirent64 structures and modify
270 * it with some cpp #define magic to work on other OSes as well if you want.
271 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500272static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700273_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800274{
275 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200276
Victor Stinner160e8192015-03-30 02:18:31 +0200277 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800278 if (fd_dir_fd == -1) {
279 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700280 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800281 return;
282 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800283 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800284 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800285 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
286 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800287 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800288 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800289 int offset;
290 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
291 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800292 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800293 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
294 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700295 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800296 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200297 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800298 }
299 }
300 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200301 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800302 }
303}
304
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700305#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800306
307#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
308
309
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700310/* Close all open file descriptors from start_fd and higher.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300311 * Do not close any in the sorted py_fds_to_keep tuple.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312 *
313 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800314 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800315 * likely to ever cause a problem is opendir() as it performs an internal
316 * malloc(). Practically this should not be a problem. The Java VM makes the
317 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
318 *
319 * readdir_r() is not used because it provides no benefit. It is typically
320 * implemented as readdir() followed by memcpy(). See also:
321 * http://womble.decadent.org.uk/readdir_r-advisory.html
322 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500323static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700324_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800325{
326 DIR *proc_fd_dir;
327#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700328 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329 ++start_fd;
330 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800331 /* Close our lowest fd before we call opendir so that it is likely to
332 * reuse that fd otherwise we might close opendir's file descriptor in
333 * our loop. This trick assumes that fd's are allocated on a lowest
334 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200335 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800336 ++start_fd;
337#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800338
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800339#if defined(__FreeBSD__)
340 if (!_is_fdescfs_mounted_on_dev_fd())
341 proc_fd_dir = NULL;
342 else
343#endif
344 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800345 if (!proc_fd_dir) {
346 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700347 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800348 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800349 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800350#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800351 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800352#else
353 int fd_used_by_opendir = start_fd - 1;
354#endif
355 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800356 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800357 int fd;
358 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
359 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700360 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800361 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200362 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800363 }
364 errno = 0;
365 }
366 if (errno) {
367 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700368 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800369 }
370 closedir(proc_fd_dir);
371 }
372}
373
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700374#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800375
376#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
377
378
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000379/*
380 * This function is code executed in the child process immediately after fork
381 * to set things up and call exec().
382 *
383 * All of the code in this function must only use async-signal-safe functions,
384 * listed at `man 7 signal` or
385 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
386 *
387 * This restriction is documented at
388 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
389 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500390static void
391child_exec(char *const exec_array[],
392 char *const argv[],
393 char *const envp[],
394 const char *cwd,
395 int p2cread, int p2cwrite,
396 int c2pread, int c2pwrite,
397 int errread, int errwrite,
398 int errpipe_read, int errpipe_write,
399 int close_fds, int restore_signals,
400 int call_setsid,
401 PyObject *py_fds_to_keep,
402 PyObject *preexec_fn,
403 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000404{
Victor Stinner185fd332015-04-01 18:35:53 +0200405 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000406 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000407 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000408 /* Buffer large enough to hold a hex integer. We can't malloc. */
409 char hex_errno[sizeof(saved_errno)*2+1];
410
Victor Stinnerdaf45552013-08-28 00:53:59 +0200411 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
412 goto error;
413
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000414 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200415 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000416 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200417 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000418 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200419 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000420 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000421 POSIX_CALL(close(errpipe_read));
422
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200423 /* When duping fds, if there arises a situation where one of the fds is
424 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
Miss Islington (bot)9c4a63f2018-09-10 21:36:20 -0700425 if (c2pwrite == 0) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200426 POSIX_CALL(c2pwrite = dup(c2pwrite));
Miss Islington (bot)9c4a63f2018-09-10 21:36:20 -0700427 /* issue32270 */
428 if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
429 goto error;
430 }
431 }
432 while (errwrite == 0 || errwrite == 1) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200433 POSIX_CALL(errwrite = dup(errwrite));
Miss Islington (bot)9c4a63f2018-09-10 21:36:20 -0700434 /* issue32270 */
435 if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
436 goto error;
437 }
438 }
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200439
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000440 /* Dup fds for child.
441 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
442 would be a no-op (issue #10806). */
443 if (p2cread == 0) {
Miss Islington (bot)2bb0bfa2018-02-05 22:31:22 -0800444 if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200445 goto error;
446 }
447 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000448 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200449
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000450 if (c2pwrite == 1) {
Miss Islington (bot)2bb0bfa2018-02-05 22:31:22 -0800451 if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200452 goto error;
453 }
454 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000455 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200456
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000457 if (errwrite == 2) {
Miss Islington (bot)2bb0bfa2018-02-05 22:31:22 -0800458 if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200459 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000460 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200461 else if (errwrite != -1)
462 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000463
Miss Islington (bot)9c4a63f2018-09-10 21:36:20 -0700464 /* We no longer manually close p2cread, c2pwrite, and errwrite here as
465 * _close_open_fds takes care when it is not already non-inheritable. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000466
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000467 if (cwd)
468 POSIX_CALL(chdir(cwd));
469
470 if (restore_signals)
471 _Py_RestoreSignals();
472
473#ifdef HAVE_SETSID
474 if (call_setsid)
475 POSIX_CALL(setsid());
476#endif
477
Gregory P. Smith5591b022012-10-10 03:34:47 -0700478 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000479 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
480 /* This is where the user has asked us to deadlock their program. */
481 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
482 if (result == NULL) {
483 /* Stringifying the exception or traceback would involve
484 * memory allocation and thus potential for deadlock.
485 * We've already faced potential deadlock by calling back
486 * into Python in the first place, so it probably doesn't
487 * matter but we avoid it to minimize the possibility. */
488 err_msg = "Exception occurred in preexec_fn.";
489 errno = 0; /* We don't want to report an OSError. */
490 goto error;
491 }
492 /* Py_DECREF(result); - We're about to exec so why bother? */
493 }
494
Charles-François Natali249cdc32013-08-25 18:24:45 +0200495 /* close FDs after executing preexec_fn, which might open FDs */
496 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200497 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700498 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200499 }
500
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000501 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
502 /* given the executable_list generated by Lib/subprocess.py. */
503 saved_errno = 0;
504 for (i = 0; exec_array[i] != NULL; ++i) {
505 const char *executable = exec_array[i];
506 if (envp) {
507 execve(executable, argv, envp);
508 } else {
509 execv(executable, argv);
510 }
511 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
512 saved_errno = errno;
513 }
514 }
515 /* Report the first exec error, not the last. */
516 if (saved_errno)
517 errno = saved_errno;
518
519error:
520 saved_errno = errno;
521 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800522 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200523 less than PIPEBUF and we cannot do anything about an error anyways.
524 Use _Py_write_noraise() to retry write() if it is interrupted by a
525 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000526 if (saved_errno) {
527 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200528 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000529 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300530 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000531 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000532 saved_errno /= 16;
533 }
Victor Stinner185fd332015-04-01 18:35:53 +0200534 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
535 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700536 if (!reached_preexec) {
537 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200538 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700539 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000540 /* We can't call strerror(saved_errno). It is not async signal safe.
541 * The parent process will look the error message up. */
542 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200543 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
544 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000545 }
546}
547
548
549static PyObject *
550subprocess_fork_exec(PyObject* self, PyObject *args)
551{
552 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200553 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000554 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000555 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000556 PyObject *preexec_fn_args_tuple = NULL;
557 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
558 int errpipe_read, errpipe_write, close_fds, restore_signals;
559 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000560 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000561 const char *cwd;
562 pid_t pid;
563 int need_to_reenable_gc = 0;
564 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800565 Py_ssize_t arg_num;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200566 int need_after_fork = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000567
568 if (!PyArg_ParseTuple(
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300569 args, "OOpO!OOiiiiiiiiiiO:fork_exec",
570 &process_args, &executable_list,
571 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000572 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000573 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
574 &errread, &errwrite, &errpipe_read, &errpipe_write,
575 &restore_signals, &call_setsid, &preexec_fn))
576 return NULL;
577
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800578 if (close_fds && errpipe_write < 3) { /* precondition */
579 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
580 return NULL;
581 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800582 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
583 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000584 return NULL;
585 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000586
587 /* We need to call gc.disable() when we'll be calling preexec_fn */
588 if (preexec_fn != Py_None) {
589 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200590 _Py_IDENTIFIER(isenabled);
591 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200592
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000593 gc_module = PyImport_ImportModule("gc");
594 if (gc_module == NULL)
595 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200596 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000597 if (result == NULL) {
598 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000599 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000600 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000601 need_to_reenable_gc = PyObject_IsTrue(result);
602 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000603 if (need_to_reenable_gc == -1) {
604 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000605 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000606 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200607 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000608 if (result == NULL) {
609 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000610 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000611 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000612 Py_DECREF(result);
613 }
614
615 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200616 if (!exec_array)
617 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000618
619 /* Convert args and env into appropriate arguments for exec() */
620 /* These conversions are done in the parent process to avoid allocating
621 or freeing memory in the child process. */
622 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000623 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000624 /* Equivalent to: */
625 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000626 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200627 if (fast_args == NULL)
628 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000629 num_args = PySequence_Fast_GET_SIZE(fast_args);
630 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000631 if (converted_args == NULL)
632 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000633 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000634 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300635 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
636 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
637 goto cleanup;
638 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000639 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000640 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
641 goto cleanup;
642 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
643 }
644
645 argv = _PySequence_BytesToCharpArray(converted_args);
646 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000647 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000648 if (!argv)
649 goto cleanup;
650 }
651
652 if (env_list != Py_None) {
653 envp = _PySequence_BytesToCharpArray(env_list);
654 if (!envp)
655 goto cleanup;
656 }
657
Victor Stinner0e59cc32010-04-16 23:49:32 +0000658 if (cwd_obj != Py_None) {
659 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
660 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000661 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000662 } else {
663 cwd = NULL;
664 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000665 }
666
Gregory P. Smith163468a2017-05-29 10:03:41 -0700667 /* This must be the last thing done before fork() because we do not
668 * want to call PyOS_BeforeFork() if there is any chance of another
669 * error leading to the cleanup: code without calling fork(). */
670 if (preexec_fn != Py_None) {
671 preexec_fn_args_tuple = PyTuple_New(0);
672 if (!preexec_fn_args_tuple)
673 goto cleanup;
674 PyOS_BeforeFork();
675 need_after_fork = 1;
676 }
677
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000678 pid = fork();
679 if (pid == 0) {
680 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000681 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000682 * Code from here to _exit() must only use async-signal-safe functions,
683 * listed at `man 7 signal` or
684 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
685 */
686
687 if (preexec_fn != Py_None) {
688 /* We'll be calling back into Python later so we need to do this.
689 * This call may not be async-signal-safe but neither is calling
690 * back into Python. The user asked us to use hope as a strategy
691 * to avoid deadlock... */
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200692 PyOS_AfterFork_Child();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000693 }
694
695 child_exec(exec_array, argv, envp, cwd,
696 p2cread, p2cwrite, c2pread, c2pwrite,
697 errread, errwrite, errpipe_read, errpipe_write,
698 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800699 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000700 _exit(255);
701 return NULL; /* Dead code to avoid a potential compiler warning. */
702 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000703 Py_XDECREF(cwd_obj2);
704
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000705 if (pid == -1) {
706 /* Capture the errno exception before errno can be clobbered. */
707 PyErr_SetFromErrno(PyExc_OSError);
708 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000709
710 /* Parent process */
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200711 if (need_after_fork)
712 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000713 if (envp)
714 _Py_FreeCharPArray(envp);
715 if (argv)
716 _Py_FreeCharPArray(argv);
717 _Py_FreeCharPArray(exec_array);
718
719 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +0000720 if (_enable_gc(need_to_reenable_gc, gc_module)) {
721 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000722 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000723 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000724 Py_XDECREF(gc_module);
725
726 if (pid == -1)
727 return NULL; /* fork() failed. Exception set earlier. */
728
729 return PyLong_FromPid(pid);
730
731cleanup:
732 if (envp)
733 _Py_FreeCharPArray(envp);
734 if (argv)
735 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200736 if (exec_array)
737 _Py_FreeCharPArray(exec_array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000738 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000739 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000740 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +0000741 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000742 Py_XDECREF(gc_module);
743 return NULL;
744}
745
746
747PyDoc_STRVAR(subprocess_fork_exec_doc,
748"fork_exec(args, executable_list, close_fds, cwd, env,\n\
749 p2cread, p2cwrite, c2pread, c2pwrite,\n\
750 errread, errwrite, errpipe_read, errpipe_write,\n\
751 restore_signals, call_setsid, preexec_fn)\n\
752\n\
753Forks a child process, closes parent file descriptors as appropriate in the\n\
754child and dups the few that are needed before calling exec() in the child\n\
755process.\n\
756\n\
757The preexec_fn, if supplied, will be called immediately before exec.\n\
758WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
759 It may trigger infrequent, difficult to debug deadlocks.\n\
760\n\
761If an error occurs in the child process before the exec, it is\n\
762serialized and written to the errpipe_write fd per subprocess.py.\n\
763\n\
764Returns: the child process's PID.\n\
765\n\
766Raises: Only on an error in the parent process.\n\
767");
768
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000769/* module level code ********************************************************/
770
771PyDoc_STRVAR(module_doc,
772"A POSIX helper for the subprocess module.");
773
774
775static PyMethodDef module_methods[] = {
776 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000777 {NULL, NULL} /* sentinel */
778};
779
780
781static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -0600782 PyModuleDef_HEAD_INIT,
783 "_posixsubprocess",
784 module_doc,
785 -1, /* No memory is needed. */
786 module_methods,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000787};
788
789PyMODINIT_FUNC
790PyInit__posixsubprocess(void)
791{
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000792 return PyModule_Create(&_posixsubprocessmodule);
793}