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