mirror of
https://github.com/Llewellynvdm/conky.git
synced 2025-02-14 10:00:21 +00:00
core.cc,linux.cc: add ${iface} (#603)
* core.cc,linux.cc: add ${iface} * doc/variables.xml: add ${iface} * Remove code smells * linux.cc: Add for loop to iterate interfaces * Index all NICs * linux.cc: Allow+Remove NIC exclusion * linux.cc: Remove code smells * fix code smell * switch skip by strcmpreturn
This commit is contained in:
parent
6c590590a3
commit
eb31df1bef
@ -1573,6 +1573,16 @@
|
||||
(i.e. contain at least one decimal place).
|
||||
<para /></listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>
|
||||
<command>
|
||||
<option>iface</option>
|
||||
</command>
|
||||
<option>(number)</option>
|
||||
</term>
|
||||
<listitem>Display interface names starting from 1, eg ${iface 1}.
|
||||
<para /></listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>
|
||||
<command>
|
||||
|
11
src/core.cc
11
src/core.cc
@ -611,9 +611,6 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
|
||||
/* information from sony_laptop kernel module
|
||||
* /sys/devices/platform/sony-laptop */
|
||||
END OBJ(sony_fanspeed, 0) obj->callbacks.print = &get_sony_fanspeed;
|
||||
END OBJ_IF(if_gw, &update_gateway_info) obj->callbacks.iftest =
|
||||
&gateway_exists;
|
||||
obj->callbacks.free = &free_gateway_info;
|
||||
END OBJ_ARG(ioscheduler, 0, "get_ioscheduler needs an argument (e.g. hda)")
|
||||
obj->data.s = strndup(dev_name(arg), text_buffer_size.get(*state));
|
||||
obj->callbacks.print = &print_ioscheduler;
|
||||
@ -1449,9 +1446,15 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
|
||||
END OBJ(gw_iface, &update_gateway_info) obj->callbacks.print =
|
||||
&print_gateway_iface;
|
||||
obj->callbacks.free = &free_gateway_info;
|
||||
END OBJ_IF(if_gw, &update_gateway_info) obj->callbacks.iftest =
|
||||
&gateway_exists;
|
||||
obj->callbacks.free = &free_gateway_info;
|
||||
END OBJ(gw_ip, &update_gateway_info) obj->callbacks.print = &print_gateway_ip;
|
||||
obj->callbacks.free = &free_gateway_info;
|
||||
#endif /* !__linux__ */
|
||||
END OBJ(iface, &update_gateway_info2) obj->data.s = STRNDUP_ARG;
|
||||
obj->callbacks.print = &print_gateway_iface2;
|
||||
obj->callbacks.free = &gen_free_opaque;
|
||||
#endif /* __linux__ */
|
||||
#if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
|
||||
defined(__DragonFly__) || defined(__OpenBSD__)) && \
|
||||
(defined(i386) || defined(__i386__))
|
||||
|
130
src/linux.cc
130
src/linux.cc
@ -94,8 +94,14 @@ struct sysfs {
|
||||
float factor, offset;
|
||||
};
|
||||
|
||||
/* To be used inside upspeed/f downspeed/f as ${gw_iface} variable */
|
||||
char e_iface[50];
|
||||
|
||||
/* To use ${iface X} where X is a number and will
|
||||
* return the current X NIC name */
|
||||
static const unsigned int iface_len = 64U;
|
||||
static char interfaces_arr[iface_len][iface_len] = {""};
|
||||
|
||||
#define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
|
||||
#define LONGSTAT_TEMPL "%*s %llu %llu %llu "
|
||||
|
||||
@ -287,10 +293,65 @@ void update_gateway_info_failure(const char *reason) {
|
||||
/* Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT */
|
||||
#define RT_ENTRY_FORMAT "%63s %lx %lx %x %*d %*d %*d %lx %*d %*d %*d\n"
|
||||
|
||||
FILE *check_procroute() {
|
||||
FILE *fp;
|
||||
if ((fp = fopen("/proc/net/route", "r")) == nullptr) {
|
||||
update_gateway_info_failure("fopen()");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* skip over the table header line, which is always present */
|
||||
if (fscanf(fp, "%*[^\n]\n") < 0) {
|
||||
fclose(fp);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return fp;
|
||||
}
|
||||
|
||||
int update_gateway_info2(void) {
|
||||
FILE *fp;
|
||||
char iface[iface_len];
|
||||
unsigned long dest;
|
||||
unsigned long gate;
|
||||
unsigned long mask;
|
||||
unsigned int flags;
|
||||
unsigned int x = 1;
|
||||
unsigned int z = 1;
|
||||
int strcmpreturn;
|
||||
|
||||
if((fp = check_procroute()) != nullptr) {
|
||||
while (!feof(fp)) {
|
||||
strcmpreturn = 1;
|
||||
if (fscanf(fp, RT_ENTRY_FORMAT, iface, &dest, &gate, &flags, &mask) != 5) {
|
||||
update_gateway_info_failure("fscanf()");
|
||||
break;
|
||||
}
|
||||
if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate)) {
|
||||
snprintf(e_iface, 49, "%s", iface);
|
||||
}
|
||||
if (1U == x) {
|
||||
snprintf(interfaces_arr[x++], iface_len - 1, "%s", iface);
|
||||
continue;
|
||||
} else if (0 == strcmp(iface, interfaces_arr[x - 1])) {
|
||||
continue;
|
||||
}
|
||||
for (z = 1; z < iface_len - 1 && strcmpreturn == 1; z++) {
|
||||
strcmpreturn = strcmp(iface, interfaces_arr[z]);
|
||||
}
|
||||
if (strcmpreturn == 1) {
|
||||
snprintf(interfaces_arr[x++], iface_len - 1, "%s", iface);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int update_gateway_info(void) {
|
||||
FILE *fp;
|
||||
struct in_addr ina;
|
||||
char iface[64];
|
||||
char iface[iface_len];
|
||||
unsigned long dest, gate, mask;
|
||||
unsigned int flags;
|
||||
|
||||
@ -298,31 +359,22 @@ int update_gateway_info(void) {
|
||||
free_and_zero(gw_info.ip);
|
||||
gw_info.count = 0;
|
||||
|
||||
if ((fp = fopen("/proc/net/route", "r")) == nullptr) {
|
||||
update_gateway_info_failure("fopen()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* skip over the table header line, which is always present */
|
||||
if (fscanf(fp, "%*[^\n]\n") < 0) {
|
||||
if((fp = check_procroute()) != nullptr) {
|
||||
while (!feof(fp)) {
|
||||
if (fscanf(fp, RT_ENTRY_FORMAT, iface, &dest, &gate, &flags, &mask) != 5) {
|
||||
update_gateway_info_failure("fscanf()");
|
||||
break;
|
||||
}
|
||||
if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate)) {
|
||||
gw_info.count++;
|
||||
snprintf(e_iface, 49, "%s", iface);
|
||||
SAVE_SET_STRING(gw_info.iface, iface)
|
||||
ina.s_addr = gate;
|
||||
SAVE_SET_STRING(gw_info.ip, inet_ntoa(ina))
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (!feof(fp)) {
|
||||
if (fscanf(fp, RT_ENTRY_FORMAT, iface, &dest, &gate, &flags, &mask) != 5) {
|
||||
update_gateway_info_failure("fscanf()");
|
||||
break;
|
||||
}
|
||||
if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate)) {
|
||||
gw_info.count++;
|
||||
snprintf(e_iface, 49, "%s", iface);
|
||||
SAVE_SET_STRING(gw_info.iface, iface)
|
||||
ina.s_addr = gate;
|
||||
SAVE_SET_STRING(gw_info.ip, inet_ntoa(ina))
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -341,10 +393,38 @@ int gateway_exists(struct text_object *obj) {
|
||||
|
||||
void print_gateway_iface(struct text_object *obj, char *p, unsigned int p_max_size) {
|
||||
(void)obj;
|
||||
|
||||
snprintf(p, p_max_size, "%s", gw_info.iface);
|
||||
}
|
||||
|
||||
void print_gateway_iface2(struct text_object *obj, char *p, unsigned int p_max_size) {
|
||||
long int z = 0;
|
||||
unsigned int x = 1;
|
||||
unsigned int found = 0;
|
||||
char buf[iface_len * iface_len] = {""};
|
||||
char *buf_ptr = buf;
|
||||
|
||||
if (0 == strcmp(obj->data.s, "")) {
|
||||
for (; x < iface_len - 1; x++) {
|
||||
if (0 == strcmp("", interfaces_arr[x])) {
|
||||
break;
|
||||
}
|
||||
buf_ptr += snprintf(buf_ptr, iface_len - 1, "%s, ", interfaces_arr[x]);
|
||||
found = 1;
|
||||
}
|
||||
if (1 == found) {
|
||||
--buf_ptr;
|
||||
*(--buf_ptr) = '\0';
|
||||
}
|
||||
snprintf(p, p_max_size, "%s", buf);
|
||||
return;
|
||||
}
|
||||
|
||||
z = strtol(obj->data.s, (char **)NULL, 10);
|
||||
if ((iface_len - 1) > z) {
|
||||
snprintf(p, p_max_size, "%s", interfaces_arr[z]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_gateway_ip(struct text_object *obj, char *p, unsigned int p_max_size) {
|
||||
(void)obj;
|
||||
|
||||
|
@ -33,9 +33,11 @@ void print_ioscheduler(struct text_object *, char *, unsigned int);
|
||||
void print_laptop_mode(struct text_object *, char *, unsigned int);
|
||||
|
||||
int update_gateway_info(void);
|
||||
int update_gateway_info2(void);
|
||||
void free_gateway_info(struct text_object *obj);
|
||||
int gateway_exists(struct text_object *);
|
||||
void print_gateway_iface(struct text_object *, char *, unsigned int);
|
||||
void print_gateway_iface2(struct text_object *, char *, unsigned int);
|
||||
void print_gateway_ip(struct text_object *, char *, unsigned int);
|
||||
|
||||
enum { PB_BATT_STATUS, PB_BATT_PERCENT, PB_BATT_TIME };
|
||||
|
Loading…
x
Reference in New Issue
Block a user