1 /* Creation of subprocesses, communicating via pipes.
2 Copyright (C) 2001-2004, 2006-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
32 #include "fatal-signal.h"
33 #include "wait-process.h"
36 #define _(str) gettext (str)
38 #if defined _MSC_VER || defined __MINGW32__
40 /* Native Woe32 API. */
42 # include "w32spawn.h"
57 #if ! HAVE_ENVIRON_DECL
58 extern char **environ;
62 # define STDIN_FILENO 0
65 # define STDOUT_FILENO 1
68 # define STDERR_FILENO 2
71 /* The results of open() in this file are not used with fchdir,
72 therefore save some unnecessary work in fchdir.c. */
79 /* EINTR handling for close().
80 These functions can return -1/EINTR even though we don't have any
81 signal handlers set up, namely when we get interrupted via SIGSTOP. */
84 nonintr_close (int fd)
90 while (retval < 0 && errno == EINTR);
94 #define close nonintr_close
97 nonintr_open (const char *pathname, int oflag, mode_t mode)
102 retval = open (pathname, oflag, mode);
103 while (retval < 0 && errno == EINTR);
107 #undef open /* avoid warning on VMS */
108 #define open nonintr_open
113 /* Open a pipe connected to a child process.
116 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
117 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
120 * At least one of pipe_stdin, pipe_stdout must be true.
121 * pipe_stdin and prog_stdin together determine the child's standard input.
122 * pipe_stdout and prog_stdout together determine the child's standard output.
123 * If pipe_stdin is true, prog_stdin is ignored.
124 * If pipe_stdout is true, prog_stdout is ignored.
127 create_pipe (const char *progname,
128 const char *prog_path, char **prog_argv,
129 bool pipe_stdin, bool pipe_stdout,
130 const char *prog_stdin, const char *prog_stdout,
132 bool slave_process, bool exit_on_error,
135 #if defined _MSC_VER || defined __MINGW32__
138 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
139 using the low-level functions CreatePipe(), DuplicateHandle(),
140 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
141 and cvs source code. */
152 prog_argv = prepare_spawn (prog_argv);
155 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
156 error (EXIT_FAILURE, errno, _("cannot create pipe"));
158 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
159 error (EXIT_FAILURE, errno, _("cannot create pipe"));
160 /* Data flow diagram:
163 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
164 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
169 /* Save standard file handles of parent process. */
170 if (pipe_stdin || prog_stdin != NULL)
171 orig_stdin = dup_noinherit (STDIN_FILENO);
172 if (pipe_stdout || prog_stdout != NULL)
173 orig_stdout = dup_noinherit (STDOUT_FILENO);
175 orig_stderr = dup_noinherit (STDERR_FILENO);
178 /* Create standard file handles of child process. */
182 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
183 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
185 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
186 && (nulloutfd == STDERR_FILENO
187 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
188 && close (nulloutfd) >= 0))))
190 || prog_stdin == NULL
191 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
192 && (stdinfd == STDIN_FILENO
193 || (dup2 (stdinfd, STDIN_FILENO) >= 0
194 && close (stdinfd) >= 0))))
196 || prog_stdout == NULL
197 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
198 && (stdoutfd == STDOUT_FILENO
199 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
200 && close (stdoutfd) >= 0)))))
201 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
202 but it inherits all open()ed or dup2()ed file handles (which is what
203 we want in the case of STD*_FILENO) and also orig_stdin,
204 orig_stdout, orig_stderr (which is not explicitly wanted but
206 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
214 /* Restore standard file handles of parent process. */
216 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
217 if (pipe_stdout || prog_stdout != NULL)
218 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
219 if (pipe_stdin || prog_stdin != NULL)
220 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
228 if (exit_on_error || !null_stderr)
229 error (exit_on_error ? EXIT_FAILURE : 0, errno,
230 _("%s subprocess failed"), progname);
249 # if HAVE_POSIX_SPAWN
250 sigset_t blocked_signals;
251 posix_spawn_file_actions_t actions;
252 bool actions_allocated;
253 posix_spawnattr_t attrs;
254 bool attrs_allocated;
263 error (EXIT_FAILURE, errno, _("cannot create pipe"));
266 error (EXIT_FAILURE, errno, _("cannot create pipe"));
267 /* Data flow diagram:
270 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
271 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
276 # if HAVE_POSIX_SPAWN
279 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
280 block_fatal_signals ();
282 actions_allocated = false;
283 attrs_allocated = false;
284 if ((err = posix_spawn_file_actions_init (&actions)) != 0
285 || (actions_allocated = true,
287 && (err = posix_spawn_file_actions_adddup2 (&actions,
288 ofd[0], STDIN_FILENO))
291 && (err = posix_spawn_file_actions_adddup2 (&actions,
292 ifd[1], STDOUT_FILENO))
295 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
298 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
301 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
304 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
307 && (err = posix_spawn_file_actions_addopen (&actions,
313 && prog_stdin != NULL
314 && (err = posix_spawn_file_actions_addopen (&actions,
316 prog_stdin, O_RDONLY,
320 && prog_stdout != NULL
321 && (err = posix_spawn_file_actions_addopen (&actions,
323 prog_stdout, O_WRONLY,
327 && ((err = posix_spawnattr_init (&attrs)) != 0
328 || (attrs_allocated = true,
329 (err = posix_spawnattr_setsigmask (&attrs,
332 || (err = posix_spawnattr_setflags (&attrs,
333 POSIX_SPAWN_SETSIGMASK))
335 || (err = posix_spawnp (&child, prog_path, &actions,
336 attrs_allocated ? &attrs : NULL, prog_argv,
340 if (actions_allocated)
341 posix_spawn_file_actions_destroy (&actions);
343 posix_spawnattr_destroy (&attrs);
345 unblock_fatal_signals ();
346 if (exit_on_error || !null_stderr)
347 error (exit_on_error ? EXIT_FAILURE : 0, err,
348 _("%s subprocess failed"), progname);
361 posix_spawn_file_actions_destroy (&actions);
363 posix_spawnattr_destroy (&attrs);
366 block_fatal_signals ();
367 /* Use vfork() instead of fork() for efficiency. */
368 if ((child = vfork ()) == 0)
370 /* Child process code. */
375 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
376 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
377 && (!pipe_stdin || close (ofd[0]) >= 0)
378 && (!pipe_stdout || close (ifd[1]) >= 0)
379 && (!pipe_stdin || close (ofd[1]) >= 0)
380 && (!pipe_stdout || close (ifd[0]) >= 0)
382 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
383 && (nulloutfd == STDERR_FILENO
384 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
385 && close (nulloutfd) >= 0))))
387 || prog_stdin == NULL
388 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
389 && (stdinfd == STDIN_FILENO
390 || (dup2 (stdinfd, STDIN_FILENO) >= 0
391 && close (stdinfd) >= 0))))
393 || prog_stdout == NULL
394 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
395 && (stdoutfd == STDOUT_FILENO
396 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
397 && close (stdoutfd) >= 0))))
398 && (!slave_process || (unblock_fatal_signals (), true)))
399 execvp (prog_path, prog_argv);
405 unblock_fatal_signals ();
406 if (exit_on_error || !null_stderr)
407 error (exit_on_error ? EXIT_FAILURE : 0, errno,
408 _("%s subprocess failed"), progname);
424 register_slave_subprocess (child);
425 unblock_fatal_signals ();
441 /* Open a bidirectional pipe.
444 * parent -> fd[1] -> STDIN_FILENO -> child
445 * parent <- fd[0] <- STDOUT_FILENO <- child
450 create_pipe_bidi (const char *progname,
451 const char *prog_path, char **prog_argv,
453 bool slave_process, bool exit_on_error,
456 pid_t result = create_pipe (progname, prog_path, prog_argv,
457 true, true, NULL, NULL,
458 null_stderr, slave_process, exit_on_error,
463 /* Open a pipe for input from a child process.
464 * The child's stdin comes from a file.
467 * parent <- fd[0] <- STDOUT_FILENO <- child
471 create_pipe_in (const char *progname,
472 const char *prog_path, char **prog_argv,
473 const char *prog_stdin, bool null_stderr,
474 bool slave_process, bool exit_on_error,
478 pid_t result = create_pipe (progname, prog_path, prog_argv,
479 false, true, prog_stdin, NULL,
480 null_stderr, slave_process, exit_on_error,
487 /* Open a pipe for output to a child process.
488 * The child's stdout goes to a file.
491 * parent -> fd[0] -> STDIN_FILENO -> child
495 create_pipe_out (const char *progname,
496 const char *prog_path, char **prog_argv,
497 const char *prog_stdout, bool null_stderr,
498 bool slave_process, bool exit_on_error,
502 pid_t result = create_pipe (progname, prog_path, prog_argv,
503 true, false, NULL, prog_stdout,
504 null_stderr, slave_process, exit_on_error,