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;
52 static int dont_daemonize = 0;
61 fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] "
62 "[-m mailto] [-n] [-s] [-o] [-x debugflag[,...]]\n");
64 fprintf(stderr, "\ndebugflags: ");
66 for(dflags = DebugFlagNames; *dflags; dflags++) {
67 fprintf(stderr, "%s ", *dflags);
69 fprintf(stderr, "\n");
78 char pidfile[MAX_FNAME];
79 char buf[MAX_TEMPSTR];
82 (void) snprintf(pidfile, sizeof(pidfile), PIDFILE, PIDDIR);
83 pfh = pidfile_open(pidfile, 0600, &otherpid);
85 if (errno == EEXIST) {
86 snprintf(buf, sizeof(buf),
87 "cron already running, pid: %d", otherpid);
89 snprintf(buf, sizeof(buf),
90 "can't open or create %s: %s", pidfile,
93 log_it("CRON", getpid(), "DEATH", buf);
94 errx(ERROR_EXIT, "%s", buf);
105 int secres1, secres2;
108 ProgramName = argv[0];
115 parse_args(argc, argv);
118 (void) signal(SIGCHLD, sigchld_handler);
120 (void) signal(SIGCLD, SIG_IGN);
122 (void) signal(SIGHUP, sighup_handler);
129 setenv("PATH", _PATH_DEFPATH, 1);
132 /* if there are no debug flags turned on, fork as a daemon should.
139 (void) fprintf(stderr, "[%d] cron started\n", getpid());
140 } else if (dont_daemonize == 0) {
141 if (daemon(1, 0) == -1) {
143 log_it("CRON",getpid(),"DEATH","can't become daemon");
148 if (madvise(NULL, 0, MADV_PROTECT) != 0)
149 log_it("CRON", getpid(), "WARNING", "madvise() failed");
152 database.head = NULL;
153 database.tail = NULL;
154 database.mtime = (time_t) 0;
155 load_database(&database);
156 secres1 = secres2 = run_at_secres(&database);
157 run_reboot_jobs(&database);
162 /* if (!(DebugFlags & DTEST)) */
163 # endif /*DEBUGGING*/
164 cron_sleep(&database, secres1);
166 if (secres1 == 0 || runnum % 60 == 0) {
167 load_database(&database);
168 secres2 = run_at_secres(&database);
169 if (secres2 != secres1) {
175 * Going from 1 sec to 60 sec res. If we
176 * are already at minute's boundary, so
177 * let it run, otherwise schedule for the
180 tm = localtime(&TargetTime);
181 if (tm->tm_sec > 0) {
191 cron_tick(&database, secres1);
193 /* sleep 1 or 60 seconds
195 TargetTime += (secres1 != 0) ? 1 : 60;
208 for (u = db->head; u != NULL; u = u->next) {
209 for (e = u->crontab; e != NULL; e = e->next) {
210 if (e->flags & WHEN_REBOOT) {
215 (void) job_runqueue();
220 cron_tick(cron_db *db, int secres)
222 static struct tm lasttm;
223 static time_t diff = 0, /* time difference in seconds from the last offset change */
224 difflimit = 0; /* end point for the time zone correction */
225 struct tm otztm; /* time in the old time zone */
226 int otzsecond, otzminute, otzhour, otzdom, otzmonth, otzdow;
227 register struct tm *tm = localtime(&TargetTime);
228 register int second, minute, hour, dom, month, dow;
232 /* make 0-based values out of these so we can use them as indices
234 second = (secres == 0) ? 0 : tm->tm_sec -FIRST_SECOND;
235 minute = tm->tm_min -FIRST_MINUTE;
236 hour = tm->tm_hour -FIRST_HOUR;
237 dom = tm->tm_mday -FIRST_DOM;
238 month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
239 dow = tm->tm_wday -FIRST_DOW;
241 Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d,%d)\n",
242 getpid(), second, minute, hour, dom, month, dow))
244 if (dst_enabled && last_time != 0
245 && TargetTime > last_time /* exclude stepping back */
246 && tm->tm_gmtoff != lasttm.tm_gmtoff ) {
248 diff = tm->tm_gmtoff - lasttm.tm_gmtoff;
250 if ( diff > 0 ) { /* ST->DST */
251 /* mark jobs for an earlier run */
252 difflimit = TargetTime + diff;
253 for (u = db->head; u != NULL; u = u->next) {
254 for (e = u->crontab; e != NULL; e = e->next) {
255 e->flags &= ~NOT_UNTIL;
256 if ( e->lastrun >= TargetTime )
258 /* not include the ends of hourly ranges */
259 if ( e->lastrun < TargetTime - 3600 )
265 } else { /* diff < 0 : DST->ST */
266 /* mark jobs for skipping */
267 difflimit = TargetTime - diff;
268 for (u = db->head; u != NULL; u = u->next) {
269 for (e = u->crontab; e != NULL; e = e->next) {
270 e->flags |= NOT_UNTIL;
278 /* if the time was reset of the end of special zone is reached */
279 if (last_time == 0 || TargetTime >= difflimit) {
280 /* disable the TZ switch checks */
283 for (u = db->head; u != NULL; u = u->next) {
284 for (e = u->crontab; e != NULL; e = e->next) {
285 e->flags &= ~(RUN_AT|NOT_UNTIL);
289 /* get the time in the old time zone */
290 time_t difftime = TargetTime + tm->tm_gmtoff - diff;
291 gmtime_r(&difftime, &otztm);
293 /* make 0-based values out of these so we can use them as indices
295 otzsecond = (secres == 0) ? 0 : otztm.tm_sec -FIRST_SECOND;
296 otzminute = otztm.tm_min -FIRST_MINUTE;
297 otzhour = otztm.tm_hour -FIRST_HOUR;
298 otzdom = otztm.tm_mday -FIRST_DOM;
299 otzmonth = otztm.tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
300 otzdow = otztm.tm_wday -FIRST_DOW;
304 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
305 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
306 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
307 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
308 * like many bizarre things, it's the standard.
310 for (u = db->head; u != NULL; u = u->next) {
311 for (e = u->crontab; e != NULL; e = e->next) {
312 Debug(DSCH|DEXT, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
313 env_get("LOGNAME", e->envp),
314 e->uid, e->gid, e->cmd))
316 if ( diff != 0 && (e->flags & (RUN_AT|NOT_UNTIL)) ) {
317 if (bit_test(e->second, otzsecond)
318 && bit_test(e->minute, otzminute)
319 && bit_test(e->hour, otzhour)
320 && bit_test(e->month, otzmonth)
321 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
322 ? (bit_test(e->dow,otzdow) && bit_test(e->dom,otzdom))
323 : (bit_test(e->dow,otzdow) || bit_test(e->dom,otzdom))
326 if ( e->flags & RUN_AT ) {
328 e->lastrun = TargetTime;
332 e->flags &= ~NOT_UNTIL;
333 } else if ( e->flags & NOT_UNTIL )
337 if (bit_test(e->second, second)
338 && bit_test(e->minute, minute)
339 && bit_test(e->hour, hour)
340 && bit_test(e->month, month)
341 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
342 ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
343 : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
347 e->lastrun = TargetTime;
353 last_time = TargetTime;
358 /* the task here is to figure out how long it's going to be until :00 of the
359 * following minute and initialize TargetTime to this value. TargetTime
360 * will subsequently slide 60 seconds at a time, with correction applied
361 * implicitly in cron_sleep(). it would be nice to let cron execute in
362 * the "current minute" before going to sleep, but by restarting cron you
363 * could then get it to execute a given minute's jobs more than once.
364 * instead we have the chance of missing a minute's jobs completely, but
365 * that's something sysadmin's know to expect what with crashing computers..
368 cron_sync(int secres) {
371 TargetTime = time((time_t*)0);
375 tm = localtime(&TargetTime);
376 TargetTime += (60 - tm->tm_sec);
381 timespec_subtract(struct timespec *result, struct timespec *x,
385 result->tv_sec -= y->tv_sec;
386 result->tv_nsec -= y->tv_nsec;
387 if (result->tv_nsec < 0) {
389 result->tv_nsec += 1000000000;
394 cron_sleep(cron_db *db, int secres)
398 struct timespec ctime, ttime, stime, remtime;
401 * Loop until we reach the top of the next minute, sleep when possible.
405 clock_gettime(CLOCK_REALTIME, &ctime);
406 ttime.tv_sec = TargetTime;
408 timespec_subtract(&stime, &ttime, &ctime);
411 * If the seconds_to_wait value is insane, jump the cron
414 if (stime.tv_sec < -600 || stime.tv_sec > 600) {
420 seconds_to_wait = (stime.tv_nsec > 0) ? stime.tv_sec + 1 :
423 Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
424 getpid(), (long)TargetTime, seconds_to_wait))
427 * If we've run out of wait time or there are no jobs left
431 if (stime.tv_sec < 0)
433 if (job_runqueue() == 0) {
434 Debug(DSCH, ("[%d] sleeping for %d seconds\n",
435 getpid(), seconds_to_wait))
438 rval = nanosleep(&stime, &remtime);
439 if (rval == 0 || errno != EINTR)
441 stime.tv_sec = remtime.tv_sec;
442 stime.tv_nsec = remtime.tv_nsec;
449 /* if the time was changed abruptly, clear the flags related
450 * to the daylight time switch handling to avoid strange effects
462 for (u = db->head; u != NULL; u = u->next) {
463 for (e = u->crontab; e != NULL; e = e->next) {
464 e->flags &= ~(RUN_AT|NOT_UNTIL);
471 sigchld_handler(int x)
478 pid = waitpid(-1, &waiter, WNOHANG);
480 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
485 ("[%d] sigchld...no children\n", getpid()))
489 ("[%d] sigchld...no dead kids\n", getpid()))
493 ("[%d] sigchld...pid #%d died, stat=%d\n",
494 getpid(), pid, WEXITSTATUS(waiter)))
498 #endif /*USE_SIGCHLD*/
502 sighup_handler(int x)
509 parse_args(argc, argv)
516 while ((argch = getopt(argc, argv, "j:J:m:nosx:")) != -1) {
519 Jitter = strtoul(optarg, &endp, 10);
520 if (*optarg == '\0' || *endp != '\0' || Jitter > 60)
522 "bad value for jitter: %s", optarg);
525 RootJitter = strtoul(optarg, &endp, 10);
526 if (*optarg == '\0' || *endp != '\0' || RootJitter > 60)
528 "bad value for root jitter: %s", optarg);
543 if (!set_debug_flags(optarg))
553 run_at_secres(cron_db *db)
558 for (u = db->head; u != NULL; u = u->next) {
559 for (e = u->crontab; e != NULL; e = e->next) {
560 if ((e->flags & SEC_RES) != 0)