From: Tim Landscheidt Date: Wed, 6 Jun 2012 15:14:06 +0000 (+0200) Subject: Previously, crond exited in the signal handler for SIGINT and SIGTERM. X-Git-Tag: cronie1.4.9~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e1249f83cb1c5c7c708b6484d72d0e2f8e6eba6b;p=cronie Previously, crond exited in the signal handler for SIGINT and SIGTERM. Thus, the Inotify close code in main() was never reached. This commit introduces a got_sigintterm variable that is set when SIGINT or SIGTERM are received and leads to a clean exit of the main loop. * src/cron.c: Rename quit() to sigintterm_handler(). Add got_sigintterm variable and set it on SIGINT and SIGTERM. Shorten sleep and exit main loop on got_sigintterm. Signed-off-by: Marcela Mašláňová --- diff --git a/src/cron.c b/src/cron.c index d1c9d80..7924277 100644 --- a/src/cron.c +++ b/src/cron.c @@ -43,9 +43,9 @@ set_time(int), cron_sleep(int, cron_db *), sigchld_handler(int), sighup_handler(int), -sigchld_reaper(void), quit(int), parse_args(int c, char *v[]); +sigchld_reaper(void), sigintterm_handler(int), parse_args(int c, char *v[]); -static volatile sig_atomic_t got_sighup, got_sigchld; +static volatile sig_atomic_t got_sighup, got_sigchld, got_sigintterm; static int timeRunning, virtualTime, clockTime; static long GMToff; static int DisableInotify; @@ -198,7 +198,7 @@ int main(int argc, char *argv[]) { (void) sigaction(SIGCHLD, &sact, NULL); sact.sa_handler = sighup_handler; (void) sigaction(SIGHUP, &sact, NULL); - sact.sa_handler = quit; + sact.sa_handler = sigintterm_handler; (void) sigaction(SIGINT, &sact, NULL); (void) sigaction(SIGTERM, &sact, NULL); @@ -296,7 +296,7 @@ int main(int argc, char *argv[]) { * timeRunning: is the time we last awakened. * clockTime: is the time when set_time was last called. */ - while (TRUE) { + while (!got_sigintterm) { int timeDiff; enum timejump wakeupKind; @@ -304,7 +304,9 @@ int main(int argc, char *argv[]) { do { cron_sleep(timeRunning + 1, &database); set_time(FALSE); - } while (clockTime == timeRunning); + } while (!got_sigintterm && clockTime == timeRunning); + if (got_sigintterm) + break; timeRunning = clockTime; /* @@ -430,6 +432,10 @@ int main(int argc, char *argv[]) { if (fd >= 0 && close(fd) < 0) log_it("CRON", pid, "INFO", "Inotify close failed", errno); #endif + + (void) unlink(_PATH_CRON_PID); + + return 0; } static void run_reboot_jobs(cron_db * db) { @@ -580,9 +586,12 @@ static void cron_sleep(int target, cron_db * db) { (long) getpid(), (long) target * SECONDS_PER_MINUTE, seconds_to_wait)) - while (seconds_to_wait > 0 && seconds_to_wait < 65) { + while (seconds_to_wait > 0 && seconds_to_wait < 65) { sleep((unsigned int) seconds_to_wait); + if (got_sigintterm) + return; + /* * Check to see if we were interrupted by a signal. * If so, service the signal(s) then continue sleeping @@ -604,9 +613,8 @@ static void sigchld_handler(int x) { got_sigchld = 1; } -static void quit(int x) { - (void) unlink(_PATH_CRON_PID); - _exit(0); +static void sigintterm_handler(int x) { + got_sigintterm = 1; } static void sigchld_reaper(void) {