Mercurial > cpython-lru_cache-weakref
annotate Modules/_posixsubprocess.c @ 84007:dbfbdf2b5c19 3.3
Fix #17967: For ftp urls CWD to target instead of hopping to each directory
towards target. This fixes a bug where target is accessible, but parent
directories are restricted.
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Sat, 01 Jun 2013 08:27:06 -0700 |
| parents | 30e643e36bae |
| children | 4c52d4bac03c 7ab1c55fcf82 |
| 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 */ |
|
82460
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
179 struct linux_dirent64 { |
|
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 unsigned long long d_ino; |
|
82460
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
181 long long d_off; |
|
74563
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
182 unsigned short d_reclen; /* Length of this linux_dirent */ |
|
82460
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
183 unsigned char d_type; |
|
74563
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
184 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
|
185 }; |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
186 |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
187 /* 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
|
188 * 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
|
189 * |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
190 * 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
|
191 * 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
|
192 * 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
|
193 * 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
|
194 * 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
|
195 * 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
|
196 * 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
|
197 * |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
198 * 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
|
199 * 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
|
200 * 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
|
201 */ |
|
74591
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
202 static void |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
203 _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
|
204 { |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
205 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
|
206 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
|
207 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
|
208 #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
|
209 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
|
210 #else |
|
09371e2ae84d
Issue #14359: Only use O_CLOEXEC in _posixmodule.c if it is defined.
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
74591
diff
changeset
|
211 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
|
212 #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
|
213 { |
|
09371e2ae84d
Issue #14359: Only use O_CLOEXEC in _posixmodule.c if it is defined.
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
74591
diff
changeset
|
214 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
|
215 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
|
216 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
|
217 } |
|
09371e2ae84d
Issue #14359: Only use O_CLOEXEC in _posixmodule.c if it is defined.
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
74591
diff
changeset
|
218 #endif |
|
09371e2ae84d
Issue #14359: Only use O_CLOEXEC in _posixmodule.c if it is defined.
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
74591
diff
changeset
|
219 #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
|
220 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
|
221 /* 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
|
222 _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
|
223 return; |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
224 } else { |
|
82460
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
225 char buffer[sizeof(struct linux_dirent64)]; |
|
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 int bytes; |
|
82460
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
227 while ((bytes = syscall(SYS_getdents64, fd_dir_fd, |
|
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
228 (struct linux_dirent64 *)buffer, |
|
74563
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
229 sizeof(buffer))) > 0) { |
|
82460
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
230 struct linux_dirent64 *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
|
231 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
|
232 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
|
233 int fd; |
|
82460
30e643e36bae
Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents:
79633
diff
changeset
|
234 entry = (struct linux_dirent64 *)(buffer + offset); |
|
74563
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
235 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
|
236 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
|
237 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
|
238 !_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
|
239 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
|
240 } |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
241 } |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
242 } |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
243 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
|
244 } |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
245 } |
|
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 #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
|
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 #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
|
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 /* 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
|
253 * 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
|
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 * 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
|
256 * 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
|
257 * 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
|
258 * 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
|
259 * 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
|
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 * 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
|
262 * 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
|
263 * 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
|
264 */ |
|
74591
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
265 static void |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
266 _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
|
267 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
|
268 { |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
269 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
|
270 #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
|
271 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
|
272 (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
|
273 ++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
|
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 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
|
276 return; |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
277 /* 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
|
278 * 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
|
279 * 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
|
280 * 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
|
281 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
|
282 ++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
|
283 #endif |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
284 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
|
285 return; |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
286 |
|
74573
754c2eb0a92c
Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents:
74568
diff
changeset
|
287 #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
|
288 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
|
289 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
|
290 else |
|
754c2eb0a92c
Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents:
74568
diff
changeset
|
291 #endif |
|
754c2eb0a92c
Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents:
74568
diff
changeset
|
292 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
|
293 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
|
294 /* 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
|
295 _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
|
296 } else { |
|
74567
d0acd8169c2a
Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents:
74563
diff
changeset
|
297 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
|
298 #ifdef HAVE_DIRFD |
|
74568
5be3dadd2eef
Another issue #8052 bugfix (related to previous commit).
Gregory P. Smith <greg@krypto.org>
parents:
74567
diff
changeset
|
299 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
|
300 #else |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
301 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
|
302 #endif |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
303 errno = 0; |
|
74567
d0acd8169c2a
Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents:
74563
diff
changeset
|
304 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
|
305 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
|
306 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
|
307 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
|
308 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
|
309 !_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
|
310 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
|
311 } |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
312 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
|
313 } |
|
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 (errno) { |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
315 /* 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
|
316 _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
|
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 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
|
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 } |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
321 |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
322 #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
|
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 #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
|
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 |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
327 /* |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
328 * 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
|
329 * 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
|
330 * |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
331 * 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
|
332 * 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
|
333 * 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
|
334 * |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
335 * 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
|
336 * 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
|
337 */ |
|
74591
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
338 static void |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
339 child_exec(char *const exec_array[], |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
340 char *const argv[], |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
341 char *const envp[], |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
342 const char *cwd, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
343 int p2cread, int p2cwrite, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
344 int c2pread, int c2pwrite, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
345 int errread, int errwrite, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
346 int errpipe_read, int errpipe_write, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
347 int close_fds, int restore_signals, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
348 int call_setsid, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
349 PyObject *py_fds_to_keep, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
350 PyObject *preexec_fn, |
|
308f4af9548e
fix declaration style
Benjamin Peterson <benjamin@python.org>
parents:
74573
diff
changeset
|
351 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
|
352 { |
|
79632
e938fa6be509
Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents:
78724
diff
changeset
|
353 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
|
354 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
|
355 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
|
356 /* 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
|
357 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
|
358 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
359 /* 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
|
360 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
|
361 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
|
362 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
363 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
|
364 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
|
365 } |
|
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 (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
|
367 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
|
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 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
|
370 |
|
71524
1140b32747c9
Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
68520
diff
changeset
|
371 /* 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 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
|
377 |
|
67310
a66a6e42538f
Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents:
67270
diff
changeset
|
378 /* 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
|
379 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
|
380 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
|
381 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
|
382 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
|
383 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
|
384 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
|
385 } 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
|
386 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
|
387 } |
|
67310
a66a6e42538f
Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents:
67270
diff
changeset
|
388 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
|
389 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
|
390 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
|
391 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
|
392 } 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
|
393 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
|
394 } |
|
67310
a66a6e42538f
Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents:
67270
diff
changeset
|
395 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
|
396 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
|
397 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
|
398 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
|
399 } 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
|
400 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
|
401 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
402 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
403 /* 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
|
404 /* 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
|
405 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
|
406 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
|
407 } |
|
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
|
408 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
|
409 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
|
410 } |
|
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
|
411 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
|
412 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
|
413 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
414 |
|
74573
754c2eb0a92c
Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents:
74568
diff
changeset
|
415 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
|
416 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
|
417 #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
|
418 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
|
419 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
|
420 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
|
421 #endif |
|
754c2eb0a92c
Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents:
74568
diff
changeset
|
422 /* 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
|
423 _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
|
424 } |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
425 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
426 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
|
427 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
|
428 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
429 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
|
430 _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
|
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 #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
|
433 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
|
434 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
|
435 #endif |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
436 |
|
79632
e938fa6be509
Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents:
78724
diff
changeset
|
437 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
|
438 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
|
439 /* 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
|
440 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
|
441 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
|
442 /* 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
|
443 * 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
|
444 * 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
|
445 * 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
|
446 * 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
|
447 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
|
448 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
|
449 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
|
450 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
451 /* 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
|
452 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
453 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
454 /* 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
|
455 /* 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
|
456 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
|
457 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
|
458 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
|
459 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
|
460 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
|
461 } else { |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
462 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
|
463 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
464 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
|
465 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
|
466 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
467 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
468 /* 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
|
469 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
|
470 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
|
471 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
472 error: |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
473 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
|
474 /* 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
|
475 /* 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
|
476 * 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
|
477 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
|
478 char *cur; |
|
74561
d01fecadf3ea
Avoid the compiler warning about the unused return value.
Gregory P. Smith <greg@krypto.org>
parents:
74127
diff
changeset
|
479 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
|
480 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
|
481 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
|
482 *--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
|
483 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
|
484 } |
|
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, 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
|
486 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
|
487 if (!reached_preexec) { |
|
e938fa6be509
Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents:
78724
diff
changeset
|
488 /* 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
|
489 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
|
490 } |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
491 /* 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
|
492 * 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
|
493 } else { |
|
74561
d01fecadf3ea
Avoid the compiler warning about the unused return value.
Gregory P. Smith <greg@krypto.org>
parents:
74127
diff
changeset
|
494 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
|
495 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
|
496 } |
|
74561
d01fecadf3ea
Avoid the compiler warning about the unused return value.
Gregory P. Smith <greg@krypto.org>
parents:
74127
diff
changeset
|
497 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
|
498 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
499 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
500 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
501 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
|
502 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
|
503 { |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
504 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>
diff
changeset
|
505 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
|
506 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
|
507 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
|
508 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
|
509 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
|
510 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
|
511 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
|
512 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
|
513 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
|
514 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
|
515 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
|
516 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
|
517 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
|
518 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
519 if (!PyArg_ParseTuple( |
|
78605
b878df1d23b1
Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
diff
changeset
|
520 args, "OOpOOOiiiiiiiiiiO:fork_exec", |
|
b878df1d23b1
Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
diff
changeset
|
521 &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
|
522 &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
|
523 &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
|
524 &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
|
525 &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
|
526 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
|
527 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
528 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
|
529 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
|
530 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
|
531 } |
|
74563
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
532 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
|
533 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
|
534 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
|
535 } |
|
61aa484a3e54
Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents:
74561
diff
changeset
|
536 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
|
537 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
|
538 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
|
539 } |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
540 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
541 /* 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
|
542 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
|
543 PyObject *result; |
|
72925
7109f31300fb
Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents:
72835
diff
changeset
|
544 _Py_IDENTIFIER(isenabled); |
|
7109f31300fb
Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents:
72835
diff
changeset
|
545 _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
|
546 |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
547 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
|
548 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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 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
|
554 } |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
555 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
|
556 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
|
557 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
|
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 } |
|
72835
5e33219492bc
Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents:
71525
diff
changeset
|
561 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
|
562 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
|
563 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
|
564 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
|
565 } |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
566 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
|
567 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
568 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
569 exec_array = _PySequence_BytesToCharpArray(executable_list); |
|
78724
081507b4ae40
Issue 15777: Fix a refleak in _posixsubprocess.
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
78670
diff
changeset
|
570 if (!exec_array) { |
|
081507b4ae40
Issue 15777: Fix a refleak in _posixsubprocess.
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
78670
diff
changeset
|
571 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
|
572 return NULL; |
|
78724
081507b4ae40
Issue 15777: Fix a refleak in _posixsubprocess.
Ross Lagerwall <rosslagerwall@gmail.com>
parents:
78670
diff
changeset
|
573 } |
|
59605
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 /* 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
|
576 /* 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
|
577 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
|
578 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
|
579 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
|
580 /* 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
|
581 /* 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
|
582 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
|
583 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
|
584 goto cleanup; |
|
59631
adb5e928bde2
* Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents:
59605
diff
changeset
|
585 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
|
586 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
|
587 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
|
588 goto cleanup; |
|
59631
adb5e928bde2
* Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents:
59605
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 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
|
593 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
|
594 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
|
595 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
596 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
597 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
|
598 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
|
599 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
|
600 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
|
601 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
|
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 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
604 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
|
605 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
|
606 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
|
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 (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
|
611 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
|
612 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
|
613 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
|
614 _PyImport_AcquireLock(); |
|
dc765bca24ad
Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59723
diff
changeset
|
615 } |
|
dc765bca24ad
Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59723
diff
changeset
|
616 |
|
dc765bca24ad
Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59723
diff
changeset
|
617 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
|
618 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
|
619 goto cleanup; |
|
60856
dd18cccb55b0
Issue #8485: PyUnicode_FSConverter() doesn't accept bytearray object anymore,
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60646
diff
changeset
|
620 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
|
621 } else { |
|
dc765bca24ad
Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59723
diff
changeset
|
622 cwd = NULL; |
|
dc765bca24ad
Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59723
diff
changeset
|
623 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
|
624 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
625 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
626 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
|
627 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
|
628 /* 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
|
629 /* |
|
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 * 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
|
631 * 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
|
632 * 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
|
633 */ |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
634 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
635 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
|
636 /* 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
|
637 * 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
|
638 * 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
|
639 * 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
|
640 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
|
641 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
642 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
643 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
|
644 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
|
645 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
|
646 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
|
647 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
|
648 _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
|
649 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
|
650 } |
|
60646
dc765bca24ad
Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59723
diff
changeset
|
651 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
|
652 |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
653 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
|
654 /* 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
|
655 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
|
656 } |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
657 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
|
658 _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
|
659 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
|
660 "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
|
661 } |
|
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 /* 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
|
664 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
|
665 _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
|
666 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
|
667 _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
|
668 _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
|
669 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
670 /* 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
|
671 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
|
672 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
|
673 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
|
674 } |
|
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
|
675 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
|
676 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
|
677 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
678 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
|
679 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
|
680 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
681 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
|
682 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
683 cleanup: |
|
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 (envp) |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
685 _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
|
686 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
|
687 _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
|
688 _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
|
689 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
|
690 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
|
691 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
|
692 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
693 /* 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
|
694 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
|
695 _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
|
696 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
|
697 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
|
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 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
700 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
701 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
|
702 "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
|
703 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
|
704 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
|
705 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
|
706 \n\ |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
707 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
|
708 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
|
709 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
|
710 \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 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
|
712 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
|
713 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
|
714 \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 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
|
716 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
|
717 \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 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
|
719 \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 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
|
721 "); |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
722 |
|
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
|
723 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
|
724 "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
|
725 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
|
726 |
|
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
|
727 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
|
728 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
|
729 { |
|
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 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
|
731 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
|
732 #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
|
733 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
|
734 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
|
735 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
|
736 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
|
737 { |
|
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
|
738 { |
|
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
|
739 #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
|
740 /* 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
|
741 * 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
|
742 * 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
|
743 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
|
744 |
|
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
|
745 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
|
746 |
|
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
|
747 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
|
748 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
|
749 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
|
750 } |
|
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 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
|
752 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
|
753 |
|
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
|
754 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
|
755 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
|
756 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
|
757 } |
|
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 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
|
759 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
|
760 #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
|
761 } |
|
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
|
762 } |
|
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
|
763 #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
|
764 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
|
765 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
|
766 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
|
767 } |
|
59605
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
768 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
769 /* 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
|
770 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
771 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
|
772 "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
|
773 |
|
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 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
|
776 {"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
|
777 {"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
|
778 {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
|
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 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
782 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
|
783 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
|
784 "_posixsubprocess", |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
785 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
|
786 -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
|
787 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
|
788 }; |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
789 |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
790 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
|
791 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
|
792 { |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
793 #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
|
794 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
|
795 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
|
796 #endif |
|
768722b2ae0a
* Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
797 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
|
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 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
|
800 } |
