]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: assign corresponding sd-device object to Wiphy object
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Jul 2022 23:48:55 +0000 (08:48 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 23 Jul 2022 11:55:02 +0000 (20:55 +0900)
As an analogy for Link object, let's each Wiphy object has the
corresponding sd-device object. The sd-device object will be used in
later commits.

src/network/networkd-manager.c
src/network/networkd-wiphy.c
src/network/networkd-wiphy.h

index 57e972d0dee338dfbe43c10a652e9494cc21bbba..302ba729bd245e529be4b7f340bd9aeb88b355dd 100644 (file)
@@ -188,6 +188,8 @@ static int manager_process_uevent(sd_device_monitor *monitor, sd_device *device,
 
         if (streq(s, "net"))
                 r = manager_udev_process_link(m, device, action);
+        else if (streq(s, "ieee80211"))
+                r = manager_udev_process_wiphy(m, device, action);
         else {
                 log_device_debug(device, "Received device with unexpected subsystem \"%s\", ignoring.", s);
                 return 0;
@@ -219,6 +221,10 @@ static int manager_connect_udev(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Could not add device monitor filter for net subsystem: %m");
 
+        r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "ieee80211", NULL);
+        if (r < 0)
+                return log_error_errno(r, "Could not add device monitor filter for ieee80211 subsystem: %m");
+
         r = sd_device_monitor_attach_event(m->device_monitor, m->event);
         if (r < 0)
                 return log_error_errno(r, "Failed to attach event to device monitor: %m");
index 41ce071edcd8b452ee3c9c7f7b1df0a6e03cf5aa..113b67147924f0508abe8e0320865f95d3379859 100644 (file)
@@ -2,10 +2,12 @@
 
 #include <linux/nl80211.h>
 
+#include "device-private.h"
 #include "device-util.h"
 #include "networkd-manager.h"
 #include "networkd-wiphy.h"
 #include "parse-util.h"
+#include "udev-util.h"
 #include "wifi-util.h"
 
 Wiphy *wiphy_free(Wiphy *w) {
@@ -18,6 +20,8 @@ Wiphy *wiphy_free(Wiphy *w) {
                         hashmap_remove_value(w->manager->wiphy_by_name, w->name, w);
         }
 
+        sd_device_unref(w->dev);
+
         free(w->name);
         return mfree(w);
 }
@@ -129,6 +133,48 @@ static int wiphy_update_name(Wiphy *w, sd_netlink_message *message) {
         return 1; /* updated */
 }
 
+static int wiphy_update_device(Wiphy *w) {
+        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+        int r;
+
+        assert(w);
+        assert(w->name);
+
+        if (!udev_available())
+                return 0;
+
+        w->dev = sd_device_unref(w->dev);
+
+        r = sd_device_new_from_subsystem_sysname(&dev, "ieee80211", w->name);
+        if (r < 0) {
+                /* The corresponding syspath may not exist yet, and may appear later. */
+                log_wiphy_debug_errno(w, r, "Failed to get wiphy device, ignoring: %m");
+                return 0;
+        }
+
+        if (DEBUG_LOGGING) {
+                const char *s = NULL;
+
+                (void) sd_device_get_syspath(dev, &s);
+                log_wiphy_debug(w, "Found device: %s", strna(s));
+        }
+
+        w->dev = TAKE_PTR(dev);
+        return 0;
+}
+
+static int wiphy_update(Wiphy *w) {
+        int r;
+
+        assert(w);
+
+        r = wiphy_update_device(w);
+        if (r < 0)
+                return log_wiphy_debug_errno(w, r, "Failed to update wiphy device: %m");
+
+        return 0;
+}
+
 int manager_genl_process_nl80211_wiphy(sd_netlink *genl, sd_netlink_message *message, Manager *manager) {
         const char *family;
         uint32_t index;
@@ -192,6 +238,10 @@ int manager_genl_process_nl80211_wiphy(sd_netlink *genl, sd_netlink_message *mes
                                 return 0;
                 }
 
+                r = wiphy_update(w);
+                if (r < 0)
+                        log_wiphy_warning_errno(w, r, "Failed to update wiphy, ignoring: %m");
+
                 break;
         }
         case NL80211_CMD_DEL_WIPHY:
@@ -212,3 +262,25 @@ int manager_genl_process_nl80211_wiphy(sd_netlink *genl, sd_netlink_message *mes
 
         return 0;
 }
+
+int manager_udev_process_wiphy(Manager *m, sd_device *device, sd_device_action_t action) {
+        const char *name;
+        Wiphy *w;
+        int r;
+
+        assert(m);
+        assert(device);
+
+        r = sd_device_get_sysname(device, &name);
+        if (r < 0)
+                return log_device_debug_errno(device, r, "Failed to get sysname: %m");
+
+        r = wiphy_get_by_name(m, name, &w);
+        if (r < 0) {
+                /* This error is not critical, as the corresponding genl message may be received later. */
+                log_device_debug_errno(device, r, "Failed to get Wiphy object, ignoring: %m");
+                return 0;
+        }
+
+        return device_unref_and_replace(w->dev, action == SD_DEVICE_REMOVE ? NULL : device);
+}
index b40354704be3c52dd3f7f8690dad3c2c017c1f8c..9e8a3a5560bdc9bbf1a004d77b3a687c30c05135 100644 (file)
@@ -14,6 +14,8 @@ typedef struct Wiphy {
 
         uint32_t index;
         char *name;
+
+        sd_device *dev;
 } Wiphy;
 
 Wiphy *wiphy_free(Wiphy *w);
@@ -23,6 +25,7 @@ int wiphy_get_by_index(Manager *manager, uint32_t index, Wiphy **ret);
 int wiphy_get_by_name(Manager *manager, const char *name, Wiphy **ret);
 
 int manager_genl_process_nl80211_wiphy(sd_netlink *genl, sd_netlink_message *message, Manager *manager);
+int manager_udev_process_wiphy(Manager *m, sd_device *device, sd_device_action_t action);
 
 #define log_wiphy_full_errno_zerook(w, level, error, ...)               \
         ({                                                              \