diff options
| author | Jesper Dangaard Brouer <brouer@redhat.com> | 2014-04-25 13:39:23 +0200 | 
|---|---|---|
| committer | Jesper Dangaard Brouer <brouer@redhat.com> | 2014-04-25 13:44:17 +0200 | 
| commit | f8206181da2a8d7e24c60d19f2193cf8f5ebe54e (patch) | |
| tree | 7d387e791f92cadb45d0240665e3f96137641b2e | |
| parent | bcd86fee383d25b14a2a85ec53b8a10e93d1e590 (diff) | |
ifpps: fix matching wrong interface/device
There were a bug in ifpps, that would match the first occurance
of a device/interface name in /proc/net/dev, because it located
the device based on a substring.
If the device name were starting with the same characters,
e.g. having a device named eth4 and a VLAN device named eth4.100.
If specifying --dev eth4, then depending on the order of
/proc/net/dev the wrong device could be choosen for stats. (Note
kernel does not guarantee the order of devices in proc file)
Fix the bug, by matching with a colon ':' appended to the device
name, as the kernel proc format prints a ':' behind the device name.
Fixes #119
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
| -rw-r--r-- | ifpps.c | 8 | 
1 files changed, 7 insertions, 1 deletions
| @@ -226,12 +226,17 @@ static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)  {  	int ret = -EINVAL;  	char buff[256]; +	char *ifname_colon;  	FILE *fp;  	fp = fopen("/proc/net/dev", "r");  	if (!fp)  		panic("Cannot open /proc/net/dev!\n"); +	ifname_colon = xstrndup(ifname, strlen(ifname)+2); +	ifname_colon[strlen(ifname)]   = ':'; +	ifname_colon[strlen(ifname)+1] = '\0'; +  	if (fgets(buff, sizeof(buff), fp)) { ; }  	if (fgets(buff, sizeof(buff), fp)) { ; } @@ -240,7 +245,7 @@ static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)  	while (fgets(buff, sizeof(buff), fp) != NULL) {  		buff[sizeof(buff) -1] = 0; -		if (strstr(buff, ifname) == NULL) +		if (strstr(buff, ifname_colon) == NULL)  			continue;  		if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu" @@ -259,6 +264,7 @@ static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)  		memset(buff, 0, sizeof(buff));  	} +	xfree(ifname_colon);  	fclose(fp);  	return ret;  } | 
