Annotation of /trunk/mkinitrd-magellan/busybox/miscutils/crond.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 22772 byte(s)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 22772 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * crond -d[#] -c <crondir> -f -b | ||
4 | * | ||
5 | * run as root, but NOT setuid root | ||
6 | * | ||
7 | * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) | ||
8 | niro | 816 | * (version 2.3.2) |
9 | niro | 532 | * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 |
10 | * | ||
11 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | ||
12 | */ | ||
13 | |||
14 | niro | 816 | #include "libbb.h" |
15 | #include <syslog.h> | ||
16 | niro | 532 | |
17 | niro | 816 | /* glibc frees previous setenv'ed value when we do next setenv() |
18 | * of the same variable. uclibc does not do this! */ | ||
19 | #if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */ | ||
20 | #define SETENV_LEAKS 0 | ||
21 | #else | ||
22 | #define SETENV_LEAKS 1 | ||
23 | #endif | ||
24 | niro | 532 | |
25 | |||
26 | niro | 984 | #define TMPDIR CONFIG_FEATURE_CROND_DIR |
27 | #define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs" | ||
28 | niro | 532 | #ifndef SENDMAIL |
29 | niro | 816 | #define SENDMAIL "sendmail" |
30 | niro | 532 | #endif |
31 | #ifndef SENDMAIL_ARGS | ||
32 | niro | 984 | #define SENDMAIL_ARGS "-ti", NULL |
33 | niro | 532 | #endif |
34 | #ifndef CRONUPDATE | ||
35 | #define CRONUPDATE "cron.update" | ||
36 | #endif | ||
37 | #ifndef MAXLINES | ||
38 | #define MAXLINES 256 /* max lines in non-root crontabs */ | ||
39 | #endif | ||
40 | |||
41 | niro | 816 | |
42 | niro | 532 | typedef struct CronFile { |
43 | struct CronFile *cf_Next; | ||
44 | struct CronLine *cf_LineBase; | ||
45 | niro | 816 | char *cf_User; /* username */ |
46 | smallint cf_Ready; /* bool: one or more jobs ready */ | ||
47 | smallint cf_Running; /* bool: one or more jobs running */ | ||
48 | smallint cf_Deleted; /* marked for deletion, ignore */ | ||
49 | niro | 532 | } CronFile; |
50 | |||
51 | typedef struct CronLine { | ||
52 | struct CronLine *cl_Next; | ||
53 | niro | 816 | char *cl_Shell; /* shell command */ |
54 | pid_t cl_Pid; /* running pid, 0, or armed (-1) */ | ||
55 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL | ||
56 | int cl_MailPos; /* 'empty file' size */ | ||
57 | smallint cl_MailFlag; /* running pid is for mail */ | ||
58 | niro | 984 | char *cl_MailTo; /* whom to mail results */ |
59 | niro | 816 | #endif |
60 | /* ordered by size, not in natural order. makes code smaller: */ | ||
61 | char cl_Dow[7]; /* 0-6, beginning sunday */ | ||
62 | char cl_Mons[12]; /* 0-11 */ | ||
63 | char cl_Hrs[24]; /* 0-23 */ | ||
64 | char cl_Days[32]; /* 1-31 */ | ||
65 | char cl_Mins[60]; /* 0-59 */ | ||
66 | niro | 532 | } CronLine; |
67 | |||
68 | |||
69 | #define DaemonUid 0 | ||
70 | |||
71 | niro | 816 | |
72 | enum { | ||
73 | OPT_l = (1 << 0), | ||
74 | OPT_L = (1 << 1), | ||
75 | OPT_f = (1 << 2), | ||
76 | OPT_b = (1 << 3), | ||
77 | OPT_S = (1 << 4), | ||
78 | OPT_c = (1 << 5), | ||
79 | OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D, | ||
80 | }; | ||
81 | #if ENABLE_FEATURE_CROND_D | ||
82 | #define DebugOpt (option_mask32 & OPT_d) | ||
83 | #else | ||
84 | #define DebugOpt 0 | ||
85 | niro | 532 | #endif |
86 | |||
87 | |||
88 | niro | 816 | struct globals { |
89 | unsigned LogLevel; /* = 8; */ | ||
90 | const char *LogFile; | ||
91 | const char *CDir; /* = CRONTABS; */ | ||
92 | CronFile *FileBase; | ||
93 | #if SETENV_LEAKS | ||
94 | char *env_var_user; | ||
95 | char *env_var_home; | ||
96 | #endif | ||
97 | }; | ||
98 | #define G (*(struct globals*)&bb_common_bufsiz1) | ||
99 | #define LogLevel (G.LogLevel ) | ||
100 | #define LogFile (G.LogFile ) | ||
101 | #define CDir (G.CDir ) | ||
102 | #define FileBase (G.FileBase ) | ||
103 | #define env_var_user (G.env_var_user ) | ||
104 | #define env_var_home (G.env_var_home ) | ||
105 | #define INIT_G() do { \ | ||
106 | LogLevel = 8; \ | ||
107 | CDir = CRONTABS; \ | ||
108 | } while (0) | ||
109 | niro | 532 | |
110 | niro | 816 | |
111 | niro | 532 | static void CheckUpdates(void); |
112 | static void SynchronizeDir(void); | ||
113 | static int TestJobs(time_t t1, time_t t2); | ||
114 | static void RunJobs(void); | ||
115 | static int CheckJobs(void); | ||
116 | niro | 816 | static void RunJob(const char *user, CronLine *line); |
117 | niro | 532 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
118 | niro | 816 | static void EndJob(const char *user, CronLine *line); |
119 | niro | 532 | #else |
120 | niro | 816 | #define EndJob(user, line) ((line)->cl_Pid = 0) |
121 | niro | 532 | #endif |
122 | static void DeleteFile(const char *userName); | ||
123 | |||
124 | |||
125 | niro | 984 | /* 0 is the most verbose, default 8 */ |
126 | niro | 816 | #define LVL5 "\x05" |
127 | #define LVL7 "\x07" | ||
128 | #define LVL8 "\x08" | ||
129 | #define WARN9 "\x49" | ||
130 | #define DIE9 "\xc9" | ||
131 | /* level >= 20 is "error" */ | ||
132 | #define ERR20 "\x14" | ||
133 | niro | 532 | |
134 | niro | 984 | static void crondlog(const char *ctl, ...) __attribute__ ((format (printf, 1, 2))); |
135 | niro | 532 | static void crondlog(const char *ctl, ...) |
136 | { | ||
137 | va_list va; | ||
138 | niro | 816 | int level = (ctl[0] & 0x1f); |
139 | niro | 532 | |
140 | va_start(va, ctl); | ||
141 | niro | 816 | if (level >= (int)LogLevel) { |
142 | /* Debug mode: all to (non-redirected) stderr, */ | ||
143 | /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */ | ||
144 | if (!DebugOpt && LogFile) { | ||
145 | /* Otherwise (log to file): we reopen log file at every write: */ | ||
146 | niro | 984 | int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0666); |
147 | niro | 816 | if (logfd >= 0) |
148 | xmove_fd(logfd, STDERR_FILENO); | ||
149 | niro | 532 | } |
150 | niro | 984 | /* When we log to syslog, level > 8 is logged at LOG_ERR |
151 | * syslog level, level <= 8 is logged at LOG_INFO. */ | ||
152 | if (level > 8) { | ||
153 | bb_verror_msg(ctl + 1, va, /* strerr: */ NULL); | ||
154 | } else { | ||
155 | char *msg = NULL; | ||
156 | vasprintf(&msg, ctl + 1, va); | ||
157 | bb_info_msg("%s: %s", applet_name, msg); | ||
158 | free(msg); | ||
159 | } | ||
160 | niro | 532 | } |
161 | va_end(va); | ||
162 | niro | 816 | if (ctl[0] & 0x80) |
163 | niro | 532 | exit(20); |
164 | } | ||
165 | |||
166 | niro | 816 | int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
167 | int crond_main(int argc UNUSED_PARAM, char **argv) | ||
168 | niro | 532 | { |
169 | niro | 984 | unsigned opts; |
170 | niro | 532 | |
171 | niro | 816 | INIT_G(); |
172 | niro | 532 | |
173 | niro | 816 | /* "-b after -f is ignored", and so on for every pair a-b */ |
174 | niro | 984 | opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l") |
175 | niro | 816 | ":l+:d+"; /* -l and -d have numeric param */ |
176 | niro | 984 | opts = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"), |
177 | niro | 816 | &LogLevel, &LogFile, &CDir |
178 | niro | 984 | IF_FEATURE_CROND_D(,&LogLevel)); |
179 | niro | 816 | /* both -d N and -l N set the same variable: LogLevel */ |
180 | niro | 532 | |
181 | niro | 984 | if (!(opts & OPT_f)) { |
182 | niro | 816 | /* close stdin, stdout, stderr. |
183 | * close unused descriptors - don't need them. */ | ||
184 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); | ||
185 | niro | 532 | } |
186 | niro | 816 | |
187 | niro | 984 | if (!(opts & OPT_d) && LogFile == NULL) { |
188 | niro | 816 | /* logging to syslog */ |
189 | openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON); | ||
190 | logmode = LOGMODE_SYSLOG; | ||
191 | niro | 532 | } |
192 | |||
193 | xchdir(CDir); | ||
194 | niro | 816 | //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */ |
195 | xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */ | ||
196 | niro | 984 | crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", LogLevel); |
197 | niro | 532 | SynchronizeDir(); |
198 | |||
199 | niro | 816 | /* main loop - synchronize to 1 second after the minute, minimum sleep |
200 | * of 1 second. */ | ||
201 | niro | 532 | { |
202 | time_t t1 = time(NULL); | ||
203 | int rescan = 60; | ||
204 | niro | 816 | int sleep_time = 60; |
205 | niro | 532 | |
206 | niro | 816 | write_pidfile("/var/run/crond.pid"); |
207 | niro | 532 | for (;;) { |
208 | niro | 984 | time_t t2; |
209 | long dt; | ||
210 | |||
211 | niro | 816 | sleep((sleep_time + 1) - (time(NULL) % sleep_time)); |
212 | niro | 532 | |
213 | t2 = time(NULL); | ||
214 | niro | 816 | dt = (long)t2 - (long)t1; |
215 | niro | 532 | |
216 | /* | ||
217 | * The file 'cron.update' is checked to determine new cron | ||
218 | * jobs. The directory is rescanned once an hour to deal | ||
219 | * with any screwups. | ||
220 | * | ||
221 | * check for disparity. Disparities over an hour either way | ||
222 | * result in resynchronization. A reverse-indexed disparity | ||
223 | * less then an hour causes us to effectively sleep until we | ||
224 | * match the original time (i.e. no re-execution of jobs that | ||
225 | * have just been run). A forward-indexed disparity less then | ||
226 | * an hour causes intermediate jobs to be run, but only once | ||
227 | * in the worst case. | ||
228 | * | ||
229 | * when running jobs, the inequality used is greater but not | ||
230 | * equal to t1, and less then or equal to t2. | ||
231 | */ | ||
232 | if (--rescan == 0) { | ||
233 | rescan = 60; | ||
234 | SynchronizeDir(); | ||
235 | } | ||
236 | CheckUpdates(); | ||
237 | if (DebugOpt) | ||
238 | niro | 816 | crondlog(LVL5 "wakeup dt=%ld", dt); |
239 | niro | 532 | if (dt < -60 * 60 || dt > 60 * 60) { |
240 | niro | 984 | crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60); |
241 | niro | 532 | } else if (dt > 0) { |
242 | TestJobs(t1, t2); | ||
243 | RunJobs(); | ||
244 | sleep(5); | ||
245 | if (CheckJobs() > 0) { | ||
246 | sleep_time = 10; | ||
247 | } else { | ||
248 | sleep_time = 60; | ||
249 | } | ||
250 | } | ||
251 | niro | 816 | t1 = t2; |
252 | niro | 984 | } /* for (;;) */ |
253 | niro | 532 | } |
254 | niro | 984 | |
255 | niro | 532 | return 0; /* not reached */ |
256 | } | ||
257 | |||
258 | niro | 816 | #if SETENV_LEAKS |
259 | /* We set environment *before* vfork (because we want to use vfork), | ||
260 | * so we cannot use setenv() - repeated calls to setenv() may leak memory! | ||
261 | * Using putenv(), and freeing memory after unsetenv() won't leak */ | ||
262 | niro | 984 | static void safe_setenv(char **pvar_val, const char *var, const char *val) |
263 | niro | 532 | { |
264 | niro | 816 | char *var_val = *pvar_val; |
265 | niro | 532 | |
266 | niro | 816 | if (var_val) { |
267 | niro | 984 | bb_unsetenv(var_val); |
268 | niro | 816 | free(var_val); |
269 | niro | 532 | } |
270 | niro | 816 | *pvar_val = xasprintf("%s=%s", var, val); |
271 | putenv(*pvar_val); | ||
272 | } | ||
273 | #endif | ||
274 | niro | 532 | |
275 | niro | 816 | static void SetEnv(struct passwd *pas) |
276 | { | ||
277 | #if SETENV_LEAKS | ||
278 | niro | 984 | safe_setenv(&env_var_user, "USER", pas->pw_name); |
279 | safe_setenv(&env_var_home, "HOME", pas->pw_dir); | ||
280 | niro | 816 | /* if we want to set user's shell instead: */ |
281 | niro | 984 | /*safe_setenv(env_var_user, "SHELL", pas->pw_shell);*/ |
282 | niro | 816 | #else |
283 | xsetenv("USER", pas->pw_name); | ||
284 | xsetenv("HOME", pas->pw_dir); | ||
285 | #endif | ||
286 | /* currently, we use constant one: */ | ||
287 | /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */ | ||
288 | niro | 532 | } |
289 | |||
290 | niro | 816 | static void ChangeUser(struct passwd *pas) |
291 | niro | 532 | { |
292 | niro | 816 | /* careful: we're after vfork! */ |
293 | change_identity(pas); /* - initgroups, setgid, setuid */ | ||
294 | if (chdir(pas->pw_dir) < 0) { | ||
295 | niro | 984 | crondlog(WARN9 "can't chdir(%s)", pas->pw_dir); |
296 | niro | 816 | if (chdir(TMPDIR) < 0) { |
297 | crondlog(DIE9 "can't chdir(%s)", TMPDIR); /* exits */ | ||
298 | niro | 532 | } |
299 | } | ||
300 | } | ||
301 | |||
302 | niro | 816 | static const char DowAry[] ALIGN1 = |
303 | "sun""mon""tue""wed""thu""fri""sat" | ||
304 | /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */ | ||
305 | ; | ||
306 | niro | 532 | |
307 | niro | 816 | static const char MonAry[] ALIGN1 = |
308 | "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec" | ||
309 | /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */ | ||
310 | ; | ||
311 | niro | 532 | |
312 | niro | 816 | static void ParseField(char *user, char *ary, int modvalue, int off, |
313 | const char *names, char *ptr) | ||
314 | /* 'names' is a pointer to a set of 3-char abbreviations */ | ||
315 | niro | 532 | { |
316 | char *base = ptr; | ||
317 | int n1 = -1; | ||
318 | int n2 = -1; | ||
319 | |||
320 | niro | 816 | // this can't happen due to config_read() |
321 | /*if (base == NULL) | ||
322 | return;*/ | ||
323 | niro | 532 | |
324 | niro | 816 | while (1) { |
325 | niro | 532 | int skip = 0; |
326 | |||
327 | /* Handle numeric digit or symbol or '*' */ | ||
328 | if (*ptr == '*') { | ||
329 | n1 = 0; /* everything will be filled */ | ||
330 | n2 = modvalue - 1; | ||
331 | skip = 1; | ||
332 | ++ptr; | ||
333 | niro | 816 | } else if (isdigit(*ptr)) { |
334 | niro | 984 | char *endp; |
335 | niro | 532 | if (n1 < 0) { |
336 | niro | 984 | n1 = strtol(ptr, &endp, 10) + off; |
337 | niro | 532 | } else { |
338 | niro | 984 | n2 = strtol(ptr, &endp, 10) + off; |
339 | niro | 532 | } |
340 | niro | 984 | ptr = endp; /* gcc likes temp var for &endp */ |
341 | niro | 532 | skip = 1; |
342 | } else if (names) { | ||
343 | int i; | ||
344 | |||
345 | niro | 816 | for (i = 0; names[i]; i += 3) { |
346 | /* was using strncmp before... */ | ||
347 | if (strncasecmp(ptr, &names[i], 3) == 0) { | ||
348 | ptr += 3; | ||
349 | if (n1 < 0) { | ||
350 | n1 = i / 3; | ||
351 | } else { | ||
352 | n2 = i / 3; | ||
353 | } | ||
354 | skip = 1; | ||
355 | niro | 532 | break; |
356 | } | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /* handle optional range '-' */ | ||
361 | if (skip == 0) { | ||
362 | niro | 816 | goto err; |
363 | niro | 532 | } |
364 | if (*ptr == '-' && n2 < 0) { | ||
365 | ++ptr; | ||
366 | continue; | ||
367 | } | ||
368 | |||
369 | /* | ||
370 | * collapse single-value ranges, handle skipmark, and fill | ||
371 | * in the character array appropriately. | ||
372 | */ | ||
373 | if (n2 < 0) { | ||
374 | n2 = n1; | ||
375 | } | ||
376 | if (*ptr == '/') { | ||
377 | niro | 984 | char *endp; |
378 | skip = strtol(ptr + 1, &endp, 10); | ||
379 | ptr = endp; /* gcc likes temp var for &endp */ | ||
380 | niro | 532 | } |
381 | niro | 816 | |
382 | niro | 532 | /* |
383 | * fill array, using a failsafe is the easiest way to prevent | ||
384 | * an endless loop | ||
385 | */ | ||
386 | { | ||
387 | int s0 = 1; | ||
388 | int failsafe = 1024; | ||
389 | |||
390 | --n1; | ||
391 | do { | ||
392 | n1 = (n1 + 1) % modvalue; | ||
393 | |||
394 | if (--s0 == 0) { | ||
395 | ary[n1 % modvalue] = 1; | ||
396 | s0 = skip; | ||
397 | } | ||
398 | niro | 816 | if (--failsafe == 0) { |
399 | goto err; | ||
400 | } | ||
401 | } while (n1 != n2); | ||
402 | niro | 532 | |
403 | } | ||
404 | if (*ptr != ',') { | ||
405 | break; | ||
406 | } | ||
407 | ++ptr; | ||
408 | n1 = -1; | ||
409 | n2 = -1; | ||
410 | } | ||
411 | |||
412 | niro | 816 | if (*ptr) { |
413 | err: | ||
414 | crondlog(WARN9 "user %s: parse error at %s", user, base); | ||
415 | return; | ||
416 | niro | 532 | } |
417 | |||
418 | niro | 816 | if (DebugOpt && (LogLevel <= 5)) { /* like LVL5 */ |
419 | /* can't use crondlog, it inserts '\n' */ | ||
420 | niro | 532 | int i; |
421 | niro | 816 | for (i = 0; i < modvalue; ++i) |
422 | fprintf(stderr, "%d", (unsigned char)ary[i]); | ||
423 | fputc('\n', stderr); | ||
424 | niro | 532 | } |
425 | } | ||
426 | |||
427 | niro | 816 | static void FixDayDow(CronLine *line) |
428 | niro | 532 | { |
429 | niro | 816 | unsigned i; |
430 | niro | 532 | int weekUsed = 0; |
431 | int daysUsed = 0; | ||
432 | |||
433 | niro | 816 | for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) { |
434 | niro | 532 | if (line->cl_Dow[i] == 0) { |
435 | weekUsed = 1; | ||
436 | break; | ||
437 | } | ||
438 | } | ||
439 | niro | 816 | for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) { |
440 | niro | 532 | if (line->cl_Days[i] == 0) { |
441 | daysUsed = 1; | ||
442 | break; | ||
443 | } | ||
444 | } | ||
445 | niro | 816 | if (weekUsed != daysUsed) { |
446 | if (weekUsed) | ||
447 | memset(line->cl_Days, 0, sizeof(line->cl_Days)); | ||
448 | else /* daysUsed */ | ||
449 | memset(line->cl_Dow, 0, sizeof(line->cl_Dow)); | ||
450 | niro | 532 | } |
451 | } | ||
452 | |||
453 | static void SynchronizeFile(const char *fileName) | ||
454 | { | ||
455 | niro | 816 | struct parser_t *parser; |
456 | struct stat sbuf; | ||
457 | niro | 532 | int maxLines; |
458 | niro | 816 | char *tokens[6]; |
459 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL | ||
460 | char *mailTo = NULL; | ||
461 | #endif | ||
462 | niro | 532 | |
463 | niro | 816 | if (!fileName) |
464 | return; | ||
465 | niro | 532 | |
466 | niro | 816 | DeleteFile(fileName); |
467 | parser = config_open(fileName); | ||
468 | if (!parser) | ||
469 | return; | ||
470 | niro | 532 | |
471 | niro | 816 | maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES; |
472 | niro | 532 | |
473 | niro | 816 | if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) { |
474 | CronFile *file = xzalloc(sizeof(CronFile)); | ||
475 | CronLine **pline; | ||
476 | int n; | ||
477 | niro | 532 | |
478 | niro | 816 | file->cf_User = xstrdup(fileName); |
479 | pline = &file->cf_LineBase; | ||
480 | niro | 532 | |
481 | niro | 816 | while (1) { |
482 | CronLine *line; | ||
483 | niro | 532 | |
484 | niro | 816 | if (!--maxLines) |
485 | break; | ||
486 | n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY); | ||
487 | if (!n) | ||
488 | break; | ||
489 | niro | 532 | |
490 | niro | 816 | if (DebugOpt) |
491 | crondlog(LVL5 "user:%s entry:%s", fileName, parser->data); | ||
492 | niro | 532 | |
493 | niro | 816 | /* check if line is setting MAILTO= */ |
494 | if (0 == strncmp(tokens[0], "MAILTO=", 7)) { | ||
495 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL | ||
496 | free(mailTo); | ||
497 | mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL; | ||
498 | #endif /* otherwise just ignore such lines */ | ||
499 | continue; | ||
500 | } | ||
501 | /* check if a minimum of tokens is specified */ | ||
502 | if (n < 6) | ||
503 | continue; | ||
504 | *pline = line = xzalloc(sizeof(*line)); | ||
505 | /* parse date ranges */ | ||
506 | ParseField(file->cf_User, line->cl_Mins, 60, 0, NULL, tokens[0]); | ||
507 | ParseField(file->cf_User, line->cl_Hrs, 24, 0, NULL, tokens[1]); | ||
508 | ParseField(file->cf_User, line->cl_Days, 32, 0, NULL, tokens[2]); | ||
509 | ParseField(file->cf_User, line->cl_Mons, 12, -1, MonAry, tokens[3]); | ||
510 | ParseField(file->cf_User, line->cl_Dow, 7, 0, DowAry, tokens[4]); | ||
511 | /* | ||
512 | * fix days and dow - if one is not "*" and the other | ||
513 | * is "*", the other is set to 0, and vise-versa | ||
514 | */ | ||
515 | FixDayDow(line); | ||
516 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL | ||
517 | /* copy mailto (can be NULL) */ | ||
518 | line->cl_MailTo = xstrdup(mailTo); | ||
519 | niro | 532 | #endif |
520 | niro | 816 | /* copy command */ |
521 | line->cl_Shell = xstrdup(tokens[5]); | ||
522 | if (DebugOpt) { | ||
523 | crondlog(LVL5 " command:%s", tokens[5]); | ||
524 | } | ||
525 | pline = &line->cl_Next; | ||
526 | //bb_error_msg("M[%s]F[%s][%s][%s][%s][%s][%s]", mailTo, tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]); | ||
527 | } | ||
528 | *pline = NULL; | ||
529 | niro | 532 | |
530 | niro | 816 | file->cf_Next = FileBase; |
531 | FileBase = file; | ||
532 | niro | 532 | |
533 | niro | 816 | if (maxLines == 0) { |
534 | crondlog(WARN9 "user %s: too many lines", fileName); | ||
535 | niro | 532 | } |
536 | } | ||
537 | niro | 816 | config_close(parser); |
538 | niro | 532 | } |
539 | |||
540 | static void CheckUpdates(void) | ||
541 | { | ||
542 | FILE *fi; | ||
543 | char buf[256]; | ||
544 | |||
545 | niro | 816 | fi = fopen_for_read(CRONUPDATE); |
546 | niro | 532 | if (fi != NULL) { |
547 | niro | 816 | unlink(CRONUPDATE); |
548 | niro | 532 | while (fgets(buf, sizeof(buf), fi) != NULL) { |
549 | niro | 816 | /* use first word only */ |
550 | niro | 532 | SynchronizeFile(strtok(buf, " \t\r\n")); |
551 | } | ||
552 | fclose(fi); | ||
553 | } | ||
554 | } | ||
555 | |||
556 | static void SynchronizeDir(void) | ||
557 | { | ||
558 | niro | 816 | CronFile *file; |
559 | niro | 532 | /* Attempt to delete the database. */ |
560 | niro | 816 | again: |
561 | for (file = FileBase; file; file = file->cf_Next) { | ||
562 | if (!file->cf_Deleted) { | ||
563 | DeleteFile(file->cf_User); | ||
564 | goto again; | ||
565 | niro | 532 | } |
566 | } | ||
567 | |||
568 | /* | ||
569 | * Remove cron update file | ||
570 | * | ||
571 | * Re-chdir, in case directory was renamed & deleted, or otherwise | ||
572 | * screwed up. | ||
573 | * | ||
574 | * scan directory and add associated users | ||
575 | */ | ||
576 | niro | 816 | unlink(CRONUPDATE); |
577 | niro | 532 | if (chdir(CDir) < 0) { |
578 | niro | 816 | crondlog(DIE9 "can't chdir(%s)", CDir); |
579 | niro | 532 | } |
580 | { | ||
581 | DIR *dir = opendir("."); | ||
582 | struct dirent *den; | ||
583 | |||
584 | niro | 816 | if (!dir) |
585 | crondlog(DIE9 "can't chdir(%s)", "."); /* exits */ | ||
586 | while ((den = readdir(dir)) != NULL) { | ||
587 | if (strchr(den->d_name, '.') != NULL) { | ||
588 | continue; | ||
589 | niro | 532 | } |
590 | niro | 816 | if (getpwnam(den->d_name)) { |
591 | SynchronizeFile(den->d_name); | ||
592 | } else { | ||
593 | crondlog(LVL7 "ignoring %s", den->d_name); | ||
594 | } | ||
595 | niro | 532 | } |
596 | niro | 816 | closedir(dir); |
597 | niro | 532 | } |
598 | } | ||
599 | |||
600 | /* | ||
601 | * DeleteFile() - delete user database | ||
602 | * | ||
603 | * Note: multiple entries for same user may exist if we were unable to | ||
604 | * completely delete a database due to running processes. | ||
605 | */ | ||
606 | static void DeleteFile(const char *userName) | ||
607 | { | ||
608 | CronFile **pfile = &FileBase; | ||
609 | CronFile *file; | ||
610 | |||
611 | while ((file = *pfile) != NULL) { | ||
612 | if (strcmp(userName, file->cf_User) == 0) { | ||
613 | CronLine **pline = &file->cf_LineBase; | ||
614 | CronLine *line; | ||
615 | |||
616 | file->cf_Running = 0; | ||
617 | file->cf_Deleted = 1; | ||
618 | |||
619 | while ((line = *pline) != NULL) { | ||
620 | if (line->cl_Pid > 0) { | ||
621 | file->cf_Running = 1; | ||
622 | pline = &line->cl_Next; | ||
623 | } else { | ||
624 | *pline = line->cl_Next; | ||
625 | free(line->cl_Shell); | ||
626 | free(line); | ||
627 | } | ||
628 | } | ||
629 | if (file->cf_Running == 0) { | ||
630 | *pfile = file->cf_Next; | ||
631 | free(file->cf_User); | ||
632 | free(file); | ||
633 | } else { | ||
634 | pfile = &file->cf_Next; | ||
635 | } | ||
636 | } else { | ||
637 | pfile = &file->cf_Next; | ||
638 | } | ||
639 | } | ||
640 | } | ||
641 | |||
642 | /* | ||
643 | * TestJobs() | ||
644 | * | ||
645 | * determine which jobs need to be run. Under normal conditions, the | ||
646 | * period is about a minute (one scan). Worst case it will be one | ||
647 | * hour (60 scans). | ||
648 | */ | ||
649 | static int TestJobs(time_t t1, time_t t2) | ||
650 | { | ||
651 | int nJobs = 0; | ||
652 | time_t t; | ||
653 | |||
654 | /* Find jobs > t1 and <= t2 */ | ||
655 | |||
656 | for (t = t1 - t1 % 60; t <= t2; t += 60) { | ||
657 | niro | 984 | struct tm *ptm; |
658 | niro | 816 | CronFile *file; |
659 | CronLine *line; | ||
660 | niro | 532 | |
661 | niro | 816 | if (t <= t1) |
662 | continue; | ||
663 | |||
664 | niro | 984 | ptm = localtime(&t); |
665 | niro | 816 | for (file = FileBase; file; file = file->cf_Next) { |
666 | if (DebugOpt) | ||
667 | crondlog(LVL5 "file %s:", file->cf_User); | ||
668 | if (file->cf_Deleted) | ||
669 | continue; | ||
670 | for (line = file->cf_LineBase; line; line = line->cl_Next) { | ||
671 | niro | 532 | if (DebugOpt) |
672 | niro | 816 | crondlog(LVL5 " line %s", line->cl_Shell); |
673 | niro | 984 | if (line->cl_Mins[ptm->tm_min] && line->cl_Hrs[ptm->tm_hour] |
674 | && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday]) | ||
675 | && line->cl_Mons[ptm->tm_mon] | ||
676 | niro | 816 | ) { |
677 | if (DebugOpt) { | ||
678 | crondlog(LVL5 " job: %d %s", | ||
679 | (int)line->cl_Pid, line->cl_Shell); | ||
680 | niro | 532 | } |
681 | niro | 816 | if (line->cl_Pid > 0) { |
682 | crondlog(LVL8 "user %s: process already running: %s", | ||
683 | file->cf_User, line->cl_Shell); | ||
684 | } else if (line->cl_Pid == 0) { | ||
685 | line->cl_Pid = -1; | ||
686 | file->cf_Ready = 1; | ||
687 | ++nJobs; | ||
688 | } | ||
689 | niro | 532 | } |
690 | } | ||
691 | } | ||
692 | } | ||
693 | return nJobs; | ||
694 | } | ||
695 | |||
696 | static void RunJobs(void) | ||
697 | { | ||
698 | CronFile *file; | ||
699 | CronLine *line; | ||
700 | |||
701 | for (file = FileBase; file; file = file->cf_Next) { | ||
702 | niro | 816 | if (!file->cf_Ready) |
703 | continue; | ||
704 | niro | 532 | |
705 | niro | 816 | file->cf_Ready = 0; |
706 | for (line = file->cf_LineBase; line; line = line->cl_Next) { | ||
707 | if (line->cl_Pid >= 0) | ||
708 | continue; | ||
709 | niro | 532 | |
710 | niro | 816 | RunJob(file->cf_User, line); |
711 | crondlog(LVL8 "USER %s pid %3d cmd %s", | ||
712 | file->cf_User, (int)line->cl_Pid, line->cl_Shell); | ||
713 | if (line->cl_Pid < 0) { | ||
714 | file->cf_Ready = 1; | ||
715 | } else if (line->cl_Pid > 0) { | ||
716 | file->cf_Running = 1; | ||
717 | niro | 532 | } |
718 | } | ||
719 | } | ||
720 | } | ||
721 | |||
722 | /* | ||
723 | * CheckJobs() - check for job completion | ||
724 | * | ||
725 | * Check for job completion, return number of jobs still running after | ||
726 | * all done. | ||
727 | */ | ||
728 | static int CheckJobs(void) | ||
729 | { | ||
730 | CronFile *file; | ||
731 | CronLine *line; | ||
732 | int nStillRunning = 0; | ||
733 | |||
734 | for (file = FileBase; file; file = file->cf_Next) { | ||
735 | if (file->cf_Running) { | ||
736 | file->cf_Running = 0; | ||
737 | |||
738 | for (line = file->cf_LineBase; line; line = line->cl_Next) { | ||
739 | niro | 816 | int status, r; |
740 | if (line->cl_Pid <= 0) | ||
741 | continue; | ||
742 | niro | 532 | |
743 | niro | 816 | r = waitpid(line->cl_Pid, &status, WNOHANG); |
744 | if (r < 0 || r == line->cl_Pid) { | ||
745 | EndJob(file->cf_User, line); | ||
746 | if (line->cl_Pid) { | ||
747 | niro | 532 | file->cf_Running = 1; |
748 | } | ||
749 | niro | 816 | } else if (r == 0) { |
750 | file->cf_Running = 1; | ||
751 | niro | 532 | } |
752 | } | ||
753 | } | ||
754 | nStillRunning += file->cf_Running; | ||
755 | } | ||
756 | return nStillRunning; | ||
757 | } | ||
758 | |||
759 | niro | 816 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
760 | niro | 532 | |
761 | niro | 816 | // TODO: sendmail should be _run-time_ option, not compile-time! |
762 | |||
763 | niro | 532 | static void |
764 | niro | 816 | ForkJob(const char *user, CronLine *line, int mailFd, |
765 | const char *prog, const char *cmd, const char *arg, | ||
766 | const char *mail_filename) | ||
767 | niro | 532 | { |
768 | niro | 816 | struct passwd *pas; |
769 | pid_t pid; | ||
770 | niro | 532 | |
771 | niro | 816 | /* prepare things before vfork */ |
772 | pas = getpwnam(user); | ||
773 | if (!pas) { | ||
774 | niro | 984 | crondlog(WARN9 "can't get uid for %s", user); |
775 | niro | 816 | goto err; |
776 | } | ||
777 | SetEnv(pas); | ||
778 | |||
779 | pid = vfork(); | ||
780 | niro | 532 | if (pid == 0) { |
781 | /* CHILD */ | ||
782 | niro | 816 | /* change running state to the user in question */ |
783 | ChangeUser(pas); | ||
784 | niro | 532 | if (DebugOpt) { |
785 | niro | 816 | crondlog(LVL5 "child running %s", prog); |
786 | niro | 532 | } |
787 | if (mailFd >= 0) { | ||
788 | niro | 816 | xmove_fd(mailFd, mail_filename ? 1 : 0); |
789 | dup2(1, 2); | ||
790 | niro | 532 | } |
791 | niro | 816 | /* crond 3.0pl1-100 puts tasks in separate process groups */ |
792 | bb_setpgrp(); | ||
793 | niro | 984 | execlp(prog, prog, cmd, arg, (char *) NULL); |
794 | niro | 816 | crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg); |
795 | if (mail_filename) { | ||
796 | niro | 532 | fdprintf(1, "Exec failed: %s -c %s\n", prog, arg); |
797 | } | ||
798 | niro | 816 | _exit(EXIT_SUCCESS); |
799 | } | ||
800 | |||
801 | line->cl_Pid = pid; | ||
802 | if (pid < 0) { | ||
803 | niro | 532 | /* FORK FAILED */ |
804 | niro | 816 | crondlog(ERR20 "can't vfork"); |
805 | err: | ||
806 | niro | 532 | line->cl_Pid = 0; |
807 | niro | 816 | if (mail_filename) { |
808 | unlink(mail_filename); | ||
809 | niro | 532 | } |
810 | niro | 816 | } else if (mail_filename) { |
811 | niro | 532 | /* PARENT, FORK SUCCESS |
812 | * rename mail-file based on pid of process | ||
813 | */ | ||
814 | char mailFile2[128]; | ||
815 | |||
816 | niro | 816 | snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid); |
817 | rename(mail_filename, mailFile2); // TODO: xrename? | ||
818 | niro | 532 | } |
819 | niro | 816 | |
820 | niro | 532 | /* |
821 | * Close the mail file descriptor.. we can't just leave it open in | ||
822 | * a structure, closing it later, because we might run out of descriptors | ||
823 | */ | ||
824 | if (mailFd >= 0) { | ||
825 | close(mailFd); | ||
826 | } | ||
827 | } | ||
828 | |||
829 | niro | 816 | static void RunJob(const char *user, CronLine *line) |
830 | niro | 532 | { |
831 | char mailFile[128]; | ||
832 | niro | 816 | int mailFd = -1; |
833 | niro | 532 | |
834 | line->cl_Pid = 0; | ||
835 | line->cl_MailFlag = 0; | ||
836 | |||
837 | niro | 816 | if (line->cl_MailTo) { |
838 | /* open mail file - owner root so nobody can screw with it. */ | ||
839 | snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid()); | ||
840 | mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600); | ||
841 | niro | 532 | |
842 | niro | 816 | if (mailFd >= 0) { |
843 | line->cl_MailFlag = 1; | ||
844 | fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_MailTo, | ||
845 | line->cl_Shell); | ||
846 | line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR); | ||
847 | } else { | ||
848 | niro | 984 | crondlog(ERR20 "can't create mail file %s for user %s, " |
849 | niro | 816 | "discarding output", mailFile, user); |
850 | } | ||
851 | niro | 532 | } |
852 | |||
853 | ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile); | ||
854 | } | ||
855 | |||
856 | /* | ||
857 | * EndJob - called when job terminates and when mail terminates | ||
858 | */ | ||
859 | niro | 816 | static void EndJob(const char *user, CronLine *line) |
860 | niro | 532 | { |
861 | int mailFd; | ||
862 | char mailFile[128]; | ||
863 | struct stat sbuf; | ||
864 | |||
865 | /* No job */ | ||
866 | if (line->cl_Pid <= 0) { | ||
867 | line->cl_Pid = 0; | ||
868 | return; | ||
869 | } | ||
870 | |||
871 | /* | ||
872 | * End of job and no mail file | ||
873 | * End of sendmail job | ||
874 | */ | ||
875 | niro | 816 | snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, line->cl_Pid); |
876 | niro | 532 | line->cl_Pid = 0; |
877 | |||
878 | niro | 816 | if (line->cl_MailFlag == 0) { |
879 | niro | 532 | return; |
880 | } | ||
881 | line->cl_MailFlag = 0; | ||
882 | |||
883 | /* | ||
884 | * End of primary job - check for mail file. If size has increased and | ||
885 | * the file is still valid, we sendmail it. | ||
886 | */ | ||
887 | mailFd = open(mailFile, O_RDONLY); | ||
888 | niro | 816 | unlink(mailFile); |
889 | niro | 532 | if (mailFd < 0) { |
890 | return; | ||
891 | } | ||
892 | |||
893 | niro | 816 | if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid |
894 | || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos | ||
895 | || !S_ISREG(sbuf.st_mode) | ||
896 | ) { | ||
897 | niro | 532 | close(mailFd); |
898 | return; | ||
899 | } | ||
900 | niro | 816 | if (line->cl_MailTo) |
901 | ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL); | ||
902 | niro | 532 | } |
903 | |||
904 | niro | 816 | #else /* crond without sendmail */ |
905 | |||
906 | static void RunJob(const char *user, CronLine *line) | ||
907 | niro | 532 | { |
908 | niro | 816 | struct passwd *pas; |
909 | pid_t pid; | ||
910 | niro | 532 | |
911 | niro | 816 | /* prepare things before vfork */ |
912 | pas = getpwnam(user); | ||
913 | if (!pas) { | ||
914 | niro | 984 | crondlog(WARN9 "can't get uid for %s", user); |
915 | niro | 816 | goto err; |
916 | } | ||
917 | SetEnv(pas); | ||
918 | |||
919 | /* fork as the user in question and run program */ | ||
920 | pid = vfork(); | ||
921 | niro | 532 | if (pid == 0) { |
922 | /* CHILD */ | ||
923 | niro | 816 | /* change running state to the user in question */ |
924 | ChangeUser(pas); | ||
925 | niro | 532 | if (DebugOpt) { |
926 | niro | 816 | crondlog(LVL5 "child running %s", DEFAULT_SHELL); |
927 | niro | 532 | } |
928 | niro | 816 | /* crond 3.0pl1-100 puts tasks in separate process groups */ |
929 | bb_setpgrp(); | ||
930 | niro | 984 | execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, (char *) NULL); |
931 | niro | 816 | crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, |
932 | DEFAULT_SHELL, "-c", line->cl_Shell); | ||
933 | _exit(EXIT_SUCCESS); | ||
934 | } | ||
935 | if (pid < 0) { | ||
936 | niro | 532 | /* FORK FAILED */ |
937 | niro | 816 | crondlog(ERR20 "can't vfork"); |
938 | err: | ||
939 | niro | 532 | pid = 0; |
940 | } | ||
941 | line->cl_Pid = pid; | ||
942 | } | ||
943 | niro | 816 | |
944 | niro | 532 | #endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */ |