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