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