12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #include <syslog.h>
- #if 0
- # define SETENV_LEAKS 0
- #else
- # define SETENV_LEAKS 1
- #endif
- #define CRON_DIR CONFIG_FEATURE_CROND_DIR
- #define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
- #define CRON_REBOOT CONFIG_PID_FILE_PATH "/crond.reboot"
- #ifndef SENDMAIL
- # define SENDMAIL "sendmail"
- #endif
- #ifndef SENDMAIL_ARGS
- # define SENDMAIL_ARGS "-ti"
- #endif
- #ifndef CRONUPDATE
- # define CRONUPDATE "cron.update"
- #endif
- #ifndef MAXLINES
- # define MAXLINES 256
- #endif
- typedef struct CronFile {
- struct CronFile *cf_next;
- struct CronLine *cf_lines;
- char *cf_username;
- smallint cf_wants_starting;
- smallint cf_has_running;
- smallint cf_deleted;
- } CronFile;
- typedef struct CronLine {
- struct CronLine *cl_next;
- char *cl_cmd;
- pid_t cl_pid;
- #define START_ME_REBOOT -2
- #define START_ME_NORMAL -1
- #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
- int cl_empty_mail_size;
- char *cl_mailto;
- #endif
- char *cl_shell;
- char *cl_path;
-
- char cl_Dow[7];
- char cl_Mons[12];
- char cl_Hrs[24];
- char cl_Days[32];
- char cl_Mins[60];
- } CronLine;
- #define DAEMON_UID 0
- enum {
- OPT_l = (1 << 0),
- OPT_L = (1 << 1),
- OPT_f = (1 << 2),
- OPT_b = (1 << 3),
- OPT_S = (1 << 4),
- OPT_c = (1 << 5),
- OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
- };
- struct globals {
- unsigned log_level;
- time_t crontab_dir_mtime;
- const char *log_filename;
- const char *crontab_dir_name;
- CronFile *cron_files;
- char *default_shell;
- #if SETENV_LEAKS
- char *env_var_user;
- char *env_var_home;
- char *env_var_shell;
- char *env_var_logname;
- #endif
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- #define INIT_G() do { \
- setup_common_bufsiz(); \
- G.log_level = 8; \
- G.crontab_dir_name = CRONTABS; \
- } while (0)
- static void crondlog(unsigned level, const char *msg, va_list va)
- {
- if (level >= G.log_level) {
-
- bb_vinfo_msg(msg, va);
- }
- }
- static void log5(const char *msg, ...)
- {
- va_list va;
- va_start(va, msg);
- crondlog(5, msg, va);
- va_end(va);
- }
- static void log7(const char *msg, ...)
- {
- va_list va;
- va_start(va, msg);
- crondlog(7, msg, va);
- va_end(va);
- }
- static void log8(const char *msg, ...)
- {
- va_list va;
- va_start(va, msg);
- crondlog(8, msg, va);
- va_end(va);
- }
- static const char DowAry[] ALIGN1 =
- "sun""mon""tue""wed""thu""fri""sat"
- ;
- static const char MonAry[] ALIGN1 =
- "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
- ;
- static void ParseField(char *user, char *ary, int modvalue, int off,
- const char *names, char *ptr)
- {
- char *base = ptr;
- int n1 = -1;
- int n2 = -1;
-
-
- while (1) {
- int skip = 0;
-
- if (*ptr == '*') {
- n1 = 0;
- n2 = modvalue - 1;
- skip = 1;
- ++ptr;
- } else if (isdigit(*ptr)) {
- char *endp;
- if (n1 < 0) {
- n1 = strtol(ptr, &endp, 10) + off;
- } else {
- n2 = strtol(ptr, &endp, 10) + off;
- }
- ptr = endp;
- skip = 1;
- } else if (names) {
- int i;
- for (i = 0; names[i]; i += 3) {
-
- if (strncasecmp(ptr, &names[i], 3) == 0) {
- ptr += 3;
- if (n1 < 0) {
- n1 = i / 3;
- } else {
- n2 = i / 3;
- }
- skip = 1;
- break;
- }
- }
- }
-
- if (skip == 0) {
- goto err;
- }
- if (*ptr == '-' && n2 < 0) {
- ++ptr;
- continue;
- }
-
- if (n2 < 0) {
- n2 = n1;
- }
- if (*ptr == '/') {
- char *endp;
- skip = strtol(ptr + 1, &endp, 10);
- ptr = endp;
- }
-
- {
- int s0 = 1;
- int failsafe = 1024;
- --n1;
- do {
- n1 = (n1 + 1) % modvalue;
- if (--s0 == 0) {
- ary[n1 % modvalue] = 1;
- s0 = skip;
- }
- if (--failsafe == 0) {
- goto err;
- }
- } while (n1 != n2);
- }
- if (*ptr != ',') {
- break;
- }
- ++ptr;
- n1 = -1;
- n2 = -1;
- }
- if (*ptr) {
- err:
- bb_error_msg("user %s: parse error at %s", user, base);
- return;
- }
-
- if (G.log_level <= 5 && logmode != LOGMODE_SYSLOG) {
- int i;
- for (i = 0; i < modvalue; ++i)
- fprintf(stderr, "%d", (unsigned char)ary[i]);
- bb_putchar_stderr('\n');
- }
- }
- static void FixDayDow(CronLine *line)
- {
- unsigned i;
- int weekUsed = 0;
- int daysUsed = 0;
- for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
- if (line->cl_Dow[i] == 0) {
- weekUsed = 1;
- break;
- }
- }
- for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
- if (line->cl_Days[i] == 0) {
- daysUsed = 1;
- break;
- }
- }
- if (weekUsed != daysUsed) {
- if (weekUsed)
- memset(line->cl_Days, 0, sizeof(line->cl_Days));
- else
- memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
- }
- }
- static void delete_cronfile(const char *userName)
- {
- CronFile **pfile = &G.cron_files;
- CronFile *file;
- while ((file = *pfile) != NULL) {
- if (strcmp(userName, file->cf_username) == 0) {
- CronLine **pline = &file->cf_lines;
- CronLine *line;
- file->cf_has_running = 0;
- file->cf_deleted = 1;
- while ((line = *pline) != NULL) {
- if (line->cl_pid > 0) {
- file->cf_has_running = 1;
- pline = &line->cl_next;
- } else {
- *pline = line->cl_next;
- free(line->cl_cmd);
- free(line);
- }
- }
- if (file->cf_has_running == 0) {
- *pfile = file->cf_next;
- free(file->cf_username);
- free(file);
- continue;
- }
- }
- pfile = &file->cf_next;
- }
- }
- static void load_crontab(const char *fileName)
- {
- struct parser_t *parser;
- struct stat sbuf;
- int maxLines;
- char *tokens[6];
- #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
- char *mailTo = NULL;
- #endif
- char *shell = NULL;
- char *path = NULL;
- delete_cronfile(fileName);
- if (!getpwnam(fileName)) {
- log7("ignoring file '%s' (no such user)", fileName);
- return;
- }
- parser = config_open(fileName);
- if (!parser)
- return;
- maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
- if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
- CronFile *file = xzalloc(sizeof(CronFile));
- CronLine **pline;
- int n;
- file->cf_username = xstrdup(fileName);
- pline = &file->cf_lines;
- while (1) {
- CronLine *line;
- if (!--maxLines) {
- bb_error_msg("user %s: too many lines", fileName);
- break;
- }
- n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
- if (!n)
- break;
- log5("user:%s entry:%s", fileName, parser->data);
-
- if (is_prefixed_with(tokens[0], "MAILTO=")) {
- #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
- free(mailTo);
- mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
- #endif
- continue;
- }
- if (is_prefixed_with(tokens[0], "SHELL=")) {
- free(shell);
- shell = xstrdup(&tokens[0][6]);
- continue;
- }
- if (is_prefixed_with(tokens[0], "PATH=")) {
- free(path);
- path = xstrdup(&tokens[0][5]);
- continue;
- }
- #if ENABLE_FEATURE_CROND_SPECIAL_TIMES
- if (tokens[0][0] == '@') {
-
- typedef struct SpecialEntry {
- const char *name;
- const char tokens[8];
- } SpecialEntry;
- static const SpecialEntry SpecAry[] ALIGN8 = {
-
- { "yearly", "0\0" "1\0" "1\0" "*" },
- { "annually", "0\0" "1\0" "1\0" "*" },
- { "monthly", "0\0" "1\0" "*\0" "*" },
- { "weekly", "0\0" "*\0" "*\0" "0" },
- { "daily", "0\0" "*\0" "*\0" "*" },
- { "midnight", "0\0" "*\0" "*\0" "*" },
- { "hourly", "*\0" "*\0" "*\0" "*" },
- { "reboot", "" },
- };
- const SpecialEntry *e = SpecAry;
- if (n < 2)
- continue;
- for (;;) {
- if (strcmp(e->name, tokens[0] + 1) == 0) {
-
- tokens[5] = skip_whitespace(
- skip_non_whitespace(
- skip_whitespace(parser->data)));
- if (e->tokens[0]) {
- char *et = (char*)e->tokens;
-
- tokens[0] = (char*)"0";
- tokens[1] = et;
- tokens[2] = et + 2;
- tokens[3] = et + 4;
- tokens[4] = et + 6;
- }
- goto got_it;
- }
- if (!e->tokens[0])
- break;
- e++;
- }
- continue;
- }
- #endif
-
- if (n < 6)
- continue;
- IF_FEATURE_CROND_SPECIAL_TIMES(
- got_it:
- )
- *pline = line = xzalloc(sizeof(*line));
- #if ENABLE_FEATURE_CROND_SPECIAL_TIMES
- if (tokens[0][0] == '@') {
- file->cf_wants_starting = 1;
- line->cl_pid = START_ME_REBOOT;
-
- } else
- #endif
- {
-
- ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
- ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
- ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
- ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
- ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
-
- FixDayDow(line);
- }
- #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
-
- line->cl_mailto = xstrdup(mailTo);
- #endif
- line->cl_shell = xstrdup(shell);
- line->cl_path = xstrdup(path);
-
- line->cl_cmd = xstrdup(tokens[5]);
- pline = &line->cl_next;
- }
- *pline = NULL;
- file->cf_next = G.cron_files;
- G.cron_files = file;
- }
- config_close(parser);
- #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
- free(mailTo);
- #endif
- free(shell);
- }
- static void process_cron_update_file(void)
- {
- FILE *fi;
- char buf[256];
- fi = fopen_for_read(CRONUPDATE);
- if (fi != NULL) {
- unlink(CRONUPDATE);
- while (fgets(buf, sizeof(buf), fi) != NULL) {
-
- skip_non_whitespace(buf)[0] = '\0';
- load_crontab(buf);
- }
- fclose(fi);
- }
- }
- static void rescan_crontab_dir(void)
- {
- CronFile *file;
-
- again:
- for (file = G.cron_files; file; file = file->cf_next) {
- if (!file->cf_deleted) {
- delete_cronfile(file->cf_username);
- goto again;
- }
- }
-
- unlink(CRONUPDATE);
-
- xchdir(G.crontab_dir_name);
-
- {
- DIR *dir = opendir(".");
- struct dirent *den;
-
- if (!dir)
- bb_error_msg_and_die("can't open '%s'", G.crontab_dir_name);
- while ((den = readdir(dir)) != NULL) {
- if (strchr(den->d_name, '.') != NULL) {
- continue;
- }
- load_crontab(den->d_name);
- }
- closedir(dir);
- }
- }
- #if SETENV_LEAKS
- static void safe_setenv(char **pvar_val, const char *var, const char *val)
- {
- char *var_val = *pvar_val;
- if (var_val) {
- bb_unsetenv_and_free(var_val);
- }
- *pvar_val = xasprintf("%s=%s", var, val);
- putenv(*pvar_val);
- }
- #endif
- static void set_env_vars(struct passwd *pas, const char *shell, const char *path)
- {
-
- #if SETENV_LEAKS
- safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name);
- safe_setenv(&G.env_var_user, "USER", pas->pw_name);
- safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
- safe_setenv(&G.env_var_shell, "SHELL", shell);
- if (path) safe_setenv(&G.env_var_shell, "PATH", path);
- #else
- xsetenv("LOGNAME", pas->pw_name);
- xsetenv("USER", pas->pw_name);
- xsetenv("HOME", pas->pw_dir);
- xsetenv("SHELL", shell);
- if (path) xsetenv("PATH", path);
- #endif
- }
- static void change_user(struct passwd *pas)
- {
-
- change_identity(pas);
- if (chdir_or_warn(pas->pw_dir) != 0) {
- xchdir(CRON_DIR);
- }
- }
- #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
- static pid_t
- fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail)
- {
- struct passwd *pas;
- const char *shell, *prog;
- smallint sv_logmode;
- pid_t pid;
-
- pas = getpwnam(user);
- if (!pas) {
- bb_error_msg("can't get uid for %s", user);
- goto err;
- }
- shell = line->cl_shell ? line->cl_shell : G.default_shell;
- prog = run_sendmail ? SENDMAIL : shell;
- set_env_vars(pas, shell, NULL);
- sv_logmode = logmode;
- pid = vfork();
- if (pid == 0) {
-
-
- change_user(pas);
- log5("child running %s", prog);
- if (mailFd >= 0) {
- xmove_fd(mailFd, run_sendmail ? 0 : 1);
- dup2(1, 2);
- }
-
- bb_setpgrp();
- if (!run_sendmail)
- execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL);
- else
- execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL);
-
- logmode |= LOGMODE_STDIO;
- bb_error_msg_and_die("can't execute '%s' for user %s", prog, user);
- }
- logmode = sv_logmode;
- if (pid < 0) {
- bb_simple_perror_msg("vfork");
- err:
- pid = 0;
- }
-
- if (mailFd >= 0) {
- close(mailFd);
- }
- return pid;
- }
- static pid_t start_one_job(const char *user, CronLine *line)
- {
- char mailFile[128];
- int mailFd = -1;
- line->cl_pid = 0;
- line->cl_empty_mail_size = 0;
- if (line->cl_mailto) {
-
- snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, getpid());
- mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
- if (mailFd >= 0) {
- fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
- line->cl_cmd);
- line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
- } else {
- bb_error_msg("can't create mail file %s for user %s, "
- "discarding output", mailFile, user);
- }
- }
- line->cl_pid = fork_job(user, mailFd, line, 0);
- if (mailFd >= 0) {
- if (line->cl_pid <= 0) {
- unlink(mailFile);
- } else {
-
- char *mailFile2 = xasprintf("%s/cron.%s.%d", CRON_DIR, user, (int)line->cl_pid);
- rename(mailFile, mailFile2);
- free(mailFile2);
- }
- }
- return line->cl_pid;
- }
- static void process_finished_job(const char *user, CronLine *line)
- {
- pid_t pid;
- int mailFd;
- char mailFile[128];
- struct stat sbuf;
- pid = line->cl_pid;
- line->cl_pid = 0;
- if (pid <= 0) {
-
- return;
- }
- if (line->cl_empty_mail_size <= 0) {
-
- return;
- }
-
- snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, (int)pid);
- mailFd = open(mailFile, O_RDONLY);
- unlink(mailFile);
- if (mailFd < 0) {
- return;
- }
- if (fstat(mailFd, &sbuf) < 0
- || sbuf.st_uid != DAEMON_UID
- || sbuf.st_nlink != 0
- || sbuf.st_size == line->cl_empty_mail_size
- || !S_ISREG(sbuf.st_mode)
- ) {
- close(mailFd);
- return;
- }
- line->cl_empty_mail_size = 0;
-
- line->cl_pid = fork_job(user, mailFd, line, 1);
- }
- #else
- static pid_t start_one_job(const char *user, CronLine *line)
- {
- const char *shell;
- struct passwd *pas;
- pid_t pid;
- pas = getpwnam(user);
- if (!pas) {
- bb_error_msg("can't get uid for %s", user);
- goto err;
- }
-
- shell = line->cl_shell ? line->cl_shell : G.default_shell;
- set_env_vars(pas, shell, line->cl_path);
-
- pid = vfork();
- if (pid == 0) {
-
-
- change_user(pas);
- log5("child running %s", shell);
-
- bb_setpgrp();
- execl(shell, shell, "-c", line->cl_cmd, (char *) NULL);
- bb_error_msg_and_die("can't execute '%s' for user %s", shell, user);
- }
- if (pid < 0) {
- bb_simple_perror_msg("vfork");
- err:
- pid = 0;
- }
- line->cl_pid = pid;
- return pid;
- }
- #define process_finished_job(user, line) ((line)->cl_pid = 0)
- #endif
- static void flag_starting_jobs(time_t t1, time_t t2)
- {
- time_t t;
-
- for (t = t1 - t1 % 60; t <= t2; t += 60) {
- struct tm *ptm;
- CronFile *file;
- CronLine *line;
- if (t <= t1)
- continue;
- ptm = localtime(&t);
- for (file = G.cron_files; file; file = file->cf_next) {
- log5("file %s:", file->cf_username);
- if (file->cf_deleted)
- continue;
- for (line = file->cf_lines; line; line = line->cl_next) {
- log5(" line %s", line->cl_cmd);
- if (line->cl_Mins[ptm->tm_min]
- && line->cl_Hrs[ptm->tm_hour]
- && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
- && line->cl_Mons[ptm->tm_mon]
- ) {
- log5(" job: %d %s",
- (int)line->cl_pid, line->cl_cmd);
- if (line->cl_pid > 0) {
- log8("user %s: process already running: %s",
- file->cf_username, line->cl_cmd);
- } else if (line->cl_pid == 0) {
- line->cl_pid = START_ME_NORMAL;
- file->cf_wants_starting = 1;
- }
- }
- }
- }
- }
- }
- #if ENABLE_FEATURE_CROND_SPECIAL_TIMES
- static int touch_reboot_file(void)
- {
- int fd = open(CRON_REBOOT, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0000);
- if (fd >= 0) {
- close(fd);
- return 1;
- }
-
- return 0;
- }
- #endif
- static void start_jobs(int wants_start)
- {
- CronFile *file;
- CronLine *line;
- for (file = G.cron_files; file; file = file->cf_next) {
- if (!file->cf_wants_starting)
- continue;
- file->cf_wants_starting = 0;
- for (line = file->cf_lines; line; line = line->cl_next) {
- pid_t pid;
- if (line->cl_pid != wants_start)
- continue;
- pid = start_one_job(file->cf_username, line);
- log8("USER %s pid %3d cmd %s",
- file->cf_username, (int)pid, line->cl_cmd);
- if (pid < 0) {
- file->cf_wants_starting = 1;
- }
- if (pid > 0) {
- file->cf_has_running = 1;
- }
- }
- }
- }
- static int check_completions(void)
- {
- CronFile *file;
- CronLine *line;
- int num_still_running = 0;
- for (file = G.cron_files; file; file = file->cf_next) {
- if (!file->cf_has_running)
- continue;
- file->cf_has_running = 0;
- for (line = file->cf_lines; line; line = line->cl_next) {
- int r;
- if (line->cl_pid <= 0)
- continue;
- r = waitpid(line->cl_pid, NULL, WNOHANG);
- if (r < 0 || r == line->cl_pid) {
- process_finished_job(file->cf_username, line);
- if (line->cl_pid == 0) {
-
- continue;
- }
-
- }
-
- file->cf_has_running = 1;
- }
- num_still_running += file->cf_has_running;
- }
- return num_still_running;
- }
- static void reopen_logfile_to_stderr(void)
- {
- if (G.log_filename) {
- int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
- if (logfd >= 0)
- xmove_fd(logfd, STDERR_FILENO);
- }
- }
- int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int crond_main(int argc UNUSED_PARAM, char **argv)
- {
- time_t t2;
- unsigned rescan;
- unsigned sleep_time;
- unsigned opts;
- INIT_G();
- opts = getopt32(argv, "^"
- "l:L:fbSc:" IF_FEATURE_CROND_D("d:")
- "\0"
-
- "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
-
- ":l+" IF_FEATURE_CROND_D(":d+")
- ,
- &G.log_level, &G.log_filename, &G.crontab_dir_name
- IF_FEATURE_CROND_D(,&G.log_level)
- );
-
- if (!(opts & OPT_f)) {
-
- bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
- }
- if (!(opts & OPT_d) && G.log_filename == NULL) {
-
- openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
- logmode = LOGMODE_SYSLOG;
- }
-
- reopen_logfile_to_stderr();
- xchdir(G.crontab_dir_name);
-
-
- G.default_shell = xstrdup(get_shell_name());
- log8("crond (busybox "BB_VER") started, log level %d", G.log_level);
- rescan_crontab_dir();
- write_pidfile_std_path_and_ext("crond");
- #if ENABLE_FEATURE_CROND_SPECIAL_TIMES
- if (touch_reboot_file())
- start_jobs(START_ME_REBOOT);
- #endif
-
- t2 = time(NULL);
- rescan = 60;
- sleep_time = 60;
- for (;;) {
- struct stat sbuf;
- time_t t1;
- long dt;
-
- t1 = t2;
- sleep(sleep_time - (time(NULL) % sleep_time));
- t2 = time(NULL);
- dt = (long)t2 - (long)t1;
- reopen_logfile_to_stderr();
-
- if (stat(G.crontab_dir_name, &sbuf) != 0)
- sbuf.st_mtime = 0;
- if (G.crontab_dir_mtime != sbuf.st_mtime) {
- G.crontab_dir_mtime = sbuf.st_mtime;
- rescan = 1;
- }
- if (--rescan == 0) {
- rescan = 60;
- rescan_crontab_dir();
- }
- process_cron_update_file();
- log5("wakeup dt=%ld", dt);
- if (dt < -60 * 60 || dt > 60 * 60) {
- bb_info_msg("time disparity of %ld minutes detected", dt / 60);
-
- } else if (dt > 0) {
-
- flag_starting_jobs(t1, t2);
- start_jobs(START_ME_NORMAL);
- sleep_time = 60;
- if (check_completions() > 0) {
-
- sleep_time = 10;
- }
- }
-
- }
- return 0;
- }
|