blob: 648a569ca7ffe3ba83c8d244206f0ca9e65f1d09 [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. Smithefeb9da2014-04-14 13:31:21 -070021#if defined(__ANDROID__) && !defined(SYS_getdents64)
22/* Android doesn't expose syscalls, add the definition manually. */
23# include <sys/linux-syscalls.h>
24# define SYS_getdents64 __NR_getdents64
25#endif
26
Gregory P. Smithe3f78482012-01-21 15:16:17 -080027#if defined(sun)
28/* readdir64 is used to work around Solaris 9 bug 6395699. */
29# define readdir readdir64
30# define dirent dirent64
31# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080032/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080033# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080034# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080035# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080036#endif
37
Gregory P. Smith4842efc2012-01-21 21:01:24 -080038#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
39# define FD_DIR "/dev/fd"
40#else
41# define FD_DIR "/proc/self/fd"
42#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000043
Victor Stinnerdaf45552013-08-28 00:53:59 +020044#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000045
46
47/* Maximum file descriptor, initialized on module load. */
48static long max_fd;
49
50
51/* Given the gc module call gc.enable() and return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050052static int
53_enable_gc(PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000054{
55 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020056 _Py_IDENTIFIER(enable);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020057
58 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000059 if (result == NULL)
60 return 1;
61 Py_DECREF(result);
62 return 0;
63}
64
65
Gregory P. Smith8facece2012-01-21 14:01:08 -080066/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050067static int
68_pos_int_from_ascii(char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080069{
70 int num = 0;
71 while (*name >= '0' && *name <= '9') {
72 num = num * 10 + (*name - '0');
73 ++name;
74 }
75 if (*name)
76 return -1; /* Non digit found, not a number. */
77 return num;
78}
79
80
Gregory P. Smith4842efc2012-01-21 21:01:24 -080081#if defined(__FreeBSD__)
82/* When /dev/fd isn't mounted it is often a static directory populated
83 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
84 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
85 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
86 * that properly supports /dev/fd.
87 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050088static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020089_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080090{
91 struct stat dev_stat;
92 struct stat dev_fd_stat;
93 if (stat("/dev", &dev_stat) != 0)
94 return 0;
95 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020096 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -080097 if (dev_stat.st_dev == dev_fd_stat.st_dev)
98 return 0; /* / == /dev == /dev/fd means it is static. #fail */
99 return 1;
100}
101#endif
102
103
Gregory P. Smith8facece2012-01-21 14:01:08 -0800104/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500105static int
106_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800107{
108 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
109 long prev_fd = -1;
110 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
111 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
112 long iter_fd = PyLong_AsLong(py_fd);
113 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
114 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
115 return 1;
116 }
117 }
118 return 0;
119}
120
121
122/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500123static int
124_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800125{
126 /* Binary search. */
127 Py_ssize_t search_min = 0;
128 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
129 if (search_max < 0)
130 return 0;
131 do {
132 long middle = (search_min + search_max) / 2;
133 long middle_fd = PyLong_AsLong(
134 PySequence_Fast_GET_ITEM(fd_sequence, middle));
135 if (fd == middle_fd)
136 return 1;
137 if (fd > middle_fd)
138 search_min = middle + 1;
139 else
140 search_max = middle - 1;
141 } while (search_min <= search_max);
142 return 0;
143}
144
Victor Stinnerdaf45552013-08-28 00:53:59 +0200145static int
146make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
147{
148 Py_ssize_t i, len;
149
150 len = PySequence_Length(py_fds_to_keep);
151 for (i = 0; i < len; ++i) {
152 PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
153 long fd = PyLong_AsLong(fdobj);
154 assert(!PyErr_Occurred());
155 assert(0 <= fd && fd <= INT_MAX);
156 if (fd == errpipe_write) {
157 /* errpipe_write is part of py_fds_to_keep. It must be closed at
158 exec(), but kept open in the child process until exec() is
159 called. */
160 continue;
161 }
162 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
163 return -1;
164 }
165 return 0;
166}
167
Gregory P. Smith8facece2012-01-21 14:01:08 -0800168
169/* Close all file descriptors in the range start_fd inclusive to
170 * end_fd exclusive except for those in py_fds_to_keep. If the
171 * range defined by [start_fd, end_fd) is large this will take a
172 * long time as it calls close() on EVERY possible fd.
173 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500174static void
175_close_fds_by_brute_force(int start_fd, int end_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800176{
177 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
178 Py_ssize_t keep_seq_idx;
179 int fd_num;
180 /* As py_fds_to_keep is sorted we can loop through the list closing
181 * fds inbetween any in the keep list falling within our range. */
182 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
183 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
184 keep_seq_idx);
185 int keep_fd = PyLong_AsLong(py_keep_fd);
186 if (keep_fd < start_fd)
187 continue;
188 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
189 while (close(fd_num) < 0 && errno == EINTR);
190 }
191 start_fd = keep_fd + 1;
192 }
193 if (start_fd <= end_fd) {
194 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
195 while (close(fd_num) < 0 && errno == EINTR);
196 }
197 }
198}
199
200
201#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
202/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
203 * only to read a directory of short file descriptor number names. The kernel
204 * will return an error if we didn't give it enough space. Highly Unlikely.
205 * This structure is very old and stable: It will not change unless the kernel
206 * chooses to break compatibility with all existing binaries. Highly Unlikely.
207 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800208struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700209 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800210 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800211 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800212 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800213 char d_name[256]; /* Filename (null-terminated) */
214};
215
216/* Close all open file descriptors in the range start_fd inclusive to end_fd
217 * exclusive. Do not close any in the sorted py_fds_to_keep list.
218 *
219 * This version is async signal safe as it does not make any unsafe C library
220 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
221 * to resort to making a kernel system call directly but this is the ONLY api
222 * available that does no harm. opendir/readdir/closedir perform memory
223 * allocation and locking so while they usually work they are not guaranteed
224 * to (especially if you have replaced your malloc implementation). A version
225 * of this function that uses those can be found in the _maybe_unsafe variant.
226 *
227 * This is Linux specific because that is all I am ready to test it on. It
228 * should be easy to add OS specific dirent or dirent64 structures and modify
229 * it with some cpp #define magic to work on other OSes as well if you want.
230 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500231static void
232_close_open_fd_range_safe(int start_fd, int end_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800233{
234 int fd_dir_fd;
235 if (start_fd >= end_fd)
236 return;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200237
238 fd_dir_fd = _Py_open(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800239 if (fd_dir_fd == -1) {
240 /* No way to get a list of open fds. */
241 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
242 return;
243 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800244 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800245 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800246 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
247 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800248 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800249 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800250 int offset;
251 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
252 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800253 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800254 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
255 continue; /* Not a number. */
256 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
257 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
258 while (close(fd) < 0 && errno == EINTR);
259 }
260 }
261 }
262 close(fd_dir_fd);
263 }
264}
265
266#define _close_open_fd_range _close_open_fd_range_safe
267
268#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
269
270
271/* Close all open file descriptors in the range start_fd inclusive to end_fd
272 * exclusive. Do not close any in the sorted py_fds_to_keep list.
273 *
274 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800275 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800276 * likely to ever cause a problem is opendir() as it performs an internal
277 * malloc(). Practically this should not be a problem. The Java VM makes the
278 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
279 *
280 * readdir_r() is not used because it provides no benefit. It is typically
281 * implemented as readdir() followed by memcpy(). See also:
282 * http://womble.decadent.org.uk/readdir_r-advisory.html
283 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500284static void
285_close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
286 PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800287{
288 DIR *proc_fd_dir;
289#ifndef HAVE_DIRFD
290 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
291 (start_fd < end_fd)) {
292 ++start_fd;
293 }
294 if (start_fd >= end_fd)
295 return;
296 /* Close our lowest fd before we call opendir so that it is likely to
297 * reuse that fd otherwise we might close opendir's file descriptor in
298 * our loop. This trick assumes that fd's are allocated on a lowest
299 * available basis. */
300 while (close(start_fd) < 0 && errno == EINTR);
301 ++start_fd;
302#endif
303 if (start_fd >= end_fd)
304 return;
305
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800306#if defined(__FreeBSD__)
307 if (!_is_fdescfs_mounted_on_dev_fd())
308 proc_fd_dir = NULL;
309 else
310#endif
311 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312 if (!proc_fd_dir) {
313 /* No way to get a list of open fds. */
314 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
315 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800316 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800317#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800318 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800319#else
320 int fd_used_by_opendir = start_fd - 1;
321#endif
322 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800323 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800324 int fd;
325 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
326 continue; /* Not a number. */
327 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
328 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
329 while (close(fd) < 0 && errno == EINTR);
330 }
331 errno = 0;
332 }
333 if (errno) {
334 /* readdir error, revert behavior. Highly Unlikely. */
335 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
336 }
337 closedir(proc_fd_dir);
338 }
339}
340
341#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
342
343#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
344
345
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000346/*
347 * This function is code executed in the child process immediately after fork
348 * to set things up and call exec().
349 *
350 * All of the code in this function must only use async-signal-safe functions,
351 * listed at `man 7 signal` or
352 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
353 *
354 * This restriction is documented at
355 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
356 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500357static void
358child_exec(char *const exec_array[],
359 char *const argv[],
360 char *const envp[],
361 const char *cwd,
362 int p2cread, int p2cwrite,
363 int c2pread, int c2pwrite,
364 int errread, int errwrite,
365 int errpipe_read, int errpipe_write,
366 int close_fds, int restore_signals,
367 int call_setsid,
368 PyObject *py_fds_to_keep,
369 PyObject *preexec_fn,
370 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000371{
Gregory P. Smith5591b022012-10-10 03:34:47 -0700372 int i, saved_errno, unused, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000373 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000374 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000375 /* Buffer large enough to hold a hex integer. We can't malloc. */
376 char hex_errno[sizeof(saved_errno)*2+1];
377
Victor Stinnerdaf45552013-08-28 00:53:59 +0200378 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
379 goto error;
380
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000381 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200382 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000383 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200384 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000385 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200386 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000387 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000388 POSIX_CALL(close(errpipe_read));
389
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200390 /* When duping fds, if there arises a situation where one of the fds is
391 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
392 if (c2pwrite == 0)
393 POSIX_CALL(c2pwrite = dup(c2pwrite));
394 if (errwrite == 0 || errwrite == 1)
395 POSIX_CALL(errwrite = dup(errwrite));
396
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000397 /* Dup fds for child.
398 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
399 would be a no-op (issue #10806). */
400 if (p2cread == 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200401 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
402 goto error;
403 }
404 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000405 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200406
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000407 if (c2pwrite == 1) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200408 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
409 goto error;
410 }
411 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000412 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200413
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000414 if (errwrite == 2) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200415 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
416 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000417 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200418 else if (errwrite != -1)
419 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000420
421 /* Close pipe fds. Make sure we don't close the same fd more than */
422 /* once, or standard fds. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200423 if (p2cread > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000424 POSIX_CALL(close(p2cread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200425 if (c2pwrite > 2 && c2pwrite != p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000426 POSIX_CALL(close(c2pwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200427 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000428 POSIX_CALL(close(errwrite));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000429
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000430 if (cwd)
431 POSIX_CALL(chdir(cwd));
432
433 if (restore_signals)
434 _Py_RestoreSignals();
435
436#ifdef HAVE_SETSID
437 if (call_setsid)
438 POSIX_CALL(setsid());
439#endif
440
Gregory P. Smith5591b022012-10-10 03:34:47 -0700441 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000442 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
443 /* This is where the user has asked us to deadlock their program. */
444 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
445 if (result == NULL) {
446 /* Stringifying the exception or traceback would involve
447 * memory allocation and thus potential for deadlock.
448 * We've already faced potential deadlock by calling back
449 * into Python in the first place, so it probably doesn't
450 * matter but we avoid it to minimize the possibility. */
451 err_msg = "Exception occurred in preexec_fn.";
452 errno = 0; /* We don't want to report an OSError. */
453 goto error;
454 }
455 /* Py_DECREF(result); - We're about to exec so why bother? */
456 }
457
Charles-François Natali249cdc32013-08-25 18:24:45 +0200458 /* close FDs after executing preexec_fn, which might open FDs */
459 if (close_fds) {
460 int local_max_fd = max_fd;
461#if defined(__NetBSD__)
462 local_max_fd = fcntl(0, F_MAXFD);
463 if (local_max_fd < 0)
464 local_max_fd = max_fd;
465#endif
466 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800467 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200468 }
469
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000470 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
471 /* given the executable_list generated by Lib/subprocess.py. */
472 saved_errno = 0;
473 for (i = 0; exec_array[i] != NULL; ++i) {
474 const char *executable = exec_array[i];
475 if (envp) {
476 execve(executable, argv, envp);
477 } else {
478 execv(executable, argv);
479 }
480 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
481 saved_errno = errno;
482 }
483 }
484 /* Report the first exec error, not the last. */
485 if (saved_errno)
486 errno = saved_errno;
487
488error:
489 saved_errno = errno;
490 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800491 /* We ignore all write() return values as the total size of our writes is
492 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000493 if (saved_errno) {
494 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800495 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000496 cur = hex_errno + sizeof(hex_errno);
497 while (saved_errno != 0 && cur > hex_errno) {
498 *--cur = "0123456789ABCDEF"[saved_errno % 16];
499 saved_errno /= 16;
500 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800501 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
502 unused = write(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700503 if (!reached_preexec) {
504 /* Indicate to the parent that the error happened before exec(). */
505 unused = write(errpipe_write, "noexec", 6);
506 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000507 /* We can't call strerror(saved_errno). It is not async signal safe.
508 * The parent process will look the error message up. */
509 } else {
Gregory P. Smith8d07c262012-11-10 23:53:47 -0800510 unused = write(errpipe_write, "SubprocessError:0:", 18);
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800511 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000512 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800513 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000514}
515
516
517static PyObject *
518subprocess_fork_exec(PyObject* self, PyObject *args)
519{
520 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200521 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000522 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000523 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000524 PyObject *preexec_fn_args_tuple = NULL;
525 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
526 int errpipe_read, errpipe_write, close_fds, restore_signals;
527 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000528 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000529 const char *cwd;
530 pid_t pid;
531 int need_to_reenable_gc = 0;
532 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800533 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000534
535 if (!PyArg_ParseTuple(
Antoine Pitrou721738f2012-08-15 23:20:39 +0200536 args, "OOpOOOiiiiiiiiiiO:fork_exec",
537 &process_args, &executable_list, &close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000538 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000539 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
540 &errread, &errwrite, &errpipe_read, &errpipe_write,
541 &restore_signals, &call_setsid, &preexec_fn))
542 return NULL;
543
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800544 if (close_fds && errpipe_write < 3) { /* precondition */
545 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
546 return NULL;
547 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800548 if (PySequence_Length(py_fds_to_keep) < 0) {
549 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
550 return NULL;
551 }
552 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
553 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000554 return NULL;
555 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000556
557 /* We need to call gc.disable() when we'll be calling preexec_fn */
558 if (preexec_fn != Py_None) {
559 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200560 _Py_IDENTIFIER(isenabled);
561 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200562
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000563 gc_module = PyImport_ImportModule("gc");
564 if (gc_module == NULL)
565 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200566 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000567 if (result == NULL) {
568 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000569 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000570 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000571 need_to_reenable_gc = PyObject_IsTrue(result);
572 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000573 if (need_to_reenable_gc == -1) {
574 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000575 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000576 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200577 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000578 if (result == NULL) {
579 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000580 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000581 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000582 Py_DECREF(result);
583 }
584
585 exec_array = _PySequence_BytesToCharpArray(executable_list);
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200586 if (!exec_array) {
587 Py_XDECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000588 return NULL;
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200589 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000590
591 /* Convert args and env into appropriate arguments for exec() */
592 /* These conversions are done in the parent process to avoid allocating
593 or freeing memory in the child process. */
594 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000595 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000596 /* Equivalent to: */
597 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000598 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200599 if (fast_args == NULL)
600 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000601 num_args = PySequence_Fast_GET_SIZE(fast_args);
602 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000603 if (converted_args == NULL)
604 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000605 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000606 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000607 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000608 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
609 goto cleanup;
610 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
611 }
612
613 argv = _PySequence_BytesToCharpArray(converted_args);
614 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000615 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000616 if (!argv)
617 goto cleanup;
618 }
619
620 if (env_list != Py_None) {
621 envp = _PySequence_BytesToCharpArray(env_list);
622 if (!envp)
623 goto cleanup;
624 }
625
626 if (preexec_fn != Py_None) {
627 preexec_fn_args_tuple = PyTuple_New(0);
628 if (!preexec_fn_args_tuple)
629 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000630 _PyImport_AcquireLock();
631 }
632
633 if (cwd_obj != Py_None) {
634 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
635 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000636 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000637 } else {
638 cwd = NULL;
639 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000640 }
641
642 pid = fork();
643 if (pid == 0) {
644 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000645 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000646 * Code from here to _exit() must only use async-signal-safe functions,
647 * listed at `man 7 signal` or
648 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
649 */
650
651 if (preexec_fn != Py_None) {
652 /* We'll be calling back into Python later so we need to do this.
653 * This call may not be async-signal-safe but neither is calling
654 * back into Python. The user asked us to use hope as a strategy
655 * to avoid deadlock... */
656 PyOS_AfterFork();
657 }
658
659 child_exec(exec_array, argv, envp, cwd,
660 p2cread, p2cwrite, c2pread, c2pwrite,
661 errread, errwrite, errpipe_read, errpipe_write,
662 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800663 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000664 _exit(255);
665 return NULL; /* Dead code to avoid a potential compiler warning. */
666 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000667 Py_XDECREF(cwd_obj2);
668
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000669 if (pid == -1) {
670 /* Capture the errno exception before errno can be clobbered. */
671 PyErr_SetFromErrno(PyExc_OSError);
672 }
673 if (preexec_fn != Py_None &&
674 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
675 PyErr_SetString(PyExc_RuntimeError,
676 "not holding the import lock");
677 }
678
679 /* Parent process */
680 if (envp)
681 _Py_FreeCharPArray(envp);
682 if (argv)
683 _Py_FreeCharPArray(argv);
684 _Py_FreeCharPArray(exec_array);
685
686 /* Reenable gc in the parent process (or if fork failed). */
687 if (need_to_reenable_gc && _enable_gc(gc_module)) {
688 Py_XDECREF(gc_module);
689 return NULL;
690 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000691 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000692 Py_XDECREF(gc_module);
693
694 if (pid == -1)
695 return NULL; /* fork() failed. Exception set earlier. */
696
697 return PyLong_FromPid(pid);
698
699cleanup:
700 if (envp)
701 _Py_FreeCharPArray(envp);
702 if (argv)
703 _Py_FreeCharPArray(argv);
704 _Py_FreeCharPArray(exec_array);
705 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000706 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000707 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000708
709 /* Reenable gc if it was disabled. */
710 if (need_to_reenable_gc)
711 _enable_gc(gc_module);
712 Py_XDECREF(gc_module);
713 return NULL;
714}
715
716
717PyDoc_STRVAR(subprocess_fork_exec_doc,
718"fork_exec(args, executable_list, close_fds, cwd, env,\n\
719 p2cread, p2cwrite, c2pread, c2pwrite,\n\
720 errread, errwrite, errpipe_read, errpipe_write,\n\
721 restore_signals, call_setsid, preexec_fn)\n\
722\n\
723Forks a child process, closes parent file descriptors as appropriate in the\n\
724child and dups the few that are needed before calling exec() in the child\n\
725process.\n\
726\n\
727The preexec_fn, if supplied, will be called immediately before exec.\n\
728WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
729 It may trigger infrequent, difficult to debug deadlocks.\n\
730\n\
731If an error occurs in the child process before the exec, it is\n\
732serialized and written to the errpipe_write fd per subprocess.py.\n\
733\n\
734Returns: the child process's PID.\n\
735\n\
736Raises: Only on an error in the parent process.\n\
737");
738
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000739/* module level code ********************************************************/
740
741PyDoc_STRVAR(module_doc,
742"A POSIX helper for the subprocess module.");
743
744
745static PyMethodDef module_methods[] = {
746 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000747 {NULL, NULL} /* sentinel */
748};
749
750
751static struct PyModuleDef _posixsubprocessmodule = {
752 PyModuleDef_HEAD_INIT,
753 "_posixsubprocess",
754 module_doc,
755 -1, /* No memory is needed. */
756 module_methods,
757};
758
759PyMODINIT_FUNC
760PyInit__posixsubprocess(void)
761{
762#ifdef _SC_OPEN_MAX
763 max_fd = sysconf(_SC_OPEN_MAX);
764 if (max_fd == -1)
765#endif
766 max_fd = 256; /* Matches Lib/subprocess.py */
767
768 return PyModule_Create(&_posixsubprocessmodule);
769}