blob: 8c8777cfe33f5bdbb0f93ea9b22c82215bfb777c [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 }
172 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
173 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
223 * fds inbetween any in the keep list falling within our range. */
224 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). */
425 if (c2pwrite == 0)
426 POSIX_CALL(c2pwrite = dup(c2pwrite));
427 if (errwrite == 0 || errwrite == 1)
428 POSIX_CALL(errwrite = dup(errwrite));
429
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000430 /* Dup fds for child.
431 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
432 would be a no-op (issue #10806). */
433 if (p2cread == 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200434 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
435 goto error;
436 }
437 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000438 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200439
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000440 if (c2pwrite == 1) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200441 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
442 goto error;
443 }
444 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000445 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200446
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000447 if (errwrite == 2) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200448 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
449 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000450 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200451 else if (errwrite != -1)
452 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000453
454 /* Close pipe fds. Make sure we don't close the same fd more than */
455 /* once, or standard fds. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200456 if (p2cread > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000457 POSIX_CALL(close(p2cread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200458 if (c2pwrite > 2 && c2pwrite != p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000459 POSIX_CALL(close(c2pwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200460 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000461 POSIX_CALL(close(errwrite));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000462
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000463 if (cwd)
464 POSIX_CALL(chdir(cwd));
465
466 if (restore_signals)
467 _Py_RestoreSignals();
468
469#ifdef HAVE_SETSID
470 if (call_setsid)
471 POSIX_CALL(setsid());
472#endif
473
Gregory P. Smith5591b022012-10-10 03:34:47 -0700474 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000475 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
476 /* This is where the user has asked us to deadlock their program. */
477 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
478 if (result == NULL) {
479 /* Stringifying the exception or traceback would involve
480 * memory allocation and thus potential for deadlock.
481 * We've already faced potential deadlock by calling back
482 * into Python in the first place, so it probably doesn't
483 * matter but we avoid it to minimize the possibility. */
484 err_msg = "Exception occurred in preexec_fn.";
485 errno = 0; /* We don't want to report an OSError. */
486 goto error;
487 }
488 /* Py_DECREF(result); - We're about to exec so why bother? */
489 }
490
Charles-François Natali249cdc32013-08-25 18:24:45 +0200491 /* close FDs after executing preexec_fn, which might open FDs */
492 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200493 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700494 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200495 }
496
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000497 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
498 /* given the executable_list generated by Lib/subprocess.py. */
499 saved_errno = 0;
500 for (i = 0; exec_array[i] != NULL; ++i) {
501 const char *executable = exec_array[i];
502 if (envp) {
503 execve(executable, argv, envp);
504 } else {
505 execv(executable, argv);
506 }
507 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
508 saved_errno = errno;
509 }
510 }
511 /* Report the first exec error, not the last. */
512 if (saved_errno)
513 errno = saved_errno;
514
515error:
516 saved_errno = errno;
517 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800518 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200519 less than PIPEBUF and we cannot do anything about an error anyways.
520 Use _Py_write_noraise() to retry write() if it is interrupted by a
521 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000522 if (saved_errno) {
523 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200524 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000525 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300526 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000527 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000528 saved_errno /= 16;
529 }
Victor Stinner185fd332015-04-01 18:35:53 +0200530 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
531 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700532 if (!reached_preexec) {
533 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200534 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700535 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000536 /* We can't call strerror(saved_errno). It is not async signal safe.
537 * The parent process will look the error message up. */
538 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200539 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
540 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000541 }
542}
543
544
545static PyObject *
546subprocess_fork_exec(PyObject* self, PyObject *args)
547{
548 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200549 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000550 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000551 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000552 PyObject *preexec_fn_args_tuple = NULL;
553 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
554 int errpipe_read, errpipe_write, close_fds, restore_signals;
555 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000556 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000557 const char *cwd;
558 pid_t pid;
559 int need_to_reenable_gc = 0;
560 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800561 Py_ssize_t arg_num;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200562 int need_after_fork = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000563
564 if (!PyArg_ParseTuple(
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300565 args, "OOpO!OOiiiiiiiiiiO:fork_exec",
566 &process_args, &executable_list,
567 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000568 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000569 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
570 &errread, &errwrite, &errpipe_read, &errpipe_write,
571 &restore_signals, &call_setsid, &preexec_fn))
572 return NULL;
573
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800574 if (close_fds && errpipe_write < 3) { /* precondition */
575 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
576 return NULL;
577 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800578 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
579 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000580 return NULL;
581 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000582
583 /* We need to call gc.disable() when we'll be calling preexec_fn */
584 if (preexec_fn != Py_None) {
585 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200586 _Py_IDENTIFIER(isenabled);
587 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200588
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000589 gc_module = PyImport_ImportModule("gc");
590 if (gc_module == NULL)
591 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200592 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000593 if (result == NULL) {
594 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000595 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000596 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000597 need_to_reenable_gc = PyObject_IsTrue(result);
598 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000599 if (need_to_reenable_gc == -1) {
600 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000601 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000602 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200603 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000604 if (result == NULL) {
605 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000606 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000607 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000608 Py_DECREF(result);
609 }
610
611 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200612 if (!exec_array)
613 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000614
615 /* Convert args and env into appropriate arguments for exec() */
616 /* These conversions are done in the parent process to avoid allocating
617 or freeing memory in the child process. */
618 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000619 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000620 /* Equivalent to: */
621 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000622 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200623 if (fast_args == NULL)
624 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000625 num_args = PySequence_Fast_GET_SIZE(fast_args);
626 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000627 if (converted_args == NULL)
628 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000629 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000630 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300631 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
632 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
633 goto cleanup;
634 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000635 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000636 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
637 goto cleanup;
638 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
639 }
640
641 argv = _PySequence_BytesToCharpArray(converted_args);
642 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000643 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000644 if (!argv)
645 goto cleanup;
646 }
647
648 if (env_list != Py_None) {
649 envp = _PySequence_BytesToCharpArray(env_list);
650 if (!envp)
651 goto cleanup;
652 }
653
Victor Stinner0e59cc32010-04-16 23:49:32 +0000654 if (cwd_obj != Py_None) {
655 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
656 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000657 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000658 } else {
659 cwd = NULL;
660 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000661 }
662
Gregory P. Smith163468a2017-05-29 10:03:41 -0700663 /* This must be the last thing done before fork() because we do not
664 * want to call PyOS_BeforeFork() if there is any chance of another
665 * error leading to the cleanup: code without calling fork(). */
666 if (preexec_fn != Py_None) {
667 preexec_fn_args_tuple = PyTuple_New(0);
668 if (!preexec_fn_args_tuple)
669 goto cleanup;
670 PyOS_BeforeFork();
671 need_after_fork = 1;
672 }
673
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000674 pid = fork();
675 if (pid == 0) {
676 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000677 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000678 * Code from here to _exit() must only use async-signal-safe functions,
679 * listed at `man 7 signal` or
680 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
681 */
682
683 if (preexec_fn != Py_None) {
684 /* We'll be calling back into Python later so we need to do this.
685 * This call may not be async-signal-safe but neither is calling
686 * back into Python. The user asked us to use hope as a strategy
687 * to avoid deadlock... */
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200688 PyOS_AfterFork_Child();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000689 }
690
691 child_exec(exec_array, argv, envp, cwd,
692 p2cread, p2cwrite, c2pread, c2pwrite,
693 errread, errwrite, errpipe_read, errpipe_write,
694 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800695 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000696 _exit(255);
697 return NULL; /* Dead code to avoid a potential compiler warning. */
698 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000699 Py_XDECREF(cwd_obj2);
700
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000701 if (pid == -1) {
702 /* Capture the errno exception before errno can be clobbered. */
703 PyErr_SetFromErrno(PyExc_OSError);
704 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000705
706 /* Parent process */
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200707 if (need_after_fork)
708 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000709 if (envp)
710 _Py_FreeCharPArray(envp);
711 if (argv)
712 _Py_FreeCharPArray(argv);
713 _Py_FreeCharPArray(exec_array);
714
715 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +0000716 if (_enable_gc(need_to_reenable_gc, gc_module)) {
717 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000718 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000719 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000720 Py_XDECREF(gc_module);
721
722 if (pid == -1)
723 return NULL; /* fork() failed. Exception set earlier. */
724
725 return PyLong_FromPid(pid);
726
727cleanup:
728 if (envp)
729 _Py_FreeCharPArray(envp);
730 if (argv)
731 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200732 if (exec_array)
733 _Py_FreeCharPArray(exec_array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000734 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000735 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000736 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +0000737 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000738 Py_XDECREF(gc_module);
739 return NULL;
740}
741
742
743PyDoc_STRVAR(subprocess_fork_exec_doc,
744"fork_exec(args, executable_list, close_fds, cwd, env,\n\
745 p2cread, p2cwrite, c2pread, c2pwrite,\n\
746 errread, errwrite, errpipe_read, errpipe_write,\n\
747 restore_signals, call_setsid, preexec_fn)\n\
748\n\
749Forks a child process, closes parent file descriptors as appropriate in the\n\
750child and dups the few that are needed before calling exec() in the child\n\
751process.\n\
752\n\
753The preexec_fn, if supplied, will be called immediately before exec.\n\
754WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
755 It may trigger infrequent, difficult to debug deadlocks.\n\
756\n\
757If an error occurs in the child process before the exec, it is\n\
758serialized and written to the errpipe_write fd per subprocess.py.\n\
759\n\
760Returns: the child process's PID.\n\
761\n\
762Raises: Only on an error in the parent process.\n\
763");
764
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000765/* module level code ********************************************************/
766
767PyDoc_STRVAR(module_doc,
768"A POSIX helper for the subprocess module.");
769
770
771static PyMethodDef module_methods[] = {
772 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000773 {NULL, NULL} /* sentinel */
774};
775
776
777static struct PyModuleDef _posixsubprocessmodule = {
778 PyModuleDef_HEAD_INIT,
779 "_posixsubprocess",
780 module_doc,
781 -1, /* No memory is needed. */
782 module_methods,
783};
784
785PyMODINIT_FUNC
786PyInit__posixsubprocess(void)
787{
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000788 return PyModule_Create(&_posixsubprocessmodule);
789}