| 1 | #if !defined(lint) && !defined(LINT) |
| 2 | static char rcsid[] = "$Header: /home/local/site/cron2.1/orig/RCS/crond.c,v 2.2 1992/05/13 05:56:35 rich Exp $"; |
| 3 | #endif |
| 4 | |
| 5 | /* Copyright 1988,1990 by Paul Vixie |
| 6 | * All rights reserved |
| 7 | * |
| 8 | * Distribute freely, except: don't remove my name from the source or |
| 9 | * documentation (don't take credit for my work), mark your changes (don't |
| 10 | * get me blamed for your possible bugs), don't alter or remove this |
| 11 | * notice. May be sold if buildable source is provided to buyer. No |
| 12 | * warrantee of any kind, express or implied, is included with this |
| 13 | * software; use at your own risk, responsibility for damages (if any) to |
| 14 | * anyone resulting from the use of this software rests entirely with the |
| 15 | * user. |
| 16 | * |
| 17 | * Send bug reports, bug fixes, enhancements, requests, flames, etc., and |
| 18 | * I'll try to keep a version up to date. I can be reached as follows: |
| 19 | * Paul Vixie, 329 Noe Street, San Francisco, CA, 94114, (415) 864-7013, |
| 20 | * paul@vixie.sf.ca.us || {hoptoad,pacbell,decwrl,crash}!vixie!paul |
| 21 | * |
| 22 | * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE |
| 23 | * -------------------- ----- ---------------------- |
| 24 | * CURRENT PATCH LEVEL: 1 00131 |
| 25 | * -------------------- ----- ---------------------- |
| 26 | * |
| 27 | * 06 Apr 93 Adam Glass Fixes so it compiles quitely |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #define MAIN_PROGRAM |
| 33 | |
| 34 | |
| 35 | #include "cron.h" |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/signal.h> |
| 38 | #include <sys/types.h> |
| 39 | #if defined(BSD) |
| 40 | # include <sys/wait.h> |
| 41 | # include <sys/resource.h> |
| 42 | #endif /*BSD*/ |
| 43 | |
| 44 | extern int fork(), unlink(); |
| 45 | extern time_t time(); |
| 46 | extern void exit(); |
| 47 | extern unsigned sleep(); |
| 48 | |
| 49 | void |
| 50 | usage() |
| 51 | { |
| 52 | (void) fprintf(stderr, "usage: %s [-x debugflag[,...]]\n", ProgramName); |
| 53 | (void) exit(ERROR_EXIT); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | int |
| 58 | main(argc, argv) |
| 59 | int argc; |
| 60 | char *argv[]; |
| 61 | { |
| 62 | extern void set_cron_uid(), be_different(), load_database(), |
| 63 | set_cron_cwd(), open_logfile(); |
| 64 | |
| 65 | static void cron_tick(), cron_sleep(), cron_sync(), |
| 66 | sigchld_handler(), parse_args(), run_reboot_jobs(); |
| 67 | |
| 68 | auto cron_db database; |
| 69 | |
| 70 | ProgramName = argv[0]; |
| 71 | |
| 72 | #if defined(BSD) |
| 73 | setlinebuf(stdout); |
| 74 | setlinebuf(stderr); |
| 75 | #endif |
| 76 | |
| 77 | parse_args(argc, argv); |
| 78 | |
| 79 | # if DEBUGGING |
| 80 | /* if there are no debug flags turned on, fork as a daemon should. |
| 81 | */ |
| 82 | if (DebugFlags) |
| 83 | { |
| 84 | (void) fprintf(stderr, "[%d] crond started\n", getpid()); |
| 85 | } |
| 86 | else |
| 87 | # endif /*DEBUGGING*/ |
| 88 | { |
| 89 | switch (fork()) |
| 90 | { |
| 91 | case -1: |
| 92 | log_it("CROND",getpid(),"DEATH","can't fork"); |
| 93 | exit(0); |
| 94 | break; |
| 95 | case 0: |
| 96 | /* child process */ |
| 97 | be_different(); |
| 98 | break; |
| 99 | default: |
| 100 | /* parent process should just die */ |
| 101 | _exit(0); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #if defined(BSD) |
| 106 | (void) signal(SIGCHLD, sigchld_handler); |
| 107 | #endif /*BSD*/ |
| 108 | |
| 109 | #if defined(ATT) |
| 110 | (void) signal(SIGCLD, SIG_IGN); |
| 111 | #endif /*ATT*/ |
| 112 | |
| 113 | acquire_daemonlock(); |
| 114 | set_cron_uid(); |
| 115 | set_cron_cwd(); |
| 116 | database.head = NULL; |
| 117 | database.tail = NULL; |
| 118 | database.mtime = (time_t) 0; |
| 119 | load_database(&database); |
| 120 | run_reboot_jobs(&database); |
| 121 | cron_sync(); |
| 122 | while (TRUE) |
| 123 | { |
| 124 | # if DEBUGGING |
| 125 | if (!(DebugFlags & DTEST)) |
| 126 | # endif /*DEBUGGING*/ |
| 127 | cron_sleep(); |
| 128 | |
| 129 | load_database(&database); |
| 130 | |
| 131 | /* do this iteration |
| 132 | */ |
| 133 | cron_tick(&database); |
| 134 | |
| 135 | /* sleep 1 minute |
| 136 | */ |
| 137 | TargetTime += 60; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static void |
| 143 | run_reboot_jobs(db) |
| 144 | cron_db *db; |
| 145 | { |
| 146 | extern void job_add(); |
| 147 | extern int job_runqueue(); |
| 148 | register user *u; |
| 149 | register entry *e; |
| 150 | |
| 151 | for (u = db->head; u != NULL; u = u->next) { |
| 152 | for (e = u->crontab; e != NULL; e = e->next) { |
| 153 | if (e->flags & WHEN_REBOOT) { |
| 154 | job_add(e->cmd, u); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | (void) job_runqueue(); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static void |
| 163 | cron_tick(db) |
| 164 | cron_db *db; |
| 165 | { |
| 166 | extern void job_add(); |
| 167 | extern char *env_get(); |
| 168 | extern struct tm *localtime(); |
| 169 | register struct tm *tm = localtime(&TargetTime); |
| 170 | local int minute, hour, dom, month, dow; |
| 171 | register user *u; |
| 172 | register entry *e; |
| 173 | |
| 174 | /* make 0-based values out of these so we can use them as indicies |
| 175 | */ |
| 176 | minute = tm->tm_min -FIRST_MINUTE; |
| 177 | hour = tm->tm_hour -FIRST_HOUR; |
| 178 | dom = tm->tm_mday -FIRST_DOM; |
| 179 | month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH; |
| 180 | dow = tm->tm_wday -FIRST_DOW; |
| 181 | |
| 182 | Debug(DSCH, ("[%d] tick(%d,%d,%d,%d,%d)\n", |
| 183 | getpid(), minute, hour, dom, month, dow)) |
| 184 | |
| 185 | /* the dom/dow situation is odd. '* * 1,15 * Sun' will run on the |
| 186 | * first and fifteenth AND every Sunday; '* * * * Sun' will run *only* |
| 187 | * on Sundays; '* * 1,15 * *' will run *only* the 1st and 15th. this |
| 188 | * is why we keep 'e->dow_star' and 'e->dom_star'. yes, it's bizarre. |
| 189 | * like many bizarre things, it's the standard. |
| 190 | */ |
| 191 | for (u = db->head; u != NULL; u = u->next) { |
| 192 | Debug(DSCH|DEXT, ("user [%s:%d:%d:...]\n", |
| 193 | env_get(USERENV,u->envp), u->uid, u->gid)) |
| 194 | for (e = u->crontab; e != NULL; e = e->next) { |
| 195 | Debug(DSCH|DEXT, ("entry [%s]\n", e->cmd)) |
| 196 | if (bit_test(e->minute, minute) |
| 197 | && bit_test(e->hour, hour) |
| 198 | && bit_test(e->month, month) |
| 199 | && ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR)) |
| 200 | ? (bit_test(e->dow,dow) && bit_test(e->dom,dom)) |
| 201 | : (bit_test(e->dow,dow) || bit_test(e->dom,dom)) |
| 202 | ) |
| 203 | ) { |
| 204 | job_add(e->cmd, u); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* the task here is to figure out how long it's going to be until :00 of the |
| 212 | * following minute and initialize TargetTime to this value. TargetTime |
| 213 | * will subsequently slide 60 seconds at a time, with correction applied |
| 214 | * implicitly in cron_sleep(). it would be nice to let crond execute in |
| 215 | * the "current minute" before going to sleep, but by restarting cron you |
| 216 | * could then get it to execute a given minute's jobs more than once. |
| 217 | * instead we have the chance of missing a minute's jobs completely, but |
| 218 | * that's something sysadmin's know to expect what with crashing computers.. |
| 219 | */ |
| 220 | static void |
| 221 | cron_sync() |
| 222 | { |
| 223 | extern struct tm *localtime(); |
| 224 | register struct tm *tm; |
| 225 | |
| 226 | TargetTime = time((time_t*)0); |
| 227 | tm = localtime(&TargetTime); |
| 228 | TargetTime += (60 - tm->tm_sec); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static void |
| 233 | cron_sleep() |
| 234 | { |
| 235 | extern void do_command(); |
| 236 | extern int job_runqueue(); |
| 237 | register int seconds_to_wait; |
| 238 | |
| 239 | do { |
| 240 | seconds_to_wait = (int) (TargetTime - time((time_t*)0)); |
| 241 | Debug(DSCH, ("[%d] TargetTime=%ld, sec-to-wait=%d\n", |
| 242 | getpid(), TargetTime, seconds_to_wait)) |
| 243 | |
| 244 | /* if we intend to sleep, this means that it's finally |
| 245 | * time to empty the job queue (execute it). |
| 246 | * |
| 247 | * if we run any jobs, we'll probably screw up our timing, |
| 248 | * so go recompute. |
| 249 | * |
| 250 | * note that we depend here on the left-to-right nature |
| 251 | * of &&, and the short-circuiting. |
| 252 | */ |
| 253 | } while (seconds_to_wait > 0 && job_runqueue()); |
| 254 | |
| 255 | if (seconds_to_wait > 0) |
| 256 | { |
| 257 | Debug(DSCH, ("[%d] sleeping for %d seconds\n", |
| 258 | getpid(), seconds_to_wait)) |
| 259 | (void) sleep((unsigned int) seconds_to_wait); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
| 264 | #if defined(BSD) |
| 265 | static void |
| 266 | sigchld_handler() |
| 267 | { |
| 268 | union wait waiter; |
| 269 | int pid; |
| 270 | |
| 271 | for (;;) |
| 272 | { |
| 273 | pid = wait3((int *) &waiter, WNOHANG, (struct rusage *)0); |
| 274 | switch (pid) |
| 275 | { |
| 276 | case -1: |
| 277 | Debug(DPROC, |
| 278 | ("[%d] sigchld...no children\n", getpid())) |
| 279 | return; |
| 280 | case 0: |
| 281 | Debug(DPROC, |
| 282 | ("[%d] sigchld...no dead kids\n", getpid())) |
| 283 | return; |
| 284 | default: |
| 285 | Debug(DPROC, |
| 286 | ("[%d] sigchld...pid #%d died, stat=%d\n", |
| 287 | getpid(), pid, waiter.w_status)) |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | #endif /*BSD*/ |
| 292 | |
| 293 | |
| 294 | static void |
| 295 | parse_args(argc, argv) |
| 296 | int argc; |
| 297 | char *argv[]; |
| 298 | { |
| 299 | extern int optind, getopt(); |
| 300 | extern void usage(); |
| 301 | extern char *optarg; |
| 302 | |
| 303 | int argch; |
| 304 | |
| 305 | while (EOF != (argch = getopt(argc, argv, "x:"))) |
| 306 | { |
| 307 | switch (argch) |
| 308 | { |
| 309 | default: |
| 310 | usage(); |
| 311 | case 'x': |
| 312 | if (!set_debug_flags(optarg)) |
| 313 | usage(); |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | } |