blob: e434d2c78a25496c30e940adb25a22e513a35f9b [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
11#ifdef HAVE_SYS_SYSCALL_H
12#include <sys/syscall.h>
13#endif
14#ifdef HAVE_DIRENT_H
15#include <dirent.h>
16#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000017
Gregory P. Smithe3f78482012-01-21 15:16:17 -080018#if defined(sun)
19/* readdir64 is used to work around Solaris 9 bug 6395699. */
20# define readdir readdir64
21# define dirent dirent64
22# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080023/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080024# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080025# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080026# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080027#endif
28
29#define LINUX_SOLARIS_FD_DIR "/proc/self/fd"
30#define BSD_OSX_FD_DIR "/dev/fd"
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000031
32#define POSIX_CALL(call) if ((call) == -1) goto error
33
34
35/* Maximum file descriptor, initialized on module load. */
36static long max_fd;
37
38
39/* Given the gc module call gc.enable() and return 0 on success. */
40static int _enable_gc(PyObject *gc_module)
41{
42 PyObject *result;
43 result = PyObject_CallMethod(gc_module, "enable", NULL);
44 if (result == NULL)
45 return 1;
46 Py_DECREF(result);
47 return 0;
48}
49
50
Gregory P. Smith8facece2012-01-21 14:01:08 -080051/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
52static int _pos_int_from_ascii(char *name)
53{
54 int num = 0;
55 while (*name >= '0' && *name <= '9') {
56 num = num * 10 + (*name - '0');
57 ++name;
58 }
59 if (*name)
60 return -1; /* Non digit found, not a number. */
61 return num;
62}
63
64
65/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
66static int _sanity_check_python_fd_sequence(PyObject *fd_sequence)
67{
68 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
69 long prev_fd = -1;
70 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
71 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
72 long iter_fd = PyLong_AsLong(py_fd);
73 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
74 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
75 return 1;
76 }
77 }
78 return 0;
79}
80
81
82/* Is fd found in the sorted Python Sequence? */
83static int _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
84{
85 /* Binary search. */
86 Py_ssize_t search_min = 0;
87 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
88 if (search_max < 0)
89 return 0;
90 do {
91 long middle = (search_min + search_max) / 2;
92 long middle_fd = PyLong_AsLong(
93 PySequence_Fast_GET_ITEM(fd_sequence, middle));
94 if (fd == middle_fd)
95 return 1;
96 if (fd > middle_fd)
97 search_min = middle + 1;
98 else
99 search_max = middle - 1;
100 } while (search_min <= search_max);
101 return 0;
102}
103
104
105/* Close all file descriptors in the range start_fd inclusive to
106 * end_fd exclusive except for those in py_fds_to_keep. If the
107 * range defined by [start_fd, end_fd) is large this will take a
108 * long time as it calls close() on EVERY possible fd.
109 */
110static void _close_fds_by_brute_force(int start_fd, int end_fd,
111 PyObject *py_fds_to_keep)
112{
113 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
114 Py_ssize_t keep_seq_idx;
115 int fd_num;
116 /* As py_fds_to_keep is sorted we can loop through the list closing
117 * fds inbetween any in the keep list falling within our range. */
118 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
119 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
120 keep_seq_idx);
121 int keep_fd = PyLong_AsLong(py_keep_fd);
122 if (keep_fd < start_fd)
123 continue;
124 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
125 while (close(fd_num) < 0 && errno == EINTR);
126 }
127 start_fd = keep_fd + 1;
128 }
129 if (start_fd <= end_fd) {
130 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
131 while (close(fd_num) < 0 && errno == EINTR);
132 }
133 }
134}
135
136
137#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
138/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
139 * only to read a directory of short file descriptor number names. The kernel
140 * will return an error if we didn't give it enough space. Highly Unlikely.
141 * This structure is very old and stable: It will not change unless the kernel
142 * chooses to break compatibility with all existing binaries. Highly Unlikely.
143 */
144struct linux_dirent {
145 unsigned long d_ino; /* Inode number */
146 unsigned long d_off; /* Offset to next linux_dirent */
147 unsigned short d_reclen; /* Length of this linux_dirent */
148 char d_name[256]; /* Filename (null-terminated) */
149};
150
151/* Close all open file descriptors in the range start_fd inclusive to end_fd
152 * exclusive. Do not close any in the sorted py_fds_to_keep list.
153 *
154 * This version is async signal safe as it does not make any unsafe C library
155 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
156 * to resort to making a kernel system call directly but this is the ONLY api
157 * available that does no harm. opendir/readdir/closedir perform memory
158 * allocation and locking so while they usually work they are not guaranteed
159 * to (especially if you have replaced your malloc implementation). A version
160 * of this function that uses those can be found in the _maybe_unsafe variant.
161 *
162 * This is Linux specific because that is all I am ready to test it on. It
163 * should be easy to add OS specific dirent or dirent64 structures and modify
164 * it with some cpp #define magic to work on other OSes as well if you want.
165 */
166static void _close_open_fd_range_safe(int start_fd, int end_fd,
167 PyObject* py_fds_to_keep)
168{
169 int fd_dir_fd;
170 if (start_fd >= end_fd)
171 return;
172 fd_dir_fd = open(LINUX_SOLARIS_FD_DIR, O_RDONLY | O_CLOEXEC, 0);
173 /* Not trying to open the BSD_OSX path as this is currently Linux only. */
174 if (fd_dir_fd == -1) {
175 /* No way to get a list of open fds. */
176 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
177 return;
178 } else {
179 char buffer[sizeof(struct linux_dirent)];
180 int bytes;
181 while ((bytes = syscall(SYS_getdents, fd_dir_fd,
182 (struct linux_dirent *)buffer,
183 sizeof(buffer))) > 0) {
184 struct linux_dirent *entry;
185 int offset;
186 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
187 int fd;
188 entry = (struct linux_dirent *)(buffer + offset);
189 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
190 continue; /* Not a number. */
191 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
192 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
193 while (close(fd) < 0 && errno == EINTR);
194 }
195 }
196 }
197 close(fd_dir_fd);
198 }
199}
200
201#define _close_open_fd_range _close_open_fd_range_safe
202
203#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
204
205
206/* Close all open file descriptors in the range start_fd inclusive to end_fd
207 * exclusive. Do not close any in the sorted py_fds_to_keep list.
208 *
209 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800210 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800211 * likely to ever cause a problem is opendir() as it performs an internal
212 * malloc(). Practically this should not be a problem. The Java VM makes the
213 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
214 *
215 * readdir_r() is not used because it provides no benefit. It is typically
216 * implemented as readdir() followed by memcpy(). See also:
217 * http://womble.decadent.org.uk/readdir_r-advisory.html
218 */
219static void _close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
220 PyObject* py_fds_to_keep)
221{
222 DIR *proc_fd_dir;
223#ifndef HAVE_DIRFD
224 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
225 (start_fd < end_fd)) {
226 ++start_fd;
227 }
228 if (start_fd >= end_fd)
229 return;
230 /* Close our lowest fd before we call opendir so that it is likely to
231 * reuse that fd otherwise we might close opendir's file descriptor in
232 * our loop. This trick assumes that fd's are allocated on a lowest
233 * available basis. */
234 while (close(start_fd) < 0 && errno == EINTR);
235 ++start_fd;
236#endif
237 if (start_fd >= end_fd)
238 return;
239
240 proc_fd_dir = opendir(BSD_OSX_FD_DIR);
241 if (!proc_fd_dir)
242 proc_fd_dir = opendir(LINUX_SOLARIS_FD_DIR);
243 if (!proc_fd_dir) {
244 /* No way to get a list of open fds. */
245 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
246 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800247 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800248#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800249 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800250#else
251 int fd_used_by_opendir = start_fd - 1;
252#endif
253 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800254 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800255 int fd;
256 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
257 continue; /* Not a number. */
258 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
259 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
260 while (close(fd) < 0 && errno == EINTR);
261 }
262 errno = 0;
263 }
264 if (errno) {
265 /* readdir error, revert behavior. Highly Unlikely. */
266 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
267 }
268 closedir(proc_fd_dir);
269 }
270}
271
272#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
273
274#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
275
276
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000277/*
278 * This function is code executed in the child process immediately after fork
279 * to set things up and call exec().
280 *
281 * All of the code in this function must only use async-signal-safe functions,
282 * listed at `man 7 signal` or
283 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
284 *
285 * This restriction is documented at
286 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
287 */
288static void child_exec(char *const exec_array[],
289 char *const argv[],
290 char *const envp[],
291 const char *cwd,
292 int p2cread, int p2cwrite,
293 int c2pread, int c2pwrite,
294 int errread, int errwrite,
295 int errpipe_read, int errpipe_write,
296 int close_fds, int restore_signals,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800297 int call_setsid,
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000298 PyObject *py_fds_to_keep,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000299 PyObject *preexec_fn,
300 PyObject *preexec_fn_args_tuple)
301{
Gregory P. Smith8facece2012-01-21 14:01:08 -0800302 int i, saved_errno, unused;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000303 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000304 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000305 /* Buffer large enough to hold a hex integer. We can't malloc. */
306 char hex_errno[sizeof(saved_errno)*2+1];
307
308 /* Close parent's pipe ends. */
309 if (p2cwrite != -1) {
310 POSIX_CALL(close(p2cwrite));
311 }
312 if (c2pread != -1) {
313 POSIX_CALL(close(c2pread));
314 }
315 if (errread != -1) {
316 POSIX_CALL(close(errread));
317 }
318 POSIX_CALL(close(errpipe_read));
319
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200320 /* When duping fds, if there arises a situation where one of the fds is
321 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
322 if (c2pwrite == 0)
323 POSIX_CALL(c2pwrite = dup(c2pwrite));
324 if (errwrite == 0 || errwrite == 1)
325 POSIX_CALL(errwrite = dup(errwrite));
326
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000327 /* Dup fds for child.
328 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
329 would be a no-op (issue #10806). */
330 if (p2cread == 0) {
331 int old = fcntl(p2cread, F_GETFD);
332 if (old != -1)
333 fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
334 } else if (p2cread != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000335 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
336 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000337 if (c2pwrite == 1) {
338 int old = fcntl(c2pwrite, F_GETFD);
339 if (old != -1)
340 fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
341 } else if (c2pwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000342 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
343 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000344 if (errwrite == 2) {
345 int old = fcntl(errwrite, F_GETFD);
346 if (old != -1)
347 fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
348 } else if (errwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000349 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
350 }
351
352 /* Close pipe fds. Make sure we don't close the same fd more than */
353 /* once, or standard fds. */
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000354 if (p2cread > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000355 POSIX_CALL(close(p2cread));
356 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400357 if (c2pwrite > 2 && c2pwrite != p2cread) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000358 POSIX_CALL(close(c2pwrite));
359 }
Gregory P. Smith9c4f44f2011-03-15 14:56:39 -0400360 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000361 POSIX_CALL(close(errwrite));
362 }
363
Gregory P. Smith8facece2012-01-21 14:01:08 -0800364 if (close_fds)
365 _close_open_fd_range(3, max_fd, py_fds_to_keep);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000366
367 if (cwd)
368 POSIX_CALL(chdir(cwd));
369
370 if (restore_signals)
371 _Py_RestoreSignals();
372
373#ifdef HAVE_SETSID
374 if (call_setsid)
375 POSIX_CALL(setsid());
376#endif
377
378 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
379 /* This is where the user has asked us to deadlock their program. */
380 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
381 if (result == NULL) {
382 /* Stringifying the exception or traceback would involve
383 * memory allocation and thus potential for deadlock.
384 * We've already faced potential deadlock by calling back
385 * into Python in the first place, so it probably doesn't
386 * matter but we avoid it to minimize the possibility. */
387 err_msg = "Exception occurred in preexec_fn.";
388 errno = 0; /* We don't want to report an OSError. */
389 goto error;
390 }
391 /* Py_DECREF(result); - We're about to exec so why bother? */
392 }
393
394 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
395 /* given the executable_list generated by Lib/subprocess.py. */
396 saved_errno = 0;
397 for (i = 0; exec_array[i] != NULL; ++i) {
398 const char *executable = exec_array[i];
399 if (envp) {
400 execve(executable, argv, envp);
401 } else {
402 execv(executable, argv);
403 }
404 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
405 saved_errno = errno;
406 }
407 }
408 /* Report the first exec error, not the last. */
409 if (saved_errno)
410 errno = saved_errno;
411
412error:
413 saved_errno = errno;
414 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800415 /* We ignore all write() return values as the total size of our writes is
416 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000417 if (saved_errno) {
418 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800419 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000420 cur = hex_errno + sizeof(hex_errno);
421 while (saved_errno != 0 && cur > hex_errno) {
422 *--cur = "0123456789ABCDEF"[saved_errno % 16];
423 saved_errno /= 16;
424 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800425 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
426 unused = write(errpipe_write, ":", 1);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000427 /* We can't call strerror(saved_errno). It is not async signal safe.
428 * The parent process will look the error message up. */
429 } else {
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800430 unused = write(errpipe_write, "RuntimeError:0:", 15);
431 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000432 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800433 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000434}
435
436
437static PyObject *
438subprocess_fork_exec(PyObject* self, PyObject *args)
439{
440 PyObject *gc_module = NULL;
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000441 PyObject *executable_list, *py_close_fds, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000442 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000443 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000444 PyObject *preexec_fn_args_tuple = NULL;
445 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
446 int errpipe_read, errpipe_write, close_fds, restore_signals;
447 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000448 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000449 const char *cwd;
450 pid_t pid;
451 int need_to_reenable_gc = 0;
452 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800453 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000454
455 if (!PyArg_ParseTuple(
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000456 args, "OOOOOOiiiiiiiiiiO:fork_exec",
457 &process_args, &executable_list, &py_close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000458 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000459 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
460 &errread, &errwrite, &errpipe_read, &errpipe_write,
461 &restore_signals, &call_setsid, &preexec_fn))
462 return NULL;
463
464 close_fds = PyObject_IsTrue(py_close_fds);
465 if (close_fds && errpipe_write < 3) { /* precondition */
466 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
467 return NULL;
468 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800469 if (PySequence_Length(py_fds_to_keep) < 0) {
470 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
471 return NULL;
472 }
473 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
474 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000475 return NULL;
476 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000477
478 /* We need to call gc.disable() when we'll be calling preexec_fn */
479 if (preexec_fn != Py_None) {
480 PyObject *result;
481 gc_module = PyImport_ImportModule("gc");
482 if (gc_module == NULL)
483 return NULL;
484 result = PyObject_CallMethod(gc_module, "isenabled", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000485 if (result == NULL) {
486 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000487 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000488 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000489 need_to_reenable_gc = PyObject_IsTrue(result);
490 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000491 if (need_to_reenable_gc == -1) {
492 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000493 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000494 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000495 result = PyObject_CallMethod(gc_module, "disable", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000496 if (result == NULL) {
497 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000498 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000499 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000500 Py_DECREF(result);
501 }
502
503 exec_array = _PySequence_BytesToCharpArray(executable_list);
504 if (!exec_array)
505 return NULL;
506
507 /* Convert args and env into appropriate arguments for exec() */
508 /* These conversions are done in the parent process to avoid allocating
509 or freeing memory in the child process. */
510 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000511 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000512 /* Equivalent to: */
513 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000514 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
515 num_args = PySequence_Fast_GET_SIZE(fast_args);
516 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000517 if (converted_args == NULL)
518 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000519 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000520 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000521 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000522 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
523 goto cleanup;
524 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
525 }
526
527 argv = _PySequence_BytesToCharpArray(converted_args);
528 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000529 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000530 if (!argv)
531 goto cleanup;
532 }
533
534 if (env_list != Py_None) {
535 envp = _PySequence_BytesToCharpArray(env_list);
536 if (!envp)
537 goto cleanup;
538 }
539
540 if (preexec_fn != Py_None) {
541 preexec_fn_args_tuple = PyTuple_New(0);
542 if (!preexec_fn_args_tuple)
543 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000544 _PyImport_AcquireLock();
545 }
546
547 if (cwd_obj != Py_None) {
548 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
549 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000550 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000551 } else {
552 cwd = NULL;
553 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000554 }
555
556 pid = fork();
557 if (pid == 0) {
558 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000559 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000560 * Code from here to _exit() must only use async-signal-safe functions,
561 * listed at `man 7 signal` or
562 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
563 */
564
565 if (preexec_fn != Py_None) {
566 /* We'll be calling back into Python later so we need to do this.
567 * This call may not be async-signal-safe but neither is calling
568 * back into Python. The user asked us to use hope as a strategy
569 * to avoid deadlock... */
570 PyOS_AfterFork();
571 }
572
573 child_exec(exec_array, argv, envp, cwd,
574 p2cread, p2cwrite, c2pread, c2pwrite,
575 errread, errwrite, errpipe_read, errpipe_write,
576 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800577 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000578 _exit(255);
579 return NULL; /* Dead code to avoid a potential compiler warning. */
580 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000581 Py_XDECREF(cwd_obj2);
582
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000583 if (pid == -1) {
584 /* Capture the errno exception before errno can be clobbered. */
585 PyErr_SetFromErrno(PyExc_OSError);
586 }
587 if (preexec_fn != Py_None &&
588 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
589 PyErr_SetString(PyExc_RuntimeError,
590 "not holding the import lock");
591 }
592
593 /* Parent process */
594 if (envp)
595 _Py_FreeCharPArray(envp);
596 if (argv)
597 _Py_FreeCharPArray(argv);
598 _Py_FreeCharPArray(exec_array);
599
600 /* Reenable gc in the parent process (or if fork failed). */
601 if (need_to_reenable_gc && _enable_gc(gc_module)) {
602 Py_XDECREF(gc_module);
603 return NULL;
604 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000605 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000606 Py_XDECREF(gc_module);
607
608 if (pid == -1)
609 return NULL; /* fork() failed. Exception set earlier. */
610
611 return PyLong_FromPid(pid);
612
613cleanup:
614 if (envp)
615 _Py_FreeCharPArray(envp);
616 if (argv)
617 _Py_FreeCharPArray(argv);
618 _Py_FreeCharPArray(exec_array);
619 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000620 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000621 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000622
623 /* Reenable gc if it was disabled. */
624 if (need_to_reenable_gc)
625 _enable_gc(gc_module);
626 Py_XDECREF(gc_module);
627 return NULL;
628}
629
630
631PyDoc_STRVAR(subprocess_fork_exec_doc,
632"fork_exec(args, executable_list, close_fds, cwd, env,\n\
633 p2cread, p2cwrite, c2pread, c2pwrite,\n\
634 errread, errwrite, errpipe_read, errpipe_write,\n\
635 restore_signals, call_setsid, preexec_fn)\n\
636\n\
637Forks a child process, closes parent file descriptors as appropriate in the\n\
638child and dups the few that are needed before calling exec() in the child\n\
639process.\n\
640\n\
641The preexec_fn, if supplied, will be called immediately before exec.\n\
642WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
643 It may trigger infrequent, difficult to debug deadlocks.\n\
644\n\
645If an error occurs in the child process before the exec, it is\n\
646serialized and written to the errpipe_write fd per subprocess.py.\n\
647\n\
648Returns: the child process's PID.\n\
649\n\
650Raises: Only on an error in the parent process.\n\
651");
652
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000653PyDoc_STRVAR(subprocess_cloexec_pipe_doc,
654"cloexec_pipe() -> (read_end, write_end)\n\n\
655Create a pipe whose ends have the cloexec flag set.");
656
657static PyObject *
658subprocess_cloexec_pipe(PyObject *self, PyObject *noargs)
659{
660 int fds[2];
661 int res;
662#ifdef HAVE_PIPE2
663 Py_BEGIN_ALLOW_THREADS
664 res = pipe2(fds, O_CLOEXEC);
665 Py_END_ALLOW_THREADS
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000666 if (res != 0 && errno == ENOSYS)
667 {
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000668 {
669#endif
670 /* We hold the GIL which offers some protection from other code calling
671 * fork() before the CLOEXEC flags have been set but we can't guarantee
672 * anything without pipe2(). */
673 long oldflags;
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000674
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000675 res = pipe(fds);
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000676
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000677 if (res == 0) {
678 oldflags = fcntl(fds[0], F_GETFD, 0);
679 if (oldflags < 0) res = oldflags;
680 }
681 if (res == 0)
682 res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
683
684 if (res == 0) {
685 oldflags = fcntl(fds[1], F_GETFD, 0);
686 if (oldflags < 0) res = oldflags;
687 }
688 if (res == 0)
689 res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
690#ifdef HAVE_PIPE2
691 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000692 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000693#endif
694 if (res != 0)
695 return PyErr_SetFromErrno(PyExc_OSError);
696 return Py_BuildValue("(ii)", fds[0], fds[1]);
697}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000698
699/* module level code ********************************************************/
700
701PyDoc_STRVAR(module_doc,
702"A POSIX helper for the subprocess module.");
703
704
705static PyMethodDef module_methods[] = {
706 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000707 {"cloexec_pipe", subprocess_cloexec_pipe, METH_NOARGS, subprocess_cloexec_pipe_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000708 {NULL, NULL} /* sentinel */
709};
710
711
712static struct PyModuleDef _posixsubprocessmodule = {
713 PyModuleDef_HEAD_INIT,
714 "_posixsubprocess",
715 module_doc,
716 -1, /* No memory is needed. */
717 module_methods,
718};
719
720PyMODINIT_FUNC
721PyInit__posixsubprocess(void)
722{
723#ifdef _SC_OPEN_MAX
724 max_fd = sysconf(_SC_OPEN_MAX);
725 if (max_fd == -1)
726#endif
727 max_fd = 256; /* Matches Lib/subprocess.py */
728
729 return PyModule_Create(&_posixsubprocessmodule);
730}