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