1 /* Creation of subprocesses, communicating via pipes.
2 Copyright (C) 2001-2004, 2006-2008 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"
57 # define STDIN_FILENO 0
60 # define STDOUT_FILENO 1
63 # define STDERR_FILENO 2
66 /* The results of open() in this file are not used with fchdir,
67 therefore save some unnecessary work in fchdir.c. */
74 /* EINTR handling for close().
75 These functions can return -1/EINTR even though we don't have any
76 signal handlers set up, namely when we get interrupted via SIGSTOP. */
79 nonintr_close (int fd)
85 while (retval < 0 && errno == EINTR);
89 #define close nonintr_close
92 nonintr_open (const char *pathname, int oflag, mode_t mode)
97 retval = open (pathname, oflag, mode);
98 while (retval < 0 && errno == EINTR);
102 #undef open /* avoid warning on VMS */
103 #define open nonintr_open
108 /* Open a pipe connected to a child process.
111 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
112 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
115 * At least one of pipe_stdin, pipe_stdout must be true.
116 * pipe_stdin and prog_stdin together determine the child's standard input.
117 * pipe_stdout and prog_stdout together determine the child's standard output.
118 * If pipe_stdin is true, prog_stdin is ignored.
119 * If pipe_stdout is true, prog_stdout is ignored.
122 create_pipe (const char *progname,
123 const char *prog_path, char **prog_argv,
124 bool pipe_stdin, bool pipe_stdout,
125 const char *prog_stdin, const char *prog_stdout,
127 bool slave_process, bool exit_on_error,
130 #if defined _MSC_VER || defined __MINGW32__
133 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
134 using the low-level functions CreatePipe(), DuplicateHandle(),
135 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
136 and cvs source code. */
147 prog_argv = prepare_spawn (prog_argv);
150 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
151 error (EXIT_FAILURE, errno, _("cannot create pipe"));
153 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
154 error (EXIT_FAILURE, errno, _("cannot create pipe"));
155 /* Data flow diagram:
158 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
159 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
164 /* Save standard file handles of parent process. */
165 if (pipe_stdin || prog_stdin != NULL)
166 orig_stdin = dup_noinherit (STDIN_FILENO);
167 if (pipe_stdout || prog_stdout != NULL)
168 orig_stdout = dup_noinherit (STDOUT_FILENO);
170 orig_stderr = dup_noinherit (STDERR_FILENO);
173 /* Create standard file handles of child process. */
177 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
178 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
180 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
181 && (nulloutfd == STDERR_FILENO
182 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
183 && close (nulloutfd) >= 0))))
185 || prog_stdin == NULL
186 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
187 && (stdinfd == STDIN_FILENO
188 || (dup2 (stdinfd, STDIN_FILENO) >= 0
189 && close (stdinfd) >= 0))))
191 || prog_stdout == NULL
192 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
193 && (stdoutfd == STDOUT_FILENO
194 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
195 && close (stdoutfd) >= 0)))))
196 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
197 but it inherits all open()ed or dup2()ed file handles (which is what
198 we want in the case of STD*_FILENO) and also orig_stdin,
199 orig_stdout, orig_stderr (which is not explicitly wanted but
201 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
209 /* Restore standard file handles of parent process. */
211 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
212 if (pipe_stdout || prog_stdout != NULL)
213 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
214 if (pipe_stdin || prog_stdin != NULL)
215 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
223 if (exit_on_error || !null_stderr)
224 error (exit_on_error ? EXIT_FAILURE : 0, errno,
225 _("%s subprocess failed"), progname);
244 # if HAVE_POSIX_SPAWN
245 sigset_t blocked_signals;
246 posix_spawn_file_actions_t actions;
247 bool actions_allocated;
248 posix_spawnattr_t attrs;
249 bool attrs_allocated;
258 error (EXIT_FAILURE, errno, _("cannot create pipe"));
261 error (EXIT_FAILURE, errno, _("cannot create pipe"));
262 /* Data flow diagram:
265 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
266 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
271 # if HAVE_POSIX_SPAWN
274 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
275 block_fatal_signals ();
277 actions_allocated = false;
278 attrs_allocated = false;
279 if ((err = posix_spawn_file_actions_init (&actions)) != 0
280 || (actions_allocated = true,
282 && (err = posix_spawn_file_actions_adddup2 (&actions,
283 ofd[0], STDIN_FILENO))
286 && (err = posix_spawn_file_actions_adddup2 (&actions,
287 ifd[1], STDOUT_FILENO))
290 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
293 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
296 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
299 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
302 && (err = posix_spawn_file_actions_addopen (&actions,
308 && prog_stdin != NULL
309 && (err = posix_spawn_file_actions_addopen (&actions,
311 prog_stdin, O_RDONLY,
315 && prog_stdout != NULL
316 && (err = posix_spawn_file_actions_addopen (&actions,
318 prog_stdout, O_WRONLY,
322 && ((err = posix_spawnattr_init (&attrs)) != 0
323 || (attrs_allocated = true,
324 (err = posix_spawnattr_setsigmask (&attrs,
327 || (err = posix_spawnattr_setflags (&attrs,
328 POSIX_SPAWN_SETSIGMASK))
330 || (err = posix_spawnp (&child, prog_path, &actions,
331 attrs_allocated ? &attrs : NULL, prog_argv,
335 if (actions_allocated)
336 posix_spawn_file_actions_destroy (&actions);
338 posix_spawnattr_destroy (&attrs);
340 unblock_fatal_signals ();
341 if (exit_on_error || !null_stderr)
342 error (exit_on_error ? EXIT_FAILURE : 0, err,
343 _("%s subprocess failed"), progname);
356 posix_spawn_file_actions_destroy (&actions);
358 posix_spawnattr_destroy (&attrs);
361 block_fatal_signals ();
362 /* Use vfork() instead of fork() for efficiency. */
363 if ((child = vfork ()) == 0)
365 /* Child process code. */
370 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
371 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
372 && (!pipe_stdin || close (ofd[0]) >= 0)
373 && (!pipe_stdout || close (ifd[1]) >= 0)
374 && (!pipe_stdin || close (ofd[1]) >= 0)
375 && (!pipe_stdout || close (ifd[0]) >= 0)
377 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
378 && (nulloutfd == STDERR_FILENO
379 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
380 && close (nulloutfd) >= 0))))
382 || prog_stdin == NULL
383 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
384 && (stdinfd == STDIN_FILENO
385 || (dup2 (stdinfd, STDIN_FILENO) >= 0
386 && close (stdinfd) >= 0))))
388 || prog_stdout == NULL
389 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
390 && (stdoutfd == STDOUT_FILENO
391 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
392 && close (stdoutfd) >= 0))))
393 && (!slave_process || (unblock_fatal_signals (), true)))
394 execvp (prog_path, prog_argv);
400 unblock_fatal_signals ();
401 if (exit_on_error || !null_stderr)
402 error (exit_on_error ? EXIT_FAILURE : 0, errno,
403 _("%s subprocess failed"), progname);
419 register_slave_subprocess (child);
420 unblock_fatal_signals ();
436 /* Open a bidirectional pipe.
439 * parent -> fd[1] -> STDIN_FILENO -> child
440 * parent <- fd[0] <- STDOUT_FILENO <- child
445 create_pipe_bidi (const char *progname,
446 const char *prog_path, char **prog_argv,
448 bool slave_process, bool exit_on_error,
451 pid_t result = create_pipe (progname, prog_path, prog_argv,
452 true, true, NULL, NULL,
453 null_stderr, slave_process, exit_on_error,
458 /* Open a pipe for input from a child process.
459 * The child's stdin comes from a file.
462 * parent <- fd[0] <- STDOUT_FILENO <- child
466 create_pipe_in (const char *progname,
467 const char *prog_path, char **prog_argv,
468 const char *prog_stdin, bool null_stderr,
469 bool slave_process, bool exit_on_error,
473 pid_t result = create_pipe (progname, prog_path, prog_argv,
474 false, true, prog_stdin, NULL,
475 null_stderr, slave_process, exit_on_error,
482 /* Open a pipe for output to a child process.
483 * The child's stdout goes to a file.
486 * parent -> fd[0] -> STDIN_FILENO -> child
490 create_pipe_out (const char *progname,
491 const char *prog_path, char **prog_argv,
492 const char *prog_stdout, bool null_stderr,
493 bool slave_process, bool exit_on_error,
497 pid_t result = create_pipe (progname, prog_path, prog_argv,
498 true, false, NULL, prog_stdout,
499 null_stderr, slave_process, exit_on_error,