1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78#include "libbb.h"
79#include "common_bufsiz.h"
80#include <syslog.h>
81
82#if 0
83
84
85# define SETENV_LEAKS 0
86#else
87# define SETENV_LEAKS 1
88#endif
89
90
91#define CRON_DIR CONFIG_FEATURE_CROND_DIR
92#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
93#define CRON_REBOOT CONFIG_PID_FILE_PATH "/crond.reboot"
94#ifndef SENDMAIL
95# define SENDMAIL "sendmail"
96#endif
97#ifndef SENDMAIL_ARGS
98# define SENDMAIL_ARGS "-ti"
99#endif
100#ifndef CRONUPDATE
101# define CRONUPDATE "cron.update"
102#endif
103#ifndef MAXLINES
104# define MAXLINES 256
105#endif
106
107
108typedef struct CronFile {
109 struct CronFile *cf_next;
110 struct CronLine *cf_lines;
111 char *cf_username;
112 smallint cf_wants_starting;
113 smallint cf_has_running;
114 smallint cf_deleted;
115} CronFile;
116
117typedef struct CronLine {
118 struct CronLine *cl_next;
119 char *cl_cmd;
120 pid_t cl_pid;
121#define START_ME_REBOOT -2
122#define START_ME_NORMAL -1
123#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
124 int cl_empty_mail_size;
125 char *cl_mailto;
126#endif
127 char *cl_shell;
128 char *cl_path;
129
130 char cl_Dow[7];
131 char cl_Mons[12];
132 char cl_Hrs[24];
133 char cl_Days[32];
134 char cl_Mins[60];
135} CronLine;
136
137
138#define DAEMON_UID 0
139
140
141enum {
142 OPT_l = (1 << 0),
143 OPT_L = (1 << 1),
144 OPT_f = (1 << 2),
145 OPT_b = (1 << 3),
146 OPT_S = (1 << 4),
147 OPT_c = (1 << 5),
148 OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
149};
150
151struct globals {
152 unsigned log_level;
153 time_t crontab_dir_mtime;
154 const char *log_filename;
155 const char *crontab_dir_name;
156 CronFile *cron_files;
157 char *default_shell;
158#if SETENV_LEAKS
159 char *env_var_user;
160 char *env_var_home;
161 char *env_var_shell;
162 char *env_var_logname;
163#endif
164} FIX_ALIASING;
165#define G (*(struct globals*)bb_common_bufsiz1)
166#define INIT_G() do { \
167 setup_common_bufsiz(); \
168 G.log_level = 8; \
169 G.crontab_dir_name = CRONTABS; \
170} while (0)
171
172
173
174
175
176static void crondlog(unsigned level, const char *msg, va_list va)
177{
178 if (level >= G.log_level) {
179
180
181
182
183
184
185 bb_vinfo_msg(msg, va);
186 }
187}
188
189static void log5(const char *msg, ...)
190{
191 va_list va;
192 va_start(va, msg);
193 crondlog(4, msg, va);
194 va_end(va);
195}
196
197static void log7(const char *msg, ...)
198{
199 va_list va;
200 va_start(va, msg);
201 crondlog(7, msg, va);
202 va_end(va);
203}
204
205static void log8(const char *msg, ...)
206{
207 va_list va;
208 va_start(va, msg);
209 crondlog(8, msg, va);
210 va_end(va);
211}
212
213
214static const char DowAry[] ALIGN1 =
215 "sun""mon""tue""wed""thu""fri""sat"
216;
217
218static const char MonAry[] ALIGN1 =
219 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
220;
221
222static void ParseField(char *user, char *ary, int modvalue, int off,
223 const char *names, char *ptr)
224
225{
226 char *base = ptr;
227 int n1 = -1;
228 int n2 = -1;
229
230
231
232
233
234 while (1) {
235 int skip = 0;
236
237
238 if (*ptr == '*') {
239 n1 = 0;
240 n2 = modvalue - 1;
241 skip = 1;
242 ++ptr;
243 } else if (isdigit(*ptr)) {
244 char *endp;
245 if (n1 < 0) {
246 n1 = strtol(ptr, &endp, 10) + off;
247 } else {
248 n2 = strtol(ptr, &endp, 10) + off;
249 }
250 ptr = endp;
251 skip = 1;
252 } else if (names) {
253 int i;
254
255 for (i = 0; names[i]; i += 3) {
256
257 if (strncasecmp(ptr, &names[i], 3) == 0) {
258 ptr += 3;
259 if (n1 < 0) {
260 n1 = i / 3;
261 } else {
262 n2 = i / 3;
263 }
264 skip = 1;
265 break;
266 }
267 }
268 }
269
270
271 if (skip == 0) {
272 goto err;
273 }
274 if (*ptr == '-' && n2 < 0) {
275 ++ptr;
276 continue;
277 }
278
279
280
281
282
283 if (n2 < 0) {
284 n2 = n1;
285 }
286 if (*ptr == '/') {
287 char *endp;
288 skip = strtol(ptr + 1, &endp, 10);
289 ptr = endp;
290 }
291
292
293
294
295
296 {
297 int s0 = 1;
298 int failsafe = 1024;
299
300 --n1;
301 do {
302 n1 = (n1 + 1) % modvalue;
303
304 if (--s0 == 0) {
305 ary[n1 % modvalue] = 1;
306 s0 = skip;
307 }
308 if (--failsafe == 0) {
309 goto err;
310 }
311 } while (n1 != n2);
312 }
313 if (*ptr != ',') {
314 break;
315 }
316 ++ptr;
317 n1 = -1;
318 n2 = -1;
319 }
320
321 if (*ptr) {
322 err:
323 bb_error_msg("user %s: parse error at %s", user, base);
324 return;
325 }
326
327
328 if (G.log_level <= 5 && logmode != LOGMODE_SYSLOG) {
329 int i;
330 for (i = 0; i < modvalue; ++i)
331 fprintf(stderr, "%d", (unsigned char)ary[i]);
332 bb_putchar_stderr('\n');
333 }
334}
335
336static void FixDayDow(CronLine *line)
337{
338 unsigned i;
339 int weekUsed = 0;
340 int daysUsed = 0;
341
342 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
343 if (line->cl_Dow[i] == 0) {
344 weekUsed = 1;
345 break;
346 }
347 }
348 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
349 if (line->cl_Days[i] == 0) {
350 daysUsed = 1;
351 break;
352 }
353 }
354 if (weekUsed != daysUsed) {
355 if (weekUsed)
356 memset(line->cl_Days, 0, sizeof(line->cl_Days));
357 else
358 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
359 }
360}
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381static void delete_cronfile(const char *userName)
382{
383 CronFile **pfile = &G.cron_files;
384 CronFile *file;
385
386 while ((file = *pfile) != NULL) {
387 if (strcmp(userName, file->cf_username) == 0) {
388 CronLine **pline = &file->cf_lines;
389 CronLine *line;
390
391 file->cf_has_running = 0;
392 file->cf_deleted = 1;
393
394 while ((line = *pline) != NULL) {
395 if (line->cl_pid > 0) {
396 file->cf_has_running = 1;
397 pline = &line->cl_next;
398 } else {
399 *pline = line->cl_next;
400 free(line->cl_cmd);
401 free(line);
402 }
403 }
404 if (file->cf_has_running == 0) {
405 *pfile = file->cf_next;
406 free(file->cf_username);
407 free(file);
408 continue;
409 }
410 }
411 pfile = &file->cf_next;
412 }
413}
414
415static void load_crontab(const char *fileName)
416{
417 struct parser_t *parser;
418 struct stat sbuf;
419 int maxLines;
420 char *tokens[6];
421#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
422 char *mailTo = NULL;
423#endif
424 char *shell = NULL;
425 char *path = NULL;
426
427 delete_cronfile(fileName);
428
429 if (!getpwnam(fileName)) {
430 log7("ignoring file '%s' (no such user)", fileName);
431 return;
432 }
433
434 parser = config_open(fileName);
435 if (!parser)
436 return;
437
438 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
439
440 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
441 CronFile *file = xzalloc(sizeof(CronFile));
442 CronLine **pline;
443 int n;
444
445 file->cf_username = xstrdup(fileName);
446 pline = &file->cf_lines;
447
448 while (1) {
449 CronLine *line;
450
451 if (!--maxLines) {
452 bb_error_msg("user %s: too many lines", fileName);
453 break;
454 }
455
456 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
457 if (!n)
458 break;
459
460 log5("user:%s entry:%s", fileName, parser->data);
461
462
463 if (is_prefixed_with(tokens[0], "MAILTO=")) {
464#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
465 free(mailTo);
466 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
467#endif
468 continue;
469 }
470 if (is_prefixed_with(tokens[0], "SHELL=")) {
471 free(shell);
472 shell = xstrdup(&tokens[0][6]);
473 continue;
474 }
475 if (is_prefixed_with(tokens[0], "PATH=")) {
476 free(path);
477 path = xstrdup(&tokens[0][5]);
478 continue;
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492
493#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
494 if (tokens[0][0] == '@') {
495
496
497
498 typedef struct SpecialEntry {
499 const char *name;
500 const char tokens[8];
501 } SpecialEntry;
502 static const SpecialEntry SpecAry[] ALIGN8 = {
503
504 { "yearly", "0\0" "1\0" "1\0" "*" },
505 { "annually", "0\0" "1\0" "1\0" "*" },
506 { "monthly", "0\0" "1\0" "*\0" "*" },
507 { "weekly", "0\0" "*\0" "*\0" "0" },
508 { "daily", "0\0" "*\0" "*\0" "*" },
509 { "midnight", "0\0" "*\0" "*\0" "*" },
510 { "hourly", "*\0" "*\0" "*\0" "*" },
511 { "reboot", "" },
512 };
513 const SpecialEntry *e = SpecAry;
514
515 if (n < 2)
516 continue;
517 for (;;) {
518 if (strcmp(e->name, tokens[0] + 1) == 0) {
519
520
521
522
523
524 tokens[5] = skip_whitespace(
525 skip_non_whitespace(
526 skip_whitespace(parser->data)));
527 if (e->tokens[0]) {
528 char *et = (char*)e->tokens;
529
530 tokens[0] = (char*)"0";
531 tokens[1] = et;
532 tokens[2] = et + 2;
533 tokens[3] = et + 4;
534 tokens[4] = et + 6;
535 }
536 goto got_it;
537 }
538 if (!e->tokens[0])
539 break;
540 e++;
541 }
542 continue;
543 }
544#endif
545
546 if (n < 6)
547 continue;
548 IF_FEATURE_CROND_SPECIAL_TIMES(
549 got_it:
550 )
551 *pline = line = xzalloc(sizeof(*line));
552#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
553 if (tokens[0][0] == '@') {
554 file->cf_wants_starting = 1;
555 line->cl_pid = START_ME_REBOOT;
556
557 } else
558#endif
559 {
560
561 ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
562 ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
563 ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
564 ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
565 ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
566
567
568
569
570 FixDayDow(line);
571 }
572#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
573
574 line->cl_mailto = xstrdup(mailTo);
575#endif
576 line->cl_shell = xstrdup(shell);
577 line->cl_path = xstrdup(path);
578
579 line->cl_cmd = xstrdup(tokens[5]);
580 pline = &line->cl_next;
581
582 }
583 *pline = NULL;
584
585 file->cf_next = G.cron_files;
586 G.cron_files = file;
587 }
588 config_close(parser);
589#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
590 free(mailTo);
591#endif
592 free(shell);
593}
594
595static void process_cron_update_file(void)
596{
597 FILE *fi;
598 char buf[256];
599
600 fi = fopen_for_read(CRONUPDATE);
601 if (fi != NULL) {
602 unlink(CRONUPDATE);
603 while (fgets(buf, sizeof(buf), fi) != NULL) {
604
605 skip_non_whitespace(buf)[0] = '\0';
606 load_crontab(buf);
607 }
608 fclose(fi);
609 }
610}
611
612static void rescan_crontab_dir(void)
613{
614 CronFile *file;
615
616
617 again:
618 for (file = G.cron_files; file; file = file->cf_next) {
619 if (!file->cf_deleted) {
620 delete_cronfile(file->cf_username);
621 goto again;
622 }
623 }
624
625
626 unlink(CRONUPDATE);
627
628 xchdir(G.crontab_dir_name);
629
630
631 {
632 DIR *dir = opendir(".");
633 struct dirent *den;
634
635
636 if (!dir)
637 bb_error_msg_and_die("can't open '%s'", G.crontab_dir_name);
638 while ((den = readdir(dir)) != NULL) {
639 if (strchr(den->d_name, '.') != NULL) {
640 continue;
641 }
642 load_crontab(den->d_name);
643 }
644 closedir(dir);
645 }
646}
647
648#if SETENV_LEAKS
649
650
651
652static void safe_setenv(char **pvar_val, const char *var, const char *val)
653{
654 char *var_val = *pvar_val;
655
656 if (var_val) {
657 bb_unsetenv_and_free(var_val);
658 }
659 *pvar_val = xasprintf("%s=%s", var, val);
660 putenv(*pvar_val);
661}
662#endif
663
664static void set_env_vars(struct passwd *pas, const char *shell, const char *path)
665{
666
667
668#if SETENV_LEAKS
669 safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name);
670 safe_setenv(&G.env_var_user, "USER", pas->pw_name);
671 safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
672 safe_setenv(&G.env_var_shell, "SHELL", shell);
673 if (path) safe_setenv(&G.env_var_shell, "PATH", path);
674#else
675 xsetenv("LOGNAME", pas->pw_name);
676 xsetenv("USER", pas->pw_name);
677 xsetenv("HOME", pas->pw_dir);
678 xsetenv("SHELL", shell);
679 if (path) xsetenv("PATH", path);
680#endif
681}
682
683static void change_user(struct passwd *pas)
684{
685
686 change_identity(pas);
687 if (chdir_or_warn(pas->pw_dir) != 0) {
688 xchdir(CRON_DIR);
689 }
690}
691
692
693#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
694
695static pid_t
696fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail)
697{
698 struct passwd *pas;
699 const char *shell, *prog;
700 smallint sv_logmode;
701 pid_t pid;
702
703
704 pas = getpwnam(user);
705 if (!pas) {
706 bb_error_msg("can't get uid for %s", user);
707 goto err;
708 }
709
710 shell = line->cl_shell ? line->cl_shell : G.default_shell;
711 prog = run_sendmail ? SENDMAIL : shell;
712
713 set_env_vars(pas, shell, NULL);
714
715 sv_logmode = logmode;
716 pid = vfork();
717 if (pid == 0) {
718
719
720 change_user(pas);
721 log5("child running %s", prog);
722 if (mailFd >= 0) {
723 xmove_fd(mailFd, run_sendmail ? 0 : 1);
724 dup2(1, 2);
725 }
726
727 bb_setpgrp();
728 if (!run_sendmail)
729 execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL);
730 else
731 execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL);
732
733
734
735
736 logmode |= LOGMODE_STDIO;
737 bb_error_msg_and_die("can't execute '%s' for user %s", prog, user);
738 }
739 logmode = sv_logmode;
740
741 if (pid < 0) {
742 bb_simple_perror_msg("vfork");
743 err:
744 pid = 0;
745 }
746
747
748
749
750
751 if (mailFd >= 0) {
752 close(mailFd);
753 }
754 return pid;
755}
756
757static pid_t start_one_job(const char *user, CronLine *line)
758{
759 char mailFile[128];
760 int mailFd = -1;
761
762 line->cl_pid = 0;
763 line->cl_empty_mail_size = 0;
764
765 if (line->cl_mailto) {
766
767 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, getpid());
768 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
769
770 if (mailFd >= 0) {
771 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
772 line->cl_cmd);
773 line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
774 } else {
775 bb_error_msg("can't create mail file %s for user %s, "
776 "discarding output", mailFile, user);
777 }
778 }
779
780 line->cl_pid = fork_job(user, mailFd, line, 0);
781 if (mailFd >= 0) {
782 if (line->cl_pid <= 0) {
783 unlink(mailFile);
784 } else {
785
786 char *mailFile2 = xasprintf("%s/cron.%s.%d", CRON_DIR, user, (int)line->cl_pid);
787 rename(mailFile, mailFile2);
788 free(mailFile2);
789 }
790 }
791
792 return line->cl_pid;
793}
794
795
796
797
798static void process_finished_job(const char *user, CronLine *line)
799{
800 pid_t pid;
801 int mailFd;
802 char mailFile[128];
803 struct stat sbuf;
804
805 pid = line->cl_pid;
806 line->cl_pid = 0;
807 if (pid <= 0) {
808
809 return;
810 }
811 if (line->cl_empty_mail_size <= 0) {
812
813 return;
814 }
815
816
817
818
819
820 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, (int)pid);
821 mailFd = open(mailFile, O_RDONLY);
822 unlink(mailFile);
823 if (mailFd < 0) {
824 return;
825 }
826
827 if (fstat(mailFd, &sbuf) < 0
828 || sbuf.st_uid != DAEMON_UID
829 || sbuf.st_nlink != 0
830 || sbuf.st_size == line->cl_empty_mail_size
831 || !S_ISREG(sbuf.st_mode)
832 ) {
833 close(mailFd);
834 return;
835 }
836 line->cl_empty_mail_size = 0;
837
838 line->cl_pid = fork_job(user, mailFd, line, 1);
839}
840
841#else
842
843static pid_t start_one_job(const char *user, CronLine *line)
844{
845 const char *shell;
846 struct passwd *pas;
847 pid_t pid;
848
849 pas = getpwnam(user);
850 if (!pas) {
851 bb_error_msg("can't get uid for %s", user);
852 goto err;
853 }
854
855
856 shell = line->cl_shell ? line->cl_shell : G.default_shell;
857 set_env_vars(pas, shell, line->cl_path);
858
859
860 pid = vfork();
861 if (pid == 0) {
862
863
864 change_user(pas);
865 log5("child running %s", shell);
866
867 bb_setpgrp();
868 execl(shell, shell, "-c", line->cl_cmd, (char *) NULL);
869 bb_error_msg_and_die("can't execute '%s' for user %s", shell, user);
870 }
871 if (pid < 0) {
872 bb_simple_perror_msg("vfork");
873 err:
874 pid = 0;
875 }
876 line->cl_pid = pid;
877 return pid;
878}
879
880#define process_finished_job(user, line) ((line)->cl_pid = 0)
881
882#endif
883
884
885
886
887
888
889static void flag_starting_jobs(time_t t1, time_t t2)
890{
891 time_t t;
892
893
894
895 for (t = t1 - t1 % 60; t <= t2; t += 60) {
896 struct tm *ptm;
897 CronFile *file;
898 CronLine *line;
899
900 if (t <= t1)
901 continue;
902
903 ptm = localtime(&t);
904 for (file = G.cron_files; file; file = file->cf_next) {
905 log5("file %s:", file->cf_username);
906 if (file->cf_deleted)
907 continue;
908 for (line = file->cf_lines; line; line = line->cl_next) {
909 log5(" line %s", line->cl_cmd);
910 if (line->cl_Mins[ptm->tm_min]
911 && line->cl_Hrs[ptm->tm_hour]
912 && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
913 && line->cl_Mons[ptm->tm_mon]
914 ) {
915 log5(" job: %d %s",
916 (int)line->cl_pid, line->cl_cmd);
917 if (line->cl_pid > 0) {
918 log8("user %s: process already running: %s",
919 file->cf_username, line->cl_cmd);
920 } else if (line->cl_pid == 0) {
921 line->cl_pid = START_ME_NORMAL;
922 file->cf_wants_starting = 1;
923 }
924 }
925 }
926 }
927 }
928}
929
930#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
931static int touch_reboot_file(void)
932{
933 int fd = open(CRON_REBOOT, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0000);
934 if (fd >= 0) {
935 close(fd);
936 return 1;
937 }
938
939 return 0;
940}
941#endif
942
943static void start_jobs(int wants_start)
944{
945 CronFile *file;
946 CronLine *line;
947
948 for (file = G.cron_files; file; file = file->cf_next) {
949 if (!file->cf_wants_starting)
950 continue;
951
952 file->cf_wants_starting = 0;
953 for (line = file->cf_lines; line; line = line->cl_next) {
954 pid_t pid;
955 if (line->cl_pid != wants_start)
956 continue;
957
958 pid = start_one_job(file->cf_username, line);
959 log8("USER %s pid %3d cmd %s",
960 file->cf_username, (int)pid, line->cl_cmd);
961 if (pid < 0) {
962 file->cf_wants_starting = 1;
963 }
964 if (pid > 0) {
965 file->cf_has_running = 1;
966 }
967 }
968 }
969}
970
971
972
973
974
975static int check_completions(void)
976{
977 CronFile *file;
978 CronLine *line;
979 int num_still_running = 0;
980
981 for (file = G.cron_files; file; file = file->cf_next) {
982 if (!file->cf_has_running)
983 continue;
984
985 file->cf_has_running = 0;
986 for (line = file->cf_lines; line; line = line->cl_next) {
987 int r;
988
989 if (line->cl_pid <= 0)
990 continue;
991
992 r = waitpid(line->cl_pid, NULL, WNOHANG);
993 if (r < 0 || r == line->cl_pid) {
994 process_finished_job(file->cf_username, line);
995 if (line->cl_pid == 0) {
996
997 continue;
998 }
999
1000 }
1001
1002 file->cf_has_running = 1;
1003 }
1004
1005
1006 num_still_running += file->cf_has_running;
1007 }
1008 return num_still_running;
1009}
1010
1011static void reopen_logfile_to_stderr(void)
1012{
1013 if (G.log_filename) {
1014 int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
1015 if (logfd >= 0)
1016 xmove_fd(logfd, STDERR_FILENO);
1017 }
1018}
1019
1020int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1021int crond_main(int argc UNUSED_PARAM, char **argv)
1022{
1023 time_t t2;
1024 unsigned rescan;
1025 unsigned sleep_time;
1026 unsigned opts;
1027
1028 INIT_G();
1029
1030 opts = getopt32(argv, "^"
1031 "l:L:fbSc:" IF_FEATURE_CROND_D("d:")
1032 "\0"
1033
1034 "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
1035
1036 ":l+" IF_FEATURE_CROND_D(":d+")
1037 ,
1038 &G.log_level, &G.log_filename, &G.crontab_dir_name
1039 IF_FEATURE_CROND_D(,&G.log_level)
1040 );
1041
1042
1043 if (!(opts & OPT_f)) {
1044
1045
1046 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
1047 }
1048
1049 if (!(opts & OPT_d) && G.log_filename == NULL) {
1050
1051 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
1052 logmode = LOGMODE_SYSLOG;
1053 }
1054
1055
1056
1057 reopen_logfile_to_stderr();
1058 xchdir(G.crontab_dir_name);
1059
1060
1061 G.default_shell = xstrdup(get_shell_name());
1062
1063 log8("crond (busybox "BB_VER") started, log level %d", G.log_level);
1064 rescan_crontab_dir();
1065 write_pidfile_std_path_and_ext("crond");
1066#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
1067 if (touch_reboot_file())
1068 start_jobs(START_ME_REBOOT);
1069#endif
1070
1071
1072 t2 = time(NULL);
1073 rescan = 60;
1074 sleep_time = 60;
1075 for (;;) {
1076 struct stat sbuf;
1077 time_t t1;
1078 long dt;
1079
1080
1081 t1 = t2;
1082 sleep(sleep_time - (time(NULL) % sleep_time));
1083 t2 = time(NULL);
1084 dt = (long)t2 - (long)t1;
1085
1086 reopen_logfile_to_stderr();
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 if (stat(G.crontab_dir_name, &sbuf) != 0)
1105 sbuf.st_mtime = 0;
1106 if (G.crontab_dir_mtime != sbuf.st_mtime) {
1107 G.crontab_dir_mtime = sbuf.st_mtime;
1108 rescan = 1;
1109 }
1110 if (--rescan == 0) {
1111 rescan = 60;
1112 rescan_crontab_dir();
1113 }
1114 process_cron_update_file();
1115 log5("wakeup dt=%ld", dt);
1116 if (dt < -60 * 60 || dt > 60 * 60) {
1117 bb_info_msg("time disparity of %ld minutes detected", dt / 60);
1118
1119 } else if (dt > 0) {
1120
1121 flag_starting_jobs(t1, t2);
1122 start_jobs(START_ME_NORMAL);
1123 sleep_time = 60;
1124 if (check_completions() > 0) {
1125
1126 sleep_time = 10;
1127 }
1128 }
1129
1130 }
1131
1132 return 0;
1133}
1134