| 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 | 
| Gregory P. Smith | f968177 | 2015-04-25 23:43:34 -0700 | [diff] [blame] | 17 | #if defined(HAVE_SYS_RESOURCE_H) | 
|  | 18 | #include <sys/resource.h> | 
|  | 19 | #endif | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 20 | #ifdef HAVE_DIRENT_H | 
|  | 21 | #include <dirent.h> | 
|  | 22 | #endif | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 23 |  | 
| Gregory P. Smith | efeb9da | 2014-04-14 13:31:21 -0700 | [diff] [blame] | 24 | #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. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 30 | #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. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 35 | /* Some versions of Solaris lack dirfd(). */ | 
| Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 36 | #  define dirfd(dirp) ((dirp)->dd_fd) | 
| Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 37 | #  define HAVE_DIRFD | 
| Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 38 | # endif | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 39 | #endif | 
|  | 40 |  | 
| Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 41 | #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. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 46 |  | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 47 | #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] | 48 |  | 
|  | 49 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 50 | /* Given the gc module call gc.enable() and return 0 on success. */ | 
| Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 51 | static int | 
|  | 52 | _enable_gc(PyObject *gc_module) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 53 | { | 
|  | 54 | PyObject *result; | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 55 | _Py_IDENTIFIER(enable); | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 56 |  | 
|  | 57 | result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 58 | if (result == NULL) | 
|  | 59 | return 1; | 
|  | 60 | Py_DECREF(result); | 
|  | 61 | return 0; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 |  | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 65 | /* 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] | 66 | static int | 
|  | 67 | _pos_int_from_ascii(char *name) | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 68 | { | 
|  | 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. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 80 | #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 Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 87 | static int | 
| Ross Lagerwall | 7f4fdb2 | 2012-03-07 20:06:33 +0200 | [diff] [blame] | 88 | _is_fdescfs_mounted_on_dev_fd(void) | 
| Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 89 | { | 
|  | 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 Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 95 | return 0; | 
| Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 96 | 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. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 103 | /* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */ | 
| Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 104 | static int | 
|  | 105 | _sanity_check_python_fd_sequence(PyObject *fd_sequence) | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 106 | { | 
|  | 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); | 
|  | 112 | if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) { | 
|  | 113 | /* Negative, overflow, not a Long, unsorted, too big for a fd. */ | 
|  | 114 | return 1; | 
|  | 115 | } | 
|  | 116 | } | 
|  | 117 | return 0; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 |  | 
|  | 121 | /* Is fd found in the sorted Python Sequence? */ | 
| Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 122 | static int | 
|  | 123 | _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence) | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 124 | { | 
|  | 125 | /* Binary search. */ | 
|  | 126 | Py_ssize_t search_min = 0; | 
|  | 127 | Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1; | 
|  | 128 | if (search_max < 0) | 
|  | 129 | return 0; | 
|  | 130 | do { | 
|  | 131 | long middle = (search_min + search_max) / 2; | 
|  | 132 | long middle_fd = PyLong_AsLong( | 
|  | 133 | PySequence_Fast_GET_ITEM(fd_sequence, middle)); | 
|  | 134 | if (fd == middle_fd) | 
|  | 135 | return 1; | 
|  | 136 | if (fd > middle_fd) | 
|  | 137 | search_min = middle + 1; | 
|  | 138 | else | 
|  | 139 | search_max = middle - 1; | 
|  | 140 | } while (search_min <= search_max); | 
|  | 141 | return 0; | 
|  | 142 | } | 
|  | 143 |  | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 144 | static int | 
|  | 145 | make_inheritable(PyObject *py_fds_to_keep, int errpipe_write) | 
|  | 146 | { | 
|  | 147 | Py_ssize_t i, len; | 
|  | 148 |  | 
|  | 149 | len = PySequence_Length(py_fds_to_keep); | 
|  | 150 | for (i = 0; i < len; ++i) { | 
|  | 151 | PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i); | 
|  | 152 | long fd = PyLong_AsLong(fdobj); | 
|  | 153 | assert(!PyErr_Occurred()); | 
|  | 154 | assert(0 <= fd && fd <= INT_MAX); | 
|  | 155 | if (fd == errpipe_write) { | 
|  | 156 | /* errpipe_write is part of py_fds_to_keep. It must be closed at | 
|  | 157 | exec(), but kept open in the child process until exec() is | 
|  | 158 | called. */ | 
|  | 159 | continue; | 
|  | 160 | } | 
|  | 161 | if (_Py_set_inheritable((int)fd, 1, NULL) < 0) | 
|  | 162 | return -1; | 
|  | 163 | } | 
|  | 164 | return 0; | 
|  | 165 | } | 
|  | 166 |  | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 167 |  | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 168 | /* Get the maximum file descriptor that could be opened by this process. | 
|  | 169 | * This function is async signal safe for use between fork() and exec(). | 
|  | 170 | */ | 
|  | 171 | static long | 
|  | 172 | safe_get_max_fd(void) | 
|  | 173 | { | 
|  | 174 | long local_max_fd; | 
|  | 175 | #if defined(__NetBSD__) | 
|  | 176 | local_max_fd = fcntl(0, F_MAXFD); | 
|  | 177 | if (local_max_fd >= 0) | 
|  | 178 | return local_max_fd; | 
|  | 179 | #endif | 
| Gregory P. Smith | f968177 | 2015-04-25 23:43:34 -0700 | [diff] [blame] | 180 | #if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__) | 
|  | 181 | struct rlimit rl; | 
|  | 182 | /* Not on the POSIX async signal safe functions list but likely | 
|  | 183 | * safe.  TODO - Someone should audit OpenBSD to make sure. */ | 
|  | 184 | if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) | 
|  | 185 | return (long) rl.rlim_max; | 
|  | 186 | #endif | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 187 | #ifdef _SC_OPEN_MAX | 
|  | 188 | local_max_fd = sysconf(_SC_OPEN_MAX); | 
|  | 189 | if (local_max_fd == -1) | 
|  | 190 | #endif | 
|  | 191 | local_max_fd = 256;  /* Matches legacy Lib/subprocess.py behavior. */ | 
|  | 192 | return local_max_fd; | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 |  | 
|  | 196 | /* Close all file descriptors in the range from start_fd and higher | 
|  | 197 | * except for those in py_fds_to_keep.  If the range defined by | 
|  | 198 | * [start_fd, safe_get_max_fd()) is large this will take a long | 
|  | 199 | * time as it calls close() on EVERY possible fd. | 
|  | 200 | * | 
|  | 201 | * It isn't possible to know for sure what the max fd to go up to | 
|  | 202 | * is for processes with the capability of raising their maximum. | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 203 | */ | 
| Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 204 | static void | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 205 | _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] | 206 | { | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 207 | long end_fd = safe_get_max_fd(); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 208 | Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep); | 
|  | 209 | Py_ssize_t keep_seq_idx; | 
|  | 210 | int fd_num; | 
|  | 211 | /* As py_fds_to_keep is sorted we can loop through the list closing | 
|  | 212 | * fds inbetween any in the keep list falling within our range. */ | 
|  | 213 | for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) { | 
|  | 214 | PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep, | 
|  | 215 | keep_seq_idx); | 
|  | 216 | int keep_fd = PyLong_AsLong(py_keep_fd); | 
|  | 217 | if (keep_fd < start_fd) | 
|  | 218 | continue; | 
|  | 219 | for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) { | 
| Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 220 | close(fd_num); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 221 | } | 
|  | 222 | start_fd = keep_fd + 1; | 
|  | 223 | } | 
|  | 224 | if (start_fd <= end_fd) { | 
|  | 225 | for (fd_num = start_fd; fd_num < end_fd; ++fd_num) { | 
| Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 226 | close(fd_num); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 227 | } | 
|  | 228 | } | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 |  | 
|  | 232 | #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) | 
|  | 233 | /* It doesn't matter if d_name has room for NAME_MAX chars; we're using this | 
|  | 234 | * only to read a directory of short file descriptor number names.  The kernel | 
|  | 235 | * will return an error if we didn't give it enough space.  Highly Unlikely. | 
|  | 236 | * This structure is very old and stable: It will not change unless the kernel | 
|  | 237 | * chooses to break compatibility with all existing binaries.  Highly Unlikely. | 
|  | 238 | */ | 
| Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 239 | struct linux_dirent64 { | 
| Gregory P. Smith | 58f07a9 | 2012-06-05 13:26:39 -0700 | [diff] [blame] | 240 | unsigned long long d_ino; | 
| Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 241 | long long d_off; | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 242 | unsigned short d_reclen;     /* Length of this linux_dirent */ | 
| Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 243 | unsigned char  d_type; | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 244 | char           d_name[256];  /* Filename (null-terminated) */ | 
|  | 245 | }; | 
|  | 246 |  | 
| Gregory P. Smith | a26987a | 2014-06-01 13:46:36 -0700 | [diff] [blame] | 247 | /* Close all open file descriptors in the range from start_fd and higher | 
|  | 248 | * 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] | 249 | * | 
|  | 250 | * This version is async signal safe as it does not make any unsafe C library | 
|  | 251 | * calls, malloc calls or handle any locks.  It is _unfortunate_ to be forced | 
|  | 252 | * to resort to making a kernel system call directly but this is the ONLY api | 
|  | 253 | * available that does no harm.  opendir/readdir/closedir perform memory | 
|  | 254 | * allocation and locking so while they usually work they are not guaranteed | 
|  | 255 | * to (especially if you have replaced your malloc implementation).  A version | 
|  | 256 | * of this function that uses those can be found in the _maybe_unsafe variant. | 
|  | 257 | * | 
|  | 258 | * This is Linux specific because that is all I am ready to test it on.  It | 
|  | 259 | * should be easy to add OS specific dirent or dirent64 structures and modify | 
|  | 260 | * it with some cpp #define magic to work on other OSes as well if you want. | 
|  | 261 | */ | 
| Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 262 | static void | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 263 | _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] | 264 | { | 
|  | 265 | int fd_dir_fd; | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 266 |  | 
| Victor Stinner | 160e819 | 2015-03-30 02:18:31 +0200 | [diff] [blame] | 267 | fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 268 | if (fd_dir_fd == -1) { | 
|  | 269 | /* No way to get a list of open fds. */ | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 270 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 271 | return; | 
|  | 272 | } else { | 
| Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 273 | char buffer[sizeof(struct linux_dirent64)]; | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 274 | int bytes; | 
| Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 275 | while ((bytes = syscall(SYS_getdents64, fd_dir_fd, | 
|  | 276 | (struct linux_dirent64 *)buffer, | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 277 | sizeof(buffer))) > 0) { | 
| Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 278 | struct linux_dirent64 *entry; | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 279 | int offset; | 
|  | 280 | for (offset = 0; offset < bytes; offset += entry->d_reclen) { | 
|  | 281 | int fd; | 
| Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 282 | entry = (struct linux_dirent64 *)(buffer + offset); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 283 | if ((fd = _pos_int_from_ascii(entry->d_name)) < 0) | 
|  | 284 | continue;  /* Not a number. */ | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 285 | if (fd != fd_dir_fd && fd >= start_fd && | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 286 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { | 
| Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 287 | close(fd); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 288 | } | 
|  | 289 | } | 
|  | 290 | } | 
| Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 291 | close(fd_dir_fd); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 292 | } | 
|  | 293 | } | 
|  | 294 |  | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 295 | #define _close_open_fds _close_open_fds_safe | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 296 |  | 
|  | 297 | #else  /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ | 
|  | 298 |  | 
|  | 299 |  | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 300 | /* Close all open file descriptors from start_fd and higher. | 
|  | 301 | * 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] | 302 | * | 
|  | 303 | * This function violates the strict use of async signal safe functions. :( | 
| Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 304 | * It calls opendir(), readdir() and closedir().  Of these, the one most | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 305 | * likely to ever cause a problem is opendir() as it performs an internal | 
|  | 306 | * malloc().  Practically this should not be a problem.  The Java VM makes the | 
|  | 307 | * same calls between fork and exec in its own UNIXProcess_md.c implementation. | 
|  | 308 | * | 
|  | 309 | * readdir_r() is not used because it provides no benefit.  It is typically | 
|  | 310 | * implemented as readdir() followed by memcpy().  See also: | 
|  | 311 | *   http://womble.decadent.org.uk/readdir_r-advisory.html | 
|  | 312 | */ | 
| Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 313 | static void | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 314 | _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] | 315 | { | 
|  | 316 | DIR *proc_fd_dir; | 
|  | 317 | #ifndef HAVE_DIRFD | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 318 | 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] | 319 | ++start_fd; | 
|  | 320 | } | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 321 | /* Close our lowest fd before we call opendir so that it is likely to | 
|  | 322 | * reuse that fd otherwise we might close opendir's file descriptor in | 
|  | 323 | * our loop.  This trick assumes that fd's are allocated on a lowest | 
|  | 324 | * available basis. */ | 
| Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 325 | close(start_fd); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 326 | ++start_fd; | 
|  | 327 | #endif | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 328 |  | 
| Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 329 | #if defined(__FreeBSD__) | 
|  | 330 | if (!_is_fdescfs_mounted_on_dev_fd()) | 
|  | 331 | proc_fd_dir = NULL; | 
|  | 332 | else | 
|  | 333 | #endif | 
|  | 334 | proc_fd_dir = opendir(FD_DIR); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 335 | if (!proc_fd_dir) { | 
|  | 336 | /* No way to get a list of open fds. */ | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 337 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 338 | } else { | 
| Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 339 | struct dirent *dir_entry; | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 340 | #ifdef HAVE_DIRFD | 
| Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 341 | int fd_used_by_opendir = dirfd(proc_fd_dir); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 342 | #else | 
|  | 343 | int fd_used_by_opendir = start_fd - 1; | 
|  | 344 | #endif | 
|  | 345 | errno = 0; | 
| Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 346 | while ((dir_entry = readdir(proc_fd_dir))) { | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 347 | int fd; | 
|  | 348 | if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0) | 
|  | 349 | continue;  /* Not a number. */ | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 350 | if (fd != fd_used_by_opendir && fd >= start_fd && | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 351 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { | 
| Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 352 | close(fd); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 353 | } | 
|  | 354 | errno = 0; | 
|  | 355 | } | 
|  | 356 | if (errno) { | 
|  | 357 | /* readdir error, revert behavior. Highly Unlikely. */ | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 358 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 359 | } | 
|  | 360 | closedir(proc_fd_dir); | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 |  | 
| Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 364 | #define _close_open_fds _close_open_fds_maybe_unsafe | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 365 |  | 
|  | 366 | #endif  /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ | 
|  | 367 |  | 
|  | 368 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 369 | /* | 
|  | 370 | * This function is code executed in the child process immediately after fork | 
|  | 371 | * to set things up and call exec(). | 
|  | 372 | * | 
|  | 373 | * All of the code in this function must only use async-signal-safe functions, | 
|  | 374 | * listed at `man 7 signal` or | 
|  | 375 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. | 
|  | 376 | * | 
|  | 377 | * This restriction is documented at | 
|  | 378 | * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html. | 
|  | 379 | */ | 
| Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 380 | static void | 
|  | 381 | child_exec(char *const exec_array[], | 
|  | 382 | char *const argv[], | 
|  | 383 | char *const envp[], | 
|  | 384 | const char *cwd, | 
|  | 385 | int p2cread, int p2cwrite, | 
|  | 386 | int c2pread, int c2pwrite, | 
|  | 387 | int errread, int errwrite, | 
|  | 388 | int errpipe_read, int errpipe_write, | 
|  | 389 | int close_fds, int restore_signals, | 
|  | 390 | int call_setsid, | 
|  | 391 | PyObject *py_fds_to_keep, | 
|  | 392 | PyObject *preexec_fn, | 
|  | 393 | PyObject *preexec_fn_args_tuple) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 394 | { | 
| Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 395 | int i, saved_errno, reached_preexec = 0; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 396 | PyObject *result; | 
| Gregory P. Smith | 14affb8 | 2010-12-22 05:22:17 +0000 | [diff] [blame] | 397 | const char* err_msg = ""; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 398 | /* Buffer large enough to hold a hex integer.  We can't malloc. */ | 
|  | 399 | char hex_errno[sizeof(saved_errno)*2+1]; | 
|  | 400 |  | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 401 | if (make_inheritable(py_fds_to_keep, errpipe_write) < 0) | 
|  | 402 | goto error; | 
|  | 403 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 404 | /* Close parent's pipe ends. */ | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 405 | if (p2cwrite != -1) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 406 | POSIX_CALL(close(p2cwrite)); | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 407 | if (c2pread != -1) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 408 | POSIX_CALL(close(c2pread)); | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 409 | if (errread != -1) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 410 | POSIX_CALL(close(errread)); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 411 | POSIX_CALL(close(errpipe_read)); | 
|  | 412 |  | 
| Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 413 | /* When duping fds, if there arises a situation where one of the fds is | 
|  | 414 | either 0, 1 or 2, it is possible that it is overwritten (#12607). */ | 
|  | 415 | if (c2pwrite == 0) | 
|  | 416 | POSIX_CALL(c2pwrite = dup(c2pwrite)); | 
|  | 417 | if (errwrite == 0 || errwrite == 1) | 
|  | 418 | POSIX_CALL(errwrite = dup(errwrite)); | 
|  | 419 |  | 
| Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 420 | /* Dup fds for child. | 
|  | 421 | dup2() removes the CLOEXEC flag but we must do it ourselves if dup2() | 
|  | 422 | would be a no-op (issue #10806). */ | 
|  | 423 | if (p2cread == 0) { | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 424 | if (_Py_set_inheritable(p2cread, 1, NULL) < 0) | 
|  | 425 | goto error; | 
|  | 426 | } | 
|  | 427 | else if (p2cread != -1) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 428 | POSIX_CALL(dup2(p2cread, 0));  /* stdin */ | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 429 |  | 
| Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 430 | if (c2pwrite == 1) { | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 431 | if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0) | 
|  | 432 | goto error; | 
|  | 433 | } | 
|  | 434 | else if (c2pwrite != -1) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 435 | POSIX_CALL(dup2(c2pwrite, 1));  /* stdout */ | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 436 |  | 
| Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 437 | if (errwrite == 2) { | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 438 | if (_Py_set_inheritable(errwrite, 1, NULL) < 0) | 
|  | 439 | goto error; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 440 | } | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 441 | else if (errwrite != -1) | 
|  | 442 | POSIX_CALL(dup2(errwrite, 2));  /* stderr */ | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 443 |  | 
|  | 444 | /* Close pipe fds.  Make sure we don't close the same fd more than */ | 
|  | 445 | /* once, or standard fds. */ | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 446 | if (p2cread > 2) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 447 | POSIX_CALL(close(p2cread)); | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 448 | if (c2pwrite > 2 && c2pwrite != p2cread) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 449 | POSIX_CALL(close(c2pwrite)); | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 450 | if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 451 | POSIX_CALL(close(errwrite)); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 452 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 453 | if (cwd) | 
|  | 454 | POSIX_CALL(chdir(cwd)); | 
|  | 455 |  | 
|  | 456 | if (restore_signals) | 
|  | 457 | _Py_RestoreSignals(); | 
|  | 458 |  | 
|  | 459 | #ifdef HAVE_SETSID | 
|  | 460 | if (call_setsid) | 
|  | 461 | POSIX_CALL(setsid()); | 
|  | 462 | #endif | 
|  | 463 |  | 
| Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 464 | reached_preexec = 1; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 465 | if (preexec_fn != Py_None && preexec_fn_args_tuple) { | 
|  | 466 | /* This is where the user has asked us to deadlock their program. */ | 
|  | 467 | result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL); | 
|  | 468 | if (result == NULL) { | 
|  | 469 | /* Stringifying the exception or traceback would involve | 
|  | 470 | * memory allocation and thus potential for deadlock. | 
|  | 471 | * We've already faced potential deadlock by calling back | 
|  | 472 | * into Python in the first place, so it probably doesn't | 
|  | 473 | * matter but we avoid it to minimize the possibility. */ | 
|  | 474 | err_msg = "Exception occurred in preexec_fn."; | 
|  | 475 | errno = 0;  /* We don't want to report an OSError. */ | 
|  | 476 | goto error; | 
|  | 477 | } | 
|  | 478 | /* Py_DECREF(result); - We're about to exec so why bother? */ | 
|  | 479 | } | 
|  | 480 |  | 
| Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 481 | /* close FDs after executing preexec_fn, which might open FDs */ | 
|  | 482 | if (close_fds) { | 
| Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 483 | /* 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] | 484 | _close_open_fds(3, py_fds_to_keep); | 
| Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 485 | } | 
|  | 486 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 487 | /* This loop matches the Lib/os.py _execvpe()'s PATH search when */ | 
|  | 488 | /* given the executable_list generated by Lib/subprocess.py.     */ | 
|  | 489 | saved_errno = 0; | 
|  | 490 | for (i = 0; exec_array[i] != NULL; ++i) { | 
|  | 491 | const char *executable = exec_array[i]; | 
|  | 492 | if (envp) { | 
|  | 493 | execve(executable, argv, envp); | 
|  | 494 | } else { | 
|  | 495 | execv(executable, argv); | 
|  | 496 | } | 
|  | 497 | if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) { | 
|  | 498 | saved_errno = errno; | 
|  | 499 | } | 
|  | 500 | } | 
|  | 501 | /* Report the first exec error, not the last. */ | 
|  | 502 | if (saved_errno) | 
|  | 503 | errno = saved_errno; | 
|  | 504 |  | 
|  | 505 | error: | 
|  | 506 | saved_errno = errno; | 
|  | 507 | /* Report the posix error to our parent process. */ | 
| Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 508 | /* We ignore all write() return values as the total size of our writes is | 
| Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 509 | less than PIPEBUF and we cannot do anything about an error anyways. | 
|  | 510 | Use _Py_write_noraise() to retry write() if it is interrupted by a | 
|  | 511 | signal (fails with EINTR). */ | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 512 | if (saved_errno) { | 
|  | 513 | char *cur; | 
| Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 514 | _Py_write_noraise(errpipe_write, "OSError:", 8); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 515 | cur = hex_errno + sizeof(hex_errno); | 
|  | 516 | while (saved_errno != 0 && cur > hex_errno) { | 
| Gregory P. Smith | 4dff6f6 | 2015-04-25 23:42:38 +0000 | [diff] [blame] | 517 | *--cur = Py_hexdigits[saved_errno % 16]; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 518 | saved_errno /= 16; | 
|  | 519 | } | 
| Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 520 | _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur); | 
|  | 521 | _Py_write_noraise(errpipe_write, ":", 1); | 
| Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 522 | if (!reached_preexec) { | 
|  | 523 | /* Indicate to the parent that the error happened before exec(). */ | 
| Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 524 | _Py_write_noraise(errpipe_write, "noexec", 6); | 
| Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 525 | } | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 526 | /* We can't call strerror(saved_errno).  It is not async signal safe. | 
|  | 527 | * The parent process will look the error message up. */ | 
|  | 528 | } else { | 
| Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 529 | _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18); | 
|  | 530 | _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg)); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 531 | } | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 |  | 
|  | 535 | static PyObject * | 
|  | 536 | subprocess_fork_exec(PyObject* self, PyObject *args) | 
|  | 537 | { | 
|  | 538 | PyObject *gc_module = NULL; | 
| Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 539 | PyObject *executable_list, *py_fds_to_keep; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 540 | PyObject *env_list, *preexec_fn; | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 541 | PyObject *process_args, *converted_args = NULL, *fast_args = NULL; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 542 | PyObject *preexec_fn_args_tuple = NULL; | 
|  | 543 | int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite; | 
|  | 544 | int errpipe_read, errpipe_write, close_fds, restore_signals; | 
|  | 545 | int call_setsid; | 
| Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 546 | PyObject *cwd_obj, *cwd_obj2; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 547 | const char *cwd; | 
|  | 548 | pid_t pid; | 
|  | 549 | int need_to_reenable_gc = 0; | 
|  | 550 | char *const *exec_array, *const *argv = NULL, *const *envp = NULL; | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 551 | Py_ssize_t arg_num; | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 552 | int import_lock_held = 0; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 553 |  | 
|  | 554 | if (!PyArg_ParseTuple( | 
| Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 555 | args, "OOpOOOiiiiiiiiiiO:fork_exec", | 
|  | 556 | &process_args, &executable_list, &close_fds, &py_fds_to_keep, | 
| Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 557 | &cwd_obj, &env_list, | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 558 | &p2cread, &p2cwrite, &c2pread, &c2pwrite, | 
|  | 559 | &errread, &errwrite, &errpipe_read, &errpipe_write, | 
|  | 560 | &restore_signals, &call_setsid, &preexec_fn)) | 
|  | 561 | return NULL; | 
|  | 562 |  | 
| Gregory P. Smith | 361e30c | 2013-12-01 00:12:24 -0800 | [diff] [blame] | 563 | if (close_fds && errpipe_write < 3) {  /* precondition */ | 
|  | 564 | PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3"); | 
|  | 565 | return NULL; | 
|  | 566 | } | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 567 | if (PySequence_Length(py_fds_to_keep) < 0) { | 
|  | 568 | PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep"); | 
|  | 569 | return NULL; | 
|  | 570 | } | 
|  | 571 | if (_sanity_check_python_fd_sequence(py_fds_to_keep)) { | 
|  | 572 | PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep"); | 
| Gregory P. Smith | d4cc7bf | 2010-12-04 11:22:11 +0000 | [diff] [blame] | 573 | return NULL; | 
|  | 574 | } | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 575 |  | 
|  | 576 | /* We need to call gc.disable() when we'll be calling preexec_fn */ | 
|  | 577 | if (preexec_fn != Py_None) { | 
|  | 578 | PyObject *result; | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 579 | _Py_IDENTIFIER(isenabled); | 
|  | 580 | _Py_IDENTIFIER(disable); | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 581 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 582 | gc_module = PyImport_ImportModule("gc"); | 
|  | 583 | if (gc_module == NULL) | 
|  | 584 | return NULL; | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 585 | result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, 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 | need_to_reenable_gc = PyObject_IsTrue(result); | 
|  | 591 | Py_DECREF(result); | 
| Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 592 | if (need_to_reenable_gc == -1) { | 
|  | 593 | Py_DECREF(gc_module); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 594 | return NULL; | 
| Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 595 | } | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 596 | result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL); | 
| Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 597 | if (result == NULL) { | 
|  | 598 | Py_DECREF(gc_module); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 599 | return NULL; | 
| Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 600 | } | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 601 | Py_DECREF(result); | 
|  | 602 | } | 
|  | 603 |  | 
|  | 604 | exec_array = _PySequence_BytesToCharpArray(executable_list); | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 605 | if (!exec_array) | 
|  | 606 | goto cleanup; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 607 |  | 
|  | 608 | /* Convert args and env into appropriate arguments for exec() */ | 
|  | 609 | /* These conversions are done in the parent process to avoid allocating | 
|  | 610 | or freeing memory in the child process. */ | 
|  | 611 | if (process_args != Py_None) { | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 612 | Py_ssize_t num_args; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 613 | /* Equivalent to:  */ | 
|  | 614 | /*  tuple(PyUnicode_FSConverter(arg) for arg in process_args)  */ | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 615 | fast_args = PySequence_Fast(process_args, "argv must be a tuple"); | 
| Stefan Krah | db579d7 | 2012-08-20 14:36:47 +0200 | [diff] [blame] | 616 | if (fast_args == NULL) | 
|  | 617 | goto cleanup; | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 618 | num_args = PySequence_Fast_GET_SIZE(fast_args); | 
|  | 619 | converted_args = PyTuple_New(num_args); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 620 | if (converted_args == NULL) | 
|  | 621 | goto cleanup; | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 622 | for (arg_num = 0; arg_num < num_args; ++arg_num) { | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 623 | PyObject *borrowed_arg, *converted_arg; | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 624 | borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 625 | if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0) | 
|  | 626 | goto cleanup; | 
|  | 627 | PyTuple_SET_ITEM(converted_args, arg_num, converted_arg); | 
|  | 628 | } | 
|  | 629 |  | 
|  | 630 | argv = _PySequence_BytesToCharpArray(converted_args); | 
|  | 631 | Py_CLEAR(converted_args); | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 632 | Py_CLEAR(fast_args); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 633 | if (!argv) | 
|  | 634 | goto cleanup; | 
|  | 635 | } | 
|  | 636 |  | 
|  | 637 | if (env_list != Py_None) { | 
|  | 638 | envp = _PySequence_BytesToCharpArray(env_list); | 
|  | 639 | if (!envp) | 
|  | 640 | goto cleanup; | 
|  | 641 | } | 
|  | 642 |  | 
|  | 643 | if (preexec_fn != Py_None) { | 
|  | 644 | preexec_fn_args_tuple = PyTuple_New(0); | 
|  | 645 | if (!preexec_fn_args_tuple) | 
|  | 646 | goto cleanup; | 
| Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 647 | _PyImport_AcquireLock(); | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 648 | import_lock_held = 1; | 
| Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 649 | } | 
|  | 650 |  | 
|  | 651 | if (cwd_obj != Py_None) { | 
|  | 652 | if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0) | 
|  | 653 | goto cleanup; | 
| Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 654 | cwd = PyBytes_AsString(cwd_obj2); | 
| Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 655 | } else { | 
|  | 656 | cwd = NULL; | 
|  | 657 | cwd_obj2 = NULL; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 658 | } | 
|  | 659 |  | 
|  | 660 | pid = fork(); | 
|  | 661 | if (pid == 0) { | 
|  | 662 | /* Child process */ | 
| Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 663 | /* | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 664 | * Code from here to _exit() must only use async-signal-safe functions, | 
|  | 665 | * listed at `man 7 signal` or | 
|  | 666 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. | 
|  | 667 | */ | 
|  | 668 |  | 
|  | 669 | if (preexec_fn != Py_None) { | 
|  | 670 | /* We'll be calling back into Python later so we need to do this. | 
|  | 671 | * This call may not be async-signal-safe but neither is calling | 
|  | 672 | * back into Python.  The user asked us to use hope as a strategy | 
|  | 673 | * to avoid deadlock... */ | 
|  | 674 | PyOS_AfterFork(); | 
|  | 675 | } | 
|  | 676 |  | 
|  | 677 | child_exec(exec_array, argv, envp, cwd, | 
|  | 678 | p2cread, p2cwrite, c2pread, c2pwrite, | 
|  | 679 | errread, errwrite, errpipe_read, errpipe_write, | 
|  | 680 | close_fds, restore_signals, call_setsid, | 
| Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 681 | py_fds_to_keep, preexec_fn, preexec_fn_args_tuple); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 682 | _exit(255); | 
|  | 683 | return NULL;  /* Dead code to avoid a potential compiler warning. */ | 
|  | 684 | } | 
| Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 685 | Py_XDECREF(cwd_obj2); | 
|  | 686 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 687 | if (pid == -1) { | 
|  | 688 | /* Capture the errno exception before errno can be clobbered. */ | 
|  | 689 | PyErr_SetFromErrno(PyExc_OSError); | 
|  | 690 | } | 
|  | 691 | if (preexec_fn != Py_None && | 
|  | 692 | _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) { | 
|  | 693 | PyErr_SetString(PyExc_RuntimeError, | 
|  | 694 | "not holding the import lock"); | 
|  | 695 | } | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 696 | import_lock_held = 0; | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 697 |  | 
|  | 698 | /* Parent process */ | 
|  | 699 | if (envp) | 
|  | 700 | _Py_FreeCharPArray(envp); | 
|  | 701 | if (argv) | 
|  | 702 | _Py_FreeCharPArray(argv); | 
|  | 703 | _Py_FreeCharPArray(exec_array); | 
|  | 704 |  | 
|  | 705 | /* Reenable gc in the parent process (or if fork failed). */ | 
|  | 706 | if (need_to_reenable_gc && _enable_gc(gc_module)) { | 
|  | 707 | Py_XDECREF(gc_module); | 
|  | 708 | return NULL; | 
|  | 709 | } | 
| Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 710 | Py_XDECREF(preexec_fn_args_tuple); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 711 | Py_XDECREF(gc_module); | 
|  | 712 |  | 
|  | 713 | if (pid == -1) | 
|  | 714 | return NULL;  /* fork() failed.  Exception set earlier. */ | 
|  | 715 |  | 
|  | 716 | return PyLong_FromPid(pid); | 
|  | 717 |  | 
|  | 718 | cleanup: | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 719 | if (import_lock_held) | 
|  | 720 | _PyImport_ReleaseLock(); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 721 | if (envp) | 
|  | 722 | _Py_FreeCharPArray(envp); | 
|  | 723 | if (argv) | 
|  | 724 | _Py_FreeCharPArray(argv); | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 725 | if (exec_array) | 
|  | 726 | _Py_FreeCharPArray(exec_array); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 727 | Py_XDECREF(converted_args); | 
| Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 728 | Py_XDECREF(fast_args); | 
| Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 729 | Py_XDECREF(preexec_fn_args_tuple); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 730 |  | 
|  | 731 | /* Reenable gc if it was disabled. */ | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 732 | if (need_to_reenable_gc) { | 
|  | 733 | PyObject *exctype, *val, *tb; | 
|  | 734 | PyErr_Fetch(&exctype, &val, &tb); | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 735 | _enable_gc(gc_module); | 
| Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 736 | PyErr_Restore(exctype, val, tb); | 
|  | 737 | } | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 738 | Py_XDECREF(gc_module); | 
|  | 739 | return NULL; | 
|  | 740 | } | 
|  | 741 |  | 
|  | 742 |  | 
|  | 743 | PyDoc_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\ | 
|  | 749 | Forks a child process, closes parent file descriptors as appropriate in the\n\ | 
|  | 750 | child and dups the few that are needed before calling exec() in the child\n\ | 
|  | 751 | process.\n\ | 
|  | 752 | \n\ | 
|  | 753 | The preexec_fn, if supplied, will be called immediately before exec.\n\ | 
|  | 754 | WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\ | 
|  | 755 | It may trigger infrequent, difficult to debug deadlocks.\n\ | 
|  | 756 | \n\ | 
|  | 757 | If an error occurs in the child process before the exec, it is\n\ | 
|  | 758 | serialized and written to the errpipe_write fd per subprocess.py.\n\ | 
|  | 759 | \n\ | 
|  | 760 | Returns: the child process's PID.\n\ | 
|  | 761 | \n\ | 
|  | 762 | Raises: Only on an error in the parent process.\n\ | 
|  | 763 | "); | 
|  | 764 |  | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 765 | /* module level code ********************************************************/ | 
|  | 766 |  | 
|  | 767 | PyDoc_STRVAR(module_doc, | 
|  | 768 | "A POSIX helper for the subprocess module."); | 
|  | 769 |  | 
|  | 770 |  | 
|  | 771 | static PyMethodDef module_methods[] = { | 
|  | 772 | {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc}, | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 773 | {NULL, NULL}  /* sentinel */ | 
|  | 774 | }; | 
|  | 775 |  | 
|  | 776 |  | 
|  | 777 | static struct PyModuleDef _posixsubprocessmodule = { | 
|  | 778 | PyModuleDef_HEAD_INIT, | 
|  | 779 | "_posixsubprocess", | 
|  | 780 | module_doc, | 
|  | 781 | -1,  /* No memory is needed. */ | 
|  | 782 | module_methods, | 
|  | 783 | }; | 
|  | 784 |  | 
|  | 785 | PyMODINIT_FUNC | 
|  | 786 | PyInit__posixsubprocess(void) | 
|  | 787 | { | 
| Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 788 | return PyModule_Create(&_posixsubprocessmodule); | 
|  | 789 | } |