From fe938a0ad2af037fff80114ca2832d41dc48e55e Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 7 Dec 2021 16:21:11 +0100 Subject: indexamajig: Shut down cleanly on SIGUSR1 --- src/im-sandbox.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/im-sandbox.c b/src/im-sandbox.c index 1d334adb..9724c245 100644 --- a/src/im-sandbox.c +++ b/src/im-sandbox.c @@ -674,6 +674,13 @@ static void start_worker_process(struct sandbox *sb, int slot) exit(1); } + sa.sa_handler = SIG_IGN; + r = sigaction(SIGUSR1, &sa, NULL); + if ( r == -1 ) { + ERROR("Failed to set signal handler!\n"); + exit(1); + } + ll = 64 + strlen(sb->tmpdir); tmp = malloc(ll); if ( tmp == NULL ) { @@ -855,6 +862,7 @@ static int fill_queue(struct get_pattern_ctx *gpctx, struct sandbox *sb) volatile sig_atomic_t at_zombies = 0; volatile sig_atomic_t at_interrupt = 0; +volatile sig_atomic_t at_shutdown = 0; static void sigchld_handler(int sig, siginfo_t *si, void *uc_v) { @@ -868,6 +876,12 @@ static void sigint_handler(int sig, siginfo_t *si, void *uc_v) } +static void sigusr1_handler(int sig, siginfo_t *si, void *uc_v) +{ + at_shutdown = 1; +} + + static void check_signals(struct sandbox *sb, const char *semname_q, int respawn) { @@ -880,6 +894,14 @@ static void check_signals(struct sandbox *sb, const char *semname_q, sem_unlink(semname_q); exit(0); } + + if ( at_shutdown ) { + at_shutdown = 0; + STATUS("Received signal - shutting down cleanly.\n"); + pthread_mutex_lock(&sb->shared->totals_lock); + sb->shared->should_shutdown = 1; + pthread_mutex_unlock(&sb->shared->totals_lock); + } } @@ -1151,6 +1173,16 @@ int create_sandbox(struct index_args *iargs, int n_proc, char *prefix, return 0; } + /* Set up signal handler to shut down gracefully on request */ + sa.sa_flags = SA_SIGINFO | SA_NOCLDSTOP | SA_RESTART; + sigemptyset(&sa.sa_mask); + sa.sa_sigaction = sigusr1_handler; + r = sigaction(SIGUSR1, &sa, NULL); + if ( r == -1 ) { + ERROR("Failed to set signal handler!\n"); + return 0; + } + taccs = time_accounts_init(); do { -- cgit v1.2.3