[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.5

1.5     ! deraadt     1: /*     $OpenBSD: popen.c,v 1.4 1997/03/29 03:01:47 millert Exp $       */
1.2       deraadt     2: /*     $NetBSD: popen.c,v 1.4 1996/06/08 19:48:35 christos Exp $       */
                      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.5     ! deraadt    41: static char rcsid[] = "$OpenBSD: popen.c,v 1.4 1997/03/29 03:01:47 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);
                     82:                (void) fcntl(fileno(fp), F_SETFD, 1);
                     83:        }
                     84:        return fp;
                     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:        }
                     98:        return fp;
                     99: }
                    100:
                    101: int
                    102: Fclose(fp)
                    103:        FILE *fp;
                    104: {
                    105:        unregister_file(fp);
                    106:        return fclose(fp);
                    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)
                    121:                return NULL;
                    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.1       deraadt   135:                close(p[READ]);
                    136:                close(p[WRITE]);
                    137:                return NULL;
                    138:        }
                    139:        (void) close(hisside);
                    140:        if ((fp = fdopen(myside, mode)) != NULL)
                    141:                register_file(fp, 1, pid);
                    142:        return fp;
                    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);
                    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.1       deraadt   161:        return i;
                    162: }
                    163:
                    164: void
                    165: close_all_files()
                    166: {
                    167:
                    168:        while (fp_head)
                    169:                if (fp_head->pipe)
                    170:                        (void) Pclose(fp_head->fp);
                    171:                else
                    172:                        (void) Fclose(fp_head->fp);
                    173: }
                    174:
                    175: void
                    176: register_file(fp, pipe, pid)
                    177:        FILE *fp;
                    178:        int pipe, pid;
                    179: {
                    180:        struct fp *fpp;
                    181:
                    182:        if ((fpp = (struct fp *) malloc(sizeof *fpp)) == NULL)
                    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)
                    214:                        return (p->pid);
                    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)
                    237:                return -1;
                    238:        return wait_command(pid);
                    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) {
                    252:                perror("fork");
                    253:                return -1;
                    254:        }
                    255:        if (pid == 0) {
                    256:                char *argv[100];
                    257:                int i = getrawlist(cmd, argv, sizeof argv / sizeof *argv);
                    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);
                    265:                perror(argv[0]);
                    266:                _exit(1);
                    267:        }
                    268:        return pid;
                    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.3       dm        287:        if (nset) {
1.5     ! deraadt   288:                for (i = 1; i < NSIG; i++)
1.3       dm        289:                        if (sigismember(nset, i))
                    290:                                (void) signal(i, SIG_IGN);
                    291:                if (!sigismember(nset, SIGINT))
                    292:                        (void) signal(SIGINT, SIG_DFL);
                    293:        }
1.2       deraadt   294:        sigfillset(&fset);
                    295:        (void) sigprocmask(SIG_UNBLOCK, &fset, NULL);
1.1       deraadt   296: }
                    297:
                    298: int
                    299: wait_command(pid)
                    300:        int pid;
                    301: {
                    302:
                    303:        if (wait_child(pid) < 0) {
                    304:                printf("Fatal error in process.\n");
                    305:                return -1;
                    306:        }
                    307:        return 0;
                    308: }
                    309:
                    310: static struct child *
                    311: findchild(pid)
                    312:        int pid;
                    313: {
                    314:        register struct child **cpp;
                    315:
                    316:        for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
                    317:             cpp = &(*cpp)->link)
                    318:                        ;
                    319:        if (*cpp == NULL) {
                    320:                *cpp = (struct child *) malloc(sizeof (struct child));
                    321:                (*cpp)->pid = pid;
                    322:                (*cpp)->done = (*cpp)->free = 0;
                    323:                (*cpp)->link = NULL;
                    324:        }
                    325:        return *cpp;
                    326: }
                    327:
                    328: static void
                    329: delchild(cp)
                    330:        register struct child *cp;
                    331: {
                    332:        register struct child **cpp;
                    333:
                    334:        for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
                    335:                ;
                    336:        *cpp = cp->link;
                    337:        free((char *) cp);
                    338: }
                    339:
                    340: void
                    341: sigchild(signo)
                    342:        int signo;
                    343: {
                    344:        int pid;
                    345:        union wait status;
                    346:        register struct child *cp;
                    347:
                    348:        while ((pid =
                    349:            wait3((int *)&status, WNOHANG, (struct rusage *)0)) > 0) {
                    350:                cp = findchild(pid);
                    351:                if (cp->free)
                    352:                        delchild(cp);
                    353:                else {
                    354:                        cp->done = 1;
                    355:                        cp->status = status;
                    356:                }
                    357:        }
                    358: }
                    359:
                    360: union wait wait_status;
                    361:
                    362: /*
                    363:  * Wait for a specific child to die.
                    364:  */
                    365: int
                    366: wait_child(pid)
                    367:        int pid;
                    368: {
1.2       deraadt   369:        sigset_t nset, oset;
1.1       deraadt   370:        register struct child *cp = findchild(pid);
1.2       deraadt   371:        sigemptyset(&nset);
                    372:        sigaddset(&nset, SIGCHLD);
                    373:        sigprocmask(SIG_BLOCK, &nset, &oset);
1.1       deraadt   374:
                    375:        while (!cp->done)
1.2       deraadt   376:                sigsuspend(&oset);
1.1       deraadt   377:        wait_status = cp->status;
                    378:        delchild(cp);
1.2       deraadt   379:        sigprocmask(SIG_SETMASK, &oset, NULL);
1.1       deraadt   380:        return wait_status.w_status ? -1 : 0;
                    381: }
                    382:
                    383: /*
                    384:  * Mark a child as don't care.
                    385:  */
                    386: void
                    387: free_child(pid)
                    388:        int pid;
                    389: {
1.2       deraadt   390:        sigset_t nset, oset;
1.1       deraadt   391:        register struct child *cp = findchild(pid);
1.2       deraadt   392:        sigemptyset(&nset);
                    393:        sigaddset(&nset, SIGCHLD);
                    394:        sigprocmask(SIG_BLOCK, &nset, &oset);
1.1       deraadt   395:
                    396:        if (cp->done)
                    397:                delchild(cp);
                    398:        else
                    399:                cp->free = 1;
1.2       deraadt   400:        sigprocmask(SIG_SETMASK, &oset, NULL);
1.4       millert   401: }
                    402:
                    403: /*
                    404:  * Lock(1)/unlock(0) mail spool using mail.local's -H flag.
                    405:  * Returns 1 for success, 0 for failure, -1 for bad usage.
                    406:  */
                    407: static int
                    408: handle_spool_locks(action)
                    409:        int action;
                    410: {
                    411:        char *cmd;
                    412:        static FILE *lockfp = NULL;
                    413:        static int lock_pid;
                    414:
                    415:        if (action == 0) {
                    416:                /* Clear the lock */
                    417:                if (lockfp == NULL) {
                    418:                        fprintf(stderr,
                    419:                            "handle_spool_locks: no spool lock to remove.\n");
                    420:                        return (-1);
                    421:                }
                    422:                (void)kill(lock_pid, SIGTERM);
                    423:                Pclose(lockfp);
                    424:                lockfp = NULL;
                    425:        } else if (action == 1) {
                    426:                /* Create the lock */
                    427:                if ((cmd = (char *) malloc(sizeof(_PATH_MAIL_LOCAL) + 3)) == NULL)
                    428:                        panic("Out of memory");
                    429:                sprintf(cmd, "%s -H", _PATH_MAIL_LOCAL);
                    430:                if ((lockfp = Popen(cmd, "r")) == NULL || getc(lockfp) != '1') {
                    431:                        lockfp = NULL;
                    432:                        free(cmd);
                    433:                        return (0);
                    434:                }
                    435:
                    436:                lock_pid = fp_head->pid;        /* new entries added at head */
                    437:                free(cmd);
                    438:        } else {
                    439:                fprintf(stderr, "handle_spool_locks: unknown action %d\n",
                    440:                    action);
                    441:                return (-1);
                    442:        }
                    443:
                    444:        return (1);
                    445: }
                    446:
                    447: int
                    448: spool_lock()
                    449: {
                    450:        return(handle_spool_locks(1));
                    451: }
                    452:
                    453: int
                    454: spool_unlock()
                    455: {
                    456:        return(handle_spool_locks(0));
1.1       deraadt   457: }