blob: 24a70bc58e6ba3d0f42d57bde56384d38865a694 [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
3#include <unistd.h>
4
5
6#define POSIX_CALL(call) if ((call) == -1) goto error
7
8
9/* Maximum file descriptor, initialized on module load. */
10static long max_fd;
11
12
13/* Given the gc module call gc.enable() and return 0 on success. */
14static int _enable_gc(PyObject *gc_module)
15{
16 PyObject *result;
17 result = PyObject_CallMethod(gc_module, "enable", NULL);
18 if (result == NULL)
19 return 1;
20 Py_DECREF(result);
21 return 0;
22}
23
24
25/*
26 * This function is code executed in the child process immediately after fork
27 * to set things up and call exec().
28 *
29 * All of the code in this function must only use async-signal-safe functions,
30 * listed at `man 7 signal` or
31 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
32 *
33 * This restriction is documented at
34 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
35 */
36static void child_exec(char *const exec_array[],
37 char *const argv[],
38 char *const envp[],
39 const char *cwd,
40 int p2cread, int p2cwrite,
41 int c2pread, int c2pwrite,
42 int errread, int errwrite,
43 int errpipe_read, int errpipe_write,
44 int close_fds, int restore_signals,
45 int call_setsid,
46 PyObject *preexec_fn,
47 PyObject *preexec_fn_args_tuple)
48{
49 int i, saved_errno, fd_num;
50 PyObject *result;
51 const char* err_msg;
52 /* Buffer large enough to hold a hex integer. We can't malloc. */
53 char hex_errno[sizeof(saved_errno)*2+1];
54
55 /* Close parent's pipe ends. */
56 if (p2cwrite != -1) {
57 POSIX_CALL(close(p2cwrite));
58 }
59 if (c2pread != -1) {
60 POSIX_CALL(close(c2pread));
61 }
62 if (errread != -1) {
63 POSIX_CALL(close(errread));
64 }
65 POSIX_CALL(close(errpipe_read));
66
67 /* Dup fds for child. */
68 if (p2cread != -1) {
69 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
70 }
71 if (c2pwrite != -1) {
72 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
73 }
74 if (errwrite != -1) {
75 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
76 }
77
78 /* Close pipe fds. Make sure we don't close the same fd more than */
79 /* once, or standard fds. */
80 if (p2cread != -1 && p2cread != 0) {
81 POSIX_CALL(close(p2cread));
82 }
83 if (c2pwrite != -1 && c2pwrite != p2cread && c2pwrite != 1) {
84 POSIX_CALL(close(c2pwrite));
85 }
86 if (errwrite != -1 && errwrite != p2cread &&
87 errwrite != c2pwrite && errwrite != 2) {
88 POSIX_CALL(close(errwrite));
89 }
90
91 /* close() is intentionally not checked for errors here as we are closing */
92 /* a large range of fds, some of which may be invalid. */
93 if (close_fds) {
94 for (fd_num = 3; fd_num < errpipe_write; ++fd_num) {
95 close(fd_num);
96 }
97 for (fd_num = errpipe_write+1; fd_num < max_fd; ++fd_num) {
98 close(fd_num);
99 }
100 }
101
102 if (cwd)
103 POSIX_CALL(chdir(cwd));
104
105 if (restore_signals)
106 _Py_RestoreSignals();
107
108#ifdef HAVE_SETSID
109 if (call_setsid)
110 POSIX_CALL(setsid());
111#endif
112
113 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
114 /* This is where the user has asked us to deadlock their program. */
115 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
116 if (result == NULL) {
117 /* Stringifying the exception or traceback would involve
118 * memory allocation and thus potential for deadlock.
119 * We've already faced potential deadlock by calling back
120 * into Python in the first place, so it probably doesn't
121 * matter but we avoid it to minimize the possibility. */
122 err_msg = "Exception occurred in preexec_fn.";
123 errno = 0; /* We don't want to report an OSError. */
124 goto error;
125 }
126 /* Py_DECREF(result); - We're about to exec so why bother? */
127 }
128
129 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
130 /* given the executable_list generated by Lib/subprocess.py. */
131 saved_errno = 0;
132 for (i = 0; exec_array[i] != NULL; ++i) {
133 const char *executable = exec_array[i];
134 if (envp) {
135 execve(executable, argv, envp);
136 } else {
137 execv(executable, argv);
138 }
139 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
140 saved_errno = errno;
141 }
142 }
143 /* Report the first exec error, not the last. */
144 if (saved_errno)
145 errno = saved_errno;
146
147error:
148 saved_errno = errno;
149 /* Report the posix error to our parent process. */
150 if (saved_errno) {
151 char *cur;
152 write(errpipe_write, "OSError:", 8);
153 cur = hex_errno + sizeof(hex_errno);
154 while (saved_errno != 0 && cur > hex_errno) {
155 *--cur = "0123456789ABCDEF"[saved_errno % 16];
156 saved_errno /= 16;
157 }
158 write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
159 write(errpipe_write, ":", 1);
160 /* We can't call strerror(saved_errno). It is not async signal safe.
161 * The parent process will look the error message up. */
162 } else {
163 write(errpipe_write, "RuntimeError:0:", 15);
164 write(errpipe_write, err_msg, strlen(err_msg));
165 }
166}
167
168
169static PyObject *
170subprocess_fork_exec(PyObject* self, PyObject *args)
171{
172 PyObject *gc_module = NULL;
173 PyObject *executable_list, *py_close_fds;
174 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000175 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000176 PyObject *preexec_fn_args_tuple = NULL;
177 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
178 int errpipe_read, errpipe_write, close_fds, restore_signals;
179 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000180 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000181 const char *cwd;
182 pid_t pid;
183 int need_to_reenable_gc = 0;
184 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
185 Py_ssize_t arg_num;
186
187 if (!PyArg_ParseTuple(
Victor Stinner0e59cc32010-04-16 23:49:32 +0000188 args, "OOOOOiiiiiiiiiiO:fork_exec",
189 &process_args, &executable_list, &py_close_fds,
190 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000191 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
192 &errread, &errwrite, &errpipe_read, &errpipe_write,
193 &restore_signals, &call_setsid, &preexec_fn))
194 return NULL;
195
196 close_fds = PyObject_IsTrue(py_close_fds);
197 if (close_fds && errpipe_write < 3) { /* precondition */
198 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
199 return NULL;
200 }
201
202 /* We need to call gc.disable() when we'll be calling preexec_fn */
203 if (preexec_fn != Py_None) {
204 PyObject *result;
205 gc_module = PyImport_ImportModule("gc");
206 if (gc_module == NULL)
207 return NULL;
208 result = PyObject_CallMethod(gc_module, "isenabled", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000209 if (result == NULL) {
210 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000211 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000212 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000213 need_to_reenable_gc = PyObject_IsTrue(result);
214 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000215 if (need_to_reenable_gc == -1) {
216 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000217 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000218 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000219 result = PyObject_CallMethod(gc_module, "disable", NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000220 if (result == NULL) {
221 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000222 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000223 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000224 Py_DECREF(result);
225 }
226
227 exec_array = _PySequence_BytesToCharpArray(executable_list);
228 if (!exec_array)
229 return NULL;
230
231 /* Convert args and env into appropriate arguments for exec() */
232 /* These conversions are done in the parent process to avoid allocating
233 or freeing memory in the child process. */
234 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000235 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000236 /* Equivalent to: */
237 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000238 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
239 num_args = PySequence_Fast_GET_SIZE(fast_args);
240 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000241 if (converted_args == NULL)
242 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000243 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000244 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000245 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000246 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
247 goto cleanup;
248 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
249 }
250
251 argv = _PySequence_BytesToCharpArray(converted_args);
252 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000253 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000254 if (!argv)
255 goto cleanup;
256 }
257
258 if (env_list != Py_None) {
259 envp = _PySequence_BytesToCharpArray(env_list);
260 if (!envp)
261 goto cleanup;
262 }
263
264 if (preexec_fn != Py_None) {
265 preexec_fn_args_tuple = PyTuple_New(0);
266 if (!preexec_fn_args_tuple)
267 goto cleanup;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000268 _PyImport_AcquireLock();
269 }
270
271 if (cwd_obj != Py_None) {
272 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
273 goto cleanup;
274 if (PyBytes_Check(cwd_obj2))
275 cwd = PyBytes_AS_STRING(cwd_obj2);
276 else
277 cwd = PyByteArray_AS_STRING(cwd_obj2);
278 } else {
279 cwd = NULL;
280 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000281 }
282
283 pid = fork();
284 if (pid == 0) {
285 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000286 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000287 * Code from here to _exit() must only use async-signal-safe functions,
288 * listed at `man 7 signal` or
289 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
290 */
291
292 if (preexec_fn != Py_None) {
293 /* We'll be calling back into Python later so we need to do this.
294 * This call may not be async-signal-safe but neither is calling
295 * back into Python. The user asked us to use hope as a strategy
296 * to avoid deadlock... */
297 PyOS_AfterFork();
298 }
299
300 child_exec(exec_array, argv, envp, cwd,
301 p2cread, p2cwrite, c2pread, c2pwrite,
302 errread, errwrite, errpipe_read, errpipe_write,
303 close_fds, restore_signals, call_setsid,
304 preexec_fn, preexec_fn_args_tuple);
305 _exit(255);
306 return NULL; /* Dead code to avoid a potential compiler warning. */
307 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000308 Py_XDECREF(cwd_obj2);
309
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000310 if (pid == -1) {
311 /* Capture the errno exception before errno can be clobbered. */
312 PyErr_SetFromErrno(PyExc_OSError);
313 }
314 if (preexec_fn != Py_None &&
315 _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
316 PyErr_SetString(PyExc_RuntimeError,
317 "not holding the import lock");
318 }
319
320 /* Parent process */
321 if (envp)
322 _Py_FreeCharPArray(envp);
323 if (argv)
324 _Py_FreeCharPArray(argv);
325 _Py_FreeCharPArray(exec_array);
326
327 /* Reenable gc in the parent process (or if fork failed). */
328 if (need_to_reenable_gc && _enable_gc(gc_module)) {
329 Py_XDECREF(gc_module);
330 return NULL;
331 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000332 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000333 Py_XDECREF(gc_module);
334
335 if (pid == -1)
336 return NULL; /* fork() failed. Exception set earlier. */
337
338 return PyLong_FromPid(pid);
339
340cleanup:
341 if (envp)
342 _Py_FreeCharPArray(envp);
343 if (argv)
344 _Py_FreeCharPArray(argv);
345 _Py_FreeCharPArray(exec_array);
346 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000347 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000348 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000349
350 /* Reenable gc if it was disabled. */
351 if (need_to_reenable_gc)
352 _enable_gc(gc_module);
353 Py_XDECREF(gc_module);
354 return NULL;
355}
356
357
358PyDoc_STRVAR(subprocess_fork_exec_doc,
359"fork_exec(args, executable_list, close_fds, cwd, env,\n\
360 p2cread, p2cwrite, c2pread, c2pwrite,\n\
361 errread, errwrite, errpipe_read, errpipe_write,\n\
362 restore_signals, call_setsid, preexec_fn)\n\
363\n\
364Forks a child process, closes parent file descriptors as appropriate in the\n\
365child and dups the few that are needed before calling exec() in the child\n\
366process.\n\
367\n\
368The preexec_fn, if supplied, will be called immediately before exec.\n\
369WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
370 It may trigger infrequent, difficult to debug deadlocks.\n\
371\n\
372If an error occurs in the child process before the exec, it is\n\
373serialized and written to the errpipe_write fd per subprocess.py.\n\
374\n\
375Returns: the child process's PID.\n\
376\n\
377Raises: Only on an error in the parent process.\n\
378");
379
380
381/* module level code ********************************************************/
382
383PyDoc_STRVAR(module_doc,
384"A POSIX helper for the subprocess module.");
385
386
387static PyMethodDef module_methods[] = {
388 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
389 {NULL, NULL} /* sentinel */
390};
391
392
393static struct PyModuleDef _posixsubprocessmodule = {
394 PyModuleDef_HEAD_INIT,
395 "_posixsubprocess",
396 module_doc,
397 -1, /* No memory is needed. */
398 module_methods,
399};
400
401PyMODINIT_FUNC
402PyInit__posixsubprocess(void)
403{
404#ifdef _SC_OPEN_MAX
405 max_fd = sysconf(_SC_OPEN_MAX);
406 if (max_fd == -1)
407#endif
408 max_fd = 256; /* Matches Lib/subprocess.py */
409
410 return PyModule_Create(&_posixsubprocessmodule);
411}