blob: 07d709688c30f9bc019c720c3c9add2caa133240 [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
Victor Stinner5572ba72011-05-26 14:10:08 +02003#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
4# define _GNU_SOURCE
Gregory P. Smith51ee2702010-12-13 07:59:39 +00005#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00006#include <unistd.h>
Gregory P. Smith51ee2702010-12-13 07:59:39 +00007#include <fcntl.h>
Gregory P. Smith8facece2012-01-21 14:01:08 -08008#ifdef HAVE_SYS_TYPES_H
9#include <sys/types.h>
10#endif
Gregory P. Smith4842efc2012-01-21 21:01:24 -080011#if defined(HAVE_SYS_STAT_H) && defined(__FreeBSD__)
12#include <sys/stat.h>
13#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080014#ifdef HAVE_SYS_SYSCALL_H
15#include <sys/syscall.h>
16#endif
17#ifdef HAVE_DIRENT_H
18#include <dirent.h>
19#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000020
Gregory P. Smithe3f78482012-01-21 15:16:17 -080021#if defined(sun)
22/* readdir64 is used to work around Solaris 9 bug 6395699. */
23# define readdir readdir64
24# define dirent dirent64
25# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080026/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080027# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080028# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080029# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080030#endif
31
Gregory P. Smith4842efc2012-01-21 21:01:24 -080032#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
33# define FD_DIR "/dev/fd"
34#else
35# define FD_DIR "/proc/self/fd"
36#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000037
38#define POSIX_CALL(call) if ((call) == -1) goto error
39
40
41/* Maximum file descriptor, initialized on module load. */
42static long max_fd;
43
44
45/* Given the gc module call gc.enable() and return 0 on success. */
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;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020050 _Py_IDENTIFIER(enable);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020051
52 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000053 if (result == NULL)
54 return 1;
55 Py_DECREF(result);
56 return 0;
57}
58
59
Gregory P. Smith8facece2012-01-21 14:01:08 -080060/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050061static int
62_pos_int_from_ascii(char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080063{
64 int num = 0;
65 while (*name >= '0' && *name <= '9') {
66 num = num * 10 + (*name - '0');
67 ++name;
68 }
69 if (*name)
70 return -1; /* Non digit found, not a number. */
71 return num;
72}
73
74
Gregory P. Smith4842efc2012-01-21 21:01:24 -080075#if defined(__FreeBSD__)
76/* When /dev/fd isn't mounted it is often a static directory populated
77 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
78 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
79 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
80 * that properly supports /dev/fd.
81 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050082static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020083_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080084{
85 struct stat dev_stat;
86 struct stat dev_fd_stat;
87 if (stat("/dev", &dev_stat) != 0)
88 return 0;
89 if (stat(FD_DIR, &dev_fd_stat) != 0)
90 return 0;
91 if (dev_stat.st_dev == dev_fd_stat.st_dev)
92 return 0; /* / == /dev == /dev/fd means it is static. #fail */
93 return 1;
94}
95#endif
96
97
Gregory P. Smith8facece2012-01-21 14:01:08 -080098/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050099static int
100_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800101{
102 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
103 long prev_fd = -1;
104 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
105 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
106 long iter_fd = PyLong_AsLong(py_fd);
107 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
108 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
109 return 1;
110 }
111 }
112 return 0;
113}
114
115
116/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500117static int
118_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800119{
120 /* Binary search. */
121 Py_ssize_t search_min = 0;
122 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
123 if (search_max < 0)
124 return 0;
125 do {
126 long middle = (search_min + search_max) / 2;
127 long middle_fd = PyLong_AsLong(
128 PySequence_Fast_GET_ITEM(fd_sequence, middle));
129 if (fd == middle_fd)
130 return 1;
131 if (fd > middle_fd)
132 search_min = middle + 1;
133 else
134 search_max = middle - 1;
135 } while (search_min <= search_max);
136 return 0;
137}
138
139
140/* Close all file descriptors in the range start_fd inclusive to
141 * end_fd exclusive except for those in py_fds_to_keep. If the
142 * range defined by [start_fd, end_fd) is large this will take a
143 * long time as it calls close() on EVERY possible fd.
144 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500145static void
146_close_fds_by_brute_force(int start_fd, int end_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800147{
148 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
149 Py_ssize_t keep_seq_idx;
150 int fd_num;
151 /* As py_fds_to_keep is sorted we can loop through the list closing
152 * fds inbetween any in the keep list falling within our range. */
153 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
154 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
155 keep_seq_idx);
156 int keep_fd = PyLong_AsLong(py_keep_fd);
157 if (keep_fd < start_fd)
158 continue;
159 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
160 while (close(fd_num) < 0 && errno == EINTR);
161 }
162 start_fd = keep_fd + 1;
163 }
164 if (start_fd <= end_fd) {
165 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
166 while (close(fd_num) < 0 && errno == EINTR);
167 }
168 }
169}
170
171
172#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
173/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
174 * only to read a directory of short file descriptor number names. The kernel
175 * will return an error if we didn't give it enough space. Highly Unlikely.
176 * This structure is very old and stable: It will not change unless the kernel
177 * chooses to break compatibility with all existing binaries. Highly Unlikely.
178 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800179struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700180 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800181 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800182 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800183 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800184 char d_name[256]; /* Filename (null-terminated) */
185};
186
187/* Close all open file descriptors in the range start_fd inclusive to end_fd
188 * exclusive. Do not close any in the sorted py_fds_to_keep list.
189 *
190 * This version is async signal safe as it does not make any unsafe C library
191 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
192 * to resort to making a kernel system call directly but this is the ONLY api
193 * available that does no harm. opendir/readdir/closedir perform memory
194 * allocation and locking so while they usually work they are not guaranteed
195 * to (especially if you have replaced your malloc implementation). A version
196 * of this function that uses those can be found in the _maybe_unsafe variant.
197 *
198 * This is Linux specific because that is all I am ready to test it on. It
199 * should be easy to add OS specific dirent or dirent64 structures and modify
200 * it with some cpp #define magic to work on other OSes as well if you want.
201 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500202static void
203_close_open_fd_range_safe(int start_fd, int end_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800204{
205 int fd_dir_fd;
206 if (start_fd >= end_fd)
207 return;
Ross Lagerwall5802fdf2012-03-18 15:55:10 +0200208#ifdef O_CLOEXEC
209 fd_dir_fd = open(FD_DIR, O_RDONLY | O_CLOEXEC, 0);
210#else
211 fd_dir_fd = open(FD_DIR, O_RDONLY, 0);
212#ifdef FD_CLOEXEC
213 {
214 int old = fcntl(fd_dir_fd, F_GETFD);
215 if (old != -1)
216 fcntl(fd_dir_fd, F_SETFD, old | FD_CLOEXEC);
217 }
218#endif
219#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800220 if (fd_dir_fd == -1) {
221 /* No way to get a list of open fds. */
222 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
223 return;
224 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800225 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800226 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800227 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
228 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800229 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800230 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800231 int offset;
232 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
233 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800234 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800235 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
236 continue; /* Not a number. */
237 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
238 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
239 while (close(fd) < 0 && errno == EINTR);
240 }
241 }
242 }
243 close(fd_dir_fd);
244 }
245}
246
247#define _close_open_fd_range _close_open_fd_range_safe
248
249#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
250
251
252/* Close all open file descriptors in the range start_fd inclusive to end_fd
253 * exclusive. Do not close any in the sorted py_fds_to_keep list.
254 *
255 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800256 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800257 * likely to ever cause a problem is opendir() as it performs an internal
258 * malloc(). Practically this should not be a problem. The Java VM makes the
259 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
260 *
261 * readdir_r() is not used because it provides no benefit. It is typically
262 * implemented as readdir() followed by memcpy(). See also:
263 * http://womble.decadent.org.uk/readdir_r-advisory.html
264 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500265static void
266_close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
267 PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800268{
269 DIR *proc_fd_dir;
270#ifndef HAVE_DIRFD
271 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
272 (start_fd < end_fd)) {
273 ++start_fd;
274 }
275 if (start_fd >= end_fd)
276 return;
277 /* Close our lowest fd before we call opendir so that it is likely to
278 * reuse that fd otherwise we might close opendir's file descriptor in
279 * our loop. This trick assumes that fd's are allocated on a lowest
280 * available basis. */
281 while (close(start_fd) < 0 && errno == EINTR);
282 ++start_fd;
283#endif
284 if (start_fd >= end_fd)
285 return;
286
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800287#if defined(__FreeBSD__)
288 if (!_is_fdescfs_mounted_on_dev_fd())
289 proc_fd_dir = NULL;
290 else
291#endif
292 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800293 if (!proc_fd_dir) {
294 /* No way to get a list of open fds. */
295 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
296 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800297 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800298#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800299 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800300#else
301 int fd_used_by_opendir = start_fd - 1;
302#endif
303 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800304 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800305 int fd;
306 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
307 continue; /* Not a number. */
308 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
309 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
310 while (close(fd) < 0 && errno == EINTR);
311 }
312 errno = 0;
313 }
314 if (errno) {
315 /* readdir error, revert behavior. Highly Unlikely. */
316 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
317 }
318 closedir(proc_fd_dir);
319 }
320}
321
322#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
323
324#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
325
326
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000327/*
328 * This function is code executed in the child process immediately after fork
329 * to set things up and call exec().
330 *
331 * All of the code in this function must only use async-signal-safe functions,
332 * listed at `man 7 signal` or
333 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
334 *
335 * This restriction is documented at
336 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
337 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500338static void
339child_exec(char *const exec_array[],
340 char *const argv[],
341 char *const envp[],
342 const char *cwd,
343 int p2cread, int p2cwrite,
344 int c2pread, int c2pwrite,
345 int errread, int errwrite,
346 int errpipe_read, int errpipe_write,
347 int close_fds, int restore_signals,
348 int call_setsid,
349 PyObject *py_fds_to_keep,
350 PyObject *preexec_fn,
351 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000352{
Gregory P. Smith5591b022012-10-10 03:34:47 -0700353 int i, saved_errno, unused, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000354 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000355 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000356 /* Buffer large enough to hold a hex integer. We can't malloc. */
357 char hex_errno[sizeof(saved_errno)*2+1];
358
359 /* Close parent's pipe ends. */
360 if (p2cwrite != -1) {
361 POSIX_CALL(close(p2cwrite));
362 }
363 if (c2pread != -1) {
364 POSIX_CALL(close(c2pread));
365 }
366 if (errread != -1) {
367 POSIX_CALL(close(errread));
368 }
369 POSIX_CALL(close(errpipe_read));
370
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200371 /* When duping fds, if there arises a situation where one of the fds is
372 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
373 if (c2pwrite == 0)
374 POSIX_CALL(c2pwrite = dup(c2pwrite));
375 if (errwrite == 0 || errwrite == 1)
376 POSIX_CALL(errwrite = dup(errwrite));
377
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000378 /* Dup fds for child.
379 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
380 would be a no-op (issue #10806). */
381 if (p2cread == 0) {
382 int old = fcntl(p2cread, F_GETFD);
383 if (old != -1)
384 fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
385 } else if (p2cread != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000386 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
387 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000388 if (c2pwrite == 1) {
389 int old = fcntl(c2pwrite, F_GETFD);
390 if (old != -1)
391 fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
392 } else if (c2pwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000393 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
394 }
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000395 if (errwrite == 2) {
396 int old = fcntl(errwrite, F_GETFD);
397 if (old != -1)
398 fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
399 } else if (errwrite != -1) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000400 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
401 }
402
403 /* Close pipe fds. Make sure we don't close the same fd more than */
404 /* once, or standard fds. */
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000405 if (p2cread > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000406 POSIX_CALL(close(p2cread));
407 }
Gregory P. Smith81218982011-03-15 14:56:39 -0400408 if (c2pwrite > 2 && c2pwrite != p2cread) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000409 POSIX_CALL(close(c2pwrite));
410 }
Gregory P. Smith81218982011-03-15 14:56:39 -0400411 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000412 POSIX_CALL(close(errwrite));
413 }
414
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800415 if (close_fds) {
416 int local_max_fd = max_fd;
417#if defined(__NetBSD__)
418 local_max_fd = fcntl(0, F_MAXFD);
419 if (local_max_fd < 0)
420 local_max_fd = max_fd;
421#endif
422 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
423 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
424 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000425
426 if (cwd)
427 POSIX_CALL(chdir(cwd));
428
429 if (restore_signals)
430 _Py_RestoreSignals();
431
432#ifdef HAVE_SETSID
433 if (call_setsid)
434 POSIX_CALL(setsid());
435#endif
436
Gregory P. Smith5591b022012-10-10 03:34:47 -0700437 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000438 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
439 /* This is where the user has asked us to deadlock their program. */
440 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
441 if (result == NULL) {
442 /* Stringifying the exception or traceback would involve
443 * memory allocation and thus potential for deadlock.
444 * We've already faced potential deadlock by calling back
445 * into Python in the first place, so it probably doesn't
446 * matter but we avoid it to minimize the possibility. */
447 err_msg = "Exception occurred in preexec_fn.";
448 errno = 0; /* We don't want to report an OSError. */
449 goto error;
450 }
451 /* Py_DECREF(result); - We're about to exec so why bother? */
452 }
453
454 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
455 /* given the executable_list generated by Lib/subprocess.py. */
456 saved_errno = 0;
457 for (i = 0; exec_array[i] != NULL; ++i) {
458 const char *executable = exec_array[i];
459 if (envp) {
460 execve(executable, argv, envp);
461 } else {
462 execv(executable, argv);
463 }
464 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
465 saved_errno = errno;
466 }
467 }
468 /* Report the first exec error, not the last. */
469 if (saved_errno)
470 errno = saved_errno;
471
472error:
473 saved_errno = errno;
474 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800475 /* We ignore all write() return values as the total size of our writes is
476 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000477 if (saved_errno) {
478 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800479 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000480 cur = hex_errno + sizeof(hex_errno);
481 while (saved_errno != 0 && cur > hex_errno) {
482 *--cur = "0123456789ABCDEF"[saved_errno % 16];
483 saved_errno /= 16;
484 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800485 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
486 unused = write(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700487 if (!reached_preexec) {
488 /* Indicate to the parent that the error happened before exec(). */
489 unused = write(errpipe_write, "noexec", 6);
490 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000491 /* We can't call strerror(saved_errno). It is not async signal safe.
492 * The parent process will look the error message up. */
493 } else {
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800494 unused = write(errpipe_write, "RuntimeError:0:", 15);
495 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000496 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800497 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000498}
499
500
501static PyObject *
502subprocess_fork_exec(PyObject* self, PyObject *args)
503{
504 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200505 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000506 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000507 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000508 PyObject *preexec_fn_args_tuple = NULL;
509 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
510 int errpipe_read, errpipe_write, close_fds, restore_signals;
511 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000512 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000513 const char *cwd;
514 pid_t pid;
515 int need_to_reenable_gc = 0;
516 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800517 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000518
519 if (!PyArg_ParseTuple(
Antoine Pitrou721738f2012-08-15 23:20:39 +0200520 args, "OOpOOOiiiiiiiiiiO:fork_exec",
521 &process_args, &executable_list, &close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000522 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000523 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
524 &errread, &errwrite, &errpipe_read, &errpipe_write,
525 &restore_signals, &call_setsid, &preexec_fn))
526 return NULL;
527
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000528 if (close_fds && errpipe_write < 3) { /* precondition */
529 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
530 return NULL;
531 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800532 if (PySequence_Length(py_fds_to_keep) < 0) {
533 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
534 return NULL;
535 }
536 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
537 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000538 return NULL;
539 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000540
541 /* We need to call gc.disable() when we'll be calling preexec_fn */
542 if (preexec_fn != Py_None) {
543 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200544 _Py_IDENTIFIER(isenabled);
545 _Py_IDENTIFIER(disable);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200546
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000547 gc_module = PyImport_ImportModule("gc");
548 if (gc_module == NULL)
549 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200550 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000551 if (result == NULL) {
552 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000553 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000554 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000555 need_to_reenable_gc = PyObject_IsTrue(result);
556 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000557 if (need_to_reenable_gc == -1) {
558 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000559 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000560 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200561 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000562 if (result == NULL) {
563 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000564 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000565 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000566 Py_DECREF(result);
567 }
568
569 exec_array = _PySequence_BytesToCharpArray(executable_list);
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200570 if (!exec_array) {
571 Py_XDECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000572 return NULL;
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200573 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000574
575 /* Convert args and env into appropriate arguments for exec() */
576 /* These conversions are done in the parent process to avoid allocating
577 or freeing memory in the child process. */
578 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000579 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000580 /* Equivalent to: */
581 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000582 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200583 if (fast_args == NULL)
584 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000585 num_args = PySequence_Fast_GET_SIZE(fast_args);
586 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000587 if (converted_args == NULL)
588 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000589 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000590 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000591 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000592 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
593 goto cleanup;
594 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
595 }
596
597 argv = _PySequence_BytesToCharpArray(converted_args);
598 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000599 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000600 if (!argv)
601 goto cleanup;
602 }
603
604 if (env_list != Py_None) {
605 envp = _PySequence_BytesToCharpArray(env_list);
606 if (!envp)
607 goto cleanup;
608 }
609
610 if (preexec_fn != Py_None) {
611 preexec_fn_args_tuple = PyTuple_New(0);
612 if (!preexec_fn_args_tuple)
613 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000614 _PyImport_AcquireLock();
615 }
616
617 if (cwd_obj != Py_None) {
618 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
619 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000620 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000621 } else {
622 cwd = NULL;
623 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000624 }
625
626 pid = fork();
627 if (pid == 0) {
628 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000629 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000630 * Code from here to _exit() must only use async-signal-safe functions,
631 * listed at `man 7 signal` or
632 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
633 */
634
635 if (preexec_fn != Py_None) {
636 /* We'll be calling back into Python later so we need to do this.
637 * This call may not be async-signal-safe but neither is calling
638 * back into Python. The user asked us to use hope as a strategy
639 * to avoid deadlock... */
640 PyOS_AfterFork();
641 }
642
643 child_exec(exec_array, argv, envp, cwd,
644 p2cread, p2cwrite, c2pread, c2pwrite,
645 errread, errwrite, errpipe_read, errpipe_write,
646 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800647 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000648 _exit(255);
649 return NULL; /* Dead code to avoid a potential compiler warning. */
650 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000651 Py_XDECREF(cwd_obj2);
652
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000653 if (pid == -1) {
654 /* Capture the errno exception before errno can be clobbered. */
655 PyErr_SetFromErrno(PyExc_OSError);
656 }
657 if (preexec_fn != Py_None &&
658 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
659 PyErr_SetString(PyExc_RuntimeError,
660 "not holding the import lock");
661 }
662
663 /* Parent process */
664 if (envp)
665 _Py_FreeCharPArray(envp);
666 if (argv)
667 _Py_FreeCharPArray(argv);
668 _Py_FreeCharPArray(exec_array);
669
670 /* Reenable gc in the parent process (or if fork failed). */
671 if (need_to_reenable_gc && _enable_gc(gc_module)) {
672 Py_XDECREF(gc_module);
673 return NULL;
674 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000675 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000676 Py_XDECREF(gc_module);
677
678 if (pid == -1)
679 return NULL; /* fork() failed. Exception set earlier. */
680
681 return PyLong_FromPid(pid);
682
683cleanup:
684 if (envp)
685 _Py_FreeCharPArray(envp);
686 if (argv)
687 _Py_FreeCharPArray(argv);
688 _Py_FreeCharPArray(exec_array);
689 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000690 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000691 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000692
693 /* Reenable gc if it was disabled. */
694 if (need_to_reenable_gc)
695 _enable_gc(gc_module);
696 Py_XDECREF(gc_module);
697 return NULL;
698}
699
700
701PyDoc_STRVAR(subprocess_fork_exec_doc,
702"fork_exec(args, executable_list, close_fds, cwd, env,\n\
703 p2cread, p2cwrite, c2pread, c2pwrite,\n\
704 errread, errwrite, errpipe_read, errpipe_write,\n\
705 restore_signals, call_setsid, preexec_fn)\n\
706\n\
707Forks a child process, closes parent file descriptors as appropriate in the\n\
708child and dups the few that are needed before calling exec() in the child\n\
709process.\n\
710\n\
711The preexec_fn, if supplied, will be called immediately before exec.\n\
712WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
713 It may trigger infrequent, difficult to debug deadlocks.\n\
714\n\
715If an error occurs in the child process before the exec, it is\n\
716serialized and written to the errpipe_write fd per subprocess.py.\n\
717\n\
718Returns: the child process's PID.\n\
719\n\
720Raises: Only on an error in the parent process.\n\
721");
722
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000723PyDoc_STRVAR(subprocess_cloexec_pipe_doc,
724"cloexec_pipe() -> (read_end, write_end)\n\n\
725Create a pipe whose ends have the cloexec flag set.");
726
727static PyObject *
728subprocess_cloexec_pipe(PyObject *self, PyObject *noargs)
729{
730 int fds[2];
731 int res;
732#ifdef HAVE_PIPE2
733 Py_BEGIN_ALLOW_THREADS
734 res = pipe2(fds, O_CLOEXEC);
735 Py_END_ALLOW_THREADS
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000736 if (res != 0 && errno == ENOSYS)
737 {
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000738 {
739#endif
740 /* We hold the GIL which offers some protection from other code calling
741 * fork() before the CLOEXEC flags have been set but we can't guarantee
742 * anything without pipe2(). */
743 long oldflags;
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000744
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000745 res = pipe(fds);
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000746
Gregory P. Smithabcfcba2011-01-02 20:52:48 +0000747 if (res == 0) {
748 oldflags = fcntl(fds[0], F_GETFD, 0);
749 if (oldflags < 0) res = oldflags;
750 }
751 if (res == 0)
752 res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
753
754 if (res == 0) {
755 oldflags = fcntl(fds[1], F_GETFD, 0);
756 if (oldflags < 0) res = oldflags;
757 }
758 if (res == 0)
759 res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
760#ifdef HAVE_PIPE2
761 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000762 }
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000763#endif
764 if (res != 0)
765 return PyErr_SetFromErrno(PyExc_OSError);
766 return Py_BuildValue("(ii)", fds[0], fds[1]);
767}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000768
769/* module level code ********************************************************/
770
771PyDoc_STRVAR(module_doc,
772"A POSIX helper for the subprocess module.");
773
774
775static PyMethodDef module_methods[] = {
776 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smith51ee2702010-12-13 07:59:39 +0000777 {"cloexec_pipe", subprocess_cloexec_pipe, METH_NOARGS, subprocess_cloexec_pipe_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000778 {NULL, NULL} /* sentinel */
779};
780
781
782static struct PyModuleDef _posixsubprocessmodule = {
783 PyModuleDef_HEAD_INIT,
784 "_posixsubprocess",
785 module_doc,
786 -1, /* No memory is needed. */
787 module_methods,
788};
789
790PyMODINIT_FUNC
791PyInit__posixsubprocess(void)
792{
793#ifdef _SC_OPEN_MAX
794 max_fd = sysconf(_SC_OPEN_MAX);
795 if (max_fd == -1)
796#endif
797 max_fd = 256; /* Matches Lib/subprocess.py */
798
799 return PyModule_Create(&_posixsubprocessmodule);
800}