diff options
| author | Fabian Franzen | 2017-04-12 03:30:28 +0200 | 
|---|---|---|
| committer | Fabian Franzen | 2017-04-12 03:46:04 +0200 | 
| commit | f45581f8d8bffff8f1eb98486953ec491eb77a05 (patch) | |
| tree | 099ebfb43a2dec28dd896ba8424d9a7f9ec28da7 | |
| parent | Multiple CPU support for cpu_usage (#209) (diff) | |
Add check for virtual ethernet devices
The _first_ option for ethernet devices now uses the link in sysfs to determine
if it's a real device or just a virtual one (i.e veth** devices created by docker).
| -rw-r--r-- | src/first_network_device.c | 18 | 
1 files changed, 18 insertions, 0 deletions
| diff --git a/src/first_network_device.c b/src/first_network_device.c index 5932385..abbc9b0 100644 --- a/src/first_network_device.c +++ b/src/first_network_device.c @@ -50,6 +50,21 @@ static bool sysfs_devtype(char *dest, size_t n, const char *ifnam) {      return true;  } +static bool is_virtual(const char *ifname) { +    char path[1024]; +    char *target = NULL; +    const char virtual_template[] = "/sys/devices/virtual/"; + +    snprintf(path, sizeof(path), "/sys/class/net/%s", ifname); +    if ((target = realpath(path, NULL))) { +        if (strncmp(virtual_template, target, strlen(virtual_template)) == 0) +            return true; +    } +    free(target); + +    return false; +} +  static net_type_t iface_type(const char *ifname) {  #ifdef __linux__      char devtype[32]; @@ -57,6 +72,7 @@ static net_type_t iface_type(const char *ifname) {      if (!sysfs_devtype(devtype, sizeof(devtype), ifname))          return NET_TYPE_OTHER; +    /* Default to Ethernet when no devtype is available */      if (!devtype[0])          return NET_TYPE_ETHERNET; @@ -147,6 +163,8 @@ const char *first_eth_interface(const net_type_t type) {          iftype = iface_type(addrp->ifa_name);          if (iftype != type)              continue; +        if (is_virtual(addrp->ifa_name)) +            continue;          interface = strdup(addrp->ifa_name);          break;      } | 
