blob: 59673f4969fd2356ce0552dc988971f65b659039 [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
Ross Lagerwall667d75d2011-12-22 09:45:53 +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. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050046static int
47_enable_gc(PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000048{
49 PyObject *result;
50 result = PyObject_CallMethod(gc_module, "enable", NULL);
51 if (result == NULL)
52 return 1;
53 Py_DECREF(result);
54 return 0;
55}
56
57
Gregory P. Smith8facece2012-01-21 14:01:08 -080058/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050059static int
60_pos_int_from_ascii(char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080061{
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 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050080static int
81_is_fdescfs_mounted_on_dev_fd()
Gregory P. Smith4842efc2012-01-21 21:01:24 -080082{
83 struct stat dev_stat;
84 struct stat dev_fd_stat;
85 if (stat("/dev", &dev_stat) != 0)
86 return 0;
87 if (stat(FD_DIR, &dev_fd_stat) != 0)
88 return 0;
89 if (dev_stat.st_dev == dev_fd_stat.st_dev)
90 return 0; /* / == /dev == /dev/fd means it is static. #fail */
91 return 1;
92}
93#endif
94
95
Gregory P. Smith8facece2012-01-21 14:01:08 -080096/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050097static int
98_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -080099{
100 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
101 long prev_fd = -1;
102 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
103 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
104 long iter_fd = PyLong_AsLong(py_fd);
105 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
106 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
107 return 1;
108 }
109 }
110 return 0;
111}
112
113
114/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500115static int
116_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800117{
118 /* Binary search. */
119 Py_ssize_t search_min = 0;
120 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
121 if (search_max < 0)
122 return 0;
123 do {
124 long middle = (search_min + search_max) / 2;
125 long middle_fd = PyLong_AsLong(
126 PySequence_Fast_GET_ITEM(fd_sequence, middle));
127 if (fd == middle_fd)
128 return 1;
129 if (fd > middle_fd)
130 search_min = middle + 1;
131 else
132 search_max = middle - 1;
133 } while (search_min <= search_max);
134 return 0;
135}
136
137
138/* Close all file descriptors in the range start_fd inclusive to
139 * end_fd exclusive except for those in py_fds_to_keep. If the
140 * range defined by [start_fd, end_fd) is large this will take a
141 * long time as it calls close() on EVERY possible fd.
142 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500143static void
144_close_fds_by_brute_force(int start_fd, int end_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800145{
146 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
147 Py_ssize_t keep_seq_idx;
148 int fd_num;
149 /* As py_fds_to_keep is sorted we can loop through the list closing
150 * fds inbetween any in the keep list falling within our range. */
151 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
152 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
153 keep_seq_idx);
154 int keep_fd = PyLong_AsLong(py_keep_fd);
155 if (keep_fd < start_fd)
156 continue;
157 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
158 while (close(fd_num) < 0 && errno == EINTR);
159 }
160 start_fd = keep_fd + 1;
161 }
162 if (start_fd <= end_fd) {
163 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
164 while (close(fd_num) < 0 && errno == EINTR);
165 }
166 }
167}
168
169
170#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
171/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
172 * only to read a directory of short file descriptor number names. The kernel
173 * will return an error if we didn't give it enough space. Highly Unlikely.
174 * This structure is very old and stable: It will not change unless the kernel
175 * chooses to break compatibility with all existing binaries. Highly Unlikely.
176 */
177struct linux_dirent {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700178#if defined(__x86_64__) && defined(__ILP32__)
179 /* Support the wacky x32 ABI (fake 32-bit userspace speaking to x86_64
180 * kernel interfaces) - https://sites.google.com/site/x32abi/ */
181 unsigned long long d_ino;
182 unsigned long long d_off;
183#else
Gregory P. Smith8facece2012-01-21 14:01:08 -0800184 unsigned long d_ino; /* Inode number */
185 unsigned long d_off; /* Offset to next linux_dirent */
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700186#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800187 unsigned short d_reclen; /* Length of this linux_dirent */
188 char d_name[256]; /* Filename (null-terminated) */
189};
190
191/* Close all open file descriptors in the range start_fd inclusive to end_fd
192 * exclusive. Do not close any in the sorted py_fds_to_keep list.
193 *
194 * This version is async signal safe as it does not make any unsafe C library
195 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
196 * to resort to making a kernel system call directly but this is the ONLY api
197 * available that does no harm. opendir/readdir/closedir perform memory
198 * allocation and locking so while they usually work they are not guaranteed
199 * to (especially if you have replaced your malloc implementation). A version
200 * of this function that uses those can be found in the _maybe_unsafe variant.
201 *
202 * This is Linux specific because that is all I am ready to test it on. It
203 * should be easy to add OS specific dirent or dirent64 structures and modify
204 * it with some cpp #define magic to work on other OSes as well if you want.
205 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500206static void
207_close_open_fd_range_safe(int start_fd, int end_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800208{
209 int fd_dir_fd;
210 if (start_fd >= end_fd)
211 return;
Ross Lagerwall71faefc2012-03-19 06:08:43 +0200212#ifdef O_CLOEXEC
213 fd_dir_fd = open(FD_DIR, O_RDONLY | O_CLOEXEC, 0);
214#else
215 fd_dir_fd = open(FD_DIR, O_RDONLY, 0);
216#ifdef FD_CLOEXEC
217 {
218 int old = fcntl(fd_dir_fd, F_GETFD);
219 if (old != -1)
220 fcntl(fd_dir_fd, F_SETFD, old | FD_CLOEXEC);
221 }
222#endif
223#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800224 if (fd_dir_fd == -1) {
225 /* No way to get a list of open fds. */
226 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
227 return;
228 } else {
229 char buffer[sizeof(struct linux_dirent)];
230 int bytes;
231 while ((bytes = syscall(SYS_getdents, fd_dir_fd,
232 (struct linux_dirent *)buffer,
233 sizeof(buffer))) > 0) {
234 struct linux_dirent *entry;
235 int offset;
236 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
237 int fd;
238 entry = (struct linux_dirent *)(buffer + offset);
239 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
240 continue; /* Not a number. */
241 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
242 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
243 while (close(fd) < 0 && errno == EINTR);
244 }
245 }
246 }
247 close(fd_dir_fd);
248 }
249}
250
251#define _close_open_fd_range _close_open_fd_range_safe
252
253#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
254
255
256/* Close all open file descriptors in the range start_fd inclusive to end_fd
257 * exclusive. Do not close any in the sorted py_fds_to_keep list.
258 *
259 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800260 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800261 * likely to ever cause a problem is opendir() as it performs an internal
262 * malloc(). Practically this should not be a problem. The Java VM makes the
263 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
264 *
265 * readdir_r() is not used because it provides no benefit. It is typically
266 * implemented as readdir() followed by memcpy(). See also:
267 * http://womble.decadent.org.uk/readdir_r-advisory.html
268 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500269static void
270_close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
271 PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800272{
273 DIR *proc_fd_dir;
274#ifndef HAVE_DIRFD
275 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
276 (start_fd < end_fd)) {
277 ++start_fd;
278 }
279 if (start_fd >= end_fd)
280 return;
281 /* Close our lowest fd before we call opendir so that it is likely to
282 * reuse that fd otherwise we might close opendir's file descriptor in
283 * our loop. This trick assumes that fd's are allocated on a lowest
284 * available basis. */
285 while (close(start_fd) < 0 && errno == EINTR);
286 ++start_fd;
287#endif
288 if (start_fd >= end_fd)
289 return;
290
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800291#if defined(__FreeBSD__)
292 if (!_is_fdescfs_mounted_on_dev_fd())
293 proc_fd_dir = NULL;
294 else
295#endif
296 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800297 if (!proc_fd_dir) {
298 /* No way to get a list of open fds. */
299 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
300 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800301 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800302#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800303 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800304#else
305 int fd_used_by_opendir = start_fd - 1;
306#endif
307 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800308 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800309 int fd;
310 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
311 continue; /* Not a number. */
312 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
313 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
314 while (close(fd) < 0 && errno == EINTR);
315 }
316 errno = 0;
317 }
318 if (errno) {
319 /* readdir error, revert behavior. Highly Unlikely. */
320 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
321 }
322 closedir(proc_fd_dir);
323 }
324}
325
326#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
327
328#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
329
330
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000331/*
332 * This function is code executed in the child process immediately after fork
333 * to set things up and call exec().
334 *
335 * All of the code in this function must only use async-signal-safe functions,
336 * listed at `man 7 signal` or
337 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
338 *
339 * This restriction is documented at
340 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
341 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500342static void
343child_exec(char *const exec_array[],
344 char *const argv[],
345 char *const envp[],
346 const char *cwd,
347 int p2cread, int p2cwrite,
348 int c2pread, int c2pwrite,
349 int errread, int errwrite,
350 int errpipe_read, int errpipe_write,
351 int close_fds, int restore_signals,
352 int call_setsid,
353 PyObject *py_fds_to_keep,
354 PyObject *preexec_fn,
355 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000356{
Gregory P. Smith8facece2012-01-21 14:01:08 -0800357 int i, saved_errno, unused;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000358 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000359 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000360 /* Buffer large enough to hold a hex integer. We can't malloc. */
361 char hex_errno[sizeof(saved_errno)*2+1];
362
363 /* Close parent's pipe ends. */
364 if (p2cwrite != -1) {
365 POSIX_CALL(close(p2cwrite));
366 }
367 if (c2pread != -1) {
368 POSIX_CALL(close(c2pread));
369 }
370 if (errread != -1) {
371 POSIX_CALL(close(errread));
372 }
373 POSIX_CALL(close(errpipe_read));
374
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200375 /* When duping fds, if there arises a situation where one of the fds is
376 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
377 if (c2pwrite == 0)
378 POSIX_CALL(c2pwrite = dup(c2pwrite));
379 if (errwrite == 0 || errwrite == 1)
380 POSIX_CALL(errwrite = dup(errwrite));
381
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000382 /* Dup fds for child.
383 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
384 would be a no-op (issue #10806). */
385 if (p2cread == 0) {
386 int old = fcntl(p2cread, F_GETFD);
387 if (old != -1)
388 fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
389 } else if (p2cread != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000390 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
391 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000392 if (c2pwrite == 1) {
393 int old = fcntl(c2pwrite, F_GETFD);
394 if (old != -1)
395 fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
396 } else if (c2pwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000397 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
398 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000399 if (errwrite == 2) {
400 int old = fcntl(errwrite, F_GETFD);
401 if (old != -1)
402 fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
403 } else if (errwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000404 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
405 }
406
407 /* Close pipe fds. Make sure we don't close the same fd more than */
408 /* once, or standard fds. */
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000409 if (p2cread > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000410 POSIX_CALL(close(p2cread));
411 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400412 if (c2pwrite > 2 && c2pwrite != p2cread) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000413 POSIX_CALL(close(c2pwrite));
414 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400415 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000416 POSIX_CALL(close(errwrite));
417 }
418
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800419 if (close_fds) {
420 int local_max_fd = max_fd;
421#if defined(__NetBSD__)
422 local_max_fd = fcntl(0, F_MAXFD);
423 if (local_max_fd < 0)
424 local_max_fd = max_fd;
425#endif
426 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
427 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
428 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000429
430 if (cwd)
431 POSIX_CALL(chdir(cwd));
432
433 if (restore_signals)
434 _Py_RestoreSignals();
435
436#ifdef HAVE_SETSID
437 if (call_setsid)
438 POSIX_CALL(setsid());
439#endif
440
441 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
442 /* This is where the user has asked us to deadlock their program. */
443 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
444 if (result == NULL) {
445 /* Stringifying the exception or traceback would involve
446 * memory allocation and thus potential for deadlock.
447 * We've already faced potential deadlock by calling back
448 * into Python in the first place, so it probably doesn't
449 * matter but we avoid it to minimize the possibility. */
450 err_msg = "Exception occurred in preexec_fn.";
451 errno = 0; /* We don't want to report an OSError. */
452 goto error;
453 }
454 /* Py_DECREF(result); - We're about to exec so why bother? */
455 }
456
457 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
458 /* given the executable_list generated by Lib/subprocess.py. */
459 saved_errno = 0;
460 for (i = 0; exec_array[i] != NULL; ++i) {
461 const char *executable = exec_array[i];
462 if (envp) {
463 execve(executable, argv, envp);
464 } else {
465 execv(executable, argv);
466 }
467 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
468 saved_errno = errno;
469 }
470 }
471 /* Report the first exec error, not the last. */
472 if (saved_errno)
473 errno = saved_errno;
474
475error:
476 saved_errno = errno;
477 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800478 /* We ignore all write() return values as the total size of our writes is
479 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000480 if (saved_errno) {
481 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800482 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000483 cur = hex_errno + sizeof(hex_errno);
484 while (saved_errno != 0 && cur > hex_errno) {
485 *--cur = "0123456789ABCDEF"[saved_errno % 16];
486 saved_errno /= 16;
487 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800488 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
489 unused = write(errpipe_write, ":", 1);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000490 /* We can't call strerror(saved_errno). It is not async signal safe.
491 * The parent process will look the error message up. */
492 } else {
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800493 unused = write(errpipe_write, "RuntimeError:0:", 15);
494 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000495 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800496 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000497}
498
499
500static PyObject *
501subprocess_fork_exec(PyObject* self, PyObject *args)
502{
503 PyObject *gc_module = NULL;
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000504 PyObject *executable_list, *py_close_fds, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000505 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000506 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000507 PyObject *preexec_fn_args_tuple = NULL;
508 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
509 int errpipe_read, errpipe_write, close_fds, restore_signals;
510 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000511 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000512 const char *cwd;
513 pid_t pid;
514 int need_to_reenable_gc = 0;
515 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800516 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000517
518 if (!PyArg_ParseTuple(
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000519 args, "OOOOOOiiiiiiiiiiO:fork_exec",
520 &process_args, &executable_list, &py_close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000521 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000522 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
523 &errread, &errwrite, &errpipe_read, &errpipe_write,
524 &restore_signals, &call_setsid, &preexec_fn))
525 return NULL;
526
527 close_fds = PyObject_IsTrue(py_close_fds);
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200528 if (close_fds < 0)
529 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000530 if (close_fds && errpipe_write < 3) { /* precondition */
531 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
532 return NULL;
533 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800534 if (PySequence_Length(py_fds_to_keep) < 0) {
535 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
536 return NULL;
537 }
538 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
539 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000540 return NULL;
541 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000542
543 /* We need to call gc.disable() when we'll be calling preexec_fn */
544 if (preexec_fn != Py_None) {
545 PyObject *result;
546 gc_module = PyImport_ImportModule("gc");
547 if (gc_module == NULL)
548 return NULL;
549 result = PyObject_CallMethod(gc_module, "isenabled", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000550 if (result == NULL) {
551 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000552 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000553 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000554 need_to_reenable_gc = PyObject_IsTrue(result);
555 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000556 if (need_to_reenable_gc == -1) {
557 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000558 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000559 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000560 result = PyObject_CallMethod(gc_module, "disable", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000561 if (result == NULL) {
562 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000563 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000564 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000565 Py_DECREF(result);
566 }
567
568 exec_array = _PySequence_BytesToCharpArray(executable_list);
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200569 if (!exec_array) {
570 Py_XDECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000571 return NULL;
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200572 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000573
574 /* Convert args and env into appropriate arguments for exec() */
575 /* These conversions are done in the parent process to avoid allocating
576 or freeing memory in the child process. */
577 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000578 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000579 /* Equivalent to: */
580 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000581 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200582 if (fast_args == NULL)
583 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000584 num_args = PySequence_Fast_GET_SIZE(fast_args);
585 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000586 if (converted_args == NULL)
587 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000588 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000589 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000590 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000591 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
592 goto cleanup;
593 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
594 }
595
596 argv = _PySequence_BytesToCharpArray(converted_args);
597 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000598 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000599 if (!argv)
600 goto cleanup;
601 }
602
603 if (env_list != Py_None) {
604 envp = _PySequence_BytesToCharpArray(env_list);
605 if (!envp)
606 goto cleanup;
607 }
608
609 if (preexec_fn != Py_None) {
610 preexec_fn_args_tuple = PyTuple_New(0);
611 if (!preexec_fn_args_tuple)
612 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000613 _PyImport_AcquireLock();
614 }
615
616 if (cwd_obj != Py_None) {
617 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
618 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000619 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000620 } else {
621 cwd = NULL;
622 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000623 }
624
625 pid = fork();
626 if (pid == 0) {
627 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000628 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000629 * Code from here to _exit() must only use async-signal-safe functions,
630 * listed at `man 7 signal` or
631 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
632 */
633
634 if (preexec_fn != Py_None) {
635 /* We'll be calling back into Python later so we need to do this.
636 * This call may not be async-signal-safe but neither is calling
637 * back into Python. The user asked us to use hope as a strategy
638 * to avoid deadlock... */
639 PyOS_AfterFork();
640 }
641
642 child_exec(exec_array, argv, envp, cwd,
643 p2cread, p2cwrite, c2pread, c2pwrite,
644 errread, errwrite, errpipe_read, errpipe_write,
645 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800646 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000647 _exit(255);
648 return NULL; /* Dead code to avoid a potential compiler warning. */
649 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000650 Py_XDECREF(cwd_obj2);
651
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000652 if (pid == -1) {
653 /* Capture the errno exception before errno can be clobbered. */
654 PyErr_SetFromErrno(PyExc_OSError);
655 }
656 if (preexec_fn != Py_None &&
657 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
658 PyErr_SetString(PyExc_RuntimeError,
659 "not holding the import lock");
660 }
661
662 /* Parent process */
663 if (envp)
664 _Py_FreeCharPArray(envp);
665 if (argv)
666 _Py_FreeCharPArray(argv);
667 _Py_FreeCharPArray(exec_array);
668
669 /* Reenable gc in the parent process (or if fork failed). */
670 if (need_to_reenable_gc && _enable_gc(gc_module)) {
671 Py_XDECREF(gc_module);
672 return NULL;
673 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000674 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000675 Py_XDECREF(gc_module);
676
677 if (pid == -1)
678 return NULL; /* fork() failed. Exception set earlier. */
679
680 return PyLong_FromPid(pid);
681
682cleanup:
683 if (envp)
684 _Py_FreeCharPArray(envp);
685 if (argv)
686 _Py_FreeCharPArray(argv);
687 _Py_FreeCharPArray(exec_array);
688 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000689 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000690 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000691
692 /* Reenable gc if it was disabled. */
693 if (need_to_reenable_gc)
694 _enable_gc(gc_module);
695 Py_XDECREF(gc_module);
696 return NULL;
697}
698
699
700PyDoc_STRVAR(subprocess_fork_exec_doc,
701"fork_exec(args, executable_list, close_fds, cwd, env,\n\
702 p2cread, p2cwrite, c2pread, c2pwrite,\n\
703 errread, errwrite, errpipe_read, errpipe_write,\n\
704 restore_signals, call_setsid, preexec_fn)\n\
705\n\
706Forks a child process, closes parent file descriptors as appropriate in the\n\
707child and dups the few that are needed before calling exec() in the child\n\
708process.\n\
709\n\
710The preexec_fn, if supplied, will be called immediately before exec.\n\
711WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
712 It may trigger infrequent, difficult to debug deadlocks.\n\
713\n\
714If an error occurs in the child process before the exec, it is\n\
715serialized and written to the errpipe_write fd per subprocess.py.\n\
716\n\
717Returns: the child process's PID.\n\
718\n\
719Raises: Only on an error in the parent process.\n\
720");
721
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000722PyDoc_STRVAR(subprocess_cloexec_pipe_doc,
723"cloexec_pipe() -> (read_end, write_end)\n\n\
724Create a pipe whose ends have the cloexec flag set.");
725
726static PyObject *
727subprocess_cloexec_pipe(PyObject *self, PyObject *noargs)
728{
729 int fds[2];
730 int res;
731#ifdef HAVE_PIPE2
732 Py_BEGIN_ALLOW_THREADS
733 res = pipe2(fds, O_CLOEXEC);
734 Py_END_ALLOW_THREADS
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000735 if (res != 0 && errno == ENOSYS)
736 {
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000737 {
738#endif
739 /* We hold the GIL which offers some protection from other code calling
740 * fork() before the CLOEXEC flags have been set but we can't guarantee
741 * anything without pipe2(). */
742 long oldflags;
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000743
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000744 res = pipe(fds);
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000745
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000746 if (res == 0) {
747 oldflags = fcntl(fds[0], F_GETFD, 0);
748 if (oldflags < 0) res = oldflags;
749 }
750 if (res == 0)
751 res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
752
753 if (res == 0) {
754 oldflags = fcntl(fds[1], F_GETFD, 0);
755 if (oldflags < 0) res = oldflags;
756 }
757 if (res == 0)
758 res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
759#ifdef HAVE_PIPE2
760 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000761 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000762#endif
763 if (res != 0)
764 return PyErr_SetFromErrno(PyExc_OSError);
765 return Py_BuildValue("(ii)", fds[0], fds[1]);
766}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000767
768/* module level code ********************************************************/
769
770PyDoc_STRVAR(module_doc,
771"A POSIX helper for the subprocess module.");
772
773
774static PyMethodDef module_methods[] = {
775 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000776 {"cloexec_pipe", subprocess_cloexec_pipe, METH_NOARGS, subprocess_cloexec_pipe_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000777 {NULL, NULL} /* sentinel */
778};
779
780
781static struct PyModuleDef _posixsubprocessmodule = {
782 PyModuleDef_HEAD_INIT,
783 "_posixsubprocess",
784 module_doc,
785 -1, /* No memory is needed. */
786 module_methods,
787};
788
789PyMODINIT_FUNC
790PyInit__posixsubprocess(void)
791{
792#ifdef _SC_OPEN_MAX
793 max_fd = sysconf(_SC_OPEN_MAX);
794 if (max_fd == -1)
795#endif
796 max_fd = 256; /* Matches Lib/subprocess.py */
797
798 return PyModule_Create(&_posixsubprocessmodule);
799}