blob: a33df211e980eaa8079896333b67593add009443 [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
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000047/* Given the gc module call gc.enable() and return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050048static int
49_enable_gc(PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000050{
51 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020052 _Py_IDENTIFIER(enable);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020053
54 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000055 if (result == NULL)
56 return 1;
57 Py_DECREF(result);
58 return 0;
59}
60
61
Gregory P. Smith8facece2012-01-21 14:01:08 -080062/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050063static int
64_pos_int_from_ascii(char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080065{
66 int num = 0;
67 while (*name >= '0' && *name <= '9') {
68 num = num * 10 + (*name - '0');
69 ++name;
70 }
71 if (*name)
72 return -1; /* Non digit found, not a number. */
73 return num;
74}
75
76
Gregory P. Smith4842efc2012-01-21 21:01:24 -080077#if defined(__FreeBSD__)
78/* When /dev/fd isn't mounted it is often a static directory populated
79 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
80 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
81 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
82 * that properly supports /dev/fd.
83 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050084static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020085_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080086{
87 struct stat dev_stat;
88 struct stat dev_fd_stat;
89 if (stat("/dev", &dev_stat) != 0)
90 return 0;
91 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020092 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -080093 if (dev_stat.st_dev == dev_fd_stat.st_dev)
94 return 0; /* / == /dev == /dev/fd means it is static. #fail */
95 return 1;
96}
97#endif
98
99
Gregory P. Smith8facece2012-01-21 14:01:08 -0800100/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500101static int
102_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800103{
104 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
105 long prev_fd = -1;
106 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
107 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
108 long iter_fd = PyLong_AsLong(py_fd);
109 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
110 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
111 return 1;
112 }
113 }
114 return 0;
115}
116
117
118/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500119static int
120_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800121{
122 /* Binary search. */
123 Py_ssize_t search_min = 0;
124 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
125 if (search_max < 0)
126 return 0;
127 do {
128 long middle = (search_min + search_max) / 2;
129 long middle_fd = PyLong_AsLong(
130 PySequence_Fast_GET_ITEM(fd_sequence, middle));
131 if (fd == middle_fd)
132 return 1;
133 if (fd > middle_fd)
134 search_min = middle + 1;
135 else
136 search_max = middle - 1;
137 } while (search_min <= search_max);
138 return 0;
139}
140
Victor Stinnerdaf45552013-08-28 00:53:59 +0200141static int
142make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
143{
144 Py_ssize_t i, len;
145
146 len = PySequence_Length(py_fds_to_keep);
147 for (i = 0; i < len; ++i) {
148 PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
149 long fd = PyLong_AsLong(fdobj);
150 assert(!PyErr_Occurred());
151 assert(0 <= fd && fd <= INT_MAX);
152 if (fd == errpipe_write) {
153 /* errpipe_write is part of py_fds_to_keep. It must be closed at
154 exec(), but kept open in the child process until exec() is
155 called. */
156 continue;
157 }
158 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
159 return -1;
160 }
161 return 0;
162}
163
Gregory P. Smith8facece2012-01-21 14:01:08 -0800164
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700165/* Get the maximum file descriptor that could be opened by this process.
166 * This function is async signal safe for use between fork() and exec().
167 */
168static long
169safe_get_max_fd(void)
170{
171 long local_max_fd;
172#if defined(__NetBSD__)
173 local_max_fd = fcntl(0, F_MAXFD);
174 if (local_max_fd >= 0)
175 return local_max_fd;
176#endif
177#ifdef _SC_OPEN_MAX
178 local_max_fd = sysconf(_SC_OPEN_MAX);
179 if (local_max_fd == -1)
180#endif
181 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
182 return local_max_fd;
183}
184
185
186/* Close all file descriptors in the range from start_fd and higher
187 * except for those in py_fds_to_keep. If the range defined by
188 * [start_fd, safe_get_max_fd()) is large this will take a long
189 * time as it calls close() on EVERY possible fd.
190 *
191 * It isn't possible to know for sure what the max fd to go up to
192 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800193 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500194static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700195_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800196{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700197 long end_fd = safe_get_max_fd();
Gregory P. Smith8facece2012-01-21 14:01:08 -0800198 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
199 Py_ssize_t keep_seq_idx;
200 int fd_num;
201 /* As py_fds_to_keep is sorted we can loop through the list closing
202 * fds inbetween any in the keep list falling within our range. */
203 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
204 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
205 keep_seq_idx);
206 int keep_fd = PyLong_AsLong(py_keep_fd);
207 if (keep_fd < start_fd)
208 continue;
209 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
210 while (close(fd_num) < 0 && errno == EINTR);
211 }
212 start_fd = keep_fd + 1;
213 }
214 if (start_fd <= end_fd) {
215 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
216 while (close(fd_num) < 0 && errno == EINTR);
217 }
218 }
219}
220
221
222#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
223/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
224 * only to read a directory of short file descriptor number names. The kernel
225 * will return an error if we didn't give it enough space. Highly Unlikely.
226 * This structure is very old and stable: It will not change unless the kernel
227 * chooses to break compatibility with all existing binaries. Highly Unlikely.
228 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800229struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700230 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800231 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800232 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800233 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800234 char d_name[256]; /* Filename (null-terminated) */
235};
236
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700237/* Close all open file descriptors in the range from start_fd and higher
238 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800239 *
240 * This version is async signal safe as it does not make any unsafe C library
241 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
242 * to resort to making a kernel system call directly but this is the ONLY api
243 * available that does no harm. opendir/readdir/closedir perform memory
244 * allocation and locking so while they usually work they are not guaranteed
245 * to (especially if you have replaced your malloc implementation). A version
246 * of this function that uses those can be found in the _maybe_unsafe variant.
247 *
248 * This is Linux specific because that is all I am ready to test it on. It
249 * should be easy to add OS specific dirent or dirent64 structures and modify
250 * it with some cpp #define magic to work on other OSes as well if you want.
251 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500252static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700253_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800254{
255 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200256
257 fd_dir_fd = _Py_open(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800258 if (fd_dir_fd == -1) {
259 /* No way to get a list of open fds. */
Victor Stinnera555cfc2015-03-18 00:22:14 +0100260 PyErr_Clear();
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700261 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800262 return;
263 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800264 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800265 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800266 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
267 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800268 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800269 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800270 int offset;
271 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
272 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800273 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800274 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
275 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700276 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800277 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
278 while (close(fd) < 0 && errno == EINTR);
279 }
280 }
281 }
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700282 while (close(fd_dir_fd) < 0 && errno == EINTR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800283 }
284}
285
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700286#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800287
288#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
289
290
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700291/* Close all open file descriptors from start_fd and higher.
292 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800293 *
294 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800295 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800296 * likely to ever cause a problem is opendir() as it performs an internal
297 * malloc(). Practically this should not be a problem. The Java VM makes the
298 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
299 *
300 * readdir_r() is not used because it provides no benefit. It is typically
301 * implemented as readdir() followed by memcpy(). See also:
302 * http://womble.decadent.org.uk/readdir_r-advisory.html
303 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500304static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700305_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800306{
307 DIR *proc_fd_dir;
308#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700309 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800310 ++start_fd;
311 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312 /* Close our lowest fd before we call opendir so that it is likely to
313 * reuse that fd otherwise we might close opendir's file descriptor in
314 * our loop. This trick assumes that fd's are allocated on a lowest
315 * available basis. */
316 while (close(start_fd) < 0 && errno == EINTR);
317 ++start_fd;
318#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800319
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800320#if defined(__FreeBSD__)
321 if (!_is_fdescfs_mounted_on_dev_fd())
322 proc_fd_dir = NULL;
323 else
324#endif
325 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800326 if (!proc_fd_dir) {
327 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700328 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800330 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800331#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800332 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800333#else
334 int fd_used_by_opendir = start_fd - 1;
335#endif
336 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800337 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800338 int fd;
339 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
340 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700341 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800342 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
343 while (close(fd) < 0 && errno == EINTR);
344 }
345 errno = 0;
346 }
347 if (errno) {
348 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700349 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800350 }
351 closedir(proc_fd_dir);
352 }
353}
354
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700355#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800356
357#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
358
359
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000360/*
361 * This function is code executed in the child process immediately after fork
362 * to set things up and call exec().
363 *
364 * All of the code in this function must only use async-signal-safe functions,
365 * listed at `man 7 signal` or
366 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
367 *
368 * This restriction is documented at
369 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
370 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500371static void
372child_exec(char *const exec_array[],
373 char *const argv[],
374 char *const envp[],
375 const char *cwd,
376 int p2cread, int p2cwrite,
377 int c2pread, int c2pwrite,
378 int errread, int errwrite,
379 int errpipe_read, int errpipe_write,
380 int close_fds, int restore_signals,
381 int call_setsid,
382 PyObject *py_fds_to_keep,
383 PyObject *preexec_fn,
384 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000385{
Gregory P. Smith5591b022012-10-10 03:34:47 -0700386 int i, saved_errno, unused, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000387 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000388 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000389 /* Buffer large enough to hold a hex integer. We can't malloc. */
390 char hex_errno[sizeof(saved_errno)*2+1];
391
Victor Stinnerdaf45552013-08-28 00:53:59 +0200392 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
393 goto error;
394
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000395 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200396 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000397 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200398 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000399 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200400 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000401 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000402 POSIX_CALL(close(errpipe_read));
403
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200404 /* When duping fds, if there arises a situation where one of the fds is
405 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
406 if (c2pwrite == 0)
407 POSIX_CALL(c2pwrite = dup(c2pwrite));
408 if (errwrite == 0 || errwrite == 1)
409 POSIX_CALL(errwrite = dup(errwrite));
410
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000411 /* Dup fds for child.
412 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
413 would be a no-op (issue #10806). */
414 if (p2cread == 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200415 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
416 goto error;
417 }
418 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000419 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200420
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000421 if (c2pwrite == 1) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200422 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
423 goto error;
424 }
425 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000426 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200427
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000428 if (errwrite == 2) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200429 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
430 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000431 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200432 else if (errwrite != -1)
433 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000434
435 /* Close pipe fds. Make sure we don't close the same fd more than */
436 /* once, or standard fds. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200437 if (p2cread > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000438 POSIX_CALL(close(p2cread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200439 if (c2pwrite > 2 && c2pwrite != p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000440 POSIX_CALL(close(c2pwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200441 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000442 POSIX_CALL(close(errwrite));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000443
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000444 if (cwd)
445 POSIX_CALL(chdir(cwd));
446
447 if (restore_signals)
448 _Py_RestoreSignals();
449
450#ifdef HAVE_SETSID
451 if (call_setsid)
452 POSIX_CALL(setsid());
453#endif
454
Gregory P. Smith5591b022012-10-10 03:34:47 -0700455 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000456 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
457 /* This is where the user has asked us to deadlock their program. */
458 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
459 if (result == NULL) {
460 /* Stringifying the exception or traceback would involve
461 * memory allocation and thus potential for deadlock.
462 * We've already faced potential deadlock by calling back
463 * into Python in the first place, so it probably doesn't
464 * matter but we avoid it to minimize the possibility. */
465 err_msg = "Exception occurred in preexec_fn.";
466 errno = 0; /* We don't want to report an OSError. */
467 goto error;
468 }
469 /* Py_DECREF(result); - We're about to exec so why bother? */
470 }
471
Charles-François Natali249cdc32013-08-25 18:24:45 +0200472 /* close FDs after executing preexec_fn, which might open FDs */
473 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200474 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700475 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200476 }
477
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000478 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
479 /* given the executable_list generated by Lib/subprocess.py. */
480 saved_errno = 0;
481 for (i = 0; exec_array[i] != NULL; ++i) {
482 const char *executable = exec_array[i];
483 if (envp) {
484 execve(executable, argv, envp);
485 } else {
486 execv(executable, argv);
487 }
488 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
489 saved_errno = errno;
490 }
491 }
492 /* Report the first exec error, not the last. */
493 if (saved_errno)
494 errno = saved_errno;
495
496error:
497 saved_errno = errno;
498 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800499 /* We ignore all write() return values as the total size of our writes is
500 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000501 if (saved_errno) {
502 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800503 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000504 cur = hex_errno + sizeof(hex_errno);
505 while (saved_errno != 0 && cur > hex_errno) {
506 *--cur = "0123456789ABCDEF"[saved_errno % 16];
507 saved_errno /= 16;
508 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800509 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
510 unused = write(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700511 if (!reached_preexec) {
512 /* Indicate to the parent that the error happened before exec(). */
513 unused = write(errpipe_write, "noexec", 6);
514 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000515 /* We can't call strerror(saved_errno). It is not async signal safe.
516 * The parent process will look the error message up. */
517 } else {
Gregory P. Smith8d07c262012-11-10 23:53:47 -0800518 unused = write(errpipe_write, "SubprocessError:0:", 18);
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800519 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000520 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800521 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000522}
523
524
525static PyObject *
526subprocess_fork_exec(PyObject* self, PyObject *args)
527{
528 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200529 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000530 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000531 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000532 PyObject *preexec_fn_args_tuple = NULL;
533 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
534 int errpipe_read, errpipe_write, close_fds, restore_signals;
535 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000536 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000537 const char *cwd;
538 pid_t pid;
539 int need_to_reenable_gc = 0;
540 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800541 Py_ssize_t arg_num;
Victor Stinner8f437aa2014-10-05 17:25:19 +0200542 int import_lock_held = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000543
544 if (!PyArg_ParseTuple(
Antoine Pitrou721738f2012-08-15 23:20:39 +0200545 args, "OOpOOOiiiiiiiiiiO:fork_exec",
546 &process_args, &executable_list, &close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000547 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000548 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
549 &errread, &errwrite, &errpipe_read, &errpipe_write,
550 &restore_signals, &call_setsid, &preexec_fn))
551 return NULL;
552
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800553 if (close_fds && errpipe_write < 3) { /* precondition */
554 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
555 return NULL;
556 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800557 if (PySequence_Length(py_fds_to_keep) < 0) {
558 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
559 return NULL;
560 }
561 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
562 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000563 return NULL;
564 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000565
566 /* We need to call gc.disable() when we'll be calling preexec_fn */
567 if (preexec_fn != Py_None) {
568 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200569 _Py_IDENTIFIER(isenabled);
570 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200571
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000572 gc_module = PyImport_ImportModule("gc");
573 if (gc_module == NULL)
574 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200575 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000576 if (result == NULL) {
577 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000578 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000579 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000580 need_to_reenable_gc = PyObject_IsTrue(result);
581 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000582 if (need_to_reenable_gc == -1) {
583 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000584 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000585 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200586 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000587 if (result == NULL) {
588 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000589 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000590 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000591 Py_DECREF(result);
592 }
593
594 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200595 if (!exec_array)
596 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000597
598 /* Convert args and env into appropriate arguments for exec() */
599 /* These conversions are done in the parent process to avoid allocating
600 or freeing memory in the child process. */
601 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000602 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000603 /* Equivalent to: */
604 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000605 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200606 if (fast_args == NULL)
607 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000608 num_args = PySequence_Fast_GET_SIZE(fast_args);
609 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000610 if (converted_args == NULL)
611 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000612 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000613 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000614 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000615 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
616 goto cleanup;
617 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
618 }
619
620 argv = _PySequence_BytesToCharpArray(converted_args);
621 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000622 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000623 if (!argv)
624 goto cleanup;
625 }
626
627 if (env_list != Py_None) {
628 envp = _PySequence_BytesToCharpArray(env_list);
629 if (!envp)
630 goto cleanup;
631 }
632
633 if (preexec_fn != Py_None) {
634 preexec_fn_args_tuple = PyTuple_New(0);
635 if (!preexec_fn_args_tuple)
636 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000637 _PyImport_AcquireLock();
Victor Stinner8f437aa2014-10-05 17:25:19 +0200638 import_lock_held = 1;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000639 }
640
641 if (cwd_obj != Py_None) {
642 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
643 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000644 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000645 } else {
646 cwd = NULL;
647 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000648 }
649
650 pid = fork();
651 if (pid == 0) {
652 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000653 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000654 * Code from here to _exit() must only use async-signal-safe functions,
655 * listed at `man 7 signal` or
656 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
657 */
658
659 if (preexec_fn != Py_None) {
660 /* We'll be calling back into Python later so we need to do this.
661 * This call may not be async-signal-safe but neither is calling
662 * back into Python. The user asked us to use hope as a strategy
663 * to avoid deadlock... */
664 PyOS_AfterFork();
665 }
666
667 child_exec(exec_array, argv, envp, cwd,
668 p2cread, p2cwrite, c2pread, c2pwrite,
669 errread, errwrite, errpipe_read, errpipe_write,
670 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800671 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000672 _exit(255);
673 return NULL; /* Dead code to avoid a potential compiler warning. */
674 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000675 Py_XDECREF(cwd_obj2);
676
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000677 if (pid == -1) {
678 /* Capture the errno exception before errno can be clobbered. */
679 PyErr_SetFromErrno(PyExc_OSError);
680 }
681 if (preexec_fn != Py_None &&
682 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
683 PyErr_SetString(PyExc_RuntimeError,
684 "not holding the import lock");
685 }
Victor Stinner8f437aa2014-10-05 17:25:19 +0200686 import_lock_held = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000687
688 /* Parent process */
689 if (envp)
690 _Py_FreeCharPArray(envp);
691 if (argv)
692 _Py_FreeCharPArray(argv);
693 _Py_FreeCharPArray(exec_array);
694
695 /* Reenable gc in the parent process (or if fork failed). */
696 if (need_to_reenable_gc && _enable_gc(gc_module)) {
697 Py_XDECREF(gc_module);
698 return NULL;
699 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000700 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000701 Py_XDECREF(gc_module);
702
703 if (pid == -1)
704 return NULL; /* fork() failed. Exception set earlier. */
705
706 return PyLong_FromPid(pid);
707
708cleanup:
Victor Stinner8f437aa2014-10-05 17:25:19 +0200709 if (import_lock_held)
710 _PyImport_ReleaseLock();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000711 if (envp)
712 _Py_FreeCharPArray(envp);
713 if (argv)
714 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200715 if (exec_array)
716 _Py_FreeCharPArray(exec_array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000717 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000718 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000719 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000720
721 /* Reenable gc if it was disabled. */
Victor Stinner8f437aa2014-10-05 17:25:19 +0200722 if (need_to_reenable_gc) {
723 PyObject *exctype, *val, *tb;
724 PyErr_Fetch(&exctype, &val, &tb);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000725 _enable_gc(gc_module);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200726 PyErr_Restore(exctype, val, tb);
727 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000728 Py_XDECREF(gc_module);
729 return NULL;
730}
731
732
733PyDoc_STRVAR(subprocess_fork_exec_doc,
734"fork_exec(args, executable_list, close_fds, cwd, env,\n\
735 p2cread, p2cwrite, c2pread, c2pwrite,\n\
736 errread, errwrite, errpipe_read, errpipe_write,\n\
737 restore_signals, call_setsid, preexec_fn)\n\
738\n\
739Forks a child process, closes parent file descriptors as appropriate in the\n\
740child and dups the few that are needed before calling exec() in the child\n\
741process.\n\
742\n\
743The preexec_fn, if supplied, will be called immediately before exec.\n\
744WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
745 It may trigger infrequent, difficult to debug deadlocks.\n\
746\n\
747If an error occurs in the child process before the exec, it is\n\
748serialized and written to the errpipe_write fd per subprocess.py.\n\
749\n\
750Returns: the child process's PID.\n\
751\n\
752Raises: Only on an error in the parent process.\n\
753");
754
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000755/* module level code ********************************************************/
756
757PyDoc_STRVAR(module_doc,
758"A POSIX helper for the subprocess module.");
759
760
761static PyMethodDef module_methods[] = {
762 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000763 {NULL, NULL} /* sentinel */
764};
765
766
767static struct PyModuleDef _posixsubprocessmodule = {
768 PyModuleDef_HEAD_INIT,
769 "_posixsubprocess",
770 module_doc,
771 -1, /* No memory is needed. */
772 module_methods,
773};
774
775PyMODINIT_FUNC
776PyInit__posixsubprocess(void)
777{
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000778 return PyModule_Create(&_posixsubprocessmodule);
779}