From 22be2ae865d761c6e91bd979db28f6ac9c3796af Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 9 Dec 2016 13:03:43 +0100 Subject: [PATCH] core: use nmp_utils_sysctl_open_netdir() for platform master/slave options --- src/platform/nm-platform.c | 56 ++++++++++++++++++++-------------- src/platform/tests/test-link.c | 40 ++++++++++++------------ 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 7cc675d3a..015848d87 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1708,34 +1708,44 @@ nm_platform_link_tun_add (NMPlatform *self, /*****************************************************************************/ -static char * -link_option_path (NMPlatform *self, int master, const char *category, const char *option) +static gboolean +link_set_option (NMPlatform *self, int ifindex, const char *category, const char *option, const char *value) { - const char *name = nm_platform_link_get_name (self, master); + nm_auto_close int dirfd = -1; + char ifname_verified[IFNAMSIZ]; + const char *path; - if (!name || !category || !option) + if (!category || !option) + return FALSE; + + dirfd = nm_platform_sysctl_open_netdir (self, ifindex, ifname_verified); + if (dirfd < 0) + return FALSE; + + path = nm_sprintf_bufa (strlen (category) + strlen (option) + 2, + "%s/%s", + category, option); + return nm_platform_sysctl_set (self, NMP_SYSCTL_PATHID_NETDIR_unsafe (dirfd, ifname_verified, path), value); +} + +static char * +link_get_option (NMPlatform *self, int ifindex, const char *category, const char *option) +{ + nm_auto_close int dirfd = -1; + char ifname_verified[IFNAMSIZ]; + const char *path; + + if (!category || !option) return NULL; - return g_strdup_printf ("/sys/class/net/%s/%s/%s", - NM_ASSERT_VALID_PATH_COMPONENT (name), - NM_ASSERT_VALID_PATH_COMPONENT (category), - NM_ASSERT_VALID_PATH_COMPONENT (option)); -} + dirfd = nm_platform_sysctl_open_netdir (self, ifindex, ifname_verified); + if (dirfd < 0) + return NULL; -static gboolean -link_set_option (NMPlatform *self, int master, const char *category, const char *option, const char *value) -{ - gs_free char *path = link_option_path (self, master, category, option); - - return path && nm_platform_sysctl_set (self, NMP_SYSCTL_PATHID_ABSOLUTE (path), value); -} - -static char * -link_get_option (NMPlatform *self, int master, const char *category, const char *option) -{ - gs_free char *path = link_option_path (self, master, category, option); - - return path ? nm_platform_sysctl_get (self, NMP_SYSCTL_PATHID_ABSOLUTE (path)) : NULL; + path = nm_sprintf_bufa (strlen (category) + strlen (option) + 2, + "%s/%s", + category, option); + return nm_platform_sysctl_get (self, NMP_SYSCTL_PATHID_NETDIR_unsafe (dirfd, ifname_verified, path)); } static const char * diff --git a/src/platform/tests/test-link.c b/src/platform/tests/test-link.c index ebeea1b30..292f8b902 100644 --- a/src/platform/tests/test-link.c +++ b/src/platform/tests/test-link.c @@ -398,26 +398,28 @@ test_software (NMLinkType link_type, const char *link_typename) accept_signal (link_changed); /* Set master option */ - switch (link_type) { - case NM_LINK_TYPE_BRIDGE: - if (nmtstp_is_sysfs_writable ()) { - g_assert (nm_platform_sysctl_master_set_option (NM_PLATFORM_GET, ifindex, "forward_delay", "628")); - value = nm_platform_sysctl_master_get_option (NM_PLATFORM_GET, ifindex, "forward_delay"); - g_assert_cmpstr (value, ==, "628"); - g_free (value); + if (nmtstp_is_root_test ()) { + switch (link_type) { + case NM_LINK_TYPE_BRIDGE: + if (nmtstp_is_sysfs_writable ()) { + g_assert (nm_platform_sysctl_master_set_option (NM_PLATFORM_GET, ifindex, "forward_delay", "628")); + value = nm_platform_sysctl_master_get_option (NM_PLATFORM_GET, ifindex, "forward_delay"); + g_assert_cmpstr (value, ==, "628"); + g_free (value); + } + break; + case NM_LINK_TYPE_BOND: + if (nmtstp_is_sysfs_writable ()) { + g_assert (nm_platform_sysctl_master_set_option (NM_PLATFORM_GET, ifindex, "mode", "active-backup")); + value = nm_platform_sysctl_master_get_option (NM_PLATFORM_GET, ifindex, "mode"); + /* When reading back, the output looks slightly different. */ + g_assert (g_str_has_prefix (value, "active-backup")); + g_free (value); + } + break; + default: + break; } - break; - case NM_LINK_TYPE_BOND: - if (nmtstp_is_sysfs_writable ()) { - g_assert (nm_platform_sysctl_master_set_option (NM_PLATFORM_GET, ifindex, "mode", "active-backup")); - value = nm_platform_sysctl_master_get_option (NM_PLATFORM_GET, ifindex, "mode"); - /* When reading back, the output looks slightly different. */ - g_assert (g_str_has_prefix (value, "active-backup")); - g_free (value); - } - break; - default: - break; } /* Enslave and release */