popen.c revision 1.2 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1980 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.2 mycroft /*static char sccsid[] = "from: @(#)popen.c 5.16 (Berkeley) 4/1/91";*/
36 1.2 mycroft static char rcsid[] = "$Id: popen.c,v 1.2 1993/08/01 18:12:59 mycroft Exp $";
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #include "rcv.h"
40 1.1 cgd #include <sys/signal.h>
41 1.1 cgd #include <sys/wait.h>
42 1.1 cgd
43 1.1 cgd #define READ 0
44 1.1 cgd #define WRITE 1
45 1.1 cgd static int *pid;
46 1.1 cgd
47 1.1 cgd struct fp {
48 1.1 cgd FILE *fp;
49 1.1 cgd int pipe;
50 1.1 cgd struct fp *link;
51 1.1 cgd };
52 1.1 cgd static struct fp *fp_head;
53 1.1 cgd
54 1.1 cgd FILE *
55 1.1 cgd Fopen(file, mode)
56 1.1 cgd char *file, *mode;
57 1.1 cgd {
58 1.1 cgd FILE *fp;
59 1.1 cgd
60 1.1 cgd if ((fp = fopen(file, mode)) != NULL)
61 1.1 cgd register_file(fp, 0);
62 1.1 cgd return fp;
63 1.1 cgd }
64 1.1 cgd
65 1.1 cgd FILE *
66 1.1 cgd Fdopen(fd, mode)
67 1.1 cgd char *mode;
68 1.1 cgd {
69 1.1 cgd FILE *fp;
70 1.1 cgd
71 1.1 cgd if ((fp = fdopen(fd, mode)) != NULL)
72 1.1 cgd register_file(fp, 0);
73 1.1 cgd return fp;
74 1.1 cgd }
75 1.1 cgd
76 1.1 cgd Fclose(fp)
77 1.1 cgd FILE *fp;
78 1.1 cgd {
79 1.1 cgd unregister_file(fp);
80 1.1 cgd return fclose(fp);
81 1.1 cgd }
82 1.1 cgd
83 1.1 cgd FILE *
84 1.1 cgd Popen(cmd, mode)
85 1.1 cgd char *cmd;
86 1.1 cgd char *mode;
87 1.1 cgd {
88 1.1 cgd int p[2];
89 1.1 cgd int myside, hisside, fd0, fd1;
90 1.1 cgd FILE *fp;
91 1.1 cgd
92 1.1 cgd if (pid == 0)
93 1.1 cgd pid = (int *) malloc((unsigned) sizeof (int) * getdtablesize());
94 1.1 cgd if (pipe(p) < 0)
95 1.1 cgd return NULL;
96 1.1 cgd if (*mode == 'r') {
97 1.1 cgd myside = p[READ];
98 1.1 cgd fd0 = -1;
99 1.1 cgd hisside = fd1 = p[WRITE];
100 1.1 cgd } else {
101 1.1 cgd myside = p[WRITE];
102 1.1 cgd hisside = fd0 = p[READ];
103 1.1 cgd fd1 = -1;
104 1.1 cgd }
105 1.1 cgd if ((pid[myside] = start_command(cmd, 0, fd0, fd1, NOSTR)) < 0) {
106 1.1 cgd close(p[READ]);
107 1.1 cgd close(p[WRITE]);
108 1.1 cgd return NULL;
109 1.1 cgd }
110 1.1 cgd (void) close(hisside);
111 1.1 cgd if ((fp = fdopen(myside, mode)) != NULL)
112 1.1 cgd register_file(fp, 1);
113 1.1 cgd return fp;
114 1.1 cgd }
115 1.1 cgd
116 1.1 cgd Pclose(ptr)
117 1.1 cgd FILE *ptr;
118 1.1 cgd {
119 1.1 cgd int i;
120 1.1 cgd int omask;
121 1.1 cgd
122 1.1 cgd i = fileno(ptr);
123 1.1 cgd unregister_file(ptr);
124 1.1 cgd (void) fclose(ptr);
125 1.1 cgd omask = sigblock(sigmask(SIGINT)|sigmask(SIGHUP));
126 1.1 cgd i = wait_child(pid[i]);
127 1.1 cgd sigsetmask(omask);
128 1.1 cgd return i;
129 1.1 cgd }
130 1.1 cgd
131 1.1 cgd close_all_files()
132 1.1 cgd {
133 1.1 cgd
134 1.1 cgd while (fp_head)
135 1.1 cgd if (fp_head->pipe)
136 1.1 cgd (void) Pclose(fp_head->fp);
137 1.1 cgd else
138 1.1 cgd (void) Fclose(fp_head->fp);
139 1.1 cgd }
140 1.1 cgd
141 1.1 cgd register_file(fp, pipe)
142 1.1 cgd FILE *fp;
143 1.1 cgd {
144 1.1 cgd struct fp *fpp;
145 1.1 cgd
146 1.1 cgd if ((fpp = (struct fp *) malloc(sizeof *fpp)) == NULL)
147 1.1 cgd panic("Out of memory");
148 1.1 cgd fpp->fp = fp;
149 1.1 cgd fpp->pipe = pipe;
150 1.1 cgd fpp->link = fp_head;
151 1.1 cgd fp_head = fpp;
152 1.1 cgd }
153 1.1 cgd
154 1.1 cgd unregister_file(fp)
155 1.1 cgd FILE *fp;
156 1.1 cgd {
157 1.1 cgd struct fp **pp, *p;
158 1.1 cgd
159 1.1 cgd for (pp = &fp_head; p = *pp; pp = &p->link)
160 1.1 cgd if (p->fp == fp) {
161 1.1 cgd *pp = p->link;
162 1.1 cgd free((char *) p);
163 1.1 cgd return;
164 1.1 cgd }
165 1.1 cgd /* XXX
166 1.1 cgd * Ignore this for now; there may still be uncaught
167 1.1 cgd * duplicate closes.
168 1.1 cgd panic("Invalid file pointer");
169 1.1 cgd */
170 1.1 cgd }
171 1.1 cgd
172 1.1 cgd /*
173 1.1 cgd * Run a command without a shell, with optional arguments and splicing
174 1.1 cgd * of stdin and stdout. The command name can be a sequence of words.
175 1.1 cgd * Signals must be handled by the caller.
176 1.1 cgd * "Mask" contains the signals to ignore in the new process.
177 1.1 cgd * SIGINT is enabled unless it's in the mask.
178 1.1 cgd */
179 1.1 cgd /*VARARGS4*/
180 1.1 cgd run_command(cmd, mask, infd, outfd, a0, a1, a2)
181 1.1 cgd char *cmd;
182 1.1 cgd int mask, infd, outfd;
183 1.1 cgd char *a0, *a1, *a2;
184 1.1 cgd {
185 1.1 cgd int pid;
186 1.1 cgd
187 1.1 cgd if ((pid = start_command(cmd, mask, infd, outfd, a0, a1, a2)) < 0)
188 1.1 cgd return -1;
189 1.1 cgd return wait_command(pid);
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd /*VARARGS4*/
193 1.1 cgd start_command(cmd, mask, infd, outfd, a0, a1, a2)
194 1.1 cgd char *cmd;
195 1.1 cgd int mask, infd, outfd;
196 1.1 cgd char *a0, *a1, *a2;
197 1.1 cgd {
198 1.1 cgd int pid;
199 1.1 cgd
200 1.1 cgd if ((pid = vfork()) < 0) {
201 1.1 cgd perror("fork");
202 1.1 cgd return -1;
203 1.1 cgd }
204 1.1 cgd if (pid == 0) {
205 1.1 cgd char *argv[100];
206 1.1 cgd int i = getrawlist(cmd, argv, sizeof argv / sizeof *argv);
207 1.1 cgd
208 1.1 cgd if ((argv[i++] = a0) != NOSTR &&
209 1.1 cgd (argv[i++] = a1) != NOSTR &&
210 1.1 cgd (argv[i++] = a2) != NOSTR)
211 1.1 cgd argv[i] = NOSTR;
212 1.1 cgd prepare_child(mask, infd, outfd);
213 1.1 cgd execvp(argv[0], argv);
214 1.1 cgd perror(argv[0]);
215 1.1 cgd _exit(1);
216 1.1 cgd }
217 1.1 cgd return pid;
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd prepare_child(mask, infd, outfd)
221 1.1 cgd int mask, infd, outfd;
222 1.1 cgd {
223 1.1 cgd int i;
224 1.1 cgd
225 1.1 cgd if (infd >= 0)
226 1.1 cgd dup2(infd, 0);
227 1.1 cgd if (outfd >= 0)
228 1.1 cgd dup2(outfd, 1);
229 1.1 cgd for (i = getdtablesize(); --i > 2;)
230 1.1 cgd close(i);
231 1.1 cgd for (i = 1; i <= NSIG; i++)
232 1.1 cgd if (mask & sigmask(i))
233 1.1 cgd (void) signal(i, SIG_IGN);
234 1.1 cgd if ((mask & sigmask(SIGINT)) == 0)
235 1.1 cgd (void) signal(SIGINT, SIG_DFL);
236 1.1 cgd (void) sigsetmask(0);
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd wait_command(pid)
240 1.1 cgd int pid;
241 1.1 cgd {
242 1.1 cgd
243 1.1 cgd if (wait_child(pid) < 0) {
244 1.1 cgd printf("Fatal error in process.\n");
245 1.1 cgd return -1;
246 1.1 cgd }
247 1.1 cgd return 0;
248 1.1 cgd }
249 1.1 cgd
250 1.1 cgd struct child {
251 1.1 cgd int pid;
252 1.1 cgd char done;
253 1.1 cgd char free;
254 1.1 cgd union wait status;
255 1.1 cgd struct child *link;
256 1.1 cgd };
257 1.1 cgd static struct child *child;
258 1.1 cgd
259 1.1 cgd struct child *
260 1.1 cgd findchild(pid)
261 1.1 cgd int pid;
262 1.1 cgd {
263 1.1 cgd register struct child **cpp;
264 1.1 cgd
265 1.1 cgd for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
266 1.1 cgd cpp = &(*cpp)->link)
267 1.1 cgd ;
268 1.1 cgd if (*cpp == NULL) {
269 1.1 cgd *cpp = (struct child *) malloc(sizeof (struct child));
270 1.1 cgd (*cpp)->pid = pid;
271 1.1 cgd (*cpp)->done = (*cpp)->free = 0;
272 1.1 cgd (*cpp)->link = NULL;
273 1.1 cgd }
274 1.1 cgd return *cpp;
275 1.1 cgd }
276 1.1 cgd
277 1.1 cgd delchild(cp)
278 1.1 cgd register struct child *cp;
279 1.1 cgd {
280 1.1 cgd register struct child **cpp;
281 1.1 cgd
282 1.1 cgd for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
283 1.1 cgd ;
284 1.1 cgd *cpp = cp->link;
285 1.1 cgd free((char *) cp);
286 1.1 cgd }
287 1.1 cgd
288 1.1 cgd void
289 1.1 cgd sigchild()
290 1.1 cgd {
291 1.1 cgd int pid;
292 1.1 cgd union wait status;
293 1.1 cgd register struct child *cp;
294 1.1 cgd
295 1.1 cgd while ((pid =
296 1.1 cgd wait3((int *)&status, WNOHANG, (struct rusage *)0)) > 0) {
297 1.1 cgd cp = findchild(pid);
298 1.1 cgd if (cp->free)
299 1.1 cgd delchild(cp);
300 1.1 cgd else {
301 1.1 cgd cp->done = 1;
302 1.1 cgd cp->status = status;
303 1.1 cgd }
304 1.1 cgd }
305 1.1 cgd }
306 1.1 cgd
307 1.1 cgd union wait wait_status;
308 1.1 cgd
309 1.1 cgd /*
310 1.1 cgd * Wait for a specific child to die.
311 1.1 cgd */
312 1.1 cgd wait_child(pid)
313 1.1 cgd int pid;
314 1.1 cgd {
315 1.1 cgd int mask = sigblock(sigmask(SIGCHLD));
316 1.1 cgd register struct child *cp = findchild(pid);
317 1.1 cgd
318 1.1 cgd while (!cp->done)
319 1.1 cgd sigpause(mask);
320 1.1 cgd wait_status = cp->status;
321 1.1 cgd delchild(cp);
322 1.1 cgd sigsetmask(mask);
323 1.1 cgd return wait_status.w_status ? -1 : 0;
324 1.1 cgd }
325 1.1 cgd
326 1.1 cgd /*
327 1.1 cgd * Mark a child as don't care.
328 1.1 cgd */
329 1.1 cgd free_child(pid)
330 1.1 cgd int pid;
331 1.1 cgd {
332 1.1 cgd int mask = sigblock(sigmask(SIGCHLD));
333 1.1 cgd register struct child *cp = findchild(pid);
334 1.1 cgd
335 1.1 cgd if (cp->done)
336 1.1 cgd delchild(cp);
337 1.1 cgd else
338 1.1 cgd cp->free = 1;
339 1.1 cgd sigsetmask(mask);
340 1.1 cgd }
341