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 3 of the License, or
8 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
31 #include "fatal-signal.h"
32 #include "wait-process.h"
35 #define _(str) gettext (str)
37 #if defined _MSC_VER || defined __MINGW32__
39 /* Native Woe32 API. */
41 # include "w32spawn.h"
56 #if ! HAVE_ENVIRON_DECL
57 extern char **environ;
61 # define STDIN_FILENO 0
64 # define STDOUT_FILENO 1
67 # define STDERR_FILENO 2
70 /* The results of open() in this file are not used with fchdir,
71 therefore save some unnecessary work in fchdir.c. */
78 /* EINTR handling for close().
79 These functions can return -1/EINTR even though we don't have any
80 signal handlers set up, namely when we get interrupted via SIGSTOP. */
83 nonintr_close (int fd)
89 while (retval < 0 && errno == EINTR);
93 #define close nonintr_close
96 nonintr_open (const char *pathname, int oflag, mode_t mode)
101 retval = open (pathname, oflag, mode);
102 while (retval < 0 && errno == EINTR);
106 #undef open /* avoid warning on VMS */
107 #define open nonintr_open
112 /* Open a pipe connected to a child process.
115 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
116 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
119 * At least one of pipe_stdin, pipe_stdout must be true.
120 * pipe_stdin and prog_stdin together determine the child's standard input.
121 * pipe_stdout and prog_stdout together determine the child's standard output.
122 * If pipe_stdin is true, prog_stdin is ignored.
123 * If pipe_stdout is true, prog_stdout is ignored.
126 create_pipe (const char *progname,
127 const char *prog_path, char **prog_argv,
128 bool pipe_stdin, bool pipe_stdout,
129 const char *prog_stdin, const char *prog_stdout,
131 bool slave_process, bool exit_on_error,
134 #if defined _MSC_VER || defined __MINGW32__
137 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
138 using the low-level functions CreatePipe(), DuplicateHandle(),
139 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
140 and cvs source code. */
151 prog_argv = prepare_spawn (prog_argv);
154 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
155 error (EXIT_FAILURE, errno, _("cannot create pipe"));
157 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
158 error (EXIT_FAILURE, errno, _("cannot create pipe"));
159 /* Data flow diagram:
162 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
163 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
168 /* Save standard file handles of parent process. */
169 if (pipe_stdin || prog_stdin != NULL)
170 orig_stdin = dup_noinherit (STDIN_FILENO);
171 if (pipe_stdout || prog_stdout != NULL)
172 orig_stdout = dup_noinherit (STDOUT_FILENO);
174 orig_stderr = dup_noinherit (STDERR_FILENO);
177 /* Create standard file handles of child process. */
181 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
182 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
184 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
185 && (nulloutfd == STDERR_FILENO
186 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
187 && close (nulloutfd) >= 0))))
189 || prog_stdin == NULL
190 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
191 && (stdinfd == STDIN_FILENO
192 || (dup2 (stdinfd, STDIN_FILENO) >= 0
193 && close (stdinfd) >= 0))))
195 || prog_stdout == NULL
196 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
197 && (stdoutfd == STDOUT_FILENO
198 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
199 && close (stdoutfd) >= 0)))))
200 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
201 but it inherits all open()ed or dup2()ed file handles (which is what
202 we want in the case of STD*_FILENO) and also orig_stdin,
203 orig_stdout, orig_stderr (which is not explicitly wanted but
205 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
213 /* Restore standard file handles of parent process. */
215 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
216 if (pipe_stdout || prog_stdout != NULL)
217 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
218 if (pipe_stdin || prog_stdin != NULL)
219 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
227 if (exit_on_error || !null_stderr)
228 error (exit_on_error ? EXIT_FAILURE : 0, errno,
229 _("%s subprocess failed"), progname);
248 # if HAVE_POSIX_SPAWN
249 sigset_t blocked_signals;
250 posix_spawn_file_actions_t actions;
251 bool actions_allocated;
252 posix_spawnattr_t attrs;
253 bool attrs_allocated;
262 error (EXIT_FAILURE, errno, _("cannot create pipe"));
265 error (EXIT_FAILURE, errno, _("cannot create pipe"));
266 /* Data flow diagram:
269 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
270 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
275 # if HAVE_POSIX_SPAWN
278 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
279 block_fatal_signals ();
281 actions_allocated = false;
282 attrs_allocated = false;
283 if ((err = posix_spawn_file_actions_init (&actions)) != 0
284 || (actions_allocated = true,
286 && (err = posix_spawn_file_actions_adddup2 (&actions,
287 ofd[0], STDIN_FILENO))
290 && (err = posix_spawn_file_actions_adddup2 (&actions,
291 ifd[1], STDOUT_FILENO))
294 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
297 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
300 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
303 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
306 && (err = posix_spawn_file_actions_addopen (&actions,
312 && prog_stdin != NULL
313 && (err = posix_spawn_file_actions_addopen (&actions,
315 prog_stdin, O_RDONLY,
319 && prog_stdout != NULL
320 && (err = posix_spawn_file_actions_addopen (&actions,
322 prog_stdout, O_WRONLY,
326 && ((err = posix_spawnattr_init (&attrs)) != 0
327 || (attrs_allocated = true,
328 (err = posix_spawnattr_setsigmask (&attrs,
331 || (err = posix_spawnattr_setflags (&attrs,
332 POSIX_SPAWN_SETSIGMASK))
334 || (err = posix_spawnp (&child, prog_path, &actions,
335 attrs_allocated ? &attrs : NULL, prog_argv,
339 if (actions_allocated)
340 posix_spawn_file_actions_destroy (&actions);
342 posix_spawnattr_destroy (&attrs);
344 unblock_fatal_signals ();
345 if (exit_on_error || !null_stderr)
346 error (exit_on_error ? EXIT_FAILURE : 0, err,
347 _("%s subprocess failed"), progname);
360 posix_spawn_file_actions_destroy (&actions);
362 posix_spawnattr_destroy (&attrs);
365 block_fatal_signals ();
366 /* Use vfork() instead of fork() for efficiency. */
367 if ((child = vfork ()) == 0)
369 /* Child process code. */
374 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
375 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
376 && (!pipe_stdin || close (ofd[0]) >= 0)
377 && (!pipe_stdout || close (ifd[1]) >= 0)
378 && (!pipe_stdin || close (ofd[1]) >= 0)
379 && (!pipe_stdout || close (ifd[0]) >= 0)
381 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
382 && (nulloutfd == STDERR_FILENO
383 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
384 && close (nulloutfd) >= 0))))
386 || prog_stdin == NULL
387 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
388 && (stdinfd == STDIN_FILENO
389 || (dup2 (stdinfd, STDIN_FILENO) >= 0
390 && close (stdinfd) >= 0))))
392 || prog_stdout == NULL
393 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
394 && (stdoutfd == STDOUT_FILENO
395 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
396 && close (stdoutfd) >= 0))))
397 && (!slave_process || (unblock_fatal_signals (), true)))
398 execvp (prog_path, prog_argv);
404 unblock_fatal_signals ();
405 if (exit_on_error || !null_stderr)
406 error (exit_on_error ? EXIT_FAILURE : 0, errno,
407 _("%s subprocess failed"), progname);
423 register_slave_subprocess (child);
424 unblock_fatal_signals ();
440 /* Open a bidirectional pipe.
443 * parent -> fd[1] -> STDIN_FILENO -> child
444 * parent <- fd[0] <- STDOUT_FILENO <- child
449 create_pipe_bidi (const char *progname,
450 const char *prog_path, char **prog_argv,
452 bool slave_process, bool exit_on_error,
455 pid_t result = create_pipe (progname, prog_path, prog_argv,
456 true, true, NULL, NULL,
457 null_stderr, slave_process, exit_on_error,
462 /* Open a pipe for input from a child process.
463 * The child's stdin comes from a file.
466 * parent <- fd[0] <- STDOUT_FILENO <- child
470 create_pipe_in (const char *progname,
471 const char *prog_path, char **prog_argv,
472 const char *prog_stdin, bool null_stderr,
473 bool slave_process, bool exit_on_error,
477 pid_t result = create_pipe (progname, prog_path, prog_argv,
478 false, true, prog_stdin, NULL,
479 null_stderr, slave_process, exit_on_error,
486 /* Open a pipe for output to a child process.
487 * The child's stdout goes to a file.
490 * parent -> fd[0] -> STDIN_FILENO -> child
494 create_pipe_out (const char *progname,
495 const char *prog_path, char **prog_argv,
496 const char *prog_stdout, bool null_stderr,
497 bool slave_process, bool exit_on_error,
501 pid_t result = create_pipe (progname, prog_path, prog_argv,
502 true, false, NULL, prog_stdout,
503 null_stderr, slave_process, exit_on_error,