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