1 /* Creation of subprocesses, communicating via pipes.
2 Copyright (C) 2001-2004, 2006-2010 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 "unistd-safer.h"
33 #include "wait-process.h"
36 #define _(str) gettext (str)
38 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
40 /* Native Woe32 API. */
42 # include "w32spawn.h"
51 /* The results of open() in this file are not used with fchdir,
52 therefore save some unnecessary work in fchdir.c. */
59 /* EINTR handling for close().
60 These functions can return -1/EINTR even though we don't have any
61 signal handlers set up, namely when we get interrupted via SIGSTOP. */
64 nonintr_close (int fd)
70 while (retval < 0 && errno == EINTR);
74 #define close nonintr_close
77 nonintr_open (const char *pathname, int oflag, mode_t mode)
82 retval = open (pathname, oflag, mode);
83 while (retval < 0 && errno == EINTR);
87 #undef open /* avoid warning on VMS */
88 #define open nonintr_open
93 /* Open a pipe connected to a child process.
96 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
97 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
100 * At least one of pipe_stdin, pipe_stdout must be true.
101 * pipe_stdin and prog_stdin together determine the child's standard input.
102 * pipe_stdout and prog_stdout together determine the child's standard output.
103 * If pipe_stdin is true, prog_stdin is ignored.
104 * If pipe_stdout is true, prog_stdout is ignored.
107 create_pipe (const char *progname,
108 const char *prog_path, char **prog_argv,
109 bool pipe_stdin, bool pipe_stdout,
110 const char *prog_stdin, const char *prog_stdout,
112 bool slave_process, bool exit_on_error,
115 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
118 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
119 using the low-level functions CreatePipe(), DuplicateHandle(),
120 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
121 and cvs source code. */
133 /* FIXME: Need to free memory allocated by prepare_spawn. */
134 prog_argv = prepare_spawn (prog_argv);
137 if (pipe2_safer (ifd, O_BINARY | O_CLOEXEC) < 0)
138 error (EXIT_FAILURE, errno, _("cannot create pipe"));
140 if (pipe2_safer (ofd, O_BINARY | O_CLOEXEC) < 0)
141 error (EXIT_FAILURE, errno, _("cannot create pipe"));
142 /* Data flow diagram:
145 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
146 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
151 /* Save standard file handles of parent process. */
152 if (pipe_stdin || prog_stdin != NULL)
153 orig_stdin = dup_safer_noinherit (STDIN_FILENO);
154 if (pipe_stdout || prog_stdout != NULL)
155 orig_stdout = dup_safer_noinherit (STDOUT_FILENO);
157 orig_stderr = dup_safer_noinherit (STDERR_FILENO);
160 /* Create standard file handles of child process. */
164 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
165 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
167 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
168 && (nulloutfd == STDERR_FILENO
169 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
170 && close (nulloutfd) >= 0))))
172 || prog_stdin == NULL
173 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
174 && (stdinfd == STDIN_FILENO
175 || (dup2 (stdinfd, STDIN_FILENO) >= 0
176 && close (stdinfd) >= 0))))
178 || prog_stdout == NULL
179 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
180 && (stdoutfd == STDOUT_FILENO
181 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
182 && close (stdoutfd) >= 0)))))
183 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
184 but it inherits all open()ed or dup2()ed file handles (which is what
185 we want in the case of STD*_FILENO). */
186 /* Use spawnvpe and pass the environment explicitly. This is needed if
187 the program has modified the environment using putenv() or [un]setenv().
188 On Windows, programs have two environments, one in the "environment
189 block" of the process and managed through SetEnvironmentVariable(), and
190 one inside the process, in the location retrieved by the 'environ'
191 macro. When using spawnvp() without 'e', the child process inherits a
192 copy of the environment block - ignoring the effects of putenv() and
195 child = spawnvpe (P_NOWAIT, prog_path, (const char **) prog_argv,
196 (const char **) environ);
197 if (child < 0 && errno == ENOEXEC)
199 /* prog is not an native executable. Try to execute it as a
200 shell script. Note that prepare_spawn() has already prepended
201 a hidden element "sh.exe" to prog_argv. */
203 child = spawnvpe (P_NOWAIT, prog_argv[0], (const char **) prog_argv,
204 (const char **) environ);
216 /* Restore standard file handles of parent process. */
218 undup_safer_noinherit (orig_stderr, STDERR_FILENO);
219 if (pipe_stdout || prog_stdout != NULL)
220 undup_safer_noinherit (orig_stdout, STDOUT_FILENO);
221 if (pipe_stdin || prog_stdin != NULL)
222 undup_safer_noinherit (orig_stdin, STDIN_FILENO);
230 if (exit_on_error || !null_stderr)
231 error (exit_on_error ? EXIT_FAILURE : 0, saved_errno,
232 _("%s subprocess failed"), progname);
252 sigset_t blocked_signals;
253 posix_spawn_file_actions_t actions;
254 bool actions_allocated;
255 posix_spawnattr_t attrs;
256 bool attrs_allocated;
261 if (pipe_safer (ifd) < 0)
262 error (EXIT_FAILURE, errno, _("cannot create pipe"));
264 if (pipe_safer (ofd) < 0)
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
277 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
278 block_fatal_signals ();
280 actions_allocated = false;
281 attrs_allocated = false;
282 if ((err = posix_spawn_file_actions_init (&actions)) != 0
283 || (actions_allocated = true,
285 && (err = posix_spawn_file_actions_adddup2 (&actions,
286 ofd[0], STDIN_FILENO))
289 && (err = posix_spawn_file_actions_adddup2 (&actions,
290 ifd[1], STDOUT_FILENO))
293 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
296 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
299 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
302 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
305 && (err = posix_spawn_file_actions_addopen (&actions,
311 && prog_stdin != NULL
312 && (err = posix_spawn_file_actions_addopen (&actions,
314 prog_stdin, O_RDONLY,
318 && prog_stdout != NULL
319 && (err = posix_spawn_file_actions_addopen (&actions,
321 prog_stdout, O_WRONLY,
325 && ((err = posix_spawnattr_init (&attrs)) != 0
326 || (attrs_allocated = true,
327 (err = posix_spawnattr_setsigmask (&attrs,
330 || (err = posix_spawnattr_setflags (&attrs,
331 POSIX_SPAWN_SETSIGMASK))
333 || (err = posix_spawnp (&child, prog_path, &actions,
334 attrs_allocated ? &attrs : NULL, prog_argv,
338 if (actions_allocated)
339 posix_spawn_file_actions_destroy (&actions);
341 posix_spawnattr_destroy (&attrs);
343 unblock_fatal_signals ();
344 if (exit_on_error || !null_stderr)
345 error (exit_on_error ? EXIT_FAILURE : 0, err,
346 _("%s subprocess failed"), progname);
360 posix_spawn_file_actions_destroy (&actions);
362 posix_spawnattr_destroy (&attrs);
365 register_slave_subprocess (child);
366 unblock_fatal_signals ();
382 /* Open a bidirectional pipe.
385 * parent -> fd[1] -> STDIN_FILENO -> child
386 * parent <- fd[0] <- STDOUT_FILENO <- child
391 create_pipe_bidi (const char *progname,
392 const char *prog_path, char **prog_argv,
394 bool slave_process, bool exit_on_error,
397 pid_t result = create_pipe (progname, prog_path, prog_argv,
398 true, true, NULL, NULL,
399 null_stderr, slave_process, exit_on_error,
404 /* Open a pipe for input from a child process.
405 * The child's stdin comes from a file.
408 * parent <- fd[0] <- STDOUT_FILENO <- child
412 create_pipe_in (const char *progname,
413 const char *prog_path, char **prog_argv,
414 const char *prog_stdin, bool null_stderr,
415 bool slave_process, bool exit_on_error,
419 pid_t result = create_pipe (progname, prog_path, prog_argv,
420 false, true, prog_stdin, NULL,
421 null_stderr, slave_process, exit_on_error,
428 /* Open a pipe for output to a child process.
429 * The child's stdout goes to a file.
432 * parent -> fd[0] -> STDIN_FILENO -> child
436 create_pipe_out (const char *progname,
437 const char *prog_path, char **prog_argv,
438 const char *prog_stdout, bool null_stderr,
439 bool slave_process, bool exit_on_error,
443 pid_t result = create_pipe (progname, prog_path, prog_argv,
444 true, false, NULL, prog_stdout,
445 null_stderr, slave_process, exit_on_error,