1 /* Creation of subprocesses, communicating via pipes.
2 Copyright (C) 2001-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
38 #include "fatal-signal.h"
39 #include "wait-process.h"
42 #define _(str) gettext (str)
44 #if defined _MSC_VER || defined __MINGW32__
46 /* Native Woe32 API. */
48 # include "w32spawn.h"
53 # ifdef HAVE_POSIX_SPAWN
64 # define STDIN_FILENO 0
67 # define STDOUT_FILENO 1
70 # define STDERR_FILENO 2
76 /* EINTR handling for close().
77 These functions can return -1/EINTR even though we don't have any
78 signal handlers set up, namely when we get interrupted via SIGSTOP. */
81 nonintr_close (int fd)
87 while (retval < 0 && errno == EINTR);
91 #define close nonintr_close
94 nonintr_open (const char *pathname, int oflag, mode_t mode)
99 retval = open (pathname, oflag, mode);
100 while (retval < 0 && errno == EINTR);
104 #undef open /* avoid warning on VMS */
105 #define open nonintr_open
110 /* Open a pipe connected to a child process.
113 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
114 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
117 * At least one of pipe_stdin, pipe_stdout must be true.
118 * pipe_stdin and prog_stdin together determine the child's standard input.
119 * pipe_stdout and prog_stdout together determine the child's standard output.
120 * If pipe_stdin is true, prog_stdin is ignored.
121 * If pipe_stdout is true, prog_stdout is ignored.
124 create_pipe (const char *progname,
125 const char *prog_path, char **prog_argv,
126 bool pipe_stdin, bool pipe_stdout,
127 const char *prog_stdin, const char *prog_stdout,
129 bool slave_process, bool exit_on_error,
132 #if defined _MSC_VER || defined __MINGW32__
135 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
136 using the low-level functions CreatePipe(), DuplicateHandle(),
137 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
138 and cvs source code. */
149 prog_argv = prepare_spawn (prog_argv);
152 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
153 error (EXIT_FAILURE, errno, _("cannot create pipe"));
155 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
156 error (EXIT_FAILURE, errno, _("cannot create pipe"));
157 /* Data flow diagram:
160 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
161 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
166 /* Save standard file handles of parent process. */
167 if (pipe_stdin || prog_stdin != NULL)
168 orig_stdin = dup_noinherit (STDIN_FILENO);
169 if (pipe_stdout || prog_stdout != NULL)
170 orig_stdout = dup_noinherit (STDOUT_FILENO);
172 orig_stderr = dup_noinherit (STDERR_FILENO);
175 /* Create standard file handles of child process. */
179 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
180 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
182 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
183 && (nulloutfd == STDERR_FILENO
184 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
185 && close (nulloutfd) >= 0))))
187 || prog_stdin == NULL
188 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
189 && (stdinfd == STDIN_FILENO
190 || (dup2 (stdinfd, STDIN_FILENO) >= 0
191 && close (stdinfd) >= 0))))
193 || prog_stdout == NULL
194 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
195 && (stdoutfd == STDOUT_FILENO
196 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
197 && close (stdoutfd) >= 0)))))
198 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
199 but it inherits all open()ed or dup2()ed file handles (which is what
200 we want in the case of STD*_FILENO) and also orig_stdin,
201 orig_stdout, orig_stderr (which is not explicitly wanted but
203 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
211 /* Restore standard file handles of parent process. */
213 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
214 if (pipe_stdout || prog_stdout != NULL)
215 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
216 if (pipe_stdin || prog_stdin != NULL)
217 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
225 if (exit_on_error || !null_stderr)
226 error (exit_on_error ? EXIT_FAILURE : 0, errno,
227 _("%s subprocess failed"), progname);
246 # if HAVE_POSIX_SPAWN
247 sigset_t blocked_signals;
248 posix_spawn_file_actions_t actions;
249 bool actions_allocated;
250 posix_spawnattr_t attrs;
251 bool attrs_allocated;
260 error (EXIT_FAILURE, errno, _("cannot create pipe"));
263 error (EXIT_FAILURE, errno, _("cannot create pipe"));
264 /* Data flow diagram:
267 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
268 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
273 # if HAVE_POSIX_SPAWN
276 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
277 block_fatal_signals ();
279 actions_allocated = false;
280 attrs_allocated = false;
281 if ((err = posix_spawn_file_actions_init (&actions)) != 0
282 || (actions_allocated = true,
284 && (err = posix_spawn_file_actions_adddup2 (&actions,
285 ofd[0], STDIN_FILENO))
288 && (err = posix_spawn_file_actions_adddup2 (&actions,
289 ifd[1], STDOUT_FILENO))
292 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
295 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
298 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
301 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
304 && (err = posix_spawn_file_actions_addopen (&actions,
310 && prog_stdin != NULL
311 && (err = posix_spawn_file_actions_addopen (&actions,
313 prog_stdin, O_RDONLY,
317 && prog_stdout != NULL
318 && (err = posix_spawn_file_actions_addopen (&actions,
320 prog_stdout, O_WRONLY,
324 && ((err = posix_spawnattr_init (&attrs)) != 0
325 || (attrs_allocated = true,
326 (err = posix_spawnattr_setsigmask (&attrs,
329 || (err = posix_spawnattr_setflags (&attrs,
330 POSIX_SPAWN_SETSIGMASK))
332 || (err = posix_spawnp (&child, prog_path, &actions,
333 attrs_allocated ? &attrs : NULL, prog_argv,
337 if (actions_allocated)
338 posix_spawn_file_actions_destroy (&actions);
340 posix_spawnattr_destroy (&attrs);
342 unblock_fatal_signals ();
343 if (exit_on_error || !null_stderr)
344 error (exit_on_error ? EXIT_FAILURE : 0, err,
345 _("%s subprocess failed"), progname);
358 posix_spawn_file_actions_destroy (&actions);
360 posix_spawnattr_destroy (&attrs);
363 block_fatal_signals ();
364 /* Use vfork() instead of fork() for efficiency. */
365 if ((child = vfork ()) == 0)
367 /* Child process code. */
372 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
373 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
374 && (!pipe_stdin || close (ofd[0]) >= 0)
375 && (!pipe_stdout || close (ifd[1]) >= 0)
376 && (!pipe_stdin || close (ofd[1]) >= 0)
377 && (!pipe_stdout || close (ifd[0]) >= 0)
379 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
380 && (nulloutfd == STDERR_FILENO
381 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
382 && close (nulloutfd) >= 0))))
384 || prog_stdin == NULL
385 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
386 && (stdinfd == STDIN_FILENO
387 || (dup2 (stdinfd, STDIN_FILENO) >= 0
388 && close (stdinfd) >= 0))))
390 || prog_stdout == NULL
391 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
392 && (stdoutfd == STDOUT_FILENO
393 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
394 && close (stdoutfd) >= 0))))
395 && (!slave_process || (unblock_fatal_signals (), true)))
396 execvp (prog_path, prog_argv);
402 unblock_fatal_signals ();
403 if (exit_on_error || !null_stderr)
404 error (exit_on_error ? EXIT_FAILURE : 0, errno,
405 _("%s subprocess failed"), progname);
421 register_slave_subprocess (child);
422 unblock_fatal_signals ();
438 /* Open a bidirectional pipe.
441 * parent -> fd[1] -> STDIN_FILENO -> child
442 * parent <- fd[0] <- STDOUT_FILENO <- child
447 create_pipe_bidi (const char *progname,
448 const char *prog_path, char **prog_argv,
450 bool slave_process, bool exit_on_error,
453 pid_t result = create_pipe (progname, prog_path, prog_argv,
454 true, true, NULL, NULL,
455 null_stderr, slave_process, exit_on_error,
460 /* Open a pipe for input from a child process.
461 * The child's stdin comes from a file.
464 * parent <- fd[0] <- STDOUT_FILENO <- child
468 create_pipe_in (const char *progname,
469 const char *prog_path, char **prog_argv,
470 const char *prog_stdin, bool null_stderr,
471 bool slave_process, bool exit_on_error,
475 pid_t result = create_pipe (progname, prog_path, prog_argv,
476 false, true, prog_stdin, NULL,
477 null_stderr, slave_process, exit_on_error,
484 /* Open a pipe for output to a child process.
485 * The child's stdout goes to a file.
488 * parent -> fd[0] -> STDIN_FILENO -> child
492 create_pipe_out (const char *progname,
493 const char *prog_path, char **prog_argv,
494 const char *prog_stdout, bool null_stderr,
495 bool slave_process, bool exit_on_error,
499 pid_t result = create_pipe (progname, prog_path, prog_argv,
500 true, false, NULL, prog_stdout,
501 null_stderr, slave_process, exit_on_error,