diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 497f34e875..103cdfa2e3 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -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,31 +695,29 @@ _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); + 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; - if (g_file_test (anycast_mask, G_FILE_TEST_EXISTS)) - return NM_LINK_TYPE_OLPC_MESH; - - devtype = _linktype_read_devtype (ifname); - 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) { - /* Both BNEP and 6lowpan use DEVTYPE=bluetooth, so we must - * use arptype to distinguish between them. - */ - if (arptype != ARPHRD_ETHER) - continue; + 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) { + /* Both BNEP and 6lowpan use DEVTYPE=bluetooth, so we must + * use arptype to distinguish between them. + */ + if (arptype != ARPHRD_ETHER) + continue; + } + return linktypes[i].nm_type; } - return linktypes[i].nm_type; } - } - /* Fallback for drivers that don't call SET_NETDEV_DEVTYPE() */ - if (wifi_utils_is_wifi (ifindex, ifname)) - return NM_LINK_TYPE_WIFI; + /* Fallback for drivers that don't call SET_NETDEV_DEVTYPE() */ + 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 diff --git a/src/platform/wifi/wifi-utils.c b/src/platform/wifi/wifi-utils.c index 32210e0099..b8da02c15e 100644 --- a/src/platform/wifi/wifi-utils.c +++ b/src/platform/wifi/wifi-utils.c @@ -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 diff --git a/src/platform/wifi/wifi-utils.h b/src/platform/wifi/wifi-utils.h index 3dca2ac1d5..4fd5a80bbe 100644 --- a/src/platform/wifi/wifi-utils.h +++ b/src/platform/wifi/wifi-utils.h @@ -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);