From: Michael Tremer Date: Sat, 27 Sep 2025 13:32:00 +0000 (+0000) Subject: modules: Make them a full-blown object X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f6cc737c2574ba09c7077a5c4f8621f5a05e8da;p=telemetry.git modules: Make them a full-blown object This way we can store some module-specific things. Signed-off-by: Michael Tremer --- diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 06faf1b..2daf4bc 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -46,17 +46,17 @@ struct collecty_daemon { }; static int collecty_daemon_modules_init(sd_event_source* source, void* data) { - collecty_ctx* ctx = data; + collecty_daemon* daemon = data; // Initialize all modules - return collecty_modules_init(ctx); + return collecty_modules_init(daemon->ctx, daemon); } static int collecty_daemon_modules_shutdown(sd_event_source* source, void* data) { - collecty_ctx* ctx = data; + collecty_daemon* daemon = data; // Shutdown all modules - return collecty_modules_shutdown(ctx); + return collecty_modules_shutdown(daemon->ctx, daemon); } static int collecty_daemon_terminate(sd_event_source* source, @@ -101,7 +101,7 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) { // Initialize all modules when the loop starts r = sd_event_add_defer(self->loop, &self->events.modules_init, - collecty_daemon_modules_init, self->ctx); + collecty_daemon_modules_init, self); if (r < 0) { ERROR(self->ctx, "Failed to register module init: %s\n", strerror(-r)); return r; @@ -109,7 +109,7 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) { // Shutdown all modules when the loop exits r = sd_event_add_exit(self->loop, &self->events.modules_shutdown, - collecty_daemon_modules_shutdown, self->ctx); + collecty_daemon_modules_shutdown, self); if (r < 0) { ERROR(self->ctx, "Failed to register module shutdown: %s\n", strerror(-r)); return r; diff --git a/src/daemon/module.c b/src/daemon/module.c index 598ff21..cc7fbc3 100644 --- a/src/daemon/module.c +++ b/src/daemon/module.c @@ -18,45 +18,103 @@ # # #############################################################################*/ +#include +#include #include #include "ctx.h" +#include "daemon.h" #include "module.h" -int collecty_module_init(collecty_ctx* ctx, collecty_module* module) { +struct collecty_module { + collecty_ctx* ctx; + int nrefs; + + // Daemon + collecty_daemon* daemon; + + // Methods + const collecty_module_methods* methods; +}; + +static int collecty_module_init(collecty_module* self) { int r; - // Nothing to do if the module does not have an init method - if (!module->init) + // Do nothing if there is no init function + if (!self->methods->init) return 0; - DEBUG(ctx, "Initializing %s...\n", module->name); - // Initialize the module - r = module->init(ctx); + r = self->methods->init(self->ctx); if (r < 0) { - ERROR(ctx, "Failed to initialize %s: %s\n", module->name, strerror(-r)); - return r; + ERROR(self->ctx, "Failed to initialize %s: %s\n", + collecty_module_name(self), strerror(-r)); } - return 0; + return r; +} + +static void collecty_module_free(collecty_module* self) { + if (self->methods->free) + self->methods->free(self->ctx); + + if (self->daemon) + collecty_daemon_unref(self->daemon); + if (self->ctx) + collecty_ctx_unref(self->ctx); } -int collecty_module_shutdown(collecty_ctx* ctx, collecty_module* module) { +int collecty_module_create(collecty_module** module, + collecty_ctx* ctx, collecty_daemon* daemon, const collecty_module_methods* methods) { + collecty_module* self = NULL; int r; - // Nothing to do if the module does not have an shutdown method - if (!module->shutdown) - return 0; + // Allocate some memory + self = calloc(1, sizeof(*self)); + if (!self) + return -errno; - DEBUG(ctx, "Shutting down %s...\n", module->name); + // Initialize the reference counter + self->nrefs = 1; - // Shut down the module - r = module->shutdown(ctx); - if (r < 0) { - ERROR(ctx, "Failed to shutdown %s: %s\n", module->name, strerror(-r)); - return r; - } + // Store a reference to the context + self->ctx = collecty_ctx_ref(ctx); + + // Store a reference to the daemon + self->daemon = collecty_daemon_ref(daemon); + // Store the methods + self->methods = methods; + + // Initialize the module + r = collecty_module_init(self); + if (r < 0) + goto ERROR; + + // Return the pointer + *module = self; return 0; + +ERROR: + if (self) + collecty_module_unref(self); + + return r; +} + +collecty_module* collecty_module_ref(collecty_module* self) { + ++self->nrefs; + return self; +} + +collecty_module* collecty_module_unref(collecty_module* self) { + if (--self->nrefs > 0) + return self; + + collecty_module_free(self); + return NULL; +} + +const char* collecty_module_name(collecty_module* self) { + return self->methods->name; } diff --git a/src/daemon/module.h b/src/daemon/module.h index e62f59f..fc5db7c 100644 --- a/src/daemon/module.h +++ b/src/daemon/module.h @@ -22,18 +22,26 @@ #define COLLECTY_MODULE_H #include "ctx.h" +#include "daemon.h" -typedef struct collecty_module { +typedef struct collecty_module_methods { const char* name; // Init int (*init)(collecty_ctx* ctx); - // Shutdown - int (*shutdown)(collecty_ctx* ctx); -} collecty_module; + // Free + int (*free)(collecty_ctx* ctx); +} collecty_module_methods; -int collecty_module_init(collecty_ctx* ctx, collecty_module* module); -int collecty_module_shutdown(collecty_ctx* ctx, collecty_module* module); +typedef struct collecty_module collecty_module; + +int collecty_module_create(collecty_module** module, + collecty_ctx* ctx, collecty_daemon* daemon, const collecty_module_methods* methods); + +collecty_module* collecty_module_ref(collecty_module* self); +collecty_module* collecty_module_unref(collecty_module* self); + +const char* collecty_module_name(collecty_module* self); #endif /* COLLECTY_MODULE_H */ diff --git a/src/daemon/modules.c b/src/daemon/modules.c index 9f21640..d48f259 100644 --- a/src/daemon/modules.c +++ b/src/daemon/modules.c @@ -19,8 +19,10 @@ #############################################################################*/ #include +#include #include "ctx.h" +#include "daemon.h" #include "module.h" #include "modules.h" @@ -28,19 +30,30 @@ #include "modules/loadavg.h" // Register all modules -static collecty_module* modules[] = { +static const collecty_module_methods* available_modules[] = { &loadavg_module, NULL, }; -int collecty_modules_init(collecty_ctx* ctx) { +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 (collecty_module** m = modules; *m; m++) { - r = collecty_module_init(ctx, *m); + for (const collecty_module_methods** m = available_modules; *m; m++) { + r = collecty_module_create(&module, ctx, daemon, *m); if (r < 0) return r; } @@ -48,17 +61,19 @@ int collecty_modules_init(collecty_ctx* ctx) { return 0; } -int collecty_modules_shutdown(collecty_ctx* ctx) { +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; } +#endif return 0; } diff --git a/src/daemon/modules.h b/src/daemon/modules.h index 23f81ed..61c072b 100644 --- a/src/daemon/modules.h +++ b/src/daemon/modules.h @@ -22,8 +22,9 @@ #define COLLECTY_MODULES_H #include "ctx.h" +#include "daemon.h" -int collecty_modules_init(collecty_ctx* ctx); -int collecty_modules_shutdown(collecty_ctx* ctx); +int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon); +int collecty_modules_shutdown(collecty_ctx* ctx, collecty_daemon* daemon); #endif /* COLLECTY_MODULES_H */ diff --git a/src/daemon/modules/loadavg.c b/src/daemon/modules/loadavg.c index 641dfdc..276ea37 100644 --- a/src/daemon/modules/loadavg.c +++ b/src/daemon/modules/loadavg.c @@ -21,6 +21,6 @@ #include "../ctx.h" #include "loadavg.h" -collecty_module loadavg_module = { +const collecty_module_methods loadavg_module = { .name = "loadavg", }; diff --git a/src/daemon/modules/loadavg.h b/src/daemon/modules/loadavg.h index da9e30c..679a978 100644 --- a/src/daemon/modules/loadavg.h +++ b/src/daemon/modules/loadavg.h @@ -23,6 +23,6 @@ #include "../module.h" -extern collecty_module loadavg_module; +extern const collecty_module_methods loadavg_module; #endif /* COLLECTY_MODULE_LOADAVG_H */