Annotation of src/usr.bin/mail/popen.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: popen.c,v 1.9 1997/07/14 15:56:24 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
39: static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/6/93";
40: #else
1.10 ! millert 41: static char rcsid[] = "$OpenBSD: popen.c,v 1.9 1997/07/14 15:56:24 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>
48: #include "extern.h"
49:
50: #define READ 0
51: #define WRITE 1
52:
53: struct fp {
54: FILE *fp;
55: int pipe;
56: int pid;
57: struct fp *link;
58: };
59: static struct fp *fp_head;
60:
61: struct child {
62: int pid;
63: char done;
64: char free;
65: union wait status;
66: struct child *link;
67: };
68: static struct child *child;
69: static struct child *findchild __P((int));
70: static void delchild __P((struct child *));
1.2 deraadt 71: static int file_pid __P((FILE *));
1.4 millert 72: static int handle_spool_locks __P((int));
1.1 deraadt 73:
74: FILE *
75: Fopen(file, mode)
76: char *file, *mode;
77: {
78: FILE *fp;
79:
80: if ((fp = fopen(file, mode)) != NULL) {
81: register_file(fp, 0, 0);
1.7 millert 82: (void)fcntl(fileno(fp), F_SETFD, 1);
1.1 deraadt 83: }
1.6 millert 84: return(fp);
1.1 deraadt 85: }
86:
87: FILE *
88: Fdopen(fd, mode)
89: int fd;
90: char *mode;
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
102: Fclose(fp)
103: FILE *fp;
104: {
105: unregister_file(fp);
1.6 millert 106: return(fclose(fp));
1.1 deraadt 107: }
108:
109: FILE *
110: Popen(cmd, mode)
111: char *cmd;
112: char *mode;
113: {
114: int p[2];
115: int myside, hisside, fd0, fd1;
116: int pid;
1.2 deraadt 117: sigset_t nset;
1.1 deraadt 118: FILE *fp;
119:
120: if (pipe(p) < 0)
1.6 millert 121: return(NULL);
1.7 millert 122: (void)fcntl(p[READ], F_SETFD, 1);
123: (void)fcntl(p[WRITE], F_SETFD, 1);
1.1 deraadt 124: if (*mode == 'r') {
125: myside = p[READ];
126: fd0 = -1;
127: hisside = fd1 = p[WRITE];
128: } else {
129: myside = p[WRITE];
130: hisside = fd0 = p[READ];
131: fd1 = -1;
132: }
1.2 deraadt 133: sigemptyset(&nset);
1.8 millert 134: if ((pid = start_command(cmd, &nset, fd0, fd1, NULL, NULL, NULL)) < 0) {
1.6 millert 135: (void)close(p[READ]);
136: (void)close(p[WRITE]);
137: return(NULL);
1.1 deraadt 138: }
1.6 millert 139: (void)close(hisside);
1.1 deraadt 140: if ((fp = fdopen(myside, mode)) != NULL)
141: register_file(fp, 1, pid);
1.6 millert 142: return(fp);
1.1 deraadt 143: }
144:
145: int
146: Pclose(ptr)
147: FILE *ptr;
148: {
149: int i;
1.2 deraadt 150: sigset_t nset, oset;
1.1 deraadt 151:
152: i = file_pid(ptr);
153: unregister_file(ptr);
1.6 millert 154: (void)fclose(ptr);
1.2 deraadt 155: sigemptyset(&nset);
156: sigaddset(&nset, SIGINT);
157: sigaddset(&nset, SIGHUP);
158: sigprocmask(SIG_BLOCK, &nset, &oset);
1.1 deraadt 159: i = wait_child(i);
1.2 deraadt 160: sigprocmask(SIG_SETMASK, &oset, NULL);
1.6 millert 161: return(i);
1.1 deraadt 162: }
163:
164: void
165: close_all_files()
166: {
167:
168: while (fp_head)
169: if (fp_head->pipe)
1.6 millert 170: (void)Pclose(fp_head->fp);
1.1 deraadt 171: else
1.6 millert 172: (void)Fclose(fp_head->fp);
1.1 deraadt 173: }
174:
175: void
176: register_file(fp, pipe, pid)
177: FILE *fp;
178: int pipe, pid;
179: {
180: struct fp *fpp;
181:
1.7 millert 182: if ((fpp = (struct fp *)malloc(sizeof(*fpp))) == NULL)
1.1 deraadt 183: panic("Out of memory");
184: fpp->fp = fp;
185: fpp->pipe = pipe;
186: fpp->pid = pid;
187: fpp->link = fp_head;
188: fp_head = fpp;
189: }
190:
191: void
192: unregister_file(fp)
193: FILE *fp;
194: {
195: struct fp **pp, *p;
196:
1.2 deraadt 197: for (pp = &fp_head; (p = *pp) != NULL; pp = &p->link)
1.1 deraadt 198: if (p->fp == fp) {
199: *pp = p->link;
1.7 millert 200: (void)free(p);
1.1 deraadt 201: return;
202: }
203: panic("Invalid file pointer");
204: }
205:
1.2 deraadt 206: static int
1.1 deraadt 207: file_pid(fp)
208: FILE *fp;
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.1 deraadt 215: panic("Invalid file pointer");
216: /*NOTREACHED*/
217: }
218:
219: /*
220: * Run a command without a shell, with optional arguments and splicing
221: * of stdin and stdout. The command name can be a sequence of words.
222: * Signals must be handled by the caller.
223: * "Mask" contains the signals to ignore in the new process.
224: * SIGINT is enabled unless it's in the mask.
225: */
226: /*VARARGS4*/
227: int
1.10 ! millert 228: run_command(cmd, nset, infd, outfd, a0, a1, a2)
1.1 deraadt 229: char *cmd;
1.10 ! millert 230: sigset_t *nset;
1.2 deraadt 231: int infd, outfd;
1.1 deraadt 232: char *a0, *a1, *a2;
233: {
234: int pid;
235:
1.10 ! millert 236: if ((pid = start_command(cmd, nset, infd, outfd, a0, a1, a2)) < 0)
1.6 millert 237: return(-1);
238: return(wait_command(pid));
1.1 deraadt 239: }
240:
241: /*VARARGS4*/
242: int
1.10 ! millert 243: start_command(cmd, nset, infd, outfd, a0, a1, a2)
1.1 deraadt 244: char *cmd;
1.10 ! millert 245: sigset_t *nset;
1.2 deraadt 246: int infd, outfd;
1.1 deraadt 247: char *a0, *a1, *a2;
248: {
249: int pid;
250:
251: if ((pid = vfork()) < 0) {
1.6 millert 252: warn("fork");
253: return(-1);
1.1 deraadt 254: }
255: if (pid == 0) {
256: char *argv[100];
1.6 millert 257: int i = getrawlist(cmd, argv, sizeof(argv)/ sizeof(*argv));
1.1 deraadt 258:
1.8 millert 259: if ((argv[i++] = a0) != NULL &&
260: (argv[i++] = a1) != NULL &&
261: (argv[i++] = a2) != NULL)
262: argv[i] = NULL;
1.10 ! millert 263: prepare_child(nset, infd, outfd);
1.1 deraadt 264: execvp(argv[0], argv);
1.6 millert 265: warn(argv[0]);
1.1 deraadt 266: _exit(1);
267: }
1.6 millert 268: return(pid);
1.1 deraadt 269: }
270:
271: void
1.2 deraadt 272: prepare_child(nset, infd, outfd)
273: sigset_t *nset;
274: int infd, outfd;
1.1 deraadt 275: {
276: int i;
1.9 millert 277: sigset_t eset;
1.1 deraadt 278:
279: /*
280: * All file descriptors other than 0, 1, and 2 are supposed to be
281: * close-on-exec.
282: */
283: if (infd >= 0)
284: dup2(infd, 0);
285: if (outfd >= 0)
286: dup2(outfd, 1);
1.6 millert 287: if (nset == NULL)
288: return;
289: if (nset != NULL) {
1.5 deraadt 290: for (i = 1; i < NSIG; i++)
1.3 dm 291: if (sigismember(nset, i))
1.7 millert 292: (void)signal(i, SIG_IGN);
1.3 dm 293: }
1.6 millert 294: if (nset == NULL || !sigismember(nset, SIGINT))
1.7 millert 295: (void)signal(SIGINT, SIG_DFL);
1.9 millert 296: sigemptyset(&eset);
297: (void)sigprocmask(SIG_SETMASK, &eset, NULL);
1.1 deraadt 298: }
299:
300: int
301: wait_command(pid)
302: int pid;
303: {
304:
305: if (wait_child(pid) < 0) {
1.6 millert 306: puts("Fatal error in process.");
307: return(-1);
1.1 deraadt 308: }
1.6 millert 309: return(0);
1.1 deraadt 310: }
311:
312: static struct child *
313: findchild(pid)
314: int pid;
315: {
316: register struct child **cpp;
317:
318: for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
319: cpp = &(*cpp)->link)
320: ;
321: if (*cpp == NULL) {
1.7 millert 322: *cpp = (struct child *)malloc(sizeof(struct child));
1.1 deraadt 323: (*cpp)->pid = pid;
324: (*cpp)->done = (*cpp)->free = 0;
325: (*cpp)->link = NULL;
326: }
1.6 millert 327: return(*cpp);
1.1 deraadt 328: }
329:
330: static void
331: delchild(cp)
332: register struct child *cp;
333: {
334: register struct child **cpp;
335:
336: for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
337: ;
338: *cpp = cp->link;
1.7 millert 339: (void)free(cp);
1.1 deraadt 340: }
341:
342: void
343: sigchild(signo)
344: int signo;
345: {
346: int pid;
347: union wait status;
348: register struct child *cp;
349:
350: while ((pid =
351: wait3((int *)&status, WNOHANG, (struct rusage *)0)) > 0) {
352: cp = findchild(pid);
353: if (cp->free)
354: delchild(cp);
355: else {
356: cp->done = 1;
357: cp->status = status;
358: }
359: }
360: }
361:
362: union wait wait_status;
363:
364: /*
365: * Wait for a specific child to die.
366: */
367: int
368: wait_child(pid)
369: int pid;
370: {
1.10 ! millert 371: register struct child *cp = findchild(pid);
1.2 deraadt 372: sigset_t nset, oset;
1.9 millert 373:
1.2 deraadt 374: sigemptyset(&nset);
375: sigaddset(&nset, SIGCHLD);
376: sigprocmask(SIG_BLOCK, &nset, &oset);
1.1 deraadt 377:
378: while (!cp->done)
1.2 deraadt 379: sigsuspend(&oset);
1.1 deraadt 380: wait_status = cp->status;
381: delchild(cp);
1.2 deraadt 382: sigprocmask(SIG_SETMASK, &oset, NULL);
1.6 millert 383: return(wait_status.w_status ? -1 : 0);
1.1 deraadt 384: }
385:
386: /*
387: * Mark a child as don't care.
388: */
389: void
390: free_child(pid)
391: int pid;
392: {
1.10 ! millert 393: register struct child *cp = findchild(pid);
1.2 deraadt 394: sigset_t nset, oset;
1.9 millert 395:
1.2 deraadt 396: sigemptyset(&nset);
397: sigaddset(&nset, SIGCHLD);
398: sigprocmask(SIG_BLOCK, &nset, &oset);
1.1 deraadt 399:
400: if (cp->done)
401: delchild(cp);
402: else
403: cp->free = 1;
1.2 deraadt 404: sigprocmask(SIG_SETMASK, &oset, NULL);
1.4 millert 405: }
406:
407: /*
408: * Lock(1)/unlock(0) mail spool using mail.local's -H flag.
409: * Returns 1 for success, 0 for failure, -1 for bad usage.
410: */
411: static int
412: handle_spool_locks(action)
413: int action;
414: {
415: char *cmd;
416: static FILE *lockfp = NULL;
417: static int lock_pid;
418:
419: if (action == 0) {
420: /* Clear the lock */
421: if (lockfp == NULL) {
1.6 millert 422: fputs("handle_spool_locks: no spool lock to remove.\n",
423: stderr);
424: return(-1);
1.4 millert 425: }
426: (void)kill(lock_pid, SIGTERM);
1.6 millert 427: (void)Pclose(lockfp);
1.4 millert 428: lockfp = NULL;
429: } else if (action == 1) {
430: /* Create the lock */
1.7 millert 431: if ((cmd = (char *)malloc(sizeof(_PATH_MAIL_LOCAL) + 3)) == NULL)
1.4 millert 432: panic("Out of memory");
433: sprintf(cmd, "%s -H", _PATH_MAIL_LOCAL);
434: if ((lockfp = Popen(cmd, "r")) == NULL || getc(lockfp) != '1') {
435: lockfp = NULL;
1.7 millert 436: (void)free(cmd);
1.6 millert 437: return(0);
1.4 millert 438: }
439:
440: lock_pid = fp_head->pid; /* new entries added at head */
1.7 millert 441: (void)free(cmd);
1.4 millert 442: } else {
443: fprintf(stderr, "handle_spool_locks: unknown action %d\n",
444: action);
1.6 millert 445: return(-1);
1.4 millert 446: }
447:
1.6 millert 448: return(1);
1.4 millert 449: }
450:
451: int
452: spool_lock()
453: {
454: return(handle_spool_locks(1));
455: }
456:
457: int
458: spool_unlock()
459: {
460: return(handle_spool_locks(0));
1.1 deraadt 461: }