]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Terminate on SIGTERM/SIGINT
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 12:15:40 +0000 (12:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 12:16:44 +0000 (12:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c

index 21d7a70453bd99b99f56e2818d3a89021f84358c..213b8462eb1cf5adc7010d4575f33bed1c4e7ae1 100644 (file)
@@ -34,8 +34,23 @@ struct collecty_daemon {
 
        // Event Loop
        sd_event* loop;
+
+       // Events
+       struct {
+               sd_event_source* sigterm;
+               sd_event_source* sigint;
+       } events;
 };
 
+static int collecty_daemon_terminate(sd_event_source* source,
+               const struct signalfd_siginfo* si, void* data) {
+       collecty_daemon* self = data;
+
+       INFO(self->ctx, "Received signal to terminate...\n");
+
+       return sd_event_exit(sd_event_source_get_event(source), 0);
+}
+
 static int collecty_daemon_setup_loop(collecty_daemon* self) {
        int r;
 
@@ -51,10 +66,30 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) {
                return r;
        }
 
+       // Listen for SIGTERM
+       r = sd_event_add_signal(self->loop, &self->events.sigterm,
+                       SIGTERM|SD_EVENT_SIGNAL_PROCMASK, collecty_daemon_terminate, self);
+       if (r < 0) {
+               ERROR(self->ctx, "Could not register handling SIGTERM: %s\n", strerror(-r));
+               return r;
+       }
+
+       // Listen for SIGINT
+       r = sd_event_add_signal(self->loop, &self->events.sigint,
+                       SIGINT|SD_EVENT_SIGNAL_PROCMASK, collecty_daemon_terminate, self);
+       if (r < 0) {
+               ERROR(self->ctx, "Could not register handling SIGINT: %s\n", strerror(-r));
+               return r;
+       }
+
        return 0;
 }
 
 static void collecty_daemon_free(collecty_daemon* self) {
+       if (self->events.sigterm)
+               sd_event_source_unref(self->events.sigterm);
+       if (self->events.sigint)
+               sd_event_source_unref(self->events.sigint);
        if (self->ctx)
                collecty_ctx_unref(self->ctx);
        if (self->loop)