| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#include "pycore_modsupport.h" // _PyArg_CheckPositional() |
| 6 |
|
| 7 |
PyDoc_STRVAR(subprocess_fork_exec__doc__, |
| 8 |
"fork_exec($module, args, executable_list, close_fds, pass_fds, cwd,\n" |
| 9 |
" env, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite,\n" |
| 10 |
" errpipe_read, errpipe_write, restore_signals, call_setsid,\n" |
| 11 |
" pgid_to_set, gid, extra_groups, uid, child_umask, preexec_fn,\n" |
| 12 |
" /)\n" |
| 13 |
"--\n" |
| 14 |
"\n" |
| 15 |
"Spawn a fresh new child process.\n" |
| 16 |
"\n" |
| 17 |
"Fork a child process, close parent file descriptors as appropriate in the\n" |
| 18 |
"child and duplicate the few that are needed before calling exec() in the\n" |
| 19 |
"child process.\n" |
| 20 |
"\n" |
| 21 |
"If close_fds is True, close file descriptors 3 and higher, except those listed\n" |
| 22 |
"in the sorted tuple pass_fds.\n" |
| 23 |
"\n" |
| 24 |
"The preexec_fn, if supplied, will be called immediately before closing file\n" |
| 25 |
"descriptors and exec.\n" |
| 26 |
"\n" |
| 27 |
"WARNING: preexec_fn is NOT SAFE if your application uses threads.\n" |
| 28 |
" It may trigger infrequent, difficult to debug deadlocks.\n" |
| 29 |
"\n" |
| 30 |
"If an error occurs in the child process before the exec, it is\n" |
| 31 |
"serialized and written to the errpipe_write fd per subprocess.py.\n" |
| 32 |
"\n" |
| 33 |
"Returns: the child process\'s PID.\n" |
| 34 |
"\n" |
| 35 |
"Raises: Only on an error in the parent process."); |
| 36 |
|
| 37 |
#define SUBPROCESS_FORK_EXEC_METHODDEF \ |
| 38 |
{"fork_exec", _PyCFunction_CAST(subprocess_fork_exec), METH_FASTCALL, subprocess_fork_exec__doc__}, |
| 39 |
|
| 40 |
static PyObject * |
| 41 |
subprocess_fork_exec_impl(PyObject *module, PyObject *process_args, |
| 42 |
PyObject *executable_list, int close_fds, |
| 43 |
PyObject *py_fds_to_keep, PyObject *cwd_obj, |
| 44 |
PyObject *env_list, int p2cread, int p2cwrite, |
| 45 |
int c2pread, int c2pwrite, int errread, |
| 46 |
int errwrite, int errpipe_read, int errpipe_write, |
| 47 |
int restore_signals, int call_setsid, |
| 48 |
pid_t pgid_to_set, PyObject *gid_object, |
| 49 |
PyObject *extra_groups_packed, |
| 50 |
PyObject *uid_object, int child_umask, |
| 51 |
PyObject *preexec_fn); |
| 52 |
|
| 53 |
static PyObject * |
| 54 |
subprocess_fork_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 55 |
{ |
| 56 |
PyObject *return_value = NULL; |
| 57 |
PyObject *process_args; |
| 58 |
PyObject *executable_list; |
| 59 |
int close_fds; |
| 60 |
PyObject *py_fds_to_keep; |
| 61 |
PyObject *cwd_obj; |
| 62 |
PyObject *env_list; |
| 63 |
int p2cread; |
| 64 |
int p2cwrite; |
| 65 |
int c2pread; |
| 66 |
int c2pwrite; |
| 67 |
int errread; |
| 68 |
int errwrite; |
| 69 |
int errpipe_read; |
| 70 |
int errpipe_write; |
| 71 |
int restore_signals; |
| 72 |
int call_setsid; |
| 73 |
pid_t pgid_to_set; |
| 74 |
PyObject *gid_object; |
| 75 |
PyObject *extra_groups_packed; |
| 76 |
PyObject *uid_object; |
| 77 |
int child_umask; |
| 78 |
PyObject *preexec_fn; |
| 79 |
|
| 80 |
if (!_PyArg_CheckPositional("fork_exec", nargs, 22, 22)) { |
| 81 |
goto exit; |
| 82 |
} |
| 83 |
process_args = args[0]; |
| 84 |
executable_list = args[1]; |
| 85 |
close_fds = PyObject_IsTrue(args[2]); |
| 86 |
if (close_fds < 0) { |
| 87 |
goto exit; |
| 88 |
} |
| 89 |
if (!PyTuple_Check(args[3])) { |
| 90 |
_PyArg_BadArgument("fork_exec", "argument 4", "tuple", args[3]); |
| 91 |
goto exit; |
| 92 |
} |
| 93 |
py_fds_to_keep = args[3]; |
| 94 |
cwd_obj = args[4]; |
| 95 |
env_list = args[5]; |
| 96 |
p2cread = PyLong_AsInt(args[6]); |
| 97 |
if (p2cread == -1 && PyErr_Occurred()) { |
| 98 |
goto exit; |
| 99 |
} |
| 100 |
p2cwrite = PyLong_AsInt(args[7]); |
| 101 |
if (p2cwrite == -1 && PyErr_Occurred()) { |
| 102 |
goto exit; |
| 103 |
} |
| 104 |
c2pread = PyLong_AsInt(args[8]); |
| 105 |
if (c2pread == -1 && PyErr_Occurred()) { |
| 106 |
goto exit; |
| 107 |
} |
| 108 |
c2pwrite = PyLong_AsInt(args[9]); |
| 109 |
if (c2pwrite == -1 && PyErr_Occurred()) { |
| 110 |
goto exit; |
| 111 |
} |
| 112 |
errread = PyLong_AsInt(args[10]); |
| 113 |
if (errread == -1 && PyErr_Occurred()) { |
| 114 |
goto exit; |
| 115 |
} |
| 116 |
errwrite = PyLong_AsInt(args[11]); |
| 117 |
if (errwrite == -1 && PyErr_Occurred()) { |
| 118 |
goto exit; |
| 119 |
} |
| 120 |
errpipe_read = PyLong_AsInt(args[12]); |
| 121 |
if (errpipe_read == -1 && PyErr_Occurred()) { |
| 122 |
goto exit; |
| 123 |
} |
| 124 |
errpipe_write = PyLong_AsInt(args[13]); |
| 125 |
if (errpipe_write == -1 && PyErr_Occurred()) { |
| 126 |
goto exit; |
| 127 |
} |
| 128 |
restore_signals = PyObject_IsTrue(args[14]); |
| 129 |
if (restore_signals < 0) { |
| 130 |
goto exit; |
| 131 |
} |
| 132 |
call_setsid = PyObject_IsTrue(args[15]); |
| 133 |
if (call_setsid < 0) { |
| 134 |
goto exit; |
| 135 |
} |
| 136 |
pgid_to_set = PyLong_AsPid(args[16]); |
| 137 |
if (pgid_to_set == -1 && PyErr_Occurred()) { |
| 138 |
goto exit; |
| 139 |
} |
| 140 |
gid_object = args[17]; |
| 141 |
extra_groups_packed = args[18]; |
| 142 |
uid_object = args[19]; |
| 143 |
child_umask = PyLong_AsInt(args[20]); |
| 144 |
if (child_umask == -1 && PyErr_Occurred()) { |
| 145 |
goto exit; |
| 146 |
} |
| 147 |
preexec_fn = args[21]; |
| 148 |
return_value = subprocess_fork_exec_impl(module, process_args, executable_list, close_fds, py_fds_to_keep, cwd_obj, env_list, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, errpipe_read, errpipe_write, restore_signals, call_setsid, pgid_to_set, gid_object, extra_groups_packed, uid_object, child_umask, preexec_fn); |
| 149 |
|
| 150 |
exit: |
| 151 |
return return_value; |
| 152 |
} |
| 153 |
/*[clinic end generated code: output=942bc2748a9c2785 input=a9049054013a1b77]*/ |