From: Michael Tremer Date: Sat, 27 Sep 2025 12:42:45 +0000 (+0000) Subject: daemon: Create some scaffolding for modules X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32d856f6c2839066cf71ac02d4e210d456db318b;p=telemetry.git daemon: Create some scaffolding for modules Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 32c9f72..724ae09 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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) diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 213b846..06faf1b 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -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 index 0000000..598ff21 --- /dev/null +++ b/src/daemon/module.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#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 index 0000000..e62f59f --- /dev/null +++ b/src/daemon/module.h @@ -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 . # +# # +#############################################################################*/ + +#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 index 0000000..8a4c1b0 --- /dev/null +++ b/src/daemon/modules.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#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 index 0000000..23f81ed --- /dev/null +++ b/src/daemon/modules.h @@ -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 . # +# # +#############################################################################*/ + +#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 */