blob: d520c8c7692efc007b80da3f102e774b32f79e68 [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 {
178 unsigned long d_ino; /* Inode number */
179 unsigned long d_off; /* Offset to next linux_dirent */
180 unsigned short d_reclen; /* Length of this linux_dirent */
181 char d_name[256]; /* Filename (null-terminated) */
182};
183
184/* Close all open file descriptors in the range start_fd inclusive to end_fd
185 * exclusive. Do not close any in the sorted py_fds_to_keep list.
186 *
187 * This version is async signal safe as it does not make any unsafe C library
188 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
189 * to resort to making a kernel system call directly but this is the ONLY api
190 * available that does no harm. opendir/readdir/closedir perform memory
191 * allocation and locking so while they usually work they are not guaranteed
192 * to (especially if you have replaced your malloc implementation). A version
193 * of this function that uses those can be found in the _maybe_unsafe variant.
194 *
195 * This is Linux specific because that is all I am ready to test it on. It
196 * should be easy to add OS specific dirent or dirent64 structures and modify
197 * it with some cpp #define magic to work on other OSes as well if you want.
198 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500199static void
200_close_open_fd_range_safe(int start_fd, int end_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800201{
202 int fd_dir_fd;
203 if (start_fd >= end_fd)
204 return;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800205 fd_dir_fd = open(FD_DIR, O_RDONLY | O_CLOEXEC, 0);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800206 if (fd_dir_fd == -1) {
207 /* No way to get a list of open fds. */
208 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
209 return;
210 } else {
211 char buffer[sizeof(struct linux_dirent)];
212 int bytes;
213 while ((bytes = syscall(SYS_getdents, fd_dir_fd,
214 (struct linux_dirent *)buffer,
215 sizeof(buffer))) > 0) {
216 struct linux_dirent *entry;
217 int offset;
218 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
219 int fd;
220 entry = (struct linux_dirent *)(buffer + offset);
221 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
222 continue; /* Not a number. */
223 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
224 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
225 while (close(fd) < 0 && errno == EINTR);
226 }
227 }
228 }
229 close(fd_dir_fd);
230 }
231}
232
233#define _close_open_fd_range _close_open_fd_range_safe
234
235#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
236
237
238/* Close all open file descriptors in the range start_fd inclusive to end_fd
239 * exclusive. Do not close any in the sorted py_fds_to_keep list.
240 *
241 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800242 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800243 * likely to ever cause a problem is opendir() as it performs an internal
244 * malloc(). Practically this should not be a problem. The Java VM makes the
245 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
246 *
247 * readdir_r() is not used because it provides no benefit. It is typically
248 * implemented as readdir() followed by memcpy(). See also:
249 * http://womble.decadent.org.uk/readdir_r-advisory.html
250 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500251static void
252_close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
253 PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800254{
255 DIR *proc_fd_dir;
256#ifndef HAVE_DIRFD
257 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
258 (start_fd < end_fd)) {
259 ++start_fd;
260 }
261 if (start_fd >= end_fd)
262 return;
263 /* Close our lowest fd before we call opendir so that it is likely to
264 * reuse that fd otherwise we might close opendir's file descriptor in
265 * our loop. This trick assumes that fd's are allocated on a lowest
266 * available basis. */
267 while (close(start_fd) < 0 && errno == EINTR);
268 ++start_fd;
269#endif
270 if (start_fd >= end_fd)
271 return;
272
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800273#if defined(__FreeBSD__)
274 if (!_is_fdescfs_mounted_on_dev_fd())
275 proc_fd_dir = NULL;
276 else
277#endif
278 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800279 if (!proc_fd_dir) {
280 /* No way to get a list of open fds. */
281 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
282 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800283 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800284#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800285 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800286#else
287 int fd_used_by_opendir = start_fd - 1;
288#endif
289 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800290 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800291 int fd;
292 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
293 continue; /* Not a number. */
294 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
295 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
296 while (close(fd) < 0 && errno == EINTR);
297 }
298 errno = 0;
299 }
300 if (errno) {
301 /* readdir error, revert behavior. Highly Unlikely. */
302 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
303 }
304 closedir(proc_fd_dir);
305 }
306}
307
308#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
309
310#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
311
312
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000313/*
314 * This function is code executed in the child process immediately after fork
315 * to set things up and call exec().
316 *
317 * All of the code in this function must only use async-signal-safe functions,
318 * listed at `man 7 signal` or
319 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
320 *
321 * This restriction is documented at
322 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
323 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500324static void
325child_exec(char *const exec_array[],
326 char *const argv[],
327 char *const envp[],
328 const char *cwd,
329 int p2cread, int p2cwrite,
330 int c2pread, int c2pwrite,
331 int errread, int errwrite,
332 int errpipe_read, int errpipe_write,
333 int close_fds, int restore_signals,
334 int call_setsid,
335 PyObject *py_fds_to_keep,
336 PyObject *preexec_fn,
337 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000338{
Gregory P. Smith8facece2012-01-21 14:01:08 -0800339 int i, saved_errno, unused;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000340 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000341 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000342 /* Buffer large enough to hold a hex integer. We can't malloc. */
343 char hex_errno[sizeof(saved_errno)*2+1];
344
345 /* Close parent's pipe ends. */
346 if (p2cwrite != -1) {
347 POSIX_CALL(close(p2cwrite));
348 }
349 if (c2pread != -1) {
350 POSIX_CALL(close(c2pread));
351 }
352 if (errread != -1) {
353 POSIX_CALL(close(errread));
354 }
355 POSIX_CALL(close(errpipe_read));
356
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200357 /* When duping fds, if there arises a situation where one of the fds is
358 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
359 if (c2pwrite == 0)
360 POSIX_CALL(c2pwrite = dup(c2pwrite));
361 if (errwrite == 0 || errwrite == 1)
362 POSIX_CALL(errwrite = dup(errwrite));
363
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000364 /* Dup fds for child.
365 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
366 would be a no-op (issue #10806). */
367 if (p2cread == 0) {
368 int old = fcntl(p2cread, F_GETFD);
369 if (old != -1)
370 fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
371 } else if (p2cread != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000372 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
373 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000374 if (c2pwrite == 1) {
375 int old = fcntl(c2pwrite, F_GETFD);
376 if (old != -1)
377 fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
378 } else if (c2pwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000379 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
380 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000381 if (errwrite == 2) {
382 int old = fcntl(errwrite, F_GETFD);
383 if (old != -1)
384 fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
385 } else if (errwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000386 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
387 }
388
389 /* Close pipe fds. Make sure we don't close the same fd more than */
390 /* once, or standard fds. */
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000391 if (p2cread > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000392 POSIX_CALL(close(p2cread));
393 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400394 if (c2pwrite > 2 && c2pwrite != p2cread) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000395 POSIX_CALL(close(c2pwrite));
396 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400397 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000398 POSIX_CALL(close(errwrite));
399 }
400
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800401 if (close_fds) {
402 int local_max_fd = max_fd;
403#if defined(__NetBSD__)
404 local_max_fd = fcntl(0, F_MAXFD);
405 if (local_max_fd < 0)
406 local_max_fd = max_fd;
407#endif
408 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
409 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
410 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000411
412 if (cwd)
413 POSIX_CALL(chdir(cwd));
414
415 if (restore_signals)
416 _Py_RestoreSignals();
417
418#ifdef HAVE_SETSID
419 if (call_setsid)
420 POSIX_CALL(setsid());
421#endif
422
423 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
424 /* This is where the user has asked us to deadlock their program. */
425 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
426 if (result == NULL) {
427 /* Stringifying the exception or traceback would involve
428 * memory allocation and thus potential for deadlock.
429 * We've already faced potential deadlock by calling back
430 * into Python in the first place, so it probably doesn't
431 * matter but we avoid it to minimize the possibility. */
432 err_msg = "Exception occurred in preexec_fn.";
433 errno = 0; /* We don't want to report an OSError. */
434 goto error;
435 }
436 /* Py_DECREF(result); - We're about to exec so why bother? */
437 }
438
439 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
440 /* given the executable_list generated by Lib/subprocess.py. */
441 saved_errno = 0;
442 for (i = 0; exec_array[i] != NULL; ++i) {
443 const char *executable = exec_array[i];
444 if (envp) {
445 execve(executable, argv, envp);
446 } else {
447 execv(executable, argv);
448 }
449 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
450 saved_errno = errno;
451 }
452 }
453 /* Report the first exec error, not the last. */
454 if (saved_errno)
455 errno = saved_errno;
456
457error:
458 saved_errno = errno;
459 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800460 /* We ignore all write() return values as the total size of our writes is
461 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000462 if (saved_errno) {
463 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800464 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000465 cur = hex_errno + sizeof(hex_errno);
466 while (saved_errno != 0 && cur > hex_errno) {
467 *--cur = "0123456789ABCDEF"[saved_errno % 16];
468 saved_errno /= 16;
469 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800470 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
471 unused = write(errpipe_write, ":", 1);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000472 /* We can't call strerror(saved_errno). It is not async signal safe.
473 * The parent process will look the error message up. */
474 } else {
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800475 unused = write(errpipe_write, "RuntimeError:0:", 15);
476 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000477 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800478 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000479}
480
481
482static PyObject *
483subprocess_fork_exec(PyObject* self, PyObject *args)
484{
485 PyObject *gc_module = NULL;
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000486 PyObject *executable_list, *py_close_fds, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000487 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000488 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000489 PyObject *preexec_fn_args_tuple = NULL;
490 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
491 int errpipe_read, errpipe_write, close_fds, restore_signals;
492 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000493 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000494 const char *cwd;
495 pid_t pid;
496 int need_to_reenable_gc = 0;
497 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800498 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000499
500 if (!PyArg_ParseTuple(
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000501 args, "OOOOOOiiiiiiiiiiO:fork_exec",
502 &process_args, &executable_list, &py_close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000503 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000504 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
505 &errread, &errwrite, &errpipe_read, &errpipe_write,
506 &restore_signals, &call_setsid, &preexec_fn))
507 return NULL;
508
509 close_fds = PyObject_IsTrue(py_close_fds);
510 if (close_fds && errpipe_write < 3) { /* precondition */
511 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
512 return NULL;
513 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800514 if (PySequence_Length(py_fds_to_keep) < 0) {
515 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
516 return NULL;
517 }
518 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
519 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000520 return NULL;
521 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000522
523 /* We need to call gc.disable() when we'll be calling preexec_fn */
524 if (preexec_fn != Py_None) {
525 PyObject *result;
526 gc_module = PyImport_ImportModule("gc");
527 if (gc_module == NULL)
528 return NULL;
529 result = PyObject_CallMethod(gc_module, "isenabled", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000530 if (result == NULL) {
531 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000532 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000533 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000534 need_to_reenable_gc = PyObject_IsTrue(result);
535 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000536 if (need_to_reenable_gc == -1) {
537 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000538 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000539 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000540 result = PyObject_CallMethod(gc_module, "disable", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000541 if (result == NULL) {
542 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000543 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000544 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000545 Py_DECREF(result);
546 }
547
548 exec_array = _PySequence_BytesToCharpArray(executable_list);
549 if (!exec_array)
550 return NULL;
551
552 /* Convert args and env into appropriate arguments for exec() */
553 /* These conversions are done in the parent process to avoid allocating
554 or freeing memory in the child process. */
555 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000556 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000557 /* Equivalent to: */
558 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000559 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
560 num_args = PySequence_Fast_GET_SIZE(fast_args);
561 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000562 if (converted_args == NULL)
563 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000564 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000565 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000566 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000567 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
568 goto cleanup;
569 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
570 }
571
572 argv = _PySequence_BytesToCharpArray(converted_args);
573 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000574 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000575 if (!argv)
576 goto cleanup;
577 }
578
579 if (env_list != Py_None) {
580 envp = _PySequence_BytesToCharpArray(env_list);
581 if (!envp)
582 goto cleanup;
583 }
584
585 if (preexec_fn != Py_None) {
586 preexec_fn_args_tuple = PyTuple_New(0);
587 if (!preexec_fn_args_tuple)
588 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000589 _PyImport_AcquireLock();
590 }
591
592 if (cwd_obj != Py_None) {
593 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
594 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000595 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000596 } else {
597 cwd = NULL;
598 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000599 }
600
601 pid = fork();
602 if (pid == 0) {
603 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000604 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000605 * Code from here to _exit() must only use async-signal-safe functions,
606 * listed at `man 7 signal` or
607 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
608 */
609
610 if (preexec_fn != Py_None) {
611 /* We'll be calling back into Python later so we need to do this.
612 * This call may not be async-signal-safe but neither is calling
613 * back into Python. The user asked us to use hope as a strategy
614 * to avoid deadlock... */
615 PyOS_AfterFork();
616 }
617
618 child_exec(exec_array, argv, envp, cwd,
619 p2cread, p2cwrite, c2pread, c2pwrite,
620 errread, errwrite, errpipe_read, errpipe_write,
621 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800622 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000623 _exit(255);
624 return NULL; /* Dead code to avoid a potential compiler warning. */
625 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000626 Py_XDECREF(cwd_obj2);
627
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000628 if (pid == -1) {
629 /* Capture the errno exception before errno can be clobbered. */
630 PyErr_SetFromErrno(PyExc_OSError);
631 }
632 if (preexec_fn != Py_None &&
633 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
634 PyErr_SetString(PyExc_RuntimeError,
635 "not holding the import lock");
636 }
637
638 /* Parent process */
639 if (envp)
640 _Py_FreeCharPArray(envp);
641 if (argv)
642 _Py_FreeCharPArray(argv);
643 _Py_FreeCharPArray(exec_array);
644
645 /* Reenable gc in the parent process (or if fork failed). */
646 if (need_to_reenable_gc && _enable_gc(gc_module)) {
647 Py_XDECREF(gc_module);
648 return NULL;
649 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000650 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000651 Py_XDECREF(gc_module);
652
653 if (pid == -1)
654 return NULL; /* fork() failed. Exception set earlier. */
655
656 return PyLong_FromPid(pid);
657
658cleanup:
659 if (envp)
660 _Py_FreeCharPArray(envp);
661 if (argv)
662 _Py_FreeCharPArray(argv);
663 _Py_FreeCharPArray(exec_array);
664 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000665 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000666 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000667
668 /* Reenable gc if it was disabled. */
669 if (need_to_reenable_gc)
670 _enable_gc(gc_module);
671 Py_XDECREF(gc_module);
672 return NULL;
673}
674
675
676PyDoc_STRVAR(subprocess_fork_exec_doc,
677"fork_exec(args, executable_list, close_fds, cwd, env,\n\
678 p2cread, p2cwrite, c2pread, c2pwrite,\n\
679 errread, errwrite, errpipe_read, errpipe_write,\n\
680 restore_signals, call_setsid, preexec_fn)\n\
681\n\
682Forks a child process, closes parent file descriptors as appropriate in the\n\
683child and dups the few that are needed before calling exec() in the child\n\
684process.\n\
685\n\
686The preexec_fn, if supplied, will be called immediately before exec.\n\
687WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
688 It may trigger infrequent, difficult to debug deadlocks.\n\
689\n\
690If an error occurs in the child process before the exec, it is\n\
691serialized and written to the errpipe_write fd per subprocess.py.\n\
692\n\
693Returns: the child process's PID.\n\
694\n\
695Raises: Only on an error in the parent process.\n\
696");
697
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000698PyDoc_STRVAR(subprocess_cloexec_pipe_doc,
699"cloexec_pipe() -> (read_end, write_end)\n\n\
700Create a pipe whose ends have the cloexec flag set.");
701
702static PyObject *
703subprocess_cloexec_pipe(PyObject *self, PyObject *noargs)
704{
705 int fds[2];
706 int res;
707#ifdef HAVE_PIPE2
708 Py_BEGIN_ALLOW_THREADS
709 res = pipe2(fds, O_CLOEXEC);
710 Py_END_ALLOW_THREADS
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000711 if (res != 0 && errno == ENOSYS)
712 {
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000713 {
714#endif
715 /* We hold the GIL which offers some protection from other code calling
716 * fork() before the CLOEXEC flags have been set but we can't guarantee
717 * anything without pipe2(). */
718 long oldflags;
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000719
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000720 res = pipe(fds);
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000721
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000722 if (res == 0) {
723 oldflags = fcntl(fds[0], F_GETFD, 0);
724 if (oldflags < 0) res = oldflags;
725 }
726 if (res == 0)
727 res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
728
729 if (res == 0) {
730 oldflags = fcntl(fds[1], F_GETFD, 0);
731 if (oldflags < 0) res = oldflags;
732 }
733 if (res == 0)
734 res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
735#ifdef HAVE_PIPE2
736 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000737 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000738#endif
739 if (res != 0)
740 return PyErr_SetFromErrno(PyExc_OSError);
741 return Py_BuildValue("(ii)", fds[0], fds[1]);
742}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000743
744/* module level code ********************************************************/
745
746PyDoc_STRVAR(module_doc,
747"A POSIX helper for the subprocess module.");
748
749
750static PyMethodDef module_methods[] = {
751 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000752 {"cloexec_pipe", subprocess_cloexec_pipe, METH_NOARGS, subprocess_cloexec_pipe_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000753 {NULL, NULL} /* sentinel */
754};
755
756
757static struct PyModuleDef _posixsubprocessmodule = {
758 PyModuleDef_HEAD_INIT,
759 "_posixsubprocess",
760 module_doc,
761 -1, /* No memory is needed. */
762 module_methods,
763};
764
765PyMODINIT_FUNC
766PyInit__posixsubprocess(void)
767{
768#ifdef _SC_OPEN_MAX
769 max_fd = sysconf(_SC_OPEN_MAX);
770 if (max_fd == -1)
771#endif
772 max_fd = 256; /* Matches Lib/subprocess.py */
773
774 return PyModule_Create(&_posixsubprocessmodule);
775}