[BACK]Return to popen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mail

Annotation of src/usr.bin/mail/popen.c, Revision 1.6

1.6     ! millert     1: /*     $OpenBSD: popen.c,v 1.5 1997/05/30 08:51:43 deraadt Exp $       */
        !             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.6     ! millert    41: static char rcsid[] = "$OpenBSD: popen.c,v 1.5 1997/05/30 08:51:43 deraadt 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);
                     82:                (void) fcntl(fileno(fp), F_SETFD, 1);
                     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);
                     96:                (void) fcntl(fileno(fp), F_SETFD, 1);
                     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.1       deraadt   122:        (void) fcntl(p[READ], F_SETFD, 1);
                    123:        (void) fcntl(p[WRITE], F_SETFD, 1);
                    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);
                    134:        if ((pid = start_command(cmd, &nset, fd0, fd1, NOSTR, NOSTR, NOSTR)) < 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.6     ! 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;
                    200:                        free((char *) p);
                    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
                    228: run_command(cmd, mask, infd, outfd, a0, a1, a2)
                    229:        char *cmd;
1.2       deraadt   230:        sigset_t *mask;
                    231:        int infd, outfd;
1.1       deraadt   232:        char *a0, *a1, *a2;
                    233: {
                    234:        int pid;
                    235:
                    236:        if ((pid = start_command(cmd, mask, 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
                    243: start_command(cmd, mask, infd, outfd, a0, a1, a2)
                    244:        char *cmd;
1.2       deraadt   245:        sigset_t *mask;
                    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:
                    259:                if ((argv[i++] = a0) != NOSTR &&
                    260:                    (argv[i++] = a1) != NOSTR &&
                    261:                    (argv[i++] = a2) != NOSTR)
                    262:                        argv[i] = NOSTR;
                    263:                prepare_child(mask, infd, outfd);
                    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.2       deraadt   277:        sigset_t fset;
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))
                    292:                                (void) signal(i, SIG_IGN);
                    293:        }
1.6     ! millert   294:        if (nset == NULL || !sigismember(nset, SIGINT))
        !           295:                (void) signal(SIGINT, SIG_DFL);
1.2       deraadt   296:        sigfillset(&fset);
                    297:        (void) sigprocmask(SIG_UNBLOCK, &fset, 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.6     ! 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;
                    339:        free((char *) cp);
                    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.2       deraadt   371:        sigset_t nset, oset;
1.1       deraadt   372:        register struct child *cp = findchild(pid);
1.2       deraadt   373:        sigemptyset(&nset);
                    374:        sigaddset(&nset, SIGCHLD);
                    375:        sigprocmask(SIG_BLOCK, &nset, &oset);
1.1       deraadt   376:
                    377:        while (!cp->done)
1.2       deraadt   378:                sigsuspend(&oset);
1.1       deraadt   379:        wait_status = cp->status;
                    380:        delchild(cp);
1.2       deraadt   381:        sigprocmask(SIG_SETMASK, &oset, NULL);
1.6     ! millert   382:        return(wait_status.w_status ? -1 : 0);
1.1       deraadt   383: }
                    384:
                    385: /*
                    386:  * Mark a child as don't care.
                    387:  */
                    388: void
                    389: free_child(pid)
                    390:        int pid;
                    391: {
1.2       deraadt   392:        sigset_t nset, oset;
1.1       deraadt   393:        register struct child *cp = findchild(pid);
1.2       deraadt   394:        sigemptyset(&nset);
                    395:        sigaddset(&nset, SIGCHLD);
                    396:        sigprocmask(SIG_BLOCK, &nset, &oset);
1.1       deraadt   397:
                    398:        if (cp->done)
                    399:                delchild(cp);
                    400:        else
                    401:                cp->free = 1;
1.2       deraadt   402:        sigprocmask(SIG_SETMASK, &oset, NULL);
1.4       millert   403: }
                    404:
                    405: /*
                    406:  * Lock(1)/unlock(0) mail spool using mail.local's -H flag.
                    407:  * Returns 1 for success, 0 for failure, -1 for bad usage.
                    408:  */
                    409: static int
                    410: handle_spool_locks(action)
                    411:        int action;
                    412: {
                    413:        char *cmd;
                    414:        static FILE *lockfp = NULL;
                    415:        static int lock_pid;
                    416:
                    417:        if (action == 0) {
                    418:                /* Clear the lock */
                    419:                if (lockfp == NULL) {
1.6     ! millert   420:                        fputs("handle_spool_locks: no spool lock to remove.\n",
        !           421:                            stderr);
        !           422:                        return(-1);
1.4       millert   423:                }
                    424:                (void)kill(lock_pid, SIGTERM);
1.6     ! millert   425:                (void)Pclose(lockfp);
1.4       millert   426:                lockfp = NULL;
                    427:        } else if (action == 1) {
                    428:                /* Create the lock */
                    429:                if ((cmd = (char *) malloc(sizeof(_PATH_MAIL_LOCAL) + 3)) == NULL)
                    430:                        panic("Out of memory");
                    431:                sprintf(cmd, "%s -H", _PATH_MAIL_LOCAL);
                    432:                if ((lockfp = Popen(cmd, "r")) == NULL || getc(lockfp) != '1') {
                    433:                        lockfp = NULL;
                    434:                        free(cmd);
1.6     ! millert   435:                        return(0);
1.4       millert   436:                }
                    437:
                    438:                lock_pid = fp_head->pid;        /* new entries added at head */
                    439:                free(cmd);
                    440:        } else {
                    441:                fprintf(stderr, "handle_spool_locks: unknown action %d\n",
                    442:                    action);
1.6     ! millert   443:                return(-1);
1.4       millert   444:        }
                    445:
1.6     ! millert   446:        return(1);
1.4       millert   447: }
                    448:
                    449: int
                    450: spool_lock()
                    451: {
                    452:        return(handle_spool_locks(1));
                    453: }
                    454:
                    455: int
                    456: spool_unlock()
                    457: {
                    458:        return(handle_spool_locks(0));
1.1       deraadt   459: }