]> git.proxmox.com Git - mirror_iproute2.git/blame - misc/ifstat.c
(Logical change 1.92)
[mirror_iproute2.git] / misc / ifstat.c
CommitLineData
aba5acdf
SH
1/*
2 * ifstat.c handy utility to read net interface statistics
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <fcntl.h>
16#include <string.h>
17#include <errno.h>
18#include <time.h>
19#include <sys/time.h>
20#include <fnmatch.h>
21#include <sys/file.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <sys/poll.h>
25#include <sys/wait.h>
26#include <sys/stat.h>
27#include <signal.h>
28#include <math.h>
9f3ea250 29#include <getopt.h>
aba5acdf
SH
30
31#include <libnetlink.h>
32#include <linux/netdevice.h>
33
34#include <SNAPSHOT.h>
35
36int dump_zeros = 0;
37int reset_history = 0;
38int ignore_history = 0;
39int no_output = 0;
40int no_update = 0;
41int scan_interval = 0;
42int time_constant = 0;
43int show_errors = 0;
44double W;
45char **patterns;
46int npatterns;
47
48char info_source[128];
49int source_mismatch;
50
51#define MAXS (sizeof(struct net_device_stats)/sizeof(unsigned long))
52
53struct ifstat_ent
54{
55 struct ifstat_ent *next;
56 char *name;
57 int ifindex;
58 unsigned long long val[MAXS];
59 double rate[MAXS];
60 unsigned long ival[MAXS];
61};
62
63struct ifstat_ent *kern_db;
64struct ifstat_ent *hist_db;
65
66int match(char *id)
67{
68 int i;
69
70 if (npatterns == 0)
71 return 1;
72
73 for (i=0; i<npatterns; i++) {
74 if (!fnmatch(patterns[i], id, 0))
75 return 1;
76 }
77 return 0;
78}
79
6dc9f016 80int get_nlmsg(const struct sockaddr_nl *who, const struct nlmsghdr *m, void *arg)
aba5acdf
SH
81{
82 struct ifinfomsg *ifi = NLMSG_DATA(m);
83 struct rtattr * tb[IFLA_MAX+1];
84 int len = m->nlmsg_len;
85 struct ifstat_ent *n;
86 int i;
87
88 if (m->nlmsg_type != RTM_NEWLINK)
89 return 0;
90
91 len -= NLMSG_LENGTH(sizeof(*ifi));
92 if (len < 0)
93 return -1;
94
95 if (!(ifi->ifi_flags&IFF_UP))
96 return 0;
97
98 memset(tb, 0, sizeof(tb));
99 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
100 if (tb[IFLA_IFNAME] == NULL || tb[IFLA_STATS] == NULL)
101 return 0;
102
103 n = malloc(sizeof(*n));
104 if (!n)
105 abort();
106 n->ifindex = ifi->ifi_index;
107 n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
108 memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
109 memset(&n->rate, 0, sizeof(n->rate));
110 for (i=0; i<MAXS; i++)
111 n->val[i] = n->ival[i];
112 n->next = kern_db;
113 kern_db = n;
114 return 0;
115}
116
117void load_info(void)
118{
119 struct ifstat_ent *db, *n;
120 struct rtnl_handle rth;
121
122 if (rtnl_open(&rth, 0) < 0)
123 exit(1);
124
125 if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) {
126 perror("Cannot send dump request");
127 exit(1);
128 }
129
130 if (rtnl_dump_filter(&rth, get_nlmsg, NULL, NULL, NULL) < 0) {
131 fprintf(stderr, "Dump terminated\n");
132 exit(1);
133 }
134
135 rtnl_close(&rth);
136
137 db = kern_db;
138 kern_db = NULL;
139
140 while (db) {
141 n = db;
142 db = db->next;
143 n->next = kern_db;
144 kern_db = n;
145 }
146}
147
148void load_raw_table(FILE *fp)
149{
150 char buf[4096];
151 struct ifstat_ent *db = NULL;
152 struct ifstat_ent *n;
153
154 while (fgets(buf, sizeof(buf), fp) != NULL) {
155 char *p;
156 char *next;
157 int i;
158
159 if (buf[0] == '#') {
160 buf[strlen(buf)-1] = 0;
161 if (info_source[0] && strcmp(info_source, buf+1))
162 source_mismatch = 1;
163 strncpy(info_source, buf+1, sizeof(info_source)-1);
164 continue;
165 }
166 if ((n = malloc(sizeof(*n))) == NULL)
167 abort();
168
169 if (!(p = strchr(buf, ' ')))
170 abort();
171 *p++ = 0;
172
173 if (sscanf(buf, "%d", &n->ifindex) != 1)
174 abort();
175 if (!(next = strchr(p, ' ')))
176 abort();
177 *next++ = 0;
178
179 n->name = strdup(p);
180 p = next;
181
182 for (i=0; i<MAXS; i++) {
183 unsigned rate;
184 if (!(next = strchr(p, ' ')))
185 abort();
186 *next++ = 0;
187 if (sscanf(p, "%llu", n->val+i) != 1)
188 abort();
189 n->ival[i] = (unsigned long)n->val[i];
190 p = next;
191 if (!(next = strchr(p, ' ')))
192 abort();
193 *next++ = 0;
194 if (sscanf(p, "%u", &rate) != 1)
195 abort();
196 n->rate[i] = rate;
197 p = next;
198 }
199 n->next = db;
200 db = n;
201 }
202
203 while (db) {
204 n = db;
205 db = db->next;
206 n->next = kern_db;
207 kern_db = n;
208 }
209}
210
211void dump_raw_db(FILE *fp, int to_hist)
212{
213 struct ifstat_ent *n, *h;
214 h = hist_db;
215 fprintf(fp, "#%s\n", info_source);
216
217 for (n=kern_db; n; n=n->next) {
218 int i;
219 unsigned long long *vals = n->val;
220 double *rates = n->rate;
221 if (!match(n->name)) {
222 struct ifstat_ent *h1;
223 if (!to_hist)
224 continue;
225 for (h1 = h; h1; h1 = h1->next) {
226 if (h1->ifindex == n->ifindex) {
227 vals = h1->val;
228 rates = h1->rate;
229 h = h1->next;
230 break;
231 }
232 }
233 }
234 fprintf(fp, "%d %s ", n->ifindex, n->name);
235 for (i=0; i<MAXS; i++)
236 fprintf(fp, "%llu %u ", vals[i], (unsigned)rates[i]);
237 fprintf(fp, "\n");
238 }
239}
240
9f3ea250
SH
241/* use communication definitions of meg/kilo etc */
242static const unsigned long long giga = 1000000000ull;
243static const unsigned long mega = 1000000;
244static const unsigned long kilo = 1000;
aba5acdf
SH
245
246void format_rate(FILE *fp, unsigned long long *vals, double *rates, int i)
247{
248 char temp[64];
9f3ea250
SH
249 if (vals[i] > giga)
250 fprintf(fp, "%7lluM ", vals[i]/mega);
251 else if (vals[i] > mega)
252 fprintf(fp, "%7lluK ", vals[i]/kilo);
aba5acdf
SH
253 else
254 fprintf(fp, "%8llu ", vals[i]);
255
9f3ea250
SH
256 if (rates[i] > mega) {
257 sprintf(temp, "%uM", (unsigned)(rates[i]/mega));
aba5acdf 258 fprintf(fp, "%-6s ", temp);
9f3ea250
SH
259 } else if (rates[i] > kilo) {
260 sprintf(temp, "%uK", (unsigned)(rates[i]/kilo));
aba5acdf
SH
261 fprintf(fp, "%-6s ", temp);
262 } else
263 fprintf(fp, "%-6u ", (unsigned)rates[i]);
264}
265
266void format_pair(FILE *fp, unsigned long long *vals, int i, int k)
267{
268 char temp[64];
9f3ea250
SH
269 if (vals[i] > giga)
270 fprintf(fp, "%7lluM ", vals[i]/mega);
271 else if (vals[i] > mega)
272 fprintf(fp, "%7lluK ", vals[i]/kilo);
aba5acdf
SH
273 else
274 fprintf(fp, "%8llu ", vals[i]);
275
9f3ea250
SH
276 if (vals[k] > giga) {
277 sprintf(temp, "%uM", (unsigned)(vals[k]/mega));
aba5acdf 278 fprintf(fp, "%-6s ", temp);
9f3ea250
SH
279 } else if (vals[k] > mega) {
280 sprintf(temp, "%uK", (unsigned)(vals[k]/kilo));
aba5acdf
SH
281 fprintf(fp, "%-6s ", temp);
282 } else
283 fprintf(fp, "%-6u ", (unsigned)vals[k]);
284}
285
286void print_head(FILE *fp)
287{
288 fprintf(fp, "#%s\n", info_source);
289 fprintf(fp, "%-15s ", "Interface");
290
291 fprintf(fp, "%8s/%-6s ", "RX Pkts", "Rate");
292 fprintf(fp, "%8s/%-6s ", "TX Pkts", "Rate");
293 fprintf(fp, "%8s/%-6s ", "RX Data", "Rate");
294 fprintf(fp, "%8s/%-6s\n","TX Data", "Rate");
295
296 if (!show_errors) {
297 fprintf(fp, "%-15s ", "");
298 fprintf(fp, "%8s/%-6s ", "RX Errs", "Drop");
299 fprintf(fp, "%8s/%-6s ", "TX Errs", "Drop");
300 fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
301 fprintf(fp, "%8s/%-6s\n","TX Coll", "Rate");
302 } else {
303 fprintf(fp, "%-15s ", "");
304 fprintf(fp, "%8s/%-6s ", "RX Errs", "Rate");
305 fprintf(fp, "%8s/%-6s ", "RX Drop", "Rate");
306 fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
307 fprintf(fp, "%8s/%-6s\n","RX Leng", "Rate");
308
309 fprintf(fp, "%-15s ", "");
310 fprintf(fp, "%8s/%-6s ", "RX Crc", "Rate");
311 fprintf(fp, "%8s/%-6s ", "RX Frm", "Rate");
312 fprintf(fp, "%8s/%-6s ", "RX Fifo", "Rate");
313 fprintf(fp, "%8s/%-6s\n","RX Miss", "Rate");
314
315 fprintf(fp, "%-15s ", "");
316 fprintf(fp, "%8s/%-6s ", "TX Errs", "Rate");
317 fprintf(fp, "%8s/%-6s ", "TX Drop", "Rate");
318 fprintf(fp, "%8s/%-6s ", "TX Coll", "Rate");
319 fprintf(fp, "%8s/%-6s\n","TX Carr", "Rate");
320
321 fprintf(fp, "%-15s ", "");
322 fprintf(fp, "%8s/%-6s ", "TX Abrt", "Rate");
323 fprintf(fp, "%8s/%-6s ", "TX Fifo", "Rate");
324 fprintf(fp, "%8s/%-6s ", "TX Hear", "Rate");
325 fprintf(fp, "%8s/%-6s\n","TX Wind", "Rate");
326 }
327}
328
329void print_one_if(FILE *fp, struct ifstat_ent *n, unsigned long long *vals)
330{
331 int i;
332 fprintf(fp, "%-15s ", n->name);
333 for (i=0; i<4; i++)
334 format_rate(fp, vals, n->rate, i);
335 fprintf(fp, "\n");
336
337 if (!show_errors) {
338 fprintf(fp, "%-15s ", "");
339 format_pair(fp, vals, 4, 6);
340 format_pair(fp, vals, 5, 7);
341 format_rate(fp, vals, n->rate, 11);
342 format_rate(fp, vals, n->rate, 9);
343 fprintf(fp, "\n");
344 } else {
345 fprintf(fp, "%-15s ", "");
346 format_rate(fp, vals, n->rate, 4);
347 format_rate(fp, vals, n->rate, 6);
348 format_rate(fp, vals, n->rate, 11);
349 format_rate(fp, vals, n->rate, 10);
350 fprintf(fp, "\n");
351
352 fprintf(fp, "%-15s ", "");
353 format_rate(fp, vals, n->rate, 12);
354 format_rate(fp, vals, n->rate, 13);
355 format_rate(fp, vals, n->rate, 14);
356 format_rate(fp, vals, n->rate, 15);
357 fprintf(fp, "\n");
358
359 fprintf(fp, "%-15s ", "");
360 format_rate(fp, vals, n->rate, 5);
361 format_rate(fp, vals, n->rate, 7);
362 format_rate(fp, vals, n->rate, 9);
363 format_rate(fp, vals, n->rate, 17);
364 fprintf(fp, "\n");
365
366 fprintf(fp, "%-15s ", "");
367 format_rate(fp, vals, n->rate, 16);
368 format_rate(fp, vals, n->rate, 18);
369 format_rate(fp, vals, n->rate, 19);
370 format_rate(fp, vals, n->rate, 20);
371 fprintf(fp, "\n");
372 }
373}
374
375
376void dump_kern_db(FILE *fp)
377{
378 struct ifstat_ent *n, *h;
379 h = hist_db;
380
381 print_head(fp);
382
383 for (n=kern_db; n; n=n->next) {
384 if (!match(n->name))
385 continue;
386 print_one_if(fp, n, n->val);
387 }
388}
389
390
391void dump_incr_db(FILE *fp)
392{
393 struct ifstat_ent *n, *h;
394 h = hist_db;
395
396 print_head(fp);
397
398 for (n=kern_db; n; n=n->next) {
399 int i;
400 unsigned long long vals[MAXS];
401 struct ifstat_ent *h1;
402
403 memcpy(vals, n->val, sizeof(vals));
404
405 for (h1 = h; h1; h1 = h1->next) {
406 if (h1->ifindex == n->ifindex) {
407 for (i = 0; i < MAXS; i++)
408 vals[i] -= h1->val[i];
409 h = h1->next;
410 break;
411 }
412 }
413 if (!match(n->name))
414 continue;
415 print_one_if(fp, n, vals);
416 }
417}
418
419
420static int children;
421
422void sigchild(int signo)
423{
424}
425
426void update_db(int interval)
427{
428 struct ifstat_ent *n, *h;
429
430 n = kern_db;
431 kern_db = NULL;
432
433 load_info();
434
435 h = kern_db;
436 kern_db = n;
437
438 for (n = kern_db; n; n = n->next) {
439 struct ifstat_ent *h1;
440 for (h1 = h; h1; h1 = h1->next) {
441 if (h1->ifindex == n->ifindex) {
442 int i;
443 for (i = 0; i < MAXS; i++) {
444 if ((long)(h1->ival[i] - n->ival[i]) < 0) {
445 memset(n->ival, 0, sizeof(n->ival));
446 break;
447 }
448 }
449 for (i = 0; i < MAXS; i++) {
450 double sample;
451 unsigned long incr = h1->ival[i] - n->ival[i];
452 n->val[i] += incr;
453 n->ival[i] = h1->ival[i];
454 sample = (double)(incr*1000)/interval;
455 if (interval >= scan_interval) {
456 n->rate[i] += W*(sample-n->rate[i]);
457 } else if (interval >= 1000) {
458 if (interval >= time_constant) {
459 n->rate[i] = sample;
460 } else {
461 double w = W*(double)interval/scan_interval;
462 n->rate[i] += w*(sample-n->rate[i]);
463 }
464 }
465 }
466
467 while (h != h1) {
468 struct ifstat_ent *tmp = h;
469 h = h->next;
470 free(tmp->name);
471 free(tmp);
472 };
473 h = h1->next;
474 free(h1->name);
475 free(h1);
476 break;
477 }
478 }
479 }
480}
481
482#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
483
484
485void server_loop(int fd)
486{
487 struct timeval snaptime;
488 struct pollfd p;
489 p.fd = fd;
490 p.events = p.revents = POLLIN;
491
492 sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d",
493 getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);
494
495 load_info();
496
497 for (;;) {
498 int status;
499 int tdiff;
500 struct timeval now;
501 gettimeofday(&now, NULL);
502 tdiff = T_DIFF(now, snaptime);
503 if (tdiff >= scan_interval) {
504 update_db(tdiff);
505 snaptime = now;
506 tdiff = 0;
507 }
508 if (poll(&p, 1, tdiff + scan_interval) > 0
509 && (p.revents&POLLIN)) {
510 int clnt = accept(fd, NULL, NULL);
511 if (clnt >= 0) {
512 pid_t pid;
513 if (children >= 5) {
514 close(clnt);
515 } else if ((pid = fork()) != 0) {
516 if (pid>0)
517 children++;
518 close(clnt);
519 } else {
520 FILE *fp = fdopen(clnt, "w");
521 if (fp) {
522 if (tdiff > 0)
523 update_db(tdiff);
524 dump_raw_db(fp, 0);
525 }
526 exit(0);
527 }
528 }
529 }
530 while (children && waitpid(-1, &status, WNOHANG) > 0)
531 children--;
532 }
533}
534
535int verify_forging(int fd)
536{
537 struct ucred cred;
538 int olen = sizeof(cred);
539 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
540 olen < sizeof(cred))
541 return -1;
542 if (cred.uid == getuid() || cred.uid == 0)
543 return 0;
544 return -1;
545}
546
d7e0809e
SH
547static void usage(void) __attribute__((noreturn));
548
aba5acdf
SH
549static void usage(void)
550{
d7e0809e
SH
551 fprintf(stderr,
552"Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
553" -h, --help this message\n"
554" -a, --ignore ignore history\n"
555" -d, --scan=SECS sample every statistics every SECS\n"
556" -e, --errors show errors\n"
557" -n, --nooutput do history only\n"
558" -r, --reset reset history\n"
559" -s, --noupdate don;t update history\n"
560" -t, --interval=SECS report average over the last SECS\n"
561" -V, --version output version information\n"
562" -z, --zeros show entries with zero activity\n"
563" -e, --errors show errors\n"
564" -z, --zeros show entries with zero activity\n");
565
566 exit(-1);
aba5acdf
SH
567}
568
9f3ea250
SH
569static const struct option longopts[] = {
570 { "help", 0, 0, 'h' },
571 { "ignore", 0, 0, 'a' },
572 { "scan", 1, 0, 'd'},
573 { "errors", 0, 0, 'e' },
574 { "nooutput", 0, 0, 'n' },
575 { "reset", 0, 0, 'r' },
576 { "noupdate", 0, 0, 's' },
577 { "interval", 1, 0, 't' },
578 { "version", 0, 0, 'V' },
579 { "zeros", 0, 0, 'z' },
580 { "errors", 0, 0, 'e' },
581 { "zeros", 0, 0, 'z' },
582 { 0 }
583};
aba5acdf
SH
584
585int main(int argc, char *argv[])
586{
587 char hist_name[128];
588 struct sockaddr_un sun;
589 FILE *hist_fp = NULL;
590 int ch;
591 int fd;
592
9f3ea250
SH
593 while ((ch = getopt_long(argc, argv, "hvVzrnasd:t:eK",
594 longopts, NULL)) != EOF) {
aba5acdf
SH
595 switch(ch) {
596 case 'z':
597 dump_zeros = 1;
598 break;
599 case 'r':
600 reset_history = 1;
601 break;
602 case 'a':
603 ignore_history = 1;
604 break;
605 case 's':
606 no_update = 1;
607 break;
608 case 'n':
609 no_output = 1;
610 break;
611 case 'e':
612 show_errors = 1;
613 break;
614 case 'd':
9f3ea250
SH
615 scan_interval = atoi(optarg) * 1000;
616 if (scan_interval <= 0) {
617 fprintf(stderr, "ifstat: invalid scan interval\n");
618 exit(-1);
619 }
aba5acdf
SH
620 break;
621 case 't':
9f3ea250
SH
622 time_constant = atoi(optarg);
623 if (time_constant <= 0) {
aba5acdf
SH
624 fprintf(stderr, "ifstat: invalid time constant divisor\n");
625 exit(-1);
626 }
627 break;
628 case 'v':
629 case 'V':
630 printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT);
631 exit(0);
632 case 'h':
633 case '?':
634 default:
635 usage();
636 }
637 }
638
639 argc -= optind;
640 argv += optind;
641
642 sun.sun_family = AF_UNIX;
643 sun.sun_path[0] = 0;
644 sprintf(sun.sun_path+1, "ifstat%d", getuid());
645
646 if (scan_interval > 0) {
647 if (time_constant == 0)
648 time_constant = 60;
649 time_constant *= 1000;
650 W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
651 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
652 perror("ifstat: socket");
653 exit(-1);
654 }
655 if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
656 perror("ifstat: bind");
657 exit(-1);
658 }
659 if (listen(fd, 5) < 0) {
660 perror("ifstat: listen");
661 exit(-1);
662 }
663 if (fork())
664 exit(0);
665 chdir("/");
666 close(0); close(1); close(2); setsid();
667 signal(SIGPIPE, SIG_IGN);
668 signal(SIGCHLD, sigchild);
669 server_loop(fd);
670 exit(0);
671 }
672
673 patterns = argv;
674 npatterns = argc;
675
676 if (getenv("IFSTAT_HISTORY"))
677 snprintf(hist_name, sizeof(hist_name), getenv("IFSTAT_HISTORY"));
678 else
c0076e70 679 sprintf(hist_name, "%s/.ifstat.u%d", P_tmpdir, getuid());
aba5acdf
SH
680
681 if (reset_history)
682 unlink(hist_name);
683
684 if (!ignore_history || !no_update) {
685 struct stat stb;
686
687 fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
688 if (fd < 0) {
689 perror("ifstat: open history file");
690 exit(-1);
691 }
692 if ((hist_fp = fdopen(fd, "r+")) == NULL) {
693 perror("ifstat: fdopen history file");
694 exit(-1);
695 }
696 if (flock(fileno(hist_fp), LOCK_EX)) {
697 perror("ifstat: flock history file");
698 exit(-1);
699 }
700 if (fstat(fileno(hist_fp), &stb) != 0) {
701 perror("ifstat: fstat history file");
702 exit(-1);
703 }
704 if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
705 fprintf(stderr, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n");
706 exit(-1);
707 }
708 if (!ignore_history) {
709 FILE *tfp;
710 long uptime;
711 if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
712 if (fscanf(tfp, "%ld", &uptime) != 1)
713 uptime = -1;
714 fclose(tfp);
715 }
716 if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
717 fprintf(stderr, "ifstat: history is aged out, resetting\n");
718 ftruncate(fileno(hist_fp), 0);
719 }
720 }
721
722 load_raw_table(hist_fp);
723
724 hist_db = kern_db;
725 kern_db = NULL;
726 }
727
728 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
729 (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
730 || (strcpy(sun.sun_path+1, "ifstat0"),
731 connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
732 && verify_forging(fd) == 0) {
733 FILE *sfp = fdopen(fd, "r");
734 load_raw_table(sfp);
735 if (hist_db && source_mismatch) {
736 fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
737 hist_db = NULL;
738 }
739 fclose(sfp);
740 } else {
741 if (fd >= 0)
742 close(fd);
743 if (hist_db && info_source[0] && strcmp(info_source, "kernel")) {
744 fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
745 hist_db = NULL;
746 info_source[0] = 0;
747 }
748 load_info();
749 if (info_source[0] == 0)
750 strcpy(info_source, "kernel");
751 }
752
753 if (!no_output) {
754 if (ignore_history || hist_db == NULL)
755 dump_kern_db(stdout);
756 else
757 dump_incr_db(stdout);
758 }
759 if (!no_update) {
760 ftruncate(fileno(hist_fp), 0);
761 rewind(hist_fp);
762 dump_raw_db(hist_fp, 1);
763 fflush(hist_fp);
764 }
765 exit(0);
766}