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