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)
#include "ctx.h"
#include "daemon.h"
+#include "modules.h"
struct collecty_daemon {
collecty_ctx* ctx;
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;
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)
--- /dev/null
+/*#############################################################################
+# #
+# 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;
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 */
--- /dev/null
+/*#############################################################################
+# #
+# 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;
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 */