- unsigned opt;
- char *lopt, *Lopt, *copt;
- USE_DEBUG_CROND_OPTION(char *dopt;)
-
- opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l");
- opterr = 0; /* disable getopt 'errors' message. */
- opt = getopt32(av, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
- &lopt, &Lopt, &copt USE_DEBUG_CROND_OPTION(, &dopt));
- if (opt & 1) /* -l */
- LogLevel = xatou(lopt);
- if (opt & 2) /* -L */
- if (*Lopt)
- LogFile = Lopt;
- if (opt & 32) /* -c */
- if (*copt)
- CDir = copt;
-#if ENABLE_DEBUG_CROND_OPTION
- if (opt & 64) { /* -d */
- DebugOpt = xatou(dopt);
- LogLevel = 0;
- }
-#endif
-
- /* close stdin and stdout, stderr.
- * close unused descriptors - don't need.
- * optional detach from controlling terminal
- */
- if (!(opt & 4))
- bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, av);
-
- xchdir(CDir);
- signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
-
- startlogger(); /* need if syslog mode selected */
-
- /*
- * main loop - synchronize to 1 second after the minute, minimum sleep
- * of 1 second.
- */
- crondlog("\011%s " BB_VER " started, log level %d\n",
- applet_name, LogLevel);
-
- SynchronizeDir();
-
- {
- time_t t1 = time(NULL);
- time_t t2;
- long dt;
- int rescan = 60;
- short sleep_time = 60;
-
- write_pidfile("/var/run/crond.pid");
- for (;;) {
- sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
-
- t2 = time(NULL);
- dt = t2 - t1;
-
- /*
- * The file 'cron.update' is checked to determine new cron
- * jobs. The directory is rescanned once an hour to deal
- * with any screwups.
- *
- * check for disparity. Disparities over an hour either way
- * result in resynchronization. A reverse-indexed disparity
- * less then an hour causes us to effectively sleep until we
- * match the original time (i.e. no re-execution of jobs that
- * have just been run). A forward-indexed disparity less then
- * an hour causes intermediate jobs to be run, but only once
- * in the worst case.
- *
- * when running jobs, the inequality used is greater but not
- * equal to t1, and less then or equal to t2.
- */
-
- if (--rescan == 0) {
- rescan = 60;
- SynchronizeDir();
- }
- CheckUpdates();
-#if ENABLE_DEBUG_CROND_OPTION
- if (DebugOpt)
- crondlog("\005Wakeup dt=%d\n", dt);
-#endif
- if (dt < -60 * 60 || dt > 60 * 60) {
- t1 = t2;
- crondlog("\111time disparity of %d minutes detected\n", dt / 60);
- } else if (dt > 0) {
- TestJobs(t1, t2);
- RunJobs();
- sleep(5);
- if (CheckJobs() > 0) {
- sleep_time = 10;
- } else {
- sleep_time = 60;
- }
- t1 = t2;
- }
- }
- }
- return 0; /* not reached */