Annotation of researchv10no/cmd/at/atrun.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Run programs submitted by at.
                      3:  */
                      4: #include <stdio.h>
                      5: #include <sys/param.h>
                      6: #include <time.h>
                      7: #include <sys/stat.h>
                      8: #include <ndir.h>
                      9: #include <pwd.h>
                     10: 
                     11: # define SDIR "/usr/spool/at"
                     12: # define PDIR  "past"
                     13: # define LASTF "/usr/spool/at/lasttimedone"
                     14: 
                     15: int    nowtime;
                     16: int    nowdate;
                     17: int    nowyear;
                     18: 
                     19: main(argc, argv)
                     20: char **argv;
                     21: {
                     22:        int tt, day, year, uniq;
                     23:        DIR *dirp;
                     24:        struct direct *dp;
                     25: 
                     26:        chdir(SDIR);
                     27:        makenowtime();
                     28:        if ((dirp = opendir(".")) == 0) {
                     29:                fprintf(stderr, "Cannot read at directory\n");
                     30:                exit(1);
                     31:        }
                     32:        for (dp = readdir(dirp); dp; dp = readdir(dirp)) {
                     33:                if (dp->d_name[0] == '.') {
                     34:                        if (dp->d_name[1] == '\0')
                     35:                                continue;
                     36:                        if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
                     37:                                continue;
                     38:                }
                     39:                if (sscanf(dp->d_name, "%2d.%3d.%4d.%2d", &year, &day, &tt, &uniq) != 4)
                     40:                        continue;
                     41:                if (nowyear < year)
                     42:                        continue;
                     43:                if (nowyear == year && nowdate < day)
                     44:                        continue;
                     45:                if (nowyear == year && nowdate == day && nowtime < tt)
                     46:                        continue;
                     47:                run(dp->d_name);
                     48:        }
                     49:        closedir(dirp);
                     50:        updatetime(nowtime);
                     51:        exit(0);
                     52: }
                     53: 
                     54: makenowtime()
                     55: {
                     56:        long t;
                     57:        struct tm *localtime();
                     58:        register struct tm *tp;
                     59: 
                     60:        time(&t);
                     61:        tp = localtime(&t);
                     62:        nowtime = tp->tm_hour*100 + tp->tm_min;
                     63:        nowdate = tp->tm_yday;
                     64:        nowyear = tp->tm_year;
                     65: }
                     66: 
                     67: updatetime(t)
                     68: {
                     69:        FILE *tfile;
                     70: 
                     71:        tfile = fopen(LASTF, "w");
                     72:        if (tfile == NULL) {
                     73:                fprintf(stderr, "can't write lastfile\n");
                     74:                exit(1);
                     75:        }
                     76:        fprintf(tfile, "%.4d\n", t);
                     77: }
                     78: 
                     79: run(file)
                     80: char *file;
                     81: {
                     82:        struct stat stbuf;
                     83:        register pid, i;
                     84:        char sbuf[64];
                     85:        struct passwd *pw;
                     86:        struct passwd *getpwuid();
                     87: 
                     88:        if (fork()!=0)
                     89:                return;
                     90:        for (i=0; i<NOFILE; i++)
                     91:                close(i);
                     92:        dup(dup(dup(open("/dev/null", 2))));
                     93:        sprintf(sbuf, "%s/%s", PDIR, file);
                     94:        link(file, sbuf);
                     95:        unlink(file);
                     96:        chdir(PDIR);
                     97:        if (stat(file, &stbuf) == -1)
                     98:                exit(1);
                     99:        if ((pw = getpwuid(stbuf.st_uid)) == NULL)
                    100:                exit(1);        /* cruel but fair */
                    101:        if (pid = fork()) {
                    102:                if (pid == -1)
                    103:                        exit(1);
                    104:                wait((int *)0);
                    105:                unlink(file);
                    106:                exit(0);
                    107:        }
                    108:        setlogname(pw->pw_name);
                    109:        (void)setupshares(stbuf.st_uid, (void (*)())0);
                    110:        setupgroups(pw->pw_name, stbuf.st_gid);
                    111:        setgid(stbuf.st_gid);
                    112:        setuid(stbuf.st_uid);
                    113:        nice(5);
                    114:        execl("/bin/sh", "sh", file, 0);
                    115:        execl("/usr/bin/sh", "sh", file, 0);
                    116:        fprintf(stderr, "Can't execl shell\n");
                    117:        exit(1);
                    118: }

unix.superglobalmegacorp.com

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