blob: 1490223d3f3a568bef74a49eff149ec2a0e6212b [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
Gregory P. Smithf9681772015-04-25 23:43:34 -070017#if defined(HAVE_SYS_RESOURCE_H)
18#include <sys/resource.h>
19#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080020#ifdef HAVE_DIRENT_H
21#include <dirent.h>
22#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000023
Gregory P. Smithefeb9da2014-04-14 13:31:21 -070024#if defined(__ANDROID__) && !defined(SYS_getdents64)
25/* Android doesn't expose syscalls, add the definition manually. */
26# include <sys/linux-syscalls.h>
27# define SYS_getdents64 __NR_getdents64
28#endif
29
Gregory P. Smithe3f78482012-01-21 15:16:17 -080030#if defined(sun)
31/* readdir64 is used to work around Solaris 9 bug 6395699. */
32# define readdir readdir64
33# define dirent dirent64
34# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080035/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080036# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080037# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080038# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080039#endif
40
Gregory P. Smith4842efc2012-01-21 21:01:24 -080041#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
42# define FD_DIR "/dev/fd"
43#else
44# define FD_DIR "/proc/self/fd"
45#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000046
Victor Stinnerdaf45552013-08-28 00:53:59 +020047#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000048
49
Martin Panterafdd5132015-11-30 02:21:41 +000050/* If gc was disabled, call gc.enable(). Return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050051static int
Martin Panterafdd5132015-11-30 02:21:41 +000052_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000053{
54 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020055 _Py_IDENTIFIER(enable);
Martin Panterafdd5132015-11-30 02:21:41 +000056 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020057
Martin Panterafdd5132015-11-30 02:21:41 +000058 if (need_to_reenable_gc) {
59 PyErr_Fetch(&exctype, &val, &tb);
60 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
61 if (exctype != NULL) {
62 PyErr_Restore(exctype, val, tb);
63 }
64 if (result == NULL) {
65 return 1;
66 }
67 Py_DECREF(result);
68 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000069 return 0;
70}
71
72
Gregory P. Smith8facece2012-01-21 14:01:08 -080073/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050074static int
75_pos_int_from_ascii(char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080076{
77 int num = 0;
78 while (*name >= '0' && *name <= '9') {
79 num = num * 10 + (*name - '0');
80 ++name;
81 }
82 if (*name)
83 return -1; /* Non digit found, not a number. */
84 return num;
85}
86
87
Gregory P. Smith4842efc2012-01-21 21:01:24 -080088#if defined(__FreeBSD__)
89/* When /dev/fd isn't mounted it is often a static directory populated
90 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
91 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
92 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
93 * that properly supports /dev/fd.
94 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050095static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020096_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080097{
98 struct stat dev_stat;
99 struct stat dev_fd_stat;
100 if (stat("/dev", &dev_stat) != 0)
101 return 0;
102 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200103 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800104 if (dev_stat.st_dev == dev_fd_stat.st_dev)
105 return 0; /* / == /dev == /dev/fd means it is static. #fail */
106 return 1;
107}
108#endif
109
110
Gregory P. Smith8facece2012-01-21 14:01:08 -0800111/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500112static int
113_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800114{
115 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
116 long prev_fd = -1;
117 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
118 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
119 long iter_fd = PyLong_AsLong(py_fd);
120 if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
121 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
122 return 1;
123 }
124 }
125 return 0;
126}
127
128
129/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500130static int
131_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800132{
133 /* Binary search. */
134 Py_ssize_t search_min = 0;
135 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
136 if (search_max < 0)
137 return 0;
138 do {
139 long middle = (search_min + search_max) / 2;
140 long middle_fd = PyLong_AsLong(
141 PySequence_Fast_GET_ITEM(fd_sequence, middle));
142 if (fd == middle_fd)
143 return 1;
144 if (fd > middle_fd)
145 search_min = middle + 1;
146 else
147 search_max = middle - 1;
148 } while (search_min <= search_max);
149 return 0;
150}
151
Victor Stinnerdaf45552013-08-28 00:53:59 +0200152static int
153make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
154{
155 Py_ssize_t i, len;
156
157 len = PySequence_Length(py_fds_to_keep);
158 for (i = 0; i < len; ++i) {
159 PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
160 long fd = PyLong_AsLong(fdobj);
161 assert(!PyErr_Occurred());
162 assert(0 <= fd && fd <= INT_MAX);
163 if (fd == errpipe_write) {
164 /* errpipe_write is part of py_fds_to_keep. It must be closed at
165 exec(), but kept open in the child process until exec() is
166 called. */
167 continue;
168 }
169 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
170 return -1;
171 }
172 return 0;
173}
174
Gregory P. Smith8facece2012-01-21 14:01:08 -0800175
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700176/* Get the maximum file descriptor that could be opened by this process.
177 * This function is async signal safe for use between fork() and exec().
178 */
179static long
180safe_get_max_fd(void)
181{
182 long local_max_fd;
183#if defined(__NetBSD__)
184 local_max_fd = fcntl(0, F_MAXFD);
185 if (local_max_fd >= 0)
186 return local_max_fd;
187#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700188#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
189 struct rlimit rl;
190 /* Not on the POSIX async signal safe functions list but likely
191 * safe. TODO - Someone should audit OpenBSD to make sure. */
192 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
193 return (long) rl.rlim_max;
194#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700195#ifdef _SC_OPEN_MAX
196 local_max_fd = sysconf(_SC_OPEN_MAX);
197 if (local_max_fd == -1)
198#endif
199 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
200 return local_max_fd;
201}
202
203
204/* Close all file descriptors in the range from start_fd and higher
205 * except for those in py_fds_to_keep. If the range defined by
206 * [start_fd, safe_get_max_fd()) is large this will take a long
207 * time as it calls close() on EVERY possible fd.
208 *
209 * It isn't possible to know for sure what the max fd to go up to
210 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800211 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500212static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700213_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800214{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700215 long end_fd = safe_get_max_fd();
Gregory P. Smith8facece2012-01-21 14:01:08 -0800216 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
217 Py_ssize_t keep_seq_idx;
218 int fd_num;
219 /* As py_fds_to_keep is sorted we can loop through the list closing
220 * fds inbetween any in the keep list falling within our range. */
221 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
222 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
223 keep_seq_idx);
224 int keep_fd = PyLong_AsLong(py_keep_fd);
225 if (keep_fd < start_fd)
226 continue;
227 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200228 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800229 }
230 start_fd = keep_fd + 1;
231 }
232 if (start_fd <= end_fd) {
233 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200234 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800235 }
236 }
237}
238
239
240#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
241/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
242 * only to read a directory of short file descriptor number names. The kernel
243 * will return an error if we didn't give it enough space. Highly Unlikely.
244 * This structure is very old and stable: It will not change unless the kernel
245 * chooses to break compatibility with all existing binaries. Highly Unlikely.
246 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800247struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700248 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800249 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800250 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800251 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800252 char d_name[256]; /* Filename (null-terminated) */
253};
254
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700255/* Close all open file descriptors in the range from start_fd and higher
256 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800257 *
258 * This version is async signal safe as it does not make any unsafe C library
259 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
260 * to resort to making a kernel system call directly but this is the ONLY api
261 * available that does no harm. opendir/readdir/closedir perform memory
262 * allocation and locking so while they usually work they are not guaranteed
263 * to (especially if you have replaced your malloc implementation). A version
264 * of this function that uses those can be found in the _maybe_unsafe variant.
265 *
266 * This is Linux specific because that is all I am ready to test it on. It
267 * should be easy to add OS specific dirent or dirent64 structures and modify
268 * it with some cpp #define magic to work on other OSes as well if you want.
269 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500270static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700271_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800272{
273 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200274
275 fd_dir_fd = _Py_open(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800276 if (fd_dir_fd == -1) {
277 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700278 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800279 return;
280 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800281 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800282 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800283 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
284 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800285 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800286 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800287 int offset;
288 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
289 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800290 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800291 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
292 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700293 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800294 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200295 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800296 }
297 }
298 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200299 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800300 }
301}
302
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700303#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800304
305#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
306
307
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700308/* Close all open file descriptors from start_fd and higher.
309 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800310 *
311 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800312 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800313 * likely to ever cause a problem is opendir() as it performs an internal
314 * malloc(). Practically this should not be a problem. The Java VM makes the
315 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
316 *
317 * readdir_r() is not used because it provides no benefit. It is typically
318 * implemented as readdir() followed by memcpy(). See also:
319 * http://womble.decadent.org.uk/readdir_r-advisory.html
320 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500321static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700322_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800323{
324 DIR *proc_fd_dir;
325#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700326 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800327 ++start_fd;
328 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329 /* Close our lowest fd before we call opendir so that it is likely to
330 * reuse that fd otherwise we might close opendir's file descriptor in
331 * our loop. This trick assumes that fd's are allocated on a lowest
332 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200333 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800334 ++start_fd;
335#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800336
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800337#if defined(__FreeBSD__)
338 if (!_is_fdescfs_mounted_on_dev_fd())
339 proc_fd_dir = NULL;
340 else
341#endif
342 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800343 if (!proc_fd_dir) {
344 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700345 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800346 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800347 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800348#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800349 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800350#else
351 int fd_used_by_opendir = start_fd - 1;
352#endif
353 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800354 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800355 int fd;
356 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
357 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700358 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800359 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200360 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800361 }
362 errno = 0;
363 }
364 if (errno) {
365 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700366 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800367 }
368 closedir(proc_fd_dir);
369 }
370}
371
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700372#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800373
374#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
375
376
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000377/*
378 * This function is code executed in the child process immediately after fork
379 * to set things up and call exec().
380 *
381 * All of the code in this function must only use async-signal-safe functions,
382 * listed at `man 7 signal` or
383 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
384 *
385 * This restriction is documented at
386 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
387 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500388static void
389child_exec(char *const exec_array[],
390 char *const argv[],
391 char *const envp[],
392 const char *cwd,
393 int p2cread, int p2cwrite,
394 int c2pread, int c2pwrite,
395 int errread, int errwrite,
396 int errpipe_read, int errpipe_write,
397 int close_fds, int restore_signals,
398 int call_setsid,
399 PyObject *py_fds_to_keep,
400 PyObject *preexec_fn,
401 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000402{
Gregory P. Smith5591b022012-10-10 03:34:47 -0700403 int i, saved_errno, unused, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000404 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000405 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000406 /* Buffer large enough to hold a hex integer. We can't malloc. */
407 char hex_errno[sizeof(saved_errno)*2+1];
408
Victor Stinnerdaf45552013-08-28 00:53:59 +0200409 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
410 goto error;
411
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000412 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200413 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000414 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200415 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000416 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200417 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000418 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000419 POSIX_CALL(close(errpipe_read));
420
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200421 /* When duping fds, if there arises a situation where one of the fds is
422 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
423 if (c2pwrite == 0)
424 POSIX_CALL(c2pwrite = dup(c2pwrite));
425 if (errwrite == 0 || errwrite == 1)
426 POSIX_CALL(errwrite = dup(errwrite));
427
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000428 /* Dup fds for child.
429 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
430 would be a no-op (issue #10806). */
431 if (p2cread == 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200432 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
433 goto error;
434 }
435 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000436 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200437
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000438 if (c2pwrite == 1) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200439 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
440 goto error;
441 }
442 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000443 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200444
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000445 if (errwrite == 2) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200446 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
447 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000448 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200449 else if (errwrite != -1)
450 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000451
452 /* Close pipe fds. Make sure we don't close the same fd more than */
453 /* once, or standard fds. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200454 if (p2cread > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000455 POSIX_CALL(close(p2cread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200456 if (c2pwrite > 2 && c2pwrite != p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000457 POSIX_CALL(close(c2pwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200458 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000459 POSIX_CALL(close(errwrite));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000460
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000461 if (cwd)
462 POSIX_CALL(chdir(cwd));
463
464 if (restore_signals)
465 _Py_RestoreSignals();
466
467#ifdef HAVE_SETSID
468 if (call_setsid)
469 POSIX_CALL(setsid());
470#endif
471
Gregory P. Smith5591b022012-10-10 03:34:47 -0700472 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000473 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
474 /* This is where the user has asked us to deadlock their program. */
475 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
476 if (result == NULL) {
477 /* Stringifying the exception or traceback would involve
478 * memory allocation and thus potential for deadlock.
479 * We've already faced potential deadlock by calling back
480 * into Python in the first place, so it probably doesn't
481 * matter but we avoid it to minimize the possibility. */
482 err_msg = "Exception occurred in preexec_fn.";
483 errno = 0; /* We don't want to report an OSError. */
484 goto error;
485 }
486 /* Py_DECREF(result); - We're about to exec so why bother? */
487 }
488
Charles-François Natali249cdc32013-08-25 18:24:45 +0200489 /* close FDs after executing preexec_fn, which might open FDs */
490 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200491 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700492 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200493 }
494
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000495 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
496 /* given the executable_list generated by Lib/subprocess.py. */
497 saved_errno = 0;
498 for (i = 0; exec_array[i] != NULL; ++i) {
499 const char *executable = exec_array[i];
500 if (envp) {
501 execve(executable, argv, envp);
502 } else {
503 execv(executable, argv);
504 }
505 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
506 saved_errno = errno;
507 }
508 }
509 /* Report the first exec error, not the last. */
510 if (saved_errno)
511 errno = saved_errno;
512
513error:
514 saved_errno = errno;
515 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800516 /* We ignore all write() return values as the total size of our writes is
517 * less than PIPEBUF and we cannot do anything about an error anyways. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000518 if (saved_errno) {
519 char *cur;
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800520 unused = write(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000521 cur = hex_errno + sizeof(hex_errno);
522 while (saved_errno != 0 && cur > hex_errno) {
523 *--cur = "0123456789ABCDEF"[saved_errno % 16];
524 saved_errno /= 16;
525 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800526 unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
527 unused = write(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700528 if (!reached_preexec) {
529 /* Indicate to the parent that the error happened before exec(). */
530 unused = write(errpipe_write, "noexec", 6);
531 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000532 /* We can't call strerror(saved_errno). It is not async signal safe.
533 * The parent process will look the error message up. */
534 } else {
Gregory P. Smith8d07c262012-11-10 23:53:47 -0800535 unused = write(errpipe_write, "SubprocessError:0:", 18);
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800536 unused = write(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000537 }
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800538 if (unused) return; /* silly? yes! avoids gcc compiler warning. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000539}
540
541
542static PyObject *
543subprocess_fork_exec(PyObject* self, PyObject *args)
544{
545 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200546 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000547 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000548 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000549 PyObject *preexec_fn_args_tuple = NULL;
550 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
551 int errpipe_read, errpipe_write, close_fds, restore_signals;
552 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000553 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000554 const char *cwd;
555 pid_t pid;
556 int need_to_reenable_gc = 0;
557 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800558 Py_ssize_t arg_num;
Victor Stinner8f437aa2014-10-05 17:25:19 +0200559 int import_lock_held = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000560
561 if (!PyArg_ParseTuple(
Antoine Pitrou721738f2012-08-15 23:20:39 +0200562 args, "OOpOOOiiiiiiiiiiO:fork_exec",
563 &process_args, &executable_list, &close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000564 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000565 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
566 &errread, &errwrite, &errpipe_read, &errpipe_write,
567 &restore_signals, &call_setsid, &preexec_fn))
568 return NULL;
569
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800570 if (close_fds && errpipe_write < 3) { /* precondition */
571 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
572 return NULL;
573 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800574 if (PySequence_Length(py_fds_to_keep) < 0) {
575 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
576 return NULL;
577 }
578 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
579 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000580 return NULL;
581 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000582
583 /* We need to call gc.disable() when we'll be calling preexec_fn */
584 if (preexec_fn != Py_None) {
585 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200586 _Py_IDENTIFIER(isenabled);
587 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200588
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000589 gc_module = PyImport_ImportModule("gc");
590 if (gc_module == NULL)
591 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200592 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000593 if (result == NULL) {
594 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000595 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000596 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000597 need_to_reenable_gc = PyObject_IsTrue(result);
598 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000599 if (need_to_reenable_gc == -1) {
600 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000601 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000602 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200603 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000604 if (result == NULL) {
605 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000606 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000607 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000608 Py_DECREF(result);
609 }
610
611 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200612 if (!exec_array)
613 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000614
615 /* Convert args and env into appropriate arguments for exec() */
616 /* These conversions are done in the parent process to avoid allocating
617 or freeing memory in the child process. */
618 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000619 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000620 /* Equivalent to: */
621 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000622 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200623 if (fast_args == NULL)
624 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000625 num_args = PySequence_Fast_GET_SIZE(fast_args);
626 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000627 if (converted_args == NULL)
628 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000629 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000630 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000631 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000632 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
633 goto cleanup;
634 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
635 }
636
637 argv = _PySequence_BytesToCharpArray(converted_args);
638 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000639 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000640 if (!argv)
641 goto cleanup;
642 }
643
644 if (env_list != Py_None) {
645 envp = _PySequence_BytesToCharpArray(env_list);
646 if (!envp)
647 goto cleanup;
648 }
649
650 if (preexec_fn != Py_None) {
651 preexec_fn_args_tuple = PyTuple_New(0);
652 if (!preexec_fn_args_tuple)
653 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000654 _PyImport_AcquireLock();
Victor Stinner8f437aa2014-10-05 17:25:19 +0200655 import_lock_held = 1;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000656 }
657
658 if (cwd_obj != Py_None) {
659 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
660 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000661 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000662 } else {
663 cwd = NULL;
664 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000665 }
666
667 pid = fork();
668 if (pid == 0) {
669 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000670 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000671 * Code from here to _exit() must only use async-signal-safe functions,
672 * listed at `man 7 signal` or
673 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
674 */
675
676 if (preexec_fn != Py_None) {
677 /* We'll be calling back into Python later so we need to do this.
678 * This call may not be async-signal-safe but neither is calling
679 * back into Python. The user asked us to use hope as a strategy
680 * to avoid deadlock... */
681 PyOS_AfterFork();
682 }
683
684 child_exec(exec_array, argv, envp, cwd,
685 p2cread, p2cwrite, c2pread, c2pwrite,
686 errread, errwrite, errpipe_read, errpipe_write,
687 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800688 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000689 _exit(255);
690 return NULL; /* Dead code to avoid a potential compiler warning. */
691 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000692 Py_XDECREF(cwd_obj2);
693
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000694 if (pid == -1) {
695 /* Capture the errno exception before errno can be clobbered. */
696 PyErr_SetFromErrno(PyExc_OSError);
697 }
698 if (preexec_fn != Py_None &&
699 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
700 PyErr_SetString(PyExc_RuntimeError,
701 "not holding the import lock");
Martin Panterafdd5132015-11-30 02:21:41 +0000702 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000703 }
Victor Stinner8f437aa2014-10-05 17:25:19 +0200704 import_lock_held = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000705
706 /* Parent process */
707 if (envp)
708 _Py_FreeCharPArray(envp);
709 if (argv)
710 _Py_FreeCharPArray(argv);
711 _Py_FreeCharPArray(exec_array);
712
713 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +0000714 if (_enable_gc(need_to_reenable_gc, gc_module)) {
715 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000716 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000717 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000718 Py_XDECREF(gc_module);
719
720 if (pid == -1)
721 return NULL; /* fork() failed. Exception set earlier. */
722
723 return PyLong_FromPid(pid);
724
725cleanup:
Victor Stinner8f437aa2014-10-05 17:25:19 +0200726 if (import_lock_held)
727 _PyImport_ReleaseLock();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000728 if (envp)
729 _Py_FreeCharPArray(envp);
730 if (argv)
731 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200732 if (exec_array)
733 _Py_FreeCharPArray(exec_array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000734 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000735 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000736 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +0000737 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000738 Py_XDECREF(gc_module);
739 return NULL;
740}
741
742
743PyDoc_STRVAR(subprocess_fork_exec_doc,
744"fork_exec(args, executable_list, close_fds, cwd, env,\n\
745 p2cread, p2cwrite, c2pread, c2pwrite,\n\
746 errread, errwrite, errpipe_read, errpipe_write,\n\
747 restore_signals, call_setsid, preexec_fn)\n\
748\n\
749Forks a child process, closes parent file descriptors as appropriate in the\n\
750child and dups the few that are needed before calling exec() in the child\n\
751process.\n\
752\n\
753The preexec_fn, if supplied, will be called immediately before exec.\n\
754WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
755 It may trigger infrequent, difficult to debug deadlocks.\n\
756\n\
757If an error occurs in the child process before the exec, it is\n\
758serialized and written to the errpipe_write fd per subprocess.py.\n\
759\n\
760Returns: the child process's PID.\n\
761\n\
762Raises: Only on an error in the parent process.\n\
763");
764
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000765/* module level code ********************************************************/
766
767PyDoc_STRVAR(module_doc,
768"A POSIX helper for the subprocess module.");
769
770
771static PyMethodDef module_methods[] = {
772 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000773 {NULL, NULL} /* sentinel */
774};
775
776
777static struct PyModuleDef _posixsubprocessmodule = {
778 PyModuleDef_HEAD_INIT,
779 "_posixsubprocess",
780 module_doc,
781 -1, /* No memory is needed. */
782 module_methods,
783};
784
785PyMODINIT_FUNC
786PyInit__posixsubprocess(void)
787{
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000788 return PyModule_Create(&_posixsubprocessmodule);
789}