]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Create some scaffolding for modules
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 12:42:45 +0000 (12:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 12:42:45 +0000 (12:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/daemon.c
src/daemon/module.c [new file with mode: 0644]
src/daemon/module.h [new file with mode: 0644]
src/daemon/modules.c [new file with mode: 0644]
src/daemon/modules.h [new file with mode: 0644]

index 32c9f72ec9a4dc28190300ad8da02074a5c08475..724ae0972926028d4c633cd46d1e432d4f960d5f 100644 (file)
@@ -95,7 +95,11 @@ dist_collectyd_SOURCES = \
        src/daemon/daemon.h \
        src/daemon/logging.c \
        src/daemon/logging.h \
-       src/daemon/main.c
+       src/daemon/main.c \
+       src/daemon/module.c \
+       src/daemon/module.h \
+       src/daemon/modules.c \
+       src/daemon/modules.h
 
 collectyd_CPPFLAGS = \
        $(AM_CPPFLAGS)
index 213b8462eb1cf5adc7010d4575f33bed1c4e7ae1..06faf1bd6719b6cae6ea63a904a836de51e81ef7 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "ctx.h"
 #include "daemon.h"
+#include "modules.h"
 
 struct collecty_daemon {
        collecty_ctx* ctx;
@@ -39,9 +40,25 @@ struct collecty_daemon {
        struct {
                sd_event_source* sigterm;
                sd_event_source* sigint;
+               sd_event_source* modules_init;
+               sd_event_source* modules_shutdown;
        } events;
 };
 
+static int collecty_daemon_modules_init(sd_event_source* source, void* data) {
+       collecty_ctx* ctx = data;
+
+       // Initialize all modules
+       return collecty_modules_init(ctx);
+}
+
+static int collecty_daemon_modules_shutdown(sd_event_source* source, void* data) {
+       collecty_ctx* ctx = data;
+
+       // Shutdown all modules
+       return collecty_modules_shutdown(ctx);
+}
+
 static int collecty_daemon_terminate(sd_event_source* source,
                const struct signalfd_siginfo* si, void* data) {
        collecty_daemon* self = data;
@@ -82,10 +99,30 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) {
                return r;
        }
 
+       // Initialize all modules when the loop starts
+       r = sd_event_add_defer(self->loop, &self->events.modules_init,
+                       collecty_daemon_modules_init, self->ctx);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to register module init: %s\n", strerror(-r));
+               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->ctx);
+       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)
                sd_event_source_unref(self->events.sigterm);
        if (self->events.sigint)
diff --git a/src/daemon/module.c b/src/daemon/module.c
new file mode 100644 (file)
index 0000000..598ff21
--- /dev/null
@@ -0,0 +1,62 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <string.h>
+
+#include "ctx.h"
+#include "module.h"
+
+int collecty_module_init(collecty_ctx* ctx, collecty_module* module) {
+       int r;
+
+       // Nothing to do if the module does not have an init method
+       if (!module->init)
+               return 0;
+
+       DEBUG(ctx, "Initializing %s...\n", module->name);
+
+       // Initialize the module
+       r = module->init(ctx);
+       if (r < 0) {
+               ERROR(ctx, "Failed to initialize %s: %s\n", module->name, strerror(-r));
+               return r;
+       }
+
+       return 0;
+}
+
+int collecty_module_shutdown(collecty_ctx* ctx, collecty_module* module) {
+       int r;
+
+       // Nothing to do if the module does not have an shutdown method
+       if (!module->shutdown)
+               return 0;
+
+       DEBUG(ctx, "Shutting down %s...\n", module->name);
+
+       // 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;
+       }
+
+       return 0;
+}
diff --git a/src/daemon/module.h b/src/daemon/module.h
new file mode 100644 (file)
index 0000000..e62f59f
--- /dev/null
@@ -0,0 +1,39 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef COLLECTY_MODULE_H
+#define COLLECTY_MODULE_H
+
+#include "ctx.h"
+
+typedef struct collecty_module {
+       const char* name;
+
+       // Init
+       int (*init)(collecty_ctx* ctx);
+
+       // Shutdown
+       int (*shutdown)(collecty_ctx* ctx);
+} collecty_module;
+
+int collecty_module_init(collecty_ctx* ctx, collecty_module* module);
+int collecty_module_shutdown(collecty_ctx* ctx, collecty_module* module);
+
+#endif /* COLLECTY_MODULE_H */
diff --git a/src/daemon/modules.c b/src/daemon/modules.c
new file mode 100644 (file)
index 0000000..8a4c1b0
--- /dev/null
@@ -0,0 +1,60 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stddef.h>
+
+#include "ctx.h"
+#include "module.h"
+#include "modules.h"
+
+// Register all modules
+static collecty_module* modules[] = {
+       NULL,
+};
+
+int collecty_modules_init(collecty_ctx* ctx) {
+       int r;
+
+       DEBUG(ctx, "Initializing all modules...\n");
+
+       // Initialize all modules
+       for (collecty_module** m = modules; *m; m++) {
+               r = collecty_module_init(ctx, *m);
+               if (r < 0)
+                       return r;
+       }
+
+       return 0;
+}
+
+int collecty_modules_shutdown(collecty_ctx* ctx) {
+       int r;
+
+       DEBUG(ctx, "Shutting down all modules...\n");
+
+       // Shutdown all modules
+       for (collecty_module** m = modules; *m; m++) {
+               r = collecty_module_shutdown(ctx, *m);
+               if (r < 0)
+                       return r;
+       }
+
+       return 0;
+}
diff --git a/src/daemon/modules.h b/src/daemon/modules.h
new file mode 100644 (file)
index 0000000..23f81ed
--- /dev/null
@@ -0,0 +1,29 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef COLLECTY_MODULES_H
+#define COLLECTY_MODULES_H
+
+#include "ctx.h"
+
+int collecty_modules_init(collecty_ctx* ctx);
+int collecty_modules_shutdown(collecty_ctx* ctx);
+
+#endif /* COLLECTY_MODULES_H */