td_ctx* ctx;
int nrefs;
+ // Enabled sources
+ char** enabled_sources;
+
// Event Loop
sd_event* loop;
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;
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;
// 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)
#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);
#include "ctx.h"
#include "daemon.h"
#include "logging.h"
+#include "string.h"
const char* argp_program_version = PACKAGE_VERSION;
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;
td_ctx_set_log_level(ctx, LOG_DEBUG);
break;
+ case OPT_SOURCE:
+ return td_strings_append(&sources, arg);
+
default:
return ARGP_ERR_UNKNOWN;
}
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;
};
-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;
}
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;
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;
#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);
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "util.h"
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 */