This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.sbin / cron / cron.c
CommitLineData
693d8207
GR
1/* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
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
11 * user.
12 *
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
16 * From Id: cron.c,v 2.11 1994/01/15 20:43:43 vixie Exp
17 */
18
19#if !defined(lint) && !defined(LINT)
20static char rcsid[] = "$Header: $";
21#endif
22
23
24#define MAIN_PROGRAM
25
26
27#include "cron.h"
28#include <sys/signal.h>
29#if SYS_TIME_H
30# include <sys/time.h>
31#else
32# include <time.h>
33#endif
34
35
36static void usage __P((void)),
37 run_reboot_jobs __P((cron_db *)),
38 cron_tick __P((cron_db *)),
39 cron_sync __P((void)),
40 cron_sleep __P((void)),
41#ifdef USE_SIGCHLD
42 sigchld_handler __P((int)),
43#endif
44 sighup_handler __P((int)),
45 parse_args __P((int c, char *v[]));
46
47
48static void
49usage() {
50 fprintf(stderr, "usage: %s [-x debugflag[,...]]\n", ProgramName);
51 exit(ERROR_EXIT);
52}
53
54
55int
56main(argc, argv)
57 int argc;
58 char *argv[];
59{
60 cron_db database;
61
62 ProgramName = argv[0];
63
64#if defined(BSD)
65 setlinebuf(stdout);
66 setlinebuf(stderr);
67#endif
68
69 parse_args(argc, argv);
70
71#ifdef USE_SIGCHLD
72 (void) signal(SIGCHLD, sigchld_handler);
73#else
74 (void) signal(SIGCLD, SIG_IGN);
75#endif
76 (void) signal(SIGHUP, sighup_handler);
77
78 acquire_daemonlock(0);
79 set_cron_uid();
80 set_cron_cwd();
81
82#if defined(POSIX)
83 setenv("PATH", _PATH_DEFPATH, 1);
84#endif
85
86 /* if there are no debug flags turned on, fork as a daemon should.
87 */
88# if DEBUGGING
89 if (DebugFlags) {
90# else
91 if (0) {
92# endif
93 (void) fprintf(stderr, "[%d] cron started\n", getpid());
94 } else {
95 switch (fork()) {
96 case -1:
97 log_it("CRON",getpid(),"DEATH","can't fork");
98 exit(0);
99 break;
100 case 0:
101 /* child process */
102 log_it("CRON",getpid(),"STARTUP","fork ok");
103 (void) setsid();
104 break;
105 default:
106 /* parent process should just die */
107 _exit(0);
108 }
109 }
110
111 acquire_daemonlock(0);
112 database.head = NULL;
113 database.tail = NULL;
114 database.mtime = (time_t) 0;
115 load_database(&database);
116 run_reboot_jobs(&database);
117 cron_sync();
118 while (TRUE) {
119# if DEBUGGING
120 if (!(DebugFlags & DTEST))
121# endif /*DEBUGGING*/
122 cron_sleep();
123
124 load_database(&database);
125
126 /* do this iteration
127 */
128 cron_tick(&database);
129
130 /* sleep 1 minute
131 */
132 TargetTime += 60;
133 }
134}
135
136
137static void
138run_reboot_jobs(db)
139 cron_db *db;
140{
141 register user *u;
142 register entry *e;
143
144 for (u = db->head; u != NULL; u = u->next) {
145 for (e = u->crontab; e != NULL; e = e->next) {
146 if (e->flags & WHEN_REBOOT) {
147 job_add(e, u);
148 }
149 }
150 }
151 (void) job_runqueue();
152}
153
154
155static void
156cron_tick(db)
157 cron_db *db;
158{
159 register struct tm *tm = localtime(&TargetTime);
160 register int minute, hour, dom, month, dow;
161 register user *u;
162 register entry *e;
163
164 /* make 0-based values out of these so we can use them as indicies
165 */
166 minute = tm->tm_min -FIRST_MINUTE;
167 hour = tm->tm_hour -FIRST_HOUR;
168 dom = tm->tm_mday -FIRST_DOM;
169 month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
170 dow = tm->tm_wday -FIRST_DOW;
171
172 Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d)\n",
173 getpid(), minute, hour, dom, month, dow))
174
175 /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the
176 * first and fifteenth AND every Sunday; '* * * * Sun' will run *only*
177 * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this
178 * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre.
179 * like many bizarre things, it's the standard.
180 */
181 for (u = db->head; u != NULL; u = u->next) {
182 for (e = u->crontab; e != NULL; e = e->next) {
183 Debug(DSCH|DEXT, ("user [%s:%d:%d:...] cmd=\"%s\"\n",
184 env_get("LOGNAME", e->envp),
185 e->uid, e->gid, e->cmd))
186 if (bit_test(e->minute, minute)
187 && bit_test(e->hour, hour)
188 && bit_test(e->month, month)
189 && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
190 ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
191 : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
192 )
193 ) {
194 job_add(e, u);
195 }
196 }
197 }
198}
199
200
201/* the task here is to figure out how long it's going to be until :00 of the
202 * following minute and initialize TargetTime to this value. TargetTime
203 * will subsequently slide 60 seconds at a time, with correction applied
204 * implicitly in cron_sleep(). it would be nice to let cron execute in
205 * the "current minute" before going to sleep, but by restarting cron you
206 * could then get it to execute a given minute's jobs more than once.
207 * instead we have the chance of missing a minute's jobs completely, but
208 * that's something sysadmin's know to expect what with crashing computers..
209 */
210static void
211cron_sync() {
212 register struct tm *tm;
213
214 TargetTime = time((time_t*)0);
215 tm = localtime(&TargetTime);
216 TargetTime += (60 - tm->tm_sec);
217}
218
219
220static void
221cron_sleep() {
222 register int seconds_to_wait;
223
224 do {
225 seconds_to_wait = (int) (TargetTime - time((time_t*)0));
226 Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n",
227 getpid(), TargetTime, seconds_to_wait))
228
229 /* if we intend to sleep, this means that it's finally
230 * time to empty the job queue (execute it).
231 *
232 * if we run any jobs, we'll probably screw up our timing,
233 * so go recompute.
234 *
235 * note that we depend here on the left-to-right nature
236 * of &&, and the short-circuiting.
237 */
238 } while (seconds_to_wait > 0 && job_runqueue());
239
240 while (seconds_to_wait > 0) {
241 Debug(DSCH, ("[%d] sleeping for %d seconds\n",
242 getpid(), seconds_to_wait))
243 seconds_to_wait = (int) sleep((unsigned int) seconds_to_wait);
244 }
245}
246
247
248#ifdef USE_SIGCHLD
249static void
250sigchld_handler(x) {
251 WAIT_T waiter;
252 PID_T pid;
253
254 for (;;) {
255#ifdef POSIX
256 pid = waitpid(-1, &waiter, WNOHANG);
257#else
258 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
259#endif
260 switch (pid) {
261 case -1:
262 Debug(DPROC,
263 ("[%d] sigchld...no children\n", getpid()))
264 return;
265 case 0:
266 Debug(DPROC,
267 ("[%d] sigchld...no dead kids\n", getpid()))
268 return;
269 default:
270 Debug(DPROC,
271 ("[%d] sigchld...pid #%d died, stat=%d\n",
272 getpid(), pid, WEXITSTATUS(waiter)))
273 }
274 }
275}
276#endif /*USE_SIGCHLD*/
277
278
279static void
280sighup_handler(x) {
281 log_close();
282}
283
284
285static void
286parse_args(argc, argv)
287 int argc;
288 char *argv[];
289{
290 int argch;
291
292 while (EOF != (argch = getopt(argc, argv, "x:"))) {
293 switch (argch) {
294 default:
295 usage();
296 case 'x':
297 if (!set_debug_flags(optarg))
298 usage();
299 break;
300 }
301 }
302}