1 /* Subprocesses with pipes.
3 Copyright (C) 2002, 2004-2006, 2009-2010 Free Software Foundation,
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert <eggert@twinsun.com>
20 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */
29 #if ! defined SIGCHLD && defined SIGCLD
30 # define SIGCHLD SIGCLD
37 # define STDIN_FILENO 0
40 # define STDOUT_FILENO 1
42 #if ! HAVE_DUP2 && ! defined dup2
44 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
48 # include <sys/wait.h>
51 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
54 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
60 #if ! HAVE_WORKING_VFORK
65 #include "unistd-safer.h"
68 #define _(Msgid) gettext (Msgid)
71 /* This feature is available in gcc versions 2.5 and later. */
72 # if ! defined __GNUC__ || __GNUC__ < 2 || \
73 (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
74 # define __attribute__(Spec) /* empty */
78 #ifndef ATTRIBUTE_UNUSED
79 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
83 /* Initialize this module. */
89 /* System V fork+wait does not work if SIGCHLD is ignored. */
90 signal (SIGCHLD
, SIG_DFL
);
95 /* Create a subprocess that is run as a filter. ARGV is the
96 NULL-terminated argument vector for the subprocess. Store read and
97 write file descriptors for communication with the subprocess into
98 FD[0] and FD[1]: input meant for the process can be written into
99 FD[0], and output from the process can be read from FD[1]. Return
102 To avoid deadlock, the invoker must not let incoming data pile up
103 in FD[1] while writing data to FD[0]. */
106 create_subpipe (char const * const *argv
, int fd
[2])
112 if (pipe_safer (child_fd
) != 0 || pipe_safer (pipe_fd
) != 0)
113 error (EXIT_FAILURE
, errno
, "pipe");
116 child_fd
[1] = pipe_fd
[1];
120 error (EXIT_FAILURE
, errno
, "fork");
127 dup2 (child_fd
[0], STDIN_FILENO
);
129 dup2 (child_fd
[1], STDOUT_FILENO
);
132 /* The cast to (char **) rather than (char * const *) is needed
133 for portability to older hosts with a nonstandard prototype
135 execvp (argv
[0], (char **) argv
);
137 _exit (errno
== ENOENT
? 127 : 126);
147 /* Wait for the subprocess to exit. */
150 reap_subpipe (pid_t pid
, char const *program
)
152 #if HAVE_WAITPID || defined waitpid
154 if (waitpid (pid
, &wstatus
, 0) < 0)
155 error (EXIT_FAILURE
, errno
, "waitpid");
158 int status
= WIFEXITED (wstatus
) ? WEXITSTATUS (wstatus
) : -1;
160 error (EXIT_FAILURE
, 0,
162 ? "subsidiary program `%s' could not be invoked"
164 ? "subsidiary program `%s' not found"
166 ? "subsidiary program `%s' failed"
167 : "subsidiary program `%s' failed (exit status %d)"),
174 end_of_output_subpipe (pid_t pid ATTRIBUTE_UNUSED
,
175 int fd
[2] ATTRIBUTE_UNUSED
)