# vnstat on FreeBSD 9 broken (with quick fix)



## nixcz (Sep 23, 2011)

Not sure if I'm the only one having this problem.  On multiple machines running FBSD9-beta2 (amd64), vnstat 1.11 does not work properly.  It seems to work fine on various FBSD 7 and 8 systems.

The symptom was the error "Not enough data available yet." no matter how many times the databases were updated with `vnstat -u`.  Running it in live mode and as a daemon shows 0 bytes and 0 packets on the interface(s).

Using -D showed that interface statistics were not being retrieved correctly; consequently, they could not be calculated:


```
cc: 6510615555426900570 - 6510615555426900570 = 0
cc: 6510615555426900570 - 6510615555426900570 = 0
cc: 6510615555426900570 - 6510615555426900570 = 0
cc: 6510615555426900570 - 6510615555426900570 = 0
```

At first I thought the method to retrieve interface statistics changed between FBSD 8 and 9, but then after a few debug printf()s, I tracked the problem down to the readifaddrs() function in ifinfo.c

I found that moving the call to freeifaddrs(ifap) from before the last "if" block to after it (and right before the final return) would allow the statistics to be retrieved and calculated correctly.

I have no idea if this is the proper fix, and don't know why this would make a difference between the FBSD versions, except that maybe this is one of those compiler quirks.

Hope this helps someone.

Copy and pasted diff -u:

```
--- ifinfo.c.orig       2011-09-23 01:13:22.544097251 -0400
+++ ifinfo.c    2011-09-23 01:13:36.813101878 -0400
@@ -420,7 +420,6 @@
                        break;
                }
        }
-       freeifaddrs(ifap);

        if (check == 0) {
                if (debug)
@@ -435,6 +434,7 @@
                ifinfo.filled = 1;
        }

+       freeifaddrs(ifap);
        return 1;
 }
 #endif
```


----------



## DutchDaemon (Sep 23, 2011)

Have you contacted the port maintainer (see net/vnstat) to discuss this?


----------



## nixcz (Sep 24, 2011)

DutchDaemon said:
			
		

> Have you contacted the port maintainer (see net/vnstat) to discuss this?


Well, I did just now.  I had been hoping to avoid email completely for this episode but I guess it can't be helped.


----------



## adox (Oct 29, 2011)

The patch fixes the problem  Sorry for the long time ;| I was a little busy in the past few months. Does the patch run without problems on 7/8/9?

-dhn


----------



## nixcz (Nov 1, 2011)

adox said:
			
		

> The patch fixes the problem  Sorry for the long time ;| I was a little busy in the past few months. Does the patch run without problems on 7/8/9?
> 
> -dhn



Sorry, I'm not in a position to test it on 7 or 8 right now.  As far as I know, it is unnecessary on those versions.  It was only necessary on 9, and I haven't tried recompiling it any time recently (between then and now, since RC1 has come out).


----------

