spawn-pipe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Creation of subprocesses, communicating via pipes.
  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 "spawn-pipe.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdlib.h>
  20. #include <signal.h>
  21. #include <unistd.h>
  22. #include "penviron.h"
  23. #include "error.h"
  24. #include "fatal-signal.h"
  25. #include "unistd-safer.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. #ifdef EINTR
  42. /* EINTR handling for close().
  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. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  56. static int
  57. nonintr_open (const char *pathname, int oflag, mode_t mode)
  58. {
  59. int retval;
  60. do
  61. retval = open (pathname, oflag, mode);
  62. while (retval < 0 && errno == EINTR);
  63. return retval;
  64. }
  65. # undef open /* avoid warning on VMS */
  66. # define open nonintr_open
  67. #endif
  68. #endif
  69. /* Open a pipe connected to a child process.
  70. *
  71. * write system read
  72. * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
  73. * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
  74. * read system write
  75. *
  76. * At least one of pipe_stdin, pipe_stdout must be true.
  77. * pipe_stdin and prog_stdin together determine the child's standard input.
  78. * pipe_stdout and prog_stdout together determine the child's standard output.
  79. * If pipe_stdin is true, prog_stdin is ignored.
  80. * If pipe_stdout is true, prog_stdout is ignored.
  81. */
  82. static pid_t
  83. create_pipe (const char *progname,
  84. const char *prog_path, char **prog_argv,
  85. bool pipe_stdin, bool pipe_stdout,
  86. const char *prog_stdin, const char *prog_stdout,
  87. bool null_stderr,
  88. bool slave_process, bool exit_on_error,
  89. int fd[2])
  90. {
  91. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  92. /* Native Windows API.
  93. This uses _pipe(), dup2(), and spawnv(). It could also be implemented
  94. using the low-level functions CreatePipe(), DuplicateHandle(),
  95. CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
  96. and cvs source code. */
  97. int ifd[2];
  98. int ofd[2];
  99. int orig_stdin;
  100. int orig_stdout;
  101. int orig_stderr;
  102. int child;
  103. int nulloutfd;
  104. int stdinfd;
  105. int stdoutfd;
  106. int saved_errno;
  107. /* FIXME: Need to free memory allocated by prepare_spawn. */
  108. prog_argv = prepare_spawn (prog_argv);
  109. if (pipe_stdout)
  110. if (pipe2_safer (ifd, O_BINARY | O_CLOEXEC) < 0)
  111. error (EXIT_FAILURE, errno, _("cannot create pipe"));
  112. if (pipe_stdin)
  113. if (pipe2_safer (ofd, O_BINARY | O_CLOEXEC) < 0)
  114. error (EXIT_FAILURE, errno, _("cannot create pipe"));
  115. /* Data flow diagram:
  116. *
  117. * write system read
  118. * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
  119. * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
  120. * read system write
  121. *
  122. */
  123. /* Save standard file handles of parent process. */
  124. if (pipe_stdin || prog_stdin != NULL)
  125. orig_stdin = dup_safer_noinherit (STDIN_FILENO);
  126. if (pipe_stdout || prog_stdout != NULL)
  127. orig_stdout = dup_safer_noinherit (STDOUT_FILENO);
  128. if (null_stderr)
  129. orig_stderr = dup_safer_noinherit (STDERR_FILENO);
  130. child = -1;
  131. /* Create standard file handles of child process. */
  132. nulloutfd = -1;
  133. stdinfd = -1;
  134. stdoutfd = -1;
  135. if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
  136. && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
  137. && (!null_stderr
  138. || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
  139. && (nulloutfd == STDERR_FILENO
  140. || (dup2 (nulloutfd, STDERR_FILENO) >= 0
  141. && close (nulloutfd) >= 0))))
  142. && (pipe_stdin
  143. || prog_stdin == NULL
  144. || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
  145. && (stdinfd == STDIN_FILENO
  146. || (dup2 (stdinfd, STDIN_FILENO) >= 0
  147. && close (stdinfd) >= 0))))
  148. && (pipe_stdout
  149. || prog_stdout == NULL
  150. || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
  151. && (stdoutfd == STDOUT_FILENO
  152. || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
  153. && close (stdoutfd) >= 0)))))
  154. /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
  155. but it inherits all open()ed or dup2()ed file handles (which is what
  156. we want in the case of STD*_FILENO). */
  157. /* Use spawnvpe and pass the environment explicitly. This is needed if
  158. the program has modified the environment using putenv() or [un]setenv().
  159. On Windows, programs have two environments, one in the "environment
  160. block" of the process and managed through SetEnvironmentVariable(), and
  161. one inside the process, in the location retrieved by the 'environ'
  162. macro. When using spawnvp() without 'e', the child process inherits a
  163. copy of the environment block - ignoring the effects of putenv() and
  164. [un]setenv(). */
  165. {
  166. child = spawnvpe (P_NOWAIT, prog_path, (const char **) prog_argv,
  167. (const char **) environ);
  168. if (child < 0 && errno == ENOEXEC)
  169. {
  170. /* prog is not a native executable. Try to execute it as a
  171. shell script. Note that prepare_spawn() has already prepended
  172. a hidden element "sh.exe" to prog_argv. */
  173. --prog_argv;
  174. child = spawnvpe (P_NOWAIT, prog_argv[0], (const char **) prog_argv,
  175. (const char **) environ);
  176. }
  177. }
  178. if (child == -1)
  179. saved_errno = errno;
  180. if (stdinfd >= 0)
  181. close (stdinfd);
  182. if (stdoutfd >= 0)
  183. close (stdoutfd);
  184. if (nulloutfd >= 0)
  185. close (nulloutfd);
  186. /* Restore standard file handles of parent process. */
  187. if (null_stderr)
  188. undup_safer_noinherit (orig_stderr, STDERR_FILENO);
  189. if (pipe_stdout || prog_stdout != NULL)
  190. undup_safer_noinherit (orig_stdout, STDOUT_FILENO);
  191. if (pipe_stdin || prog_stdin != NULL)
  192. undup_safer_noinherit (orig_stdin, STDIN_FILENO);
  193. if (pipe_stdin)
  194. close (ofd[0]);
  195. if (pipe_stdout)
  196. close (ifd[1]);
  197. if (child == -1)
  198. {
  199. if (exit_on_error || !null_stderr)
  200. error (exit_on_error ? EXIT_FAILURE : 0, saved_errno,
  201. _("%s subprocess failed"), progname);
  202. if (pipe_stdout)
  203. close (ifd[0]);
  204. if (pipe_stdin)
  205. close (ofd[1]);
  206. errno = saved_errno;
  207. return -1;
  208. }
  209. if (pipe_stdout)
  210. fd[0] = ifd[0];
  211. if (pipe_stdin)
  212. fd[1] = ofd[1];
  213. return child;
  214. #else
  215. /* Unix API. */
  216. int ifd[2];
  217. int ofd[2];
  218. sigset_t blocked_signals;
  219. posix_spawn_file_actions_t actions;
  220. bool actions_allocated;
  221. posix_spawnattr_t attrs;
  222. bool attrs_allocated;
  223. int err;
  224. pid_t child;
  225. if (pipe_stdout)
  226. if (pipe_safer (ifd) < 0)
  227. error (EXIT_FAILURE, errno, _("cannot create pipe"));
  228. if (pipe_stdin)
  229. if (pipe_safer (ofd) < 0)
  230. error (EXIT_FAILURE, errno, _("cannot create pipe"));
  231. /* Data flow diagram:
  232. *
  233. * write system read
  234. * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
  235. * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
  236. * read system write
  237. *
  238. */
  239. if (slave_process)
  240. {
  241. sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
  242. block_fatal_signals ();
  243. }
  244. actions_allocated = false;
  245. attrs_allocated = false;
  246. if ((err = posix_spawn_file_actions_init (&actions)) != 0
  247. || (actions_allocated = true,
  248. (pipe_stdin
  249. && (err = posix_spawn_file_actions_adddup2 (&actions,
  250. ofd[0], STDIN_FILENO))
  251. != 0)
  252. || (pipe_stdout
  253. && (err = posix_spawn_file_actions_adddup2 (&actions,
  254. ifd[1], STDOUT_FILENO))
  255. != 0)
  256. || (pipe_stdin
  257. && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
  258. != 0)
  259. || (pipe_stdout
  260. && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
  261. != 0)
  262. || (pipe_stdin
  263. && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
  264. != 0)
  265. || (pipe_stdout
  266. && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
  267. != 0)
  268. || (null_stderr
  269. && (err = posix_spawn_file_actions_addopen (&actions,
  270. STDERR_FILENO,
  271. "/dev/null", O_RDWR,
  272. 0))
  273. != 0)
  274. || (!pipe_stdin
  275. && prog_stdin != NULL
  276. && (err = posix_spawn_file_actions_addopen (&actions,
  277. STDIN_FILENO,
  278. prog_stdin, O_RDONLY,
  279. 0))
  280. != 0)
  281. || (!pipe_stdout
  282. && prog_stdout != NULL
  283. && (err = posix_spawn_file_actions_addopen (&actions,
  284. STDOUT_FILENO,
  285. prog_stdout, O_WRONLY,
  286. 0))
  287. != 0)
  288. || (slave_process
  289. && ((err = posix_spawnattr_init (&attrs)) != 0
  290. || (attrs_allocated = true,
  291. (err = posix_spawnattr_setsigmask (&attrs,
  292. &blocked_signals))
  293. != 0
  294. || (err = posix_spawnattr_setflags (&attrs,
  295. POSIX_SPAWN_SETSIGMASK))
  296. != 0)))
  297. || (err = posix_spawnp (&child, prog_path, &actions,
  298. attrs_allocated ? &attrs : NULL, prog_argv,
  299. environ))
  300. != 0))
  301. {
  302. if (actions_allocated)
  303. posix_spawn_file_actions_destroy (&actions);
  304. if (attrs_allocated)
  305. posix_spawnattr_destroy (&attrs);
  306. if (slave_process)
  307. unblock_fatal_signals ();
  308. if (exit_on_error || !null_stderr)
  309. error (exit_on_error ? EXIT_FAILURE : 0, err,
  310. _("%s subprocess failed"), progname);
  311. if (pipe_stdout)
  312. {
  313. close (ifd[0]);
  314. close (ifd[1]);
  315. }
  316. if (pipe_stdin)
  317. {
  318. close (ofd[0]);
  319. close (ofd[1]);
  320. }
  321. errno = err;
  322. return -1;
  323. }
  324. posix_spawn_file_actions_destroy (&actions);
  325. if (attrs_allocated)
  326. posix_spawnattr_destroy (&attrs);
  327. if (slave_process)
  328. {
  329. register_slave_subprocess (child);
  330. unblock_fatal_signals ();
  331. }
  332. if (pipe_stdin)
  333. close (ofd[0]);
  334. if (pipe_stdout)
  335. close (ifd[1]);
  336. if (pipe_stdout)
  337. fd[0] = ifd[0];
  338. if (pipe_stdin)
  339. fd[1] = ofd[1];
  340. return child;
  341. #endif
  342. }
  343. /* Open a bidirectional pipe.
  344. *
  345. * write system read
  346. * parent -> fd[1] -> STDIN_FILENO -> child
  347. * parent <- fd[0] <- STDOUT_FILENO <- child
  348. * read system write
  349. *
  350. */
  351. pid_t
  352. create_pipe_bidi (const char *progname,
  353. const char *prog_path, char **prog_argv,
  354. bool null_stderr,
  355. bool slave_process, bool exit_on_error,
  356. int fd[2])
  357. {
  358. pid_t result = create_pipe (progname, prog_path, prog_argv,
  359. true, true, NULL, NULL,
  360. null_stderr, slave_process, exit_on_error,
  361. fd);
  362. return result;
  363. }
  364. /* Open a pipe for input from a child process.
  365. * The child's stdin comes from a file.
  366. *
  367. * read system write
  368. * parent <- fd[0] <- STDOUT_FILENO <- child
  369. *
  370. */
  371. pid_t
  372. create_pipe_in (const char *progname,
  373. const char *prog_path, char **prog_argv,
  374. const char *prog_stdin, bool null_stderr,
  375. bool slave_process, bool exit_on_error,
  376. int fd[1])
  377. {
  378. int iofd[2];
  379. pid_t result = create_pipe (progname, prog_path, prog_argv,
  380. false, true, prog_stdin, NULL,
  381. null_stderr, slave_process, exit_on_error,
  382. iofd);
  383. if (result != -1)
  384. fd[0] = iofd[0];
  385. return result;
  386. }
  387. /* Open a pipe for output to a child process.
  388. * The child's stdout goes to a file.
  389. *
  390. * write system read
  391. * parent -> fd[0] -> STDIN_FILENO -> child
  392. *
  393. */
  394. pid_t
  395. create_pipe_out (const char *progname,
  396. const char *prog_path, char **prog_argv,
  397. const char *prog_stdout, bool null_stderr,
  398. bool slave_process, bool exit_on_error,
  399. int fd[1])
  400. {
  401. int iofd[2];
  402. pid_t result = create_pipe (progname, prog_path, prog_argv,
  403. true, false, NULL, prog_stdout,
  404. null_stderr, slave_process, exit_on_error,
  405. iofd);
  406. if (result != -1)
  407. fd[0] = iofd[1];
  408. return result;
  409. }