core: use nmp_utils_sysctl_open_netdir() to detect link-type

This commit is contained in:
Thomas Haller
2016-12-09 10:43:06 +01:00
parent d3af925b91
commit 7fc3eace31
3 changed files with 28 additions and 53 deletions

View File

@@ -573,18 +573,14 @@ _lookup_cached_link (const NMPCache *cache, int ifindex, gboolean *completed_fro
#define DEVTYPE_PREFIX "DEVTYPE="
static char *
_linktype_read_devtype (const char *ifname)
_linktype_read_devtype (int dirfd)
{
char uevent[NM_STRLEN ("/sys/class/net/123456789012345/uevent\0") + 100 /*safety*/];
char *contents = NULL;
char *cont, *end;
nm_sprintf_buf (uevent,
"/sys/class/net/%s/uevent",
NM_ASSERT_VALID_PATH_COMPONENT (ifname));
nm_assert (strlen (uevent) < sizeof (uevent) - 1);
nm_assert (dirfd >= 0);
if (!g_file_get_contents (uevent, &contents, NULL, NULL))
if (nm_utils_file_get_contents (dirfd, "uevent", 1*1024*1024, &contents, NULL, NULL) < 0)
return NULL;
for (cont = contents; cont; cont = end) {
end = strpbrk (cont, "\r\n");
@@ -680,9 +676,10 @@ _linktype_get_type (NMPlatform *platform,
return NM_LINK_TYPE_IP6TNL;
if (ifname) {
char anycast_mask[NM_STRLEN ("/sys/class/net/123456789012345/anycast_mask\0") + 100 /*safety*/];
nm_auto_close int dirfd = -1;
gs_free char *driver = NULL;
gs_free char *devtype = NULL;
char ifname_verified[IFNAMSIZ];
/* Fallback OVS detection for kernel <= 3.16 */
if (nmp_utils_ethtool_get_driver_info (ifname, &driver, NULL, NULL)) {
@@ -698,15 +695,12 @@ _linktype_get_type (NMPlatform *platform,
}
}
nm_sprintf_buf (anycast_mask,
"/sys/class/net/%s/anycast_mask",
NM_ASSERT_VALID_PATH_COMPONENT (ifname));
nm_assert (strlen (anycast_mask) < sizeof (anycast_mask) - 1);
if (g_file_test (anycast_mask, G_FILE_TEST_EXISTS))
dirfd = nmp_utils_sysctl_open_netdir (ifindex, ifname, ifname_verified);
if (dirfd >= 0) {
if (faccessat (dirfd, "anycast_mask", F_OK, 0) == 0)
return NM_LINK_TYPE_OLPC_MESH;
devtype = _linktype_read_devtype (ifname);
devtype = _linktype_read_devtype (dirfd);
for (i = 0; devtype && i < G_N_ELEMENTS (linktypes); i++) {
if (g_strcmp0 (devtype, linktypes[i].devtype) == 0) {
if (linktypes[i].nm_type == NM_LINK_TYPE_BNEP) {
@@ -721,8 +715,9 @@ _linktype_get_type (NMPlatform *platform,
}
/* Fallback for drivers that don't call SET_NETDEV_DEVTYPE() */
if (wifi_utils_is_wifi (ifindex, ifname))
if (wifi_utils_is_wifi (dirfd, ifname_verified))
return NM_LINK_TYPE_WIFI;
}
if (arptype == ARPHRD_ETHER) {
/* Misc non-upstream WWAN drivers. rmnet is Qualcomm's proprietary

View File

@@ -183,39 +183,19 @@ wifi_utils_deinit (WifiData *data)
}
gboolean
wifi_utils_is_wifi (int ifindex, const char *ifname)
wifi_utils_is_wifi (int dirfd, const char *ifname)
{
int fd_sysnet;
int fd_phy80211;
char ifname_verified[IFNAMSIZ];
g_return_val_if_fail (dirfd >= 0, FALSE);
g_return_val_if_fail (ifindex > 0, FALSE);
fd_sysnet = nmp_utils_sysctl_open_netdir (ifindex, ifname, ifname_verified);
if (fd_sysnet < 0)
return FALSE;
/* there might have been a race and ifname might be wrong. Below for checking
* wext, use the possibly improved name that we just verified. */
ifname = ifname_verified;
fd_phy80211 = openat (fd_sysnet, "phy80211", O_CLOEXEC);
close (fd_sysnet);
if (fd_phy80211 >= 0) {
close (fd_phy80211);
if (faccessat (dirfd, "phy80211", F_OK, 0) == 0)
return TRUE;
}
#if HAVE_WEXT
if (wifi_wext_is_wifi (ifname))
return TRUE;
#endif
return FALSE;
}
/* OLPC Mesh-only functions */
guint32

View File

@@ -28,7 +28,7 @@
typedef struct WifiData WifiData;
gboolean wifi_utils_is_wifi (int ifindex, const char *ifname);
gboolean wifi_utils_is_wifi (int dirfd, const char *ifname);
WifiData *wifi_utils_init (const char *iface, int ifindex, gboolean check_scan);