annotate Modules/_posixsubprocess.c @ 79691:05c8928362ee

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