]> git.ipfire.org Git - collecty.git/commitdiff
modules: Make them a full-blown object
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 13:32:00 +0000 (13:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 13:32:00 +0000 (13:32 +0000)
This way we can store some module-specific things.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c
src/daemon/module.c
src/daemon/module.h
src/daemon/modules.c
src/daemon/modules.h
src/daemon/modules/loadavg.c
src/daemon/modules/loadavg.h

index 06faf1bd6719b6cae6ea63a904a836de51e81ef7..2daf4bca2b1846894df358b2a0db62eca0b73c32 100644 (file)
@@ -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;
index 598ff2181933b04dcc5163ee63f61bd5d74061c3..cc7fbc3df8cf286bf5ddb746a2ad484142e7c1a5 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
 #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;
 }
index e62f59f4d5d041ceccea1d2a5ded57cab2547ecf..fc5db7c4b8115d4b170b2c438e1c1b498c017993 100644 (file)
 #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 */
index 9f21640360784db2853222a2e2aae34e8ae7b817..d48f259295036af232c115ebc8f6ac3f35e3cf9f 100644 (file)
 #############################################################################*/
 
 #include <stddef.h>
+#include <sys/queue.h>
 
 #include "ctx.h"
+#include "daemon.h"
 #include "module.h"
 #include "modules.h"
 
 #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;
 }
index 23f81edad3ba08fbb9052e8b6f8951a335ec0f04..61c072b4afdcc566a49aa039854cbe141a347eff 100644 (file)
@@ -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 */
index 641dfdca4c3b49fd7d93ca9aa75360fe3d6fa709..276ea37a56e886fd153102b32fd4760c15aba0a3 100644 (file)
@@ -21,6 +21,6 @@
 #include "../ctx.h"
 #include "loadavg.h"
 
-collecty_module loadavg_module = {
+const collecty_module_methods loadavg_module = {
        .name = "loadavg",
 };
index da9e30c980035f051bf1917554d38a709dd0308c..679a978b7bfb455cf3e72650601defa033e84937 100644 (file)
@@ -23,6 +23,6 @@
 
 #include "../module.h"
 
-extern collecty_module loadavg_module;
+extern const collecty_module_methods loadavg_module;
 
 #endif /* COLLECTY_MODULE_LOADAVG_H */