From: norly Date: Tue, 14 Feb 2012 01:27:32 +0000 (+0000) Subject: Cleaned up 'netif'. X-Git-Url: https://git.enpas.org/?p=sysstatus.git;a=commitdiff_plain;h=504ab94abcd8209ce96bcb747db7536f9d49c083 Cleaned up 'netif'. Replaced 'netif' with 'netif_named'. Fixed memory leak in new 'netif'. Outsourced raw file reading to tools.c. --- diff --git a/config.h b/config.h index 7879911..58495c7 100644 --- a/config.h +++ b/config.h @@ -9,6 +9,6 @@ #define CPU_HISTORY_SIZE 10 #define NUM_CPUS 2 -#define IFNAME "ppp0" +#define NETIF_BASEDIR "/sys/class/net/" #endif diff --git a/statuses/Makefile b/statuses/Makefile index fa66069..d3f8a46 100644 --- a/statuses/Makefile +++ b/statuses/Makefile @@ -1,4 +1,4 @@ -STATUSES=cpuusage.o datetime.o memusage.o netif_named.o power.o temp.o uptime.o volume_alsa.o +STATUSES=cpuusage.o datetime.o memusage.o netif.o power.o temp.o uptime.o volume_alsa.o OTHERS=tools.o all: *.c *.h Makefile $(STATUSES) $(OTHERS) diff --git a/statuses/netif.c b/statuses/netif.c new file mode 100644 index 0000000..cd131f2 --- /dev/null +++ b/statuses/netif.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include "netif.h" +#include "tools.h" +#include "../config.h" + +#ifndef NETIF_BASEDIR + #define NETIF_BASEDIR "/sys/class/net/" +#endif + + +void status_netif(char *ifname) +{ + char ifpath[256]; + int ifpathlen; + + char stline[16]; + size_t stlen; + + double ifsum = 0.0; + int ifsumpower; + + + // Prepare path + ifpathlen = sizeof(NETIF_BASEDIR) - 1 + strlen(ifname); + if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) + { + statusError("status_netif", + "ifpath buffer too small", + ifname); + return; + } + strcpy(ifpath, NETIF_BASEDIR); + strcat(ifpath, ifname); + + + // Is the interface up? + if (access(ifpath, F_OK)) + { + //printf(" ^fg(grey)[%s] ", ifname); + return; + } + + + strcpy(&ifpath[ifpathlen], "/carrier"); + stlen = fileRead(stline, sizeof(stline), ifpath); + if (stlen > 0) + { + if (stline[0] == '1') + { + fputs("^fg(yellow)", stdout); + } + else + { + //fputs("^fg(red)", stdout); + return; + } + } + + strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes"); + stlen = fileRead(stline, sizeof(stline), ifpath); + if (stlen > 0) + ifsum = atof(stline); + + strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes"); + stlen = fileRead(stline, sizeof(stline), ifpath); + if (stlen > 0) + ifsum += atof(stline); + + + for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) + ifsum = ifsum / 1024; + + printf(" %s: %.*f %c ", ifname, + ifsumpower ? ifsumpower - 1 : ifsumpower, + ifsum, + powerToChar(ifsumpower)); +} diff --git a/statuses/netif.h b/statuses/netif.h new file mode 100644 index 0000000..92ccc1f --- /dev/null +++ b/statuses/netif.h @@ -0,0 +1,7 @@ + +#ifndef __NETIF_H__ +#define __NETIF_H__ + +void status_netif(char *ifname); + +#endif diff --git a/statuses/netif_named.c b/statuses/netif_named.c deleted file mode 100644 index 3fa90f1..0000000 --- a/statuses/netif_named.c +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include -#include -#include -#include "netif_named.h" -#include "tools.h" - -#define NETIF_BASEDIR "/sys/class/net/" - -void status_netif_named(char *ifname) -{ - char stline[16]; - int stfile; - size_t stlen; - - double ifsum; - int ifsumpower; - - char *stfilename = NULL; - - stfilename = malloc(sizeof(NETIF_BASEDIR) - 1 + strlen(ifname) + sizeof("/statistics/rx_bytes")); - - strcpy(stfilename, NETIF_BASEDIR); - strcat(stfilename, ifname); - - if (access(stfilename, F_OK)) - { - //printf(" ^fg(grey)[%s] ", ifname); - return; - } - - strcat(stfilename, "/carrier"); - stfile = open(stfilename, 0); - if (stfile != -1) - { - stlen = read(stfile, stline, sizeof(stline) - 1); - close(stfile); - if (stline[0] == '1') - fputs("^fg(yellow)", stdout); - else - { - //fputs("^fg(red)", stdout); - return; - } - } - - strcpy(stfilename, NETIF_BASEDIR); - strcat(stfilename, ifname); - strcat(stfilename, "/statistics/rx_bytes"); - stfile = open(stfilename, 0); - if (stfile != -1) - { - stlen = read(stfile, stline, sizeof(stline) - 1); - close(stfile); - stline[stlen] = '\0'; - ifsum = atof(stline); - } - - strcpy(stfilename, NETIF_BASEDIR); - strcat(stfilename, ifname); - strcat(stfilename, "/statistics/tx_bytes"); - stfile = open(stfilename, 0); - if (stfile != -1) - { - stlen = read(stfile, stline, sizeof(stline) - 1); - close(stfile); - stline[stlen] = '\0'; - ifsum += atof(stline); - } - - for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) - ifsum = ifsum / 1024; - - printf(" %s: %.*f %c ", ifname, ifsumpower ? ifsumpower - 1 : ifsumpower, ifsum, powertochar(ifsumpower)); -} diff --git a/statuses/netif_named.h b/statuses/netif_named.h deleted file mode 100644 index adb4a3e..0000000 --- a/statuses/netif_named.h +++ /dev/null @@ -1,7 +0,0 @@ - -#ifndef __NETIF_NAMED_H__ -#define __NETIF_NAMED_H__ - -void status_netif_named(char *ifname); - -#endif diff --git a/statuses/tools.c b/statuses/tools.c index 1e0385d..7a4a677 100644 --- a/statuses/tools.c +++ b/statuses/tools.c @@ -1,6 +1,9 @@ +#include +#include +#include #include "tools.h" -char powertochar(int power) +char powerToChar(int power) { switch(power) { @@ -19,6 +22,36 @@ char powertochar(int power) case 6: return 'E'; } - + return '?'; } + + +void statusError(char *where, char *what, char *extra) +{ + fprintf(stderr, "%s: %s", where, what); + if (extra) + fprintf(stderr, " -- %s", extra); + fputs("\n", stderr); +} + + +ssize_t fileRead(char *buf, size_t bufsize, char *file) +{ + int fd; + int readbytes; + + fd = open(file, 0); + if (fd < 0) + return -1; + + readbytes = read(fd, buf, bufsize - 1); + close(fd); + + if (readbytes > 0) + buf[readbytes] = '\0'; + else + buf[0] = '\0'; + + return readbytes; +} \ No newline at end of file diff --git a/statuses/tools.h b/statuses/tools.h index 1b9f344..802b9f1 100644 --- a/statuses/tools.h +++ b/statuses/tools.h @@ -2,6 +2,8 @@ #ifndef __TOOLS_H__ #define __TOOLS_H__ -char powertochar(int power); +char powerToChar(int power); +void statusError(char *where, char *what, char *extra); +ssize_t fileRead(char *buf, size_t bufsize, char *file); #endif diff --git a/sysstatus.c b/sysstatus.c index bbdb2fe..4e5d6c8 100644 --- a/sysstatus.c +++ b/sysstatus.c @@ -5,7 +5,7 @@ #include "statuses/uptime.h" #include "statuses/memusage.h" #include "statuses/cpuusage.h" -#include "statuses/netif_named.h" +#include "statuses/netif.h" #include "statuses/power.h" #include "statuses/volume_alsa.h" #include "statuses/temp.h" @@ -22,11 +22,11 @@ void updatestatus() status_memusage(); - status_netif_named("eth0"); - status_netif_named("eth1"); - status_netif_named("wlan0"); - status_netif_named("wlan1"); - status_netif_named("ppp0"); + status_netif("eth0"); + status_netif("eth1"); + status_netif("wlan0"); + status_netif("wlan1"); + status_netif("ppp0"); status_temp("GPU: ", "/sys/class/hwmon/hwmon0/device/temp4_input"); status_temp("CPU: ", "/sys/class/hwmon/hwmon0/device/temp2_input"); @@ -43,16 +43,14 @@ int main() { struct timeval tv; - updatestatus(); - for(;;) { + updatestatus(); + tv.tv_sec = UPDATE_SECS; tv.tv_usec = 0; select(0, NULL, NULL, NULL, &tv); - - updatestatus(); } return 0;