BSD 4_3_Reno release
[unix-history] / usr / src / usr.sbin / cron / cron.c
index 74739a0..a6c868b 100644 (file)
@@ -1,32 +1,34 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)cron.c      4.10 (Berkeley) %G%";
+static char *sccsid = "@(#)cron.c      4.16 (Berkeley) 6/29/90";
 #endif
 
 #include <sys/types.h>
 #endif
 
 #include <sys/types.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <signal.h>
+#include <sys/signal.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/ioctl.h>
 #include <sys/file.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/ioctl.h>
 #include <sys/file.h>
+#include <sys/resource.h>
 #include <pwd.h>
 #include <pwd.h>
+#include <fcntl.h>
+#include <syslog.h>
+#include <stdio.h>
+#include <ctype.h>
+#include "pathnames.h"
 
 #define        LISTS   (2*BUFSIZ)
 #define        MAXLIN  BUFSIZ
 
 
 #define        LISTS   (2*BUFSIZ)
 #define        MAXLIN  BUFSIZ
 
-#ifndef CRONTAB
-#define CRONTAB "/usr/lib/crontab"
-#endif
-
 #define        EXACT   100
 #define        ANY     101
 #define        LIST    102
 #define        RANGE   103
 #define        EOS     104
 #define        EXACT   100
 #define        ANY     101
 #define        LIST    102
 #define        RANGE   103
 #define        EOS     104
-char   crontab[]       = CRONTAB;
-time_t itime;
+
+char   crontab[]       = _PATH_CRON;
+char   loc_crontab[]   = _PATH_LCRON;
+time_t itime, time();
 struct tm *loct;
 struct tm *localtime();
 char   *malloc();
 struct tm *loct;
 struct tm *localtime();
 char   *malloc();
@@ -34,36 +36,85 @@ char        *realloc();
 int    reapchild();
 int    flag;
 char   *list;
 int    reapchild();
 int    flag;
 char   *list;
+char   *listend;
 unsigned listsize;
 
 unsigned listsize;
 
-main()
+FILE   *debug;
+#define dprintf if (debug) fprintf
+
+main(argc, argv)
+       int argc;
+       char **argv;
 {
        register char *cp;
        char *cmp();
        time_t filetime = 0;
 {
        register char *cp;
        char *cmp();
        time_t filetime = 0;
+       time_t lfiletime = 0;
+       char c;
+       extern char *optarg;
 
 
-       if (fork())
+       if (geteuid()) {
+               fprintf(stderr, "cron: NOT super-user\n");
+               exit(1);
+       }
+
+       openlog("cron", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_DAEMON);
+       switch (fork()) {
+
+       case -1:
+               syslog(LOG_ERR, "fork: %m");
+               exit(1);
+               /* NOTREACHED */
+       case 0:
+               break;
+       default:
                exit(0);
                exit(0);
-       chdir("/");
-       freopen("/", "r", stdout);
-       freopen("/", "r", stderr);
-       untty();
-       signal(SIGHUP, SIG_IGN);
-       signal(SIGINT, SIG_IGN);
-       signal(SIGQUIT, SIG_IGN);
-       signal(SIGCHLD, reapchild);
-       time(&itime);
+               /* NOTREACHED */
+       }
+
+       c = getopt(argc, argv, "d:");
+       if (c == 'd') {
+               debug = fopen(optarg, "w");
+               if (debug == NULL)
+                       exit(1);
+               (void) fcntl(fileno(debug), F_SETFL, FAPPEND);
+       }
+       daemon(0, 0);
+       (void) signal(SIGHUP, SIG_IGN);
+       (void) signal(SIGINT, SIG_IGN);
+       (void) signal(SIGQUIT, SIG_IGN);
+       (void) signal(SIGCHLD, reapchild);
+       (void) time(&itime);
        itime -= localtime(&itime)->tm_sec;
 
        for (;; itime+=60, slp()) {
        itime -= localtime(&itime)->tm_sec;
 
        for (;; itime+=60, slp()) {
-               struct stat cstat;
-
-               if (stat(crontab, &cstat) == -1)
-                       continue;
-               if (cstat.st_mtime > filetime) {
+               struct stat cstat, lcstat;
+               int newcron, newloc;
+               
+               newcron = 0;
+               if (stat(crontab, &cstat) < 0)
+                   cstat.st_mtime = 1;
+               if (cstat.st_mtime != filetime) {
                        filetime = cstat.st_mtime;
                        filetime = cstat.st_mtime;
+                       newcron++;
+               }
+
+               newloc  = 0;
+               if (stat(loc_crontab, &lcstat) < 0)
+                   lcstat.st_mtime = 1;
+               if (lcstat.st_mtime != lfiletime) {
+                       lfiletime = lcstat.st_mtime;
+                       newloc++;
+               }
+
+               if (newcron || newloc) {
                        init();
                        init();
+                       append(crontab);
+                       append(loc_crontab);
+                       *listend++ = EOS;
+                       *listend++ = EOS;
                }
                }
+
                loct = localtime(&itime);
                loct->tm_mon++;          /* 1-12 for month */
                if (loct->tm_wday == 0)
                loct = localtime(&itime);
                loct->tm_mon++;          /* 1-12 for month */
                if (loct->tm_wday == 0)
@@ -125,7 +176,7 @@ slp()
        register i;
        time_t t;
 
        register i;
        time_t t;
 
-       time(&t);
+       (void) time(&t);
        i = itime - t;
        if(i < -60 * 60 || i > 60 * 60) {
                itime = t;
        i = itime - t;
        if(i < -60 * 60 || i > 60 * 60) {
                itime = t;
@@ -133,57 +184,82 @@ slp()
                itime += i;
        }
        if(i > 0)
                itime += i;
        }
        if(i > 0)
-               sleep(i);
+               sleep((u_int)i);
 }
 
 ex(s)
 char *s;
 {
 }
 
 ex(s)
 char *s;
 {
-       int st;
        register struct passwd *pwd;
        char user[BUFSIZ];
        char *c = user;
        register struct passwd *pwd;
        char user[BUFSIZ];
        char *c = user;
+       int pid;
 
 
-       if(fork()) {
+       switch (fork()) {
+       case 0:
+               break;
+       case -1:
+               syslog(LOG_ERR, "cannot fork: %m (running %.40s%s)",
+                       s, strlen(s) > 40 ? "..." : "");
+               /*FALLTHROUGH*/
+       default:
                return;
        }
                return;
        }
-
+       pid = getpid();
        while(*s != ' ' && *s != '\t')
                *c++ = *s++;
        *c = '\0';
        while(*s != ' ' && *s != '\t')
                *c++ = *s++;
        *c = '\0';
+       s++;
        if ((pwd = getpwnam(user)) == NULL) {
        if ((pwd = getpwnam(user)) == NULL) {
+               syslog(LOG_ERR, "invalid user name \"%s\"", user);
+               dprintf(debug, "%d: cannot find %s\n", pid, user),
+                       fflush(debug);
                exit(1);
        }
        (void) setgid(pwd->pw_gid);
                exit(1);
        }
        (void) setgid(pwd->pw_gid);
-       initgroups(pwd->pw_name, pwd->pw_gid);
+       (void) initgroups(pwd->pw_name, pwd->pw_gid);
        (void) setuid(pwd->pw_uid);
        (void) setuid(pwd->pw_uid);
-       freopen("/", "r", stdin);
-       execl("/bin/sh", "sh", "-c", ++s, 0);
+       (void) freopen("/", "r", stdin);
+       closelog();
+       dprintf(debug, "%d: executing %s", pid, s), fflush (debug);
+       execl(_PATH_BSHELL, "sh", "-c", s, 0);
+       syslog(LOG_ERR, "cannot exec %s: %m");
+       dprintf(debug, "%d: cannot execute sh\n", pid), fflush (debug);
        exit(0);
 }
 
 init()
        exit(0);
 }
 
 init()
+{
+       /*
+        * Don't free in case was longer than LISTS.  Trades off
+        * the rare case of crontab shrinking vs. the common case of
+        * extra realloc's needed in append() for a large crontab.
+        */
+       if (list == 0) {
+               list = malloc(LISTS);
+               listsize = LISTS;
+       }
+       listend = list;
+}
+
+append(fn)
+char *fn;
 {
        register i, c;
        register char *cp;
        register char *ocp;
        register int n;
 
 {
        register i, c;
        register char *cp;
        register char *ocp;
        register int n;
 
-       freopen(crontab, "r", stdin);
-       if (list) {
-               list = realloc(list, LISTS);
-       } else
-               list = malloc(LISTS);
-       listsize = LISTS;
-       cp = list;
-
+       if (freopen(fn, "r", stdin) == NULL)
+               return;
+       cp = listend;
 loop:
        if(cp > list+listsize-MAXLIN) {
 loop:
        if(cp > list+listsize-MAXLIN) {
-               char *olist;
+               int length = cp - list;
+
                listsize += LISTS;
                listsize += LISTS;
-               olist = list;
                list = realloc(list, listsize);
                list = realloc(list, listsize);
-               cp = list + (cp - olist);
+               cp = list + length;
        }
        ocp = cp;
        for(i=0;; i++) {
        }
        ocp = cp;
        for(i=0;; i++) {
@@ -252,9 +328,8 @@ ignore:
        cp = ocp;
        while(c != '\n') {
                if(c == EOF) {
        cp = ocp;
        while(c != '\n') {
                if(c == EOF) {
-                       *cp++ = EOS;
-                       *cp++ = EOS;
-                       fclose(stdin);
+                       (void) fclose(stdin);
+                       listend = cp;
                        return;
                }
                c = getchar();
                        return;
                }
                c = getchar();
@@ -271,7 +346,7 @@ register c;
                n = n*10 + c - '0';
                c = getchar();
        }
                n = n*10 + c - '0';
                c = getchar();
        }
-       ungetc(c, stdin);
+       (void) ungetc(c, stdin);
        if (n>=100)
                return(-1);
        return(n);
        if (n>=100)
                return(-1);
        return(n);
@@ -280,18 +355,10 @@ register c;
 reapchild()
 {
        union wait status;
 reapchild()
 {
        union wait status;
+       int pid;
 
 
-       while(wait3(&status, WNOHANG, 0) > 0)
-               ;
-}
-
-untty()
-{
-       int i;
-
-       i = open("/dev/tty", O_RDWR);
-       if (i >= 0) {
-               ioctl(i, TIOCNOTTY, (char *)0);
-               (void) close(i);
-       }
+       while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0)
+               dprintf(debug, "%d: child exits with signal %d status %d\n",
+                       pid, status.w_termsig, status.w_retcode),
+                       fflush (debug);
 }
 }