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