home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume23 / vixie-cron / part02 / crond.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-09  |  6.9 KB  |  310 lines

  1. #if !defined(lint) && !defined(LINT)
  2. static char rcsid[] = "$Header: crond.c,v 2.1 90/07/18 00:23:53 vixie Exp $";
  3. #endif
  4.  
  5. /* Copyright 1988,1990 by Paul Vixie
  6.  * All rights reserved
  7.  *
  8.  * Distribute freely, except: don't remove my name from the source or
  9.  * documentation (don't take credit for my work), mark your changes (don't
  10.  * get me blamed for your possible bugs), don't alter or remove this
  11.  * notice.  May be sold if buildable source is provided to buyer.  No
  12.  * warrantee of any kind, express or implied, is included with this
  13.  * software; use at your own risk, responsibility for damages (if any) to
  14.  * anyone resulting from the use of this software rests entirely with the
  15.  * user.
  16.  *
  17.  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
  18.  * I'll try to keep a version up to date.  I can be reached as follows:
  19.  * Paul Vixie, 329 Noe Street, San Francisco, CA, 94114, (415) 864-7013,
  20.  * paul@vixie.sf.ca.us || {hoptoad,pacbell,decwrl,crash}!vixie!paul
  21.  */
  22.  
  23.  
  24. #define    MAIN_PROGRAM
  25.  
  26.  
  27. #include "cron.h"
  28. #include <sys/time.h>
  29. #include <sys/signal.h>
  30. #include <sys/types.h>
  31. #if defined(BSD)
  32. # include <sys/wait.h>
  33. # include <sys/resource.h>
  34. #endif /*BSD*/
  35.  
  36. extern int    fprintf(), fork(), unlink();
  37. extern time_t    time();
  38. extern void    exit();
  39. extern unsigned    sleep();
  40.  
  41. void
  42. usage()
  43. {
  44.     (void) fprintf(stderr, "usage:  %s [-x debugflag[,...]]\n", ProgramName);
  45.     (void) exit(ERROR_EXIT);
  46. }
  47.  
  48.  
  49. int
  50. main(argc, argv)
  51.     int    argc;
  52.     char    *argv[];
  53. {
  54.     extern void    set_cron_uid(), be_different(), load_database(),
  55.             set_cron_cwd(), open_logfile();
  56.  
  57.     static void    cron_tick(), cron_sleep(), cron_sync(),
  58.             sigchld_handler(), parse_args(), run_reboot_jobs();
  59.  
  60.     auto cron_db    database;
  61.  
  62.     ProgramName = argv[0];
  63.  
  64. #if defined(BSD)
  65.     setlinebuf(stdout);
  66.     setlinebuf(stderr);
  67. #endif
  68.  
  69.     parse_args(argc, argv);
  70.  
  71. # if DEBUGGING
  72.     /* if there are no debug flags turned on, fork as a daemon should.
  73.      */
  74.     if (DebugFlags)
  75.     {
  76.         (void) fprintf(stderr, "[%d] crond started\n", getpid());
  77.     }
  78.     else
  79. # endif /*DEBUGGING*/
  80.     {
  81.         switch (fork())
  82.         {
  83.         case -1:
  84.             log_it("CROND",getpid(),"DEATH","can't fork");
  85.             exit(0);
  86.             break;
  87.         case 0:
  88.             /* child process */
  89.             be_different();
  90.             break;
  91.         default:
  92.             /* parent process should just die */
  93.             _exit(0);
  94.         }
  95.     }
  96.  
  97. #if defined(BSD)
  98.     (void) signal(SIGCHLD, sigchld_handler);
  99. #endif /*BSD*/
  100.  
  101. #if defined(ATT)
  102.     (void) signal(SIGCLD, SIG_IGN);
  103. #endif /*ATT*/
  104.  
  105.     acquire_daemonlock();
  106.     set_cron_uid();
  107.     set_cron_cwd();
  108.     database.head = NULL;
  109.     database.tail = NULL;
  110.     database.mtime = (time_t) 0;
  111.     load_database(&database);
  112.     run_reboot_jobs(&database);
  113.     cron_sync();
  114.     while (TRUE)
  115.     {
  116. # if DEBUGGING
  117.         if (!(DebugFlags & DTEST))
  118. # endif /*DEBUGGING*/
  119.             cron_sleep();
  120.  
  121.         load_database(&database);
  122.  
  123.         /* do this iteration
  124.          */
  125.         cron_tick(&database);
  126.  
  127.         /* sleep 1 minute
  128.          */
  129.         TargetTime += 60;
  130.     }
  131. }
  132.  
  133.  
  134. static void
  135. run_reboot_jobs(db)
  136.     cron_db *db;
  137. {
  138.     extern void        job_add();
  139.     extern int        job_runqueue();
  140.     register user        *u;
  141.     register entry        *e;
  142.  
  143.     for (u = db->head;  u != NULL;  u = u->next) {
  144.         for (e = u->crontab;  e != NULL;  e = e->next) {
  145.             if (e->flags & WHEN_REBOOT) {
  146.                 job_add(e->cmd, u);
  147.             }
  148.         }
  149.     }
  150.     (void) job_runqueue();
  151. }
  152.  
  153.  
  154. static void
  155. cron_tick(db)
  156.     cron_db    *db;
  157. {
  158.     extern void        job_add();
  159.     extern char        *env_get();
  160.     extern struct tm    *localtime();
  161.      register struct tm    *tm = localtime(&TargetTime);
  162.     local int        minute, hour, dom, month, dow;
  163.     register user        *u;
  164.     register entry        *e;
  165.  
  166.     /* make 0-based values out of these so we can use them as indicies
  167.      */
  168.     minute = tm->tm_min -FIRST_MINUTE;
  169.     hour = tm->tm_hour -FIRST_HOUR;
  170.     dom = tm->tm_mday -FIRST_DOM;
  171.     month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
  172.     dow = tm->tm_wday -FIRST_DOW;
  173.  
  174.     Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d)\n",
  175.         getpid(), minute, hour, dom, month, dow))
  176.  
  177.     /* the dom/dow situation is odd.  '* * 1,15 * Sun' will run on the
  178.      * first and fifteenth AND every Sunday;  '* * * * Sun' will run *only*
  179.      * on Sundays;  '* * 1,15 * *' will run *only* the 1st and 15th.  this
  180.      * is why we keep 'e->dow_star' and 'e->dom_star'.  yes, it's bizarre.
  181.      * like many bizarre things, it's the standard.
  182.      */
  183.     for (u = db->head;  u != NULL;  u = u->next) {
  184.         Debug(DSCH|DEXT, ("user [%s:%d:%d:...]\n",
  185.             env_get(USERENV,u->envp), u->uid, u->gid))
  186.         for (e = u->crontab;  e != NULL;  e = e->next) {
  187.             Debug(DSCH|DEXT, ("entry [%s]\n", e->cmd))
  188.             if (bit_test(e->minute, minute)
  189.              && bit_test(e->hour, hour)
  190.              && bit_test(e->month, month)
  191.              && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
  192.                   ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
  193.                   : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
  194.                 )
  195.                ) {
  196.                 job_add(e->cmd, u);
  197.             }
  198.         }
  199.     }
  200. }
  201.  
  202.  
  203. /* the task here is to figure out how long it's going to be until :00 of the
  204.  * following minute and initialize TargetTime to this value.  TargetTime
  205.  * will subsequently slide 60 seconds at a time, with correction applied
  206.  * implicitly in cron_sleep().  it would be nice to let crond execute in
  207.  * the "current minute" before going to sleep, but by restarting cron you
  208.  * could then get it to execute a given minute's jobs more than once.
  209.  * instead we have the chance of missing a minute's jobs completely, but
  210.  * that's something sysadmin's know to expect what with crashing computers..
  211.  */
  212. static void
  213. cron_sync()
  214. {
  215.     extern struct tm    *localtime();
  216.      register struct tm    *tm;
  217.  
  218.     TargetTime = time((time_t*)0);
  219.     tm = localtime(&TargetTime);
  220.     TargetTime += (60 - tm->tm_sec);
  221. }
  222.  
  223.  
  224. static void
  225. cron_sleep()
  226. {
  227.     extern void    do_command();
  228.     extern int    job_runqueue();
  229.     register int    seconds_to_wait;
  230.  
  231.     do {
  232.         seconds_to_wait = (int) (TargetTime - time((time_t*)0));
  233.         Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
  234.             getpid(), TargetTime, seconds_to_wait))
  235.  
  236.         /* if we intend to sleep, this means that it's finally
  237.          * time to empty the job queue (execute it).
  238.          *
  239.          * if we run any jobs, we'll probably screw up our timing,
  240.          * so go recompute.
  241.          *
  242.          * note that we depend here on the left-to-right nature
  243.          * of &&, and the short-circuiting.
  244.          */
  245.     } while (seconds_to_wait > 0 && job_runqueue());
  246.  
  247.     if (seconds_to_wait > 0)
  248.     {
  249.         Debug(DSCH, ("[%d] sleeping for %d seconds\n",
  250.             getpid(), seconds_to_wait))
  251.         (void) sleep((unsigned int) seconds_to_wait);
  252.     }
  253. }
  254.  
  255.  
  256. #if defined(BSD)
  257. static void
  258. sigchld_handler()
  259. {
  260.     union wait    waiter;
  261.     int        pid;
  262.  
  263.     for (;;)
  264.     {
  265.         pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
  266.         switch (pid)
  267.         {
  268.         case -1:
  269.             Debug(DPROC,
  270.                 ("[%d] sigchld...no children\n", getpid()))
  271.             return;
  272.         case 0:
  273.             Debug(DPROC,
  274.                 ("[%d] sigchld...no dead kids\n", getpid()))
  275.             return;
  276.         default:
  277.             Debug(DPROC,
  278.                 ("[%d] sigchld...pid #%d died, stat=%d\n",
  279.                 getpid(), pid, waiter.w_status))
  280.         }
  281.     }
  282. }
  283. #endif /*BSD*/
  284.  
  285.  
  286. static void
  287. parse_args(argc, argv)
  288.     int    argc;
  289.     char    *argv[];
  290. {
  291.     extern    int    optind, getopt();
  292.     extern    void    usage();
  293.     extern    char    *optarg;
  294.  
  295.     int    argch;
  296.  
  297.     while (EOF != (argch = getopt(argc, argv, "x:")))
  298.     {
  299.         switch (argch)
  300.         {
  301.         default:
  302.             usage();
  303.         case 'x':
  304.             if (!set_debug_flags(optarg))
  305.                 usage();
  306.             break;
  307.         }
  308.     }
  309. }
  310.