From 5da2320932afb7ad2f17125b8d07a9ed82c3cacc Mon Sep 17 00:00:00 2001 From: Pawel Jakub Dawidek Date: Fri, 27 Aug 2010 21:28:02 +0000 Subject: [PATCH] When SIGTERM or SIGINT is received, terminate worker processes. MFC after: 2 weeks Obtained from: Wheel Systems Sp. z o.o. http://www.wheelsystems.com --- sbin/hastd/hastd.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/sbin/hastd/hastd.c b/sbin/hastd/hastd.c index c5eebf4a5b4c..785798ad768c 100644 --- a/sbin/hastd/hastd.c +++ b/sbin/hastd/hastd.c @@ -82,6 +82,10 @@ sighandler(int sig) { switch (sig) { + case SIGINT: + case SIGTERM: + sigexit_received = true; + break; case SIGCHLD: sigchld_received = true; break; @@ -373,6 +377,25 @@ failed: pjdlog_warning("Configuration not reloaded."); } +static void +terminate_workers(void) +{ + struct hast_resource *res; + + pjdlog_info("Termination signal received, exiting."); + TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { + if (res->hr_workerpid == 0) + continue; + pjdlog_info("Terminating worker process (resource=%s, role=%s, pid=%u).", + res->hr_name, role2str(res->hr_role), res->hr_workerpid); + if (kill(res->hr_workerpid, SIGTERM) == 0) + continue; + pjdlog_errno(LOG_WARNING, + "Unable to send signal to worker process (resource=%s, role=%s, pid=%u).", + res->hr_name, role2str(res->hr_role), res->hr_workerpid); + } +} + static void listen_accept(void) { @@ -599,6 +622,11 @@ main_loop(void) int cfd, lfd, maxfd, ret; for (;;) { + if (sigexit_received) { + sigexit_received = false; + terminate_workers(); + exit(EX_OK); + } if (sigchld_received) { sigchld_received = false; child_exit(); @@ -692,6 +720,8 @@ main(int argc, char *argv[]) cfg = yy_config_parse(cfgpath, true); assert(cfg != NULL); + signal(SIGINT, sighandler); + signal(SIGTERM, sighandler); signal(SIGHUP, sighandler); signal(SIGCHLD, sighandler);