Adding if_up support for FreeBSD.
authorNikos Ntarmos <ntarmos@cs.uoi.gr>
Sat, 28 Mar 2009 23:15:20 +0000 (01:15 +0200)
committerPhil Sutter <phil@nwl.cc>
Sun, 29 Mar 2009 00:25:57 +0000 (01:25 +0100)
Moved interface_up(...) from linux.{c.h} to common.{c,h} and taught it
to check for ENXIO as well to make it work on FreeBSD.

Signed-off-by: Nikos Ntarmos <ntarmos@cs.uoi.gr>

src/common.c
src/common.h
src/conky.c
src/freebsd.c
src/linux.c
src/linux.h
src/text_object.h

index a9aa2d9..ac3e27f 100644 (file)
@@ -32,6 +32,9 @@
 #include <ctype.h>
 #include <errno.h>
 #include <sys/time.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <netinet/in.h>
 #include <pthread.h>
 #include "diskio.h"
 
@@ -186,6 +189,49 @@ void clear_net_stats(void)
        memset(netstats, 0, sizeof(netstats));
 }
 
+/* We should check if this is ok with OpenBSD and NetBSD as well. */
+int interface_up(const char *dev)
+{
+       int fd;
+       struct ifreq ifr;
+
+       if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
+               CRIT_ERR("could not create sockfd");
+               return 0;
+       }
+       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+       if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
+               /* if device does not exist, treat like not up */
+               if (errno != ENODEV && errno != ENXIO)
+                       perror("SIOCGIFFLAGS");
+               goto END_FALSE;
+       }
+
+       if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
+               goto END_FALSE;
+       if (ifup_strictness == IFUP_UP)
+               goto END_TRUE;
+
+       if (!(ifr.ifr_flags & IFF_RUNNING))
+               goto END_FALSE;
+       if (ifup_strictness == IFUP_LINK)
+               goto END_TRUE;
+
+       if (ioctl(fd, SIOCGIFADDR, &ifr)) {
+               perror("SIOCGIFADDR");
+               goto END_FALSE;
+       }
+       if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
+               goto END_TRUE;
+
+END_FALSE:
+       close(fd);
+       return 0;
+END_TRUE:
+       close(fd);
+       return 1;
+}
+
 void free_dns_data(void)
 {
        int i;
index be18280..5d19886 100644 (file)
@@ -69,6 +69,7 @@ struct net_stat {
 };
 void clear_net_stats(void);
 struct net_stat *get_net_stat(const char *);
+int interface_up(const char *dev);
 
 void get_adt746x_cpu(char *, size_t);
 void get_adt746x_fan(char *, size_t);
index cb30a04..842e294 100644 (file)
@@ -722,10 +722,6 @@ static void free_text_objects(struct text_object *root)
                        case OBJ_disk_protect:
                                free(data.s);
                                break;
-                       case OBJ_if_up:
-                               free(data.ifblock.s);
-                               free(data.ifblock.str);
-                               break;
                        case OBJ_if_gw:
                                free(data.ifblock.s);
                                free(data.ifblock.str);
@@ -745,6 +741,11 @@ static void free_text_objects(struct text_object *root)
                                        free(data.s);
                                break;
 #endif
+#if (defined(__FreeBSD__) || defined(__linux__))
+                       case OBJ_if_up:
+                               free(data.ifblock.s);
+                               free(data.ifblock.str);
+#endif
 #ifdef XMMS2
                        case OBJ_xmms2_artist:
                                if (info.xmms2.artist) {
@@ -1214,12 +1215,6 @@ static struct text_object *construct_text_object(const char *s,
        END OBJ(ibm_volume, 0)
        END OBJ(ibm_brightness, 0)
 #endif
-       END OBJ_IF(if_up, 0)
-               if (!arg) {
-                       ERR("if_up needs an argument");
-                       obj->data.ifblock.s = 0;
-               } else
-                       obj->data.ifblock.s = strndup(arg, text_buffer_size);
        END OBJ_IF(if_gw, 0)
        END OBJ(ioscheduler, 0)
                if (!arg) {
@@ -1242,6 +1237,14 @@ static struct text_object *construct_text_object(const char *s,
                }
 
 #endif /* __linux__ */
+#if (defined(__FreeBSD__) || defined(__linux__))
+       END OBJ_IF(if_up, 0)
+               if (!arg) {
+                       ERR("if_up needs an argument");
+                       obj->data.ifblock.s = 0;
+               } else
+                       obj->data.ifblock.s = strndup(arg, text_buffer_size);
+#endif
 #if defined(__OpenBSD__)
        END OBJ(obsd_sensors_temp, 0)
                if (!arg) {
@@ -3357,12 +3360,6 @@ static void generate_text_internal(char *p, int p_max_size,
                                get_ibm_acpi_brightness(p, p_max_size);
                        }
 #endif /* IBM */
-                       OBJ(if_up) {
-                               if ((obj->data.ifblock.s)
-                                               && (!interface_up(obj->data.ifblock.s))) {
-                                       DO_JUMP;
-                               }
-                       }
                        OBJ(if_gw) {
                                if (!cur->gw_info.count) {
                                        DO_JUMP;
@@ -3381,7 +3378,14 @@ static void generate_text_internal(char *p, int p_max_size,
                                get_powerbook_batt_info(p, p_max_size, obj->data.i);
                        }
 #endif /* __linux__ */
-
+#if (defined(__FreeBSD__) || defined(__linux__))
+                       OBJ(if_up) {
+                               if ((obj->data.ifblock.s)
+                                               && (!interface_up(obj->data.ifblock.s))) {
+                                       DO_JUMP;
+                               }
+                       }
+#endif
 #ifdef __OpenBSD__
                        OBJ(obsd_sensors_temp) {
                                obsd_sensors.device = sensor_device;
index aa97608..35be0db 100644 (file)
 #include <sys/types.h>
 #include <sys/vmmeter.h>
 #include <sys/user.h>
-#include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <net/if_mib.h>
 #include <net/if_media.h>
 #include <net/if_var.h>
-#include <netinet/in.h>
 
 #include <devstat.h>
-#include <fcntl.h>
 #include <ifaddrs.h>
 #include <limits.h>
 #include <unistd.h>
index 1afcd60..33b331f 100644 (file)
@@ -211,48 +211,6 @@ char *get_ioscheduler(char *disk)
        return strndup("n/a", text_buffer_size);
 }
 
-int interface_up(const char *dev)
-{
-       int fd;
-       struct ifreq ifr;
-
-       if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
-               CRIT_ERR("could not create sockfd");
-               return 0;
-       }
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
-       if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
-               /* if device does not exist, treat like not up */
-               if (errno != ENODEV)
-                       perror("SIOCGIFFLAGS");
-               goto END_FALSE;
-       }
-
-       if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
-               goto END_FALSE;
-       if (ifup_strictness == IFUP_UP)
-               goto END_TRUE;
-
-       if (!(ifr.ifr_flags & IFF_RUNNING))
-               goto END_FALSE;
-       if (ifup_strictness == IFUP_LINK)
-               goto END_TRUE;
-
-       if (ioctl(fd, SIOCGIFADDR, &ifr)) {
-               perror("SIOCGIFADDR");
-               goto END_FALSE;
-       }
-       if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
-               goto END_TRUE;
-
-END_FALSE:
-       close(fd);
-       return 0;
-END_TRUE:
-       close(fd);
-       return 1;
-}
-
 #define COND_FREE(x) if(x) free(x); x = 0
 #define SAVE_SET_STRING(x, y) \
        if (x && strcmp((char *)x, (char *)y)) { \
index 4b1f098..1ebe911 100644 (file)
@@ -20,7 +20,6 @@ struct i8k_struct {
 
 struct i8k_struct i8k;
 
-int interface_up(const char *dev);
 char *get_ioscheduler(char *);
 int get_laptop_mode(void);
 void update_gateway_info(void);
index c0e2a8a..2db447e 100644 (file)
@@ -147,7 +147,6 @@ enum text_object_type {
        OBJ_smapi_bat_power,
        OBJ_if_smapi_bat_installed,
 #endif /* IBM */
-       OBJ_if_up,
        OBJ_if_gw,
        OBJ_ioscheduler,
        OBJ_gw_iface,
@@ -165,6 +164,9 @@ enum text_object_type {
        OBJ_wireless_link_qual_perc,
        OBJ_wireless_link_bar,
 #endif /* __linux__ */
+#if defined(__FreeBSD__) || defined(__linux__)
+       OBJ_if_up,
+#endif
        OBJ_if_empty,
        OBJ_if_match,
        OBJ_if_existing,