Annotation of coherent/d/etc/SYSVcron/src/cronpipe.c.1.2, revision 1.1

1.1     ! root        1: /*
        !             2:  * cronpipe.c 
        !             3:  * Special version of popen, that allows to crond keep track about
        !             4:  * children.
        !             5:  */
        !             6: #include <stdio.h>
        !             7: #include <signal.h>
        !             8: #include <time.h>
        !             9: #include <sys/types.h>
        !            10: #include "cron.h"
        !            11: 
        !            12: #define        R       0       /* Pipe read descriptor index */
        !            13: #define        W       1       /* Pipe write descriptor index */
        !            14: 
        !            15: extern child_id        *add_entry();   /* Add an new entry to a link list */
        !            16: extern child_id *current;      /* Pointer to the current child id */
        !            17: 
        !            18: extern char    acRealUser[MAX_UNAME];
        !            19: extern time_t  clock;
        !            20: extern int     flag320;
        !            21: 
        !            22: static int     poppid[_NFILE];
        !            23: 
        !            24: FILE *
        !            25: cronpipe(command, type)
        !            26: char *command, *type;
        !            27: {
        !            28:        register mode;
        !            29:        register fd;
        !            30:        int pd[2];
        !            31: 
        !            32:        if (pipe(pd) < 0)
        !            33:                return (NULL);
        !            34:        if (*type == 'w') {
        !            35:                mode = W;
        !            36:                fd = pd[W];
        !            37:        } else {
        !            38:                mode = R;
        !            39:                fd = pd[R];
        !            40:        }
        !            41:        if ((poppid[fd] = fork()) < 0) {
        !            42:                close(pd[R]);
        !            43:                close(pd[W]);
        !            44:                return (NULL);
        !            45:        }
        !            46:        if (poppid[fd] == 0) {          /* Child */
        !            47:                if (mode == W) {
        !            48:                        close(pd[W]);
        !            49:                        close(fileno(stdin));
        !            50:                        dup(pd[R]);
        !            51:                        close(pd[R]);
        !            52:                } else {
        !            53:                        close(pd[R]);
        !            54:                        close(fileno(stdout));
        !            55:                        dup(pd[W]);
        !            56:                        close(pd[W]);
        !            57:                }
        !            58:                Dprint("cronpipe: before execl %s\n", command);
        !            59:                execl("/bin/sh", "sh", "-c", command, NULL);
        !            60:                exit(1);
        !            61:        } 
        !            62:        /* Parent */
        !            63:        /* We will keep track about keeds only in SV mode */
        !            64:        if (flag320 == FALSE) {
        !            65:                Dprint("cronpipe before fork: Name %s\n", acRealUser);
        !            66:                Dprint("                      Pid %d\n", poppid[fd]);
        !            67:                Dprint("                      Name %s\n", acRealUser);
        !            68:                Dprint("                      Time %s\n", ctime(&clock));
        !            69:                Dprint("                      Command %s\n", command);
        !            70:                current = add_entry(poppid[fd], acRealUser, clock, command);
        !            71:        }
        !            72:        if (mode == W) {
        !            73:                close(pd[R]);
        !            74:                return (fdopen(pd[W], "w"));
        !            75:        } else {
        !            76:                close(pd[W]);
        !            77:                return (fdopen(pd[R], "r"));
        !            78:        }
        !            79: }
        !            80: 
        !            81: pclose(stream)
        !            82: FILE *stream;
        !            83: {
        !            84:        register fd;
        !            85:        register pid, wpid;
        !            86:        int status;
        !            87:        int (*hupfun)(), (*intfun)(), (*quitfun)();
        !            88: 
        !            89:        fd = fileno(stream);
        !            90:        pid = poppid[fd];
        !            91:        poppid[fd] = 0;
        !            92:        if (pid==0 || fclose(stream)==EOF)
        !            93:                return (-1);
        !            94:        hupfun = signal(SIGHUP, SIG_IGN);
        !            95:        intfun = signal(SIGINT, SIG_IGN);
        !            96:        quitfun = signal(SIGQUIT, SIG_IGN);
        !            97:        while ((wpid = wait(&status))!=pid && wpid>=0)
        !            98:                ;
        !            99:        if (wpid < 0)
        !           100:                status = -1;
        !           101:        signal(SIGHUP, hupfun);
        !           102:        signal(SIGINT, intfun);
        !           103:        signal(SIGQUIT, quitfun);
        !           104:        return (status);
        !           105: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.