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