blob: a2d7022992606e094f5d6d299d66546d704985aa [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
Victor Stinnerdaf45552013-08-28 00:53:59 +020038#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000039
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)
Victor Stinnerdaf45552013-08-28 00:53:59 +020090 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -080091 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
Victor Stinnerdaf45552013-08-28 00:53:59 +0200139static int
140make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
141{
142 Py_ssize_t i, len;
143
144 len = PySequence_Length(py_fds_to_keep);
145 for (i = 0; i < len; ++i) {
146 PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
147 long fd = PyLong_AsLong(fdobj);
148 assert(!PyErr_Occurred());
149 assert(0 <= fd && fd <= INT_MAX);
150 if (fd == errpipe_write) {
151 /* errpipe_write is part of py_fds_to_keep. It must be closed at
152 exec(), but kept open in the child process until exec() is
153 called. */
154 continue;
155 }
156 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
157 return -1;
158 }
159 return 0;
160}
161
Gregory P. Smith8facece2012-01-21 14:01:08 -0800162
163/* Close all file descriptors in the range start_fd inclusive to
164 * end_fd exclusive except for those in py_fds_to_keep. If the
165 * range defined by [start_fd, end_fd) is large this will take a
166 * long time as it calls close() on EVERY possible fd.
167 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500168static void
169_close_fds_by_brute_force(int start_fd, int end_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800170{
171 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
172 Py_ssize_t keep_seq_idx;
173 int fd_num;
174 /* As py_fds_to_keep is sorted we can loop through the list closing
175 * fds inbetween any in the keep list falling within our range. */
176 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
177 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
178 keep_seq_idx);
179 int keep_fd = PyLong_AsLong(py_keep_fd);
180 if (keep_fd < start_fd)
181 continue;
182 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
183 while (close(fd_num) < 0 && errno == EINTR);
184 }
185 start_fd = keep_fd + 1;
186 }
187 if (start_fd <= end_fd) {
188 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
189 while (close(fd_num) < 0 && errno == EINTR);
190 }
191 }
192}
193
194
195#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
196/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
197 * only to read a directory of short file descriptor number names. The kernel
198 * will return an error if we didn't give it enough space. Highly Unlikely.
199 * This structure is very old and stable: It will not change unless the kernel
200 * chooses to break compatibility with all existing binaries. Highly Unlikely.
201 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800202struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700203 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800204 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800205 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800206 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800207 char d_name[256]; /* Filename (null-terminated) */
208};
209
210/* Close all open file descriptors in the range start_fd inclusive to end_fd
211 * exclusive. Do not close any in the sorted py_fds_to_keep list.
212 *
213 * This version is async signal safe as it does not make any unsafe C library
214 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
215 * to resort to making a kernel system call directly but this is the ONLY api
216 * available that does no harm. opendir/readdir/closedir perform memory
217 * allocation and locking so while they usually work they are not guaranteed
218 * to (especially if you have replaced your malloc implementation). A version
219 * of this function that uses those can be found in the _maybe_unsafe variant.
220 *
221 * This is Linux specific because that is all I am ready to test it on. It
222 * should be easy to add OS specific dirent or dirent64 structures and modify
223 * it with some cpp #define magic to work on other OSes as well if you want.
224 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500225static void
226_close_open_fd_range_safe(int start_fd, int end_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800227{
228 int fd_dir_fd;
229 if (start_fd >= end_fd)
230 return;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200231
232 fd_dir_fd = _Py_open(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800233 if (fd_dir_fd == -1) {
234 /* No way to get a list of open fds. */
235 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
236 return;
237 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800238 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800239 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800240 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
241 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800242 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800243 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800244 int offset;
245 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
246 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800247 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800248 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
249 continue; /* Not a number. */
250 if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
251 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
252 while (close(fd) < 0 && errno == EINTR);
253 }
254 }
255 }
256 close(fd_dir_fd);
257 }
258}
259
260#define _close_open_fd_range _close_open_fd_range_safe
261
262#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
263
264
265/* Close all open file descriptors in the range start_fd inclusive to end_fd
266 * exclusive. Do not close any in the sorted py_fds_to_keep list.
267 *
268 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800269 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800270 * likely to ever cause a problem is opendir() as it performs an internal
271 * malloc(). Practically this should not be a problem. The Java VM makes the
272 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
273 *
274 * readdir_r() is not used because it provides no benefit. It is typically
275 * implemented as readdir() followed by memcpy(). See also:
276 * http://womble.decadent.org.uk/readdir_r-advisory.html
277 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500278static void
279_close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
280 PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800281{
282 DIR *proc_fd_dir;
283#ifndef HAVE_DIRFD
284 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep) &&
285 (start_fd < end_fd)) {
286 ++start_fd;
287 }
288 if (start_fd >= end_fd)
289 return;
290 /* Close our lowest fd before we call opendir so that it is likely to
291 * reuse that fd otherwise we might close opendir's file descriptor in
292 * our loop. This trick assumes that fd's are allocated on a lowest
293 * available basis. */
294 while (close(start_fd) < 0 && errno == EINTR);
295 ++start_fd;
296#endif
297 if (start_fd >= end_fd)
298 return;
299
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800300#if defined(__FreeBSD__)
301 if (!_is_fdescfs_mounted_on_dev_fd())
302 proc_fd_dir = NULL;
303 else
304#endif
305 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800306 if (!proc_fd_dir) {
307 /* No way to get a list of open fds. */
308 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
309 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800310 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800311#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800312 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800313#else
314 int fd_used_by_opendir = start_fd - 1;
315#endif
316 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800317 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800318 int fd;
319 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
320 continue; /* Not a number. */
321 if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
322 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
323 while (close(fd) < 0 && errno == EINTR);
324 }
325 errno = 0;
326 }
327 if (errno) {
328 /* readdir error, revert behavior. Highly Unlikely. */
329 _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep);
330 }
331 closedir(proc_fd_dir);
332 }
333}
334
335#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
336
337#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
338
339
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000340/*
341 * This function is code executed in the child process immediately after fork
342 * to set things up and call exec().
343 *
344 * All of the code in this function must only use async-signal-safe functions,
345 * listed at `man 7 signal` or
346 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
347 *
348 * This restriction is documented at
349 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
350 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500351static void
352child_exec(char *const exec_array[],
353 char *const argv[],
354 char *const envp[],
355 const char *cwd,
356 int p2cread, int p2cwrite,
357 int c2pread, int c2pwrite,
358 int errread, int errwrite,
359 int errpipe_read, int errpipe_write,
360 int close_fds, int restore_signals,
361 int call_setsid,
362 PyObject *py_fds_to_keep,
363 PyObject *preexec_fn,
364 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000365{
Gregory P. Smith5591b022012-10-10 03:34:47 -0700366 int i, saved_errno, unused, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000367 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000368 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000369 /* Buffer large enough to hold a hex integer. We can't malloc. */
370 char hex_errno[sizeof(saved_errno)*2+1];
371
Victor Stinnerdaf45552013-08-28 00:53:59 +0200372 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
373 goto error;
374
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000375 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200376 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000377 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200378 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000379 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200380 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000381 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000382 POSIX_CALL(close(errpipe_read));
383
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200384 /* When duping fds, if there arises a situation where one of the fds is
385 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
386 if (c2pwrite == 0)
387 POSIX_CALL(c2pwrite = dup(c2pwrite));
388 if (errwrite == 0 || errwrite == 1)
389 POSIX_CALL(errwrite = dup(errwrite));
390
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000391 /* Dup fds for child.
392 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
393 would be a no-op (issue #10806). */
394 if (p2cread == 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200395 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
396 goto error;
397 }
398 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000399 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200400
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000401 if (c2pwrite == 1) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200402 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
403 goto error;
404 }
405 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000406 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200407
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000408 if (errwrite == 2) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200409 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
410 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000411 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200412 else if (errwrite != -1)
413 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000414
415 /* Close pipe fds. Make sure we don't close the same fd more than */
416 /* once, or standard fds. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200417 if (p2cread > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000418 POSIX_CALL(close(p2cread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200419 if (c2pwrite > 2 && c2pwrite != p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000420 POSIX_CALL(close(c2pwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200421 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000422 POSIX_CALL(close(errwrite));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000423
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000424 if (cwd)
425 POSIX_CALL(chdir(cwd));
426
427 if (restore_signals)
428 _Py_RestoreSignals();
429
430#ifdef HAVE_SETSID
431 if (call_setsid)
432 POSIX_CALL(setsid());
433#endif
434
Gregory P. Smith5591b022012-10-10 03:34:47 -0700435 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000436 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
437 /* This is where the user has asked us to deadlock their program. */
438 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
439 if (result == NULL) {
440 /* Stringifying the exception or traceback would involve
441 * memory allocation and thus potential for deadlock.
442 * We've already faced potential deadlock by calling back
443 * into Python in the first place, so it probably doesn't
444 * matter but we avoid it to minimize the possibility. */
445 err_msg = "Exception occurred in preexec_fn.";
446 errno = 0; /* We don't want to report an OSError. */
447 goto error;
448 }
449 /* Py_DECREF(result); - We're about to exec so why bother? */
450 }
451
Charles-François Natali249cdc32013-08-25 18:24:45 +0200452 /* close FDs after executing preexec_fn, which might open FDs */
453 if (close_fds) {
454 int local_max_fd = max_fd;
455#if defined(__NetBSD__)
456 local_max_fd = fcntl(0, F_MAXFD);
457 if (local_max_fd < 0)
458 local_max_fd = max_fd;
459#endif
460 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
461 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
462 }
463
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000464 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
465 /* given the executable_list generated by Lib/subprocess.py. */
466 saved_errno = 0;
467 for (i = 0; exec_array[i] != NULL; ++i) {
468 const char *executable = exec_array[i];
469 if (envp) {
470 execve(executable, argv, envp);
471 } else {
472 execv(executable, argv);
473 }
474 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
475 saved_errno = errno;
476 }
477 }
478 /* Report the first exec error, not the last. */
479 if (saved_errno)
480 errno = saved_errno;
481
482error:
483 saved_errno = errno;
484 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800485 /* We ignore all write() return values as the total size of our writes is
486 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000487 if (saved_errno) {
488 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800489 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000490 cur = hex_errno + sizeof(hex_errno);
491 while (saved_errno != 0 && cur > hex_errno) {
492 *--cur = "0123456789ABCDEF"[saved_errno % 16];
493 saved_errno /= 16;
494 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800495 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
496 unused = write(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700497 if (!reached_preexec) {
498 /* Indicate to the parent that the error happened before exec(). */
499 unused = write(errpipe_write, "noexec", 6);
500 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000501 /* We can't call strerror(saved_errno). It is not async signal safe.
502 * The parent process will look the error message up. */
503 } else {
Gregory P. Smith8d07c262012-11-10 23:53:47 -0800504 unused = write(errpipe_write, "SubprocessError:0:", 18);
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800505 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000506 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800507 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000508}
509
510
511static PyObject *
512subprocess_fork_exec(PyObject* self, PyObject *args)
513{
514 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200515 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000516 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000517 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000518 PyObject *preexec_fn_args_tuple = NULL;
519 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
520 int errpipe_read, errpipe_write, close_fds, restore_signals;
521 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000522 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000523 const char *cwd;
524 pid_t pid;
525 int need_to_reenable_gc = 0;
526 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800527 Py_ssize_t arg_num;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000528
529 if (!PyArg_ParseTuple(
Antoine Pitrou721738f2012-08-15 23:20:39 +0200530 args, "OOpOOOiiiiiiiiiiO:fork_exec",
531 &process_args, &executable_list, &close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000532 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000533 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
534 &errread, &errwrite, &errpipe_read, &errpipe_write,
535 &restore_signals, &call_setsid, &preexec_fn))
536 return NULL;
537
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000538 if (close_fds && errpipe_write < 3) { /* precondition */
539 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
540 return NULL;
541 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800542 if (PySequence_Length(py_fds_to_keep) < 0) {
543 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
544 return NULL;
545 }
546 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
547 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000548 return NULL;
549 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000550
551 /* We need to call gc.disable() when we'll be calling preexec_fn */
552 if (preexec_fn != Py_None) {
553 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200554 _Py_IDENTIFIER(isenabled);
555 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200556
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000557 gc_module = PyImport_ImportModule("gc");
558 if (gc_module == NULL)
559 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200560 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000561 if (result == NULL) {
562 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000563 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000564 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000565 need_to_reenable_gc = PyObject_IsTrue(result);
566 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000567 if (need_to_reenable_gc == -1) {
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 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200571 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000572 if (result == NULL) {
573 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000574 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000575 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000576 Py_DECREF(result);
577 }
578
579 exec_array = _PySequence_BytesToCharpArray(executable_list);
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200580 if (!exec_array) {
581 Py_XDECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000582 return NULL;
Ross Lagerwallf2b34b82012-08-24 13:25:59 +0200583 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000584
585 /* Convert args and env into appropriate arguments for exec() */
586 /* These conversions are done in the parent process to avoid allocating
587 or freeing memory in the child process. */
588 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000589 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000590 /* Equivalent to: */
591 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000592 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200593 if (fast_args == NULL)
594 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000595 num_args = PySequence_Fast_GET_SIZE(fast_args);
596 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000597 if (converted_args == NULL)
598 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000599 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000600 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000601 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000602 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
603 goto cleanup;
604 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
605 }
606
607 argv = _PySequence_BytesToCharpArray(converted_args);
608 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000609 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000610 if (!argv)
611 goto cleanup;
612 }
613
614 if (env_list != Py_None) {
615 envp = _PySequence_BytesToCharpArray(env_list);
616 if (!envp)
617 goto cleanup;
618 }
619
620 if (preexec_fn != Py_None) {
621 preexec_fn_args_tuple = PyTuple_New(0);
622 if (!preexec_fn_args_tuple)
623 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000624 _PyImport_AcquireLock();
625 }
626
627 if (cwd_obj != Py_None) {
628 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
629 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000630 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000631 } else {
632 cwd = NULL;
633 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000634 }
635
636 pid = fork();
637 if (pid == 0) {
638 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000639 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000640 * Code from here to _exit() must only use async-signal-safe functions,
641 * listed at `man 7 signal` or
642 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
643 */
644
645 if (preexec_fn != Py_None) {
646 /* We'll be calling back into Python later so we need to do this.
647 * This call may not be async-signal-safe but neither is calling
648 * back into Python. The user asked us to use hope as a strategy
649 * to avoid deadlock... */
650 PyOS_AfterFork();
651 }
652
653 child_exec(exec_array, argv, envp, cwd,
654 p2cread, p2cwrite, c2pread, c2pwrite,
655 errread, errwrite, errpipe_read, errpipe_write,
656 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800657 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000658 _exit(255);
659 return NULL; /* Dead code to avoid a potential compiler warning. */
660 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000661 Py_XDECREF(cwd_obj2);
662
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000663 if (pid == -1) {
664 /* Capture the errno exception before errno can be clobbered. */
665 PyErr_SetFromErrno(PyExc_OSError);
666 }
667 if (preexec_fn != Py_None &&
668 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
669 PyErr_SetString(PyExc_RuntimeError,
670 "not holding the import lock");
671 }
672
673 /* Parent process */
674 if (envp)
675 _Py_FreeCharPArray(envp);
676 if (argv)
677 _Py_FreeCharPArray(argv);
678 _Py_FreeCharPArray(exec_array);
679
680 /* Reenable gc in the parent process (or if fork failed). */
681 if (need_to_reenable_gc && _enable_gc(gc_module)) {
682 Py_XDECREF(gc_module);
683 return NULL;
684 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000685 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000686 Py_XDECREF(gc_module);
687
688 if (pid == -1)
689 return NULL; /* fork() failed. Exception set earlier. */
690
691 return PyLong_FromPid(pid);
692
693cleanup:
694 if (envp)
695 _Py_FreeCharPArray(envp);
696 if (argv)
697 _Py_FreeCharPArray(argv);
698 _Py_FreeCharPArray(exec_array);
699 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000700 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000701 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000702
703 /* Reenable gc if it was disabled. */
704 if (need_to_reenable_gc)
705 _enable_gc(gc_module);
706 Py_XDECREF(gc_module);
707 return NULL;
708}
709
710
711PyDoc_STRVAR(subprocess_fork_exec_doc,
712"fork_exec(args, executable_list, close_fds, cwd, env,\n\
713 p2cread, p2cwrite, c2pread, c2pwrite,\n\
714 errread, errwrite, errpipe_read, errpipe_write,\n\
715 restore_signals, call_setsid, preexec_fn)\n\
716\n\
717Forks a child process, closes parent file descriptors as appropriate in the\n\
718child and dups the few that are needed before calling exec() in the child\n\
719process.\n\
720\n\
721The preexec_fn, if supplied, will be called immediately before exec.\n\
722WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
723 It may trigger infrequent, difficult to debug deadlocks.\n\
724\n\
725If an error occurs in the child process before the exec, it is\n\
726serialized and written to the errpipe_write fd per subprocess.py.\n\
727\n\
728Returns: the child process's PID.\n\
729\n\
730Raises: Only on an error in the parent process.\n\
731");
732
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000733/* module level code ********************************************************/
734
735PyDoc_STRVAR(module_doc,
736"A POSIX helper for the subprocess module.");
737
738
739static PyMethodDef module_methods[] = {
740 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000741 {NULL, NULL} /* sentinel */
742};
743
744
745static struct PyModuleDef _posixsubprocessmodule = {
746 PyModuleDef_HEAD_INIT,
747 "_posixsubprocess",
748 module_doc,
749 -1, /* No memory is needed. */
750 module_methods,
751};
752
753PyMODINIT_FUNC
754PyInit__posixsubprocess(void)
755{
756#ifdef _SC_OPEN_MAX
757 max_fd = sysconf(_SC_OPEN_MAX);
758 if (max_fd == -1)
759#endif
760 max_fd = 256; /* Matches Lib/subprocess.py */
761
762 return PyModule_Create(&_posixsubprocessmodule);
763}