No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / cron / cron.c
blobd4bde9622ccc836f62ce846114b7d1e74a86014b
1 /* $NetBSD: cron.c,v 1.12 2006/05/21 19:26:43 christos Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * All rights reserved
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
13 * user.
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)
22 #if 0
23 static char rcsid[] = "Id: cron.c,v 2.11 1994/01/15 20:43:43 vixie Exp";
24 #else
25 __RCSID("$NetBSD: cron.c,v 1.12 2006/05/21 19:26:43 christos Exp $");
26 #endif
27 #endif
30 #define MAIN_PROGRAM
33 #include "cron.h"
34 #include <sys/signal.h>
35 #if SYS_TIME_H
36 # include <sys/time.h>
37 #else
38 # include <time.h>
39 #endif
42 static void usage(void),
43 run_reboot_jobs(cron_db *),
44 cron_tick(cron_db *),
45 cron_sync(void),
46 cron_sleep(void),
47 #ifdef USE_SIGCHLD
48 sigchld_handler(int),
49 #endif
50 sighup_handler(int),
51 parse_args(int c, char *v[]);
54 static void
55 usage(void) {
56 fprintf(stderr, "usage: %s [-x debugflag[,...]]\n", getprogname());
57 exit(ERROR_EXIT);
61 int
62 main(int argc, char **argv)
64 cron_db database;
66 setprogname(argv[0]);
68 #if defined(BSD)
69 setlinebuf(stdout);
70 setlinebuf(stderr);
71 #endif
73 parse_args(argc, argv);
75 #ifdef USE_SIGCHLD
76 (void) signal(SIGCHLD, sigchld_handler);
77 #else
78 (void) signal(SIGCLD, SIG_IGN);
79 #endif
80 (void) signal(SIGHUP, sighup_handler);
82 acquire_daemonlock(0);
83 set_cron_uid();
84 set_cron_cwd();
86 #if defined(POSIX)
87 setenv("PATH", _PATH_DEFPATH, 1);
88 #endif
90 /* if there are no debug flags turned on, fork as a daemon should.
92 # if DEBUGGING
93 if (DebugFlags) {
94 # else
95 if (0) {
96 # endif
97 (void) fprintf(stderr, "[%d] cron started\n", getpid());
98 } else {
99 if (daemon(1, 0)) {
100 log_it("CRON",getpid(),"DEATH","can't fork");
101 exit(1);
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);
111 cron_sync();
112 while (TRUE) {
113 # if DEBUGGING
114 if (!(DebugFlags & DTEST))
115 # endif /*DEBUGGING*/
116 cron_sleep();
118 load_database(&database);
120 /* do this iteration
122 cron_tick(&database);
124 /* sleep 1 minute
126 TargetTime += 60;
131 static void
132 run_reboot_jobs(cron_db *db)
134 user *u;
135 entry *e;
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) {
140 job_add(e, u);
144 (void) job_runqueue();
148 static void
149 cron_tick(cron_db *db)
151 char *orig_tz, *job_tz;
152 struct tm *tm;
153 int minute, hour, dom, month, dow;
154 user *u;
155 entry *e;
157 /* make 0-based values out of these so we can use them as indicies
159 #define maketime(tz1, tz2) do { \
160 char *t = tz1; \
161 if (t != NULL && *t != '\0') \
162 setenv("TZ", t, 1); \
163 else if ((tz2) != NULL) \
164 setenv("TZ", (tz2), 1); \
165 else \
166 unsetenv("TZ"); \
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; \
173 } while (0)
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))
202 job_add(e, u);
206 if (orig_tz != NULL)
207 setenv("TZ", orig_tz, 1);
208 else
209 unsetenv("TZ");
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..
222 static void
223 cron_sync(void) {
224 struct tm *tm;
226 TargetTime = time((time_t*)0);
227 tm = localtime(&TargetTime);
228 TargetTime += (60 - tm->tm_sec);
232 static void
233 cron_sleep(void) {
234 int seconds_to_wait;
236 do {
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,
245 * so go recompute.
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);
260 #ifdef USE_SIGCHLD
261 static void
262 sigchld_handler(int x __unused)
264 WAIT_T waiter;
265 PID_T pid;
267 for (;;) {
268 #ifdef POSIX
269 pid = waitpid(-1, &waiter, WNOHANG);
270 #else
271 pid = wait3(&waiter, WNOHANG, (struct rusage *)0);
272 #endif
273 switch (pid) {
274 case -1:
275 Debug(DPROC,
276 ("[%d] sigchld...no children\n", getpid()))
277 return;
278 case 0:
279 Debug(DPROC,
280 ("[%d] sigchld...no dead kids\n", getpid()))
281 return;
282 default:
283 Debug(DPROC,
284 ("[%d] sigchld...pid #%d died, stat=%d\n",
285 getpid(), pid, WEXITSTATUS(waiter)))
289 #endif /*USE_SIGCHLD*/
292 static void
293 sighup_handler(int x __unused)
295 log_close();
299 static void
300 parse_args(int argc, char **argv)
302 int argch;
304 while (-1 != (argch = getopt(argc, argv, "x:"))) {
305 switch (argch) {
306 default:
307 usage();
308 case 'x':
309 if (!set_debug_flags(optarg))
310 usage();
311 break;