]> git.ipfire.org Git - collecty.git/commitdiff
modules: Have each module call their own destructor
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 13:58:52 +0000 (13:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 13:58:52 +0000 (13:58 +0000)
That allows us to not keep any references to the modules.

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

index 2daf4bca2b1846894df358b2a0db62eca0b73c32..993efcc382828bb54b331691366a996338373bdb 100644 (file)
@@ -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;
 
index cda6a3dbf282935eae77b02489161e90be3c10c2..a2f98a0673bf97587ad6bc1dfd75424160095ba9 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef COLLECTY_DAEMON_H
 #define COLLECTY_DAEMON_H
 
+#include <systemd/sd-event.h>
+
 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 */
index cc7fbc3df8cf286bf5ddb746a2ad484142e7c1a5..b6dcb40f66769d4bb44edcc4602a52215ad59a10 100644 (file)
@@ -22,6 +22,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <systemd/sd-event.h>
+
 #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)
index d48f259295036af232c115ebc8f6ac3f35e3cf9f..eead3822cb00c17ba2ac59a918461e24c4c97e86 100644 (file)
@@ -18,8 +18,9 @@
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
 #include <stddef.h>
-#include <sys/queue.h>
+#include <stdlib.h>
 
 #include "ctx.h"
 #include "daemon.h"
 #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;
 }
index 61c072b4afdcc566a49aa039854cbe141a347eff..0dfc45078c7df7d1d3996ea821585bc019b17381 100644 (file)
@@ -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 */