]>
Commit | Line | Data |
---|---|---|
5b2d69cc PE |
1 | /* Subprocesses with pipes. |
2 | ||
3ea5f0ec | 3 | Copyright (C) 2002, 2004, 2005 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 | ||
22 | #if HAVE_CONFIG_H | |
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 PE |
36 | |
37 | #if HAVE_UNISTD_H | |
38 | # include <unistd.h> | |
39 | #endif | |
40 | #ifndef STDIN_FILENO | |
41 | # define STDIN_FILENO 0 | |
42 | #endif | |
43 | #ifndef STDOUT_FILENO | |
44 | # define STDOUT_FILENO 1 | |
45 | #endif | |
46 | #if ! HAVE_DUP2 && ! defined dup2 | |
47 | # if HAVE_FCNTL_H | |
48 | # include <fcntl.h> | |
49 | # endif | |
50 | # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t)) | |
51 | #endif | |
52 | ||
53 | #if HAVE_SYS_WAIT_H | |
54 | # include <sys/wait.h> | |
55 | #endif | |
56 | #ifndef WEXITSTATUS | |
57 | # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8) | |
58 | #endif | |
59 | #ifndef WIFEXITED | |
60 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | |
61 | #endif | |
62 | ||
63 | #if HAVE_VFORK_H | |
64 | # include <vfork.h> | |
65 | #endif | |
66 | #if ! HAVE_WORKING_VFORK | |
67 | # define vfork fork | |
68 | #endif | |
69 | ||
70 | #include "error.h" | |
3ea5f0ec | 71 | #include "unistd-safer.h" |
5b2d69cc PE |
72 | |
73 | #include "gettext.h" | |
74 | #define _(Msgid) gettext (Msgid) | |
75 | ||
5b2d69cc PE |
76 | |
77 | /* Initialize this module. */ | |
78 | ||
79 | void | |
80 | init_subpipe (void) | |
81 | { | |
82 | #ifdef SIGCHLD | |
83 | /* System V fork+wait does not work if SIGCHLD is ignored. */ | |
84 | signal (SIGCHLD, SIG_DFL); | |
85 | #endif | |
86 | } | |
87 | ||
88 | ||
89 | /* Create a subprocess that is run as a filter. ARGV is the | |
90 | NULL-terminated argument vector for the subprocess. Store read and | |
91 | write file descriptors for communication with the subprocess into | |
92 | FD[0] and FD[1]: input meant for the process can be written into | |
93 | FD[0], and output from the process can be read from FD[1]. Return | |
94 | the subprocess id. | |
95 | ||
96 | To avoid deadlock, the invoker must not let incoming data pile up | |
97 | in FD[1] while writing data to FD[0]. */ | |
98 | ||
99 | pid_t | |
100 | create_subpipe (char const * const *argv, int fd[2]) | |
101 | { | |
102 | int pipe_fd[2]; | |
103 | int from_in_fd; | |
104 | int from_out_fd; | |
105 | int to_in_fd; | |
106 | int to_out_fd; | |
107 | pid_t pid; | |
108 | ||
3ea5f0ec PE |
109 | if (pipe (pipe_fd) != 0 |
110 | || (to_in_fd = fd_safer (pipe_fd[0])) < 0 | |
111 | || (to_out_fd = fd_safer (pipe_fd[1])) < 0 | |
112 | || pipe (pipe_fd) != 0 | |
113 | || (from_in_fd = fd_safer (pipe_fd[0])) < 0 | |
114 | || (from_out_fd = fd_safer (pipe_fd[1])) < 0) | |
5b2d69cc | 115 | error (EXIT_FAILURE, errno, "pipe"); |
5b2d69cc | 116 | |
4d1801f1 PE |
117 | /* Save the local variables in the parent now, in case vfork |
118 | clobbers them. */ | |
119 | fd[0] = to_out_fd; | |
120 | fd[1] = from_in_fd; | |
121 | pipe_fd[0] = to_in_fd; | |
122 | pipe_fd[1] = from_out_fd; | |
123 | ||
5b2d69cc PE |
124 | pid = vfork (); |
125 | if (pid < 0) | |
126 | error (EXIT_FAILURE, errno, "fork"); | |
127 | ||
128 | if (! pid) | |
129 | { | |
130 | /* Child. */ | |
131 | close (to_out_fd); | |
132 | close (from_in_fd); | |
3ea5f0ec PE |
133 | dup2 (to_in_fd, STDIN_FILENO); |
134 | close (to_in_fd); | |
135 | dup2 (from_out_fd, STDOUT_FILENO); | |
136 | close (from_out_fd); | |
5b2d69cc PE |
137 | |
138 | /* The cast to (char **) rather than (char * const *) is needed | |
139 | for portability to older hosts with a nonstandard prototype | |
140 | for execvp. */ | |
141 | execvp (argv[0], (char **) argv); | |
3ea5f0ec | 142 | |
5b2d69cc PE |
143 | _exit (errno == ENOENT ? 127 : 126); |
144 | } | |
145 | ||
146 | /* Parent. */ | |
4d1801f1 PE |
147 | close (pipe_fd[0]); |
148 | close (pipe_fd[1]); | |
5b2d69cc PE |
149 | return pid; |
150 | } | |
151 | ||
152 | ||
153 | /* Wait for the subprocess to exit. */ | |
154 | ||
155 | void | |
156 | reap_subpipe (pid_t pid, char const *program) | |
157 | { | |
158 | #if HAVE_WAITPID || defined waitpid | |
159 | int wstatus; | |
160 | if (waitpid (pid, &wstatus, 0) < 0) | |
161 | error (EXIT_FAILURE, errno, "waitpid"); | |
162 | else | |
163 | { | |
164 | int status = WIFEXITED (wstatus) ? WEXITSTATUS (wstatus) : -1; | |
165 | if (status) | |
166 | error (EXIT_FAILURE, 0, | |
167 | _(status == 126 | |
168 | ? "subsidiary program `%s' could not be invoked" | |
169 | : status == 127 | |
170 | ? "subsidiary program `%s' not found" | |
171 | : status < 0 | |
172 | ? "subsidiary program `%s' failed" | |
173 | : "subsidiary program `%s' failed (exit status %d)"), | |
174 | program, status); | |
175 | } | |
176 | #endif | |
177 | } |