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