1 /* $NetBSD: cron.c,v 1.12 2006/05/21 19:26:43 christos Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
6 * Distribute freely, except: don't remove my name from the source or
7 * documentation (don't take credit for my work), mark your changes (don't
8 * get me blamed for your possible bugs), don't alter or remove this
9 * notice. May be sold if buildable source is provided to buyer. No
10 * warrantee of any kind, express or implied, is included with this
11 * software; use at your own risk, responsibility for damages (if any) to
12 * anyone resulting from the use of this software rests entirely with the
15 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
16 * I'll try to keep a version up to date. I can be reached as follows:
17 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
20 #include <sys/cdefs.h>
21 #if !defined(lint) && !defined(LINT)
23 static char rcsid
[] = "Id: cron.c,v 2.11 1994/01/15 20:43:43 vixie Exp";
25 __RCSID("$NetBSD: cron.c,v 1.12 2006/05/21 19:26:43 christos Exp $");
34 #include <sys/signal.h>
36 # include <sys/time.h>
42 static void usage(void),
43 run_reboot_jobs(cron_db
*),
51 parse_args(int c
, char *v
[]);
56 fprintf(stderr
, "usage: %s [-x debugflag[,...]]\n", getprogname());
62 main(int argc
, char **argv
)
73 parse_args(argc
, argv
);
76 (void) signal(SIGCHLD
, sigchld_handler
);
78 (void) signal(SIGCLD
, SIG_IGN
);
80 (void) signal(SIGHUP
, sighup_handler
);
82 acquire_daemonlock(0);
87 setenv("PATH", _PATH_DEFPATH
, 1);
90 /* if there are no debug flags turned on, fork as a daemon should.
97 (void) fprintf(stderr
, "[%d] cron started\n", getpid());
100 log_it("CRON",getpid(),"DEATH","can't fork");
105 acquire_daemonlock(0);
106 database
.head
= NULL
;
107 database
.tail
= NULL
;
108 database
.mtime
= (time_t) 0;
109 load_database(&database
);
110 run_reboot_jobs(&database
);
114 if (!(DebugFlags
& DTEST
))
115 # endif /*DEBUGGING*/
118 load_database(&database
);
122 cron_tick(&database
);
132 run_reboot_jobs(cron_db
*db
)
137 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
138 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
139 if (e
->flags
& WHEN_REBOOT
) {
144 (void) job_runqueue();
149 cron_tick(cron_db
*db
)
151 char *orig_tz
, *job_tz
;
153 int minute
, hour
, dom
, month
, dow
;
157 /* make 0-based values out of these so we can use them as indicies
159 #define maketime(tz1, tz2) do { \
161 if (t != NULL && *t != '\0') \
162 setenv("TZ", t, 1); \
163 else if ((tz2) != NULL) \
164 setenv("TZ", (tz2), 1); \
167 tm = localtime(&TargetTime); \
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; \
175 orig_tz
= getenv("TZ");
176 maketime(NULL
, orig_tz
);
178 Debug(DSCH
, ("[%d] tick(%d,%d,%d,%d,%d)\n",
179 getpid(), minute
, hour
, dom
, month
, dow
))
181 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
182 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
183 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
184 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
185 * like many bizarre things, it's the standard.
187 for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
188 for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
189 Debug(DSCH
|DEXT
, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
190 env_get("LOGNAME", e
->envp
),
191 e
->uid
, e
->gid
, e
->cmd
))
192 job_tz
= env_get("CRON_TZ", e
->envp
);
193 maketime(job_tz
, orig_tz
);
194 if (bit_test(e
->minute
, minute
)
195 && bit_test(e
->hour
, hour
)
196 && bit_test(e
->month
, month
)
197 && ( ((e
->flags
& DOM_STAR
) || (e
->flags
& DOW_STAR
))
198 ? (bit_test(e
->dow
,dow
) && bit_test(e
->dom
,dom
))
199 : (bit_test(e
->dow
,dow
) || bit_test(e
->dom
,dom
))
207 setenv("TZ", orig_tz
, 1);
213 /* the task here is to figure out how long it's going to be until :00 of the
214 * following minute and initialize TargetTime to this value. TargetTime
215 * will subsequently slide 60 seconds at a time, with correction applied
216 * implicitly in cron_sleep(). it would be nice to let cron execute in
217 * the "current minute" before going to sleep, but by restarting cron you
218 * could then get it to execute a given minute's jobs more than once.
219 * instead we have the chance of missing a minute's jobs completely, but
220 * that's something sysadmin's know to expect what with crashing computers..
226 TargetTime
= time((time_t*)0);
227 tm
= localtime(&TargetTime
);
228 TargetTime
+= (60 - tm
->tm_sec
);
237 seconds_to_wait
= (int) (TargetTime
- time((time_t*)0));
238 Debug(DSCH
, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
239 getpid(), (long)TargetTime
, seconds_to_wait
))
241 /* if we intend to sleep, this means that it's finally
242 * time to empty the job queue (execute it).
244 * if we run any jobs, we'll probably screw up our timing,
247 * note that we depend here on the left-to-right nature
248 * of &&, and the short-circuiting.
250 } while (seconds_to_wait
> 0 && job_runqueue());
252 while ((seconds_to_wait
= (int) (TargetTime
- time((time_t *)0))) > 0) {
253 Debug(DSCH
, ("[%d] sleeping for %d seconds\n",
254 getpid(), seconds_to_wait
))
255 sleep((unsigned int) seconds_to_wait
);
262 sigchld_handler(int x __unused
)
269 pid
= waitpid(-1, &waiter
, WNOHANG
);
271 pid
= wait3(&waiter
, WNOHANG
, (struct rusage
*)0);
276 ("[%d] sigchld...no children\n", getpid()))
280 ("[%d] sigchld...no dead kids\n", getpid()))
284 ("[%d] sigchld...pid #%d died, stat=%d\n",
285 getpid(), pid
, WEXITSTATUS(waiter
)))
289 #endif /*USE_SIGCHLD*/
293 sighup_handler(int x __unused
)
300 parse_args(int argc
, char **argv
)
304 while (-1 != (argch
= getopt(argc
, argv
, "x:"))) {
309 if (!set_debug_flags(optarg
))