execute.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Creation of autonomous subprocesses.
  2. Copyright (C) 2001-2004, 2006-2013 Free Software Foundation, Inc.
  3. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "execute.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdbool.h>
  20. #include <stdlib.h>
  21. #include <signal.h>
  22. #include <unistd.h>
  23. #include "penviron.h"
  24. #include "error.h"
  25. #include "fatal-signal.h"
  26. #include "wait-process.h"
  27. #include "gettext.h"
  28. #define _(str) gettext (str)
  29. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  30. /* Native Windows API. */
  31. # include <process.h>
  32. # include "w32spawn.h"
  33. #else
  34. /* Unix API. */
  35. # include <spawn.h>
  36. #endif
  37. /* The results of open() in this file are not used with fchdir,
  38. therefore save some unnecessary work in fchdir.c. */
  39. #undef open
  40. #undef close
  41. #if defined EINTR && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
  42. /* EINTR handling for close(), open().
  43. These functions can return -1/EINTR even though we don't have any
  44. signal handlers set up, namely when we get interrupted via SIGSTOP. */
  45. static int
  46. nonintr_close (int fd)
  47. {
  48. int retval;
  49. do
  50. retval = close (fd);
  51. while (retval < 0 && errno == EINTR);
  52. return retval;
  53. }
  54. #define close nonintr_close
  55. static int
  56. nonintr_open (const char *pathname, int oflag, mode_t mode)
  57. {
  58. int retval;
  59. do
  60. retval = open (pathname, oflag, mode);
  61. while (retval < 0 && errno == EINTR);
  62. return retval;
  63. }
  64. #undef open /* avoid warning on VMS */
  65. #define open nonintr_open
  66. #endif
  67. /* Execute a command, optionally redirecting any of the three standard file
  68. descriptors to /dev/null. Return its exit code.
  69. If it didn't terminate correctly, exit if exit_on_error is true, otherwise
  70. return 127.
  71. If slave_process is true, the child process will be terminated when its
  72. creator receives a catchable fatal signal. */
  73. int
  74. execute (const char *progname,
  75. const char *prog_path, char **prog_argv,
  76. bool ignore_sigpipe,
  77. bool null_stdin, bool null_stdout, bool null_stderr,
  78. bool slave_process, bool exit_on_error,
  79. int *termsigp)
  80. {
  81. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  82. /* Native Windows API. */
  83. int orig_stdin;
  84. int orig_stdout;
  85. int orig_stderr;
  86. int exitcode;
  87. int nullinfd;
  88. int nulloutfd;
  89. /* FIXME: Need to free memory allocated by prepare_spawn. */
  90. prog_argv = prepare_spawn (prog_argv);
  91. /* Save standard file handles of parent process. */
  92. if (null_stdin)
  93. orig_stdin = dup_safer_noinherit (STDIN_FILENO);
  94. if (null_stdout)
  95. orig_stdout = dup_safer_noinherit (STDOUT_FILENO);
  96. if (null_stderr)
  97. orig_stderr = dup_safer_noinherit (STDERR_FILENO);
  98. exitcode = -1;
  99. /* Create standard file handles of child process. */
  100. nullinfd = -1;
  101. nulloutfd = -1;
  102. if ((!null_stdin
  103. || ((nullinfd = open ("NUL", O_RDONLY, 0)) >= 0
  104. && (nullinfd == STDIN_FILENO
  105. || (dup2 (nullinfd, STDIN_FILENO) >= 0
  106. && close (nullinfd) >= 0))))
  107. && (!(null_stdout || null_stderr)
  108. || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
  109. && (!null_stdout
  110. || nulloutfd == STDOUT_FILENO
  111. || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
  112. && (!null_stderr
  113. || nulloutfd == STDERR_FILENO
  114. || dup2 (nulloutfd, STDERR_FILENO) >= 0)
  115. && ((null_stdout && nulloutfd == STDOUT_FILENO)
  116. || (null_stderr && nulloutfd == STDERR_FILENO)
  117. || close (nulloutfd) >= 0))))
  118. /* Use spawnvpe and pass the environment explicitly. This is needed if
  119. the program has modified the environment using putenv() or [un]setenv().
  120. On Windows, programs have two environments, one in the "environment
  121. block" of the process and managed through SetEnvironmentVariable(), and
  122. one inside the process, in the location retrieved by the 'environ'
  123. macro. When using spawnvp() without 'e', the child process inherits a
  124. copy of the environment block - ignoring the effects of putenv() and
  125. [un]setenv(). */
  126. {
  127. exitcode = spawnvpe (P_WAIT, prog_path, (const char **) prog_argv,
  128. (const char **) environ);
  129. if (exitcode < 0 && errno == ENOEXEC)
  130. {
  131. /* prog is not a native executable. Try to execute it as a
  132. shell script. Note that prepare_spawn() has already prepended
  133. a hidden element "sh.exe" to prog_argv. */
  134. --prog_argv;
  135. exitcode = spawnvpe (P_WAIT, prog_argv[0], (const char **) prog_argv,
  136. (const char **) environ);
  137. }
  138. }
  139. if (nulloutfd >= 0)
  140. close (nulloutfd);
  141. if (nullinfd >= 0)
  142. close (nullinfd);
  143. /* Restore standard file handles of parent process. */
  144. if (null_stderr)
  145. undup_safer_noinherit (orig_stderr, STDERR_FILENO);
  146. if (null_stdout)
  147. undup_safer_noinherit (orig_stdout, STDOUT_FILENO);
  148. if (null_stdin)
  149. undup_safer_noinherit (orig_stdin, STDIN_FILENO);
  150. if (termsigp != NULL)
  151. *termsigp = 0;
  152. if (exitcode == -1)
  153. {
  154. if (exit_on_error || !null_stderr)
  155. error (exit_on_error ? EXIT_FAILURE : 0, errno,
  156. _("%s subprocess failed"), progname);
  157. return 127;
  158. }
  159. return exitcode;
  160. #else
  161. /* Unix API. */
  162. /* Note about 127: Some errors during posix_spawnp() cause the function
  163. posix_spawnp() to return an error code; some other errors cause the
  164. subprocess to exit with return code 127. It is implementation
  165. dependent which error is reported which way. We treat both cases as
  166. equivalent. */
  167. sigset_t blocked_signals;
  168. posix_spawn_file_actions_t actions;
  169. bool actions_allocated;
  170. posix_spawnattr_t attrs;
  171. bool attrs_allocated;
  172. int err;
  173. pid_t child;
  174. if (slave_process)
  175. {
  176. sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
  177. block_fatal_signals ();
  178. }
  179. actions_allocated = false;
  180. attrs_allocated = false;
  181. if ((err = posix_spawn_file_actions_init (&actions)) != 0
  182. || (actions_allocated = true,
  183. (null_stdin
  184. && (err = posix_spawn_file_actions_addopen (&actions,
  185. STDIN_FILENO,
  186. "/dev/null", O_RDONLY,
  187. 0))
  188. != 0)
  189. || (null_stdout
  190. && (err = posix_spawn_file_actions_addopen (&actions,
  191. STDOUT_FILENO,
  192. "/dev/null", O_RDWR,
  193. 0))
  194. != 0)
  195. || (null_stderr
  196. && (err = posix_spawn_file_actions_addopen (&actions,
  197. STDERR_FILENO,
  198. "/dev/null", O_RDWR,
  199. 0))
  200. != 0)
  201. || (slave_process
  202. && ((err = posix_spawnattr_init (&attrs)) != 0
  203. || (attrs_allocated = true,
  204. (err = posix_spawnattr_setsigmask (&attrs,
  205. &blocked_signals))
  206. != 0
  207. || (err = posix_spawnattr_setflags (&attrs,
  208. POSIX_SPAWN_SETSIGMASK))
  209. != 0)))
  210. || (err = posix_spawnp (&child, prog_path, &actions,
  211. attrs_allocated ? &attrs : NULL, prog_argv,
  212. environ))
  213. != 0))
  214. {
  215. if (actions_allocated)
  216. posix_spawn_file_actions_destroy (&actions);
  217. if (attrs_allocated)
  218. posix_spawnattr_destroy (&attrs);
  219. if (slave_process)
  220. unblock_fatal_signals ();
  221. if (termsigp != NULL)
  222. *termsigp = 0;
  223. if (exit_on_error || !null_stderr)
  224. error (exit_on_error ? EXIT_FAILURE : 0, err,
  225. _("%s subprocess failed"), progname);
  226. return 127;
  227. }
  228. posix_spawn_file_actions_destroy (&actions);
  229. if (attrs_allocated)
  230. posix_spawnattr_destroy (&attrs);
  231. if (slave_process)
  232. {
  233. register_slave_subprocess (child);
  234. unblock_fatal_signals ();
  235. }
  236. return wait_subprocess (child, progname, ignore_sigpipe, null_stderr,
  237. slave_process, exit_on_error, termsigp);
  238. #endif
  239. }