platform: wifi: use nmp_utils_open_sysctl() to check if device is wifi

Since function nmp_utils_open_sysctl() can avoid race condition, use it
in wifi_utils_is_wifi() to open sysfs and correctly check if it's a wifi
device.

https://bugzilla.gnome.org/show_bug.cgi?id=775613
Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
This commit is contained in:
Kai-Heng Feng
2016-12-07 18:40:09 +08:00
committed by Thomas Haller
parent 713c74f6e4
commit b95556eb78
3 changed files with 26 additions and 11 deletions

View File

@@ -721,7 +721,7 @@ _linktype_get_type (NMPlatform *platform,
} }
/* Fallback for drivers that don't call SET_NETDEV_DEVTYPE() */ /* Fallback for drivers that don't call SET_NETDEV_DEVTYPE() */
if (wifi_utils_is_wifi (ifname)) if (wifi_utils_is_wifi (ifindex, ifname))
return NM_LINK_TYPE_WIFI; return NM_LINK_TYPE_WIFI;
if (arptype == ARPHRD_ETHER) { if (arptype == ARPHRD_ETHER) {

View File

@@ -26,6 +26,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <fcntl.h>
#include "wifi-utils-private.h" #include "wifi-utils-private.h"
#include "wifi-utils-nl80211.h" #include "wifi-utils-nl80211.h"
@@ -34,6 +35,8 @@
#endif #endif
#include "nm-core-utils.h" #include "nm-core-utils.h"
#include "platform/nm-platform-utils.h"
gpointer gpointer
wifi_data_new (const char *iface, int ifindex, gsize len) wifi_data_new (const char *iface, int ifindex, gsize len)
{ {
@@ -180,23 +183,35 @@ wifi_utils_deinit (WifiData *data)
} }
gboolean gboolean
wifi_utils_is_wifi (const char *iface) wifi_utils_is_wifi (int ifindex, const char *ifname)
{ {
char phy80211_path[NM_STRLEN ("/sys/class/net/123456789012345/phy80211\0") + 100 /*safety*/]; int fd_sysnet;
int fd_phy80211;
struct stat s; struct stat s;
g_return_val_if_fail (iface != NULL, FALSE); g_return_val_if_fail (ifname != NULL, FALSE);
nm_sprintf_buf (phy80211_path, fd_sysnet = nmp_utils_open_sysctl (ifindex, ifname);
"/sys/class/net/%s/phy80211", if (fd_sysnet < 0)
NM_ASSERT_VALID_PATH_COMPONENT (iface)); return FALSE;
nm_assert (strlen (phy80211_path) < sizeof (phy80211_path) - 1);
if ((stat (phy80211_path, &s) == 0 && (s.st_mode & S_IFDIR))) fd_phy80211 = openat (fd_sysnet, "phy80211", 0);
if (fd_phy80211 < 0) {
close (fd_sysnet);
return FALSE;
}
if ((fstat (fd_phy80211, &s) == 0 && (s.st_mode & S_IFDIR))) {
close (fd_sysnet);
close (fd_phy80211);
return TRUE; return TRUE;
}
close (fd_sysnet);
close (fd_phy80211);
#if HAVE_WEXT #if HAVE_WEXT
if (wifi_wext_is_wifi (iface)) if (wifi_wext_is_wifi (ifname))
return TRUE; return TRUE;
#endif #endif

View File

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