From 82646a9023aac6a73a7b7fddfbbee0565290d5de Mon Sep 17 00:00:00 2001 From: Molly Miller <33266253+sysvinit@users.noreply.github.com> Date: Wed, 7 Dec 2022 02:06:51 +0100 Subject: [PATCH] Add explicit SIGTERM and SIGINT handlers. (#1106) coturn running inside a docker container runs as PID 1, however PID 1 has special signal handling semantics (see the note at the bottom of the section [here](https://docs.docker.com/engine/reference/run/#foreground)). coturn relies on the default behaviour of SIGTERM to terminate the process, however as no signal handler is explicitly installed, it doesn't respond to SIGTERM when running inside a container. This PR fixes this problem by installing explicit signal handlers for SIGINT and SIGTERM, which trigger the same termination mechanism as the admin interface "halt" command. This is a port of wireapp#6 for upstream. --- src/apps/relay/mainrelay.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/apps/relay/mainrelay.c b/src/apps/relay/mainrelay.c index 0e6e0d6..ef5da09 100644 --- a/src/apps/relay/mainrelay.c +++ b/src/apps/relay/mainrelay.c @@ -265,6 +265,8 @@ static char procgroupname[1025] = "\0"; static void read_config_file(int argc, char **argv, int pass); static void reload_ssl_certs(evutil_socket_t sock, short events, void *args); +static void shutdown_handler(evutil_socket_t sock, short events, void *args); + ////////////////////////////////////////////////// static int make_local_listeners_list(void) { @@ -3211,6 +3213,11 @@ int main(int argc, char **argv) { #else struct event *ev = evsignal_new(turn_params.listener.event_base, SIGUSR2, reload_ssl_certs, NULL); event_add(ev, NULL); + + ev = evsignal_new(turn_params.listener.event_base, SIGTERM, shutdown_handler, NULL); + event_add(ev, NULL); + ev = evsignal_new(turn_params.listener.event_base, SIGINT, shutdown_handler, NULL); + event_add(ev, NULL); #endif drop_privileges(); @@ -3821,4 +3828,12 @@ static void reload_ssl_certs(evutil_socket_t sock, short events, void *args) { UNUSED_ARG(args); } +static void shutdown_handler(evutil_socket_t sock, short events, void *args) { + TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Terminating on signal %d\n", sock); + turn_params.stop_turn_server = 1; + + UNUSED_ARG(events); + UNUSED_ARG(args); +} + ///////////////////////////////