]> git.ipfire.org Git - telemetry.git/commitdiff
daemon: Create an event loop
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 12:10:30 +0000 (12:10 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 12:10:30 +0000 (12:10 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c
src/daemon/daemon.h
src/daemon/main.c

index 13bb41fc23a48b38f5cdb4f1c4fc29f51498e079..15a2c8dcaa7e48300a2013a628834bd7a1c84bd3 100644 (file)
 
 #include <errno.h>
 #include <stdlib.h>
+#include <string.h>
+
+#include <systemd/sd-daemon.h>
+#include <systemd/sd-event.h>
 
 #include "ctx.h"
 #include "daemon.h"
 struct collecty_daemon {
        collecty_ctx* ctx;
        int nrefs;
+
+       // Event Loop
+       sd_event* loop;
 };
 
-static void collecty_daemon_free(collecty_daemon* daemon) {
-       if (daemon->ctx)
-               collecty_ctx_unref(daemon->ctx);
+static int collecty_daemon_setup_loop(collecty_daemon* self) {
+       int r;
+
+       // Create a new loop
+       r = sd_event_new(&self->loop);
+       if (r < 0)
+               return r;
+
+       return 0;
+}
+
+static void collecty_daemon_free(collecty_daemon* self) {
+       if (self->ctx)
+               collecty_ctx_unref(self->ctx);
+       if (self->loop)
+               sd_event_unref(self->loop);
 }
 
 int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx) {
        collecty_daemon* self = NULL;
+       int r;
 
        // Allocate some memory
        self = calloc(1, sizeof(*self));
@@ -48,10 +69,20 @@ int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx) {
        // Store a reference to the context
        self->ctx = collecty_ctx_ref(ctx);
 
+       // Setup the event loop
+       r = collecty_daemon_setup_loop(self);
+       if (r < 0)
+               goto ERROR;
+
        // Return the pointer
        *daemon = self;
-
        return 0;
+
+ERROR:
+       if (self)
+               collecty_daemon_unref(self);
+
+       return r;
 }
 
 collecty_daemon* collecty_daemon_ref(collecty_daemon* daemon) {
@@ -66,3 +97,27 @@ collecty_daemon* collecty_daemon_unref(collecty_daemon* daemon) {
        collecty_daemon_free(daemon);
        return NULL;
 }
+
+int collecty_daemon_run(collecty_daemon* self) {
+       int r;
+
+       // We are now ready
+       sd_notify(0, "READY=1\n" "STATUS=Ready...");
+
+       // Launch the event loop
+       r = sd_event_loop(self->loop);
+       if (r < 0) {
+               ERROR(self->ctx, "Could not run the event loop: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
+       // Let systemd know that we are shutting down
+       sd_notify(0, "STOPPING=1\n" "STATUS=Shutting down...");
+
+       return 0;
+
+ERROR:
+       sd_notifyf(0, "ERRNO=%i", -r);
+
+       return 1;
+}
index 04ecc65d5479eb62f8b541f608f7a050dffdc542..cda6a3dbf282935eae77b02489161e90be3c10c2 100644 (file)
@@ -30,4 +30,6 @@ int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx);
 collecty_daemon* collecty_daemon_ref(collecty_daemon* daemon);
 collecty_daemon* collecty_daemon_unref(collecty_daemon* daemon);
 
+int collecty_daemon_run(collecty_daemon* self);
+
 #endif /* COLLECTY_DAEMON_H */
index dbcc3fb16262afce14a48b1336f11b897007fa78..58a110a07bc58f27a12b46fd1a833b5e3dccdaab 100644 (file)
@@ -88,6 +88,9 @@ int main(int argc, char* argv[]) {
                goto ERROR;
        }
 
+       // Run the daemon
+       r = collecty_daemon_run(daemon);
+
 ERROR:
        if (daemon)
                collecty_daemon_unref(daemon);