diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-07-06 18:47:00 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-07-06 18:47:00 +0200 |
commit | d3c2b71ffae3f408e8e0e51b50a9f98e1b8b85e3 (patch) | |
tree | 32d9dcd28739ca30305b3a27d0ff657859ed37ca | |
parent | 8f65b0cf31837e3400e1f54f6826198d8ca81b42 (diff) | |
download | busybox-w32-d3c2b71ffae3f408e8e0e51b50a9f98e1b8b85e3.tar.gz busybox-w32-d3c2b71ffae3f408e8e0e51b50a9f98e1b8b85e3.tar.bz2 busybox-w32-d3c2b71ffae3f408e8e0e51b50a9f98e1b8b85e3.zip |
crond: code shrink
function old new delta
ForkJob 513 457 -56
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/crond.c | 127 |
1 files changed, 65 insertions, 62 deletions
diff --git a/miscutils/crond.c b/miscutils/crond.c index 4a3103cb9..49ecf745e 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c | |||
@@ -54,8 +54,8 @@ typedef struct CronLine { | |||
54 | pid_t cl_Pid; /* running pid, 0, or armed (-1) */ | 54 | pid_t cl_Pid; /* running pid, 0, or armed (-1) */ |
55 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL | 55 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
56 | int cl_MailPos; /* 'empty file' size */ | 56 | int cl_MailPos; /* 'empty file' size */ |
57 | smallint cl_MailFlag; /* running pid is for mail */ | ||
58 | char *cl_MailTo; /* whom to mail results */ | 57 | char *cl_MailTo; /* whom to mail results */ |
58 | smallint cl_MailFlag; /* running pid is for mail */ | ||
59 | #endif | 59 | #endif |
60 | /* ordered by size, not in natural order. makes code smaller: */ | 60 | /* ordered by size, not in natural order. makes code smaller: */ |
61 | char cl_Dow[7]; /* 0-6, beginning sunday */ | 61 | char cl_Dow[7]; /* 0-6, beginning sunday */ |
@@ -166,6 +166,9 @@ static void crondlog(const char *ctl, ...) | |||
166 | int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 166 | int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
167 | int crond_main(int argc UNUSED_PARAM, char **argv) | 167 | int crond_main(int argc UNUSED_PARAM, char **argv) |
168 | { | 168 | { |
169 | time_t t2; | ||
170 | int rescan; | ||
171 | int sleep_time; | ||
169 | unsigned opts; | 172 | unsigned opts; |
170 | 173 | ||
171 | INIT_G(); | 174 | INIT_G(); |
@@ -195,62 +198,62 @@ int crond_main(int argc UNUSED_PARAM, char **argv) | |||
195 | xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */ | 198 | xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */ |
196 | crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", LogLevel); | 199 | crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", LogLevel); |
197 | SynchronizeDir(); | 200 | SynchronizeDir(); |
201 | write_pidfile("/var/run/crond.pid"); | ||
198 | 202 | ||
199 | /* main loop - synchronize to 1 second after the minute, minimum sleep | 203 | /* main loop - synchronize to 1 second after the minute, minimum sleep |
200 | * of 1 second. */ | 204 | * of 1 second. */ |
201 | { | 205 | t2 = time(NULL); |
202 | time_t t1 = time(NULL); | 206 | rescan = 60; |
203 | int rescan = 60; | 207 | sleep_time = 60; |
204 | int sleep_time = 60; | 208 | for (;;) { |
209 | time_t t1; | ||
210 | long dt; | ||
205 | 211 | ||
206 | write_pidfile("/var/run/crond.pid"); | 212 | t1 = t2; |
207 | for (;;) { | 213 | sleep((sleep_time + 1) - (time(NULL) % sleep_time)); |
208 | time_t t2; | ||
209 | long dt; | ||
210 | 214 | ||
211 | sleep((sleep_time + 1) - (time(NULL) % sleep_time)); | 215 | t2 = time(NULL); |
216 | dt = (long)t2 - (long)t1; | ||
212 | 217 | ||
213 | t2 = time(NULL); | 218 | /* |
214 | dt = (long)t2 - (long)t1; | 219 | * The file 'cron.update' is checked to determine new cron |
215 | 220 | * jobs. The directory is rescanned once an hour to deal | |
216 | /* | 221 | * with any screwups. |
217 | * The file 'cron.update' is checked to determine new cron | 222 | * |
218 | * jobs. The directory is rescanned once an hour to deal | 223 | * Check for time jump. Disparities over an hour either way |
219 | * with any screwups. | 224 | * result in resynchronization. A negative disparity |
220 | * | 225 | * less than an hour causes us to effectively sleep until we |
221 | * check for disparity. Disparities over an hour either way | 226 | * match the original time (i.e. no re-execution of jobs that |
222 | * result in resynchronization. A reverse-indexed disparity | 227 | * have just been run). A positive disparity less than |
223 | * less then an hour causes us to effectively sleep until we | 228 | * an hour causes intermediate jobs to be run, but only once |
224 | * match the original time (i.e. no re-execution of jobs that | 229 | * in the worst case. |
225 | * have just been run). A forward-indexed disparity less then | 230 | * |
226 | * an hour causes intermediate jobs to be run, but only once | 231 | * When running jobs, the inequality used is greater but not |
227 | * in the worst case. | 232 | * equal to t1, and less then or equal to t2. |
228 | * | 233 | */ |
229 | * when running jobs, the inequality used is greater but not | 234 | if (--rescan == 0) { |
230 | * equal to t1, and less then or equal to t2. | 235 | rescan = 60; |
231 | */ | 236 | SynchronizeDir(); |
232 | if (--rescan == 0) { | 237 | } |
233 | rescan = 60; | 238 | CheckUpdates(); |
234 | SynchronizeDir(); | 239 | if (DebugOpt) |
235 | } | 240 | crondlog(LVL5 "wakeup dt=%ld", dt); |
236 | CheckUpdates(); | 241 | if (dt < -60 * 60 || dt > 60 * 60) { |
237 | if (DebugOpt) | 242 | crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60); |
238 | crondlog(LVL5 "wakeup dt=%ld", dt); | 243 | /* and we do not run any jobs in this case */ |
239 | if (dt < -60 * 60 || dt > 60 * 60) { | 244 | } else if (dt > 0) { |
240 | crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60); | 245 | /* Usual case: time advances forwad, as expected */ |
241 | } else if (dt > 0) { | 246 | TestJobs(t1, t2); |
242 | TestJobs(t1, t2); | 247 | RunJobs(); |
243 | RunJobs(); | 248 | sleep(5); |
244 | sleep(5); | 249 | if (CheckJobs() > 0) { |
245 | if (CheckJobs() > 0) { | 250 | sleep_time = 10; |
246 | sleep_time = 10; | 251 | } else { |
247 | } else { | 252 | sleep_time = 60; |
248 | sleep_time = 60; | ||
249 | } | ||
250 | } | 253 | } |
251 | t1 = t2; | 254 | } |
252 | } /* for (;;) */ | 255 | /* else: time jumped back, do not run any jobs */ |
253 | } | 256 | } /* for (;;) */ |
254 | 257 | ||
255 | return 0; /* not reached */ | 258 | return 0; /* not reached */ |
256 | } | 259 | } |
@@ -277,7 +280,7 @@ static void SetEnv(struct passwd *pas) | |||
277 | safe_setenv(&env_var_user, "USER", pas->pw_name); | 280 | safe_setenv(&env_var_user, "USER", pas->pw_name); |
278 | safe_setenv(&env_var_home, "HOME", pas->pw_dir); | 281 | safe_setenv(&env_var_home, "HOME", pas->pw_dir); |
279 | /* if we want to set user's shell instead: */ | 282 | /* if we want to set user's shell instead: */ |
280 | /*safe_setenv(env_var_user, "SHELL", pas->pw_shell);*/ | 283 | /*safe_setenv(env_var_shell, "SHELL", pas->pw_shell);*/ |
281 | #else | 284 | #else |
282 | xsetenv("USER", pas->pw_name); | 285 | xsetenv("USER", pas->pw_name); |
283 | xsetenv("HOME", pas->pw_dir); | 286 | xsetenv("HOME", pas->pw_dir); |
@@ -597,10 +600,10 @@ static void SynchronizeDir(void) | |||
597 | } | 600 | } |
598 | 601 | ||
599 | /* | 602 | /* |
600 | * DeleteFile() - delete user database | 603 | * DeleteFile() - delete user database |
601 | * | 604 | * |
602 | * Note: multiple entries for same user may exist if we were unable to | 605 | * Note: multiple entries for same user may exist if we were unable to |
603 | * completely delete a database due to running processes. | 606 | * completely delete a database due to running processes. |
604 | */ | 607 | */ |
605 | static void DeleteFile(const char *userName) | 608 | static void DeleteFile(const char *userName) |
606 | { | 609 | { |
@@ -806,14 +809,14 @@ ForkJob(const char *user, CronLine *line, int mailFd, | |||
806 | if (mail_filename) { | 809 | if (mail_filename) { |
807 | unlink(mail_filename); | 810 | unlink(mail_filename); |
808 | } | 811 | } |
809 | } else if (mail_filename) { | 812 | } else { |
810 | /* PARENT, FORK SUCCESS | 813 | /* PARENT, FORK SUCCESS */ |
811 | * rename mail-file based on pid of process | 814 | if (mail_filename) { |
812 | */ | 815 | /* rename mail-file based on pid of process */ |
813 | char mailFile2[128]; | 816 | char *mailFile2 = xasprintf("%s/cron.%s.%d", TMPDIR, user, (int)pid); |
814 | 817 | rename(mail_filename, mailFile2); // TODO: xrename? | |
815 | snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid); | 818 | free(mailFile2); |
816 | rename(mail_filename, mailFile2); // TODO: xrename? | 819 | } |
817 | } | 820 | } |
818 | 821 | ||
819 | /* | 822 | /* |