blob: 3d4eb7722cb4a54daab0553a974ad2b78e3b7a67 [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
17#ifdef HAVE_DIRENT_H
18#include <dirent.h>
19#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000020
Gregory P. Smithe3f78482012-01-21 15:16:17 -080021#if defined(sun)
22/* readdir64 is used to work around Solaris 9 bug 6395699. */
23# define readdir readdir64
24# define dirent dirent64
25# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080026/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080027# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080028# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080029# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080030#endif
31
Gregory P. Smith4842efc2012-01-21 21:01:24 -080032#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
33# define FD_DIR "/dev/fd"
34#else
35# define FD_DIR "/proc/self/fd"
36#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000037
38#define POSIX_CALL(call) if ((call) == -1) goto error
39
40
41/* Maximum file descriptor, initialized on module load. */
42static long max_fd;
43
44
45/* Given the gc module call gc.enable() and return 0 on success. */
46static int _enable_gc(PyObject *gc_module)
47{
48 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020049 _Py_IDENTIFIER(enable);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020050
51 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000052 if (result == NULL)
53 return 1;
54 Py_DECREF(result);
55 return 0;
56}
57
58
Gregory P. Smith8facece2012-01-21 14:01:08 -080059/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
60static int _pos_int_from_ascii(char *name)
61{
62 int num = 0;
63 while (*name >= '0' && *name <= '9') {
64 num = num * 10 + (*name - '0');
65 ++name;
66 }
67 if (*name)
68 return -1; /* Non digit found, not a number. */
69 return num;
70}
71
72
Gregory P. Smith4842efc2012-01-21 21:01:24 -080073#if defined(__FreeBSD__)
74/* When /dev/fd isn't mounted it is often a static directory populated
75 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
76 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
77 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
78 * that properly supports /dev/fd.
79 */
80static int _is_fdescfs_mounted_on_dev_fd()
81{
82 struct stat dev_stat;
83 struct stat dev_fd_stat;
84 if (stat("/dev", &dev_stat) != 0)
85 return 0;
86 if (stat(FD_DIR, &dev_fd_stat) != 0)
87 return 0;
88 if (dev_stat.st_dev == dev_fd_stat.st_dev)
89 return 0; /* / == /dev == /dev/fd means it is static. #fail */
90 return 1;
91}
92#endif
93
94
Gregory P. Smith8facece2012-01-21 14:01:08 -080095/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
96static int _sanity_check_python_fd_sequence(PyObject *fd_sequence)
97{
98 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
99 long prev_fd = -1;
100 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
101 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
102 long iter_fd = PyLong_AsLong(py_fd);
103 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
104 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
105 return 1;
106 }
107 }
108 return 0;
109}
110
111
112/* Is fd found in the sorted Python Sequence? */
113static int _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
114{
115 /* Binary search. */
116 Py_ssize_t search_min = 0;
117 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
118 if (search_max < 0)
119 return 0;
120 do {
121 long middle = (search_min + search_max) / 2;
122 long middle_fd = PyLong_AsLong(
123 PySequence_Fast_GET_ITEM(fd_sequence, middle));
124 if (fd == middle_fd)
125 return 1;
126 if (fd > middle_fd)
127 search_min = middle + 1;
128 else
129 search_max = middle - 1;
130 } while (search_min <= search_max);
131 return 0;
132}
133
134
135/* Close all file descriptors in the range start_fd inclusive to
136 * end_fd exclusive except for those in py_fds_to_keep. If the
137 * range defined by [start_fd, end_fd) is large this will take a
138 * long time as it calls close() on EVERY possible fd.
139 */
140static void _close_fds_by_brute_force(int start_fd, int end_fd,
141 PyObject *py_fds_to_keep)
142{
143 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
144 Py_ssize_t keep_seq_idx;
145 int fd_num;
146 /* As py_fds_to_keep is sorted we can loop through the list closing
147 * fds inbetween any in the keep list falling within our range. */
148 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
149 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
150 keep_seq_idx);
151 int keep_fd = PyLong_AsLong(py_keep_fd);
152 if (keep_fd < start_fd)
153 continue;
154 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
155 while (close(fd_num) < 0 && errno == EINTR);
156 }
157 start_fd = keep_fd + 1;
158 }
159 if (start_fd <= end_fd) {
160 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
161 while (close(fd_num) < 0 && errno == EINTR);
162 }
163 }
164}
165
166
167#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
168/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
169 * only to read a directory of short file descriptor number names. The kernel
170 * will return an error if we didn't give it enough space. Highly Unlikely.
171 * This structure is very old and stable: It will not change unless the kernel
172 * chooses to break compatibility with all existing binaries. Highly Unlikely.
173 */
174struct linux_dirent {
175 unsigned long d_ino; /* Inode number */
176 unsigned long d_off; /* Offset to next linux_dirent */
177 unsigned short d_reclen; /* Length of this linux_dirent */
178 char d_name[256]; /* Filename (null-terminated) */
179};
180
181/* Close all open file descriptors in the range start_fd inclusive to end_fd
182 * exclusive. Do not close any in the sorted py_fds_to_keep list.
183 *
184 * This version is async signal safe as it does not make any unsafe C library
185 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
186 * to resort to making a kernel system call directly but this is the ONLY api
187 * available that does no harm. opendir/readdir/closedir perform memory
188 * allocation and locking so while they usually work they are not guaranteed
189 * to (especially if you have replaced your malloc implementation). A version
190 * of this function that uses those can be found in the _maybe_unsafe variant.
191 *
192 * This is Linux specific because that is all I am ready to test it on. It
193 * should be easy to add OS specific dirent or dirent64 structures and modify
194 * it with some cpp #define magic to work on other OSes as well if you want.
195 */
196static void _close_open_fd_range_safe(int start_fd, int end_fd,
197 PyObject* py_fds_to_keep)
198{
199 int fd_dir_fd;
200 if (start_fd >= end_fd)
201 return;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800202 fd_dir_fd = open(FD_DIR, O_RDONLY | O_CLOEXEC, 0);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800203 if (fd_dir_fd == -1) {
204 /* No way to get a list of open fds. */
205 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
206 return;
207 } else {
208 char buffer[sizeof(struct linux_dirent)];
209 int bytes;
210 while ((bytes = syscall(SYS_getdents, fd_dir_fd,
211 (struct linux_dirent *)buffer,
212 sizeof(buffer))) > 0) {
213 struct linux_dirent *entry;
214 int offset;
215 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
216 int fd;
217 entry = (struct linux_dirent *)(buffer + offset);
218 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
219 continue; /* Not a number. */
220 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
221 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
222 while (close(fd) < 0 && errno == EINTR);
223 }
224 }
225 }
226 close(fd_dir_fd);
227 }
228}
229
230#define _close_open_fd_range _close_open_fd_range_safe
231
232#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
233
234
235/* Close all open file descriptors in the range start_fd inclusive to end_fd
236 * exclusive. Do not close any in the sorted py_fds_to_keep list.
237 *
238 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800239 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800240 * likely to ever cause a problem is opendir() as it performs an internal
241 * malloc(). Practically this should not be a problem. The Java VM makes the
242 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
243 *
244 * readdir_r() is not used because it provides no benefit. It is typically
245 * implemented as readdir() followed by memcpy(). See also:
246 * http://womble.decadent.org.uk/readdir_r-advisory.html
247 */
248static void _close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
249 PyObject* py_fds_to_keep)
250{
251 DIR *proc_fd_dir;
252#ifndef HAVE_DIRFD
253 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
254 (start_fd < end_fd)) {
255 ++start_fd;
256 }
257 if (start_fd >= end_fd)
258 return;
259 /* Close our lowest fd before we call opendir so that it is likely to
260 * reuse that fd otherwise we might close opendir's file descriptor in
261 * our loop. This trick assumes that fd's are allocated on a lowest
262 * available basis. */
263 while (close(start_fd) < 0 && errno == EINTR);
264 ++start_fd;
265#endif
266 if (start_fd >= end_fd)
267 return;
268
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800269#if defined(__FreeBSD__)
270 if (!_is_fdescfs_mounted_on_dev_fd())
271 proc_fd_dir = NULL;
272 else
273#endif
274 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800275 if (!proc_fd_dir) {
276 /* No way to get a list of open fds. */
277 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
278 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800279 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800280#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800281 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800282#else
283 int fd_used_by_opendir = start_fd - 1;
284#endif
285 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800286 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800287 int fd;
288 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
289 continue; /* Not a number. */
290 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
291 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
292 while (close(fd) < 0 && errno == EINTR);
293 }
294 errno = 0;
295 }
296 if (errno) {
297 /* readdir error, revert behavior. Highly Unlikely. */
298 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
299 }
300 closedir(proc_fd_dir);
301 }
302}
303
304#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
305
306#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
307
308
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000309/*
310 * This function is code executed in the child process immediately after fork
311 * to set things up and call exec().
312 *
313 * All of the code in this function must only use async-signal-safe functions,
314 * listed at `man 7 signal` or
315 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
316 *
317 * This restriction is documented at
318 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
319 */
320static void child_exec(char *const exec_array[],
321 char *const argv[],
322 char *const envp[],
323 const char *cwd,
324 int p2cread, int p2cwrite,
325 int c2pread, int c2pwrite,
326 int errread, int errwrite,
327 int errpipe_read, int errpipe_write,
328 int close_fds, int restore_signals,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329 int call_setsid,
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000330 PyObject *py_fds_to_keep,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000331 PyObject *preexec_fn,
332 PyObject *preexec_fn_args_tuple)
333{
Gregory P. Smith8facece2012-01-21 14:01:08 -0800334 int i, saved_errno, unused;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000335 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000336 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000337 /* Buffer large enough to hold a hex integer. We can't malloc. */
338 char hex_errno[sizeof(saved_errno)*2+1];
339
340 /* Close parent's pipe ends. */
341 if (p2cwrite != -1) {
342 POSIX_CALL(close(p2cwrite));
343 }
344 if (c2pread != -1) {
345 POSIX_CALL(close(c2pread));
346 }
347 if (errread != -1) {
348 POSIX_CALL(close(errread));
349 }
350 POSIX_CALL(close(errpipe_read));
351
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200352 /* When duping fds, if there arises a situation where one of the fds is
353 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
354 if (c2pwrite == 0)
355 POSIX_CALL(c2pwrite = dup(c2pwrite));
356 if (errwrite == 0 || errwrite == 1)
357 POSIX_CALL(errwrite = dup(errwrite));
358
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000359 /* Dup fds for child.
360 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
361 would be a no-op (issue #10806). */
362 if (p2cread == 0) {
363 int old = fcntl(p2cread, F_GETFD);
364 if (old != -1)
365 fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
366 } else if (p2cread != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000367 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
368 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000369 if (c2pwrite == 1) {
370 int old = fcntl(c2pwrite, F_GETFD);
371 if (old != -1)
372 fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
373 } else if (c2pwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000374 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
375 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000376 if (errwrite == 2) {
377 int old = fcntl(errwrite, F_GETFD);
378 if (old != -1)
379 fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
380 } else if (errwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000381 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
382 }
383
384 /* Close pipe fds. Make sure we don't close the same fd more than */
385 /* once, or standard fds. */
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000386 if (p2cread > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000387 POSIX_CALL(close(p2cread));
388 }
Gregory P. Smith81218982011-03-15 14:56:39 -0400389 if (c2pwrite > 2 && c2pwrite != p2cread) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000390 POSIX_CALL(close(c2pwrite));
391 }
Gregory P. Smith81218982011-03-15 14:56:39 -0400392 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000393 POSIX_CALL(close(errwrite));
394 }
395
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800396 if (close_fds) {
397 int local_max_fd = max_fd;
398#if defined(__NetBSD__)
399 local_max_fd = fcntl(0, F_MAXFD);
400 if (local_max_fd < 0)
401 local_max_fd = max_fd;
402#endif
403 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
404 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
405 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000406
407 if (cwd)
408 POSIX_CALL(chdir(cwd));
409
410 if (restore_signals)
411 _Py_RestoreSignals();
412
413#ifdef HAVE_SETSID
414 if (call_setsid)
415 POSIX_CALL(setsid());
416#endif
417
418 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
419 /* This is where the user has asked us to deadlock their program. */
420 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
421 if (result == NULL) {
422 /* Stringifying the exception or traceback would involve
423 * memory allocation and thus potential for deadlock.
424 * We've already faced potential deadlock by calling back
425 * into Python in the first place, so it probably doesn't
426 * matter but we avoid it to minimize the possibility. */
427 err_msg = "Exception occurred in preexec_fn.";
428 errno = 0; /* We don't want to report an OSError. */
429 goto error;
430 }
431 /* Py_DECREF(result); - We're about to exec so why bother? */
432 }
433
434 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
435 /* given the executable_list generated by Lib/subprocess.py. */
436 saved_errno = 0;
437 for (i = 0; exec_array[i] != NULL; ++i) {
438 const char *executable = exec_array[i];
439 if (envp) {
440 execve(executable, argv, envp);
441 } else {
442 execv(executable, argv);
443 }
444 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
445 saved_errno = errno;
446 }
447 }
448 /* Report the first exec error, not the last. */
449 if (saved_errno)
450 errno = saved_errno;
451
452error:
453 saved_errno = errno;
454 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800455 /* We ignore all write() return values as the total size of our writes is
456 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000457 if (saved_errno) {
458 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800459 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000460 cur = hex_errno + sizeof(hex_errno);
461 while (saved_errno != 0 && cur > hex_errno) {
462 *--cur = "0123456789ABCDEF"[saved_errno % 16];
463 saved_errno /= 16;
464 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800465 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
466 unused = write(errpipe_write, ":", 1);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000467 /* We can't call strerror(saved_errno). It is not async signal safe.
468 * The parent process will look the error message up. */
469 } else {
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800470 unused = write(errpipe_write, "RuntimeError:0:", 15);
471 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000472 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800473 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000474}
475
476
477static PyObject *
478subprocess_fork_exec(PyObject* self, PyObject *args)
479{
480 PyObject *gc_module = NULL;
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000481 PyObject *executable_list, *py_close_fds, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000482 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000483 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000484 PyObject *preexec_fn_args_tuple = NULL;
485 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
486 int errpipe_read, errpipe_write, close_fds, restore_signals;
487 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000488 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000489 const char *cwd;
490 pid_t pid;
491 int need_to_reenable_gc = 0;
492 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800493 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000494
495 if (!PyArg_ParseTuple(
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000496 args, "OOOOOOiiiiiiiiiiO:fork_exec",
497 &process_args, &executable_list, &py_close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000498 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000499 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
500 &errread, &errwrite, &errpipe_read, &errpipe_write,
501 &restore_signals, &call_setsid, &preexec_fn))
502 return NULL;
503
504 close_fds = PyObject_IsTrue(py_close_fds);
505 if (close_fds && errpipe_write < 3) { /* precondition */
506 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
507 return NULL;
508 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800509 if (PySequence_Length(py_fds_to_keep) < 0) {
510 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
511 return NULL;
512 }
513 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
514 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000515 return NULL;
516 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000517
518 /* We need to call gc.disable() when we'll be calling preexec_fn */
519 if (preexec_fn != Py_None) {
520 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200521 _Py_IDENTIFIER(isenabled);
522 _Py_IDENTIFIER(disable);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200523
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000524 gc_module = PyImport_ImportModule("gc");
525 if (gc_module == NULL)
526 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200527 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000528 if (result == NULL) {
529 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000530 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000531 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000532 need_to_reenable_gc = PyObject_IsTrue(result);
533 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000534 if (need_to_reenable_gc == -1) {
535 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000536 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000537 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200538 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000539 if (result == NULL) {
540 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000541 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000542 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000543 Py_DECREF(result);
544 }
545
546 exec_array = _PySequence_BytesToCharpArray(executable_list);
547 if (!exec_array)
548 return NULL;
549
550 /* Convert args and env into appropriate arguments for exec() */
551 /* These conversions are done in the parent process to avoid allocating
552 or freeing memory in the child process. */
553 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000554 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000555 /* Equivalent to: */
556 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000557 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
558 num_args = PySequence_Fast_GET_SIZE(fast_args);
559 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000560 if (converted_args == NULL)
561 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000562 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000563 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000564 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000565 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
566 goto cleanup;
567 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
568 }
569
570 argv = _PySequence_BytesToCharpArray(converted_args);
571 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000572 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000573 if (!argv)
574 goto cleanup;
575 }
576
577 if (env_list != Py_None) {
578 envp = _PySequence_BytesToCharpArray(env_list);
579 if (!envp)
580 goto cleanup;
581 }
582
583 if (preexec_fn != Py_None) {
584 preexec_fn_args_tuple = PyTuple_New(0);
585 if (!preexec_fn_args_tuple)
586 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000587 _PyImport_AcquireLock();
588 }
589
590 if (cwd_obj != Py_None) {
591 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
592 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000593 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000594 } else {
595 cwd = NULL;
596 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000597 }
598
599 pid = fork();
600 if (pid == 0) {
601 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000602 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000603 * Code from here to _exit() must only use async-signal-safe functions,
604 * listed at `man 7 signal` or
605 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
606 */
607
608 if (preexec_fn != Py_None) {
609 /* We'll be calling back into Python later so we need to do this.
610 * This call may not be async-signal-safe but neither is calling
611 * back into Python. The user asked us to use hope as a strategy
612 * to avoid deadlock... */
613 PyOS_AfterFork();
614 }
615
616 child_exec(exec_array, argv, envp, cwd,
617 p2cread, p2cwrite, c2pread, c2pwrite,
618 errread, errwrite, errpipe_read, errpipe_write,
619 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800620 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000621 _exit(255);
622 return NULL; /* Dead code to avoid a potential compiler warning. */
623 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000624 Py_XDECREF(cwd_obj2);
625
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000626 if (pid == -1) {
627 /* Capture the errno exception before errno can be clobbered. */
628 PyErr_SetFromErrno(PyExc_OSError);
629 }
630 if (preexec_fn != Py_None &&
631 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
632 PyErr_SetString(PyExc_RuntimeError,
633 "not holding the import lock");
634 }
635
636 /* Parent process */
637 if (envp)
638 _Py_FreeCharPArray(envp);
639 if (argv)
640 _Py_FreeCharPArray(argv);
641 _Py_FreeCharPArray(exec_array);
642
643 /* Reenable gc in the parent process (or if fork failed). */
644 if (need_to_reenable_gc && _enable_gc(gc_module)) {
645 Py_XDECREF(gc_module);
646 return NULL;
647 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000648 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000649 Py_XDECREF(gc_module);
650
651 if (pid == -1)
652 return NULL; /* fork() failed. Exception set earlier. */
653
654 return PyLong_FromPid(pid);
655
656cleanup:
657 if (envp)
658 _Py_FreeCharPArray(envp);
659 if (argv)
660 _Py_FreeCharPArray(argv);
661 _Py_FreeCharPArray(exec_array);
662 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000663 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000664 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000665
666 /* Reenable gc if it was disabled. */
667 if (need_to_reenable_gc)
668 _enable_gc(gc_module);
669 Py_XDECREF(gc_module);
670 return NULL;
671}
672
673
674PyDoc_STRVAR(subprocess_fork_exec_doc,
675"fork_exec(args, executable_list, close_fds, cwd, env,\n\
676 p2cread, p2cwrite, c2pread, c2pwrite,\n\
677 errread, errwrite, errpipe_read, errpipe_write,\n\
678 restore_signals, call_setsid, preexec_fn)\n\
679\n\
680Forks a child process, closes parent file descriptors as appropriate in the\n\
681child and dups the few that are needed before calling exec() in the child\n\
682process.\n\
683\n\
684The preexec_fn, if supplied, will be called immediately before exec.\n\
685WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
686 It may trigger infrequent, difficult to debug deadlocks.\n\
687\n\
688If an error occurs in the child process before the exec, it is\n\
689serialized and written to the errpipe_write fd per subprocess.py.\n\
690\n\
691Returns: the child process's PID.\n\
692\n\
693Raises: Only on an error in the parent process.\n\
694");
695
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000696PyDoc_STRVAR(subprocess_cloexec_pipe_doc,
697"cloexec_pipe() -> (read_end, write_end)\n\n\
698Create a pipe whose ends have the cloexec flag set.");
699
700static PyObject *
701subprocess_cloexec_pipe(PyObject *self, PyObject *noargs)
702{
703 int fds[2];
704 int res;
705#ifdef HAVE_PIPE2
706 Py_BEGIN_ALLOW_THREADS
707 res = pipe2(fds, O_CLOEXEC);
708 Py_END_ALLOW_THREADS
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000709 if (res != 0 && errno == ENOSYS)
710 {
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000711 {
712#endif
713 /* We hold the GIL which offers some protection from other code calling
714 * fork() before the CLOEXEC flags have been set but we can't guarantee
715 * anything without pipe2(). */
716 long oldflags;
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000717
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000718 res = pipe(fds);
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000719
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000720 if (res == 0) {
721 oldflags = fcntl(fds[0], F_GETFD, 0);
722 if (oldflags < 0) res = oldflags;
723 }
724 if (res == 0)
725 res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
726
727 if (res == 0) {
728 oldflags = fcntl(fds[1], F_GETFD, 0);
729 if (oldflags < 0) res = oldflags;
730 }
731 if (res == 0)
732 res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
733#ifdef HAVE_PIPE2
734 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000735 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000736#endif
737 if (res != 0)
738 return PyErr_SetFromErrno(PyExc_OSError);
739 return Py_BuildValue("(ii)", fds[0], fds[1]);
740}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000741
742/* module level code ********************************************************/
743
744PyDoc_STRVAR(module_doc,
745"A POSIX helper for the subprocess module.");
746
747
748static PyMethodDef module_methods[] = {
749 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000750 {"cloexec_pipe", subprocess_cloexec_pipe, METH_NOARGS, subprocess_cloexec_pipe_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000751 {NULL, NULL} /* sentinel */
752};
753
754
755static struct PyModuleDef _posixsubprocessmodule = {
756 PyModuleDef_HEAD_INIT,
757 "_posixsubprocess",
758 module_doc,
759 -1, /* No memory is needed. */
760 module_methods,
761};
762
763PyMODINIT_FUNC
764PyInit__posixsubprocess(void)
765{
766#ifdef _SC_OPEN_MAX
767 max_fd = sysconf(_SC_OPEN_MAX);
768 if (max_fd == -1)
769#endif
770 max_fd = 256; /* Matches Lib/subprocess.py */
771
772 return PyModule_Create(&_posixsubprocessmodule);
773}