• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "          allow_vfork, /)\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, int allow_vfork);
52 
53 static PyObject *
subprocess_fork_exec(PyObject * module,PyObject * const * args,Py_ssize_t nargs)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     int allow_vfork;
80 
81     if (!_PyArg_CheckPositional("fork_exec", nargs, 23, 23)) {
82         goto exit;
83     }
84     process_args = args[0];
85     executable_list = args[1];
86     close_fds = PyObject_IsTrue(args[2]);
87     if (close_fds < 0) {
88         goto exit;
89     }
90     if (!PyTuple_Check(args[3])) {
91         _PyArg_BadArgument("fork_exec", "argument 4", "tuple", args[3]);
92         goto exit;
93     }
94     py_fds_to_keep = args[3];
95     cwd_obj = args[4];
96     env_list = args[5];
97     p2cread = PyLong_AsInt(args[6]);
98     if (p2cread == -1 && PyErr_Occurred()) {
99         goto exit;
100     }
101     p2cwrite = PyLong_AsInt(args[7]);
102     if (p2cwrite == -1 && PyErr_Occurred()) {
103         goto exit;
104     }
105     c2pread = PyLong_AsInt(args[8]);
106     if (c2pread == -1 && PyErr_Occurred()) {
107         goto exit;
108     }
109     c2pwrite = PyLong_AsInt(args[9]);
110     if (c2pwrite == -1 && PyErr_Occurred()) {
111         goto exit;
112     }
113     errread = PyLong_AsInt(args[10]);
114     if (errread == -1 && PyErr_Occurred()) {
115         goto exit;
116     }
117     errwrite = PyLong_AsInt(args[11]);
118     if (errwrite == -1 && PyErr_Occurred()) {
119         goto exit;
120     }
121     errpipe_read = PyLong_AsInt(args[12]);
122     if (errpipe_read == -1 && PyErr_Occurred()) {
123         goto exit;
124     }
125     errpipe_write = PyLong_AsInt(args[13]);
126     if (errpipe_write == -1 && PyErr_Occurred()) {
127         goto exit;
128     }
129     restore_signals = PyObject_IsTrue(args[14]);
130     if (restore_signals < 0) {
131         goto exit;
132     }
133     call_setsid = PyObject_IsTrue(args[15]);
134     if (call_setsid < 0) {
135         goto exit;
136     }
137     pgid_to_set = PyLong_AsPid(args[16]);
138     if (pgid_to_set == -1 && PyErr_Occurred()) {
139         goto exit;
140     }
141     gid_object = args[17];
142     extra_groups_packed = args[18];
143     uid_object = args[19];
144     child_umask = PyLong_AsInt(args[20]);
145     if (child_umask == -1 && PyErr_Occurred()) {
146         goto exit;
147     }
148     preexec_fn = args[21];
149     allow_vfork = PyObject_IsTrue(args[22]);
150     if (allow_vfork < 0) {
151         goto exit;
152     }
153     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);
154 
155 exit:
156     return return_value;
157 }
158 /*[clinic end generated code: output=48555f5965a871be input=a9049054013a1b77]*/
159