blob: 2cdc38176beeb6993a7e1a8a1f372035e2c5ac0b [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
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000050/* Given the gc module call gc.enable() and return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050051static int
52_enable_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 v. Löwisafe55bb2011-10-09 10:38:36 +020056
57 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000058 if (result == NULL)
59 return 1;
60 Py_DECREF(result);
61 return 0;
62}
63
64
Gregory P. Smith8facece2012-01-21 14:01:08 -080065/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050066static int
67_pos_int_from_ascii(char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080068{
69 int num = 0;
70 while (*name >= '0' && *name <= '9') {
71 num = num * 10 + (*name - '0');
72 ++name;
73 }
74 if (*name)
75 return -1; /* Non digit found, not a number. */
76 return num;
77}
78
79
Gregory P. Smith4842efc2012-01-21 21:01:24 -080080#if defined(__FreeBSD__)
81/* When /dev/fd isn't mounted it is often a static directory populated
82 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
83 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
84 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
85 * that properly supports /dev/fd.
86 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050087static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020088_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080089{
90 struct stat dev_stat;
91 struct stat dev_fd_stat;
92 if (stat("/dev", &dev_stat) != 0)
93 return 0;
94 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020095 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -080096 if (dev_stat.st_dev == dev_fd_stat.st_dev)
97 return 0; /* / == /dev == /dev/fd means it is static. #fail */
98 return 1;
99}
100#endif
101
102
Gregory P. Smith8facece2012-01-21 14:01:08 -0800103/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500104static int
105_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800106{
107 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
108 long prev_fd = -1;
109 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
110 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
111 long iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800112 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800113 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
114 return 1;
115 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800116 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800117 }
118 return 0;
119}
120
121
122/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500123static int
124_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800125{
126 /* Binary search. */
127 Py_ssize_t search_min = 0;
128 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
129 if (search_max < 0)
130 return 0;
131 do {
132 long middle = (search_min + search_max) / 2;
133 long middle_fd = PyLong_AsLong(
134 PySequence_Fast_GET_ITEM(fd_sequence, middle));
135 if (fd == middle_fd)
136 return 1;
137 if (fd > middle_fd)
138 search_min = middle + 1;
139 else
140 search_max = middle - 1;
141 } while (search_min <= search_max);
142 return 0;
143}
144
Victor Stinnerdaf45552013-08-28 00:53:59 +0200145static int
146make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
147{
148 Py_ssize_t i, len;
149
150 len = PySequence_Length(py_fds_to_keep);
151 for (i = 0; i < len; ++i) {
152 PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
153 long fd = PyLong_AsLong(fdobj);
154 assert(!PyErr_Occurred());
155 assert(0 <= fd && fd <= INT_MAX);
156 if (fd == errpipe_write) {
157 /* errpipe_write is part of py_fds_to_keep. It must be closed at
158 exec(), but kept open in the child process until exec() is
159 called. */
160 continue;
161 }
162 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
163 return -1;
164 }
165 return 0;
166}
167
Gregory P. Smith8facece2012-01-21 14:01:08 -0800168
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700169/* Get the maximum file descriptor that could be opened by this process.
170 * This function is async signal safe for use between fork() and exec().
171 */
172static long
173safe_get_max_fd(void)
174{
175 long local_max_fd;
176#if defined(__NetBSD__)
177 local_max_fd = fcntl(0, F_MAXFD);
178 if (local_max_fd >= 0)
179 return local_max_fd;
180#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700181#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
182 struct rlimit rl;
183 /* Not on the POSIX async signal safe functions list but likely
184 * safe. TODO - Someone should audit OpenBSD to make sure. */
185 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
186 return (long) rl.rlim_max;
187#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700188#ifdef _SC_OPEN_MAX
189 local_max_fd = sysconf(_SC_OPEN_MAX);
190 if (local_max_fd == -1)
191#endif
192 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
193 return local_max_fd;
194}
195
196
197/* Close all file descriptors in the range from start_fd and higher
198 * except for those in py_fds_to_keep. If the range defined by
199 * [start_fd, safe_get_max_fd()) is large this will take a long
200 * time as it calls close() on EVERY possible fd.
201 *
202 * It isn't possible to know for sure what the max fd to go up to
203 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800204 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500205static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700206_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800207{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700208 long end_fd = safe_get_max_fd();
Gregory P. Smith8facece2012-01-21 14:01:08 -0800209 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
210 Py_ssize_t keep_seq_idx;
211 int fd_num;
212 /* As py_fds_to_keep is sorted we can loop through the list closing
213 * fds inbetween any in the keep list falling within our range. */
214 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
215 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
216 keep_seq_idx);
217 int keep_fd = PyLong_AsLong(py_keep_fd);
218 if (keep_fd < start_fd)
219 continue;
220 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200221 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800222 }
223 start_fd = keep_fd + 1;
224 }
225 if (start_fd <= end_fd) {
226 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200227 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800228 }
229 }
230}
231
232
233#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
234/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
235 * only to read a directory of short file descriptor number names. The kernel
236 * will return an error if we didn't give it enough space. Highly Unlikely.
237 * This structure is very old and stable: It will not change unless the kernel
238 * chooses to break compatibility with all existing binaries. Highly Unlikely.
239 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800240struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700241 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800242 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800243 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800244 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800245 char d_name[256]; /* Filename (null-terminated) */
246};
247
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700248/* Close all open file descriptors in the range from start_fd and higher
249 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800250 *
251 * This version is async signal safe as it does not make any unsafe C library
252 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
253 * to resort to making a kernel system call directly but this is the ONLY api
254 * available that does no harm. opendir/readdir/closedir perform memory
255 * allocation and locking so while they usually work they are not guaranteed
256 * to (especially if you have replaced your malloc implementation). A version
257 * of this function that uses those can be found in the _maybe_unsafe variant.
258 *
259 * This is Linux specific because that is all I am ready to test it on. It
260 * should be easy to add OS specific dirent or dirent64 structures and modify
261 * it with some cpp #define magic to work on other OSes as well if you want.
262 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500263static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700264_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800265{
266 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200267
Victor Stinner160e8192015-03-30 02:18:31 +0200268 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800269 if (fd_dir_fd == -1) {
270 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700271 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800272 return;
273 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800274 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800275 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800276 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
277 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800278 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800279 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800280 int offset;
281 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
282 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800283 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800284 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
285 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700286 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800287 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200288 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800289 }
290 }
291 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200292 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800293 }
294}
295
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700296#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800297
298#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
299
300
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700301/* Close all open file descriptors from start_fd and higher.
302 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800303 *
304 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800305 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800306 * likely to ever cause a problem is opendir() as it performs an internal
307 * malloc(). Practically this should not be a problem. The Java VM makes the
308 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
309 *
310 * readdir_r() is not used because it provides no benefit. It is typically
311 * implemented as readdir() followed by memcpy(). See also:
312 * http://womble.decadent.org.uk/readdir_r-advisory.html
313 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500314static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700315_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800316{
317 DIR *proc_fd_dir;
318#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700319 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800320 ++start_fd;
321 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800322 /* Close our lowest fd before we call opendir so that it is likely to
323 * reuse that fd otherwise we might close opendir's file descriptor in
324 * our loop. This trick assumes that fd's are allocated on a lowest
325 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200326 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800327 ++start_fd;
328#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800330#if defined(__FreeBSD__)
331 if (!_is_fdescfs_mounted_on_dev_fd())
332 proc_fd_dir = NULL;
333 else
334#endif
335 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800336 if (!proc_fd_dir) {
337 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700338 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800339 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800340 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800341#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800342 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800343#else
344 int fd_used_by_opendir = start_fd - 1;
345#endif
346 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800347 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800348 int fd;
349 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
350 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700351 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800352 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200353 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800354 }
355 errno = 0;
356 }
357 if (errno) {
358 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700359 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800360 }
361 closedir(proc_fd_dir);
362 }
363}
364
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700365#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800366
367#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
368
369
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000370/*
371 * This function is code executed in the child process immediately after fork
372 * to set things up and call exec().
373 *
374 * All of the code in this function must only use async-signal-safe functions,
375 * listed at `man 7 signal` or
376 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
377 *
378 * This restriction is documented at
379 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
380 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500381static void
382child_exec(char *const exec_array[],
383 char *const argv[],
384 char *const envp[],
385 const char *cwd,
386 int p2cread, int p2cwrite,
387 int c2pread, int c2pwrite,
388 int errread, int errwrite,
389 int errpipe_read, int errpipe_write,
390 int close_fds, int restore_signals,
391 int call_setsid,
392 PyObject *py_fds_to_keep,
393 PyObject *preexec_fn,
394 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000395{
Victor Stinner185fd332015-04-01 18:35:53 +0200396 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000397 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000398 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000399 /* Buffer large enough to hold a hex integer. We can't malloc. */
400 char hex_errno[sizeof(saved_errno)*2+1];
401
Victor Stinnerdaf45552013-08-28 00:53:59 +0200402 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
403 goto error;
404
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000405 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200406 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000407 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200408 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000409 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200410 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000411 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000412 POSIX_CALL(close(errpipe_read));
413
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200414 /* When duping fds, if there arises a situation where one of the fds is
415 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
416 if (c2pwrite == 0)
417 POSIX_CALL(c2pwrite = dup(c2pwrite));
418 if (errwrite == 0 || errwrite == 1)
419 POSIX_CALL(errwrite = dup(errwrite));
420
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000421 /* Dup fds for child.
422 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
423 would be a no-op (issue #10806). */
424 if (p2cread == 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200425 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
426 goto error;
427 }
428 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000429 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200430
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000431 if (c2pwrite == 1) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200432 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
433 goto error;
434 }
435 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000436 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200437
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000438 if (errwrite == 2) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200439 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
440 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000441 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200442 else if (errwrite != -1)
443 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000444
445 /* Close pipe fds. Make sure we don't close the same fd more than */
446 /* once, or standard fds. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200447 if (p2cread > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000448 POSIX_CALL(close(p2cread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200449 if (c2pwrite > 2 && c2pwrite != p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000450 POSIX_CALL(close(c2pwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200451 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000452 POSIX_CALL(close(errwrite));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000453
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000454 if (cwd)
455 POSIX_CALL(chdir(cwd));
456
457 if (restore_signals)
458 _Py_RestoreSignals();
459
460#ifdef HAVE_SETSID
461 if (call_setsid)
462 POSIX_CALL(setsid());
463#endif
464
Gregory P. Smith5591b022012-10-10 03:34:47 -0700465 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000466 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
467 /* This is where the user has asked us to deadlock their program. */
468 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
469 if (result == NULL) {
470 /* Stringifying the exception or traceback would involve
471 * memory allocation and thus potential for deadlock.
472 * We've already faced potential deadlock by calling back
473 * into Python in the first place, so it probably doesn't
474 * matter but we avoid it to minimize the possibility. */
475 err_msg = "Exception occurred in preexec_fn.";
476 errno = 0; /* We don't want to report an OSError. */
477 goto error;
478 }
479 /* Py_DECREF(result); - We're about to exec so why bother? */
480 }
481
Charles-François Natali249cdc32013-08-25 18:24:45 +0200482 /* close FDs after executing preexec_fn, which might open FDs */
483 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200484 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700485 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200486 }
487
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000488 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
489 /* given the executable_list generated by Lib/subprocess.py. */
490 saved_errno = 0;
491 for (i = 0; exec_array[i] != NULL; ++i) {
492 const char *executable = exec_array[i];
493 if (envp) {
494 execve(executable, argv, envp);
495 } else {
496 execv(executable, argv);
497 }
498 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
499 saved_errno = errno;
500 }
501 }
502 /* Report the first exec error, not the last. */
503 if (saved_errno)
504 errno = saved_errno;
505
506error:
507 saved_errno = errno;
508 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800509 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200510 less than PIPEBUF and we cannot do anything about an error anyways.
511 Use _Py_write_noraise() to retry write() if it is interrupted by a
512 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000513 if (saved_errno) {
514 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200515 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000516 cur = hex_errno + sizeof(hex_errno);
517 while (saved_errno != 0 && cur > hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000518 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000519 saved_errno /= 16;
520 }
Victor Stinner185fd332015-04-01 18:35:53 +0200521 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
522 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700523 if (!reached_preexec) {
524 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200525 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700526 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000527 /* We can't call strerror(saved_errno). It is not async signal safe.
528 * The parent process will look the error message up. */
529 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200530 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
531 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000532 }
533}
534
535
536static PyObject *
537subprocess_fork_exec(PyObject* self, PyObject *args)
538{
539 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200540 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000541 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000542 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000543 PyObject *preexec_fn_args_tuple = NULL;
544 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
545 int errpipe_read, errpipe_write, close_fds, restore_signals;
546 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000547 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000548 const char *cwd;
549 pid_t pid;
550 int need_to_reenable_gc = 0;
551 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800552 Py_ssize_t arg_num;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200553#ifdef WITH_THREAD
Victor Stinner8f437aa2014-10-05 17:25:19 +0200554 int import_lock_held = 0;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200555#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000556
557 if (!PyArg_ParseTuple(
Antoine Pitrou721738f2012-08-15 23:20:39 +0200558 args, "OOpOOOiiiiiiiiiiO:fork_exec",
559 &process_args, &executable_list, &close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000560 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000561 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
562 &errread, &errwrite, &errpipe_read, &errpipe_write,
563 &restore_signals, &call_setsid, &preexec_fn))
564 return NULL;
565
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800566 if (close_fds && errpipe_write < 3) { /* precondition */
567 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
568 return NULL;
569 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800570 if (PySequence_Length(py_fds_to_keep) < 0) {
571 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
572 return NULL;
573 }
574 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
575 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000576 return NULL;
577 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000578
579 /* We need to call gc.disable() when we'll be calling preexec_fn */
580 if (preexec_fn != Py_None) {
581 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200582 _Py_IDENTIFIER(isenabled);
583 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200584
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000585 gc_module = PyImport_ImportModule("gc");
586 if (gc_module == NULL)
587 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200588 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000589 if (result == NULL) {
590 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000591 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000592 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000593 need_to_reenable_gc = PyObject_IsTrue(result);
594 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000595 if (need_to_reenable_gc == -1) {
596 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000597 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000598 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200599 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000600 if (result == NULL) {
601 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000602 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000603 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000604 Py_DECREF(result);
605 }
606
607 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200608 if (!exec_array)
609 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000610
611 /* Convert args and env into appropriate arguments for exec() */
612 /* These conversions are done in the parent process to avoid allocating
613 or freeing memory in the child process. */
614 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000615 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000616 /* Equivalent to: */
617 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000618 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200619 if (fast_args == NULL)
620 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000621 num_args = PySequence_Fast_GET_SIZE(fast_args);
622 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000623 if (converted_args == NULL)
624 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000625 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000626 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000627 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000628 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
629 goto cleanup;
630 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
631 }
632
633 argv = _PySequence_BytesToCharpArray(converted_args);
634 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000635 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000636 if (!argv)
637 goto cleanup;
638 }
639
640 if (env_list != Py_None) {
641 envp = _PySequence_BytesToCharpArray(env_list);
642 if (!envp)
643 goto cleanup;
644 }
645
646 if (preexec_fn != Py_None) {
647 preexec_fn_args_tuple = PyTuple_New(0);
648 if (!preexec_fn_args_tuple)
649 goto cleanup;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200650#ifdef WITH_THREAD
Victor Stinner0e59cc32010-04-16 23:49:32 +0000651 _PyImport_AcquireLock();
Victor Stinner8f437aa2014-10-05 17:25:19 +0200652 import_lock_held = 1;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200653#endif
Victor Stinner0e59cc32010-04-16 23:49:32 +0000654 }
655
656 if (cwd_obj != Py_None) {
657 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
658 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000659 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000660 } else {
661 cwd = NULL;
662 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000663 }
664
665 pid = fork();
666 if (pid == 0) {
667 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000668 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000669 * Code from here to _exit() must only use async-signal-safe functions,
670 * listed at `man 7 signal` or
671 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
672 */
673
674 if (preexec_fn != Py_None) {
675 /* We'll be calling back into Python later so we need to do this.
676 * This call may not be async-signal-safe but neither is calling
677 * back into Python. The user asked us to use hope as a strategy
678 * to avoid deadlock... */
679 PyOS_AfterFork();
680 }
681
682 child_exec(exec_array, argv, envp, cwd,
683 p2cread, p2cwrite, c2pread, c2pwrite,
684 errread, errwrite, errpipe_read, errpipe_write,
685 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800686 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000687 _exit(255);
688 return NULL; /* Dead code to avoid a potential compiler warning. */
689 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000690 Py_XDECREF(cwd_obj2);
691
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000692 if (pid == -1) {
693 /* Capture the errno exception before errno can be clobbered. */
694 PyErr_SetFromErrno(PyExc_OSError);
695 }
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200696#ifdef WITH_THREAD
697 if (preexec_fn != Py_None
698 && _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000699 PyErr_SetString(PyExc_RuntimeError,
700 "not holding the import lock");
701 }
Victor Stinner8f437aa2014-10-05 17:25:19 +0200702 import_lock_held = 0;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200703#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000704
705 /* Parent process */
706 if (envp)
707 _Py_FreeCharPArray(envp);
708 if (argv)
709 _Py_FreeCharPArray(argv);
710 _Py_FreeCharPArray(exec_array);
711
712 /* Reenable gc in the parent process (or if fork failed). */
713 if (need_to_reenable_gc && _enable_gc(gc_module)) {
714 Py_XDECREF(gc_module);
715 return NULL;
716 }
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 Stinnerbc5b80b2015-10-11 09:54:42 +0200726#ifdef WITH_THREAD
Victor Stinner8f437aa2014-10-05 17:25:19 +0200727 if (import_lock_held)
728 _PyImport_ReleaseLock();
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200729#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000730 if (envp)
731 _Py_FreeCharPArray(envp);
732 if (argv)
733 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200734 if (exec_array)
735 _Py_FreeCharPArray(exec_array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000736 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000737 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000738 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000739
740 /* Reenable gc if it was disabled. */
Victor Stinner8f437aa2014-10-05 17:25:19 +0200741 if (need_to_reenable_gc) {
742 PyObject *exctype, *val, *tb;
743 PyErr_Fetch(&exctype, &val, &tb);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000744 _enable_gc(gc_module);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200745 PyErr_Restore(exctype, val, tb);
746 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000747 Py_XDECREF(gc_module);
748 return NULL;
749}
750
751
752PyDoc_STRVAR(subprocess_fork_exec_doc,
753"fork_exec(args, executable_list, close_fds, cwd, env,\n\
754 p2cread, p2cwrite, c2pread, c2pwrite,\n\
755 errread, errwrite, errpipe_read, errpipe_write,\n\
756 restore_signals, call_setsid, preexec_fn)\n\
757\n\
758Forks a child process, closes parent file descriptors as appropriate in the\n\
759child and dups the few that are needed before calling exec() in the child\n\
760process.\n\
761\n\
762The preexec_fn, if supplied, will be called immediately before exec.\n\
763WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
764 It may trigger infrequent, difficult to debug deadlocks.\n\
765\n\
766If an error occurs in the child process before the exec, it is\n\
767serialized and written to the errpipe_write fd per subprocess.py.\n\
768\n\
769Returns: the child process's PID.\n\
770\n\
771Raises: Only on an error in the parent process.\n\
772");
773
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000774/* module level code ********************************************************/
775
776PyDoc_STRVAR(module_doc,
777"A POSIX helper for the subprocess module.");
778
779
780static PyMethodDef module_methods[] = {
781 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000782 {NULL, NULL} /* sentinel */
783};
784
785
786static struct PyModuleDef _posixsubprocessmodule = {
787 PyModuleDef_HEAD_INIT,
788 "_posixsubprocess",
789 module_doc,
790 -1, /* No memory is needed. */
791 module_methods,
792};
793
794PyMODINIT_FUNC
795PyInit__posixsubprocess(void)
796{
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000797 return PyModule_Create(&_posixsubprocessmodule);
798}