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

1.1       root        1: /*
                      2:  * cronpipe.c 
                      3:  * Special version of popen, that allows to cron 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 int     ifmail();
                     17: 
                     18: extern char    acRealUser[MAX_UNAME];
                     19: extern time_t  clock;
                     20: extern child_id *current;      /* Pointer to the current child id */
                     21: extern int     flag320;
                     22: extern int     mailFlag;
                     23: extern int     set_uid_flag;
                     24: 
                     25: static int     poppid[_NFILE];
                     26: 
                     27: FILE *
                     28: cronpipe(command, type)
                     29: char *command, *type;
                     30: {
                     31:        register mode;
                     32:        register fd;
                     33:        int pd[2];
                     34: 
                     35:        if (pipe(pd) < 0)
                     36:                return (NULL);
                     37:        if (*type == 'w') {
                     38:                mode = W;
                     39:                fd = pd[W];
                     40:        } else {
                     41:                mode = R;
                     42:                fd = pd[R];
                     43:        }
                     44:        if ((poppid[fd] = fork()) < 0) {
                     45:                close(pd[R]);
                     46:                close(pd[W]);
                     47:                return (NULL);
                     48:        }
                     49:        if (poppid[fd] == 0) {          /* Child */
                     50:                /* Set uid to user which crontab is going to be executed.
                     51:                 * In case of COHERENT3.2.0 set uid to daemon (set_uid_flag
                     52:                 * is TRUE).
                     53:                 */
                     54:                Dprint("Set user ID to '%s'\n", acRealUser);
                     55:                if (set_uid_flag == FALSE) {
                     56:                        /* If file name is not user name ignore it */
                     57:                        if ((set_uid_flag = set_uid(acRealUser)) != TRUE) {
                     58:                                /* During testing were user traps
                     59:                                        from smail. So they may come from here.
                     60:                                if (ifmail(acRealUser)) {
                     61:                                        char mailBuf[80];
                     62:                                        sprintf(mailBuf,"smail %s < %s" 
                     63:                                          "cron: cannot find/set user '%s'\n", 
                     64:                                           acRealUser, acRealUser);
                     65:                                        system(mailBuf);
                     66:                                }*/
                     67:                                exit(0);
                     68:                        } 
                     69:                }
                     70:                if (mode == W) {
                     71:                        close(pd[W]);
                     72:                        close(fileno(stdin));
                     73:                        dup(pd[R]);
                     74:                        close(pd[R]);
                     75:                } else {
                     76:                        close(pd[R]);
                     77:                        close(fileno(stdout));
                     78:                        dup(pd[W]);
                     79:                        close(pd[W]);
                     80:                }
                     81:                Dprint("cronpipe: before execl %s\n", command);
                     82:                execl("/bin/sh", "sh", "-c", command, NULL);
                     83:                exit(1);
                     84:        } 
                     85:        /* Parent */
                     86:        /* We will keep track about keeds only in SV mode */
                     87:        if (flag320 == FALSE) {
                     88:                Dprint("cronpipe before fork: Name %s\t", acRealUser);
                     89:                Dprint("                      Pid %d\t", poppid[fd]);
                     90:                Dprint("                      Command %s\n", command);
                     91:                current = add_entry(poppid[fd], acRealUser, clock, command);
                     92:                Dprint("cronpipe: after add. Pid is %d\n", current->pid);
                     93:        }
                     94:        if (mode == W) {
                     95:                close(pd[R]);
                     96:                return (fdopen(pd[W], "w"));
                     97:        } else {
                     98:                close(pd[W]);
                     99:                return (fdopen(pd[R], "r"));
                    100:        }
                    101: }
                    102: 
                    103: pclose(stream)
                    104: FILE *stream;
                    105: {
                    106:        register fd;
                    107:        register pid, wpid;
                    108:        int status;
                    109:        int (*hupfun)(), (*intfun)(), (*quitfun)();
                    110: 
                    111:        fd = fileno(stream);
                    112:        pid = poppid[fd];
                    113:        poppid[fd] = 0;
                    114:        if (pid==0 || fclose(stream)==EOF)
                    115:                return (-1);
                    116:        hupfun = signal(SIGHUP, SIG_IGN);
                    117:        intfun = signal(SIGINT, SIG_IGN);
                    118:        quitfun = signal(SIGQUIT, SIG_IGN);
                    119:        while ((wpid = wait(&status))!=pid && wpid>=0)
                    120:                ;
                    121:        if (wpid < 0)
                    122:                status = -1;
                    123:        signal(SIGHUP, hupfun);
                    124:        signal(SIGINT, intfun);
                    125:        signal(SIGQUIT, quitfun);
                    126:        return (status);
                    127: }

unix.superglobalmegacorp.com

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