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