From 99a496b3ea09af27e7cdb30f05309d57787c1e1e Mon Sep 17 00:00:00 2001 From: Nikos Ntarmos Date: Sun, 29 Mar 2009 01:15:20 +0200 Subject: [PATCH] Adding if_up support for FreeBSD. 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 --- src/common.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 1 + src/conky.c | 38 +++++++++++++++++++++----------------- src/freebsd.c | 3 --- src/linux.c | 42 ------------------------------------------ src/linux.h | 1 - src/text_object.h | 4 +++- 7 files changed, 71 insertions(+), 64 deletions(-) diff --git a/src/common.c b/src/common.c index a9aa2d9d..ac3e27fa 100644 --- a/src/common.c +++ b/src/common.c @@ -32,6 +32,9 @@ #include #include #include +#include +#include +#include #include #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; diff --git a/src/common.h b/src/common.h index be182808..5d19886d 100644 --- a/src/common.h +++ b/src/common.h @@ -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); diff --git a/src/conky.c b/src/conky.c index cb30a041..842e2943 100644 --- a/src/conky.c +++ b/src/conky.c @@ -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; diff --git a/src/freebsd.c b/src/freebsd.c index aa976088..35be0db3 100644 --- a/src/freebsd.c +++ b/src/freebsd.c @@ -34,16 +34,13 @@ #include #include #include -#include #include #include #include #include -#include #include -#include #include #include #include diff --git a/src/linux.c b/src/linux.c index 1afcd601..33b331ff 100644 --- a/src/linux.c +++ b/src/linux.c @@ -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)) { \ diff --git a/src/linux.h b/src/linux.h index 4b1f098a..1ebe911b 100644 --- a/src/linux.h +++ b/src/linux.h @@ -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); diff --git a/src/text_object.h b/src/text_object.h index c0e2a8a4..2db447ed 100644 --- a/src/text_object.h +++ b/src/text_object.h @@ -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,