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