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