From d839e1c817193d821c4ac70e34b9255aa29016bd Mon Sep 17 00:00:00 2001 From: Thomas Graf Date: Wed, 9 Nov 2011 11:22:39 +0100 Subject: [PATCH] core: convert nm_system_get_link_type() to return an int Kills the strdup() and avoids having the caller free the memory. Also renames the function to nm_system_get_iface_type() since "link" is not a common term in NM. Signed-off-by: Thomas Graf --- src/nm-system.c | 32 +++++++++++++++++++------------- src/nm-system.h | 9 ++++++++- src/nm-udev-manager.c | 16 ++++++++-------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/nm-system.c b/src/nm-system.c index 709a08b7aa..3317eb49bc 100644 --- a/src/nm-system.c +++ b/src/nm-system.c @@ -1423,33 +1423,39 @@ out: } /** - * nm_system_get_link_type: - * @name: name of link + * nm_system_get_iface_type: + * @name: name of interface * - * Lookup virtual link type. The returned string is allocated and needs - * to be freed after usage. + * Lookup the type of an interface * - * Returns: Name of virtual link type or NULL if not a virtual link. + * Returns: Interface type (NM_IFACE_TYPE_*) or NM_IFACE_TYPE_UNSPEC. **/ -char * -nm_system_get_link_type (const char *name) +int +nm_system_get_iface_type (const char *name) { struct rtnl_link *result; struct nl_sock *nlh; char *type; + int res = NM_IFACE_TYPE_UNSPEC; nlh = nm_netlink_get_default_handle (); if (!nlh) - return NULL; + goto out; if (rtnl_link_get_kernel (nlh, 0, name, &result) < 0) - return NULL; + goto out; - if ((type = rtnl_link_get_type (result))) - type = g_strdup (type); + type = rtnl_link_get_type (result); + + if (!g_strcmp0 (type, "bond")) + res = NM_IFACE_TYPE_BOND; + else if (!g_strcmp0 (type, "vlan")) + res = NM_IFACE_TYPE_VLAN; + else if (!g_strcmp0 (type, "dummy")) + res = NM_IFACE_TYPE_DUMMY; rtnl_link_put (result); - - return type; +out: + return res; } diff --git a/src/nm-system.h b/src/nm-system.h index 339dfa7431..f151e90511 100644 --- a/src/nm-system.h +++ b/src/nm-system.h @@ -95,6 +95,13 @@ gboolean nm_system_add_bonding_master (NMSettingBond *setting); gboolean nm_system_iface_enslave (NMDevice *slave, NMDevice *master); gboolean nm_system_iface_release (NMDevice *slave, NMDevice *master); -char * nm_system_get_link_type (const char *name); +enum { + NM_IFACE_TYPE_UNSPEC = 0, + NM_IFACE_TYPE_BOND, + NM_IFACE_TYPE_VLAN, + NM_IFACE_TYPE_DUMMY, +}; + +int nm_system_get_iface_type (const char *name); #endif diff --git a/src/nm-udev-manager.c b/src/nm-udev-manager.c index 9ffb5f0080..8a519c0fb6 100644 --- a/src/nm-udev-manager.c +++ b/src/nm-udev-manager.c @@ -423,15 +423,15 @@ device_creator (NMUdevManager *manager, } if (!driver) { - char *type; + switch (nm_system_get_iface_type (ifname)) { + case NM_IFACE_TYPE_BOND: + driver = "bonding"; + break; - type = nm_system_get_link_type (ifname); - if (type) { - if (g_strcmp0 (type, "bond") == 0) - driver = "bonding"; - g_free (type); - } else if (g_str_has_prefix (ifname, "easytether")) { - driver = "easytether"; + default: + if (g_str_has_prefix (ifname, "easytether")) + driver = "easytether"; + break; } if (!driver) {