From: Michael Tremer Date: Mon, 30 Mar 2026 15:00:28 +0000 (+0000) Subject: netlink: Start scaffolding to move all netlink stuff X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=79d75826b819ba4254f0b455fc321c948fa28376;p=telemetry.git netlink: Start scaffolding to move all netlink stuff Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 640d28c..cf35ce1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -170,6 +170,8 @@ dist_telemetryd_SOURCES = \ src/daemon/main.c \ src/daemon/metrics.c \ src/daemon/metrics.h \ + src/daemon/netlink.c \ + src/daemon/netlink.h \ src/daemon/parse.c \ src/daemon/parse.h \ src/daemon/priv.c \ diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 71100f7..95d7aae 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -43,6 +43,7 @@ #include "graphs.h" #include "graph-bus.h" #include "metrics.h" +#include "netlink.h" #include "queue.h" #include "sensors.h" #include "source.h" @@ -84,6 +85,9 @@ struct td_daemon { // udev struct udev* udev; + // Netlink + td_netlink* netlink; + // Netlink Sockets #ifdef HAVE_LIBNL3 struct { @@ -292,6 +296,19 @@ static int td_daemon_setup_udev(td_daemon* self) { return 0; } +static int td_daemon_setup_netlink(td_daemon* self) { + int r; + + // Create netlink + r = td_netlink_create(&self->netlink, self->ctx); + if (r < 0) { + ERROR(self->ctx, "Failed to setup netlink: %s\n", strerror(-r)); + return r; + } + + return 0; +} + static void td_daemon_free(td_daemon* self) { // Free Netlink Sockets #ifdef HAVE_LIBNL3 @@ -319,6 +336,8 @@ static void td_daemon_free(td_daemon* self) { sd_event_source_unref(self->events.init); if (self->events.exit) sd_event_source_unref(self->events.exit); + if (self->netlink) + td_netlink_unref(self->netlink); if (self->sources) td_sources_unref(self->sources); if (self->graphs) @@ -374,6 +393,11 @@ int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources) { if (r < 0) goto ERROR; + // Connect to netlink + r = td_daemon_setup_netlink(self); + if (r < 0) + goto ERROR; + // Return the pointer *daemon = self; return 0; @@ -457,6 +481,10 @@ int td_daemon_flush_source( return td_queue_flush_source(self->queue, source, object); } +td_netlink* td_daemon_get_netlink(td_daemon* self) { + return td_netlink_ref(self->netlink); +} + #ifdef HAVE_LIBNL3 static struct nl_sock* td_daemon_get_nl_socket(td_daemon* self, int protocol) { struct nl_sock* sock = NULL; diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 02daba4..5f48ad8 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -37,6 +37,7 @@ typedef struct td_daemon td_daemon; #include "ctx.h" #include "graphs.h" #include "metrics.h" +#include "netlink.h" #include "source.h" #include "sources.h" @@ -59,6 +60,8 @@ int td_daemon_flush_source( td_daemon* self, td_source* source, const char* object); #ifdef HAVE_LIBNL3 +td_netlink* td_daemon_get_netlink(td_daemon* self); + # ifdef HAVE_LIBNL3_GENL struct nl_sock* td_daemon_get_nl_80211_socket(td_daemon* self); int td_daemon_get_nl_80211_id(td_daemon* self); diff --git a/src/daemon/netlink.c b/src/daemon/netlink.c new file mode 100644 index 0000000..90a39c1 --- /dev/null +++ b/src/daemon/netlink.c @@ -0,0 +1,73 @@ +/*############################################################################# +# # +# telemetryd - The IPFire Telemetry Collection Service # +# Copyright (C) 2026 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 . # +# # +#############################################################################*/ + +#ifdef HAVE_LIBNL3 + +#include +#include + +#include "ctx.h" +#include "netlink.h" + +struct td_netlink { + td_ctx* ctx; + int nrefs; +}; + +static void td_netlink_free(td_netlink* self) { + if (self->ctx) + td_ctx_unref(self->ctx); + free(self); +} + +int td_netlink_create(td_netlink** netlink, td_ctx* ctx) { + td_netlink* self = NULL; + + // Allocate some memory + self = calloc(1, sizeof(*self)); + if (!self) + return -errno; + + // Initialize the reference counter + self->nrefs = 1; + + // Store a reference to the context + self->ctx = td_ctx_ref(ctx); + + // Return the pointer + *netlink = self; + + return 0; +} + +td_netlink* td_netlink_ref(td_netlink* self) { + ++self->nrefs; + return self; +} + +td_netlink* td_netlink_unref(td_netlink* self) { + if (--self->nrefs > 0) + return self; + + td_netlink_free(self); + return NULL; +} + +#endif /* HAVE_LIBNL3 */ diff --git a/src/daemon/netlink.h b/src/daemon/netlink.h new file mode 100644 index 0000000..a7ef944 --- /dev/null +++ b/src/daemon/netlink.h @@ -0,0 +1,37 @@ +/*############################################################################# +# # +# telemetryd - The IPFire Telemetry Collection Service # +# Copyright (C) 2026 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 TELEMETRY_NETLINK_H +#define TELEMETRY_NETLINK_H + +#ifdef HAVE_LIBNL3 + +#include "ctx.h" + +typedef struct td_netlink td_netlink; + +int td_netlink_create(td_netlink** netlink, td_ctx* ctx); + +td_netlink* td_netlink_ref(td_netlink* self); +td_netlink* td_netlink_unref(td_netlink* self); + +#endif /* HAVE_LIBNL3 */ + +#endif /* TELEMETRY_NETLINK_H */