network: assign corresponding sd-device object to Wiphy object

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.
This commit is contained in:
Yu Watanabe
2022-07-23 20:55:02 +09:00
parent fc85b32302
commit c49d536295
3 changed files with 81 additions and 0 deletions
+6
View 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");
+72
View 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);
}
+3
View 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, ...) \
({ \