1 /* Creation of subprocesses, communicating via pipes.
2 Copyright (C) 2001-2004, 2006 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. */
33 #include "fatal-signal.h"
34 #include "wait-process.h"
37 #define _(str) gettext (str)
39 #if defined _MSC_VER || defined __MINGW32__
41 /* Native Woe32 API. */
43 # include "w32spawn.h"
48 # ifdef HAVE_POSIX_SPAWN
58 #ifndef HAVE_ENVIRON_DECL
59 extern char **environ;
63 # define STDIN_FILENO 0
66 # define STDOUT_FILENO 1
69 # define STDERR_FILENO 2
75 /* EINTR handling for close().
76 These functions can return -1/EINTR even though we don't have any
77 signal handlers set up, namely when we get interrupted via SIGSTOP. */
80 nonintr_close (int fd)
86 while (retval < 0 && errno == EINTR);
90 #define close nonintr_close
93 nonintr_open (const char *pathname, int oflag, mode_t mode)
98 retval = open (pathname, oflag, mode);
99 while (retval < 0 && errno == EINTR);
103 #undef open /* avoid warning on VMS */
104 #define open nonintr_open
109 /* Open a pipe connected to a child process.
112 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
113 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
116 * At least one of pipe_stdin, pipe_stdout must be true.
117 * pipe_stdin and prog_stdin together determine the child's standard input.
118 * pipe_stdout and prog_stdout together determine the child's standard output.
119 * If pipe_stdin is true, prog_stdin is ignored.
120 * If pipe_stdout is true, prog_stdout is ignored.
123 create_pipe (const char *progname,
124 const char *prog_path, char **prog_argv,
125 bool pipe_stdin, bool pipe_stdout,
126 const char *prog_stdin, const char *prog_stdout,
128 bool slave_process, bool exit_on_error,
131 #if defined _MSC_VER || defined __MINGW32__
134 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
135 using the low-level functions CreatePipe(), DuplicateHandle(),
136 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
137 and cvs source code. */
148 prog_argv = prepare_spawn (prog_argv);
151 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
152 error (EXIT_FAILURE, errno, _("cannot create pipe"));
154 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
155 error (EXIT_FAILURE, errno, _("cannot create pipe"));
156 /* Data flow diagram:
159 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
160 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
165 /* Save standard file handles of parent process. */
166 if (pipe_stdin || prog_stdin != NULL)
167 orig_stdin = dup_noinherit (STDIN_FILENO);
168 if (pipe_stdout || prog_stdout != NULL)
169 orig_stdout = dup_noinherit (STDOUT_FILENO);
171 orig_stderr = dup_noinherit (STDERR_FILENO);
174 /* Create standard file handles of child process. */
178 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
179 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
181 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
182 && (nulloutfd == STDERR_FILENO
183 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
184 && close (nulloutfd) >= 0))))
186 || prog_stdin == NULL
187 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
188 && (stdinfd == STDIN_FILENO
189 || (dup2 (stdinfd, STDIN_FILENO) >= 0
190 && close (stdinfd) >= 0))))
192 || prog_stdout == NULL
193 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
194 && (stdoutfd == STDOUT_FILENO
195 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
196 && close (stdoutfd) >= 0)))))
197 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
198 but it inherits all open()ed or dup2()ed file handles (which is what
199 we want in the case of STD*_FILENO) and also orig_stdin,
200 orig_stdout, orig_stderr (which is not explicitly wanted but
202 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
210 /* Restore standard file handles of parent process. */
212 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
213 if (pipe_stdout || prog_stdout != NULL)
214 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
215 if (pipe_stdin || prog_stdin != NULL)
216 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
224 if (exit_on_error || !null_stderr)
225 error (exit_on_error ? EXIT_FAILURE : 0, errno,
226 _("%s subprocess failed"), progname);
245 # if HAVE_POSIX_SPAWN
246 sigset_t blocked_signals;
247 posix_spawn_file_actions_t actions;
248 bool actions_allocated;
249 posix_spawnattr_t attrs;
250 bool attrs_allocated;
259 error (EXIT_FAILURE, errno, _("cannot create pipe"));
262 error (EXIT_FAILURE, errno, _("cannot create pipe"));
263 /* Data flow diagram:
266 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
267 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
272 # if HAVE_POSIX_SPAWN
275 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
276 block_fatal_signals ();
278 actions_allocated = false;
279 attrs_allocated = false;
280 if ((err = posix_spawn_file_actions_init (&actions)) != 0
281 || (actions_allocated = true,
283 && (err = posix_spawn_file_actions_adddup2 (&actions,
284 ofd[0], STDIN_FILENO))
287 && (err = posix_spawn_file_actions_adddup2 (&actions,
288 ifd[1], STDOUT_FILENO))
291 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
294 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
297 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
300 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
303 && (err = posix_spawn_file_actions_addopen (&actions,
309 && prog_stdin != NULL
310 && (err = posix_spawn_file_actions_addopen (&actions,
312 prog_stdin, O_RDONLY,
316 && prog_stdout != NULL
317 && (err = posix_spawn_file_actions_addopen (&actions,
319 prog_stdout, O_WRONLY,
323 && ((err = posix_spawnattr_init (&attrs)) != 0
324 || (attrs_allocated = true,
325 (err = posix_spawnattr_setsigmask (&attrs,
328 || (err = posix_spawnattr_setflags (&attrs,
329 POSIX_SPAWN_SETSIGMASK))
331 || (err = posix_spawnp (&child, prog_path, &actions,
332 attrs_allocated ? &attrs : NULL, prog_argv,
336 if (actions_allocated)
337 posix_spawn_file_actions_destroy (&actions);
339 posix_spawnattr_destroy (&attrs);
341 unblock_fatal_signals ();
342 if (exit_on_error || !null_stderr)
343 error (exit_on_error ? EXIT_FAILURE : 0, err,
344 _("%s subprocess failed"), progname);
357 posix_spawn_file_actions_destroy (&actions);
359 posix_spawnattr_destroy (&attrs);
362 block_fatal_signals ();
363 /* Use vfork() instead of fork() for efficiency. */
364 if ((child = vfork ()) == 0)
366 /* Child process code. */
371 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
372 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
373 && (!pipe_stdin || close (ofd[0]) >= 0)
374 && (!pipe_stdout || close (ifd[1]) >= 0)
375 && (!pipe_stdin || close (ofd[1]) >= 0)
376 && (!pipe_stdout || close (ifd[0]) >= 0)
378 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
379 && (nulloutfd == STDERR_FILENO
380 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
381 && close (nulloutfd) >= 0))))
383 || prog_stdin == NULL
384 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
385 && (stdinfd == STDIN_FILENO
386 || (dup2 (stdinfd, STDIN_FILENO) >= 0
387 && close (stdinfd) >= 0))))
389 || prog_stdout == NULL
390 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
391 && (stdoutfd == STDOUT_FILENO
392 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
393 && close (stdoutfd) >= 0))))
394 && (!slave_process || (unblock_fatal_signals (), true)))
395 execvp (prog_path, prog_argv);
401 unblock_fatal_signals ();
402 if (exit_on_error || !null_stderr)
403 error (exit_on_error ? EXIT_FAILURE : 0, errno,
404 _("%s subprocess failed"), progname);
420 register_slave_subprocess (child);
421 unblock_fatal_signals ();
437 /* Open a bidirectional pipe.
440 * parent -> fd[1] -> STDIN_FILENO -> child
441 * parent <- fd[0] <- STDOUT_FILENO <- child
446 create_pipe_bidi (const char *progname,
447 const char *prog_path, char **prog_argv,
449 bool slave_process, bool exit_on_error,
452 pid_t result = create_pipe (progname, prog_path, prog_argv,
453 true, true, NULL, NULL,
454 null_stderr, slave_process, exit_on_error,
459 /* Open a pipe for input from a child process.
460 * The child's stdin comes from a file.
463 * parent <- fd[0] <- STDOUT_FILENO <- child
467 create_pipe_in (const char *progname,
468 const char *prog_path, char **prog_argv,
469 const char *prog_stdin, bool null_stderr,
470 bool slave_process, bool exit_on_error,
474 pid_t result = create_pipe (progname, prog_path, prog_argv,
475 false, true, prog_stdin, NULL,
476 null_stderr, slave_process, exit_on_error,
483 /* Open a pipe for output to a child process.
484 * The child's stdout goes to a file.
487 * parent -> fd[0] -> STDIN_FILENO -> child
491 create_pipe_out (const char *progname,
492 const char *prog_path, char **prog_argv,
493 const char *prog_stdout, bool null_stderr,
494 bool slave_process, bool exit_on_error,
498 pid_t result = create_pipe (progname, prog_path, prog_argv,
499 true, false, NULL, prog_stdout,
500 null_stderr, slave_process, exit_on_error,