From e171f8886525cbaa48e40f7d5b31acb6db657fb2 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 21 Sep 2018 18:08:48 +0200 Subject: Remove pid fron cron serives, reap children in signal handler Signed-off-by: David Oberhollenzer --- crond/main.c | 18 +++++++----------- crond/runjob.c | 5 ++--- lib/cron/rdcron.c | 1 - lib/include/crontab.h | 1 - 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/crond/main.c b/crond/main.c index d7cc33e..613a1ae 100644 --- a/crond/main.c +++ b/crond/main.c @@ -79,6 +79,8 @@ static void runjobs(void) static void sighandler(int signo) { + pid_t pid; + switch (signo) { case SIGINT: case SIGTERM: @@ -87,6 +89,10 @@ static void sighandler(int signo) case SIGHUP: rescan = 1; break; + case SIGCHLD: + while ((pid = waitpid(-1, NULL, WNOHANG)) != -1) + ; + break; } } @@ -94,15 +100,14 @@ int main(void) { struct timespec stime; struct sigaction act; - crontab_t *t; int timeout; - pid_t pid; memset(&act, 0, sizeof(act)); act.sa_handler = sighandler; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); sigaction(SIGHUP, &act, NULL); + sigaction(SIGCHLD, &act, NULL); while (run) { if (rescan == 1) { @@ -124,15 +129,6 @@ int main(void) break; } } - - while ((pid = waitpid(-1, NULL, WNOHANG)) != -1) { - for (t = jobs; t != NULL; t = t->next) { - if (t->pid == pid) { - t->pid = -1; - break; - } - } - } } return EXIT_SUCCESS; diff --git a/crond/runjob.c b/crond/runjob.c index 7650748..83c4278 100644 --- a/crond/runjob.c +++ b/crond/runjob.c @@ -33,10 +33,8 @@ int runjob(crontab_t *tab) return -1; } - if (pid != 0) { - tab->pid = pid; + if (pid != 0) return 0; - } /* XXX: inside the child process */ memset(&act, 0, sizeof(act)); @@ -44,6 +42,7 @@ int runjob(crontab_t *tab) sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); sigaction(SIGHUP, &act, NULL); + sigaction(SIGCHLD, &act, NULL); if (setup_tty(tab->ctty, tab->tty_truncate)) exit(EXIT_FAILURE); diff --git a/lib/cron/rdcron.c b/lib/cron/rdcron.c index c581b7e..1c11cbb 100644 --- a/lib/cron/rdcron.c +++ b/lib/cron/rdcron.c @@ -489,7 +489,6 @@ crontab_t *rdcron(int dirfd, const char *filename) goto out; } - cron->pid = -1; cron->minute = 0xFFFFFFFFFFFFFFFFUL; cron->hour = 0xFFFFFFFF; cron->dayofmonth = 0xFFFFFFFF; diff --git a/lib/include/crontab.h b/lib/include/crontab.h index 408d276..1ea3883 100644 --- a/lib/include/crontab.h +++ b/lib/include/crontab.h @@ -32,7 +32,6 @@ typedef struct crontab_t { uid_t uid; gid_t gid; - pid_t pid; uint64_t minute; uint32_t hour; -- cgit v1.2.3