Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1 | /* Authors: Gregory P. Smith & Jeffrey Yasskin */ |
| 2 | #include "Python.h" |
Victor Stinner | 5572ba7 | 2011-05-26 14:10:08 +0200 | [diff] [blame] | 3 | #if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE) |
| 4 | # define _GNU_SOURCE |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 5 | #endif |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 6 | #include <unistd.h> |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 7 | #include <fcntl.h> |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 8 | #ifdef HAVE_SYS_TYPES_H |
| 9 | #include <sys/types.h> |
| 10 | #endif |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 11 | #if defined(HAVE_SYS_STAT_H) && defined(__FreeBSD__) |
| 12 | #include <sys/stat.h> |
| 13 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 14 | #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. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 20 | |
Gregory P. Smith | efeb9da | 2014-04-14 13:31:21 -0700 | [diff] [blame] | 21 | #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. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 27 | #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. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 32 | /* Some versions of Solaris lack dirfd(). */ |
Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 33 | # define dirfd(dirp) ((dirp)->dd_fd) |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 34 | # define HAVE_DIRFD |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 35 | # endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 36 | #endif |
| 37 | |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 38 | #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. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 43 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 44 | #define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 45 | |
| 46 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 47 | /* Given the gc module call gc.enable() and return 0 on success. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 48 | static int |
| 49 | _enable_gc(PyObject *gc_module) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 50 | { |
| 51 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 52 | _Py_IDENTIFIER(enable); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 53 | |
| 54 | result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 55 | if (result == NULL) |
| 56 | return 1; |
| 57 | Py_DECREF(result); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 62 | /* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 63 | static int |
| 64 | _pos_int_from_ascii(char *name) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 65 | { |
| 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. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 77 | #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 Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 84 | static int |
Ross Lagerwall | 7f4fdb2 | 2012-03-07 20:06:33 +0200 | [diff] [blame] | 85 | _is_fdescfs_mounted_on_dev_fd(void) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 86 | { |
| 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 Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 92 | return 0; |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 93 | 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. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 100 | /* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 101 | static int |
| 102 | _sanity_check_python_fd_sequence(PyObject *fd_sequence) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 103 | { |
| 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 Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 119 | static int |
| 120 | _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 121 | { |
| 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 Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 141 | static int |
| 142 | make_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. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 164 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 165 | /* 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 | */ |
| 168 | static long |
| 169 | safe_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. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 193 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 194 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 195 | _close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 196 | { |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 197 | long end_fd = safe_get_max_fd(); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 198 | 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. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 229 | struct linux_dirent64 { |
Gregory P. Smith | 58f07a9 | 2012-06-05 13:26:39 -0700 | [diff] [blame] | 230 | unsigned long long d_ino; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 231 | long long d_off; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 232 | unsigned short d_reclen; /* Length of this linux_dirent */ |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 233 | unsigned char d_type; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 234 | char d_name[256]; /* Filename (null-terminated) */ |
| 235 | }; |
| 236 | |
Gregory P. Smith | a26987a | 2014-06-01 13:46:36 -0700 | [diff] [blame] | 237 | /* 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. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 239 | * |
| 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 Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 252 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 253 | _close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 254 | { |
| 255 | int fd_dir_fd; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 256 | |
| 257 | fd_dir_fd = _Py_open(FD_DIR, O_RDONLY); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 258 | if (fd_dir_fd == -1) { |
| 259 | /* No way to get a list of open fds. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 260 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 261 | return; |
| 262 | } else { |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 263 | char buffer[sizeof(struct linux_dirent64)]; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 264 | int bytes; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 265 | while ((bytes = syscall(SYS_getdents64, fd_dir_fd, |
| 266 | (struct linux_dirent64 *)buffer, |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 267 | sizeof(buffer))) > 0) { |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 268 | struct linux_dirent64 *entry; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 269 | int offset; |
| 270 | for (offset = 0; offset < bytes; offset += entry->d_reclen) { |
| 271 | int fd; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 272 | entry = (struct linux_dirent64 *)(buffer + offset); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 273 | if ((fd = _pos_int_from_ascii(entry->d_name)) < 0) |
| 274 | continue; /* Not a number. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 275 | if (fd != fd_dir_fd && fd >= start_fd && |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 276 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { |
| 277 | while (close(fd) < 0 && errno == EINTR); |
| 278 | } |
| 279 | } |
| 280 | } |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 281 | while (close(fd_dir_fd) < 0 && errno == EINTR); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 285 | #define _close_open_fds _close_open_fds_safe |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 286 | |
| 287 | #else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ |
| 288 | |
| 289 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 290 | /* Close all open file descriptors from start_fd and higher. |
| 291 | * Do not close any in the sorted py_fds_to_keep list. |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 292 | * |
| 293 | * This function violates the strict use of async signal safe functions. :( |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 294 | * It calls opendir(), readdir() and closedir(). Of these, the one most |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 295 | * likely to ever cause a problem is opendir() as it performs an internal |
| 296 | * malloc(). Practically this should not be a problem. The Java VM makes the |
| 297 | * same calls between fork and exec in its own UNIXProcess_md.c implementation. |
| 298 | * |
| 299 | * readdir_r() is not used because it provides no benefit. It is typically |
| 300 | * implemented as readdir() followed by memcpy(). See also: |
| 301 | * http://womble.decadent.org.uk/readdir_r-advisory.html |
| 302 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 303 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 304 | _close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 305 | { |
| 306 | DIR *proc_fd_dir; |
| 307 | #ifndef HAVE_DIRFD |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 308 | while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) { |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 309 | ++start_fd; |
| 310 | } |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 311 | /* Close our lowest fd before we call opendir so that it is likely to |
| 312 | * reuse that fd otherwise we might close opendir's file descriptor in |
| 313 | * our loop. This trick assumes that fd's are allocated on a lowest |
| 314 | * available basis. */ |
| 315 | while (close(start_fd) < 0 && errno == EINTR); |
| 316 | ++start_fd; |
| 317 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 318 | |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 319 | #if defined(__FreeBSD__) |
| 320 | if (!_is_fdescfs_mounted_on_dev_fd()) |
| 321 | proc_fd_dir = NULL; |
| 322 | else |
| 323 | #endif |
| 324 | proc_fd_dir = opendir(FD_DIR); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 325 | if (!proc_fd_dir) { |
| 326 | /* No way to get a list of open fds. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 327 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 328 | } else { |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 329 | struct dirent *dir_entry; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 330 | #ifdef HAVE_DIRFD |
Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 331 | int fd_used_by_opendir = dirfd(proc_fd_dir); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 332 | #else |
| 333 | int fd_used_by_opendir = start_fd - 1; |
| 334 | #endif |
| 335 | errno = 0; |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 336 | while ((dir_entry = readdir(proc_fd_dir))) { |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 337 | int fd; |
| 338 | if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0) |
| 339 | continue; /* Not a number. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 340 | if (fd != fd_used_by_opendir && fd >= start_fd && |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 341 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { |
| 342 | while (close(fd) < 0 && errno == EINTR); |
| 343 | } |
| 344 | errno = 0; |
| 345 | } |
| 346 | if (errno) { |
| 347 | /* readdir error, revert behavior. Highly Unlikely. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 348 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 349 | } |
| 350 | closedir(proc_fd_dir); |
| 351 | } |
| 352 | } |
| 353 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 354 | #define _close_open_fds _close_open_fds_maybe_unsafe |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 355 | |
| 356 | #endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ |
| 357 | |
| 358 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 359 | /* |
| 360 | * This function is code executed in the child process immediately after fork |
| 361 | * to set things up and call exec(). |
| 362 | * |
| 363 | * All of the code in this function must only use async-signal-safe functions, |
| 364 | * listed at `man 7 signal` or |
| 365 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 366 | * |
| 367 | * This restriction is documented at |
| 368 | * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html. |
| 369 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 370 | static void |
| 371 | child_exec(char *const exec_array[], |
| 372 | char *const argv[], |
| 373 | char *const envp[], |
| 374 | const char *cwd, |
| 375 | int p2cread, int p2cwrite, |
| 376 | int c2pread, int c2pwrite, |
| 377 | int errread, int errwrite, |
| 378 | int errpipe_read, int errpipe_write, |
| 379 | int close_fds, int restore_signals, |
| 380 | int call_setsid, |
| 381 | PyObject *py_fds_to_keep, |
| 382 | PyObject *preexec_fn, |
| 383 | PyObject *preexec_fn_args_tuple) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 384 | { |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 385 | int i, saved_errno, unused, reached_preexec = 0; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 386 | PyObject *result; |
Gregory P. Smith | 14affb8 | 2010-12-22 05:22:17 +0000 | [diff] [blame] | 387 | const char* err_msg = ""; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 388 | /* Buffer large enough to hold a hex integer. We can't malloc. */ |
| 389 | char hex_errno[sizeof(saved_errno)*2+1]; |
| 390 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 391 | if (make_inheritable(py_fds_to_keep, errpipe_write) < 0) |
| 392 | goto error; |
| 393 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 394 | /* Close parent's pipe ends. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 395 | if (p2cwrite != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 396 | POSIX_CALL(close(p2cwrite)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 397 | if (c2pread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 398 | POSIX_CALL(close(c2pread)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 399 | if (errread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 400 | POSIX_CALL(close(errread)); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 401 | POSIX_CALL(close(errpipe_read)); |
| 402 | |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 403 | /* When duping fds, if there arises a situation where one of the fds is |
| 404 | either 0, 1 or 2, it is possible that it is overwritten (#12607). */ |
| 405 | if (c2pwrite == 0) |
| 406 | POSIX_CALL(c2pwrite = dup(c2pwrite)); |
| 407 | if (errwrite == 0 || errwrite == 1) |
| 408 | POSIX_CALL(errwrite = dup(errwrite)); |
| 409 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 410 | /* Dup fds for child. |
| 411 | dup2() removes the CLOEXEC flag but we must do it ourselves if dup2() |
| 412 | would be a no-op (issue #10806). */ |
| 413 | if (p2cread == 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 414 | if (_Py_set_inheritable(p2cread, 1, NULL) < 0) |
| 415 | goto error; |
| 416 | } |
| 417 | else if (p2cread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 418 | POSIX_CALL(dup2(p2cread, 0)); /* stdin */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 419 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 420 | if (c2pwrite == 1) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 421 | if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0) |
| 422 | goto error; |
| 423 | } |
| 424 | else if (c2pwrite != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 425 | POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 426 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 427 | if (errwrite == 2) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 428 | if (_Py_set_inheritable(errwrite, 1, NULL) < 0) |
| 429 | goto error; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 430 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 431 | else if (errwrite != -1) |
| 432 | POSIX_CALL(dup2(errwrite, 2)); /* stderr */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 433 | |
| 434 | /* Close pipe fds. Make sure we don't close the same fd more than */ |
| 435 | /* once, or standard fds. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 436 | if (p2cread > 2) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 437 | POSIX_CALL(close(p2cread)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 438 | if (c2pwrite > 2 && c2pwrite != p2cread) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 439 | POSIX_CALL(close(c2pwrite)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 440 | if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 441 | POSIX_CALL(close(errwrite)); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 442 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 443 | if (cwd) |
| 444 | POSIX_CALL(chdir(cwd)); |
| 445 | |
| 446 | if (restore_signals) |
| 447 | _Py_RestoreSignals(); |
| 448 | |
| 449 | #ifdef HAVE_SETSID |
| 450 | if (call_setsid) |
| 451 | POSIX_CALL(setsid()); |
| 452 | #endif |
| 453 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 454 | reached_preexec = 1; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 455 | if (preexec_fn != Py_None && preexec_fn_args_tuple) { |
| 456 | /* This is where the user has asked us to deadlock their program. */ |
| 457 | result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL); |
| 458 | if (result == NULL) { |
| 459 | /* Stringifying the exception or traceback would involve |
| 460 | * memory allocation and thus potential for deadlock. |
| 461 | * We've already faced potential deadlock by calling back |
| 462 | * into Python in the first place, so it probably doesn't |
| 463 | * matter but we avoid it to minimize the possibility. */ |
| 464 | err_msg = "Exception occurred in preexec_fn."; |
| 465 | errno = 0; /* We don't want to report an OSError. */ |
| 466 | goto error; |
| 467 | } |
| 468 | /* Py_DECREF(result); - We're about to exec so why bother? */ |
| 469 | } |
| 470 | |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 471 | /* close FDs after executing preexec_fn, which might open FDs */ |
| 472 | if (close_fds) { |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 473 | /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 474 | _close_open_fds(3, py_fds_to_keep); |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 477 | /* This loop matches the Lib/os.py _execvpe()'s PATH search when */ |
| 478 | /* given the executable_list generated by Lib/subprocess.py. */ |
| 479 | saved_errno = 0; |
| 480 | for (i = 0; exec_array[i] != NULL; ++i) { |
| 481 | const char *executable = exec_array[i]; |
| 482 | if (envp) { |
| 483 | execve(executable, argv, envp); |
| 484 | } else { |
| 485 | execv(executable, argv); |
| 486 | } |
| 487 | if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) { |
| 488 | saved_errno = errno; |
| 489 | } |
| 490 | } |
| 491 | /* Report the first exec error, not the last. */ |
| 492 | if (saved_errno) |
| 493 | errno = saved_errno; |
| 494 | |
| 495 | error: |
| 496 | saved_errno = errno; |
| 497 | /* Report the posix error to our parent process. */ |
Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 498 | /* We ignore all write() return values as the total size of our writes is |
| 499 | * less than PIPEBUF and we cannot do anything about an error anyways. */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 500 | if (saved_errno) { |
| 501 | char *cur; |
Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 502 | unused = write(errpipe_write, "OSError:", 8); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 503 | cur = hex_errno + sizeof(hex_errno); |
| 504 | while (saved_errno != 0 && cur > hex_errno) { |
| 505 | *--cur = "0123456789ABCDEF"[saved_errno % 16]; |
| 506 | saved_errno /= 16; |
| 507 | } |
Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 508 | unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur); |
| 509 | unused = write(errpipe_write, ":", 1); |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 510 | if (!reached_preexec) { |
| 511 | /* Indicate to the parent that the error happened before exec(). */ |
| 512 | unused = write(errpipe_write, "noexec", 6); |
| 513 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 514 | /* We can't call strerror(saved_errno). It is not async signal safe. |
| 515 | * The parent process will look the error message up. */ |
| 516 | } else { |
Gregory P. Smith | 8d07c26 | 2012-11-10 23:53:47 -0800 | [diff] [blame] | 517 | unused = write(errpipe_write, "SubprocessError:0:", 18); |
Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 518 | unused = write(errpipe_write, err_msg, strlen(err_msg)); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 519 | } |
Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 520 | if (unused) return; /* silly? yes! avoids gcc compiler warning. */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
| 524 | static PyObject * |
| 525 | subprocess_fork_exec(PyObject* self, PyObject *args) |
| 526 | { |
| 527 | PyObject *gc_module = NULL; |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 528 | PyObject *executable_list, *py_fds_to_keep; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 529 | PyObject *env_list, *preexec_fn; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 530 | PyObject *process_args, *converted_args = NULL, *fast_args = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 531 | PyObject *preexec_fn_args_tuple = NULL; |
| 532 | int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite; |
| 533 | int errpipe_read, errpipe_write, close_fds, restore_signals; |
| 534 | int call_setsid; |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 535 | PyObject *cwd_obj, *cwd_obj2; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 536 | const char *cwd; |
| 537 | pid_t pid; |
| 538 | int need_to_reenable_gc = 0; |
| 539 | char *const *exec_array, *const *argv = NULL, *const *envp = NULL; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 540 | Py_ssize_t arg_num; |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 541 | int import_lock_held = 0; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 542 | |
| 543 | if (!PyArg_ParseTuple( |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 544 | args, "OOpOOOiiiiiiiiiiO:fork_exec", |
| 545 | &process_args, &executable_list, &close_fds, &py_fds_to_keep, |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 546 | &cwd_obj, &env_list, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 547 | &p2cread, &p2cwrite, &c2pread, &c2pwrite, |
| 548 | &errread, &errwrite, &errpipe_read, &errpipe_write, |
| 549 | &restore_signals, &call_setsid, &preexec_fn)) |
| 550 | return NULL; |
| 551 | |
Gregory P. Smith | 361e30c | 2013-12-01 00:12:24 -0800 | [diff] [blame] | 552 | if (close_fds && errpipe_write < 3) { /* precondition */ |
| 553 | PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3"); |
| 554 | return NULL; |
| 555 | } |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 556 | if (PySequence_Length(py_fds_to_keep) < 0) { |
| 557 | PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep"); |
| 558 | return NULL; |
| 559 | } |
| 560 | if (_sanity_check_python_fd_sequence(py_fds_to_keep)) { |
| 561 | PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep"); |
Gregory P. Smith | d4cc7bf | 2010-12-04 11:22:11 +0000 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 564 | |
| 565 | /* We need to call gc.disable() when we'll be calling preexec_fn */ |
| 566 | if (preexec_fn != Py_None) { |
| 567 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 568 | _Py_IDENTIFIER(isenabled); |
| 569 | _Py_IDENTIFIER(disable); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 570 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 571 | gc_module = PyImport_ImportModule("gc"); |
| 572 | if (gc_module == NULL) |
| 573 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 574 | result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 575 | if (result == NULL) { |
| 576 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 577 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 578 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 579 | need_to_reenable_gc = PyObject_IsTrue(result); |
| 580 | Py_DECREF(result); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 581 | if (need_to_reenable_gc == -1) { |
| 582 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 583 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 584 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 585 | result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 586 | if (result == NULL) { |
| 587 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 588 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 589 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 590 | Py_DECREF(result); |
| 591 | } |
| 592 | |
| 593 | exec_array = _PySequence_BytesToCharpArray(executable_list); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 594 | if (!exec_array) |
| 595 | goto cleanup; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 596 | |
| 597 | /* Convert args and env into appropriate arguments for exec() */ |
| 598 | /* These conversions are done in the parent process to avoid allocating |
| 599 | or freeing memory in the child process. */ |
| 600 | if (process_args != Py_None) { |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 601 | Py_ssize_t num_args; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 602 | /* Equivalent to: */ |
| 603 | /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */ |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 604 | fast_args = PySequence_Fast(process_args, "argv must be a tuple"); |
Stefan Krah | db579d7 | 2012-08-20 14:36:47 +0200 | [diff] [blame] | 605 | if (fast_args == NULL) |
| 606 | goto cleanup; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 607 | num_args = PySequence_Fast_GET_SIZE(fast_args); |
| 608 | converted_args = PyTuple_New(num_args); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 609 | if (converted_args == NULL) |
| 610 | goto cleanup; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 611 | for (arg_num = 0; arg_num < num_args; ++arg_num) { |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 612 | PyObject *borrowed_arg, *converted_arg; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 613 | borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 614 | if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0) |
| 615 | goto cleanup; |
| 616 | PyTuple_SET_ITEM(converted_args, arg_num, converted_arg); |
| 617 | } |
| 618 | |
| 619 | argv = _PySequence_BytesToCharpArray(converted_args); |
| 620 | Py_CLEAR(converted_args); |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 621 | Py_CLEAR(fast_args); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 622 | if (!argv) |
| 623 | goto cleanup; |
| 624 | } |
| 625 | |
| 626 | if (env_list != Py_None) { |
| 627 | envp = _PySequence_BytesToCharpArray(env_list); |
| 628 | if (!envp) |
| 629 | goto cleanup; |
| 630 | } |
| 631 | |
| 632 | if (preexec_fn != Py_None) { |
| 633 | preexec_fn_args_tuple = PyTuple_New(0); |
| 634 | if (!preexec_fn_args_tuple) |
| 635 | goto cleanup; |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 636 | _PyImport_AcquireLock(); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 637 | import_lock_held = 1; |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if (cwd_obj != Py_None) { |
| 641 | if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0) |
| 642 | goto cleanup; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 643 | cwd = PyBytes_AsString(cwd_obj2); |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 644 | } else { |
| 645 | cwd = NULL; |
| 646 | cwd_obj2 = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | pid = fork(); |
| 650 | if (pid == 0) { |
| 651 | /* Child process */ |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 652 | /* |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 653 | * Code from here to _exit() must only use async-signal-safe functions, |
| 654 | * listed at `man 7 signal` or |
| 655 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 656 | */ |
| 657 | |
| 658 | if (preexec_fn != Py_None) { |
| 659 | /* We'll be calling back into Python later so we need to do this. |
| 660 | * This call may not be async-signal-safe but neither is calling |
| 661 | * back into Python. The user asked us to use hope as a strategy |
| 662 | * to avoid deadlock... */ |
| 663 | PyOS_AfterFork(); |
| 664 | } |
| 665 | |
| 666 | child_exec(exec_array, argv, envp, cwd, |
| 667 | p2cread, p2cwrite, c2pread, c2pwrite, |
| 668 | errread, errwrite, errpipe_read, errpipe_write, |
| 669 | close_fds, restore_signals, call_setsid, |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 670 | py_fds_to_keep, preexec_fn, preexec_fn_args_tuple); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 671 | _exit(255); |
| 672 | return NULL; /* Dead code to avoid a potential compiler warning. */ |
| 673 | } |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 674 | Py_XDECREF(cwd_obj2); |
| 675 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 676 | if (pid == -1) { |
| 677 | /* Capture the errno exception before errno can be clobbered. */ |
| 678 | PyErr_SetFromErrno(PyExc_OSError); |
| 679 | } |
| 680 | if (preexec_fn != Py_None && |
| 681 | _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) { |
| 682 | PyErr_SetString(PyExc_RuntimeError, |
| 683 | "not holding the import lock"); |
| 684 | } |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 685 | import_lock_held = 0; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 686 | |
| 687 | /* Parent process */ |
| 688 | if (envp) |
| 689 | _Py_FreeCharPArray(envp); |
| 690 | if (argv) |
| 691 | _Py_FreeCharPArray(argv); |
| 692 | _Py_FreeCharPArray(exec_array); |
| 693 | |
| 694 | /* Reenable gc in the parent process (or if fork failed). */ |
| 695 | if (need_to_reenable_gc && _enable_gc(gc_module)) { |
| 696 | Py_XDECREF(gc_module); |
| 697 | return NULL; |
| 698 | } |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 699 | Py_XDECREF(preexec_fn_args_tuple); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 700 | Py_XDECREF(gc_module); |
| 701 | |
| 702 | if (pid == -1) |
| 703 | return NULL; /* fork() failed. Exception set earlier. */ |
| 704 | |
| 705 | return PyLong_FromPid(pid); |
| 706 | |
| 707 | cleanup: |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 708 | if (import_lock_held) |
| 709 | _PyImport_ReleaseLock(); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 710 | if (envp) |
| 711 | _Py_FreeCharPArray(envp); |
| 712 | if (argv) |
| 713 | _Py_FreeCharPArray(argv); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 714 | if (exec_array) |
| 715 | _Py_FreeCharPArray(exec_array); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 716 | Py_XDECREF(converted_args); |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 717 | Py_XDECREF(fast_args); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 718 | Py_XDECREF(preexec_fn_args_tuple); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 719 | |
| 720 | /* Reenable gc if it was disabled. */ |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 721 | if (need_to_reenable_gc) { |
| 722 | PyObject *exctype, *val, *tb; |
| 723 | PyErr_Fetch(&exctype, &val, &tb); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 724 | _enable_gc(gc_module); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 725 | PyErr_Restore(exctype, val, tb); |
| 726 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 727 | Py_XDECREF(gc_module); |
| 728 | return NULL; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | PyDoc_STRVAR(subprocess_fork_exec_doc, |
| 733 | "fork_exec(args, executable_list, close_fds, cwd, env,\n\ |
| 734 | p2cread, p2cwrite, c2pread, c2pwrite,\n\ |
| 735 | errread, errwrite, errpipe_read, errpipe_write,\n\ |
| 736 | restore_signals, call_setsid, preexec_fn)\n\ |
| 737 | \n\ |
| 738 | Forks a child process, closes parent file descriptors as appropriate in the\n\ |
| 739 | child and dups the few that are needed before calling exec() in the child\n\ |
| 740 | process.\n\ |
| 741 | \n\ |
| 742 | The preexec_fn, if supplied, will be called immediately before exec.\n\ |
| 743 | WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\ |
| 744 | It may trigger infrequent, difficult to debug deadlocks.\n\ |
| 745 | \n\ |
| 746 | If an error occurs in the child process before the exec, it is\n\ |
| 747 | serialized and written to the errpipe_write fd per subprocess.py.\n\ |
| 748 | \n\ |
| 749 | Returns: the child process's PID.\n\ |
| 750 | \n\ |
| 751 | Raises: Only on an error in the parent process.\n\ |
| 752 | "); |
| 753 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 754 | /* module level code ********************************************************/ |
| 755 | |
| 756 | PyDoc_STRVAR(module_doc, |
| 757 | "A POSIX helper for the subprocess module."); |
| 758 | |
| 759 | |
| 760 | static PyMethodDef module_methods[] = { |
| 761 | {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc}, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 762 | {NULL, NULL} /* sentinel */ |
| 763 | }; |
| 764 | |
| 765 | |
| 766 | static struct PyModuleDef _posixsubprocessmodule = { |
| 767 | PyModuleDef_HEAD_INIT, |
| 768 | "_posixsubprocess", |
| 769 | module_doc, |
| 770 | -1, /* No memory is needed. */ |
| 771 | module_methods, |
| 772 | }; |
| 773 | |
| 774 | PyMODINIT_FUNC |
| 775 | PyInit__posixsubprocess(void) |
| 776 | { |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 777 | return PyModule_Create(&_posixsubprocessmodule); |
| 778 | } |