From: Michael Tremer Date: Sat, 27 Sep 2025 13:58:52 +0000 (+0000) Subject: modules: Have each module call their own destructor X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9cbb39f287980ad89cad2b3c9638fdce248b2eef;p=collecty.git modules: Have each module call their own destructor That allows us to not keep any references to the modules. Signed-off-by: Michael Tremer --- diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 2daf4bc..993efcc 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -41,7 +41,6 @@ struct collecty_daemon { sd_event_source* sigterm; sd_event_source* sigint; sd_event_source* modules_init; - sd_event_source* modules_shutdown; } events; }; @@ -52,13 +51,6 @@ static int collecty_daemon_modules_init(sd_event_source* source, void* data) { return collecty_modules_init(daemon->ctx, daemon); } -static int collecty_daemon_modules_shutdown(sd_event_source* source, void* data) { - collecty_daemon* daemon = data; - - // Shutdown all modules - return collecty_modules_shutdown(daemon->ctx, daemon); -} - static int collecty_daemon_terminate(sd_event_source* source, const struct signalfd_siginfo* si, void* data) { collecty_daemon* self = data; @@ -107,20 +99,10 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) { return r; } - // Shutdown all modules when the loop exits - r = sd_event_add_exit(self->loop, &self->events.modules_shutdown, - collecty_daemon_modules_shutdown, self); - if (r < 0) { - ERROR(self->ctx, "Failed to register module shutdown: %s\n", strerror(-r)); - return r; - } - return 0; } static void collecty_daemon_free(collecty_daemon* self) { - if (self->events.modules_shutdown) - sd_event_source_unref(self->events.modules_shutdown); if (self->events.modules_init) sd_event_source_unref(self->events.modules_init); if (self->events.sigterm) @@ -177,6 +159,10 @@ collecty_daemon* collecty_daemon_unref(collecty_daemon* daemon) { return NULL; } +sd_event* collecty_daemon_loop(collecty_daemon* self) { + return sd_event_ref(self->loop); +} + int collecty_daemon_run(collecty_daemon* self) { int r; diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index cda6a3d..a2f98a0 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -21,6 +21,8 @@ #ifndef COLLECTY_DAEMON_H #define COLLECTY_DAEMON_H +#include + typedef struct collecty_daemon collecty_daemon; #include "ctx.h" @@ -30,6 +32,8 @@ 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); +sd_event* collecty_daemon_loop(collecty_daemon* self); + int collecty_daemon_run(collecty_daemon* self); #endif /* COLLECTY_DAEMON_H */ diff --git a/src/daemon/module.c b/src/daemon/module.c index cc7fbc3..b6dcb40 100644 --- a/src/daemon/module.c +++ b/src/daemon/module.c @@ -22,6 +22,8 @@ #include #include +#include + #include "ctx.h" #include "daemon.h" #include "module.h" @@ -35,8 +37,40 @@ struct collecty_module { // Methods const collecty_module_methods* methods; + + // Event Loop + sd_event* loop; + + // Events + struct { + sd_event_source* shutdown; + } events; }; +static int collecty_daemon_module_shutdown(sd_event_source* source, void* data) { + collecty_module* self = data; + + DEBUG(self->ctx, "Shutting down module '%s'\n", collecty_module_name(self)); + + // Decrement the reference counter to free the module + collecty_module_unref(self); + + return 0; +} + +static int collecty_module_register_shutdown(collecty_module* self) { + int r; + + // Call the shutdown function when the loop exits + r = sd_event_add_exit(self->loop, &self->events.shutdown, + collecty_daemon_module_shutdown, collecty_module_ref(self)); + if (r < 0) { + ERROR(self->ctx, "Failed to register module shutdown: %s\n", strerror(-r)); + } + + return r; +} + static int collecty_module_init(collecty_module* self) { int r; @@ -57,7 +91,10 @@ static int collecty_module_init(collecty_module* self) { static void collecty_module_free(collecty_module* self) { if (self->methods->free) self->methods->free(self->ctx); - + if (self->events.shutdown) + sd_event_source_unref(self->events.shutdown); + if (self->loop) + sd_event_unref(self->loop); if (self->daemon) collecty_daemon_unref(self->daemon); if (self->ctx) @@ -83,9 +120,17 @@ int collecty_module_create(collecty_module** module, // Store a reference to the daemon self->daemon = collecty_daemon_ref(daemon); + // Fetch a reference to the event loop + self->loop = collecty_daemon_loop(daemon); + // Store the methods self->methods = methods; + // Register shutdown + r = collecty_module_register_shutdown(self); + if (r < 0) + goto ERROR; + // Initialize the module r = collecty_module_init(self); if (r < 0) diff --git a/src/daemon/modules.c b/src/daemon/modules.c index d48f259..eead382 100644 --- a/src/daemon/modules.c +++ b/src/daemon/modules.c @@ -18,8 +18,9 @@ # # #############################################################################*/ +#include #include -#include +#include #include "ctx.h" #include "daemon.h" @@ -30,50 +31,25 @@ #include "modules/loadavg.h" // Register all modules -static const collecty_module_methods* available_modules[] = { +static const collecty_module_methods* modules[] = { &loadavg_module, NULL, }; -struct module { - collecty_module* module; - STAILQ_ENTRY(module) nodes; -}; - -STAILQ_HEAD(modules, module) modules; - int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon) { collecty_module* module = NULL; int r; - // Initialize the queue - STAILQ_INIT(&modules); - DEBUG(ctx, "Initializing all modules...\n"); // Initialize all modules - for (const collecty_module_methods** m = available_modules; *m; m++) { + for (const collecty_module_methods** m = modules; *m; m++) { r = collecty_module_create(&module, ctx, daemon, *m); if (r < 0) return r; - } - return 0; -} - -int collecty_modules_shutdown(collecty_ctx* ctx, collecty_daemon* daemon) { - int r; - - DEBUG(ctx, "Shutting down all modules...\n"); - -#if 0 - // Shutdown all modules - for (collecty_module** m = modules; *m; m++) { - r = collecty_module_shutdown(ctx, *m); - if (r < 0) - return r; + collecty_module_unref(module); } -#endif return 0; } diff --git a/src/daemon/modules.h b/src/daemon/modules.h index 61c072b..0dfc450 100644 --- a/src/daemon/modules.h +++ b/src/daemon/modules.h @@ -25,6 +25,5 @@ #include "daemon.h" int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon); -int collecty_modules_shutdown(collecty_ctx* ctx, collecty_daemon* daemon); #endif /* COLLECTY_MODULES_H */