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