From 7b6c994712ebe6fc2a697f0d37dae06e5368c685 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 30 Sep 2025 16:50:25 +0000 Subject: [PATCH] daemon: Create a modules registry Just like we have for graphs now Signed-off-by: Michael Tremer --- src/daemon/daemon.c | 13 +++++- src/daemon/modules.c | 106 ++++++++++++++++++++++++++++++++++++++++--- src/daemon/modules.h | 8 +++- 3 files changed, 119 insertions(+), 8 deletions(-) diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 436db95..c3eabe4 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -56,6 +56,9 @@ struct collecty_daemon { // Queue collecty_queue* queue; + // Modules + collecty_modules* modules; + // Graphs collecty_graphs* graphs; }; @@ -67,7 +70,7 @@ static int collecty_daemon_init(sd_event_source* source, void* data) { DEBUG(self->ctx, "Initializing daemon...\n"); // Initialize all modules - r = collecty_modules_init(self->ctx, self); + r = collecty_modules_create(&self->modules, self->ctx, self); if (r < 0) return r; @@ -90,6 +93,12 @@ static int collecty_daemon_exit(sd_event_source* source, void* data) { self->graphs = NULL; } + // Free all modules + if (self->modules) { + collecty_modules_unref(self->modules); + self->modules = NULL; + } + return 0; } @@ -179,6 +188,8 @@ static void collecty_daemon_free(collecty_daemon* self) { sd_event_source_unref(self->events.init); if (self->events.exit) sd_event_source_unref(self->events.exit); + if (self->modules) + collecty_modules_unref(self->modules); if (self->graphs) collecty_graphs_unref(self->graphs); if (self->queue) diff --git a/src/daemon/modules.c b/src/daemon/modules.c index c9e2698..dfd4c91 100644 --- a/src/daemon/modules.c +++ b/src/daemon/modules.c @@ -33,27 +33,121 @@ #include "modules/loadavg.h" // Register all modules -static const collecty_module_methods* modules[] = { +static const collecty_module_methods* module_impls[] = { &conntrack_module, &contextswitches_module, &loadavg_module, NULL, }; -int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon) { +struct collecty_modules { + collecty_ctx* ctx; + int nrefs; + + // Daemon + collecty_daemon* daemon; + + // Modules + collecty_module** modules; + unsigned int num_modules; +}; + + +static int collecty_modules_init(collecty_modules* self) { + collecty_module** modules = NULL; collecty_module* module = NULL; int r; - DEBUG(ctx, "Initializing all modules...\n"); - // Initialize all modules - for (const collecty_module_methods** m = modules; *m; m++) { - r = collecty_module_create(&module, ctx, daemon, *m); + for (const collecty_module_methods** impl = module_impls; *impl; impl++) { + r = collecty_module_create(&module, self->ctx, self->daemon, *impl); if (r < 0) return r; + // Make space to store the module + modules = reallocarray(self->modules, self->num_modules + 1, sizeof(*self->modules)); + if (!modules) { + r = -errno; + goto ERROR; + } + + // Store a reference to the module in the array + modules[self->num_modules++] = collecty_module_ref(module); + + // Replace the array + self->modules = modules; + + // Unref the module + collecty_module_unref(module); + module = NULL; + } + + return 0; + +ERROR: + if (module) collecty_module_unref(module); + + return r; +} + +static void collecty_modules_free(collecty_modules* self) { + if (self->modules) { + for (unsigned int i = 0; i < self->num_modules; i++) + collecty_module_unref(self->modules[i]); + free(self->modules); } + if (self->daemon) + collecty_daemon_unref(self->daemon); + if (self->ctx) + collecty_ctx_unref(self->ctx); + free(self); +} + +int collecty_modules_create(collecty_modules** modules, + collecty_ctx* ctx, collecty_daemon* daemon) { + collecty_modules* self = NULL; + int r; + + // Allocate some memory + self = calloc(1, sizeof(*self)); + if (!self) + return -errno; + + // Initialize the reference counter + self->nrefs = 1; + + // Store a reference to the context + self->ctx = collecty_ctx_ref(ctx); + + // Store a reference to the daemon + self->daemon = collecty_daemon_ref(daemon); + // Setup all modules + r = collecty_modules_init(self); + if (r < 0) + goto ERROR; + + // Return the pointer + *modules = self; return 0; + +ERROR: + if (self) + collecty_modules_unref(self); + + return r; +} + +collecty_modules* collecty_modules_ref(collecty_modules* self) { + ++self->nrefs; + return self; +} + +collecty_modules* collecty_modules_unref(collecty_modules* self) { + if (--self->nrefs > 0) + return self; + + collecty_modules_free(self); + return NULL; } diff --git a/src/daemon/modules.h b/src/daemon/modules.h index 0dfc450..f244750 100644 --- a/src/daemon/modules.h +++ b/src/daemon/modules.h @@ -21,9 +21,15 @@ #ifndef COLLECTY_MODULES_H #define COLLECTY_MODULES_H +typedef struct collecty_modules collecty_modules; + #include "ctx.h" #include "daemon.h" -int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon); +int collecty_modules_create(collecty_modules** modules, + collecty_ctx* ctx, collecty_daemon* daemon); + +collecty_modules* collecty_modules_ref(collecty_modules* self); +collecty_modules* collecty_modules_unref(collecty_modules* self); #endif /* COLLECTY_MODULES_H */ -- 2.47.3