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
17 * $FreeBSD: src/usr.sbin/cron/cron/cron.c,v 1.9.2.2 2001/05/28 23:37:26 babkin Exp $
18 * $DragonFly: src/usr.sbin/cron/cron/cron.c,v 1.5 2004/03/10 18:27:26 dillon Exp $
25 #include <sys/signal.h>
27 # include <sys/time.h>
33 static void usage(void),
34 run_reboot_jobs(cron_db *),
37 cron_sleep(cron_db *),
38 cron_clean(cron_db *),
43 parse_args(int c, char *v[]);
45 static time_t last_time = 0;
46 static int dst_enabled = 0;
53 fprintf(stderr, "usage: cron [-s] [-o] [-x debugflag[,...]]\n");
54 fprintf(stderr, "\ndebugflags: ");
56 for(dflags = DebugFlagNames; *dflags; dflags++) {
57 fprintf(stderr, "%s ", *dflags);
59 fprintf(stderr, "\n");
66 main(int argc, char **argv)
70 ProgramName = argv[0];
77 parse_args(argc, argv);
80 (void) signal(SIGCHLD, sigchld_handler);
82 (void) signal(SIGCLD, SIG_IGN);
84 (void) signal(SIGHUP, sighup_handler);
86 acquire_daemonlock(0);
91 setenv("PATH", _PATH_DEFPATH, 1);
94 /* if there are no debug flags turned on, fork as a daemon should.
101 (void) fprintf(stderr, "[%d] cron started\n", getpid());
103 if (daemon(1, 0) == -1) {
104 log_it("CRON",getpid(),"DEATH","can't become daemon");
109 acquire_daemonlock(0);
110 database.head = NULL;
111 database.tail = NULL;
112 database.mtime = (time_t) 0;
113 load_database(&database);
114 run_reboot_jobs(&database);
118 /* if (!(DebugFlags & DTEST)) */
119 # endif /*DEBUGGING*/
120 cron_sleep(&database);
122 load_database(&database);
126 cron_tick(&database);
136 run_reboot_jobs(cron_db *db)
141 for (u = db->head; u != NULL; u = u->next) {
142 for (e = u->crontab; e != NULL; e = e->next) {
143 if (e->flags & WHEN_REBOOT) {
148 (void) job_runqueue();
153 cron_tick(cron_db *db)
155 static struct tm lasttm;
156 static time_t diff; /* delta time from the last offset change */
157 static time_t difflimit; /* end point for the time zone correction */
158 struct tm otztm; /* time in the old time zone */
159 int otzminute, otzhour, otzdom, otzmonth, otzdow;
161 int minute, hour, dom, month, dow;
165 tm = localtime(&TargetTime);
166 /* make 0-based values out of these so we can use them as indicies
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;
174 Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d)\n",
175 getpid(), minute, hour, dom, month, dow))
177 if (dst_enabled && last_time != 0
178 && TargetTime > last_time /* exclude stepping back */
179 && tm->tm_gmtoff != lasttm.tm_gmtoff ) {
181 diff = tm->tm_gmtoff - lasttm.tm_gmtoff;
183 if ( diff > 0 ) { /* ST->DST */
184 /* mark jobs for an earlier run */
185 difflimit = TargetTime + diff;
186 for (u = db->head; u != NULL; u = u->next) {
187 for (e = u->crontab; e != NULL; e = e->next) {
188 e->flags &= ~NOT_UNTIL;
189 if ( e->lastrun >= TargetTime )
191 /* not include the ends of hourly ranges */
192 if ( e->lastrun < TargetTime - 3600 )
198 } else { /* diff < 0 : DST->ST */
199 /* mark jobs for skipping */
200 difflimit = TargetTime - diff;
201 for (u = db->head; u != NULL; u = u->next) {
202 for (e = u->crontab; e != NULL; e = e->next) {
203 e->flags |= NOT_UNTIL;
211 /* if the time was reset of the end of special zone is reached */
212 if (last_time == 0 || TargetTime >= difflimit) {
213 /* disable the TZ switch checks */
216 for (u = db->head; u != NULL; u = u->next) {
217 for (e = u->crontab; e != NULL; e = e->next) {
218 e->flags &= ~(RUN_AT|NOT_UNTIL);
222 /* get the time in the old time zone */
223 time_t difftime = TargetTime + tm->tm_gmtoff - diff;
224 gmtime_r(&difftime, &otztm);
226 /* make 0-based values out of these so we can use them as indicies
228 otzminute = otztm.tm_min -FIRST_MINUTE;
229 otzhour = otztm.tm_hour -FIRST_HOUR;
230 otzdom = otztm.tm_mday -FIRST_DOM;
231 otzmonth = otztm.tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
232 otzdow = otztm.tm_wday -FIRST_DOW;
236 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
237 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
238 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
239 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
240 * like many bizarre things, it's the standard.
242 for (u = db->head; u != NULL; u = u->next) {
243 for (e = u->crontab; e != NULL; e = e->next) {
244 Debug(DSCH|DEXT, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
245 env_get("LOGNAME", e->envp),
246 e->uid, e->gid, e->cmd))
248 if ( diff != 0 && (e->flags & (RUN_AT|NOT_UNTIL)) ) {
249 if (bit_test(e->minute, otzminute)
250 && bit_test(e->hour, otzhour)
251 && bit_test(e->month, otzmonth)
252 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
253 ? (bit_test(e->dow,otzdow) && bit_test(e->dom,otzdom))
254 : (bit_test(e->dow,otzdow) || bit_test(e->dom,otzdom))
257 if ( e->flags & RUN_AT ) {
259 e->lastrun = TargetTime;
263 e->flags &= ~NOT_UNTIL;
264 } else if ( e->flags & NOT_UNTIL )
268 if (bit_test(e->minute, minute)
269 && bit_test(e->hour, hour)
270 && bit_test(e->month, month)
271 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
272 ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
273 : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
277 e->lastrun = TargetTime;
283 last_time = TargetTime;
288 /* the task here is to figure out how long it's going to be until :00 of the
289 * following minute and initialize TargetTime to this value. TargetTime
290 * will subsequently slide 60 seconds at a time, with correction applied
291 * implicitly in cron_sleep(). it would be nice to let cron execute in
292 * the "current minute" before going to sleep, but by restarting cron you
293 * could then get it to execute a given minute's jobs more than once.
294 * instead we have the chance of missing a minute's jobs completely, but
295 * that's something sysadmin's know to expect what with crashing computers..
302 TargetTime = time((time_t*)0);
303 tm = localtime(&TargetTime);
304 TargetTime += (60 - tm->tm_sec);
309 cron_sleep(cron_db *db)
311 int seconds_to_wait = 0;
314 * Loop until we reach the top of the next minute, sleep when possible.
318 seconds_to_wait = (int) (TargetTime - time((time_t*)0));
321 * If the seconds_to_wait value is insane, jump the cron
324 if (seconds_to_wait < -600 || seconds_to_wait > 600) {
330 Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
331 getpid(), (long)TargetTime, seconds_to_wait))
334 * If we've run out of wait time or there are no jobs left
338 if (seconds_to_wait <= 0)
340 if (job_runqueue() == 0) {
341 Debug(DSCH, ("[%d] sleeping for %d seconds\n",
342 getpid(), seconds_to_wait))
344 sleep(seconds_to_wait);
350 /* if the time was changed abruptly, clear the flags related
351 * to the daylight time switch handling to avoid strange effects
355 cron_clean(cron_db *db)
362 for (u = db->head; u != NULL; u = u->next) {
363 for (e = u->crontab; e != NULL; e = e->next) {
364 e->flags &= ~(RUN_AT|NOT_UNTIL);
371 sigchld_handler(int x)
378 pid = waitpid(-1, &waiter, WNOHANG);
380 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
385 ("[%d] sigchld...no children\n", getpid()))
389 ("[%d] sigchld...no dead kids\n", getpid()))
393 ("[%d] sigchld...pid #%d died, stat=%d\n",
394 getpid(), pid, WEXITSTATUS(waiter)))
398 #endif /*USE_SIGCHLD*/
402 sighup_handler(int x)
409 parse_args(int argc, char **argv)
413 while ((argch = getopt(argc, argv, "osx:")) != -1) {
422 if (!set_debug_flags(optarg))