1 /* Subprocesses with pipes.
3 Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
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/>. */
18 /* Written by Paul Eggert <eggert@twinsun.com>
19 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */
28 #if ! defined SIGCHLD && defined SIGCLD
29 # define SIGCHLD SIGCLD
36 # define STDIN_FILENO 0
39 # define STDOUT_FILENO 1
41 #if ! HAVE_DUP2 && ! defined dup2
43 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
47 # include <sys/wait.h>
50 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
53 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
59 #if ! HAVE_WORKING_VFORK
64 #include "unistd-safer.h"
67 #define _(Msgid) gettext (Msgid)
70 /* This feature is available in gcc versions 2.5 and later. */
71 # if ! defined __GNUC__ || __GNUC__ < 2 || \
72 (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
73 # define __attribute__(Spec) /* empty */
77 #ifndef ATTRIBUTE_UNUSED
78 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
82 /* Initialize this module. */
88 /* System V fork+wait does not work if SIGCHLD is ignored. */
89 signal (SIGCHLD
, SIG_DFL
);
94 /* Create a subprocess that is run as a filter. ARGV is the
95 NULL-terminated argument vector for the subprocess. Store read and
96 write file descriptors for communication with the subprocess into
97 FD[0] and FD[1]: input meant for the process can be written into
98 FD[0], and output from the process can be read from FD[1]. Return
101 To avoid deadlock, the invoker must not let incoming data pile up
102 in FD[1] while writing data to FD[0]. */
105 create_subpipe (char const * const *argv
, int fd
[2])
111 if (pipe_safer (child_fd
) != 0 || pipe_safer (pipe_fd
) != 0)
112 error (EXIT_FAILURE
, errno
, "pipe");
115 child_fd
[1] = pipe_fd
[1];
119 error (EXIT_FAILURE
, errno
, "fork");
126 dup2 (child_fd
[0], STDIN_FILENO
);
128 dup2 (child_fd
[1], STDOUT_FILENO
);
131 /* The cast to (char **) rather than (char * const *) is needed
132 for portability to older hosts with a nonstandard prototype
134 execvp (argv
[0], (char **) argv
);
136 _exit (errno
== ENOENT
? 127 : 126);
146 /* Wait for the subprocess to exit. */
149 reap_subpipe (pid_t pid
, char const *program
)
151 #if HAVE_WAITPID || defined waitpid
153 if (waitpid (pid
, &wstatus
, 0) < 0)
154 error (EXIT_FAILURE
, errno
, "waitpid");
157 int status
= WIFEXITED (wstatus
) ? WEXITSTATUS (wstatus
) : -1;
159 error (EXIT_FAILURE
, 0,
161 ? "subsidiary program `%s' could not be invoked"
163 ? "subsidiary program `%s' not found"
165 ? "subsidiary program `%s' failed"
166 : "subsidiary program `%s' failed (exit status %d)"),
173 end_of_output_subpipe (pid_t pid ATTRIBUTE_UNUSED
,
174 int fd
[2] ATTRIBUTE_UNUSED
)