annotate Modules/_posixsubprocess.c @ 66807:d28126ebd60d

Entries for datetime, callable, and collections.Counter.
author Raymond Hettinger <python@rcn.com>
date Thu, 09 Dec 2010 16:41:54 +0000
parents 296e6f85f56d
children de05d846795e
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"
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 #include <unistd.h>
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
5
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 #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
7
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 /* 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
10 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
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 /* 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
14 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
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 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
17 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
18 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
19 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
20 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
21 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
22 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
23
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
24
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
25 /*
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
26 * 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
27 * 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
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 * 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
30 * 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
31 * 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
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 * 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
34 * 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
35 */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48 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
49 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
50 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
51 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
52 const char* 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
53 /* 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
54 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
55
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
56 /* 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
57 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
58 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
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 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
61 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
62 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
63 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
64 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
65 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
66 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
67
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
68 /* Dup fds for child. */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
69 if (p2cread != -1) {
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(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
71 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
72 if (c2pwrite != -1) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
73 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
74 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
75 if (errwrite != -1) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
76 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
77 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
78
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
79 /* 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
80 /* once, or standard fds. */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
81 if (p2cread != -1 && p2cread != 0) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
82 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
83 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
84 if (c2pwrite != -1 && c2pwrite != p2cread && c2pwrite != 1) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
85 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
86 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
87 if (errwrite != -1 && errwrite != p2cread &&
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
88 errwrite != c2pwrite && errwrite != 2) {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
89 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
90 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
91
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
92 /* 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
93 /* 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 }
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
106 if (keep_fd <= start_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
107 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
108 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
109 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
110 }
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
111 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
112 }
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
113 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
114 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
115 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
116 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
117 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
118 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
119
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
120 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
121 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
122
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
123 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
124 _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
125
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
126 #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
127 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
128 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
129 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
130
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
131 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
132 /* 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
133 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
134 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
135 /* 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
136 * 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
137 * 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
138 * 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
139 * 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
140 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
141 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
142 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
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 /* 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
145 }
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 /* 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
148 /* 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
149 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
150 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
151 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
152 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
153 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
154 } else {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
155 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
156 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
157 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
158 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
159 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
160 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
161 /* 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
162 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
163 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
164
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
165 error:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
166 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
167 /* 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
168 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
169 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
170 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
171 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
172 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
173 *--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
174 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
175 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
176 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
177 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
178 /* 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
179 * 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
180 } else {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
181 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
182 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
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
186
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
187 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
188 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
189 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
190 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
191 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
192 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
193 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
194 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
195 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
196 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
197 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
198 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
199 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
200 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
201 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
202 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
203 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
204
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205 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
206 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
207 &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
208 &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
209 &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
210 &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
211 &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
212 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
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 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
215 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
216 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
217 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
218 }
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
219 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
220 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
221 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
222 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
223 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
224
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
225 /* 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
236 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
237 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
238 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
239 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
240 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
241 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
242 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
243 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
244 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
245 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
246 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
247 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
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
250 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
251 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
252 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
253
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
254 /* 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
255 /* 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
256 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
257 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
258 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
259 /* 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
260 /* 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
261 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
262 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
263 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
264 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
265 goto cleanup;
59631
adb5e928bde2 * Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
266 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
267 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
268 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
269 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
270 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
271 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
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 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
275 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
276 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
277 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
278 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
279 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
280
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 (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
282 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
283 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
284 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
285 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
287 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
288 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
289 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
290 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
291 _PyImport_AcquireLock();
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
292 }
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
293
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
294 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
295 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
296 goto cleanup;
60856
dd18cccb55b0 Issue #8485: PyUnicode_FSConverter() doesn't accept bytearray object anymore,
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60646
diff changeset
297 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
298 } else {
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
299 cwd = NULL;
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
300 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
301 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
302
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
303 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
304 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
305 /* 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
306 /*
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
307 * 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
308 * 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
309 * 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
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
312 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
313 /* 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
314 * 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
315 * 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
316 * 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
317 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
318 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
319
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
320 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
321 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
322 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
323 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
324 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
325 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
326 _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
327 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
328 }
60646
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
329 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
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 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
332 /* 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
333 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
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 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
336 _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
337 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
338 "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
339 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
340
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
341 /* 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
342 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
343 _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
344 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
345 _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
346 _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
347
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348 /* 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
349 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
350 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
351 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
352 }
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
353 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
354 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
355
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
356 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
357 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
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 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
360
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
361 cleanup:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
362 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
363 _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
364 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
365 _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
366 _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
367 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
368 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
369 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
370
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
371 /* 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
372 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
373 _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 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
377
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
378
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
379 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
380 "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
381 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
382 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
383 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
384 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
385 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
386 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
387 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
388 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
389 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
390 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
391 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
392 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
393 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
394 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
395 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396 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
397 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
398 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
399 ");
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 /* 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
403
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
404 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
405 "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
406
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
407
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
408 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
409 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
410 {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
411 };
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
412
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
413
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
414 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
415 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
416 "_posixsubprocess",
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
417 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
418 -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
419 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
420 };
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
421
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
422 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
423 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
424 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
425 #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
426 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
427 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
428 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
429 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
430
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
431 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
432 }