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