annotate Modules/_posixsubprocess.c @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 786a106fc6b7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1 /* Authors: Gregory P. Smith & Jeffrey Yasskin */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2 #include "Python.h"
74127
69eee9084ba3 Don't redefine _GNU_SOURCE if it's already defined.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74125
diff changeset
3 #if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
69eee9084ba3 Don't redefine _GNU_SOURCE if it's already defined.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74125
diff changeset
4 # define _GNU_SOURCE
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
5 #endif
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 #include <unistd.h>
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
7 #include <fcntl.h>
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
8 #ifdef HAVE_SYS_TYPES_H
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
9 #include <sys/types.h>
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
10 #endif
74573
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
11 #if defined(HAVE_SYS_STAT_H) && defined(__FreeBSD__)
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
12 #include <sys/stat.h>
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
13 #endif
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
14 #ifdef HAVE_SYS_SYSCALL_H
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
15 #include <sys/syscall.h>
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
16 #endif
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
17 #ifdef HAVE_DIRENT_H
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
18 #include <dirent.h>
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
19 #endif
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
20
74567
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
21 #if defined(sun)
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
22 /* readdir64 is used to work around Solaris 9 bug 6395699. */
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
23 # define readdir readdir64
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
24 # define dirent dirent64
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
25 # if !defined(HAVE_DIRFD)
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
26 /* Some versions of Solaris lack dirfd(). */
74568
5be3dadd2eef Another issue #8052 bugfix (related to previous commit).
Gregory P. Smith <greg@krypto.org>
parents: 74567
diff changeset
27 # define dirfd(dirp) ((dirp)->dd_fd)
74567
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
28 # define HAVE_DIRFD
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
29 # endif
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
30 #endif
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
31
74573
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
32 #if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
33 # define FD_DIR "/dev/fd"
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
34 #else
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
35 # define FD_DIR "/proc/self/fd"
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
36 #endif
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
38 #define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (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
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)
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
90 return 0;
74573
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
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
139 static int
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
140 make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
141 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
142 Py_ssize_t i, len;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
143
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
144 len = PySequence_Length(py_fds_to_keep);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
145 for (i = 0; i < len; ++i) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
146 PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
147 long fd = PyLong_AsLong(fdobj);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
148 assert(!PyErr_Occurred());
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
149 assert(0 <= fd && fd <= INT_MAX);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
150 if (fd == errpipe_write) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
151 /* errpipe_write is part of py_fds_to_keep. It must be closed at
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
152 exec(), but kept open in the child process until exec() is
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
153 called. */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
154 continue;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
155 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
156 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
157 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
158 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
159 return 0;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
160 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
161
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
162
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
163 /* 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
164 * 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
165 * 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
166 * 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
167 */
74591
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
168 static void
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
169 _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
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 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
172 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
173 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
174 /* 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
175 * 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
176 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
177 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
178 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
179 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
180 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
181 continue;
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
182 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
183 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
184 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
185 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
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 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
188 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
189 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
190 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
191 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
192 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
193
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
194
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
195 #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
196 /* 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
197 * 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
198 * 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
199 * 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
200 * 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
201 */
82460
30e643e36bae Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents: 79633
diff changeset
202 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
203 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
204 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
205 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
206 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
207 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
208 };
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
209
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
210 /* 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
211 * 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
212 *
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
213 * 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
214 * 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
215 * 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
216 * 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
217 * 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
218 * 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
219 * 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
220 *
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
221 * 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
222 * 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
223 * 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
224 */
74591
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
225 static void
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
226 _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
227 {
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
228 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
229 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
230 return;
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
231
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
232 fd_dir_fd = _Py_open(FD_DIR, O_RDONLY);
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
233 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
234 /* 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
235 _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
236 return;
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
237 } else {
82460
30e643e36bae Issue #16962: Use getdents64 instead of the obsolete getdents syscall in
Gregory P. Smith <greg@krypto.org>
parents: 79633
diff changeset
238 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
239 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
240 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
241 (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
242 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
243 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
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 !_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
252 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
253 }
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 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
256 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
257 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
258 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
259
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
260 #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
261
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
262 #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
263
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
264
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
265 /* 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
266 * 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
267 *
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
268 * 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
269 * 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
270 * 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
271 * 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
272 * 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
273 *
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
274 * 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
275 * 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
276 * 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
277 */
74591
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
278 static void
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
279 _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
280 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
281 {
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
282 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
283 #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
284 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
285 (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
286 ++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
287 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
288 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
289 return;
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
290 /* 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
291 * 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
292 * 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
293 * 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
294 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
295 ++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
296 #endif
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
297 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
298 return;
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
299
74573
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
300 #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
301 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
302 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
303 else
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
304 #endif
754c2eb0a92c Fix FreeBSD, NetBSD and OpenBSD behavior of the issue #8052 fix.
Gregory P. Smith <greg@krypto.org>
parents: 74568
diff changeset
305 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
306 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
307 /* 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
308 _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
309 } else {
74567
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
310 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
311 #ifdef HAVE_DIRFD
74568
5be3dadd2eef Another issue #8052 bugfix (related to previous commit).
Gregory P. Smith <greg@krypto.org>
parents: 74567
diff changeset
312 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
313 #else
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
314 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
315 #endif
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
316 errno = 0;
74567
d0acd8169c2a Bugfix for issue #8052 fix on *BSD variants.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
317 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
318 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
319 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
320 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
321 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
322 !_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
323 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
324 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
325 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
326 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
327 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
328 /* 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
329 _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
330 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
331 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
332 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
333 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
334
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
335 #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
336
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
337 #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
338
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
339
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
340 /*
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
341 * This 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
342 * 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
343 *
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
344 * 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
345 * 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
346 * 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
347 *
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348 * 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
349 * 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
350 */
74591
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
351 static void
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
352 child_exec(char *const exec_array[],
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
353 char *const argv[],
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
354 char *const envp[],
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
355 const char *cwd,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
356 int p2cread, int p2cwrite,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
357 int c2pread, int c2pwrite,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
358 int errread, int errwrite,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
359 int errpipe_read, int errpipe_write,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
360 int close_fds, int restore_signals,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
361 int call_setsid,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
362 PyObject *py_fds_to_keep,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
363 PyObject *preexec_fn,
308f4af9548e fix declaration style
Benjamin Peterson <benjamin@python.org>
parents: 74573
diff changeset
364 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
365 {
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 78724
diff changeset
366 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
367 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
368 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
369 /* 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
370 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
371
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
372 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
373 goto error;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
374
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
375 /* Close parent's pipe ends. */
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
376 if (p2cwrite != -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
377 POSIX_CALL(close(p2cwrite));
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
378 if (c2pread != -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
379 POSIX_CALL(close(c2pread));
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
380 if (errread != -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
381 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
382 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
383
71524
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68520
diff changeset
384 /* 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
385 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
386 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
387 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
388 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
389 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
390
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
391 /* 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
392 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
393 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
394 if (p2cread == 0) {
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
395 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
396 goto error;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
397 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
398 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
399 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
400
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
401 if (c2pwrite == 1) {
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
402 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
403 goto error;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
404 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
405 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
406 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
407
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
408 if (errwrite == 2) {
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
409 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
410 goto error;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
411 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
412 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
413 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
414
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
415 /* 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
416 /* once, or standard fds. */
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
417 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
418 POSIX_CALL(close(p2cread));
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
419 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
420 POSIX_CALL(close(c2pwrite));
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
421 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
422 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
423
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
424 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
425 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
426
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
427 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
428 _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
429
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
430 #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
431 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
432 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
433 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
434
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 78724
diff changeset
435 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
436 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
437 /* 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
438 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
439 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
440 /* 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
441 * 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
442 * 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
443 * 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
444 * 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
445 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
446 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
447 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
448 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
449 /* 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
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
85391
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
452 /* close FDs after executing preexec_fn, which might open FDs */
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
453 if (close_fds) {
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
454 int local_max_fd = max_fd;
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
455 #if defined(__NetBSD__)
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
456 local_max_fd = fcntl(0, F_MAXFD);
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
457 if (local_max_fd < 0)
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
458 local_max_fd = max_fd;
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
459 #endif
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
460 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
87674
efcdf2a70f2a Undo supposed fix for Issue #15798 until I understand why this is
Gregory P. Smith <greg@krypto.org>
parents: 87672
diff changeset
461 _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
85391
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
462 }
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 82460
diff changeset
463
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
464 /* 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
465 /* 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
466 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
467 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
468 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
469 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
470 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
471 } else {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
472 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
473 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
474 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
475 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
476 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
477 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
478 /* 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
479 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
480 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
481
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
482 error:
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 = errno;
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
484 /* 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
485 /* 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
486 * 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
487 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
488 char *cur;
74561
d01fecadf3ea Avoid the compiler warning about the unused return value.
Gregory P. Smith <greg@krypto.org>
parents: 74127
diff changeset
489 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
490 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
491 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
492 *--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
493 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
494 }
74561
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, 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
496 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
497 if (!reached_preexec) {
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 78724
diff changeset
498 /* 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
499 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
500 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
501 /* 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
502 * 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
503 } else {
80375
9180fb4eccc7 Raise our own SubprocessError rather than a RuntimeError in when dealing with
Gregory P. Smith <greg@krypto.org>
parents: 79633
diff changeset
504 unused = write(errpipe_write, "SubprocessError:0:", 18);
74561
d01fecadf3ea Avoid the compiler warning about the unused return value.
Gregory P. Smith <greg@krypto.org>
parents: 74127
diff changeset
505 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
506 }
74561
d01fecadf3ea Avoid the compiler warning about the unused return value.
Gregory P. Smith <greg@krypto.org>
parents: 74127
diff changeset
507 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
508 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
509
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
510
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
511 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
512 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
513 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
514 PyObject *gc_module = NULL;
78605
b878df1d23b1 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77364 78604
diff changeset
515 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
516 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
517 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
518 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
519 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
520 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
521 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
522 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
523 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
524 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
525 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
526 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
527 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
528
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
529 if (!PyArg_ParseTuple(
78605
b878df1d23b1 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77364 78604
diff changeset
530 args, "OOpOOOiiiiiiiiiiO:fork_exec",
b878df1d23b1 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77364 78604
diff changeset
531 &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
532 &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
533 &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
534 &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
535 &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
536 return NULL;
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
537
87674
efcdf2a70f2a Undo supposed fix for Issue #15798 until I understand why this is
Gregory P. Smith <greg@krypto.org>
parents: 87672
diff changeset
538 if (close_fds && errpipe_write < 3) { /* precondition */
efcdf2a70f2a Undo supposed fix for Issue #15798 until I understand why this is
Gregory P. Smith <greg@krypto.org>
parents: 87672
diff changeset
539 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
efcdf2a70f2a Undo supposed fix for Issue #15798 until I understand why this is
Gregory P. Smith <greg@krypto.org>
parents: 87672
diff changeset
540 return NULL;
efcdf2a70f2a Undo supposed fix for Issue #15798 until I understand why this is
Gregory P. Smith <greg@krypto.org>
parents: 87672
diff changeset
541 }
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
542 if (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
543 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
544 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
545 }
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 74561
diff changeset
546 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
547 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
548 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
549 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
550
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
551 /* 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
552 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
553 PyObject *result;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
554 _Py_IDENTIFIER(isenabled);
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
555 _Py_IDENTIFIER(disable);
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
556
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
557 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
558 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
559 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
560 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
561 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
562 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
563 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
564 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
565 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
566 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
567 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
568 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
569 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
570 }
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 71525
diff changeset
571 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
572 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
573 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
574 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
575 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
576 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
577 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
578
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
579 exec_array = _PySequence_BytesToCharpArray(executable_list);
78724
081507b4ae40 Issue 15777: Fix a refleak in _posixsubprocess.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 78670
diff changeset
580 if (!exec_array) {
081507b4ae40 Issue 15777: Fix a refleak in _posixsubprocess.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 78670
diff changeset
581 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
582 return NULL;
78724
081507b4ae40 Issue 15777: Fix a refleak in _posixsubprocess.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 78670
diff changeset
583 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
584
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
585 /* 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
586 /* 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
587 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
588 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
589 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
590 /* 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
591 /* 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
592 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
593 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
594 goto cleanup;
59631
adb5e928bde2 * Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
595 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
596 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
597 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
598 goto cleanup;
59631
adb5e928bde2 * Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
599 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
600 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
601 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
602 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
603 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
604 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
605 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
606
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
607 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
608 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
609 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
610 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
611 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
612 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
613
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
614 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
615 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
616 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
617 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
618 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
619
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
620 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
621 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
622 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
623 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
624 _PyImport_AcquireLock();
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
625 }
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
626
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
627 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
628 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
629 goto cleanup;
60856
dd18cccb55b0 Issue #8485: PyUnicode_FSConverter() doesn't accept bytearray object anymore,
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60646
diff changeset
630 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
631 } else {
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
632 cwd = NULL;
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
633 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
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
636 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
637 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
638 /* 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
639 /*
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
640 * 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
641 * 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
642 * 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
643 */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
644
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
645 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
646 /* 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
647 * 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
648 * 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
649 * 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
650 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
651 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
652
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
653 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
654 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
655 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
656 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
657 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
658 _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
659 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
660 }
60646
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
661 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
662
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
663 if (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
664 /* 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
665 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
666 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
667 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
668 _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
669 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
670 "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
671 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
672
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
673 /* 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
674 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
675 _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
676 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
677 _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
678 _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
679
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
680 /* 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
681 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
682 Py_XDECREF(gc_module);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
683 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
684 }
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
685 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
686 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
687
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
688 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
689 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
690
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
691 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
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 cleanup:
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 (envp)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
695 _Py_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
696 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
697 _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
698 _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
699 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
700 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
701 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
702
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
703 /* 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
704 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
705 _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
706 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
707 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
708 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
709
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
710
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
711 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
712 "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
713 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
714 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
715 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
716 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
717 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
718 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
719 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
720 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
721 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
722 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
723 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
724 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
725 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
726 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
727 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
728 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
729 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
730 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
731 ");
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
732
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
733 /* 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
734
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
735 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
736 "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
737
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
738
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
739 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
740 {"fork_exec", subprocess_fork_exec, METH_VARARGS, 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
741 {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
742 };
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
743
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
744
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
745 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
746 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
747 "_posixsubprocess",
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
748 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
749 -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
750 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
751 };
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
752
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
753 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
754 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
755 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
756 #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
757 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
758 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
759 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
760 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
761
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
762 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
763 }