1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
27 #include <sys/signal.h>
29 # include <sys/time.h>
35 static void usage __P((void)),
36 run_reboot_jobs __P((cron_db *)),
37 cron_tick __P((cron_db *)),
38 cron_sync __P((void)),
39 cron_sleep __P((cron_db *)),
40 cron_clean __P((cron_db *)),
42 sigchld_handler __P((int)),
44 sighup_handler __P((int)),
45 parse_args __P((int c, char *v[]));
47 static time_t last_time = 0;
48 static int dst_enabled = 0;
54 fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] "
55 "[-s] [-o] [-x debugflag[,...]]\n");
56 fprintf(stderr, "\ndebugflags: ");
58 for(dflags = DebugFlagNames; *dflags; dflags++) {
59 fprintf(stderr, "%s ", *dflags);
61 fprintf(stderr, "\n");
74 ProgramName = argv[0];
81 parse_args(argc, argv);
84 (void) signal(SIGCHLD, sigchld_handler);
86 (void) signal(SIGCLD, SIG_IGN);
88 (void) signal(SIGHUP, sighup_handler);
90 acquire_daemonlock(0);
95 setenv("PATH", _PATH_DEFPATH, 1);
98 /* if there are no debug flags turned on, fork as a daemon should.
105 (void) fprintf(stderr, "[%d] cron started\n", getpid());
107 if (daemon(1, 0) == -1) {
108 log_it("CRON",getpid(),"DEATH","can't become daemon");
113 acquire_daemonlock(0);
114 database.head = NULL;
115 database.tail = NULL;
116 database.mtime = (time_t) 0;
117 load_database(&database);
118 run_reboot_jobs(&database);
122 /* if (!(DebugFlags & DTEST)) */
123 # endif /*DEBUGGING*/
124 cron_sleep(&database);
126 load_database(&database);
130 cron_tick(&database);
146 for (u = db->head; u != NULL; u = u->next) {
147 for (e = u->crontab; e != NULL; e = e->next) {
148 if (e->flags & WHEN_REBOOT) {
153 (void) job_runqueue();
161 static struct tm lasttm;
162 static time_t diff = 0, /* time difference in seconds from the last offset change */
163 difflimit = 0; /* end point for the time zone correction */
164 struct tm otztm; /* time in the old time zone */
165 int otzminute, otzhour, otzdom, otzmonth, otzdow;
166 register struct tm *tm = localtime(&TargetTime);
167 register int minute, hour, dom, month, dow;
171 /* make 0-based values out of these so we can use them as indicies
173 minute = tm->tm_min -FIRST_MINUTE;
174 hour = tm->tm_hour -FIRST_HOUR;
175 dom = tm->tm_mday -FIRST_DOM;
176 month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
177 dow = tm->tm_wday -FIRST_DOW;
179 Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d)\n",
180 getpid(), minute, hour, dom, month, dow))
182 if (dst_enabled && last_time != 0
183 && TargetTime > last_time /* exclude stepping back */
184 && tm->tm_gmtoff != lasttm.tm_gmtoff ) {
186 diff = tm->tm_gmtoff - lasttm.tm_gmtoff;
188 if ( diff > 0 ) { /* ST->DST */
189 /* mark jobs for an earlier run */
190 difflimit = TargetTime + diff;
191 for (u = db->head; u != NULL; u = u->next) {
192 for (e = u->crontab; e != NULL; e = e->next) {
193 e->flags &= ~NOT_UNTIL;
194 if ( e->lastrun >= TargetTime )
196 /* not include the ends of hourly ranges */
197 if ( e->lastrun < TargetTime - 3600 )
203 } else { /* diff < 0 : DST->ST */
204 /* mark jobs for skipping */
205 difflimit = TargetTime - diff;
206 for (u = db->head; u != NULL; u = u->next) {
207 for (e = u->crontab; e != NULL; e = e->next) {
208 e->flags |= NOT_UNTIL;
216 /* if the time was reset of the end of special zone is reached */
217 if (last_time == 0 || TargetTime >= difflimit) {
218 /* disable the TZ switch checks */
221 for (u = db->head; u != NULL; u = u->next) {
222 for (e = u->crontab; e != NULL; e = e->next) {
223 e->flags &= ~(RUN_AT|NOT_UNTIL);
227 /* get the time in the old time zone */
228 time_t difftime = TargetTime + tm->tm_gmtoff - diff;
229 gmtime_r(&difftime, &otztm);
231 /* make 0-based values out of these so we can use them as indicies
233 otzminute = otztm.tm_min -FIRST_MINUTE;
234 otzhour = otztm.tm_hour -FIRST_HOUR;
235 otzdom = otztm.tm_mday -FIRST_DOM;
236 otzmonth = otztm.tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
237 otzdow = otztm.tm_wday -FIRST_DOW;
241 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
242 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
243 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
244 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
245 * like many bizarre things, it's the standard.
247 for (u = db->head; u != NULL; u = u->next) {
248 for (e = u->crontab; e != NULL; e = e->next) {
249 Debug(DSCH|DEXT, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
250 env_get("LOGNAME", e->envp),
251 e->uid, e->gid, e->cmd))
253 if ( diff != 0 && (e->flags & (RUN_AT|NOT_UNTIL)) ) {
254 if (bit_test(e->minute, otzminute)
255 && bit_test(e->hour, otzhour)
256 && bit_test(e->month, otzmonth)
257 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
258 ? (bit_test(e->dow,otzdow) && bit_test(e->dom,otzdom))
259 : (bit_test(e->dow,otzdow) || bit_test(e->dom,otzdom))
262 if ( e->flags & RUN_AT ) {
264 e->lastrun = TargetTime;
268 e->flags &= ~NOT_UNTIL;
269 } else if ( e->flags & NOT_UNTIL )
273 if (bit_test(e->minute, minute)
274 && bit_test(e->hour, hour)
275 && bit_test(e->month, month)
276 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
277 ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
278 : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
282 e->lastrun = TargetTime;
288 last_time = TargetTime;
293 /* the task here is to figure out how long it's going to be until :00 of the
294 * following minute and initialize TargetTime to this value. TargetTime
295 * will subsequently slide 60 seconds at a time, with correction applied
296 * implicitly in cron_sleep(). it would be nice to let cron execute in
297 * the "current minute" before going to sleep, but by restarting cron you
298 * could then get it to execute a given minute's jobs more than once.
299 * instead we have the chance of missing a minute's jobs completely, but
300 * that's something sysadmin's know to expect what with crashing computers..
304 register struct tm *tm;
306 TargetTime = time((time_t*)0);
307 tm = localtime(&TargetTime);
308 TargetTime += (60 - tm->tm_sec);
316 int seconds_to_wait = 0;
319 * Loop until we reach the top of the next minute, sleep when possible.
323 seconds_to_wait = (int) (TargetTime - time((time_t*)0));
326 * If the seconds_to_wait value is insane, jump the cron
329 if (seconds_to_wait < -600 || seconds_to_wait > 600) {
335 Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
336 getpid(), (long)TargetTime, seconds_to_wait))
339 * If we've run out of wait time or there are no jobs left
343 if (seconds_to_wait <= 0)
345 if (job_runqueue() == 0) {
346 Debug(DSCH, ("[%d] sleeping for %d seconds\n",
347 getpid(), seconds_to_wait))
349 sleep(seconds_to_wait);
355 /* if the time was changed abruptly, clear the flags related
356 * to the daylight time switch handling to avoid strange effects
368 for (u = db->head; u != NULL; u = u->next) {
369 for (e = u->crontab; e != NULL; e = e->next) {
370 e->flags &= ~(RUN_AT|NOT_UNTIL);
383 pid = waitpid(-1, &waiter, WNOHANG);
385 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
390 ("[%d] sigchld...no children\n", getpid()))
394 ("[%d] sigchld...no dead kids\n", getpid()))
398 ("[%d] sigchld...pid #%d died, stat=%d\n",
399 getpid(), pid, WEXITSTATUS(waiter)))
403 #endif /*USE_SIGCHLD*/
413 parse_args(argc, argv)
420 while ((argch = getopt(argc, argv, "j:J:osx:")) != -1) {
423 Jitter = strtoul(optarg, &endp, 10);
424 if (*optarg == '\0' || *endp != '\0' || Jitter > 60)
426 "bad value for jitter: %s", optarg);
429 RootJitter = strtoul(optarg, &endp, 10);
430 if (*optarg == '\0' || *endp != '\0' || RootJitter > 60)
432 "bad value for root jitter: %s", optarg);
441 if (!set_debug_flags(optarg))