annotate Modules/_posixsubprocess.c @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents 53ea5663b290
children 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"
70400
53ea5663b290 _posixsubprocess.c: don't redefine _GNU_SOURCE if it's already defined
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68520
diff changeset
3 #if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
53ea5663b290 _posixsubprocess.c: don't redefine _GNU_SOURCE if it's already defined
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68520
diff changeset
4 # define _GNU_SOURCE
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
5 #endif
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 #include <unistd.h>
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66692
diff changeset
7 #include <fcntl.h>
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
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
72 /* 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
73 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
74 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
75 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
76 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
77 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
78 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
79 } 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
80 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
81 }
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67270
diff changeset
82 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
83 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
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(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
86 } 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
87 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
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 (errwrite == 2) {
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(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
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(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
93 } 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
94 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
95 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
96
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
97 /* 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
98 /* 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
99 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
100 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
101 }
68520
adcf03b074b7 Fix issue #11432. if the stdin pipe is the same file descriptor as either stdout or stderr
Gregory P. Smith <greg@krypto.org>
parents: 67310
diff changeset
102 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
103 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
104 }
68520
adcf03b074b7 Fix issue #11432. if the stdin pipe is the same file descriptor as either stdout or stderr
Gregory P. Smith <greg@krypto.org>
parents: 67310
diff changeset
105 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
106 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
107 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
108
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
109 /* 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
110 /* 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 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
122 }
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
123 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
124 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
125 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
126 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
127 }
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 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
129 }
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
130 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
131 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
132 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
133 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
134 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
135 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
136
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
137 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
138 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
139
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
140 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
141 _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
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 #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
144 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
145 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
146 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
147
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
148 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
149 /* 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
150 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
151 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
152 /* 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
153 * 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
154 * 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
155 * 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
156 * 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
157 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
158 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
159 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
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 /* 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
162 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
163
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
164 /* 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
165 /* 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
166 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
167 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
168 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
169 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
170 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
171 } else {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
172 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
173 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
174 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
175 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
176 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
177 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
178 /* 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
179 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
180 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
181
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
182 error:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
183 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
184 /* 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
185 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
186 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
187 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
188 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
189 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
190 *--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
191 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
192 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
193 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
194 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
195 /* 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
196 * 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
197 } else {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
198 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
199 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
200 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
201 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
202
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
203
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
204 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
205 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
206 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
207 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
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
222 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
223 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
224 &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
225 &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
226 &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
227 &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
228 &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
229 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
230
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
231 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
232 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
233 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
234 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
235 }
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
236 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
237 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
238 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
239 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
240 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
241
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
242 /* 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
243 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
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
253 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
254 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
255 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
256 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
257 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
258 }
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 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
260 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
261 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
262 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
263 }
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 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
265 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
266
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
267 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
268 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
269 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
270
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
271 /* 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
272 /* 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
273 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
274 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
275 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
276 /* 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
277 /* 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
278 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
279 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
280 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
281 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
282 goto cleanup;
59631
adb5e928bde2 * Fix the refcount leak in _PySequence_BytesToCharpArray from r78946.
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
283 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
284 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
285 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
286 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
287 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
288 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
289 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
290
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
291 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
292 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
293 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
294 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
295 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
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 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
299 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
300 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
301 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
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
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 (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
305 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
306 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
307 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
308 _PyImport_AcquireLock();
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
309 }
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
310
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
311 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
312 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
313 goto cleanup;
60856
dd18cccb55b0 Issue #8485: PyUnicode_FSConverter() doesn't accept bytearray object anymore,
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60646
diff changeset
314 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
315 } else {
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
316 cwd = NULL;
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
317 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
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 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
321 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
322 /* 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
323 /*
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
324 * 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
325 * 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
326 * 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
327 */
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
328
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
329 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
330 /* 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
331 * 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
332 * 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
333 * 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
334 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
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
337 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
338 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
339 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
340 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
341 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
342 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
343 _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
344 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
345 }
60646
dc765bca24ad Issue #8393: subprocess accepts bytes, bytearray and str with surrogates for
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59723
diff changeset
346 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
347
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348 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
349 /* 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
350 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
351 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
352 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
353 _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
354 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
355 "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
356 }
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
357
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
358 /* 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
359 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
360 _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
361 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
362 _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
363 _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
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 /* 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
366 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
367 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
368 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
369 }
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
370 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
371 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
372
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 (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
374 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
375
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
376 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
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 cleanup:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
379 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
380 _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
381 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
382 _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
383 _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
384 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
385 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
386 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
387
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
388 /* 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
389 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
390 _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
391 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
392 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
393 }
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396 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
397 "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
398 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
399 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
400 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
401 \n\
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
402 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
403 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
404 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
405 \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 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
407 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
408 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
409 \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 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
411 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
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 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
414 \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 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
416 ");
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
417
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
418 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
419 "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
420 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
421
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
422 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
423 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
424 {
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 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
426 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
427 #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
428 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
429 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
430 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
431 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
432 {
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
433 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
434 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
435 "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
436 "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
437 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
438 }
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 #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
441 /* 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
442 * 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
443 * 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
444 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
445
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
446 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
447
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
448 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
449 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
450 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
451 }
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
452 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
453 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
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[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
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[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
461 #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
462 }
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
463 }
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
464 #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
465 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
466 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
467 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
468 }
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
469
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
470 /* 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
471
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
472 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
473 "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
474
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
475
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
476 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
477 {"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
478 {"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
479 {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
480 };
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 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
484 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
485 "_posixsubprocess",
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
486 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
487 -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
488 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
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
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
491 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
492 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
493 {
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
494 #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
495 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
496 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
497 #endif
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
498 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
499
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
500 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
501 }