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.3 2003/11/03 19:31:36 eirikn 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;
52 fprintf(stderr, "usage: cron [-s] [-o] [-x debugflag[,...]]\n");
53 fprintf(stderr, "\ndebugflags: ");
55 for(dflags = DebugFlagNames; *dflags; dflags++) {
56 fprintf(stderr, "%s ", *dflags);
58 fprintf(stderr, "\n");
71 ProgramName = argv[0];
78 parse_args(argc, argv);
81 (void) signal(SIGCHLD, sigchld_handler);
83 (void) signal(SIGCLD, SIG_IGN);
85 (void) signal(SIGHUP, sighup_handler);
87 acquire_daemonlock(0);
92 setenv("PATH", _PATH_DEFPATH, 1);
95 /* if there are no debug flags turned on, fork as a daemon should.
102 (void) fprintf(stderr, "[%d] cron started\n", getpid());
104 if (daemon(1, 0) == -1) {
105 log_it("CRON",getpid(),"DEATH","can't become daemon");
110 acquire_daemonlock(0);
111 database.head = NULL;
112 database.tail = NULL;
113 database.mtime = (time_t) 0;
114 load_database(&database);
115 run_reboot_jobs(&database);
119 /* if (!(DebugFlags & DTEST)) */
120 # endif /*DEBUGGING*/
121 cron_sleep(&database);
123 load_database(&database);
127 cron_tick(&database);
143 for (u = db->head; u != NULL; u = u->next) {
144 for (e = u->crontab; e != NULL; e = e->next) {
145 if (e->flags & WHEN_REBOOT) {
150 (void) job_runqueue();
158 static struct tm lasttm;
159 static time_t diff = 0, /* time difference in seconds from the last offset change */
160 difflimit = 0; /* end point for the time zone correction */
161 struct tm otztm; /* time in the old time zone */
162 int otzminute, otzhour, otzdom, otzmonth, otzdow;
163 register struct tm *tm = localtime(&TargetTime);
164 register int minute, hour, dom, month, dow;
168 /* make 0-based values out of these so we can use them as indicies
170 minute = tm->tm_min -FIRST_MINUTE;
171 hour = tm->tm_hour -FIRST_HOUR;
172 dom = tm->tm_mday -FIRST_DOM;
173 month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
174 dow = tm->tm_wday -FIRST_DOW;
176 Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d)\n",
177 getpid(), minute, hour, dom, month, dow))
179 if (dst_enabled && last_time != 0
180 && TargetTime > last_time /* exclude stepping back */
181 && tm->tm_gmtoff != lasttm.tm_gmtoff ) {
183 diff = tm->tm_gmtoff - lasttm.tm_gmtoff;
185 if ( diff > 0 ) { /* ST->DST */
186 /* mark jobs for an earlier run */
187 difflimit = TargetTime + diff;
188 for (u = db->head; u != NULL; u = u->next) {
189 for (e = u->crontab; e != NULL; e = e->next) {
190 e->flags &= ~NOT_UNTIL;
191 if ( e->lastrun >= TargetTime )
193 /* not include the ends of hourly ranges */
194 if ( e->lastrun < TargetTime - 3600 )
200 } else { /* diff < 0 : DST->ST */
201 /* mark jobs for skipping */
202 difflimit = TargetTime - diff;
203 for (u = db->head; u != NULL; u = u->next) {
204 for (e = u->crontab; e != NULL; e = e->next) {
205 e->flags |= NOT_UNTIL;
213 /* if the time was reset of the end of special zone is reached */
214 if (last_time == 0 || TargetTime >= difflimit) {
215 /* disable the TZ switch checks */
218 for (u = db->head; u != NULL; u = u->next) {
219 for (e = u->crontab; e != NULL; e = e->next) {
220 e->flags &= ~(RUN_AT|NOT_UNTIL);
224 /* get the time in the old time zone */
225 time_t difftime = TargetTime + tm->tm_gmtoff - diff;
226 gmtime_r(&difftime, &otztm);
228 /* make 0-based values out of these so we can use them as indicies
230 otzminute = otztm.tm_min -FIRST_MINUTE;
231 otzhour = otztm.tm_hour -FIRST_HOUR;
232 otzdom = otztm.tm_mday -FIRST_DOM;
233 otzmonth = otztm.tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
234 otzdow = otztm.tm_wday -FIRST_DOW;
238 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
239 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
240 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
241 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
242 * like many bizarre things, it's the standard.
244 for (u = db->head; u != NULL; u = u->next) {
245 for (e = u->crontab; e != NULL; e = e->next) {
246 Debug(DSCH|DEXT, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
247 env_get("LOGNAME", e->envp),
248 e->uid, e->gid, e->cmd))
250 if ( diff != 0 && (e->flags & (RUN_AT|NOT_UNTIL)) ) {
251 if (bit_test(e->minute, otzminute)
252 && bit_test(e->hour, otzhour)
253 && bit_test(e->month, otzmonth)
254 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
255 ? (bit_test(e->dow,otzdow) && bit_test(e->dom,otzdom))
256 : (bit_test(e->dow,otzdow) || bit_test(e->dom,otzdom))
259 if ( e->flags & RUN_AT ) {
261 e->lastrun = TargetTime;
265 e->flags &= ~NOT_UNTIL;
266 } else if ( e->flags & NOT_UNTIL )
270 if (bit_test(e->minute, minute)
271 && bit_test(e->hour, hour)
272 && bit_test(e->month, month)
273 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
274 ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
275 : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
279 e->lastrun = TargetTime;
285 last_time = TargetTime;
290 /* the task here is to figure out how long it's going to be until :00 of the
291 * following minute and initialize TargetTime to this value. TargetTime
292 * will subsequently slide 60 seconds at a time, with correction applied
293 * implicitly in cron_sleep(). it would be nice to let cron execute in
294 * the "current minute" before going to sleep, but by restarting cron you
295 * could then get it to execute a given minute's jobs more than once.
296 * instead we have the chance of missing a minute's jobs completely, but
297 * that's something sysadmin's know to expect what with crashing computers..
301 register struct tm *tm;
303 TargetTime = time((time_t*)0);
304 tm = localtime(&TargetTime);
305 TargetTime += (60 - tm->tm_sec);
313 int seconds_to_wait = 0;
316 * Loop until we reach the top of the next minute, sleep when possible.
320 seconds_to_wait = (int) (TargetTime - time((time_t*)0));
323 * If the seconds_to_wait value is insane, jump the cron
326 if (seconds_to_wait < -600 || seconds_to_wait > 600) {
332 Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
333 getpid(), (long)TargetTime, seconds_to_wait))
336 * If we've run out of wait time or there are no jobs left
340 if (seconds_to_wait <= 0)
342 if (job_runqueue() == 0) {
343 Debug(DSCH, ("[%d] sleeping for %d seconds\n",
344 getpid(), seconds_to_wait))
346 sleep(seconds_to_wait);
352 /* if the time was changed abruptly, clear the flags related
353 * to the daylight time switch handling to avoid strange effects
365 for (u = db->head; u != NULL; u = u->next) {
366 for (e = u->crontab; e != NULL; e = e->next) {
367 e->flags &= ~(RUN_AT|NOT_UNTIL);
380 pid = waitpid(-1, &waiter, WNOHANG);
382 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
387 ("[%d] sigchld...no children\n", getpid()))
391 ("[%d] sigchld...no dead kids\n", getpid()))
395 ("[%d] sigchld...pid #%d died, stat=%d\n",
396 getpid(), pid, WEXITSTATUS(waiter)))
400 #endif /*USE_SIGCHLD*/
410 parse_args(argc, argv)
416 while ((argch = getopt(argc, argv, "osx:")) != -1) {
425 if (!set_debug_flags(optarg))