From d97f32cd52e46c47975f4291ea91e8e42287d5a6 Mon Sep 17 00:00:00 2001 From: Erin Shepherd Date: Sun, 16 Jul 2023 21:19:07 +0200 Subject: [PATCH] Add support for graceful restart by signal (SIGUSR2) --- sysdep/unix/io.c | 7 +++++++ sysdep/unix/main.c | 19 +++++++++++++++++++ sysdep/unix/unix.h | 2 ++ 3 files changed, 28 insertions(+) diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index 6aedcfb6..044b19e2 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -2296,6 +2296,13 @@ io_loop(void) async_shutdown_flag = 0; continue; } + if (async_graceful_shutdown_flag) + { + io_log_event(async_graceful_shutdown, NULL); + async_graceful_shutdown(); + async_graceful_shutdown_flag = 0; + continue; + } /* And finally enter poll() to find active sockets */ watchdog_stop(); diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index 0d3ec0c0..16d94e80 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -618,6 +618,14 @@ async_shutdown(void) order_shutdown(0); } +void +async_graceful_shutdown(void) +{ + DBG("Shutting down for graceful restart...\n"); + order_shutdown(1); +} + void sysdep_shutdown_done(void) { @@ -645,6 +653,7 @@ cmd_graceful_restart(void) volatile sig_atomic_t async_config_flag; volatile sig_atomic_t async_dump_flag; volatile sig_atomic_t async_shutdown_flag; +volatile sig_atomic_t async_graceful_shutdown_flag; static void handle_sighup(int sig UNUSED) @@ -667,6 +676,13 @@ handle_sigterm(int sig UNUSED) async_shutdown_flag = 1; } +static void +handle_sigusr2(int sig UNUSED) +{ + DBG("Caught SIGUSR2...\n"); + async_graceful_shutdown_flag = 1; +} + void watchdog_sigalrm(int sig UNUSED); static void @@ -678,6 +694,9 @@ signal_init(void) sa.sa_handler = handle_sigusr; sa.sa_flags = SA_RESTART; sigaction(SIGUSR1, &sa, NULL); + sa.sa_handler = handle_sigusr2; + sa.sa_flags = SA_RESTART; + sigaction(SIGUSR2, &sa, NULL); sa.sa_handler = handle_sighup; sa.sa_flags = SA_RESTART; sigaction(SIGHUP, &sa, NULL); diff --git a/sysdep/unix/unix.h b/sysdep/unix/unix.h index ad85d1ea..df1ad0af 100644 --- a/sysdep/unix/unix.h +++ b/sysdep/unix/unix.h @@ -24,6 +24,7 @@ extern int parse_and_exit; void async_config(void); void async_dump(void); void async_shutdown(void); +void async_graceful_shutdown(void); char *get_hostname(linpool *lp); void cmd_check_config(const char *name); void cmd_reconfig(const char *name, int type, uint timeout); @@ -103,6 +104,7 @@ int sockaddr_read(sockaddr *sa, int af, ip_addr *a, struct iface **ifa, uint *po extern volatile sig_atomic_t async_config_flag; extern volatile sig_atomic_t async_dump_flag; extern volatile sig_atomic_t async_shutdown_flag; +extern volatile sig_atomic_t async_graceful_shutdown_flag; void io_init(void); void io_loop(void); -- 2.39.2