crond: code shrink

function                                             old     new   delta
ForkJob                                              513     457     -56

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-07-06 18:47:00 +02:00
parent 8f65b0cf31
commit d3c2b71ffa

View File

@ -54,8 +54,8 @@ typedef struct CronLine {
pid_t cl_Pid; /* running pid, 0, or armed (-1) */ pid_t cl_Pid; /* running pid, 0, or armed (-1) */
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
int cl_MailPos; /* 'empty file' size */ int cl_MailPos; /* 'empty file' size */
smallint cl_MailFlag; /* running pid is for mail */
char *cl_MailTo; /* whom to mail results */ char *cl_MailTo; /* whom to mail results */
smallint cl_MailFlag; /* running pid is for mail */
#endif #endif
/* ordered by size, not in natural order. makes code smaller: */ /* ordered by size, not in natural order. makes code smaller: */
char cl_Dow[7]; /* 0-6, beginning sunday */ char cl_Dow[7]; /* 0-6, beginning sunday */
@ -166,6 +166,9 @@ static void crondlog(const char *ctl, ...)
int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int crond_main(int argc UNUSED_PARAM, char **argv) int crond_main(int argc UNUSED_PARAM, char **argv)
{ {
time_t t2;
int rescan;
int sleep_time;
unsigned opts; unsigned opts;
INIT_G(); INIT_G();
@ -195,62 +198,62 @@ int crond_main(int argc UNUSED_PARAM, char **argv)
xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */ xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */
crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", LogLevel); crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", LogLevel);
SynchronizeDir(); SynchronizeDir();
write_pidfile("/var/run/crond.pid");
/* main loop - synchronize to 1 second after the minute, minimum sleep /* main loop - synchronize to 1 second after the minute, minimum sleep
* of 1 second. */ * of 1 second. */
{ t2 = time(NULL);
time_t t1 = time(NULL); rescan = 60;
int rescan = 60; sleep_time = 60;
int sleep_time = 60; for (;;) {
time_t t1;
long dt;
write_pidfile("/var/run/crond.pid"); t1 = t2;
for (;;) { sleep((sleep_time + 1) - (time(NULL) % sleep_time));
time_t t2;
long dt;
sleep((sleep_time + 1) - (time(NULL) % sleep_time)); t2 = time(NULL);
dt = (long)t2 - (long)t1;
t2 = time(NULL); /*
dt = (long)t2 - (long)t1; * The file 'cron.update' is checked to determine new cron
* jobs. The directory is rescanned once an hour to deal
/* * with any screwups.
* The file 'cron.update' is checked to determine new cron *
* jobs. The directory is rescanned once an hour to deal * Check for time jump. Disparities over an hour either way
* with any screwups. * result in resynchronization. A negative disparity
* * less than an hour causes us to effectively sleep until we
* check for disparity. Disparities over an hour either way * match the original time (i.e. no re-execution of jobs that
* result in resynchronization. A reverse-indexed disparity * have just been run). A positive disparity less than
* less then an hour causes us to effectively sleep until we * an hour causes intermediate jobs to be run, but only once
* match the original time (i.e. no re-execution of jobs that * in the worst case.
* have just been run). A forward-indexed disparity less then *
* an hour causes intermediate jobs to be run, but only once * When running jobs, the inequality used is greater but not
* in the worst case. * equal to t1, and less then or equal to t2.
* */
* when running jobs, the inequality used is greater but not if (--rescan == 0) {
* equal to t1, and less then or equal to t2. rescan = 60;
*/ SynchronizeDir();
if (--rescan == 0) { }
rescan = 60; CheckUpdates();
SynchronizeDir(); if (DebugOpt)
crondlog(LVL5 "wakeup dt=%ld", dt);
if (dt < -60 * 60 || dt > 60 * 60) {
crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60);
/* and we do not run any jobs in this case */
} else if (dt > 0) {
/* Usual case: time advances forwad, as expected */
TestJobs(t1, t2);
RunJobs();
sleep(5);
if (CheckJobs() > 0) {
sleep_time = 10;
} else {
sleep_time = 60;
} }
CheckUpdates(); }
if (DebugOpt) /* else: time jumped back, do not run any jobs */
crondlog(LVL5 "wakeup dt=%ld", dt); } /* for (;;) */
if (dt < -60 * 60 || dt > 60 * 60) {
crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60);
} else if (dt > 0) {
TestJobs(t1, t2);
RunJobs();
sleep(5);
if (CheckJobs() > 0) {
sleep_time = 10;
} else {
sleep_time = 60;
}
}
t1 = t2;
} /* for (;;) */
}
return 0; /* not reached */ return 0; /* not reached */
} }
@ -277,7 +280,7 @@ static void SetEnv(struct passwd *pas)
safe_setenv(&env_var_user, "USER", pas->pw_name); safe_setenv(&env_var_user, "USER", pas->pw_name);
safe_setenv(&env_var_home, "HOME", pas->pw_dir); safe_setenv(&env_var_home, "HOME", pas->pw_dir);
/* if we want to set user's shell instead: */ /* if we want to set user's shell instead: */
/*safe_setenv(env_var_user, "SHELL", pas->pw_shell);*/ /*safe_setenv(env_var_shell, "SHELL", pas->pw_shell);*/
#else #else
xsetenv("USER", pas->pw_name); xsetenv("USER", pas->pw_name);
xsetenv("HOME", pas->pw_dir); xsetenv("HOME", pas->pw_dir);
@ -597,10 +600,10 @@ static void SynchronizeDir(void)
} }
/* /*
* DeleteFile() - delete user database * DeleteFile() - delete user database
* *
* Note: multiple entries for same user may exist if we were unable to * Note: multiple entries for same user may exist if we were unable to
* completely delete a database due to running processes. * completely delete a database due to running processes.
*/ */
static void DeleteFile(const char *userName) static void DeleteFile(const char *userName)
{ {
@ -806,14 +809,14 @@ ForkJob(const char *user, CronLine *line, int mailFd,
if (mail_filename) { if (mail_filename) {
unlink(mail_filename); unlink(mail_filename);
} }
} else if (mail_filename) { } else {
/* PARENT, FORK SUCCESS /* PARENT, FORK SUCCESS */
* rename mail-file based on pid of process if (mail_filename) {
*/ /* rename mail-file based on pid of process */
char mailFile2[128]; char *mailFile2 = xasprintf("%s/cron.%s.%d", TMPDIR, user, (int)pid);
rename(mail_filename, mailFile2); // TODO: xrename?
snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid); free(mailFile2);
rename(mail_filename, mailFile2); // TODO: xrename? }
} }
/* /*