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.8 2006/03/18 20:29:50 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 [-j jitter] [-J rootjitter] "
54 "[-s] [-o] [-x debugflag[,...]]\n");
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 signal(SIGCHLD
, sigchld_handler
);
82 signal(SIGCLD
, SIG_IGN
);
84 signal(SIGHUP
, sighup_handler
);
86 acquire_daemonlock(0);
91 if (setenv("PATH", _PATH_DEFPATH
, 1) == -1) {
92 log_it("CRON", getpid(), "DEATH",
93 "setenv: cannot set PATH");
98 /* if there are no debug flags turned on, fork as a daemon should.
105 fprintf(stderr
, "[%d] cron started\n", getpid());
107 if (daemon(1, 0) == -1) {
108 log_it("CRON",getpid(),"DEATH","can't become daemon");
113 acquire_daemonlock(0);
114 database
.head
= NULL
;
115 database
.tail
= NULL
;
116 database
.mtime
= (time_t) 0;
117 load_database(&database
);
118 run_reboot_jobs(&database
);
122 /* if (!(DebugFlags & DTEST)) */
123 # endif /*DEBUGGING*/
124 cron_sleep(&database
);
126 load_database(&database
);
130 cron_tick(&database
);
140 run_reboot_jobs(cron_db
*db
)
145 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
146 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
147 if (e
->flags
& WHEN_REBOOT
) {
157 cron_tick(cron_db
*db
)
159 static struct tm lasttm
;
160 static time_t diff
; /* delta time from the last offset change */
161 static time_t difflimit
; /* end point for the time zone correction */
162 struct tm otztm
; /* time in the old time zone */
163 int otzminute
, otzhour
, otzdom
, otzmonth
, otzdow
;
165 int minute
, hour
, dom
, month
, dow
;
169 tm
= localtime(&TargetTime
);
170 /* make 0-based values out of these so we can use them as indicies
172 minute
= tm
->tm_min
-FIRST_MINUTE
;
173 hour
= tm
->tm_hour
-FIRST_HOUR
;
174 dom
= tm
->tm_mday
-FIRST_DOM
;
175 month
= tm
->tm_mon
+1 /* 0..11 -> 1..12 */ -FIRST_MONTH
;
176 dow
= tm
->tm_wday
-FIRST_DOW
;
178 Debug(DSCH
, ("[%d] tick(%d,%d,%d,%d,%d)\n",
179 getpid(), minute
, hour
, dom
, month
, dow
))
181 if (dst_enabled
&& last_time
!= 0
182 && TargetTime
> last_time
/* exclude stepping back */
183 && tm
->tm_gmtoff
!= lasttm
.tm_gmtoff
) {
185 diff
= tm
->tm_gmtoff
- lasttm
.tm_gmtoff
;
187 if ( diff
> 0 ) { /* ST->DST */
188 /* mark jobs for an earlier run */
189 difflimit
= TargetTime
+ diff
;
190 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
191 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
192 e
->flags
&= ~NOT_UNTIL
;
193 if ( e
->lastrun
>= TargetTime
)
195 /* not include the ends of hourly ranges */
196 if ( e
->lastrun
< TargetTime
- 3600 )
202 } else { /* diff < 0 : DST->ST */
203 /* mark jobs for skipping */
204 difflimit
= TargetTime
- diff
;
205 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
206 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
207 e
->flags
|= NOT_UNTIL
;
215 /* if the time was reset of the end of special zone is reached */
216 if (last_time
== 0 || TargetTime
>= difflimit
) {
217 /* disable the TZ switch checks */
220 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
221 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
222 e
->flags
&= ~(RUN_AT
|NOT_UNTIL
);
226 /* get the time in the old time zone */
227 time_t difftime
= TargetTime
+ tm
->tm_gmtoff
- diff
;
228 gmtime_r(&difftime
, &otztm
);
230 /* make 0-based values out of these so we can use them as indicies
232 otzminute
= otztm
.tm_min
-FIRST_MINUTE
;
233 otzhour
= otztm
.tm_hour
-FIRST_HOUR
;
234 otzdom
= otztm
.tm_mday
-FIRST_DOM
;
235 otzmonth
= otztm
.tm_mon
+1 /* 0..11 -> 1..12 */ -FIRST_MONTH
;
236 otzdow
= otztm
.tm_wday
-FIRST_DOW
;
240 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
241 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
242 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
243 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
244 * like many bizarre things, it's the standard.
246 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
247 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
248 Debug(DSCH
|DEXT
, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
249 env_get("LOGNAME", e
->envp
),
250 e
->uid
, e
->gid
, e
->cmd
))
252 if ( diff
!= 0 && (e
->flags
& (RUN_AT
|NOT_UNTIL
)) ) {
253 if (bit_test(e
->minute
, otzminute
)
254 && bit_test(e
->hour
, otzhour
)
255 && bit_test(e
->month
, otzmonth
)
256 && ( ((e
->flags
& DOM_STAR
) || (e
->flags
& DOW_STAR
))
257 ? (bit_test(e
->dow
,otzdow
) && bit_test(e
->dom
,otzdom
))
258 : (bit_test(e
->dow
,otzdow
) || bit_test(e
->dom
,otzdom
))
261 if ( e
->flags
& RUN_AT
) {
263 e
->lastrun
= TargetTime
;
267 e
->flags
&= ~NOT_UNTIL
;
268 } else if ( e
->flags
& NOT_UNTIL
)
272 if (bit_test(e
->minute
, minute
)
273 && bit_test(e
->hour
, hour
)
274 && bit_test(e
->month
, month
)
275 && ( ((e
->flags
& DOM_STAR
) || (e
->flags
& DOW_STAR
))
276 ? (bit_test(e
->dow
,dow
) && bit_test(e
->dom
,dom
))
277 : (bit_test(e
->dow
,dow
) || bit_test(e
->dom
,dom
))
281 e
->lastrun
= TargetTime
;
287 last_time
= TargetTime
;
292 /* the task here is to figure out how long it's going to be until :00 of the
293 * following minute and initialize TargetTime to this value. TargetTime
294 * will subsequently slide 60 seconds at a time, with correction applied
295 * implicitly in cron_sleep(). it would be nice to let cron execute in
296 * the "current minute" before going to sleep, but by restarting cron you
297 * could then get it to execute a given minute's jobs more than once.
298 * instead we have the chance of missing a minute's jobs completely, but
299 * that's something sysadmin's know to expect what with crashing computers..
306 TargetTime
= time((time_t*)0);
307 tm
= localtime(&TargetTime
);
308 TargetTime
+= (60 - tm
->tm_sec
);
313 cron_sleep(cron_db
*db
)
315 int seconds_to_wait
= 0;
318 * Loop until we reach the top of the next minute, sleep when possible.
322 seconds_to_wait
= (int) (TargetTime
- time((time_t*)0));
325 * If the seconds_to_wait value is insane, jump the cron
328 if (seconds_to_wait
< -600 || seconds_to_wait
> 600) {
334 Debug(DSCH
, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
335 getpid(), (long)TargetTime
, seconds_to_wait
))
338 * If we've run out of wait time or there are no jobs left
342 if (seconds_to_wait
<= 0)
344 if (job_runqueue() == 0) {
345 Debug(DSCH
, ("[%d] sleeping for %d seconds\n",
346 getpid(), seconds_to_wait
))
348 sleep(seconds_to_wait
);
354 /* if the time was changed abruptly, clear the flags related
355 * to the daylight time switch handling to avoid strange effects
359 cron_clean(cron_db
*db
)
366 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
367 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
368 e
->flags
&= ~(RUN_AT
|NOT_UNTIL
);
375 sigchld_handler(int x
)
382 pid
= waitpid(-1, &waiter
, WNOHANG
);
384 pid
= wait3(&waiter
, WNOHANG
, (struct rusage
*)0);
389 ("[%d] sigchld...no children\n", getpid()))
393 ("[%d] sigchld...no dead kids\n", getpid()))
397 ("[%d] sigchld...pid #%d died, stat=%d\n",
398 getpid(), pid
, WEXITSTATUS(waiter
)))
402 #endif /*USE_SIGCHLD*/
406 sighup_handler(int x
)
413 parse_args(int argc
, char **argv
)
418 while ((argch
= getopt(argc
, argv
, "j:J:osx:")) != -1) {
421 Jitter
= strtoul(optarg
, &endp
, 10);
422 if (*optarg
== '\0' || *endp
!= '\0' || Jitter
> 60)
424 "bad value for jitter: %s", optarg
);
427 RootJitter
= strtoul(optarg
, &endp
, 10);
428 if (*optarg
== '\0' || *endp
!= '\0' || RootJitter
> 60)
430 "bad value for root jitter: %s", optarg
);
439 if (!set_debug_flags(optarg
))