]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Flush the queue on SIGHUP
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 15:46:59 +0000 (15:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 15:46:59 +0000 (15:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c

index 95be5ed2af08e74d44f4fe459877c6a868cce3fb..3dff76b940b5dff1fab6ea1501c04191b991d82a 100644 (file)
@@ -61,6 +61,7 @@ struct collecty_daemon {
        struct {
                sd_event_source* sigterm;
                sd_event_source* sigint;
+               sd_event_source* sighup;
                sd_event_source* modules_init;
                sd_event_source* flush;
        } events;
@@ -69,6 +70,14 @@ struct collecty_daemon {
        STAILQ_HEAD(queue, collecty_queue_object) queue;
 };
 
+static int collecty_daemon_flush(collecty_daemon* self) {
+       DEBUG(self->ctx, "Flushing the queue...\n");
+
+       // XXX TODO
+
+       return 0;
+}
+
 static int collecty_daemon_modules_init(sd_event_source* source, void* data) {
        collecty_daemon* daemon = data;
 
@@ -85,6 +94,14 @@ static int collecty_daemon_terminate(sd_event_source* source,
        return sd_event_exit(sd_event_source_get_event(source), 0);
 }
 
+static int collecty_daemon_SIGHUP(sd_event_source* source,
+               const struct signalfd_siginfo* si, void* data) {
+       collecty_daemon* self = data;
+
+       // Flush the queue
+       return collecty_daemon_flush(self);
+}
+
 static int collecty_daemon_setup_loop(collecty_daemon* self) {
        int r;
 
@@ -116,6 +133,14 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) {
                return r;
        }
 
+       // Listen for SIGHUP
+       r = sd_event_add_signal(self->loop, &self->events.sighup,
+                       SIGHUP|SD_EVENT_SIGNAL_PROCMASK, collecty_daemon_SIGHUP, self);
+       if (r < 0) {
+               ERROR(self->ctx, "Could not register handling SIGHUP: %s\n", strerror(-r));
+               return r;
+       }
+
        // Initialize all modules when the loop starts
        r = sd_event_add_defer(self->loop, &self->events.modules_init,
                        collecty_daemon_modules_init, self);
@@ -137,14 +162,10 @@ static void collecty_daemon_free_queue_object(struct collecty_queue_object* o) {
        free(o);
 }
 
-static int collecty_daemon_flush(sd_event_source* source, uint64_t usec, void* data) {
+static int collecty_daemon_flush_timer(sd_event_source* source, uint64_t usec, void* data) {
        collecty_daemon* self = data;
        int r;
 
-       DEBUG(self->ctx, "Flushing the queue...\n");
-
-       // TODO
-
        // Call again soon...
        r = sd_event_source_set_time(source, usec + FLUSH_QUEUE_INTERVAL);
        if (r < 0)
@@ -155,7 +176,8 @@ static int collecty_daemon_flush(sd_event_source* source, uint64_t usec, void* d
        if (r < 0)
                return r;
 
-       return 0;
+       // Flush the queue
+       return collecty_daemon_flush(self);
 }
 
 static int collecty_daemon_setup_queue(collecty_daemon* self) {
@@ -166,7 +188,7 @@ static int collecty_daemon_setup_queue(collecty_daemon* self) {
 
        // Create a timer which regularly flushes the queue
        r = sd_event_add_time_relative(self->loop, &self->events.flush,
-               CLOCK_MONOTONIC, FLUSH_QUEUE_INTERVAL, 0, collecty_daemon_flush, self);
+               CLOCK_MONOTONIC, FLUSH_QUEUE_INTERVAL, 0, collecty_daemon_flush_timer, self);
        if (r < 0) {
                ERROR(self->ctx, "Failed to set up the flush timer: %s\n", strerror(-r));
                return r;
@@ -199,6 +221,8 @@ static void collecty_daemon_free(collecty_daemon* self) {
                sd_event_source_unref(self->events.sigterm);
        if (self->events.sigint)
                sd_event_source_unref(self->events.sigint);
+       if (self->events.sighup)
+               sd_event_source_unref(self->events.sighup);
        if (self->ctx)
                collecty_ctx_unref(self->ctx);
        if (self->loop)