_posixsubprocess.c.h 5.3 KB

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