From 5aaa8f942e1d75853f90d9c9e7c71aa71db54920 Mon Sep 17 00:00:00 2001 From: Bill Joy Date: Sat, 8 Dec 1979 04:40:10 -0800 Subject: [PATCH] BSD 3 development Work on file usr/src/cmd/atrun.c Synthesized-from: 3bsd --- usr/src/cmd/atrun.c | 108 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 usr/src/cmd/atrun.c diff --git a/usr/src/cmd/atrun.c b/usr/src/cmd/atrun.c new file mode 100644 index 0000000000..401862a8cf --- /dev/null +++ b/usr/src/cmd/atrun.c @@ -0,0 +1,108 @@ +/* + * Run programs submitted by at. + */ +#include +#include +#include +#include +#include + +# define DIR "/usr/spool/at" +# define PDIR "past" +# define LASTF "/usr/spool/at/lasttimedone" + +int nowtime; +int nowdate; +int nowyear; + +main(argc, argv) +char **argv; +{ + int tt, day, year, uniq; + struct direct dirent; + FILE *dirf; + + setuid(0); + setgid(0); + chdir(DIR); + makenowtime(); + if ((dirf = fopen(".", "r")) == NULL) { + fprintf(stderr, "Cannot read at directory\n"); + exit(1); + } + while (fread((char *)&dirent, sizeof(dirent), 1, dirf) == 1) { + if (dirent.d_ino==0) + continue; + if (sscanf(dirent.d_name, "%2d.%3d.%4d.%2d", &year, &day, &tt, &uniq) != 4) + continue; + if (nowyear < year) + continue; + if (nowyear==year && nowdate < day) + continue; + if (nowyear==year && nowdate==day && nowtime < tt) + continue; + run(dirent.d_name); + } + fclose(dirf); + updatetime(nowtime); + exit(0); +} + +makenowtime() +{ + long t; + struct tm *localtime(); + register struct tm *tp; + + time(&t); + tp = localtime(&t); + nowtime = tp->tm_hour*100 + tp->tm_min; + nowdate = tp->tm_yday; + nowyear = tp->tm_year; +} + +updatetime(t) +{ + FILE *tfile; + + tfile = fopen(LASTF, "w"); + if (tfile == NULL) { + fprintf(stderr, "can't write lastfile\n"); + exit(1); + } + fprintf(tfile, "%04d\n", t); +} + +run(file) +char *file; +{ + struct stat stbuf; + register pid, i; + char sbuf[64]; + + /* printf("running %s\n", file); */ + if (fork()!=0) + return; + for (i=0; i<15; i++) + close(i); + dup(dup(open("/dev/null", 0))); + sprintf(sbuf, "/bin/mv %.14s %s", file, PDIR); + system(sbuf); + chdir(PDIR); + if (stat(file, &stbuf) == -1) + exit(1); + if (pid = fork()) { + if (pid == -1) + exit(1); + wait((int *)0); + unlink(file); + exit(0); + } + setgid(stbuf.st_gid); + setuid(stbuf.st_uid); + nice(3); + execl("/bin/sh", "sh", file, 0); + execl("/usr/bin/sh", "sh", file, 0); + fprintf(stderr, "Can't execl shell\n"); + exit(1); +} -- 2.20.1