popen.c revision 1.1.1.2 1 1.1 cgd /*
2 1.1.1.2 tls * Copyright (c) 1980, 1993
3 1.1.1.2 tls * The Regents of the University of California. 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.1.1.2 tls static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/6/93";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #include "rcv.h"
39 1.1 cgd #include <sys/wait.h>
40 1.1.1.2 tls #include <fcntl.h>
41 1.1.1.2 tls #include "extern.h"
42 1.1 cgd
43 1.1 cgd #define READ 0
44 1.1 cgd #define WRITE 1
45 1.1 cgd
46 1.1 cgd struct fp {
47 1.1 cgd FILE *fp;
48 1.1 cgd int pipe;
49 1.1.1.2 tls int pid;
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.1.2 tls struct child {
55 1.1.1.2 tls int pid;
56 1.1.1.2 tls char done;
57 1.1.1.2 tls char free;
58 1.1.1.2 tls union wait status;
59 1.1.1.2 tls struct child *link;
60 1.1.1.2 tls };
61 1.1.1.2 tls static struct child *child;
62 1.1.1.2 tls static struct child *findchild __P((int));
63 1.1.1.2 tls static void delchild __P((struct child *));
64 1.1.1.2 tls
65 1.1 cgd FILE *
66 1.1 cgd Fopen(file, mode)
67 1.1 cgd char *file, *mode;
68 1.1 cgd {
69 1.1 cgd FILE *fp;
70 1.1 cgd
71 1.1.1.2 tls if ((fp = fopen(file, mode)) != NULL) {
72 1.1.1.2 tls register_file(fp, 0, 0);
73 1.1.1.2 tls (void) fcntl(fileno(fp), F_SETFD, 1);
74 1.1.1.2 tls }
75 1.1 cgd return fp;
76 1.1 cgd }
77 1.1 cgd
78 1.1 cgd FILE *
79 1.1 cgd Fdopen(fd, mode)
80 1.1.1.2 tls int fd;
81 1.1 cgd char *mode;
82 1.1 cgd {
83 1.1 cgd FILE *fp;
84 1.1 cgd
85 1.1.1.2 tls if ((fp = fdopen(fd, mode)) != NULL) {
86 1.1.1.2 tls register_file(fp, 0, 0);
87 1.1.1.2 tls (void) fcntl(fileno(fp), F_SETFD, 1);
88 1.1.1.2 tls }
89 1.1 cgd return fp;
90 1.1 cgd }
91 1.1 cgd
92 1.1.1.2 tls int
93 1.1 cgd Fclose(fp)
94 1.1 cgd FILE *fp;
95 1.1 cgd {
96 1.1 cgd unregister_file(fp);
97 1.1 cgd return fclose(fp);
98 1.1 cgd }
99 1.1 cgd
100 1.1 cgd FILE *
101 1.1 cgd Popen(cmd, mode)
102 1.1 cgd char *cmd;
103 1.1 cgd char *mode;
104 1.1 cgd {
105 1.1 cgd int p[2];
106 1.1 cgd int myside, hisside, fd0, fd1;
107 1.1.1.2 tls int pid;
108 1.1 cgd FILE *fp;
109 1.1 cgd
110 1.1 cgd if (pipe(p) < 0)
111 1.1 cgd return NULL;
112 1.1.1.2 tls (void) fcntl(p[READ], F_SETFD, 1);
113 1.1.1.2 tls (void) fcntl(p[WRITE], F_SETFD, 1);
114 1.1 cgd if (*mode == 'r') {
115 1.1 cgd myside = p[READ];
116 1.1 cgd fd0 = -1;
117 1.1 cgd hisside = fd1 = p[WRITE];
118 1.1 cgd } else {
119 1.1 cgd myside = p[WRITE];
120 1.1 cgd hisside = fd0 = p[READ];
121 1.1 cgd fd1 = -1;
122 1.1 cgd }
123 1.1.1.2 tls if ((pid = start_command(cmd, 0, fd0, fd1, NOSTR, NOSTR, NOSTR)) < 0) {
124 1.1 cgd close(p[READ]);
125 1.1 cgd close(p[WRITE]);
126 1.1 cgd return NULL;
127 1.1 cgd }
128 1.1 cgd (void) close(hisside);
129 1.1 cgd if ((fp = fdopen(myside, mode)) != NULL)
130 1.1.1.2 tls register_file(fp, 1, pid);
131 1.1 cgd return fp;
132 1.1 cgd }
133 1.1 cgd
134 1.1.1.2 tls int
135 1.1 cgd Pclose(ptr)
136 1.1 cgd FILE *ptr;
137 1.1 cgd {
138 1.1 cgd int i;
139 1.1 cgd int omask;
140 1.1 cgd
141 1.1.1.2 tls i = file_pid(ptr);
142 1.1 cgd unregister_file(ptr);
143 1.1 cgd (void) fclose(ptr);
144 1.1 cgd omask = sigblock(sigmask(SIGINT)|sigmask(SIGHUP));
145 1.1.1.2 tls i = wait_child(i);
146 1.1 cgd sigsetmask(omask);
147 1.1 cgd return i;
148 1.1 cgd }
149 1.1 cgd
150 1.1.1.2 tls void
151 1.1 cgd close_all_files()
152 1.1 cgd {
153 1.1 cgd
154 1.1 cgd while (fp_head)
155 1.1 cgd if (fp_head->pipe)
156 1.1 cgd (void) Pclose(fp_head->fp);
157 1.1 cgd else
158 1.1 cgd (void) Fclose(fp_head->fp);
159 1.1 cgd }
160 1.1 cgd
161 1.1.1.2 tls void
162 1.1.1.2 tls register_file(fp, pipe, pid)
163 1.1 cgd FILE *fp;
164 1.1.1.2 tls int pipe, pid;
165 1.1 cgd {
166 1.1 cgd struct fp *fpp;
167 1.1 cgd
168 1.1 cgd if ((fpp = (struct fp *) malloc(sizeof *fpp)) == NULL)
169 1.1 cgd panic("Out of memory");
170 1.1 cgd fpp->fp = fp;
171 1.1 cgd fpp->pipe = pipe;
172 1.1.1.2 tls fpp->pid = pid;
173 1.1 cgd fpp->link = fp_head;
174 1.1 cgd fp_head = fpp;
175 1.1 cgd }
176 1.1 cgd
177 1.1.1.2 tls void
178 1.1 cgd unregister_file(fp)
179 1.1 cgd FILE *fp;
180 1.1 cgd {
181 1.1 cgd struct fp **pp, *p;
182 1.1 cgd
183 1.1 cgd for (pp = &fp_head; p = *pp; pp = &p->link)
184 1.1 cgd if (p->fp == fp) {
185 1.1 cgd *pp = p->link;
186 1.1 cgd free((char *) p);
187 1.1 cgd return;
188 1.1 cgd }
189 1.1 cgd panic("Invalid file pointer");
190 1.1.1.2 tls }
191 1.1.1.2 tls
192 1.1.1.2 tls file_pid(fp)
193 1.1.1.2 tls FILE *fp;
194 1.1.1.2 tls {
195 1.1.1.2 tls struct fp *p;
196 1.1.1.2 tls
197 1.1.1.2 tls for (p = fp_head; p; p = p->link)
198 1.1.1.2 tls if (p->fp == fp)
199 1.1.1.2 tls return (p->pid);
200 1.1.1.2 tls panic("Invalid file pointer");
201 1.1.1.2 tls /*NOTREACHED*/
202 1.1 cgd }
203 1.1 cgd
204 1.1 cgd /*
205 1.1 cgd * Run a command without a shell, with optional arguments and splicing
206 1.1 cgd * of stdin and stdout. The command name can be a sequence of words.
207 1.1 cgd * Signals must be handled by the caller.
208 1.1 cgd * "Mask" contains the signals to ignore in the new process.
209 1.1 cgd * SIGINT is enabled unless it's in the mask.
210 1.1 cgd */
211 1.1 cgd /*VARARGS4*/
212 1.1.1.2 tls int
213 1.1 cgd run_command(cmd, mask, infd, outfd, a0, a1, a2)
214 1.1 cgd char *cmd;
215 1.1 cgd int mask, infd, outfd;
216 1.1 cgd char *a0, *a1, *a2;
217 1.1 cgd {
218 1.1 cgd int pid;
219 1.1 cgd
220 1.1 cgd if ((pid = start_command(cmd, mask, infd, outfd, a0, a1, a2)) < 0)
221 1.1 cgd return -1;
222 1.1 cgd return wait_command(pid);
223 1.1 cgd }
224 1.1 cgd
225 1.1 cgd /*VARARGS4*/
226 1.1.1.2 tls int
227 1.1 cgd start_command(cmd, mask, infd, outfd, a0, a1, a2)
228 1.1 cgd char *cmd;
229 1.1 cgd int mask, infd, outfd;
230 1.1 cgd char *a0, *a1, *a2;
231 1.1 cgd {
232 1.1 cgd int pid;
233 1.1 cgd
234 1.1 cgd if ((pid = vfork()) < 0) {
235 1.1 cgd perror("fork");
236 1.1 cgd return -1;
237 1.1 cgd }
238 1.1 cgd if (pid == 0) {
239 1.1 cgd char *argv[100];
240 1.1 cgd int i = getrawlist(cmd, argv, sizeof argv / sizeof *argv);
241 1.1 cgd
242 1.1 cgd if ((argv[i++] = a0) != NOSTR &&
243 1.1 cgd (argv[i++] = a1) != NOSTR &&
244 1.1 cgd (argv[i++] = a2) != NOSTR)
245 1.1 cgd argv[i] = NOSTR;
246 1.1 cgd prepare_child(mask, infd, outfd);
247 1.1 cgd execvp(argv[0], argv);
248 1.1 cgd perror(argv[0]);
249 1.1 cgd _exit(1);
250 1.1 cgd }
251 1.1 cgd return pid;
252 1.1 cgd }
253 1.1 cgd
254 1.1.1.2 tls void
255 1.1 cgd prepare_child(mask, infd, outfd)
256 1.1 cgd int mask, infd, outfd;
257 1.1 cgd {
258 1.1 cgd int i;
259 1.1 cgd
260 1.1.1.2 tls /*
261 1.1.1.2 tls * All file descriptors other than 0, 1, and 2 are supposed to be
262 1.1.1.2 tls * close-on-exec.
263 1.1.1.2 tls */
264 1.1 cgd if (infd >= 0)
265 1.1 cgd dup2(infd, 0);
266 1.1 cgd if (outfd >= 0)
267 1.1 cgd dup2(outfd, 1);
268 1.1 cgd for (i = 1; i <= NSIG; i++)
269 1.1 cgd if (mask & sigmask(i))
270 1.1 cgd (void) signal(i, SIG_IGN);
271 1.1 cgd if ((mask & sigmask(SIGINT)) == 0)
272 1.1 cgd (void) signal(SIGINT, SIG_DFL);
273 1.1 cgd (void) sigsetmask(0);
274 1.1 cgd }
275 1.1 cgd
276 1.1.1.2 tls int
277 1.1 cgd wait_command(pid)
278 1.1 cgd int pid;
279 1.1 cgd {
280 1.1 cgd
281 1.1 cgd if (wait_child(pid) < 0) {
282 1.1 cgd printf("Fatal error in process.\n");
283 1.1 cgd return -1;
284 1.1 cgd }
285 1.1 cgd return 0;
286 1.1 cgd }
287 1.1 cgd
288 1.1.1.2 tls static struct child *
289 1.1 cgd findchild(pid)
290 1.1 cgd int pid;
291 1.1 cgd {
292 1.1 cgd register struct child **cpp;
293 1.1 cgd
294 1.1 cgd for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
295 1.1 cgd cpp = &(*cpp)->link)
296 1.1 cgd ;
297 1.1 cgd if (*cpp == NULL) {
298 1.1 cgd *cpp = (struct child *) malloc(sizeof (struct child));
299 1.1 cgd (*cpp)->pid = pid;
300 1.1 cgd (*cpp)->done = (*cpp)->free = 0;
301 1.1 cgd (*cpp)->link = NULL;
302 1.1 cgd }
303 1.1 cgd return *cpp;
304 1.1 cgd }
305 1.1 cgd
306 1.1.1.2 tls static void
307 1.1 cgd delchild(cp)
308 1.1 cgd register struct child *cp;
309 1.1 cgd {
310 1.1 cgd register struct child **cpp;
311 1.1 cgd
312 1.1 cgd for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
313 1.1 cgd ;
314 1.1 cgd *cpp = cp->link;
315 1.1 cgd free((char *) cp);
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd void
319 1.1.1.2 tls sigchild(signo)
320 1.1.1.2 tls int signo;
321 1.1 cgd {
322 1.1 cgd int pid;
323 1.1 cgd union wait status;
324 1.1 cgd register struct child *cp;
325 1.1 cgd
326 1.1 cgd while ((pid =
327 1.1 cgd wait3((int *)&status, WNOHANG, (struct rusage *)0)) > 0) {
328 1.1 cgd cp = findchild(pid);
329 1.1 cgd if (cp->free)
330 1.1 cgd delchild(cp);
331 1.1 cgd else {
332 1.1 cgd cp->done = 1;
333 1.1 cgd cp->status = status;
334 1.1 cgd }
335 1.1 cgd }
336 1.1 cgd }
337 1.1 cgd
338 1.1 cgd union wait wait_status;
339 1.1 cgd
340 1.1 cgd /*
341 1.1 cgd * Wait for a specific child to die.
342 1.1 cgd */
343 1.1.1.2 tls int
344 1.1 cgd wait_child(pid)
345 1.1 cgd int pid;
346 1.1 cgd {
347 1.1 cgd int mask = sigblock(sigmask(SIGCHLD));
348 1.1 cgd register struct child *cp = findchild(pid);
349 1.1 cgd
350 1.1 cgd while (!cp->done)
351 1.1 cgd sigpause(mask);
352 1.1 cgd wait_status = cp->status;
353 1.1 cgd delchild(cp);
354 1.1 cgd sigsetmask(mask);
355 1.1 cgd return wait_status.w_status ? -1 : 0;
356 1.1 cgd }
357 1.1 cgd
358 1.1 cgd /*
359 1.1 cgd * Mark a child as don't care.
360 1.1 cgd */
361 1.1.1.2 tls void
362 1.1 cgd free_child(pid)
363 1.1 cgd int pid;
364 1.1 cgd {
365 1.1 cgd int mask = sigblock(sigmask(SIGCHLD));
366 1.1 cgd register struct child *cp = findchild(pid);
367 1.1 cgd
368 1.1 cgd if (cp->done)
369 1.1 cgd delchild(cp);
370 1.1 cgd else
371 1.1 cgd cp->free = 1;
372 1.1 cgd sigsetmask(mask);
373 1.1 cgd }
374