Commit dd81ee04 authored by Phil Sutter's avatar Phil Sutter Committed by Stephen Hemminger

ifstat, nstat: fix daemon mode

Since the relevant code (and it's bugs) is identical in both files, fix
them in one go. This patch fixes multiple issues:

* Using 'int' for the 'tdiff' variable does not suffice on 64bit
  systems, the assigned initial time difference makes it wrap and
  contain a negative value afterwards. Instead use the more appropriate
  'time_t' type.

* As far as I understood the code, poll() is supposed to time out just
  at the right time to trigger update_db() in the configured interval.
  Therefore it's timeout must be set to the desired interval *minus* the
  time that has already passed since then.

* With the last change to the algorithm in place, it does not make sense
  to call update_db() before returning data to the connected client.
  Actually, it never does otherwise we could skip the periodic updates
  in the first place.
Signed-off-by: default avatarPhil Sutter <phil@nwl.cc>
parent 5f4d27d5
...@@ -589,7 +589,7 @@ static void server_loop(int fd) ...@@ -589,7 +589,7 @@ static void server_loop(int fd)
for (;;) { for (;;) {
int status; int status;
int tdiff; time_t tdiff;
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
...@@ -600,7 +600,7 @@ static void server_loop(int fd) ...@@ -600,7 +600,7 @@ static void server_loop(int fd)
tdiff = 0; tdiff = 0;
} }
if (poll(&p, 1, tdiff + scan_interval) > 0 if (poll(&p, 1, scan_interval - tdiff) > 0
&& (p.revents&POLLIN)) { && (p.revents&POLLIN)) {
int clnt = accept(fd, NULL, NULL); int clnt = accept(fd, NULL, NULL);
if (clnt >= 0) { if (clnt >= 0) {
...@@ -613,11 +613,8 @@ static void server_loop(int fd) ...@@ -613,11 +613,8 @@ static void server_loop(int fd)
close(clnt); close(clnt);
} else { } else {
FILE *fp = fdopen(clnt, "w"); FILE *fp = fdopen(clnt, "w");
if (fp) { if (fp)
if (tdiff > 0)
update_db(tdiff);
dump_raw_db(fp, 0); dump_raw_db(fp, 0);
}
exit(0); exit(0);
} }
} }
......
...@@ -433,7 +433,7 @@ static void server_loop(int fd) ...@@ -433,7 +433,7 @@ static void server_loop(int fd)
for (;;) { for (;;) {
int status; int status;
int tdiff; time_t tdiff;
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
tdiff = T_DIFF(now, snaptime); tdiff = T_DIFF(now, snaptime);
...@@ -442,7 +442,7 @@ static void server_loop(int fd) ...@@ -442,7 +442,7 @@ static void server_loop(int fd)
snaptime = now; snaptime = now;
tdiff = 0; tdiff = 0;
} }
if (poll(&p, 1, tdiff + scan_interval) > 0 if (poll(&p, 1, scan_interval - tdiff) > 0
&& (p.revents&POLLIN)) { && (p.revents&POLLIN)) {
int clnt = accept(fd, NULL, NULL); int clnt = accept(fd, NULL, NULL);
if (clnt >= 0) { if (clnt >= 0) {
...@@ -455,11 +455,8 @@ static void server_loop(int fd) ...@@ -455,11 +455,8 @@ static void server_loop(int fd)
close(clnt); close(clnt);
} else { } else {
FILE *fp = fdopen(clnt, "w"); FILE *fp = fdopen(clnt, "w");
if (fp) { if (fp)
if (tdiff > 0)
update_db(tdiff);
dump_kern_db(fp, 0); dump_kern_db(fp, 0);
}
exit(0); exit(0);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment