From 9f3741877767c49fea8262732d16e894fd58f925 Mon Sep 17 00:00:00 2001 From: Frank Louwers Date: Wed, 25 Sep 2019 12:07:14 +0200 Subject: [PATCH] Add signal handling for SIGTERM and SIGINT in pdns_recursor The Linux kernel handles signals for PID 1 processes differently. It doesn't implement a default handler for some signals such as SIGTERM/SIGINT. When running pdns_recursor as a container, this causes a few annoyances. You can work around those by running your containers with --init or by installing `tini` inside the container. Or you can handle the signals in the application itself. This commit adds signal() handlers for SIGTERM and SIGINT for pdns_recursor. --- pdns/pdns_recursor.cc | 7 +++++++ pdns/rec_channel.hh | 3 +++ pdns/rec_channel_rec.cc | 6 +++--- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 75f2bdf64..5a462be4b 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -2786,6 +2786,11 @@ static void daemonize(void) } } +static void termIntHandler(int) +{ + doExitNicely(); +} + static void usr1Handler(int) { statsWanted=true; @@ -4075,6 +4080,8 @@ static int serviceMain(int argc, char*argv[]) g_log.toConsole(Logger::Critical); daemonize(); } + signal(SIGTERM,termIntHandler); + signal(SIGINT,termIntHandler); signal(SIGUSR1,usr1Handler); signal(SIGUSR2,usr2Handler); signal(SIGPIPE,SIG_IGN); diff --git a/pdns/rec_channel.hh b/pdns/rec_channel.hh index 041c2ca2a..ee653752b 100644 --- a/pdns/rec_channel.hh +++ b/pdns/rec_channel.hh @@ -89,3 +89,6 @@ void blacklistStats(StatComponent component, const string& stats); void registerAllStats(); +void doExitGeneric(bool nicely); +void doExit(); +void doExitNicely(); diff --git a/pdns/rec_channel_rec.cc b/pdns/rec_channel_rec.cc index 432b8a22e..1a1cdcd5b 100644 --- a/pdns/rec_channel_rec.cc +++ b/pdns/rec_channel_rec.cc @@ -1161,7 +1161,7 @@ void registerAllStats() } } -static void doExitGeneric(bool nicely) +void doExitGeneric(bool nicely) { g_log<