popen.c revision 1.20 1 1.20 christos /* $NetBSD: popen.c,v 1.20 2006/10/21 21:37:21 christos 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.20 christos __RCSID("$NetBSD: popen.c,v 1.20 2006/10/21 21:37:21 christos Exp $");
38 1.4 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.20 christos #include <errno.h>
42 1.16 christos #include <fcntl.h>
43 1.16 christos #include <stdarg.h>
44 1.20 christos #include <util.h>
45 1.20 christos #include <sys/wait.h>
46 1.20 christos
47 1.20 christos #include "rcv.h"
48 1.3 deraadt #include "extern.h"
49 1.1 cgd
50 1.1 cgd #define READ 0
51 1.1 cgd #define WRITE 1
52 1.1 cgd
53 1.1 cgd struct fp {
54 1.1 cgd FILE *fp;
55 1.1 cgd int pipe;
56 1.16 christos pid_t pid;
57 1.1 cgd struct fp *link;
58 1.1 cgd };
59 1.1 cgd static struct fp *fp_head;
60 1.1 cgd
61 1.3 deraadt struct child {
62 1.16 christos pid_t pid;
63 1.3 deraadt char done;
64 1.3 deraadt char free;
65 1.9 christos int status;
66 1.3 deraadt struct child *link;
67 1.3 deraadt };
68 1.16 christos static struct child *child, *child_freelist = NULL;
69 1.16 christos
70 1.16 christos static struct child *findchild(pid_t, int);
71 1.10 wiz static void delchild(struct child *);
72 1.16 christos static pid_t file_pid(FILE *);
73 1.18 christos static pid_t start_commandv(const char *, sigset_t *, int, int, va_list);
74 1.3 deraadt
75 1.1 cgd FILE *
76 1.18 christos Fopen(const char *fn, const char *mode)
77 1.1 cgd {
78 1.1 cgd FILE *fp;
79 1.1 cgd
80 1.11 wiz if ((fp = fopen(fn, mode)) != NULL) {
81 1.3 deraadt register_file(fp, 0, 0);
82 1.20 christos (void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
83 1.3 deraadt }
84 1.1 cgd return fp;
85 1.1 cgd }
86 1.1 cgd
87 1.1 cgd FILE *
88 1.18 christos Fdopen(int fd, const char *mode)
89 1.1 cgd {
90 1.1 cgd FILE *fp;
91 1.1 cgd
92 1.3 deraadt if ((fp = fdopen(fd, mode)) != NULL) {
93 1.3 deraadt register_file(fp, 0, 0);
94 1.20 christos (void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
95 1.3 deraadt }
96 1.1 cgd return fp;
97 1.1 cgd }
98 1.1 cgd
99 1.3 deraadt int
100 1.10 wiz Fclose(FILE *fp)
101 1.1 cgd {
102 1.16 christos
103 1.1 cgd unregister_file(fp);
104 1.1 cgd return fclose(fp);
105 1.1 cgd }
106 1.1 cgd
107 1.1 cgd FILE *
108 1.18 christos Popen(const char *cmd, const char *mode)
109 1.1 cgd {
110 1.1 cgd int p[2];
111 1.1 cgd int myside, hisside, fd0, fd1;
112 1.16 christos pid_t pid;
113 1.4 christos sigset_t nset;
114 1.1 cgd FILE *fp;
115 1.20 christos char *shellcmd;
116 1.1 cgd
117 1.1 cgd if (pipe(p) < 0)
118 1.1 cgd return NULL;
119 1.20 christos (void)fcntl(p[READ], F_SETFD, FD_CLOEXEC);
120 1.20 christos (void)fcntl(p[WRITE], F_SETFD, FD_CLOEXEC);
121 1.1 cgd if (*mode == 'r') {
122 1.1 cgd myside = p[READ];
123 1.16 christos hisside = fd0 = fd1 = p[WRITE];
124 1.1 cgd } else {
125 1.1 cgd myside = p[WRITE];
126 1.1 cgd hisside = fd0 = p[READ];
127 1.1 cgd fd1 = -1;
128 1.1 cgd }
129 1.19 christos (void)sigemptyset(&nset);
130 1.20 christos if ((shellcmd = value("SHELL")) == NULL)
131 1.20 christos shellcmd = __UNCONST(_PATH_CSHELL);
132 1.20 christos pid = start_command(shellcmd, &nset, fd0, fd1, "-c", cmd, NULL);
133 1.16 christos if (pid < 0) {
134 1.16 christos (void)close(p[READ]);
135 1.16 christos (void)close(p[WRITE]);
136 1.1 cgd return NULL;
137 1.1 cgd }
138 1.13 wiz (void)close(hisside);
139 1.1 cgd if ((fp = fdopen(myside, mode)) != NULL)
140 1.3 deraadt register_file(fp, 1, pid);
141 1.1 cgd return fp;
142 1.1 cgd }
143 1.1 cgd
144 1.3 deraadt int
145 1.10 wiz Pclose(FILE *ptr)
146 1.1 cgd {
147 1.1 cgd int i;
148 1.4 christos sigset_t nset, oset;
149 1.1 cgd
150 1.3 deraadt i = file_pid(ptr);
151 1.1 cgd unregister_file(ptr);
152 1.13 wiz (void)fclose(ptr);
153 1.19 christos (void)sigemptyset(&nset);
154 1.19 christos (void)sigaddset(&nset, SIGINT);
155 1.19 christos (void)sigaddset(&nset, SIGHUP);
156 1.19 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
157 1.3 deraadt i = wait_child(i);
158 1.19 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
159 1.1 cgd return i;
160 1.1 cgd }
161 1.1 cgd
162 1.3 deraadt void
163 1.10 wiz close_all_files(void)
164 1.1 cgd {
165 1.20 christos while (fp_head)
166 1.20 christos if (fp_head->pipe)
167 1.20 christos (void)Pclose(fp_head->fp);
168 1.20 christos else
169 1.20 christos (void)Fclose(fp_head->fp);
170 1.20 christos }
171 1.20 christos
172 1.20 christos #ifdef MIME_SUPPORT
173 1.20 christos #if 0 /* XXX - debugging stuff. This should go away eventually! */
174 1.20 christos static void
175 1.20 christos show_one_file(FILE *fo, struct fp *fpp)
176 1.20 christos {
177 1.20 christos (void)fprintf(fo, ">>> fp: %p, pipe: %d, pid: %d, link: %p\n",
178 1.20 christos fpp->fp, fpp->pipe, fpp->pid, fpp->link);
179 1.20 christos }
180 1.1 cgd
181 1.20 christos void show_all_files(FILE *fo);
182 1.20 christos __unused
183 1.20 christos void
184 1.20 christos show_all_files(FILE *fo)
185 1.20 christos {
186 1.20 christos struct fp *fpp;
187 1.20 christos
188 1.20 christos (void)fprintf(fo, ">> FILES\n");
189 1.20 christos for (fpp = fp_head; fpp; fpp = fpp->link)
190 1.20 christos show_one_file(fo, fpp);
191 1.20 christos (void)fprintf(fo, ">> -------\n");
192 1.20 christos (void)fflush(fo);
193 1.20 christos }
194 1.20 christos #endif
195 1.20 christos
196 1.20 christos FILE *
197 1.20 christos last_registered_file(int last_pipe)
198 1.20 christos {
199 1.20 christos struct fp *fpp;
200 1.20 christos
201 1.20 christos if (last_pipe == 0)
202 1.20 christos return fp_head ? fp_head->fp : NULL;
203 1.20 christos
204 1.20 christos for (fpp = fp_head; fpp; fpp = fpp->link)
205 1.20 christos if (fpp->pipe)
206 1.20 christos return fpp->fp;
207 1.20 christos return NULL;
208 1.20 christos }
209 1.20 christos
210 1.20 christos void
211 1.20 christos close_top_files(FILE *fp_stop)
212 1.20 christos {
213 1.20 christos while (fp_head && fp_head->fp != fp_stop)
214 1.1 cgd if (fp_head->pipe)
215 1.13 wiz (void)Pclose(fp_head->fp);
216 1.1 cgd else
217 1.13 wiz (void)Fclose(fp_head->fp);
218 1.1 cgd }
219 1.1 cgd
220 1.3 deraadt void
221 1.20 christos flush_files(FILE *fo, int only_pipes)
222 1.20 christos {
223 1.20 christos struct fp *fpp;
224 1.20 christos
225 1.20 christos if (fo)
226 1.20 christos (void)fflush(fo);
227 1.20 christos
228 1.20 christos for (fpp = fp_head; fpp; fpp = fpp->link)
229 1.20 christos if (!only_pipes || fpp->pipe)
230 1.20 christos (void)fflush(fpp->fp);
231 1.20 christos
232 1.20 christos (void)fflush(stdout);
233 1.20 christos }
234 1.20 christos #endif /* ENABLE_MIME */
235 1.20 christos
236 1.20 christos void
237 1.16 christos register_file(FILE *fp, int pipefd, pid_t pid)
238 1.1 cgd {
239 1.1 cgd struct fp *fpp;
240 1.1 cgd
241 1.20 christos fpp = (struct fp *)emalloc(sizeof(*fpp));
242 1.1 cgd fpp->fp = fp;
243 1.11 wiz fpp->pipe = pipefd;
244 1.3 deraadt fpp->pid = pid;
245 1.1 cgd fpp->link = fp_head;
246 1.1 cgd fp_head = fpp;
247 1.1 cgd }
248 1.1 cgd
249 1.3 deraadt void
250 1.10 wiz unregister_file(FILE *fp)
251 1.1 cgd {
252 1.1 cgd struct fp **pp, *p;
253 1.1 cgd
254 1.4 christos for (pp = &fp_head; (p = *pp) != NULL; pp = &p->link)
255 1.1 cgd if (p->fp == fp) {
256 1.1 cgd *pp = p->link;
257 1.16 christos (void)free(p);
258 1.1 cgd return;
259 1.1 cgd }
260 1.7 lukem errx(1, "Invalid file pointer");
261 1.3 deraadt }
262 1.3 deraadt
263 1.16 christos static pid_t
264 1.10 wiz file_pid(FILE *fp)
265 1.3 deraadt {
266 1.3 deraadt struct fp *p;
267 1.3 deraadt
268 1.3 deraadt for (p = fp_head; p; p = p->link)
269 1.3 deraadt if (p->fp == fp)
270 1.16 christos return p->pid;
271 1.7 lukem errx(1, "Invalid file pointer");
272 1.3 deraadt /*NOTREACHED*/
273 1.1 cgd }
274 1.1 cgd
275 1.1 cgd /*
276 1.1 cgd * Run a command without a shell, with optional arguments and splicing
277 1.16 christos * of stdin (-1 means none) and stdout. The command name can be a sequence
278 1.16 christos * of words.
279 1.1 cgd * Signals must be handled by the caller.
280 1.16 christos * "nset" contains the signals to ignore in the new process.
281 1.16 christos * SIGINT is enabled unless it's in "nset".
282 1.1 cgd */
283 1.16 christos static pid_t
284 1.18 christos start_commandv(const char *cmd, sigset_t *nset, int infd, int outfd,
285 1.18 christos va_list args)
286 1.1 cgd {
287 1.16 christos pid_t pid;
288 1.1 cgd
289 1.16 christos if ((pid = fork()) < 0) {
290 1.14 wiz warn("fork");
291 1.1 cgd return -1;
292 1.1 cgd }
293 1.1 cgd if (pid == 0) {
294 1.1 cgd char *argv[100];
295 1.16 christos int i = getrawlist(cmd, argv, sizeof(argv)/ sizeof(*argv));
296 1.1 cgd
297 1.19 christos while ((argv[i++] = va_arg(args, char *)) != NULL)
298 1.19 christos continue;
299 1.16 christos argv[i] = NULL;
300 1.16 christos prepare_child(nset, infd, outfd);
301 1.19 christos (void)execvp(argv[0], argv);
302 1.14 wiz warn("%s", argv[0]);
303 1.1 cgd _exit(1);
304 1.1 cgd }
305 1.1 cgd return pid;
306 1.1 cgd }
307 1.1 cgd
308 1.16 christos int
309 1.18 christos run_command(const char *cmd, sigset_t *nset, int infd, int outfd, ...)
310 1.16 christos {
311 1.16 christos pid_t pid;
312 1.16 christos va_list args;
313 1.16 christos
314 1.16 christos va_start(args, outfd);
315 1.16 christos pid = start_commandv(cmd, nset, infd, outfd, args);
316 1.16 christos va_end(args);
317 1.16 christos if (pid < 0)
318 1.16 christos return -1;
319 1.16 christos return wait_command(pid);
320 1.16 christos }
321 1.16 christos
322 1.16 christos int
323 1.18 christos start_command(const char *cmd, sigset_t *nset, int infd, int outfd, ...)
324 1.16 christos {
325 1.16 christos va_list args;
326 1.16 christos int r;
327 1.16 christos
328 1.16 christos va_start(args, outfd);
329 1.16 christos r = start_commandv(cmd, nset, infd, outfd, args);
330 1.16 christos va_end(args);
331 1.16 christos return r;
332 1.16 christos }
333 1.16 christos
334 1.3 deraadt void
335 1.10 wiz prepare_child(sigset_t *nset, int infd, int outfd)
336 1.1 cgd {
337 1.1 cgd int i;
338 1.8 mycroft sigset_t eset;
339 1.1 cgd
340 1.3 deraadt /*
341 1.3 deraadt * All file descriptors other than 0, 1, and 2 are supposed to be
342 1.3 deraadt * close-on-exec.
343 1.3 deraadt */
344 1.16 christos if (infd > 0) {
345 1.19 christos (void)dup2(infd, 0);
346 1.16 christos } else if (infd != 0) {
347 1.16 christos /* we don't want the child stealing my stdin input */
348 1.19 christos (void)close(0);
349 1.19 christos (void)open(_PATH_DEVNULL, O_RDONLY, 0);
350 1.16 christos }
351 1.16 christos if (outfd >= 0 && outfd != 1)
352 1.19 christos (void)dup2(outfd, 1);
353 1.16 christos if (nset == NULL)
354 1.16 christos return;
355 1.16 christos if (nset != NULL) {
356 1.16 christos for (i = 1; i < NSIG; i++)
357 1.16 christos if (sigismember(nset, i))
358 1.16 christos (void)signal(i, SIG_IGN);
359 1.16 christos }
360 1.5 christos if (nset == NULL || !sigismember(nset, SIGINT))
361 1.13 wiz (void)signal(SIGINT, SIG_DFL);
362 1.19 christos (void)sigemptyset(&eset);
363 1.13 wiz (void)sigprocmask(SIG_SETMASK, &eset, NULL);
364 1.1 cgd }
365 1.1 cgd
366 1.3 deraadt int
367 1.16 christos wait_command(pid_t pid)
368 1.1 cgd {
369 1.1 cgd
370 1.1 cgd if (wait_child(pid) < 0) {
371 1.19 christos (void)puts("Fatal error in process.");
372 1.1 cgd return -1;
373 1.1 cgd }
374 1.1 cgd return 0;
375 1.1 cgd }
376 1.1 cgd
377 1.3 deraadt static struct child *
378 1.16 christos findchild(pid_t pid, int dont_alloc)
379 1.1 cgd {
380 1.7 lukem struct child **cpp;
381 1.1 cgd
382 1.1 cgd for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
383 1.1 cgd cpp = &(*cpp)->link)
384 1.20 christos continue;
385 1.1 cgd if (*cpp == NULL) {
386 1.16 christos if (dont_alloc)
387 1.16 christos return NULL;
388 1.16 christos if (child_freelist) {
389 1.16 christos *cpp = child_freelist;
390 1.16 christos child_freelist = (*cpp)->link;
391 1.20 christos } else
392 1.20 christos *cpp = (struct child *)emalloc(sizeof(struct child));
393 1.20 christos
394 1.1 cgd (*cpp)->pid = pid;
395 1.1 cgd (*cpp)->done = (*cpp)->free = 0;
396 1.1 cgd (*cpp)->link = NULL;
397 1.1 cgd }
398 1.1 cgd return *cpp;
399 1.1 cgd }
400 1.1 cgd
401 1.3 deraadt static void
402 1.10 wiz delchild(struct child *cp)
403 1.1 cgd {
404 1.7 lukem struct child **cpp;
405 1.1 cgd
406 1.1 cgd for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
407 1.20 christos continue;
408 1.1 cgd *cpp = cp->link;
409 1.16 christos cp->link = child_freelist;
410 1.16 christos child_freelist = cp;
411 1.1 cgd }
412 1.1 cgd
413 1.1 cgd void
414 1.19 christos /*ARGSUSED*/
415 1.20 christos sigchild(int signo __unused)
416 1.1 cgd {
417 1.16 christos pid_t pid;
418 1.9 christos int status;
419 1.7 lukem struct child *cp;
420 1.16 christos int save_errno = errno;
421 1.1 cgd
422 1.16 christos while ((pid =
423 1.16 christos waitpid((pid_t)-1, &status, WNOHANG)) > 0) {
424 1.16 christos cp = findchild(pid, 1);
425 1.16 christos if (!cp)
426 1.16 christos continue;
427 1.1 cgd if (cp->free)
428 1.1 cgd delchild(cp);
429 1.1 cgd else {
430 1.1 cgd cp->done = 1;
431 1.1 cgd cp->status = status;
432 1.1 cgd }
433 1.1 cgd }
434 1.16 christos errno = save_errno;
435 1.1 cgd }
436 1.1 cgd
437 1.9 christos int wait_status;
438 1.1 cgd
439 1.1 cgd /*
440 1.1 cgd * Wait for a specific child to die.
441 1.1 cgd */
442 1.3 deraadt int
443 1.16 christos wait_child(pid_t pid)
444 1.1 cgd {
445 1.16 christos struct child *cp;
446 1.4 christos sigset_t nset, oset;
447 1.16 christos pid_t rv = 0;
448 1.16 christos
449 1.19 christos (void)sigemptyset(&nset);
450 1.19 christos (void)sigaddset(&nset, SIGCHLD);
451 1.19 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
452 1.16 christos /*
453 1.16 christos * If we have not already waited on the pid (via sigchild)
454 1.16 christos * wait on it now. Otherwise, use the wait status stashed
455 1.16 christos * by sigchild.
456 1.16 christos */
457 1.16 christos cp = findchild(pid, 1);
458 1.16 christos if (cp == NULL || !cp->done)
459 1.16 christos rv = waitpid(pid, &wait_status, 0);
460 1.16 christos else
461 1.16 christos wait_status = cp->status;
462 1.16 christos if (cp != NULL)
463 1.16 christos delchild(cp);
464 1.19 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
465 1.16 christos if (rv == -1 || (WIFEXITED(wait_status) && WEXITSTATUS(wait_status)))
466 1.16 christos return -1;
467 1.16 christos else
468 1.16 christos return 0;
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd /*
472 1.1 cgd * Mark a child as don't care.
473 1.1 cgd */
474 1.3 deraadt void
475 1.16 christos free_child(pid_t pid)
476 1.1 cgd {
477 1.16 christos struct child *cp;
478 1.4 christos sigset_t nset, oset;
479 1.16 christos
480 1.19 christos (void)sigemptyset(&nset);
481 1.19 christos (void)sigaddset(&nset, SIGCHLD);
482 1.19 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
483 1.16 christos if ((cp = findchild(pid, 0)) != NULL) {
484 1.16 christos if (cp->done)
485 1.16 christos delchild(cp);
486 1.16 christos else
487 1.16 christos cp->free = 1;
488 1.16 christos }
489 1.19 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
490 1.1 cgd }
491