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[] =
28 #include <sys/signal.h>
30 # include <sys/time.h>
36 static void usage(void),
37 run_reboot_jobs(cron_db *),
38 cron_tick(cron_db *, int),
40 cron_sleep(cron_db *, int),
41 cron_clean(cron_db *),
46 parse_args(int c, char *v[]);
48 static int run_at_secres(cron_db *);
50 static time_t last_time = 0;
51 static int dst_enabled = 0;
60 fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] "
61 "[-m mailto] [-s] [-o] [-x debugflag[,...]]\n");
63 fprintf(stderr, "\ndebugflags: ");
65 for(dflags = DebugFlagNames; *dflags; dflags++) {
66 fprintf(stderr, "%s ", *dflags);
68 fprintf(stderr, "\n");
77 char pidfile[MAX_FNAME];
78 char buf[MAX_TEMPSTR];
81 (void) snprintf(pidfile, sizeof(pidfile), PIDFILE, PIDDIR);
82 pfh = pidfile_open(pidfile, 0600, &otherpid);
84 if (errno == EEXIST) {
85 snprintf(buf, sizeof(buf),
86 "cron already running, pid: %d", otherpid);
88 snprintf(buf, sizeof(buf),
89 "can't open or create %s: %s", pidfile,
92 log_it("CRON", getpid(), "DEATH", buf);
93 errx(ERROR_EXIT, "%s", buf);
104 int secres1, secres2;
107 ProgramName = argv[0];
114 parse_args(argc, argv);
117 (void) signal(SIGCHLD, sigchld_handler);
119 (void) signal(SIGCLD, SIG_IGN);
121 (void) signal(SIGHUP, sighup_handler);
128 setenv("PATH", _PATH_DEFPATH, 1);
131 /* if there are no debug flags turned on, fork as a daemon should.
138 (void) fprintf(stderr, "[%d] cron started\n", getpid());
140 if (daemon(1, 0) == -1) {
142 log_it("CRON",getpid(),"DEATH","can't become daemon");
147 if (madvise(NULL, 0, MADV_PROTECT) != 0)
148 log_it("CRON", getpid(), "WARNING", "madvise() failed");
151 database.head = NULL;
152 database.tail = NULL;
153 database.mtime = (time_t) 0;
154 load_database(&database);
155 secres1 = secres2 = run_at_secres(&database);
156 run_reboot_jobs(&database);
161 /* if (!(DebugFlags & DTEST)) */
162 # endif /*DEBUGGING*/
163 cron_sleep(&database, secres1);
165 if (secres1 == 0 || runnum % 60 == 0) {
166 load_database(&database);
167 secres2 = run_at_secres(&database);
168 if (secres2 != secres1) {
174 * Going from 1 sec to 60 sec res. If we
175 * are already at minute's boundary, so
176 * let it run, otherwise schedule for the
179 tm = localtime(&TargetTime);
180 if (tm->tm_sec > 0) {
190 cron_tick(&database, secres1);
192 /* sleep 1 or 60 seconds
194 TargetTime += (secres1 != 0) ? 1 : 60;
207 for (u = db->head; u != NULL; u = u->next) {
208 for (e = u->crontab; e != NULL; e = e->next) {
209 if (e->flags & WHEN_REBOOT) {
214 (void) job_runqueue();
219 cron_tick(cron_db *db, int secres)
221 static struct tm lasttm;
222 static time_t diff = 0, /* time difference in seconds from the last offset change */
223 difflimit = 0; /* end point for the time zone correction */
224 struct tm otztm; /* time in the old time zone */
225 int otzsecond, otzminute, otzhour, otzdom, otzmonth, otzdow;
226 register struct tm *tm = localtime(&TargetTime);
227 register int second, minute, hour, dom, month, dow;
231 /* make 0-based values out of these so we can use them as indicies
233 second = (secres == 0) ? 0 : tm->tm_sec -FIRST_SECOND;
234 minute = tm->tm_min -FIRST_MINUTE;
235 hour = tm->tm_hour -FIRST_HOUR;
236 dom = tm->tm_mday -FIRST_DOM;
237 month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
238 dow = tm->tm_wday -FIRST_DOW;
240 Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d,%d)\n",
241 getpid(), second, minute, hour, dom, month, dow))
243 if (dst_enabled && last_time != 0
244 && TargetTime > last_time /* exclude stepping back */
245 && tm->tm_gmtoff != lasttm.tm_gmtoff ) {
247 diff = tm->tm_gmtoff - lasttm.tm_gmtoff;
249 if ( diff > 0 ) { /* ST->DST */
250 /* mark jobs for an earlier run */
251 difflimit = TargetTime + diff;
252 for (u = db->head; u != NULL; u = u->next) {
253 for (e = u->crontab; e != NULL; e = e->next) {
254 e->flags &= ~NOT_UNTIL;
255 if ( e->lastrun >= TargetTime )
257 /* not include the ends of hourly ranges */
258 if ( e->lastrun < TargetTime - 3600 )
264 } else { /* diff < 0 : DST->ST */
265 /* mark jobs for skipping */
266 difflimit = TargetTime - diff;
267 for (u = db->head; u != NULL; u = u->next) {
268 for (e = u->crontab; e != NULL; e = e->next) {
269 e->flags |= NOT_UNTIL;
277 /* if the time was reset of the end of special zone is reached */
278 if (last_time == 0 || TargetTime >= difflimit) {
279 /* disable the TZ switch checks */
282 for (u = db->head; u != NULL; u = u->next) {
283 for (e = u->crontab; e != NULL; e = e->next) {
284 e->flags &= ~(RUN_AT|NOT_UNTIL);
288 /* get the time in the old time zone */
289 time_t difftime = TargetTime + tm->tm_gmtoff - diff;
290 gmtime_r(&difftime, &otztm);
292 /* make 0-based values out of these so we can use them as indicies
294 otzsecond = (secres == 0) ? 0 : otztm.tm_sec -FIRST_SECOND;
295 otzminute = otztm.tm_min -FIRST_MINUTE;
296 otzhour = otztm.tm_hour -FIRST_HOUR;
297 otzdom = otztm.tm_mday -FIRST_DOM;
298 otzmonth = otztm.tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
299 otzdow = otztm.tm_wday -FIRST_DOW;
303 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
304 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
305 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
306 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
307 * like many bizarre things, it's the standard.
309 for (u = db->head; u != NULL; u = u->next) {
310 for (e = u->crontab; e != NULL; e = e->next) {
311 Debug(DSCH|DEXT, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
312 env_get("LOGNAME", e->envp),
313 e->uid, e->gid, e->cmd))
315 if ( diff != 0 && (e->flags & (RUN_AT|NOT_UNTIL)) ) {
316 if (bit_test(e->second, otzsecond)
317 && bit_test(e->minute, otzminute)
318 && bit_test(e->hour, otzhour)
319 && bit_test(e->month, otzmonth)
320 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
321 ? (bit_test(e->dow,otzdow) && bit_test(e->dom,otzdom))
322 : (bit_test(e->dow,otzdow) || bit_test(e->dom,otzdom))
325 if ( e->flags & RUN_AT ) {
327 e->lastrun = TargetTime;
331 e->flags &= ~NOT_UNTIL;
332 } else if ( e->flags & NOT_UNTIL )
336 if (bit_test(e->second, second)
337 && bit_test(e->minute, minute)
338 && bit_test(e->hour, hour)
339 && bit_test(e->month, month)
340 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
341 ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
342 : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
346 e->lastrun = TargetTime;
352 last_time = TargetTime;
357 /* the task here is to figure out how long it's going to be until :00 of the
358 * following minute and initialize TargetTime to this value. TargetTime
359 * will subsequently slide 60 seconds at a time, with correction applied
360 * implicitly in cron_sleep(). it would be nice to let cron execute in
361 * the "current minute" before going to sleep, but by restarting cron you
362 * could then get it to execute a given minute's jobs more than once.
363 * instead we have the chance of missing a minute's jobs completely, but
364 * that's something sysadmin's know to expect what with crashing computers..
367 cron_sync(int secres) {
370 TargetTime = time((time_t*)0);
374 tm = localtime(&TargetTime);
375 TargetTime += (60 - tm->tm_sec);
380 timespec_subtract(struct timespec *result, struct timespec *x,
384 result->tv_sec -= y->tv_sec;
385 result->tv_nsec -= y->tv_nsec;
386 if (result->tv_nsec < 0) {
388 result->tv_nsec += 1000000000;
393 cron_sleep(cron_db *db, int secres)
397 struct timespec ctime, ttime, stime, remtime;
400 * Loop until we reach the top of the next minute, sleep when possible.
404 clock_gettime(CLOCK_REALTIME, &ctime);
405 ttime.tv_sec = TargetTime;
407 timespec_subtract(&stime, &ttime, &ctime);
410 * If the seconds_to_wait value is insane, jump the cron
413 if (stime.tv_sec < -600 || stime.tv_sec > 600) {
419 seconds_to_wait = (stime.tv_nsec > 0) ? stime.tv_sec + 1 :
422 Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
423 getpid(), (long)TargetTime, seconds_to_wait))
426 * If we've run out of wait time or there are no jobs left
430 if (stime.tv_sec < 0)
432 if (job_runqueue() == 0) {
433 Debug(DSCH, ("[%d] sleeping for %d seconds\n",
434 getpid(), seconds_to_wait))
437 rval = nanosleep(&stime, &remtime);
438 if (rval == 0 || errno != EINTR)
440 stime.tv_sec = remtime.tv_sec;
441 stime.tv_nsec = remtime.tv_nsec;
448 /* if the time was changed abruptly, clear the flags related
449 * to the daylight time switch handling to avoid strange effects
461 for (u = db->head; u != NULL; u = u->next) {
462 for (e = u->crontab; e != NULL; e = e->next) {
463 e->flags &= ~(RUN_AT|NOT_UNTIL);
470 sigchld_handler(int x)
477 pid = waitpid(-1, &waiter, WNOHANG);
479 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
484 ("[%d] sigchld...no children\n", getpid()))
488 ("[%d] sigchld...no dead kids\n", getpid()))
492 ("[%d] sigchld...pid #%d died, stat=%d\n",
493 getpid(), pid, WEXITSTATUS(waiter)))
497 #endif /*USE_SIGCHLD*/
501 sighup_handler(int x)
508 parse_args(argc, argv)
515 while ((argch = getopt(argc, argv, "j:J:m:osx:")) != -1) {
518 Jitter = strtoul(optarg, &endp, 10);
519 if (*optarg == '\0' || *endp != '\0' || Jitter > 60)
521 "bad value for jitter: %s", optarg);
524 RootJitter = strtoul(optarg, &endp, 10);
525 if (*optarg == '\0' || *endp != '\0' || RootJitter > 60)
527 "bad value for root jitter: %s", optarg);
539 if (!set_debug_flags(optarg))
549 run_at_secres(cron_db *db)
554 for (u = db->head; u != NULL; u = u->next) {
555 for (e = u->crontab; e != NULL; e = e->next) {
556 if ((e->flags & SEC_RES) != 0)