#if !defined(lint) && !defined(LINT)
static char rcsid
[] = "$Header: /home/local/site/cron2.1/orig/RCS/crond.c,v 2.2 1992/05/13 05:56:35 rich Exp $";
/* Copyright 1988,1990 by Paul Vixie
* Distribute freely, except: don't remove my name from the source or
* documentation (don't take credit for my work), mark your changes (don't
* get me blamed for your possible bugs), don't alter or remove this
* notice. May be sold if buildable source is provided to buyer. No
* warrantee of any kind, express or implied, is included with this
* software; use at your own risk, responsibility for damages (if any) to
* anyone resulting from the use of this software rests entirely with the
* Send bug reports, bug fixes, enhancements, requests, flames, etc., and
* I'll try to keep a version up to date. I can be reached as follows:
* Paul Vixie, 329 Noe Street, San Francisco, CA, 94114, (415) 864-7013,
* paul@vixie.sf.ca.us || {hoptoad,pacbell,decwrl,crash}!vixie!paul
* PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
* -------------------- ----- ----------------------
* CURRENT PATCH LEVEL: 1 00131
* -------------------- ----- ----------------------
* 06 Apr 93 Adam Glass Fixes so it compiles quitely
# include <sys/resource.h>
extern int fork(), unlink();
(void) fprintf(stderr
, "usage: %s [-x debugflag[,...]]\n", ProgramName
);
extern void set_cron_uid(), be_different(), load_database(),
set_cron_cwd(), open_logfile();
static void cron_tick(), cron_sleep(), cron_sync(),
sigchld_handler(), parse_args(), run_reboot_jobs();
/* if there are no debug flags turned on, fork as a daemon should.
(void) fprintf(stderr
, "[%d] crond started\n", getpid());
log_it("CROND",getpid(),"DEATH","can't fork");
/* parent process should just die */
(void) signal(SIGCHLD
, sigchld_handler
);
(void) signal(SIGCLD
, SIG_IGN
);
database
.mtime
= (time_t) 0;
load_database(&database
);
run_reboot_jobs(&database
);
if (!(DebugFlags
& DTEST
))
load_database(&database
);
extern int job_runqueue();
for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
if (e
->flags
& WHEN_REBOOT
) {
extern struct tm
*localtime();
register struct tm
*tm
= localtime(&TargetTime
);
local
int minute
, hour
, dom
, month
, dow
;
/* make 0-based values out of these so we can use them as indicies
minute
= tm
->tm_min
-FIRST_MINUTE
;
hour
= tm
->tm_hour
-FIRST_HOUR
;
dom
= tm
->tm_mday
-FIRST_DOM
;
month
= tm
->tm_mon
+1 /* 0..11 -> 1..12 */ -FIRST_MONTH
;
dow
= tm
->tm_wday
-FIRST_DOW
;
Debug(DSCH
, ("[%d] tick(%d,%d,%d,%d,%d)\n",
getpid(), minute
, hour
, dom
, month
, dow
))
/* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
* first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
* on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
* is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
* like many bizarre things, it's the standard.
for (u
= db
->head
; u
!= NULL
; u
= u
->next
) {
Debug(DSCH
|DEXT
, ("user [%s:%d:%d:...]\n",
env_get(USERENV
,u
->envp
), u
->uid
, u
->gid
))
for (e
= u
->crontab
; e
!= NULL
; e
= e
->next
) {
Debug(DSCH
|DEXT
, ("entry [%s]\n", e
->cmd
))
if (bit_test(e
->minute
, minute
)
&& bit_test(e
->hour
, hour
)
&& bit_test(e
->month
, month
)
&& ( ((e
->flags
& DOM_STAR
) || (e
->flags
& DOW_STAR
))
? (bit_test(e
->dow
,dow
) && bit_test(e
->dom
,dom
))
: (bit_test(e
->dow
,dow
) || bit_test(e
->dom
,dom
))
/* the task here is to figure out how long it's going to be until :00 of the
* following minute and initialize TargetTime to this value. TargetTime
* will subsequently slide 60 seconds at a time, with correction applied
* implicitly in cron_sleep(). it would be nice to let crond execute in
* the "current minute" before going to sleep, but by restarting cron you
* could then get it to execute a given minute's jobs more than once.
* instead we have the chance of missing a minute's jobs completely, but
* that's something sysadmin's know to expect what with crashing computers..
extern struct tm
*localtime();
TargetTime
= time((time_t*)0);
tm
= localtime(&TargetTime
);
TargetTime
+= (60 - tm
->tm_sec
);
extern void do_command();
extern int job_runqueue();
register int seconds_to_wait
;
seconds_to_wait
= (int) (TargetTime
- time((time_t*)0));
Debug(DSCH
, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
getpid(), TargetTime
, seconds_to_wait
))
/* if we intend to sleep, this means that it's finally
* time to empty the job queue (execute it).
* if we run any jobs, we'll probably screw up our timing,
* note that we depend here on the left-to-right nature
* of &&, and the short-circuiting.
} while (seconds_to_wait
> 0 && job_runqueue());
Debug(DSCH
, ("[%d] sleeping for %d seconds\n",
getpid(), seconds_to_wait
))
(void) sleep((unsigned int) seconds_to_wait
);
pid
= wait3((int *) &waiter
, WNOHANG
, (struct rusage
*)0);
("[%d] sigchld...no children\n", getpid()))
("[%d] sigchld...no dead kids\n", getpid()))
("[%d] sigchld...pid #%d died, stat=%d\n",
getpid(), pid
, waiter
.w_status
))
extern int optind
, getopt();
while (EOF
!= (argch
= getopt(argc
, argv
, "x:")))
if (!set_debug_flags(optarg
))