From ea521b6b9c42132a6476bcdca14dffe06543b73b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 30 Sep 2025 15:51:12 +0000 Subject: [PATCH] graphs: Export them over dbus Signed-off-by: Michael Tremer --- src/daemon/daemon.c | 7 +++++ src/daemon/daemon.h | 2 ++ src/daemon/graph-bus.c | 60 +++++++++++++++++++++++++++++++++--------- src/daemon/graph.c | 22 ++++++++++++++++ src/daemon/graph.h | 3 +++ src/daemon/graphs.c | 51 +++++++++++++++++++++++++++++++++++ src/daemon/graphs.h | 4 +++ 7 files changed, 137 insertions(+), 12 deletions(-) diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 92715b0..436db95 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -250,6 +250,13 @@ sd_event* collecty_daemon_loop(collecty_daemon* self) { return sd_event_ref(self->loop); } +collecty_graphs* collecty_daemon_get_graphs(collecty_daemon* self) { + if (self->graphs) + return collecty_graphs_ref(self->graphs); + + return NULL; +} + int collecty_daemon_run(collecty_daemon* self) { int r; diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 5d965e7..ffb4217 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -27,6 +27,7 @@ typedef struct collecty_daemon collecty_daemon; #include "bus.h" #include "ctx.h" +#include "graphs.h" #include "module.h" int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx); @@ -35,6 +36,7 @@ collecty_daemon* collecty_daemon_ref(collecty_daemon* daemon); collecty_daemon* collecty_daemon_unref(collecty_daemon* daemon); sd_event* collecty_daemon_loop(collecty_daemon* self); +collecty_graphs* collecty_daemon_get_graphs(collecty_daemon* self); int collecty_daemon_run(collecty_daemon* self); diff --git a/src/daemon/graph-bus.c b/src/daemon/graph-bus.c index 8e5139c..f2820ec 100644 --- a/src/daemon/graph-bus.c +++ b/src/daemon/graph-bus.c @@ -25,36 +25,72 @@ static int collecty_graph_node_enumerator(sd_bus* bus, const char* path, void* data, char*** nodes, sd_bus_error* error) { - //collecty_daemon* daemon = data; + collecty_daemon* daemon = data; + collecty_graphs* graphs = NULL; + int r = 1; + + // Fetch all graphs + graphs = collecty_daemon_get_graphs(daemon); + if (!graphs) + goto ERROR; + + // Fetch the bus paths + *nodes = collecty_graphs_get_bus_paths(graphs); + if (*nodes) + goto ERROR; + + // Success + r = 0; - // XXX TODO +ERROR: + if (graphs) + collecty_graphs_unref(graphs); - return 0; + return r; } static int collecty_graph_object_find(sd_bus* bus, const char* path, const char* interface, void* data, void** found, sd_bus_error* error) { collecty_daemon* daemon = data; + collecty_graphs* graphs = NULL; collecty_graph* graph = NULL; char* name = NULL; int r; // Decode the path of the requested object r = sd_bus_path_decode(path, "/org/ipfire/collecty1/graph", &name); - if (r <= 0) - return 0; + if (r <= 0) { + r = 0; + goto ERROR; + } + + // Fetch all graphs + graphs = collecty_daemon_get_graphs(daemon); + if (!graphs) { + r = 0; + goto ERROR; + } -#if 0 // Find the graph - graph = collecty_daemon_get_graph_by_name(daemon, name); - if (!graph) - return 0; -#endif + graph = collecty_graphs_get_by_name(graphs, name); + if (!graph) { + r = 0; + goto ERROR; + } // Match! - *found = collecty_graph_unref(graph); + *found = graph; + + // Success + r = 1; + +ERROR: + if (graphs) + collecty_graphs_unref(graphs); + if (graph) + collecty_graph_unref(graph); - return 1; + return r; } static const sd_bus_vtable collecty_graph_vtable[] = { diff --git a/src/daemon/graph.c b/src/daemon/graph.c index 8a2065c..d03f8f1 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -82,3 +82,25 @@ collecty_graph* collecty_graph_unref(collecty_graph* self) { collecty_graph_free(self); return NULL; } + +const char* collecty_graph_get_name(collecty_graph* self) { + return self->impl->name; +} + +char* collecty_graph_get_bus_path(collecty_graph* self) { + const char* name = NULL; + char* path = NULL; + int r; + + // Fetch the name + name = collecty_graph_get_name(self); + if (!name) + return NULL; + + // Format the bus path + r = sd_bus_path_encode("/org/ipfire/collecty1/graph", name, &path); + if (r < 0) + return NULL; + + return path; +} diff --git a/src/daemon/graph.h b/src/daemon/graph.h index f0828d8..8330c1e 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -39,4 +39,7 @@ int collecty_graph_create(collecty_graph** graph, collecty_graph* collecty_graph_ref(collecty_graph* self); collecty_graph* collecty_graph_unref(collecty_graph* self); +const char* collecty_graph_get_name(collecty_graph* self); +char* collecty_graph_get_bus_path(collecty_graph* self); + #endif /* COLLECTY_GRAPH_H */ diff --git a/src/daemon/graphs.c b/src/daemon/graphs.c index 5f96e95..ebd9c61 100644 --- a/src/daemon/graphs.c +++ b/src/daemon/graphs.c @@ -145,3 +145,54 @@ collecty_graphs* collecty_graphs_unref(collecty_graphs* self) { collecty_graphs_free(self); return NULL; } + +collecty_graph* collecty_graphs_get_by_name(collecty_graphs* self, const char* name) { + const char* n = NULL; + + // Iterate over all graphs to find a match + for (unsigned int i = 0; i < self->num_graphs; i++) { + // Fetch the name + n = collecty_graph_get_name(self->graphs[i]); + if (!n) + continue; + + // Return the object if the name matches + if (strcmp(name, n) == 0) + return collecty_graph_ref(self->graphs[i]); + } + + return NULL; +} + +char** collecty_graphs_get_bus_paths(collecty_graphs* self) { + char** paths = NULL; + char* path = NULL; + + // Allocate an array to store all the strings + paths = calloc(self->num_graphs + 1, sizeof(*path)); + if (!paths) + goto ERROR; + + // Add all paths for all graphs + for (unsigned int i = 0; i < self->num_graphs; i++) { + // Fetch the bus path + path = collecty_graph_get_bus_path(self->graphs[i]); + if (!path) + continue; + + // Assign to the output array + paths[i] = path; + } + + return paths; + +ERROR: + if (paths) { + for (unsigned int i = 0; i < self->num_graphs; i++) + if (paths[i]) + free(paths[i]); + free(paths); + } + + return NULL; +} diff --git a/src/daemon/graphs.h b/src/daemon/graphs.h index 92c2f0c..aa07b65 100644 --- a/src/daemon/graphs.h +++ b/src/daemon/graphs.h @@ -25,10 +25,14 @@ typedef struct collecty_graphs collecty_graphs; #include "ctx.h" #include "daemon.h" +#include "graph.h" int collecty_graphs_create(collecty_graphs** graphs, collecty_ctx* ctx, collecty_daemon* daemon); collecty_graphs* collecty_graphs_ref(collecty_graphs* self); collecty_graphs* collecty_graphs_unref(collecty_graphs* self); +collecty_graph* collecty_graphs_get_by_name(collecty_graphs* self, const char* name); +char** collecty_graphs_get_bus_paths(collecty_graphs* self); + #endif /* COLLECTY_GRAPHS_H */ -- 2.47.3