annotate Modules/_posixsubprocess.c @ 73807:1efefeda00a7 3.2

#8414: add more tests for "assert". Initial patch by Gregory Nofi.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 02 Dec 2011 18:22:52 +0200
parents 1140b32747c9
children dc913f73a7fb 943d323cb4b8
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"
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
3 #ifdef HAVE_PIPE2
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
4 #define _GNU_SOURCE
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>
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
8
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
9
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10 #define POSIX_CALL(call) if ((call) == -1) goto error
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
11
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
12
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
13 /* 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
14 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
15
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
16
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
17 /* Given the gc module call gc.enable() and return 0 on success. */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
18 static int _enable_gc(PyObject *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
19 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
20 PyObject *result;
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21 result = PyObject_CallMethod(gc_module, "enable", NULL);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
22 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
23 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
24 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
25 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
26 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
27
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
28
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
29 /*
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
30 * 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
31 * 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
32 *
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
33 * 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
34 * 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
35 * 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
36 *
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37 * 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
38 * 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
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 static void child_exec(char *const 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
41 char *const argv[],
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
42 char *const envp[],
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
43 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
44 int p2cread, int p2cwrite,
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
45 int c2pread, int c2pwrite,
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
46 int errread, int errwrite,
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
47 int errpipe_read, int 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
48 int close_fds, int restore_signals,
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
49 int call_setsid, Py_ssize_t num_fds_to_keep,
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
50 PyObject *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
51 PyObject *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
52 PyObject *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
53 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
54 int i, saved_errno, fd_num;
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
55 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
56 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
57 /* 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
58 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
59
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
60 /* Close parent's pipe ends. */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
61 if (p2cwrite != -1) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
62 POSIX_CALL(close(p2cwrite));
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
63 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
64 if (c2pread != -1) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
65 POSIX_CALL(close(c2pread));
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
66 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
67 if (errread != -1) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
68 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
69 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
70 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
71
71524
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68525
diff changeset
72 /* 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: 68525
diff changeset
73 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: 68525
diff changeset
74 if (c2pwrite == 0)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68525
diff changeset
75 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: 68525
diff changeset
76 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: 68525
diff changeset
77 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: 68525
diff changeset
78
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
79 /* 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
80 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
81 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
82 if (p2cread == 0) {
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
83 int old = fcntl(p2cread, F_GETFD);
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
84 if (old != -1)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
85 fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
86 } 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
87 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
88 }
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
89 if (c2pwrite == 1) {
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
90 int old = fcntl(c2pwrite, F_GETFD);
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
91 if (old != -1)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
92 fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
93 } 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
94 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
95 }
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
96 if (errwrite == 2) {
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
97 int old = fcntl(errwrite, F_GETFD);
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
98 if (old != -1)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
99 fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
100 } 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
101 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
102 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
103
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
104 /* 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
105 /* once, or standard fds. */
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
106 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
107 POSIX_CALL(close(p2cread));
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
108 }
68525
ead52adcd0c8 Fix issue #11432. if the stdin pipe is the same file descriptor as either stdout or stderr
Gregory P. Smith <greg@krypto.org>
parents: 67310
diff changeset
109 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
110 POSIX_CALL(close(c2pwrite));
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
111 }
68525
ead52adcd0c8 Fix issue #11432. if the stdin pipe is the same file descriptor as either stdout or stderr
Gregory P. Smith <greg@krypto.org>
parents: 67310
diff changeset
112 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
113 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
114 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
115
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
116 /* close() is intentionally not checked for errors here as we are closing */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
117 /* a large range of fds, some of which may be invalid. */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
118 if (close_fds) {
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
119 Py_ssize_t keep_seq_idx;
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
120 int start_fd = 3;
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
121 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
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
122 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
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
123 keep_seq_idx);
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
124 int keep_fd = PyLong_AsLong(py_keep_fd);
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
125 if (keep_fd < 0) { /* Negative number, overflow or not a Long. */
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
126 err_msg = "bad value in fds_to_keep.";
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
127 errno = 0; /* We don't want to report an OSError. */
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
128 goto error;
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
129 }
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
130 if (keep_fd < start_fd)
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
131 continue;
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
132 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
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
133 close(fd_num);
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
134 }
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
135 start_fd = keep_fd + 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
136 }
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
137 if (start_fd <= max_fd) {
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
138 for (fd_num = start_fd; fd_num < max_fd; ++fd_num) {
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
139 close(fd_num);
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
140 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
141 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
142 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
143
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
144 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
145 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
146
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
147 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
148 _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
149
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150 #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
151 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
152 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
153 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
154
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
155 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
156 /* 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
157 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
158 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
159 /* 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
160 * 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
161 * 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
162 * 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
163 * 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
164 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
165 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
166 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
167 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
168 /* 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
169 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
170
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
171 /* 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
172 /* 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
173 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
174 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
175 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
176 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
177 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
178 } else {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
179 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
180 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
181 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
182 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
183 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
184 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
185 /* 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
186 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
187 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
188
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
189 error:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
190 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
191 /* Report the posix error to our 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
192 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
193 char *cur;
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
194 write(errpipe_write, "OSError:", 8);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
195 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
196 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
197 *--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
198 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
199 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
200 write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
201 write(errpipe_write, ":", 1);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
202 /* 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
203 * 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
204 } else {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205 write(errpipe_write, "RuntimeError:0:", 15);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
206 write(errpipe_write, err_msg, strlen(err_msg));
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
207 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
208 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
209
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
210
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
211 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
212 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
213 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
214 PyObject *gc_module = NULL;
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
215 PyObject *executable_list, *py_close_fds, *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
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
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
227 Py_ssize_t arg_num, num_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
228
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
229 if (!PyArg_ParseTuple(
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
230 args, "OOOOOOiiiiiiiiiiO:fork_exec",
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
231 &process_args, &executable_list, &py_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
232 &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
233 &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
234 &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
235 &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
236 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
237
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
238 close_fds = PyObject_IsTrue(py_close_fds);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
239 if (close_fds && errpipe_write < 3) { /* precondition */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
240 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
241 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
242 }
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
243 num_fds_to_keep = PySequence_Length(py_fds_to_keep);
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
244 if (num_fds_to_keep < 0) {
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
245 PyErr_SetString(PyExc_ValueError, "bad fds_to_keep");
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
246 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
247 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
248
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
249 /* 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
250 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
251 PyObject *result;
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
252 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
253 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
254 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
255 result = PyObject_CallMethod(gc_module, "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
256 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
257 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
258 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
259 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
260 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
261 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
262 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
263 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
264 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
265 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
266 result = PyObject_CallMethod(gc_module, "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
267 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
268 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
269 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
270 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
271 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
272 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
273
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
274 exec_array = _PySequence_BytesToCharpArray(executable_list);
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
275 if (!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
276 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
277
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
278 /* 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
279 /* 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
280 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
281 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
282 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
283 /* 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
284 /* 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
285 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
adb5e928bde2 * Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
286 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
287 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
288 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
289 goto cleanup;
59631
adb5e928bde2 * Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
290 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
291 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
292 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
293 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
294 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
295 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
296 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
297
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
298 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
299 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
300 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
301 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
302 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
303 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
304
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
305 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
306 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
307 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
308 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
309 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
310
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
311 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
312 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
313 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
314 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
315 _PyImport_AcquireLock();
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
316 }
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
317
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
318 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
319 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
320 goto cleanup;
60856
dd18cccb55b0 Issue #8485: PyUnicode_FSConverter() doesn't accept bytearray object anymore,
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60646
diff changeset
321 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
322 } else {
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
323 cwd = NULL;
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
324 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
325 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
326
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
327 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
328 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
329 /* 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
330 /*
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
331 * 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
332 * listed at `man 7 signal` or
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
333 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
334 */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
336 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
337 /* 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
338 * 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
339 * 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
340 * 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
341 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
342 }
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 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
345 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
346 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
347 close_fds, restore_signals, call_setsid,
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
348 num_fds_to_keep, 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
349 preexec_fn, 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
350 _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
351 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
352 }
60646
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
353 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
354
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
355 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
356 /* 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
357 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
358 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
359 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
360 _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
361 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
362 "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
363 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
364
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
365 /* 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
366 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
367 _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
368 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
369 _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
370 _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
371
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
372 /* 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
373 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
374 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
375 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
376 }
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
377 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
378 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
379
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
380 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
381 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
382
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
383 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
384
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
385 cleanup:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
386 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
387 _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
388 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
389 _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
390 _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
391 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
392 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
393 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
394
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
395 /* 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
396 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
397 _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
398 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
399 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
400 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
401
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
402
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
403 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
404 "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
405 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
406 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
407 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
408 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
409 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
410 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
411 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
412 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
413 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
414 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
415 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
416 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
417 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
418 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
419 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
420 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
421 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
422 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
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
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
425 PyDoc_STRVAR(subprocess_cloexec_pipe_doc,
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
426 "cloexec_pipe() -> (read_end, write_end)\n\n\
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
427 Create a pipe whose ends have the cloexec flag set.");
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
428
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
429 static PyObject *
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
430 subprocess_cloexec_pipe(PyObject *self, PyObject *noargs)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
431 {
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
432 int fds[2];
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
433 int res;
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
434 #ifdef HAVE_PIPE2
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
435 Py_BEGIN_ALLOW_THREADS
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
436 res = pipe2(fds, O_CLOEXEC);
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
437 Py_END_ALLOW_THREADS
67270
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
438 if (res != 0 && errno == ENOSYS)
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
439 {
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
440 if (PyErr_WarnEx(
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
441 PyExc_RuntimeWarning,
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
442 "pipe2 set errno ENOSYS; falling "
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
443 "back to non-atomic pipe+fcntl.", 1) != 0) {
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
444 return NULL;
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
445 }
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
446 {
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
447 #endif
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
448 /* We hold the GIL which offers some protection from other code calling
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
449 * fork() before the CLOEXEC flags have been set but we can't guarantee
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
450 * anything without pipe2(). */
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
451 long oldflags;
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
452
67270
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
453 res = pipe(fds);
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
454
67270
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
455 if (res == 0) {
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
456 oldflags = fcntl(fds[0], F_GETFD, 0);
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
457 if (oldflags < 0) res = oldflags;
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
458 }
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
459 if (res == 0)
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
460 res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
461
67270
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
462 if (res == 0) {
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
463 oldflags = fcntl(fds[1], F_GETFD, 0);
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
464 if (oldflags < 0) res = oldflags;
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
465 }
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
466 if (res == 0)
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
467 res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
468 #ifdef HAVE_PIPE2
a5ae3a17d5fb issue10802: fallback to pipe+fcntl when the pipe2 syscall fails with errno ENOSYS.
Gregory P. Smith <greg@mad-scientist.com>
parents: 67082
diff changeset
469 }
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
470 }
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
471 #endif
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
472 if (res != 0)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
473 return PyErr_SetFromErrno(PyExc_OSError);
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
474 return Py_BuildValue("(ii)", fds[0], fds[1]);
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
475 }
59605
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 /* 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
478
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
479 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
480 "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
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
483 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
484 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
485 {"cloexec_pipe", subprocess_cloexec_pipe, METH_NOARGS, subprocess_cloexec_pipe_doc},
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
486 {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
487 };
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
488
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
489
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
490 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
491 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
492 "_posixsubprocess",
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
493 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
494 -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
495 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
496 };
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
497
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
498 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
499 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
500 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
501 #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
502 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
503 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
504 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
505 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
506
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
507 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
508 }