blob: a33b79ae69b757fc5b14a8a0770e5fbd659638c8 [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. */
46static int _enable_gc(PyObject *gc_module)
47{
48 PyObject *result;
49 result = PyObject_CallMethod(gc_module, "enable", NULL);
50 if (result == NULL)
51 return 1;
52 Py_DECREF(result);
53 return 0;
54}
55
56
Gregory P. Smith8facece2012-01-21 14:01:08 -080057/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
58static int _pos_int_from_ascii(char *name)
59{
60 int num = 0;
61 while (*name >= '0' && *name <= '9') {
62 num = num * 10 + (*name - '0');
63 ++name;
64 }
65 if (*name)
66 return -1; /* Non digit found, not a number. */
67 return num;
68}
69
70
Gregory P. Smith4842efc2012-01-21 21:01:24 -080071#if defined(__FreeBSD__)
72/* When /dev/fd isn't mounted it is often a static directory populated
73 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
74 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
75 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
76 * that properly supports /dev/fd.
77 */
78static int _is_fdescfs_mounted_on_dev_fd()
79{
80 struct stat dev_stat;
81 struct stat dev_fd_stat;
82 if (stat("/dev", &dev_stat) != 0)
83 return 0;
84 if (stat(FD_DIR, &dev_fd_stat) != 0)
85 return 0;
86 if (dev_stat.st_dev == dev_fd_stat.st_dev)
87 return 0; /* / == /dev == /dev/fd means it is static. #fail */
88 return 1;
89}
90#endif
91
92
Gregory P. Smith8facece2012-01-21 14:01:08 -080093/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
94static int _sanity_check_python_fd_sequence(PyObject *fd_sequence)
95{
96 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
97 long prev_fd = -1;
98 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
99 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
100 long iter_fd = PyLong_AsLong(py_fd);
101 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
102 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
103 return 1;
104 }
105 }
106 return 0;
107}
108
109
110/* Is fd found in the sorted Python Sequence? */
111static int _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
112{
113 /* Binary search. */
114 Py_ssize_t search_min = 0;
115 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
116 if (search_max < 0)
117 return 0;
118 do {
119 long middle = (search_min + search_max) / 2;
120 long middle_fd = PyLong_AsLong(
121 PySequence_Fast_GET_ITEM(fd_sequence, middle));
122 if (fd == middle_fd)
123 return 1;
124 if (fd > middle_fd)
125 search_min = middle + 1;
126 else
127 search_max = middle - 1;
128 } while (search_min <= search_max);
129 return 0;
130}
131
132
133/* Close all file descriptors in the range start_fd inclusive to
134 * end_fd exclusive except for those in py_fds_to_keep. If the
135 * range defined by [start_fd, end_fd) is large this will take a
136 * long time as it calls close() on EVERY possible fd.
137 */
138static void _close_fds_by_brute_force(int start_fd, int end_fd,
139 PyObject *py_fds_to_keep)
140{
141 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
142 Py_ssize_t keep_seq_idx;
143 int fd_num;
144 /* As py_fds_to_keep is sorted we can loop through the list closing
145 * fds inbetween any in the keep list falling within our range. */
146 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
147 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
148 keep_seq_idx);
149 int keep_fd = PyLong_AsLong(py_keep_fd);
150 if (keep_fd < start_fd)
151 continue;
152 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
153 while (close(fd_num) < 0 && errno == EINTR);
154 }
155 start_fd = keep_fd + 1;
156 }
157 if (start_fd <= end_fd) {
158 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
159 while (close(fd_num) < 0 && errno == EINTR);
160 }
161 }
162}
163
164
165#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
166/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
167 * only to read a directory of short file descriptor number names. The kernel
168 * will return an error if we didn't give it enough space. Highly Unlikely.
169 * This structure is very old and stable: It will not change unless the kernel
170 * chooses to break compatibility with all existing binaries. Highly Unlikely.
171 */
172struct linux_dirent {
173 unsigned long d_ino; /* Inode number */
174 unsigned long d_off; /* Offset to next linux_dirent */
175 unsigned short d_reclen; /* Length of this linux_dirent */
176 char d_name[256]; /* Filename (null-terminated) */
177};
178
179/* Close all open file descriptors in the range start_fd inclusive to end_fd
180 * exclusive. Do not close any in the sorted py_fds_to_keep list.
181 *
182 * This version is async signal safe as it does not make any unsafe C library
183 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
184 * to resort to making a kernel system call directly but this is the ONLY api
185 * available that does no harm. opendir/readdir/closedir perform memory
186 * allocation and locking so while they usually work they are not guaranteed
187 * to (especially if you have replaced your malloc implementation). A version
188 * of this function that uses those can be found in the _maybe_unsafe variant.
189 *
190 * This is Linux specific because that is all I am ready to test it on. It
191 * should be easy to add OS specific dirent or dirent64 structures and modify
192 * it with some cpp #define magic to work on other OSes as well if you want.
193 */
194static void _close_open_fd_range_safe(int start_fd, int end_fd,
195 PyObject* py_fds_to_keep)
196{
197 int fd_dir_fd;
198 if (start_fd >= end_fd)
199 return;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800200 fd_dir_fd = open(FD_DIR, O_RDONLY | O_CLOEXEC, 0);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800201 if (fd_dir_fd == -1) {
202 /* No way to get a list of open fds. */
203 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
204 return;
205 } else {
206 char buffer[sizeof(struct linux_dirent)];
207 int bytes;
208 while ((bytes = syscall(SYS_getdents, fd_dir_fd,
209 (struct linux_dirent *)buffer,
210 sizeof(buffer))) > 0) {
211 struct linux_dirent *entry;
212 int offset;
213 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
214 int fd;
215 entry = (struct linux_dirent *)(buffer + offset);
216 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
217 continue; /* Not a number. */
218 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
219 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
220 while (close(fd) < 0 && errno == EINTR);
221 }
222 }
223 }
224 close(fd_dir_fd);
225 }
226}
227
228#define _close_open_fd_range _close_open_fd_range_safe
229
230#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
231
232
233/* Close all open file descriptors in the range start_fd inclusive to end_fd
234 * exclusive. Do not close any in the sorted py_fds_to_keep list.
235 *
236 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800237 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800238 * likely to ever cause a problem is opendir() as it performs an internal
239 * malloc(). Practically this should not be a problem. The Java VM makes the
240 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
241 *
242 * readdir_r() is not used because it provides no benefit. It is typically
243 * implemented as readdir() followed by memcpy(). See also:
244 * http://womble.decadent.org.uk/readdir_r-advisory.html
245 */
246static void _close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
247 PyObject* py_fds_to_keep)
248{
249 DIR *proc_fd_dir;
250#ifndef HAVE_DIRFD
251 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
252 (start_fd < end_fd)) {
253 ++start_fd;
254 }
255 if (start_fd >= end_fd)
256 return;
257 /* Close our lowest fd before we call opendir so that it is likely to
258 * reuse that fd otherwise we might close opendir's file descriptor in
259 * our loop. This trick assumes that fd's are allocated on a lowest
260 * available basis. */
261 while (close(start_fd) < 0 && errno == EINTR);
262 ++start_fd;
263#endif
264 if (start_fd >= end_fd)
265 return;
266
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800267#if defined(__FreeBSD__)
268 if (!_is_fdescfs_mounted_on_dev_fd())
269 proc_fd_dir = NULL;
270 else
271#endif
272 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800273 if (!proc_fd_dir) {
274 /* No way to get a list of open fds. */
275 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
276 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800277 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800278#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800279 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800280#else
281 int fd_used_by_opendir = start_fd - 1;
282#endif
283 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800284 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800285 int fd;
286 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
287 continue; /* Not a number. */
288 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
289 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
290 while (close(fd) < 0 && errno == EINTR);
291 }
292 errno = 0;
293 }
294 if (errno) {
295 /* readdir error, revert behavior. Highly Unlikely. */
296 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
297 }
298 closedir(proc_fd_dir);
299 }
300}
301
302#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
303
304#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
305
306
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000307/*
308 * This function is code executed in the child process immediately after fork
309 * to set things up and call exec().
310 *
311 * All of the code in this function must only use async-signal-safe functions,
312 * listed at `man 7 signal` or
313 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
314 *
315 * This restriction is documented at
316 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
317 */
318static void child_exec(char *const exec_array[],
319 char *const argv[],
320 char *const envp[],
321 const char *cwd,
322 int p2cread, int p2cwrite,
323 int c2pread, int c2pwrite,
324 int errread, int errwrite,
325 int errpipe_read, int errpipe_write,
326 int close_fds, int restore_signals,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800327 int call_setsid,
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000328 PyObject *py_fds_to_keep,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000329 PyObject *preexec_fn,
330 PyObject *preexec_fn_args_tuple)
331{
Gregory P. Smith8facece2012-01-21 14:01:08 -0800332 int i, saved_errno, unused;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000333 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000334 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000335 /* Buffer large enough to hold a hex integer. We can't malloc. */
336 char hex_errno[sizeof(saved_errno)*2+1];
337
338 /* Close parent's pipe ends. */
339 if (p2cwrite != -1) {
340 POSIX_CALL(close(p2cwrite));
341 }
342 if (c2pread != -1) {
343 POSIX_CALL(close(c2pread));
344 }
345 if (errread != -1) {
346 POSIX_CALL(close(errread));
347 }
348 POSIX_CALL(close(errpipe_read));
349
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200350 /* When duping fds, if there arises a situation where one of the fds is
351 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
352 if (c2pwrite == 0)
353 POSIX_CALL(c2pwrite = dup(c2pwrite));
354 if (errwrite == 0 || errwrite == 1)
355 POSIX_CALL(errwrite = dup(errwrite));
356
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000357 /* Dup fds for child.
358 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
359 would be a no-op (issue #10806). */
360 if (p2cread == 0) {
361 int old = fcntl(p2cread, F_GETFD);
362 if (old != -1)
363 fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
364 } else if (p2cread != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000365 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
366 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000367 if (c2pwrite == 1) {
368 int old = fcntl(c2pwrite, F_GETFD);
369 if (old != -1)
370 fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
371 } else if (c2pwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000372 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
373 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000374 if (errwrite == 2) {
375 int old = fcntl(errwrite, F_GETFD);
376 if (old != -1)
377 fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
378 } else if (errwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000379 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
380 }
381
382 /* Close pipe fds. Make sure we don't close the same fd more than */
383 /* once, or standard fds. */
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000384 if (p2cread > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000385 POSIX_CALL(close(p2cread));
386 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400387 if (c2pwrite > 2 && c2pwrite != p2cread) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000388 POSIX_CALL(close(c2pwrite));
389 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400390 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000391 POSIX_CALL(close(errwrite));
392 }
393
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800394 if (close_fds) {
395 int local_max_fd = max_fd;
396#if defined(__NetBSD__)
397 local_max_fd = fcntl(0, F_MAXFD);
398 if (local_max_fd < 0)
399 local_max_fd = max_fd;
400#endif
401 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
402 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
403 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000404
405 if (cwd)
406 POSIX_CALL(chdir(cwd));
407
408 if (restore_signals)
409 _Py_RestoreSignals();
410
411#ifdef HAVE_SETSID
412 if (call_setsid)
413 POSIX_CALL(setsid());
414#endif
415
416 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
417 /* This is where the user has asked us to deadlock their program. */
418 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
419 if (result == NULL) {
420 /* Stringifying the exception or traceback would involve
421 * memory allocation and thus potential for deadlock.
422 * We've already faced potential deadlock by calling back
423 * into Python in the first place, so it probably doesn't
424 * matter but we avoid it to minimize the possibility. */
425 err_msg = "Exception occurred in preexec_fn.";
426 errno = 0; /* We don't want to report an OSError. */
427 goto error;
428 }
429 /* Py_DECREF(result); - We're about to exec so why bother? */
430 }
431
432 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
433 /* given the executable_list generated by Lib/subprocess.py. */
434 saved_errno = 0;
435 for (i = 0; exec_array[i] != NULL; ++i) {
436 const char *executable = exec_array[i];
437 if (envp) {
438 execve(executable, argv, envp);
439 } else {
440 execv(executable, argv);
441 }
442 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
443 saved_errno = errno;
444 }
445 }
446 /* Report the first exec error, not the last. */
447 if (saved_errno)
448 errno = saved_errno;
449
450error:
451 saved_errno = errno;
452 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800453 /* We ignore all write() return values as the total size of our writes is
454 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000455 if (saved_errno) {
456 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800457 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000458 cur = hex_errno + sizeof(hex_errno);
459 while (saved_errno != 0 && cur > hex_errno) {
460 *--cur = "0123456789ABCDEF"[saved_errno % 16];
461 saved_errno /= 16;
462 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800463 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
464 unused = write(errpipe_write, ":", 1);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000465 /* We can't call strerror(saved_errno). It is not async signal safe.
466 * The parent process will look the error message up. */
467 } else {
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800468 unused = write(errpipe_write, "RuntimeError:0:", 15);
469 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000470 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800471 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000472}
473
474
475static PyObject *
476subprocess_fork_exec(PyObject* self, PyObject *args)
477{
478 PyObject *gc_module = NULL;
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000479 PyObject *executable_list, *py_close_fds, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000480 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000481 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000482 PyObject *preexec_fn_args_tuple = NULL;
483 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
484 int errpipe_read, errpipe_write, close_fds, restore_signals;
485 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000486 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000487 const char *cwd;
488 pid_t pid;
489 int need_to_reenable_gc = 0;
490 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800491 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000492
493 if (!PyArg_ParseTuple(
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000494 args, "OOOOOOiiiiiiiiiiO:fork_exec",
495 &process_args, &executable_list, &py_close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000496 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000497 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
498 &errread, &errwrite, &errpipe_read, &errpipe_write,
499 &restore_signals, &call_setsid, &preexec_fn))
500 return NULL;
501
502 close_fds = PyObject_IsTrue(py_close_fds);
503 if (close_fds && errpipe_write < 3) { /* precondition */
504 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
505 return NULL;
506 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800507 if (PySequence_Length(py_fds_to_keep) < 0) {
508 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
509 return NULL;
510 }
511 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
512 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000513 return NULL;
514 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000515
516 /* We need to call gc.disable() when we'll be calling preexec_fn */
517 if (preexec_fn != Py_None) {
518 PyObject *result;
519 gc_module = PyImport_ImportModule("gc");
520 if (gc_module == NULL)
521 return NULL;
522 result = PyObject_CallMethod(gc_module, "isenabled", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000523 if (result == NULL) {
524 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000525 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000526 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000527 need_to_reenable_gc = PyObject_IsTrue(result);
528 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000529 if (need_to_reenable_gc == -1) {
530 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000531 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000532 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000533 result = PyObject_CallMethod(gc_module, "disable", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000534 if (result == NULL) {
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 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000538 Py_DECREF(result);
539 }
540
541 exec_array = _PySequence_BytesToCharpArray(executable_list);
542 if (!exec_array)
543 return NULL;
544
545 /* Convert args and env into appropriate arguments for exec() */
546 /* These conversions are done in the parent process to avoid allocating
547 or freeing memory in the child process. */
548 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000549 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000550 /* Equivalent to: */
551 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000552 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
553 num_args = PySequence_Fast_GET_SIZE(fast_args);
554 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000555 if (converted_args == NULL)
556 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000557 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000558 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000559 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000560 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
561 goto cleanup;
562 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
563 }
564
565 argv = _PySequence_BytesToCharpArray(converted_args);
566 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000567 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000568 if (!argv)
569 goto cleanup;
570 }
571
572 if (env_list != Py_None) {
573 envp = _PySequence_BytesToCharpArray(env_list);
574 if (!envp)
575 goto cleanup;
576 }
577
578 if (preexec_fn != Py_None) {
579 preexec_fn_args_tuple = PyTuple_New(0);
580 if (!preexec_fn_args_tuple)
581 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000582 _PyImport_AcquireLock();
583 }
584
585 if (cwd_obj != Py_None) {
586 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
587 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000588 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000589 } else {
590 cwd = NULL;
591 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000592 }
593
594 pid = fork();
595 if (pid == 0) {
596 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000597 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000598 * Code from here to _exit() must only use async-signal-safe functions,
599 * listed at `man 7 signal` or
600 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
601 */
602
603 if (preexec_fn != Py_None) {
604 /* We'll be calling back into Python later so we need to do this.
605 * This call may not be async-signal-safe but neither is calling
606 * back into Python. The user asked us to use hope as a strategy
607 * to avoid deadlock... */
608 PyOS_AfterFork();
609 }
610
611 child_exec(exec_array, argv, envp, cwd,
612 p2cread, p2cwrite, c2pread, c2pwrite,
613 errread, errwrite, errpipe_read, errpipe_write,
614 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800615 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000616 _exit(255);
617 return NULL; /* Dead code to avoid a potential compiler warning. */
618 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000619 Py_XDECREF(cwd_obj2);
620
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000621 if (pid == -1) {
622 /* Capture the errno exception before errno can be clobbered. */
623 PyErr_SetFromErrno(PyExc_OSError);
624 }
625 if (preexec_fn != Py_None &&
626 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
627 PyErr_SetString(PyExc_RuntimeError,
628 "not holding the import lock");
629 }
630
631 /* Parent process */
632 if (envp)
633 _Py_FreeCharPArray(envp);
634 if (argv)
635 _Py_FreeCharPArray(argv);
636 _Py_FreeCharPArray(exec_array);
637
638 /* Reenable gc in the parent process (or if fork failed). */
639 if (need_to_reenable_gc && _enable_gc(gc_module)) {
640 Py_XDECREF(gc_module);
641 return NULL;
642 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000643 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000644 Py_XDECREF(gc_module);
645
646 if (pid == -1)
647 return NULL; /* fork() failed. Exception set earlier. */
648
649 return PyLong_FromPid(pid);
650
651cleanup:
652 if (envp)
653 _Py_FreeCharPArray(envp);
654 if (argv)
655 _Py_FreeCharPArray(argv);
656 _Py_FreeCharPArray(exec_array);
657 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000658 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000659 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000660
661 /* Reenable gc if it was disabled. */
662 if (need_to_reenable_gc)
663 _enable_gc(gc_module);
664 Py_XDECREF(gc_module);
665 return NULL;
666}
667
668
669PyDoc_STRVAR(subprocess_fork_exec_doc,
670"fork_exec(args, executable_list, close_fds, cwd, env,\n\
671 p2cread, p2cwrite, c2pread, c2pwrite,\n\
672 errread, errwrite, errpipe_read, errpipe_write,\n\
673 restore_signals, call_setsid, preexec_fn)\n\
674\n\
675Forks a child process, closes parent file descriptors as appropriate in the\n\
676child and dups the few that are needed before calling exec() in the child\n\
677process.\n\
678\n\
679The preexec_fn, if supplied, will be called immediately before exec.\n\
680WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
681 It may trigger infrequent, difficult to debug deadlocks.\n\
682\n\
683If an error occurs in the child process before the exec, it is\n\
684serialized and written to the errpipe_write fd per subprocess.py.\n\
685\n\
686Returns: the child process's PID.\n\
687\n\
688Raises: Only on an error in the parent process.\n\
689");
690
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000691PyDoc_STRVAR(subprocess_cloexec_pipe_doc,
692"cloexec_pipe() -> (read_end, write_end)\n\n\
693Create a pipe whose ends have the cloexec flag set.");
694
695static PyObject *
696subprocess_cloexec_pipe(PyObject *self, PyObject *noargs)
697{
698 int fds[2];
699 int res;
700#ifdef HAVE_PIPE2
701 Py_BEGIN_ALLOW_THREADS
702 res = pipe2(fds, O_CLOEXEC);
703 Py_END_ALLOW_THREADS
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000704 if (res != 0 && errno == ENOSYS)
705 {
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000706 {
707#endif
708 /* We hold the GIL which offers some protection from other code calling
709 * fork() before the CLOEXEC flags have been set but we can't guarantee
710 * anything without pipe2(). */
711 long oldflags;
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000712
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000713 res = pipe(fds);
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000714
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000715 if (res == 0) {
716 oldflags = fcntl(fds[0], F_GETFD, 0);
717 if (oldflags < 0) res = oldflags;
718 }
719 if (res == 0)
720 res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
721
722 if (res == 0) {
723 oldflags = fcntl(fds[1], F_GETFD, 0);
724 if (oldflags < 0) res = oldflags;
725 }
726 if (res == 0)
727 res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
728#ifdef HAVE_PIPE2
729 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000730 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000731#endif
732 if (res != 0)
733 return PyErr_SetFromErrno(PyExc_OSError);
734 return Py_BuildValue("(ii)", fds[0], fds[1]);
735}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000736
737/* module level code ********************************************************/
738
739PyDoc_STRVAR(module_doc,
740"A POSIX helper for the subprocess module.");
741
742
743static PyMethodDef module_methods[] = {
744 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000745 {"cloexec_pipe", subprocess_cloexec_pipe, METH_NOARGS, subprocess_cloexec_pipe_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000746 {NULL, NULL} /* sentinel */
747};
748
749
750static struct PyModuleDef _posixsubprocessmodule = {
751 PyModuleDef_HEAD_INIT,
752 "_posixsubprocess",
753 module_doc,
754 -1, /* No memory is needed. */
755 module_methods,
756};
757
758PyMODINIT_FUNC
759PyInit__posixsubprocess(void)
760{
761#ifdef _SC_OPEN_MAX
762 max_fd = sysconf(_SC_OPEN_MAX);
763 if (max_fd == -1)
764#endif
765 max_fd = 256; /* Matches Lib/subprocess.py */
766
767 return PyModule_Create(&_posixsubprocessmodule);
768}