From 175865c8d310098c5de87c7c439bf36041256f87 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 31 Oct 2023 12:07:20 +0100 Subject: [PATCH] core: refactor nm_platform_link_get_unmanaged() to return ternary value It seems easier to understand this way and to implement. Next, another udev property will be honored. In light of that, the change makes more sense. --- src/core/devices/nm-device.c | 17 ++++++++++------- src/libnm-platform/nm-platform.c | 27 +++++++++++++++------------ src/libnm-platform/nm-platform.h | 6 +++--- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index b8c2f88cc7..74a967e123 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -15097,20 +15097,23 @@ nm_device_set_unmanaged_by_user_settings(NMDevice *self, gboolean now) void nm_device_set_unmanaged_by_user_udev(NMDevice *self) { - int ifindex; - gboolean platform_unmanaged = FALSE; + NMOptionBool platform_unmanaged; + int ifindex; ifindex = self->_priv->ifindex; - if (ifindex <= 0 - || !nm_platform_link_get_unmanaged(nm_device_get_platform(self), - ifindex, - &platform_unmanaged)) + if (ifindex <= 0) + return; + + platform_unmanaged = nm_platform_link_get_unmanaged(nm_device_get_platform(self), ifindex); + if (platform_unmanaged == NM_OPTION_BOOL_DEFAULT) return; nm_device_set_unmanaged_by_flags(self, NM_UNMANAGED_USER_UDEV, - platform_unmanaged, + platform_unmanaged == NM_OPTION_BOOL_TRUE + ? NM_UNMAN_FLAG_OP_SET_UNMANAGED + : NM_UNMAN_FLAG_OP_SET_MANAGED, NM_DEVICE_STATE_REASON_USER_REQUESTED); } diff --git a/src/libnm-platform/nm-platform.c b/src/libnm-platform/nm-platform.c index 1b513d34be..e55c749931 100644 --- a/src/libnm-platform/nm-platform.c +++ b/src/libnm-platform/nm-platform.c @@ -1636,7 +1636,7 @@ nm_platform_link_get_udev_property(NMPlatform *self, const char *name, const char **out_value) { - struct udev_device *udevice = NULL; + struct udev_device *udevice; const char *uproperty; udevice = nm_platform_link_get_udev_device(self, ifindex); @@ -1655,22 +1655,25 @@ nm_platform_link_get_udev_property(NMPlatform *self, * nm_platform_link_get_unmanaged: * @self: platform instance * @ifindex: interface index - * @unmanaged: management status (in case %TRUE is returned) * - * Returns: %TRUE if platform overrides NM default-unmanaged status, - * %FALSE otherwise (with @unmanaged unmodified). + * Returns: %NM_OPTION_BOOL_DEFAULT if the udev property NM_UNMANAGED + * is not set. Otherwise, return NM_UNMANAGED as boolean. */ -gboolean -nm_platform_link_get_unmanaged(NMPlatform *self, int ifindex, gboolean *unmanaged) +NMOptionBool +nm_platform_link_get_unmanaged(NMPlatform *self, int ifindex) { - const char *value; + struct udev_device *udevice; + const char *val; - if (nm_platform_link_get_udev_property(self, ifindex, "NM_UNMANAGED", &value)) { - NM_SET_OUT(unmanaged, _nm_utils_ascii_str_to_bool(value, FALSE)); - return TRUE; - } + udevice = nm_platform_link_get_udev_device(self, ifindex); + if (!udevice) + return NM_OPTION_BOOL_DEFAULT; - return FALSE; + val = udev_device_get_property_value(udevice, "NM_UNMANAGED"); + if (val) + return _nm_utils_ascii_str_to_bool(val, FALSE); + + return NM_OPTION_BOOL_DEFAULT; } /** diff --git a/src/libnm-platform/nm-platform.h b/src/libnm-platform/nm-platform.h index bc5bc5e8b4..81d2b9d6fe 100644 --- a/src/libnm-platform/nm-platform.h +++ b/src/libnm-platform/nm-platform.h @@ -1941,9 +1941,9 @@ int nm_platform_link_get_master(NMPlatform *self, int slave); gboolean nm_platform_link_can_assume(NMPlatform *self, int ifindex); -gboolean nm_platform_link_get_unmanaged(NMPlatform *self, int ifindex, gboolean *unmanaged); -gboolean nm_platform_link_supports_slaves(NMPlatform *self, int ifindex); -const char *nm_platform_link_get_type_name(NMPlatform *self, int ifindex); +NMOptionBool nm_platform_link_get_unmanaged(NMPlatform *self, int ifindex); +gboolean nm_platform_link_supports_slaves(NMPlatform *self, int ifindex); +const char *nm_platform_link_get_type_name(NMPlatform *self, int ifindex); gboolean nm_platform_link_refresh(NMPlatform *self, int ifindex); void nm_platform_process_events(NMPlatform *self);