]> git.ipfire.org Git - network.git/commitdiff
networkd: Connect to udev
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Feb 2023 11:05:23 +0000 (11:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Feb 2023 11:05:23 +0000 (11:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkd/daemon.c
src/networkd/devmon.c [new file with mode: 0644]
src/networkd/devmon.h [new file with mode: 0644]

index 3972d8dca9f38939af4a8ca6e682ef8333169262..2ebe622410422e1f1fc367ac0126898482c20b05 100644 (file)
@@ -316,6 +316,8 @@ dist_networkd_SOURCES = \
        src/networkd/daemon.h \
        src/networkd/daemon-bus.c \
        src/networkd/daemon-bus.h \
+       src/networkd/devmon.c \
+       src/networkd/devmon.h \
        src/networkd/logging.h \
        src/networkd/main.c \
        src/networkd/string.h \
index 5b5cbb44447acb7d71064e6957dfc486d8d5a05f..960fd96cb25f807e25f04d8b80ace527c58bda51 100644 (file)
 
 #include <systemd/sd-bus.h>
 #include <systemd/sd-daemon.h>
+#include <systemd/sd-device.h>
 #include <systemd/sd-event.h>
 
 #include "bus.h"
 #include "config.h"
 #include "daemon.h"
+#include "devmon.h"
 #include "logging.h"
 #include "zone.h"
 #include "zones.h"
 
+// Increase the receive buffer to 128 MiB
+#define RCVBUF_SIZE                                            128 * 1024 * 1024
+
 struct nw_daemon {
        int nrefs;
 
@@ -43,6 +48,9 @@ struct nw_daemon {
        // DBus Connection
        sd_bus* bus;
 
+       // udev Connection
+       sd_device_monitor* devmon;
+
        // Zones
        struct nw_zones* zones;
 };
@@ -126,6 +134,57 @@ static int nw_daemon_load_config(struct nw_daemon* daemon) {
        return r;
 }
 
+static int nw_start_device_monitor(struct nw_daemon* daemon) {
+       int r;
+
+       const char* subsystems[] = {
+               "net",
+               "ieee80211",
+               "rfkill",
+               NULL,
+       };
+
+       // Create a new connection to monitor any devices
+       r = sd_device_monitor_new(&daemon->devmon);
+       if (r < 0) {
+               ERROR("Could not inititalize the device monitor: %m\n");
+               return 1;
+       }
+
+       // Increase the receive buffer
+       r = sd_device_monitor_set_receive_buffer_size(daemon->devmon, RCVBUF_SIZE);
+       if (r < 0) {
+               ERROR("Could not increase buffer size for the device monitor: %m\n");
+               return 1;
+       }
+
+       // Filter for events for all relevant subsystems
+       for (const char** subsystem = subsystems; *subsystem; subsystem++) {
+               r = sd_device_monitor_filter_add_match_subsystem_devtype(
+                       daemon->devmon, *subsystem, NULL);
+               if (r < 1) {
+                       ERROR("Could not add device monitor for the %s subsystem: %m\n", *subsystem);
+                       return 1;
+               }
+       }
+
+       // Attach the device monitor to the event loop
+       r = sd_device_monitor_attach_event(daemon->devmon, daemon->loop);
+       if (r < 0) {
+               ERROR("Could not attach the device monitor to the event loop: %m\n");
+               return 1;
+       }
+
+       // Start processing events...
+       r = sd_device_monitor_start(daemon->devmon, nw_devmon_handle_uevent, daemon);
+       if (r < 0) {
+               ERROR("Could not start the device monitor: %m\n");
+               return 1;
+       }
+
+       return 0;
+}
+
 static int nw_daemon_setup(struct nw_daemon* daemon) {
        int r;
 
@@ -144,6 +203,11 @@ static int nw_daemon_setup(struct nw_daemon* daemon) {
        if (r)
                return r;
 
+       // Connect to udev
+       r = nw_start_device_monitor(daemon);
+       if (r)
+               return r;
+
        return 0;
 }
 
diff --git a/src/networkd/devmon.c b/src/networkd/devmon.c
new file mode 100644 (file)
index 0000000..0a8c26d
--- /dev/null
@@ -0,0 +1,27 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network 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/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <systemd/sd-device.h>
+
+#include "devmon.h"
+
+int nw_devmon_handle_uevent(sd_device_monitor* monitor, sd_device* device, void* data) {
+       return 0;
+}
diff --git a/src/networkd/devmon.h b/src/networkd/devmon.h
new file mode 100644 (file)
index 0000000..0d8970a
--- /dev/null
@@ -0,0 +1,28 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network 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 NETWORKD_DEVMON_H
+#define NETWORKD_DEVMON_H
+
+#include <systemd/sd-device.h>
+
+int nw_devmon_handle_uevent(sd_device_monitor* monitor, sd_device* device, void* data);
+
+#endif /* NETWORKD_DEVMON_H */