From: Michael Tremer Date: Sun, 5 Feb 2023 11:05:23 +0000 (+0000) Subject: networkd: Connect to udev X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b286cd831946a3134f5355fe45901a8a4057dce1;p=network.git networkd: Connect to udev Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 3972d8dc..2ebe6224 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index 5b5cbb44..960fd96c 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -23,15 +23,20 @@ #include #include +#include #include #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 index 00000000..0a8c26d4 --- /dev/null +++ b/src/networkd/devmon.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#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 index 00000000..0d8970aa --- /dev/null +++ b/src/networkd/devmon.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef NETWORKD_DEVMON_H +#define NETWORKD_DEVMON_H + +#include + +int nw_devmon_handle_uevent(sd_device_monitor* monitor, sd_device* device, void* data); + +#endif /* NETWORKD_DEVMON_H */