aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/server.c2
-rw-r--r--src/signal.c38
-rw-r--r--src/signal.h8
-rw-r--r--src/tcp_server.c14
-rw-r--r--src/worker.c2
5 files changed, 38 insertions, 26 deletions
diff --git a/src/server.c b/src/server.c
index 67f9e17..3df1f7d 100644
--- a/src/server.c
+++ b/src/server.c
@@ -213,7 +213,7 @@ int server_create(struct server **_server, const struct settings *settings)
struct storage_settings storage_settings;
int ret = 0;
- ret = signal_install_global_handler();
+ ret = signal_handle_stop_signals();
if (ret < 0)
return ret;
diff --git a/src/signal.c b/src/signal.c
index ecf45ca..0676e60 100644
--- a/src/signal.c
+++ b/src/signal.c
@@ -10,8 +10,11 @@
#include "log.h"
#include <signal.h>
+#include <stddef.h>
#include <string.h>
+static int stop_signals[] = {SIGINT, SIGTERM, SIGQUIT};
+
volatile sig_atomic_t global_stop_flag = 0;
static void set_global_stop_flag(UNUSED int signum)
@@ -32,7 +35,7 @@ static int my_sigaction(int signo, const struct sigaction *act)
return ret;
}
-int signal_install_global_handler(void)
+int signal_handle_stop_signals(void)
{
int ret = 0;
@@ -42,20 +45,17 @@ int signal_install_global_handler(void)
/* Don't care about proper cleanup here; we exit the program if this
* fails anyway. */
- ret = my_sigaction(SIGINT, &sa);
- if (ret < 0)
- return ret;
- ret = my_sigaction(SIGQUIT, &sa);
- if (ret < 0)
- return ret;
- ret = my_sigaction(SIGTERM, &sa);
- if (ret < 0)
- return ret;
+
+ for (size_t i = 0; i < sizeof(stop_signals) / sizeof(stop_signals[0]); ++i) {
+ ret = my_sigaction(stop_signals[i], &sa);
+ if (ret < 0)
+ return ret;
+ }
return ret;
}
-int signal_set(const sigset_t *new, sigset_t *old)
+static int signal_set(const sigset_t *new, sigset_t *old)
{
int ret = 0;
@@ -68,19 +68,25 @@ int signal_set(const sigset_t *new, sigset_t *old)
return ret;
}
-int signal_block_parent(sigset_t *old)
+int signal_block_all(sigset_t *old)
{
sigset_t new;
sigfillset(&new);
return signal_set(&new, old);
}
-int signal_block_child(void)
+int signal_restore(const sigset_t *new)
+{
+ return signal_set(new, NULL);
+}
+
+int signal_unblock_all_except_stop_signals(void)
{
sigset_t set;
sigemptyset(&set);
- sigaddset(&set, SIGINT);
- sigaddset(&set, SIGQUIT);
- sigaddset(&set, SIGTERM);
+
+ for (size_t i = 0; i < sizeof(stop_signals) / sizeof(stop_signals[0]); ++i)
+ sigaddset(&set, stop_signals[i]);
+
return signal_set(&set, NULL);
}
diff --git a/src/signal.h b/src/signal.h
index 88c9734..713c842 100644
--- a/src/signal.h
+++ b/src/signal.h
@@ -11,11 +11,11 @@
#include <signal.h>
extern volatile sig_atomic_t global_stop_flag;
-int signal_install_global_handler(void);
+int signal_handle_stop_signals(void);
-int signal_set(const sigset_t *new, sigset_t *old);
+int signal_block_all(sigset_t *old);
+int signal_restore(const sigset_t *new);
-int signal_block_parent(sigset_t *old);
-int signal_block_child(void);
+int signal_unblock_all_except_stop_signals(void);
#endif
diff --git a/src/tcp_server.c b/src/tcp_server.c
index d840414..dbf1146 100644
--- a/src/tcp_server.c
+++ b/src/tcp_server.c
@@ -60,7 +60,9 @@ static void *connection_thread(void *_ctx)
struct child_context *ctx = (struct child_context *)_ctx;
int ret = 0;
- ret = signal_block_child();
+ /* Let the child thread handle its signals except those that should be
+ * handled in the main thread. */
+ ret = signal_unblock_all_except_stop_signals();
if (ret < 0)
goto free_ctx;
@@ -92,7 +94,10 @@ int tcp_server_accept(const struct tcp_server *server, tcp_server_conn_handler h
goto free_ctx;
ctx->fd = ret;
- ret = signal_block_parent(&old_mask);
+ /* Block all signals (we'll unblock them later); the child thread will
+ * have all signals blocked initially. This allows the main thread to
+ * handle SIGINT/SIGTERM/etc. */
+ ret = signal_block_all(&old_mask);
if (ret < 0)
goto close_conn;
@@ -102,12 +107,13 @@ int tcp_server_accept(const struct tcp_server *server, tcp_server_conn_handler h
goto restore_mask;
}
- signal_set(&old_mask, NULL);
+ /* Restore the previously-enabled signals for handling in the main thread. */
+ signal_restore(&old_mask);
return ret;
restore_mask:
- signal_set(&old_mask, NULL);
+ signal_restore(&old_mask);
close_conn:
log_errno_if(close(ctx->fd), "close");
diff --git a/src/worker.c b/src/worker.c
index a316d7f..d085000 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -27,7 +27,7 @@ int worker_create(struct worker **_worker)
{
int ret = 0;
- ret = signal_install_global_handler();
+ ret = signal_handle_stop_signals();
if (ret < 0)
return ret;