From: Michael Tremer Date: Mon, 20 Oct 2025 10:15:29 +0000 (+0000) Subject: daemon: Allow to select sources on the CLI X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f77ddf0a90e4ec202717d6b827fa247dc654d4b;p=telemetry.git daemon: Allow to select sources on the CLI This is only useful for testing to basically disable all other sources for better log readability. Signed-off-by: Michael Tremer --- diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 4e0ba95..48178ec 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -38,6 +38,9 @@ struct td_daemon { td_ctx* ctx; int nrefs; + // Enabled sources + char** enabled_sources; + // Event Loop sd_event* loop; @@ -71,7 +74,7 @@ static int td_daemon_init(sd_event_source* source, void* data) { DEBUG(self->ctx, "Initializing daemon...\n"); // Initialize all sources - r = td_sources_create(&self->sources, self->ctx, self); + r = td_sources_create(&self->sources, self->ctx, self, self->enabled_sources); if (r < 0) return r; @@ -219,7 +222,7 @@ static void td_daemon_free(td_daemon* self) { free(self); } -int td_daemon_create(td_daemon** daemon, td_ctx* ctx) { +int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources) { td_daemon* self = NULL; int r; @@ -234,6 +237,9 @@ int td_daemon_create(td_daemon** daemon, td_ctx* ctx) { // Store a reference to the context self->ctx = td_ctx_ref(ctx); + // Store the enabled sources + self->enabled_sources = enabled_sources; + // Setup the event loop r = td_daemon_setup_loop(self); if (r < 0) diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 503c580..d02bb24 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -31,7 +31,7 @@ typedef struct td_daemon td_daemon; #include "source.h" #include "sources.h" -int td_daemon_create(td_daemon** daemon, td_ctx* ctx); +int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources); td_daemon* td_daemon_ref(td_daemon* daemon); td_daemon* td_daemon_unref(td_daemon* daemon); diff --git a/src/daemon/main.c b/src/daemon/main.c index fce7fc6..cdd3a83 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -27,6 +27,7 @@ #include "ctx.h" #include "daemon.h" #include "logging.h" +#include "string.h" const char* argp_program_version = PACKAGE_VERSION; @@ -34,13 +35,18 @@ static const char* doc = "The IPFire Telemetry Daemon"; enum { OPT_DEBUG = 1, + OPT_SOURCE = 2, }; static struct argp_option options[] = { { "debug", OPT_DEBUG, NULL, 0, "Run in debug mode", 0 }, + { "source", OPT_SOURCE, "SOURCE", 0, "Only enable this source (for testing)", 0 }, { NULL }, }; +// Collect all sources that should be enabled +static char** sources = NULL; + static error_t parse(int key, char* arg, struct argp_state* state) { td_ctx* ctx = state->input; @@ -53,6 +59,9 @@ static error_t parse(int key, char* arg, struct argp_state* state) { td_ctx_set_log_level(ctx, LOG_DEBUG); break; + case OPT_SOURCE: + return td_strings_append(&sources, arg); + default: return ARGP_ERR_UNKNOWN; } @@ -82,7 +91,7 @@ int main(int argc, char* argv[]) { goto ERROR; // Create a daemon - r = td_daemon_create(&daemon, ctx); + r = td_daemon_create(&daemon, ctx, sources); if (r < 0) { ERROR(ctx, "Failed to initialize the daemon: %s\n", strerror(-r)); goto ERROR; diff --git a/src/daemon/sources.c b/src/daemon/sources.c index c3e08c4..d237836 100644 --- a/src/daemon/sources.c +++ b/src/daemon/sources.c @@ -89,13 +89,28 @@ struct td_sources { }; -static int td_sources_init(td_sources* self) { +static int td_sources_init(td_sources* self, char** enabled_sources) { td_source** sources = NULL; td_source* source = NULL; int r; + // Dump enabled sources + if (enabled_sources) { + DEBUG(self->ctx, "Will only enable the following sources:\n"); + + for (char** enabled_source = enabled_sources; *enabled_source; enabled_source++) + DEBUG(self->ctx, " %s\n", *enabled_source); + } + // Initialize all sources for (const td_source_impl** impl = source_impls; *impl; impl++) { + const char* name = (*impl)->name; + + // Skip source if not enabled + if (enabled_sources && !td_strings_contain(enabled_sources, name)) + continue; + + // Create the source object r = td_source_create(&source, self->ctx, self->daemon, *impl); if (r < 0) return r; @@ -141,7 +156,7 @@ static void td_sources_free(td_sources* self) { } int td_sources_create(td_sources** sources, - td_ctx* ctx, td_daemon* daemon) { + td_ctx* ctx, td_daemon* daemon, char** enabled_sources) { td_sources* self = NULL; int r; @@ -160,7 +175,7 @@ int td_sources_create(td_sources** sources, self->daemon = td_daemon_ref(daemon); // Setup all sources - r = td_sources_init(self); + r = td_sources_init(self, enabled_sources); if (r < 0) goto ERROR; diff --git a/src/daemon/sources.h b/src/daemon/sources.h index 98c0c52..34cd910 100644 --- a/src/daemon/sources.h +++ b/src/daemon/sources.h @@ -28,7 +28,7 @@ typedef struct td_sources td_sources; #include "source.h" int td_sources_create(td_sources** sources, - td_ctx* ctx, td_daemon* daemon); + td_ctx* ctx, td_daemon* daemon, char** enabled_sources); td_sources* td_sources_ref(td_sources* self); td_sources* td_sources_unref(td_sources* self); diff --git a/src/daemon/string.h b/src/daemon/string.h index e0faac3..3caf5de 100644 --- a/src/daemon/string.h +++ b/src/daemon/string.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "util.h" @@ -163,4 +164,99 @@ static inline void td_string_strip(char* s) { td_string_rstrip(s); } +static inline void td_strings_free(char** array) { + for (char** s = array; *s; s++) + free(*s); + + free(array); +} + +static inline size_t td_strings_length(char** array) { + size_t length = 0; + + // If we have received NULL, we will return zero + if (!array) + return 0; + + // Count all elements + for (char** s = array; *s; s++) + length++; + + return length; +} + +static inline int td_strings_append(char*** array, const char* s) { + if (!array) + return -EINVAL; + + // Copy the string + char* p = strdup(s); + if (!p) + goto ERROR; + + // Fetch the length + size_t length = td_strings_length(*array); + + // Resize the array + *array = reallocarray(*array, length + 2, sizeof(**array)); + if (!*array) + goto ERROR; + + // Store the string + (*array)[length++] = p; + + // Terminate the array + (*array)[length] = NULL; + + // Return the total length of the array + return 0; + +ERROR: + if (p) + free(p); + + return -errno; +} + +/* + This function dumps the string array to the console. This is useful for debugging only. +*/ +static inline int td_strings_dump(char** array) { + int r; + + // Check inputs + if (!array) + return -EINVAL; + + // Determine the length of the array + size_t length = td_strings_length(array); + + // If the array is empty, we return a message + if (!length) { + printf("Empty string array\n"); + return 0; + } + + // Dump the array + for (unsigned int i = 0; i < length; i++) { + r = printf("array[%u] : %s\n", i, array[i]); + if (r < 0) + return r; + } + + return 0; +} + +static inline int td_strings_contain(char** array, const char* string) { + int r; + + for (char** s = array; *s; s++) { + r = td_string_equals(*s, string); + if (r) + return r; + } + + return 0; +} + #endif /* TELEMETRY_STRING_H */