popen.c revision 1.19 1 1.19 christos /* $NetBSD: popen.c,v 1.19 2005/07/19 23:07:10 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.19 christos __RCSID("$NetBSD: popen.c,v 1.19 2005/07/19 23:07:10 christos 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.18 christos static pid_t start_commandv(const char *, sigset_t *, int, int, va_list);
72 1.3 deraadt
73 1.1 cgd FILE *
74 1.18 christos Fopen(const char *fn, const 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.18 christos Fdopen(int fd, const 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.18 christos Popen(const char *cmd, const 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.19 christos (void)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.19 christos (void)sigemptyset(&nset);
149 1.19 christos (void)sigaddset(&nset, SIGINT);
150 1.19 christos (void)sigaddset(&nset, SIGHUP);
151 1.19 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
152 1.3 deraadt i = wait_child(i);
153 1.19 christos (void)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.18 christos start_commandv(const char *cmd, sigset_t *nset, int infd, int outfd,
218 1.18 christos va_list args)
219 1.1 cgd {
220 1.16 christos pid_t pid;
221 1.1 cgd
222 1.16 christos if ((pid = fork()) < 0) {
223 1.14 wiz warn("fork");
224 1.1 cgd return -1;
225 1.1 cgd }
226 1.1 cgd if (pid == 0) {
227 1.1 cgd char *argv[100];
228 1.16 christos int i = getrawlist(cmd, argv, sizeof(argv)/ sizeof(*argv));
229 1.1 cgd
230 1.19 christos while ((argv[i++] = va_arg(args, char *)) != NULL)
231 1.19 christos continue;
232 1.16 christos argv[i] = NULL;
233 1.16 christos prepare_child(nset, infd, outfd);
234 1.19 christos (void)execvp(argv[0], argv);
235 1.14 wiz warn("%s", argv[0]);
236 1.1 cgd _exit(1);
237 1.1 cgd }
238 1.1 cgd return pid;
239 1.1 cgd }
240 1.1 cgd
241 1.16 christos int
242 1.18 christos run_command(const char *cmd, sigset_t *nset, int infd, int outfd, ...)
243 1.16 christos {
244 1.16 christos pid_t pid;
245 1.16 christos va_list args;
246 1.16 christos
247 1.16 christos va_start(args, outfd);
248 1.16 christos pid = start_commandv(cmd, nset, infd, outfd, args);
249 1.16 christos va_end(args);
250 1.16 christos if (pid < 0)
251 1.16 christos return -1;
252 1.16 christos return wait_command(pid);
253 1.16 christos }
254 1.16 christos
255 1.16 christos int
256 1.18 christos start_command(const char *cmd, sigset_t *nset, int infd, int outfd, ...)
257 1.16 christos {
258 1.16 christos va_list args;
259 1.16 christos int r;
260 1.16 christos
261 1.16 christos va_start(args, outfd);
262 1.16 christos r = start_commandv(cmd, nset, infd, outfd, args);
263 1.16 christos va_end(args);
264 1.16 christos return r;
265 1.16 christos }
266 1.16 christos
267 1.3 deraadt void
268 1.10 wiz prepare_child(sigset_t *nset, int infd, int outfd)
269 1.1 cgd {
270 1.1 cgd int i;
271 1.8 mycroft sigset_t eset;
272 1.1 cgd
273 1.3 deraadt /*
274 1.3 deraadt * All file descriptors other than 0, 1, and 2 are supposed to be
275 1.3 deraadt * close-on-exec.
276 1.3 deraadt */
277 1.16 christos if (infd > 0) {
278 1.19 christos (void)dup2(infd, 0);
279 1.16 christos } else if (infd != 0) {
280 1.16 christos /* we don't want the child stealing my stdin input */
281 1.19 christos (void)close(0);
282 1.19 christos (void)open(_PATH_DEVNULL, O_RDONLY, 0);
283 1.16 christos }
284 1.16 christos if (outfd >= 0 && outfd != 1)
285 1.19 christos (void)dup2(outfd, 1);
286 1.16 christos if (nset == NULL)
287 1.16 christos return;
288 1.16 christos if (nset != NULL) {
289 1.16 christos for (i = 1; i < NSIG; i++)
290 1.16 christos if (sigismember(nset, i))
291 1.16 christos (void)signal(i, SIG_IGN);
292 1.16 christos }
293 1.5 christos if (nset == NULL || !sigismember(nset, SIGINT))
294 1.13 wiz (void)signal(SIGINT, SIG_DFL);
295 1.19 christos (void)sigemptyset(&eset);
296 1.13 wiz (void)sigprocmask(SIG_SETMASK, &eset, NULL);
297 1.1 cgd }
298 1.1 cgd
299 1.3 deraadt int
300 1.16 christos wait_command(pid_t pid)
301 1.1 cgd {
302 1.1 cgd
303 1.1 cgd if (wait_child(pid) < 0) {
304 1.19 christos (void)puts("Fatal error in process.");
305 1.1 cgd return -1;
306 1.1 cgd }
307 1.1 cgd return 0;
308 1.1 cgd }
309 1.1 cgd
310 1.3 deraadt static struct child *
311 1.16 christos findchild(pid_t pid, int dont_alloc)
312 1.1 cgd {
313 1.7 lukem struct child **cpp;
314 1.1 cgd
315 1.1 cgd for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
316 1.1 cgd cpp = &(*cpp)->link)
317 1.1 cgd ;
318 1.1 cgd if (*cpp == NULL) {
319 1.16 christos if (dont_alloc)
320 1.16 christos return NULL;
321 1.16 christos if (child_freelist) {
322 1.16 christos *cpp = child_freelist;
323 1.16 christos child_freelist = (*cpp)->link;
324 1.16 christos } else {
325 1.16 christos *cpp = (struct child *)malloc(sizeof(struct child));
326 1.16 christos if (*cpp == NULL)
327 1.16 christos errx(1, "Out of memory");
328 1.16 christos }
329 1.1 cgd (*cpp)->pid = pid;
330 1.1 cgd (*cpp)->done = (*cpp)->free = 0;
331 1.1 cgd (*cpp)->link = NULL;
332 1.1 cgd }
333 1.1 cgd return *cpp;
334 1.1 cgd }
335 1.1 cgd
336 1.3 deraadt static void
337 1.10 wiz delchild(struct child *cp)
338 1.1 cgd {
339 1.7 lukem struct child **cpp;
340 1.1 cgd
341 1.1 cgd for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
342 1.1 cgd ;
343 1.1 cgd *cpp = cp->link;
344 1.16 christos cp->link = child_freelist;
345 1.16 christos child_freelist = cp;
346 1.1 cgd }
347 1.1 cgd
348 1.1 cgd void
349 1.19 christos /*ARGSUSED*/
350 1.10 wiz sigchild(int signo)
351 1.1 cgd {
352 1.16 christos pid_t pid;
353 1.9 christos int status;
354 1.7 lukem struct child *cp;
355 1.16 christos int save_errno = errno;
356 1.1 cgd
357 1.16 christos while ((pid =
358 1.16 christos waitpid((pid_t)-1, &status, WNOHANG)) > 0) {
359 1.16 christos cp = findchild(pid, 1);
360 1.16 christos if (!cp)
361 1.16 christos continue;
362 1.1 cgd if (cp->free)
363 1.1 cgd delchild(cp);
364 1.1 cgd else {
365 1.1 cgd cp->done = 1;
366 1.1 cgd cp->status = status;
367 1.1 cgd }
368 1.1 cgd }
369 1.16 christos errno = save_errno;
370 1.1 cgd }
371 1.1 cgd
372 1.9 christos int wait_status;
373 1.1 cgd
374 1.1 cgd /*
375 1.1 cgd * Wait for a specific child to die.
376 1.1 cgd */
377 1.3 deraadt int
378 1.16 christos wait_child(pid_t pid)
379 1.1 cgd {
380 1.16 christos struct child *cp;
381 1.4 christos sigset_t nset, oset;
382 1.16 christos pid_t rv = 0;
383 1.16 christos
384 1.19 christos (void)sigemptyset(&nset);
385 1.19 christos (void)sigaddset(&nset, SIGCHLD);
386 1.19 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
387 1.16 christos /*
388 1.16 christos * If we have not already waited on the pid (via sigchild)
389 1.16 christos * wait on it now. Otherwise, use the wait status stashed
390 1.16 christos * by sigchild.
391 1.16 christos */
392 1.16 christos cp = findchild(pid, 1);
393 1.16 christos if (cp == NULL || !cp->done)
394 1.16 christos rv = waitpid(pid, &wait_status, 0);
395 1.16 christos else
396 1.16 christos wait_status = cp->status;
397 1.16 christos if (cp != NULL)
398 1.16 christos delchild(cp);
399 1.19 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
400 1.16 christos if (rv == -1 || (WIFEXITED(wait_status) && WEXITSTATUS(wait_status)))
401 1.16 christos return -1;
402 1.16 christos else
403 1.16 christos return 0;
404 1.1 cgd }
405 1.1 cgd
406 1.1 cgd /*
407 1.1 cgd * Mark a child as don't care.
408 1.1 cgd */
409 1.3 deraadt void
410 1.16 christos free_child(pid_t pid)
411 1.1 cgd {
412 1.16 christos struct child *cp;
413 1.4 christos sigset_t nset, oset;
414 1.16 christos
415 1.19 christos (void)sigemptyset(&nset);
416 1.19 christos (void)sigaddset(&nset, SIGCHLD);
417 1.19 christos (void)sigprocmask(SIG_BLOCK, &nset, &oset);
418 1.16 christos if ((cp = findchild(pid, 0)) != NULL) {
419 1.16 christos if (cp->done)
420 1.16 christos delchild(cp);
421 1.16 christos else
422 1.16 christos cp->free = 1;
423 1.16 christos }
424 1.19 christos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
425 1.1 cgd }
426