]> git.sur5r.net Git - i3/i3status/blobdiff - src/first_network_device.c
Merge branch 'master' into master
[i3/i3status] / src / first_network_device.c
index 593238531223bd401696d94d9cc3e389db05a7d2..b930f5357a709e803c635b499132b6cd160a5cb1 100644 (file)
 
 #include "i3status.h"
 
-#ifdef __linux__
-#define LOOPBACK_DEV "lo"
-#else
+#ifdef __OpenBSD__
 #define LOOPBACK_DEV "lo0"
+#else
+#define LOOPBACK_DEV "lo"
 #endif
 
 static bool sysfs_devtype(char *dest, size_t n, const char *ifnam) {
@@ -50,24 +50,24 @@ static bool sysfs_devtype(char *dest, size_t n, const char *ifnam) {
     return true;
 }
 
-static net_type_t iface_type(const char *ifname) {
-#ifdef __linux__
-    char devtype[32];
-
-    if (!sysfs_devtype(devtype, sizeof(devtype), ifname))
-        return NET_TYPE_OTHER;
-
-    if (!devtype[0])
-        return NET_TYPE_ETHERNET;
+static bool is_virtual(const char *ifname) {
+    char path[1024];
+    char *target = NULL;
+    bool is_virtual = false;
 
-    if (strcmp(devtype, "wlan") == 0)
-        return NET_TYPE_WIRELESS;
+    snprintf(path, sizeof(path), "/sys/class/net/%s", ifname);
+    if ((target = realpath(path, NULL))) {
+        if (BEGINS_WITH(target, "/sys/devices/virtual/")) {
+            is_virtual = true;
+        }
+    }
 
-    if (strcmp(devtype, "wwan") == 0)
-        return NET_TYPE_OTHER;
+    free(target);
+    return is_virtual;
+}
 
-    return NET_TYPE_OTHER;
-#elif __OpenBSD__
+static net_type_t iface_type(const char *ifname) {
+#ifdef __OpenBSD__
     /*
      *First determine if the device is a wireless device by trying two ioctl(2)
      * commands against it. If either succeeds we can be sure it's a wireless
@@ -110,8 +110,24 @@ static net_type_t iface_type(const char *ifname) {
         return NET_TYPE_ETHERNET;
     }
 #else
-#error Missing implementation to determine interface type.
+    char devtype[32];
+
+    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;
+
+    if (strcmp(devtype, "wlan") == 0)
+        return NET_TYPE_WIRELESS;
+
+    if (strcmp(devtype, "wwan") == 0)
+        return NET_TYPE_OTHER;
+
+    return NET_TYPE_OTHER;
 #endif
+    return NET_TYPE_OTHER;
 }
 
 const char *first_eth_interface(const net_type_t type) {
@@ -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;
     }