]> git.ipfire.org Git - telemetry.git/commitdiff
netlink: Start scaffolding to move all netlink stuff
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Mar 2026 15:00:28 +0000 (15:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Mar 2026 15:00:28 +0000 (15:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/daemon.c
src/daemon/daemon.h
src/daemon/netlink.c [new file with mode: 0644]
src/daemon/netlink.h [new file with mode: 0644]

index 640d28c939d2942bd25ff388b5a414eaf8127aae..cf35ce174bebe28d7a9b98634ce9a69ee3ccc68e 100644 (file)
@@ -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 \
index 71100f72b1e3133620aec28c3b589575107d2e22..95d7aae9b0c86c954da6cc96771464e6c5f5ad44 100644 (file)
@@ -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;
index 02daba4c6a8481667bd37ba96ac9b8e3fcbc5186..5f48ad848d998dc350531748f081e91fb31a5909 100644 (file)
@@ -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 (file)
index 0000000..90a39c1
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifdef HAVE_LIBNL3
+
+#include <errno.h>
+#include <stdlib.h>
+
+#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 (file)
index 0000000..a7ef944
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */