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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert <eggert@twinsun.com>
20 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */
31 #if ! defined SIGCHLD && defined SIGCLD
32 # define SIGCHLD SIGCLD
41 # define STDIN_FILENO 0
44 # define STDOUT_FILENO 1
46 #if ! HAVE_DUP2 && ! defined dup2
48 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
52 # include <sys/wait.h>
55 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
58 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
64 #if ! HAVE_WORKING_VFORK
69 #include "unistd-safer.h"
72 #define _(Msgid) gettext (Msgid)
75 /* This feature is available in gcc versions 2.5 and later. */
76 # if ! defined __GNUC__ || __GNUC__ < 2 || \
77 (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
78 # define __attribute__(Spec) /* empty */
82 #ifndef ATTRIBUTE_UNUSED
83 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
87 /* Initialize this module. */
93 /* System V fork+wait does not work if SIGCHLD is ignored. */
94 signal (SIGCHLD
, SIG_DFL
);
99 /* Create a subprocess that is run as a filter. ARGV is the
100 NULL-terminated argument vector for the subprocess. Store read and
101 write file descriptors for communication with the subprocess into
102 FD[0] and FD[1]: input meant for the process can be written into
103 FD[0], and output from the process can be read from FD[1]. Return
106 To avoid deadlock, the invoker must not let incoming data pile up
107 in FD[1] while writing data to FD[0]. */
110 create_subpipe (char const * const *argv
, int fd
[2])
116 if (pipe_safer (child_fd
) != 0 || pipe_safer (pipe_fd
) != 0)
117 error (EXIT_FAILURE
, errno
, "pipe");
120 child_fd
[1] = pipe_fd
[1];
124 error (EXIT_FAILURE
, errno
, "fork");
131 dup2 (child_fd
[0], STDIN_FILENO
);
133 dup2 (child_fd
[1], STDOUT_FILENO
);
136 /* The cast to (char **) rather than (char * const *) is needed
137 for portability to older hosts with a nonstandard prototype
139 execvp (argv
[0], (char **) argv
);
141 _exit (errno
== ENOENT
? 127 : 126);
151 /* Wait for the subprocess to exit. */
154 reap_subpipe (pid_t pid
, char const *program
)
156 #if HAVE_WAITPID || defined waitpid
158 if (waitpid (pid
, &wstatus
, 0) < 0)
159 error (EXIT_FAILURE
, errno
, "waitpid");
162 int status
= WIFEXITED (wstatus
) ? WEXITSTATUS (wstatus
) : -1;
164 error (EXIT_FAILURE
, 0,
166 ? "subsidiary program `%s' could not be invoked"
168 ? "subsidiary program `%s' not found"
170 ? "subsidiary program `%s' failed"
171 : "subsidiary program `%s' failed (exit status %d)"),
178 end_of_output_subpipe (pid_t pid ATTRIBUTE_UNUSED
,
179 int fd
[2] ATTRIBUTE_UNUSED
)