Re: [PATCH] linux: check the prefix 'wlan' for WiFi device as the last fallback method

On Mon, 2016-12-05 at 15:37 +0800, Shih-Yuan Lee (FourDollars) wrote:
The sysfs path may not exist due to race conditions while accessing

It's better to check the prefix 'wlan' as the last fallback method.

For example, 'wlan0' has been renamed to 'wlp1s0', but it's still
'wlan0' above so it will always fail.

You can see this problem below.


I think it would be better to solve this differently. Relying on a
certain name is a terrible hack.

The problem exists with all those APIs that use a ifname instead of an
ifindex. In this case, it's worse because we don't re-detect the
device-type after having it done once.

How about, wifi_utils_is_wifi() gets also an argument ifindex.
Instead of operating on the path name directly, it would first
open("/sys/class/net/wlan0") and then use openat to navigate inside
this directory.

  nmp_utils_open_sysctl(int ifindex, const char *ifname)
      #define SYS_CLASS_NET "/sys/class/net/"
      char ifname_buf[IFNAMSIZ];
      guint try_count = 0;
      char sysdir[NM_STRLEN (SYS_CLASS_NET) + IFNAMSIZ + 1] = SYS_CLASS_NET;
      char fd_buf[256];

      while (++try_count < 4) {
           if (!ifname) {
              ifname = if_indextoname(ifindex, ifname_buf);
              if (!ifname)
                  return -1;
           nm_utils_ifname_cpy (&sysdir[NM_STRLEN (SYS_CLASS_NET)], ifname);
           fd = open (sysdir, O_DIRECTORY);
           if (fd < 0)
                goto next;
           fd_ifindex = openat (fd, "ifindex", 0);
           if {fd_ifindex < 0) {
               goto next;
           /* read ifindex file, and compare it to @ifindex. If match, return fd. */
           nn = nm_utils_fd_read_loop (fd_ifindex, fd_buf, sizeof (fd_buf) - 1, FALSE);
           if (nn < 0) {
                 close (fd_ifindex);
                 goto next;
           fd_buf[sizeof (fd_buf) - 1] = '\0'

           if (ifindex != _nm_utils_ascii_str_to_int64 (fd_buf, 10, 1, G_MAXINT, -1) {
                 close (fd_ifindex);
                 goto next;
           return fd;
          ifname = NULL;
     return -1;


Attachment: signature.asc
Description: This is a digitally signed message part

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]