]> git.ipfire.org Git - network.git/commitdiff
networkd: Register SIGTERM/SIGINT/SIGHUP
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jan 2023 22:24:53 +0000 (22:24 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jan 2023 22:57:29 +0000 (22:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c

index 4d21bb660e88a1286043673551950037c83aaa2f..483252dc493266b6941f126dd34ba3547fa3bf8e 100644 (file)
@@ -18,7 +18,9 @@
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <systemd/sd-event.h>
 
@@ -32,20 +34,60 @@ struct nw_daemon {
        sd_event* loop;
 };
 
+static int nw_daemon_terminate(sd_event_source* source, const struct signalfd_siginfo* si,
+               void* data) {
+       DEBUG("Received signal to terminate...\n");
+
+       return sd_event_exit(sd_event_source_get_event(source), 0);
+}
+
+static int nw_daemon_reload(sd_event_source* source, const struct signalfd_siginfo* si,
+               void* data) {
+       DEBUG("Received signal to reload...\n");
+
+       // TODO
+
+       return 0;
+}
+
 static int nw_daemon_setup_loop(struct nw_daemon* daemon) {
        int r;
 
        // Fetch a reference to the default event loop
        r = sd_event_default(&daemon->loop);
        if (r < 0) {
-               ERROR("Could not setup event loop: %m\n");
+               ERROR("Could not setup event loop: %s\n", strerror(-r));
                return 1;
        }
 
        // Enable the watchdog
        r = sd_event_set_watchdog(daemon->loop, 1);
        if (r < 0) {
-               ERROR("Could not activate watchdog: %m\n");
+               ERROR("Could not activate watchdog: %s\n", strerror(-r));
+               return 1;
+       }
+
+       // Listen for SIGTERM
+       r = sd_event_add_signal(daemon->loop, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK,
+               nw_daemon_terminate, daemon);
+       if (r < 0) {
+               ERROR("Could not register handling SIGTERM: %s\n", strerror(-r));
+               return 1;
+       }
+
+       // Listen for SIGINT
+       r = sd_event_add_signal(daemon->loop, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK,
+               nw_daemon_terminate, daemon);
+       if (r < 0) {
+               ERROR("Could not register handling SIGINT: %s\n", strerror(-r));
+               return 1;
+       }
+
+       // Listen for SIGHUP
+       r = sd_event_add_signal(daemon->loop, NULL, SIGHUP|SD_EVENT_SIGNAL_PROCMASK,
+               nw_daemon_reload, daemon);
+       if (r < 0) {
+               ERROR("Could not register handling SIGHUP: %s\n", strerror(-r));
                return 1;
        }