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 *),
40 cron_sleep(cron_db *),
41 cron_clean(cron_db *),
46 parse_args(int c, char *v[]);
48 static time_t last_time = 0;
49 static int dst_enabled = 0;
56 fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] "
57 "[-m mailto] [-s] [-o] [-x debugflag[,...]]\n");
58 fprintf(stderr, "\ndebugflags: ");
60 for(dflags = DebugFlagNames; *dflags; dflags++) {
61 fprintf(stderr, "%s ", *dflags);
63 fprintf(stderr, "\n");
71 char pidfile[MAX_FNAME];
72 char buf[MAX_TEMPSTR];
75 (void) snprintf(pidfile, sizeof(pidfile), PIDFILE, PIDDIR);
76 pfh = pidfile_open(pidfile, 0600, &otherpid);
78 if (errno == EEXIST) {
79 snprintf(buf, sizeof(buf),
80 "cron already running, pid: %d", otherpid);
82 snprintf(buf, sizeof(buf),
83 "can't open or create %s: %s", pidfile,
86 log_it("CRON", getpid(), "DEATH", buf);
87 errx(ERROR_EXIT, "%s", buf);
98 ProgramName = argv[0];
105 parse_args(argc, argv);
108 (void) signal(SIGCHLD, sigchld_handler);
110 (void) signal(SIGCLD, SIG_IGN);
112 (void) signal(SIGHUP, sighup_handler);
119 setenv("PATH", _PATH_DEFPATH, 1);
122 /* if there are no debug flags turned on, fork as a daemon should.
129 (void) fprintf(stderr, "[%d] cron started\n", getpid());
131 if (daemon(1, 0) == -1) {
133 log_it("CRON",getpid(),"DEATH","can't become daemon");
138 if (madvise(NULL, 0, MADV_PROTECT) != 0)
139 log_it("CRON", getpid(), "WARNING", "madvise() failed");
142 database.head = NULL;
143 database.tail = NULL;
144 database.mtime = (time_t) 0;
145 load_database(&database);
146 run_reboot_jobs(&database);
150 /* if (!(DebugFlags & DTEST)) */
151 # endif /*DEBUGGING*/
152 cron_sleep(&database);
154 load_database(&database);
158 cron_tick(&database);
174 for (u = db->head; u != NULL; u = u->next) {
175 for (e = u->crontab; e != NULL; e = e->next) {
176 if (e->flags & WHEN_REBOOT) {
181 (void) job_runqueue();
189 static struct tm lasttm;
190 static time_t diff = 0, /* time difference in seconds from the last offset change */
191 difflimit = 0; /* end point for the time zone correction */
192 struct tm otztm; /* time in the old time zone */
193 int otzminute, otzhour, otzdom, otzmonth, otzdow;
194 register struct tm *tm = localtime(&TargetTime);
195 register int minute, hour, dom, month, dow;
199 /* make 0-based values out of these so we can use them as indicies
201 minute = tm->tm_min -FIRST_MINUTE;
202 hour = tm->tm_hour -FIRST_HOUR;
203 dom = tm->tm_mday -FIRST_DOM;
204 month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
205 dow = tm->tm_wday -FIRST_DOW;
207 Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d)\n",
208 getpid(), minute, hour, dom, month, dow))
210 if (dst_enabled && last_time != 0
211 && TargetTime > last_time /* exclude stepping back */
212 && tm->tm_gmtoff != lasttm.tm_gmtoff ) {
214 diff = tm->tm_gmtoff - lasttm.tm_gmtoff;
216 if ( diff > 0 ) { /* ST->DST */
217 /* mark jobs for an earlier run */
218 difflimit = TargetTime + diff;
219 for (u = db->head; u != NULL; u = u->next) {
220 for (e = u->crontab; e != NULL; e = e->next) {
221 e->flags &= ~NOT_UNTIL;
222 if ( e->lastrun >= TargetTime )
224 /* not include the ends of hourly ranges */
225 if ( e->lastrun < TargetTime - 3600 )
231 } else { /* diff < 0 : DST->ST */
232 /* mark jobs for skipping */
233 difflimit = TargetTime - diff;
234 for (u = db->head; u != NULL; u = u->next) {
235 for (e = u->crontab; e != NULL; e = e->next) {
236 e->flags |= NOT_UNTIL;
244 /* if the time was reset of the end of special zone is reached */
245 if (last_time == 0 || TargetTime >= difflimit) {
246 /* disable the TZ switch checks */
249 for (u = db->head; u != NULL; u = u->next) {
250 for (e = u->crontab; e != NULL; e = e->next) {
251 e->flags &= ~(RUN_AT|NOT_UNTIL);
255 /* get the time in the old time zone */
256 time_t difftime = TargetTime + tm->tm_gmtoff - diff;
257 gmtime_r(&difftime, &otztm);
259 /* make 0-based values out of these so we can use them as indicies
261 otzminute = otztm.tm_min -FIRST_MINUTE;
262 otzhour = otztm.tm_hour -FIRST_HOUR;
263 otzdom = otztm.tm_mday -FIRST_DOM;
264 otzmonth = otztm.tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
265 otzdow = otztm.tm_wday -FIRST_DOW;
269 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
270 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
271 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
272 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
273 * like many bizarre things, it's the standard.
275 for (u = db->head; u != NULL; u = u->next) {
276 for (e = u->crontab; e != NULL; e = e->next) {
277 Debug(DSCH|DEXT, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
278 env_get("LOGNAME", e->envp),
279 e->uid, e->gid, e->cmd))
281 if ( diff != 0 && (e->flags & (RUN_AT|NOT_UNTIL)) ) {
282 if (bit_test(e->minute, otzminute)
283 && bit_test(e->hour, otzhour)
284 && bit_test(e->month, otzmonth)
285 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
286 ? (bit_test(e->dow,otzdow) && bit_test(e->dom,otzdom))
287 : (bit_test(e->dow,otzdow) || bit_test(e->dom,otzdom))
290 if ( e->flags & RUN_AT ) {
292 e->lastrun = TargetTime;
296 e->flags &= ~NOT_UNTIL;
297 } else if ( e->flags & NOT_UNTIL )
301 if (bit_test(e->minute, minute)
302 && bit_test(e->hour, hour)
303 && bit_test(e->month, month)
304 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
305 ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
306 : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
310 e->lastrun = TargetTime;
316 last_time = TargetTime;
321 /* the task here is to figure out how long it's going to be until :00 of the
322 * following minute and initialize TargetTime to this value. TargetTime
323 * will subsequently slide 60 seconds at a time, with correction applied
324 * implicitly in cron_sleep(). it would be nice to let cron execute in
325 * the "current minute" before going to sleep, but by restarting cron you
326 * could then get it to execute a given minute's jobs more than once.
327 * instead we have the chance of missing a minute's jobs completely, but
328 * that's something sysadmin's know to expect what with crashing computers..
332 register struct tm *tm;
334 TargetTime = time((time_t*)0);
335 tm = localtime(&TargetTime);
336 TargetTime += (60 - tm->tm_sec);
344 int seconds_to_wait = 0;
347 * Loop until we reach the top of the next minute, sleep when possible.
351 seconds_to_wait = (int) (TargetTime - time((time_t*)0));
354 * If the seconds_to_wait value is insane, jump the cron
357 if (seconds_to_wait < -600 || seconds_to_wait > 600) {
363 Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
364 getpid(), (long)TargetTime, seconds_to_wait))
367 * If we've run out of wait time or there are no jobs left
371 if (seconds_to_wait <= 0)
373 if (job_runqueue() == 0) {
374 Debug(DSCH, ("[%d] sleeping for %d seconds\n",
375 getpid(), seconds_to_wait))
377 sleep(seconds_to_wait);
383 /* if the time was changed abruptly, clear the flags related
384 * to the daylight time switch handling to avoid strange effects
396 for (u = db->head; u != NULL; u = u->next) {
397 for (e = u->crontab; e != NULL; e = e->next) {
398 e->flags &= ~(RUN_AT|NOT_UNTIL);
405 sigchld_handler(int x)
412 pid = waitpid(-1, &waiter, WNOHANG);
414 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
419 ("[%d] sigchld...no children\n", getpid()))
423 ("[%d] sigchld...no dead kids\n", getpid()))
427 ("[%d] sigchld...pid #%d died, stat=%d\n",
428 getpid(), pid, WEXITSTATUS(waiter)))
432 #endif /*USE_SIGCHLD*/
436 sighup_handler(int x)
443 parse_args(argc, argv)
450 while ((argch = getopt(argc, argv, "j:J:m:osx:")) != -1) {
453 Jitter = strtoul(optarg, &endp, 10);
454 if (*optarg == '\0' || *endp != '\0' || Jitter > 60)
456 "bad value for jitter: %s", optarg);
459 RootJitter = strtoul(optarg, &endp, 10);
460 if (*optarg == '\0' || *endp != '\0' || RootJitter > 60)
462 "bad value for root jitter: %s", optarg);
474 if (!set_debug_flags(optarg))