]>
Commit | Line | Data |
---|---|---|
5b2d69cc PE |
1 | /* Subprocesses with pipes. |
2 | ||
c66dfadd | 3 | Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. |
5b2d69cc PE |
4 | |
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) | |
8 | any later version. | |
9 | ||
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. | |
14 | ||
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, | |
0fb669f9 | 17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
5b2d69cc PE |
18 | |
19 | /* Written by Paul Eggert <eggert@twinsun.com> | |
20 | and Florian Krohm <florian@edamail.fishkill.ibm.com>. */ | |
21 | ||
02650b7f | 22 | #ifdef HAVE_CONFIG_H |
5b2d69cc PE |
23 | # include <config.h> |
24 | #endif | |
25 | ||
b970803c | 26 | #include "subpipe.h" |
5b2d69cc PE |
27 | |
28 | #include <errno.h> | |
5b2d69cc PE |
29 | |
30 | #include <signal.h> | |
31 | #if ! defined SIGCHLD && defined SIGCLD | |
32 | # define SIGCHLD SIGCLD | |
33 | #endif | |
34 | ||
b970803c | 35 | #include <stdlib.h> |
5b2d69cc | 36 | |
3b2942e6 | 37 | #include <unistd.h> |
5b2d69cc PE |
38 | #ifndef STDIN_FILENO |
39 | # define STDIN_FILENO 0 | |
40 | #endif | |
41 | #ifndef STDOUT_FILENO | |
42 | # define STDOUT_FILENO 1 | |
43 | #endif | |
44 | #if ! HAVE_DUP2 && ! defined dup2 | |
04098407 | 45 | # include <fcntl.h> |
5b2d69cc PE |
46 | # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t)) |
47 | #endif | |
48 | ||
49 | #if HAVE_SYS_WAIT_H | |
50 | # include <sys/wait.h> | |
51 | #endif | |
52 | #ifndef WEXITSTATUS | |
53 | # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8) | |
54 | #endif | |
55 | #ifndef WIFEXITED | |
56 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | |
57 | #endif | |
58 | ||
59 | #if HAVE_VFORK_H | |
60 | # include <vfork.h> | |
61 | #endif | |
62 | #if ! HAVE_WORKING_VFORK | |
63 | # define vfork fork | |
64 | #endif | |
65 | ||
66 | #include "error.h" | |
3ea5f0ec | 67 | #include "unistd-safer.h" |
5b2d69cc PE |
68 | |
69 | #include "gettext.h" | |
70 | #define _(Msgid) gettext (Msgid) | |
71 | ||
c66dfadd PE |
72 | #ifndef __attribute__ |
73 | /* This feature is available in gcc versions 2.5 and later. */ | |
02650b7f | 74 | # if ! defined __GNUC__ || __GNUC__ < 2 || \ |
c66dfadd PE |
75 | (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ |
76 | # define __attribute__(Spec) /* empty */ | |
77 | # endif | |
78 | #endif | |
79 | ||
80 | #ifndef ATTRIBUTE_UNUSED | |
81 | # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) | |
82 | #endif | |
83 | ||
5b2d69cc PE |
84 | |
85 | /* Initialize this module. */ | |
86 | ||
87 | void | |
88 | init_subpipe (void) | |
89 | { | |
90 | #ifdef SIGCHLD | |
91 | /* System V fork+wait does not work if SIGCHLD is ignored. */ | |
92 | signal (SIGCHLD, SIG_DFL); | |
93 | #endif | |
94 | } | |
95 | ||
96 | ||
97 | /* Create a subprocess that is run as a filter. ARGV is the | |
98 | NULL-terminated argument vector for the subprocess. Store read and | |
99 | write file descriptors for communication with the subprocess into | |
100 | FD[0] and FD[1]: input meant for the process can be written into | |
101 | FD[0], and output from the process can be read from FD[1]. Return | |
102 | the subprocess id. | |
103 | ||
104 | To avoid deadlock, the invoker must not let incoming data pile up | |
105 | in FD[1] while writing data to FD[0]. */ | |
106 | ||
107 | pid_t | |
108 | create_subpipe (char const * const *argv, int fd[2]) | |
109 | { | |
110 | int pipe_fd[2]; | |
0fab8d7b | 111 | int child_fd[2]; |
5b2d69cc PE |
112 | pid_t pid; |
113 | ||
b4c2a797 PE |
114 | if (pipe_safer (child_fd) != 0 || pipe_safer (pipe_fd) != 0) |
115 | error (EXIT_FAILURE, errno, "pipe"); | |
116 | fd[0] = child_fd[1]; | |
117 | fd[1] = pipe_fd[0]; | |
118 | child_fd[1] = pipe_fd[1]; | |
5b2d69cc PE |
119 | |
120 | pid = vfork (); | |
121 | if (pid < 0) | |
b4c2a797 | 122 | error (EXIT_FAILURE, errno, "fork"); |
5b2d69cc PE |
123 | |
124 | if (! pid) | |
125 | { | |
126 | /* Child. */ | |
0fab8d7b PE |
127 | close (fd[0]); |
128 | close (fd[1]); | |
129 | dup2 (child_fd[0], STDIN_FILENO); | |
130 | close (child_fd[0]); | |
131 | dup2 (child_fd[1], STDOUT_FILENO); | |
132 | close (child_fd[1]); | |
5b2d69cc PE |
133 | |
134 | /* The cast to (char **) rather than (char * const *) is needed | |
135 | for portability to older hosts with a nonstandard prototype | |
136 | for execvp. */ | |
137 | execvp (argv[0], (char **) argv); | |
3ea5f0ec | 138 | |
5b2d69cc PE |
139 | _exit (errno == ENOENT ? 127 : 126); |
140 | } | |
141 | ||
142 | /* Parent. */ | |
0fab8d7b PE |
143 | close (child_fd[0]); |
144 | close (child_fd[1]); | |
5b2d69cc PE |
145 | return pid; |
146 | } | |
147 | ||
148 | ||
149 | /* Wait for the subprocess to exit. */ | |
150 | ||
151 | void | |
152 | reap_subpipe (pid_t pid, char const *program) | |
153 | { | |
154 | #if HAVE_WAITPID || defined waitpid | |
155 | int wstatus; | |
156 | if (waitpid (pid, &wstatus, 0) < 0) | |
b4c2a797 | 157 | error (EXIT_FAILURE, errno, "waitpid"); |
5b2d69cc PE |
158 | else |
159 | { | |
160 | int status = WIFEXITED (wstatus) ? WEXITSTATUS (wstatus) : -1; | |
161 | if (status) | |
162 | error (EXIT_FAILURE, 0, | |
163 | _(status == 126 | |
164 | ? "subsidiary program `%s' could not be invoked" | |
165 | : status == 127 | |
166 | ? "subsidiary program `%s' not found" | |
167 | : status < 0 | |
168 | ? "subsidiary program `%s' failed" | |
169 | : "subsidiary program `%s' failed (exit status %d)"), | |
170 | program, status); | |
171 | } | |
172 | #endif | |
173 | } | |
3d86656c JMG |
174 | |
175 | void | |
c66dfadd PE |
176 | end_of_output_subpipe (pid_t pid ATTRIBUTE_UNUSED, |
177 | int fd[2] ATTRIBUTE_UNUSED) | |
3d86656c JMG |
178 | { |
179 | } |