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
63 #ifndef HAVE_ENVIRON_DECL
64 extern char **environ
;
68 # define STDIN_FILENO 0
71 # define STDOUT_FILENO 1
74 # define STDERR_FILENO 2
80 /* EINTR handling for close().
81 These functions can return -1/EINTR even though we don't have any
82 signal handlers set up, namely when we get interrupted via SIGSTOP. */
85 nonintr_close (int fd
)
91 while (retval
< 0 && errno
== EINTR
);
95 #define close nonintr_close
98 nonintr_open (const char *pathname
, int oflag
, mode_t mode
)
103 retval
= open (pathname
, oflag
, mode
);
104 while (retval
< 0 && errno
== EINTR
);
108 #undef open /* avoid warning on VMS */
109 #define open nonintr_open
114 /* Open a pipe connected to a child process.
117 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
118 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
121 * At least one of pipe_stdin, pipe_stdout must be true.
122 * pipe_stdin and prog_stdin together determine the child's standard input.
123 * pipe_stdout and prog_stdout together determine the child's standard output.
124 * If pipe_stdin is true, prog_stdin is ignored.
125 * If pipe_stdout is true, prog_stdout is ignored.
128 create_pipe (const char *progname
,
129 const char *prog_path
, char **prog_argv
,
130 bool pipe_stdin
, bool pipe_stdout
,
131 const char *prog_stdin
, const char *prog_stdout
,
133 bool slave_process
, bool exit_on_error
,
136 #if defined _MSC_VER || defined __MINGW32__
139 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
140 using the low-level functions CreatePipe(), DuplicateHandle(),
141 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
142 and cvs source code. */
153 prog_argv
= prepare_spawn (prog_argv
);
156 if (_pipe (ifd
, 4096, O_BINARY
| O_NOINHERIT
) < 0)
157 error (EXIT_FAILURE
, errno
, _("cannot create pipe"));
159 if (_pipe (ofd
, 4096, O_BINARY
| O_NOINHERIT
) < 0)
160 error (EXIT_FAILURE
, errno
, _("cannot create pipe"));
161 /* Data flow diagram:
164 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
165 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
170 /* Save standard file handles of parent process. */
171 if (pipe_stdin
|| prog_stdin
!= NULL
)
172 orig_stdin
= dup_noinherit (STDIN_FILENO
);
173 if (pipe_stdout
|| prog_stdout
!= NULL
)
174 orig_stdout
= dup_noinherit (STDOUT_FILENO
);
176 orig_stderr
= dup_noinherit (STDERR_FILENO
);
179 /* Create standard file handles of child process. */
183 if ((!pipe_stdin
|| dup2 (ofd
[0], STDIN_FILENO
) >= 0)
184 && (!pipe_stdout
|| dup2 (ifd
[1], STDOUT_FILENO
) >= 0)
186 || ((nulloutfd
= open ("NUL", O_RDWR
, 0)) >= 0
187 && (nulloutfd
== STDERR_FILENO
188 || (dup2 (nulloutfd
, STDERR_FILENO
) >= 0
189 && close (nulloutfd
) >= 0))))
191 || prog_stdin
== NULL
192 || ((stdinfd
= open (prog_stdin
, O_RDONLY
, 0)) >= 0
193 && (stdinfd
== STDIN_FILENO
194 || (dup2 (stdinfd
, STDIN_FILENO
) >= 0
195 && close (stdinfd
) >= 0))))
197 || prog_stdout
== NULL
198 || ((stdoutfd
= open (prog_stdout
, O_WRONLY
, 0)) >= 0
199 && (stdoutfd
== STDOUT_FILENO
200 || (dup2 (stdoutfd
, STDOUT_FILENO
) >= 0
201 && close (stdoutfd
) >= 0)))))
202 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
203 but it inherits all open()ed or dup2()ed file handles (which is what
204 we want in the case of STD*_FILENO) and also orig_stdin,
205 orig_stdout, orig_stderr (which is not explicitly wanted but
207 child
= spawnvp (P_NOWAIT
, prog_path
, prog_argv
);
215 /* Restore standard file handles of parent process. */
217 dup2 (orig_stderr
, STDERR_FILENO
), close (orig_stderr
);
218 if (pipe_stdout
|| prog_stdout
!= NULL
)
219 dup2 (orig_stdout
, STDOUT_FILENO
), close (orig_stdout
);
220 if (pipe_stdin
|| prog_stdin
!= NULL
)
221 dup2 (orig_stdin
, STDIN_FILENO
), close (orig_stdin
);
229 if (exit_on_error
|| !null_stderr
)
230 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
231 _("%s subprocess failed"), progname
);
250 # if HAVE_POSIX_SPAWN
251 sigset_t blocked_signals
;
252 posix_spawn_file_actions_t actions
;
253 bool actions_allocated
;
254 posix_spawnattr_t attrs
;
255 bool attrs_allocated
;
264 error (EXIT_FAILURE
, errno
, _("cannot create pipe"));
267 error (EXIT_FAILURE
, errno
, _("cannot create pipe"));
268 /* Data flow diagram:
271 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
272 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
277 # if HAVE_POSIX_SPAWN
280 sigprocmask (SIG_SETMASK
, NULL
, &blocked_signals
);
281 block_fatal_signals ();
283 actions_allocated
= false;
284 attrs_allocated
= false;
285 if ((err
= posix_spawn_file_actions_init (&actions
)) != 0
286 || (actions_allocated
= true,
288 && (err
= posix_spawn_file_actions_adddup2 (&actions
,
289 ofd
[0], STDIN_FILENO
))
292 && (err
= posix_spawn_file_actions_adddup2 (&actions
,
293 ifd
[1], STDOUT_FILENO
))
296 && (err
= posix_spawn_file_actions_addclose (&actions
, ofd
[0]))
299 && (err
= posix_spawn_file_actions_addclose (&actions
, ifd
[1]))
302 && (err
= posix_spawn_file_actions_addclose (&actions
, ofd
[1]))
305 && (err
= posix_spawn_file_actions_addclose (&actions
, ifd
[0]))
308 && (err
= posix_spawn_file_actions_addopen (&actions
,
314 && prog_stdin
!= NULL
315 && (err
= posix_spawn_file_actions_addopen (&actions
,
317 prog_stdin
, O_RDONLY
,
321 && prog_stdout
!= NULL
322 && (err
= posix_spawn_file_actions_addopen (&actions
,
324 prog_stdout
, O_WRONLY
,
328 && ((err
= posix_spawnattr_init (&attrs
)) != 0
329 || (attrs_allocated
= true,
330 (err
= posix_spawnattr_setsigmask (&attrs
,
333 || (err
= posix_spawnattr_setflags (&attrs
,
334 POSIX_SPAWN_SETSIGMASK
))
336 || (err
= posix_spawnp (&child
, prog_path
, &actions
,
337 attrs_allocated
? &attrs
: NULL
, prog_argv
,
341 if (actions_allocated
)
342 posix_spawn_file_actions_destroy (&actions
);
344 posix_spawnattr_destroy (&attrs
);
346 unblock_fatal_signals ();
347 if (exit_on_error
|| !null_stderr
)
348 error (exit_on_error
? EXIT_FAILURE
: 0, err
,
349 _("%s subprocess failed"), progname
);
362 posix_spawn_file_actions_destroy (&actions
);
364 posix_spawnattr_destroy (&attrs
);
367 block_fatal_signals ();
368 /* Use vfork() instead of fork() for efficiency. */
369 if ((child
= vfork ()) == 0)
371 /* Child process code. */
376 if ((!pipe_stdin
|| dup2 (ofd
[0], STDIN_FILENO
) >= 0)
377 && (!pipe_stdout
|| dup2 (ifd
[1], STDOUT_FILENO
) >= 0)
378 && (!pipe_stdin
|| close (ofd
[0]) >= 0)
379 && (!pipe_stdout
|| close (ifd
[1]) >= 0)
380 && (!pipe_stdin
|| close (ofd
[1]) >= 0)
381 && (!pipe_stdout
|| close (ifd
[0]) >= 0)
383 || ((nulloutfd
= open ("/dev/null", O_RDWR
, 0)) >= 0
384 && (nulloutfd
== STDERR_FILENO
385 || (dup2 (nulloutfd
, STDERR_FILENO
) >= 0
386 && close (nulloutfd
) >= 0))))
388 || prog_stdin
== NULL
389 || ((stdinfd
= open (prog_stdin
, O_RDONLY
, 0)) >= 0
390 && (stdinfd
== STDIN_FILENO
391 || (dup2 (stdinfd
, STDIN_FILENO
) >= 0
392 && close (stdinfd
) >= 0))))
394 || prog_stdout
== NULL
395 || ((stdoutfd
= open (prog_stdout
, O_WRONLY
, 0)) >= 0
396 && (stdoutfd
== STDOUT_FILENO
397 || (dup2 (stdoutfd
, STDOUT_FILENO
) >= 0
398 && close (stdoutfd
) >= 0))))
399 && (!slave_process
|| (unblock_fatal_signals (), true)))
400 execvp (prog_path
, prog_argv
);
406 unblock_fatal_signals ();
407 if (exit_on_error
|| !null_stderr
)
408 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
409 _("%s subprocess failed"), progname
);
425 register_slave_subprocess (child
);
426 unblock_fatal_signals ();
442 /* Open a bidirectional pipe.
445 * parent -> fd[1] -> STDIN_FILENO -> child
446 * parent <- fd[0] <- STDOUT_FILENO <- child
451 create_pipe_bidi (const char *progname
,
452 const char *prog_path
, char **prog_argv
,
454 bool slave_process
, bool exit_on_error
,
457 pid_t result
= create_pipe (progname
, prog_path
, prog_argv
,
458 true, true, NULL
, NULL
,
459 null_stderr
, slave_process
, exit_on_error
,
464 /* Open a pipe for input from a child process.
465 * The child's stdin comes from a file.
468 * parent <- fd[0] <- STDOUT_FILENO <- child
472 create_pipe_in (const char *progname
,
473 const char *prog_path
, char **prog_argv
,
474 const char *prog_stdin
, bool null_stderr
,
475 bool slave_process
, bool exit_on_error
,
479 pid_t result
= create_pipe (progname
, prog_path
, prog_argv
,
480 false, true, prog_stdin
, NULL
,
481 null_stderr
, slave_process
, exit_on_error
,
488 /* Open a pipe for output to a child process.
489 * The child's stdout goes to a file.
492 * parent -> fd[0] -> STDIN_FILENO -> child
496 create_pipe_out (const char *progname
,
497 const char *prog_path
, char **prog_argv
,
498 const char *prog_stdout
, bool null_stderr
,
499 bool slave_process
, bool exit_on_error
,
503 pid_t result
= create_pipe (progname
, prog_path
, prog_argv
,
504 true, false, NULL
, prog_stdout
,
505 null_stderr
, slave_process
, exit_on_error
,