From: Michael Tremer Date: Sat, 27 Sep 2025 12:10:30 +0000 (+0000) Subject: daemon: Create an event loop X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7dd1877b0a5fde2ba44ec400e1ed824ae0231f9a;p=telemetry.git daemon: Create an event loop Signed-off-by: Michael Tremer --- diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 13bb41f..15a2c8d 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -20,6 +20,10 @@ #include #include +#include + +#include +#include #include "ctx.h" #include "daemon.h" @@ -27,15 +31,32 @@ 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; +} diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 04ecc65..cda6a3d 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -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 */ diff --git a/src/daemon/main.c b/src/daemon/main.c index dbcc3fb..58a110a 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -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);