1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 12:27:52 +00:00

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 <ntarmos@cs.uoi.gr>
This commit is contained in:
Nikos Ntarmos 2009-03-29 01:15:20 +02:00 committed by Phil Sutter
parent 50bac84006
commit 99a496b3ea
7 changed files with 71 additions and 64 deletions

View File

@ -32,6 +32,9 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pthread.h> #include <pthread.h>
#include "diskio.h" #include "diskio.h"
@ -186,6 +189,49 @@ void clear_net_stats(void)
memset(netstats, 0, sizeof(netstats)); 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) void free_dns_data(void)
{ {
int i; int i;

View File

@ -69,6 +69,7 @@ struct net_stat {
}; };
void clear_net_stats(void); void clear_net_stats(void);
struct net_stat *get_net_stat(const char *); struct net_stat *get_net_stat(const char *);
int interface_up(const char *dev);
void get_adt746x_cpu(char *, size_t); void get_adt746x_cpu(char *, size_t);
void get_adt746x_fan(char *, size_t); void get_adt746x_fan(char *, size_t);

View File

@ -722,10 +722,6 @@ static void free_text_objects(struct text_object *root)
case OBJ_disk_protect: case OBJ_disk_protect:
free(data.s); free(data.s);
break; break;
case OBJ_if_up:
free(data.ifblock.s);
free(data.ifblock.str);
break;
case OBJ_if_gw: case OBJ_if_gw:
free(data.ifblock.s); free(data.ifblock.s);
free(data.ifblock.str); free(data.ifblock.str);
@ -745,6 +741,11 @@ static void free_text_objects(struct text_object *root)
free(data.s); free(data.s);
break; break;
#endif #endif
#if (defined(__FreeBSD__) || defined(__linux__))
case OBJ_if_up:
free(data.ifblock.s);
free(data.ifblock.str);
#endif
#ifdef XMMS2 #ifdef XMMS2
case OBJ_xmms2_artist: case OBJ_xmms2_artist:
if (info.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_volume, 0)
END OBJ(ibm_brightness, 0) END OBJ(ibm_brightness, 0)
#endif #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_IF(if_gw, 0)
END OBJ(ioscheduler, 0) END OBJ(ioscheduler, 0)
if (!arg) { if (!arg) {
@ -1242,6 +1237,14 @@ static struct text_object *construct_text_object(const char *s,
} }
#endif /* __linux__ */ #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__) #if defined(__OpenBSD__)
END OBJ(obsd_sensors_temp, 0) END OBJ(obsd_sensors_temp, 0)
if (!arg) { 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); get_ibm_acpi_brightness(p, p_max_size);
} }
#endif /* IBM */ #endif /* IBM */
OBJ(if_up) {
if ((obj->data.ifblock.s)
&& (!interface_up(obj->data.ifblock.s))) {
DO_JUMP;
}
}
OBJ(if_gw) { OBJ(if_gw) {
if (!cur->gw_info.count) { if (!cur->gw_info.count) {
DO_JUMP; 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); get_powerbook_batt_info(p, p_max_size, obj->data.i);
} }
#endif /* __linux__ */ #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__ #ifdef __OpenBSD__
OBJ(obsd_sensors_temp) { OBJ(obsd_sensors_temp) {
obsd_sensors.device = sensor_device; obsd_sensors.device = sensor_device;

View File

@ -34,16 +34,13 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/vmmeter.h> #include <sys/vmmeter.h>
#include <sys/user.h> #include <sys/user.h>
#include <sys/ioctl.h>
#include <net/if.h> #include <net/if.h>
#include <net/if_mib.h> #include <net/if_mib.h>
#include <net/if_media.h> #include <net/if_media.h>
#include <net/if_var.h> #include <net/if_var.h>
#include <netinet/in.h>
#include <devstat.h> #include <devstat.h>
#include <fcntl.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <limits.h> #include <limits.h>
#include <unistd.h> #include <unistd.h>

View File

@ -211,48 +211,6 @@ char *get_ioscheduler(char *disk)
return strndup("n/a", text_buffer_size); 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 COND_FREE(x) if(x) free(x); x = 0
#define SAVE_SET_STRING(x, y) \ #define SAVE_SET_STRING(x, y) \
if (x && strcmp((char *)x, (char *)y)) { \ if (x && strcmp((char *)x, (char *)y)) { \

View File

@ -20,7 +20,6 @@ struct i8k_struct {
struct i8k_struct i8k; struct i8k_struct i8k;
int interface_up(const char *dev);
char *get_ioscheduler(char *); char *get_ioscheduler(char *);
int get_laptop_mode(void); int get_laptop_mode(void);
void update_gateway_info(void); void update_gateway_info(void);

View File

@ -147,7 +147,6 @@ enum text_object_type {
OBJ_smapi_bat_power, OBJ_smapi_bat_power,
OBJ_if_smapi_bat_installed, OBJ_if_smapi_bat_installed,
#endif /* IBM */ #endif /* IBM */
OBJ_if_up,
OBJ_if_gw, OBJ_if_gw,
OBJ_ioscheduler, OBJ_ioscheduler,
OBJ_gw_iface, OBJ_gw_iface,
@ -165,6 +164,9 @@ enum text_object_type {
OBJ_wireless_link_qual_perc, OBJ_wireless_link_qual_perc,
OBJ_wireless_link_bar, OBJ_wireless_link_bar,
#endif /* __linux__ */ #endif /* __linux__ */
#if defined(__FreeBSD__) || defined(__linux__)
OBJ_if_up,
#endif
OBJ_if_empty, OBJ_if_empty,
OBJ_if_match, OBJ_if_match,
OBJ_if_existing, OBJ_if_existing,