source: rtems-libbsd/freebsd/usr.bin/netstat/if.c @ ce96623

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since ce96623 was 0a57e1d, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 08:35:05

Reduce divergence from FreeBSD sources

  • Property mode set to 100644
File size: 19.5 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*-
4 * Copyright (c) 1983, 1988, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by the University of
18 *      California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#if 0
37#ifndef lint
38static char sccsid[] = "@(#)if.c        8.3 (Berkeley) 4/28/95";
39#endif /* not lint */
40#endif
41
42#ifdef __rtems__
43#include <machine/rtems-bsd-program.h>
44#endif /* __rtems__ */
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD$");
47
48#include <rtems/bsd/sys/types.h>
49#include <sys/protosw.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/sysctl.h>
53#include <rtems/bsd/sys/time.h>
54
55#include <net/if.h>
56#include <net/if_var.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/ethernet.h>
60#include <net/pfvar.h>
61#include <net/if_pfsync.h>
62#include <netinet/in.h>
63#include <netinet/in_var.h>
64#ifndef __rtems__
65#include <netipx/ipx.h>
66#include <netipx/ipx_if.h>
67#endif /* __rtems__ */
68#include <arpa/inet.h>
69
70#include <err.h>
71#include <errno.h>
72#include <libutil.h>
73#ifdef INET6
74#include <netdb.h>
75#endif
76#include <signal.h>
77#include <stdint.h>
78#include <stdio.h>
79#include <stdlib.h>
80#include <string.h>
81#include <unistd.h>
82
83#include "netstat.h"
84
85#define YES     1
86#define NO      0
87
88static void sidewaysintpr(int, u_long);
89static void catchalarm(int);
90
91#ifdef INET6
92static char addr_buf[NI_MAXHOST];               /* for getnameinfo() */
93#endif
94
95/*
96 * Dump pfsync statistics structure.
97 */
98void
99pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
100{
101        struct pfsyncstats pfsyncstat, zerostat;
102        size_t len = sizeof(struct pfsyncstats);
103
104        if (live) {
105                if (zflag)
106                        memset(&zerostat, 0, len);
107                if (sysctlbyname("net.inet.pfsync.stats", &pfsyncstat, &len,
108                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
109                        if (errno != ENOENT)
110                                warn("sysctl: net.inet.pfsync.stats");
111                        return;
112                }
113        } else
114                kread(off, &pfsyncstat, len);
115
116        printf("%s:\n", name);
117
118#define p(f, m) if (pfsyncstat.f || sflag <= 1) \
119        printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
120#define p2(f, m) if (pfsyncstat.f || sflag <= 1) \
121        printf(m, (uintmax_t)pfsyncstat.f)
122
123        p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
124        p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
125        p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
126        p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
127        p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
128        p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
129        p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
130        p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
131        p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
132        p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
133        p(pfsyncs_stale, "\t\t%ju stale state%s\n");
134        p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
135        p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
136        p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
137        p2(pfsyncs_onomem, "\t\t%ju send failed due to mbuf memory error\n");
138        p2(pfsyncs_oerrors, "\t\t%ju send error\n");
139#undef p
140#undef p2
141}
142
143/*
144 * Display a formatted value, or a '-' in the same space.
145 */
146static void
147show_stat(const char *fmt, int width, u_long value, short showvalue)
148{
149        const char *lsep, *rsep;
150        char newfmt[32];
151
152        lsep = "";
153        if (strncmp(fmt, "LS", 2) == 0) {
154                lsep = " ";
155                fmt += 2;
156        }
157        rsep = " ";
158        if (strncmp(fmt, "NRS", 3) == 0) {
159                rsep = "";
160                fmt += 3;
161        }
162        if (showvalue == 0) {
163                /* Print just dash. */
164                sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
165                printf(newfmt, "-");
166                return;
167        }
168
169        if (hflag) {
170                char buf[5];
171
172                /* Format in human readable form. */
173                humanize_number(buf, sizeof(buf), (int64_t)value, "",
174                    HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
175                sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
176                printf(newfmt, buf);
177        } else {
178                /* Construct the format string. */
179                sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
180                printf(newfmt, value);
181        }
182}
183
184/*
185 * Print a description of the network interfaces.
186 */
187void
188intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
189{
190        struct ifnet ifnet;
191        struct ifnethead ifnethead;
192        union {
193                struct ifaddr ifa;
194                struct in_ifaddr in;
195#ifdef INET6
196                struct in6_ifaddr in6;
197#endif
198#ifndef __rtems__
199                struct ipx_ifaddr ipx;
200#endif /* __rtems__ */
201        } ifaddr;
202        u_long ifaddraddr;
203        u_long ifaddrfound;
204        u_long opackets;
205        u_long ipackets;
206        u_long obytes;
207        u_long ibytes;
208        u_long omcasts;
209        u_long imcasts;
210        u_long oerrors;
211        u_long ierrors;
212        u_long idrops;
213        u_long collisions;
214        short timer;
215        int drops;
216        struct sockaddr *sa = NULL;
217        char name[IFNAMSIZ];
218        short network_layer;
219        short link_layer;
220
221        if (ifnetaddr == 0) {
222                printf("ifnet: symbol not defined\n");
223                return;
224        }
225        if (interval1) {
226                sidewaysintpr(interval1, ifnetaddr);
227                return;
228        }
229        if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
230                return;
231        ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
232        if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
233                return;
234
235        if (!pfunc) {
236                if (Wflag)
237                        printf("%-7.7s", "Name");
238                else
239                        printf("%-5.5s", "Name");
240                printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
241                    "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
242                if (bflag)
243                        printf(" %10.10s","Ibytes");
244                printf(" %8.8s %5.5s", "Opkts", "Oerrs");
245                if (bflag)
246                        printf(" %10.10s","Obytes");
247                printf(" %5s", "Coll");
248                if (tflag)
249                        printf(" %s", "Time");
250                if (dflag)
251                        printf(" %s", "Drop");
252                putchar('\n');
253        }
254        ifaddraddr = 0;
255        while (ifnetaddr || ifaddraddr) {
256                struct sockaddr_in *sockin;
257#ifdef INET6
258                struct sockaddr_in6 *sockin6;
259#endif
260                char *cp;
261                int n, m;
262
263                network_layer = 0;
264                link_layer = 0;
265
266                if (ifaddraddr == 0) {
267                        if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
268                                return;
269                        strlcpy(name, ifnet.if_xname, sizeof(name));
270                        ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
271                        if (interface != 0 && strcmp(name, interface) != 0)
272                                continue;
273                        cp = index(name, '\0');
274
275                        if (pfunc) {
276                                (*pfunc)(name);
277                                continue;
278                        }
279
280                        if ((ifnet.if_flags&IFF_UP) == 0)
281                                *cp++ = '*';
282                        *cp = '\0';
283                        ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
284                }
285                ifaddrfound = ifaddraddr;
286
287                /*
288                 * Get the interface stats.  These may get
289                 * overriden below on a per-interface basis.
290                 */
291                opackets = ifnet.if_opackets;
292                ipackets = ifnet.if_ipackets;
293                obytes = ifnet.if_obytes;
294                ibytes = ifnet.if_ibytes;
295                omcasts = ifnet.if_omcasts;
296                imcasts = ifnet.if_imcasts;
297                oerrors = ifnet.if_oerrors;
298                ierrors = ifnet.if_ierrors;
299                idrops = ifnet.if_iqdrops;
300                collisions = ifnet.if_collisions;
301                timer = ifnet.if_timer;
302                drops = ifnet.if_snd.ifq_drops;
303
304                if (ifaddraddr == 0) {
305                        if (Wflag)
306                                printf("%-7.7s", name);
307                        else
308                                printf("%-5.5s", name);
309                        printf(" %5lu ", ifnet.if_mtu);
310                        printf("%-13.13s ", "none");
311                        printf("%-17.17s ", "none");
312                } else {
313                        if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
314                            != 0) {
315                                ifaddraddr = 0;
316                                continue;
317                        }
318#define CP(x) ((char *)(x))
319                        cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
320                                CP(&ifaddr);
321                        sa = (struct sockaddr *)cp;
322                        if (af != AF_UNSPEC && sa->sa_family != af) {
323                                ifaddraddr =
324                                    (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
325                                continue;
326                        }
327                        if (Wflag)
328                                printf("%-7.7s", name);
329                        else
330                                printf("%-5.5s", name);
331                        printf(" %5lu ", ifnet.if_mtu);
332                        switch (sa->sa_family) {
333                        case AF_UNSPEC:
334                                printf("%-13.13s ", "none");
335                                printf("%-15.15s ", "none");
336                                break;
337                        case AF_INET:
338                                sockin = (struct sockaddr_in *)sa;
339#ifdef notdef
340                                /* can't use inet_makeaddr because kernel
341                                 * keeps nets unshifted.
342                                 */
343                                in = inet_makeaddr(ifaddr.in.ia_subnet,
344                                        INADDR_ANY);
345                                printf("%-13.13s ", netname(in.s_addr,
346                                    ifaddr.in.ia_subnetmask));
347#else
348                                printf("%-13.13s ",
349                                    netname(htonl(ifaddr.in.ia_subnet),
350                                    ifaddr.in.ia_subnetmask));
351#endif
352                                printf("%-17.17s ",
353                                    routename(sockin->sin_addr.s_addr));
354
355                                network_layer = 1;
356                                break;
357#ifdef INET6
358                        case AF_INET6:
359                                sockin6 = (struct sockaddr_in6 *)sa;
360                                in6_fillscopeid(&ifaddr.in6.ia_addr);
361                                printf("%-13.13s ",
362                                       netname6(&ifaddr.in6.ia_addr,
363                                                &ifaddr.in6.ia_prefixmask.sin6_addr));
364                                in6_fillscopeid(sockin6);
365                                getnameinfo(sa, sa->sa_len, addr_buf,
366                                    sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
367                                printf("%-17.17s ", addr_buf);
368
369                                network_layer = 1;
370                                break;
371#endif /*INET6*/
372#ifndef __rtems__
373                        case AF_IPX:
374                                {
375                                struct sockaddr_ipx *sipx =
376                                        (struct sockaddr_ipx *)sa;
377                                u_long net;
378                                char netnum[10];
379
380                                *(union ipx_net *) &net = sipx->sipx_addr.x_net;
381                                sprintf(netnum, "%lx", (u_long)ntohl(net));
382                                printf("ipx:%-8s  ", netnum);
383/*                              printf("ipx:%-8s ", netname(net, 0L)); */
384                                printf("%-17s ",
385                                    ipx_phost((struct sockaddr *)sipx));
386                                }
387
388                                network_layer = 1;
389                                break;
390#endif /* __rtems__ */
391
392#ifndef __rtems__
393                        case AF_APPLETALK:
394                                printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
395                                printf("%-11.11s  ",atalk_print(sa,0x0b) );
396                                break;
397#endif /* __rtems__ */
398                        case AF_LINK:
399                                {
400                                struct sockaddr_dl *sdl =
401                                        (struct sockaddr_dl *)sa;
402                                char linknum[10];
403                                cp = (char *)LLADDR(sdl);
404                                n = sdl->sdl_alen;
405                                sprintf(linknum, "<Link#%d>", sdl->sdl_index);
406                                m = printf("%-13.13s ", linknum);
407                                }
408                                goto hexprint;
409                        default:
410                                m = printf("(%d)", sa->sa_family);
411                                for (cp = sa->sa_len + (char *)sa;
412                                        --cp > sa->sa_data && (*cp == 0);) {}
413                                n = cp - sa->sa_data + 1;
414                                cp = sa->sa_data;
415                        hexprint:
416                                while ((--n >= 0) && (m < 30))
417                                        m += printf("%02x%c", *cp++ & 0xff,
418                                                    n > 0 ? ':' : ' ');
419                                m = 32 - m;
420                                while (m-- > 0)
421                                        putchar(' ');
422
423                                link_layer = 1;
424                                break;
425                        }
426
427                        /*
428                         * Fixup the statistics for interfaces that
429                         * update stats for their network addresses
430                         */
431                        if (network_layer) {
432                                opackets = ifaddr.in.ia_ifa.if_opackets;
433                                ipackets = ifaddr.in.ia_ifa.if_ipackets;
434                                obytes = ifaddr.in.ia_ifa.if_obytes;
435                                ibytes = ifaddr.in.ia_ifa.if_ibytes;
436                        }
437
438                        ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
439                }
440
441                show_stat("lu", 8, ipackets, link_layer|network_layer);
442                show_stat("lu", 5, ierrors, link_layer);
443                show_stat("lu", 5, idrops, link_layer);
444                if (bflag)
445                        show_stat("lu", 10, ibytes, link_layer|network_layer);
446
447                show_stat("lu", 8, opackets, link_layer|network_layer);
448                show_stat("lu", 5, oerrors, link_layer);
449                if (bflag)
450                        show_stat("lu", 10, obytes, link_layer|network_layer);
451
452                show_stat("NRSlu", 5, collisions, link_layer);
453                if (tflag)
454                        show_stat("LSd", 4, timer, link_layer);
455                if (dflag)
456                        show_stat("LSd", 4, drops, link_layer);
457                putchar('\n');
458
459                if (aflag && ifaddrfound) {
460                        /*
461                         * Print family's multicast addresses
462                         */
463                        struct ifmultiaddr *multiaddr;
464                        struct ifmultiaddr ifma;
465                        union {
466                                struct sockaddr sa;
467                                struct sockaddr_in in;
468#ifdef INET6
469                                struct sockaddr_in6 in6;
470#endif /* INET6 */
471                                struct sockaddr_dl dl;
472                        } msa;
473                        const char *fmt;
474
475                        TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
476                                if (kread((u_long)multiaddr, (char *)&ifma,
477                                          sizeof ifma) != 0)
478                                        break;
479                                multiaddr = &ifma;
480                                if (kread((u_long)ifma.ifma_addr, (char *)&msa,
481                                          sizeof msa) != 0)
482                                        break;
483                                if (msa.sa.sa_family != sa->sa_family)
484                                        continue;
485
486                                fmt = 0;
487                                switch (msa.sa.sa_family) {
488                                case AF_INET:
489                                        fmt = routename(msa.in.sin_addr.s_addr);
490                                        break;
491#ifdef INET6
492                                case AF_INET6:
493                                        in6_fillscopeid(&msa.in6);
494                                        getnameinfo(&msa.sa, msa.sa.sa_len,
495                                            addr_buf, sizeof(addr_buf), 0, 0,
496                                            NI_NUMERICHOST);
497                                        printf("%*s %-19.19s(refs: %d)\n",
498                                               Wflag ? 27 : 25, "",
499                                               addr_buf, ifma.ifma_refcount);
500                                        break;
501#endif /* INET6 */
502                                case AF_LINK:
503                                        switch (msa.dl.sdl_type) {
504                                        case IFT_ETHER:
505                                        case IFT_FDDI:
506                                                fmt = ether_ntoa(
507                                                        (struct ether_addr *)
508                                                        LLADDR(&msa.dl));
509                                                break;
510                                        }
511                                        break;
512                                }
513                                if (fmt) {
514                                        printf("%*s %-17.17s",
515                                            Wflag ? 27 : 25, "", fmt);
516                                        if (msa.sa.sa_family == AF_LINK) {
517                                                printf(" %8lu", imcasts);
518                                                printf("%*s",
519                                                    bflag ? 17 : 6, "");
520                                                printf(" %8lu", omcasts);
521                                        }
522                                        putchar('\n');
523                                }
524                        }
525                }
526        }
527}
528
529struct  iftot {
530        SLIST_ENTRY(iftot) chain;
531        char    ift_name[IFNAMSIZ];     /* interface name */
532        u_long  ift_ip;                 /* input packets */
533        u_long  ift_ie;                 /* input errors */
534        u_long  ift_id;                 /* input drops */
535        u_long  ift_op;                 /* output packets */
536        u_long  ift_oe;                 /* output errors */
537        u_long  ift_co;                 /* collisions */
538        u_int   ift_dr;                 /* drops */
539        u_long  ift_ib;                 /* input bytes */
540        u_long  ift_ob;                 /* output bytes */
541};
542
543u_char  signalled;                      /* set if alarm goes off "early" */
544
545/*
546 * Print a running summary of interface statistics.
547 * Repeat display every interval1 seconds, showing statistics
548 * collected over that interval.  Assumes that interval1 is non-zero.
549 * First line printed at top of screen is always cumulative.
550 * XXX - should be rewritten to use ifmib(4).
551 */
552static void
553sidewaysintpr(int interval1, u_long off)
554{
555        struct ifnet ifnet;
556        u_long firstifnet;
557        struct ifnethead ifnethead;
558        struct itimerval interval_it;
559        struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
560        int line;
561        int oldmask, first;
562        u_long interesting_off;
563
564        if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
565                return;
566        firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
567
568        if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
569                printf("malloc failed\n");
570                exit(1);
571        }
572        memset(iftot, 0, sizeof(struct iftot));
573
574        interesting = NULL;
575        interesting_off = 0;
576        for (off = firstifnet, ip = iftot; off;) {
577                char name[IFNAMSIZ];
578
579                if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
580                        break;
581                strlcpy(name, ifnet.if_xname, sizeof(name));
582                if (interface && strcmp(name, interface) == 0) {
583                        interesting = ip;
584                        interesting_off = off;
585                }
586                snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
587                if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
588                        printf("malloc failed\n");
589                        exit(1);
590                }
591                memset(ipn, 0, sizeof(struct iftot));
592                SLIST_NEXT(ip, chain) = ipn;
593                ip = ipn;
594                off = (u_long)TAILQ_NEXT(&ifnet, if_link);
595        }
596        if (interface && interesting == NULL)
597                errx(1, "%s: unknown interface", interface);
598        if ((total = malloc(sizeof(struct iftot))) == NULL) {
599                printf("malloc failed\n");
600                exit(1);
601        }
602        memset(total, 0, sizeof(struct iftot));
603        if ((sum = malloc(sizeof(struct iftot))) == NULL) {
604                printf("malloc failed\n");
605                exit(1);
606        }
607        memset(sum, 0, sizeof(struct iftot));
608
609        (void)signal(SIGALRM, catchalarm);
610        signalled = NO;
611        interval_it.it_interval.tv_sec = interval1;
612        interval_it.it_interval.tv_usec = 0;
613        interval_it.it_value = interval_it.it_interval;
614        setitimer(ITIMER_REAL, &interval_it, NULL);
615        first = 1;
616banner:
617        printf("%17s %14s %16s", "input",
618            interesting ? interesting->ift_name : "(Total)", "output");
619        putchar('\n');
620        printf("%10s %5s %5s %10s %10s %5s %10s %5s",
621            "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
622            "colls");
623        if (dflag)
624                printf(" %5.5s", "drops");
625        putchar('\n');
626        fflush(stdout);
627        line = 0;
628loop:
629        if (interesting != NULL) {
630                ip = interesting;
631                if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
632                        printf("???\n");
633                        exit(1);
634                };
635                if (!first) {
636                        show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
637                        show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
638                        show_stat("lu", 5, ifnet.if_iqdrops - ip->ift_id, 1);
639                        show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
640                        show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
641                        show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
642                        show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
643                        show_stat("NRSlu", 5,
644                            ifnet.if_collisions - ip->ift_co, 1);
645                        if (dflag)
646                                show_stat("LSu", 5,
647                                    ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
648                }
649                ip->ift_ip = ifnet.if_ipackets;
650                ip->ift_ie = ifnet.if_ierrors;
651                ip->ift_id = ifnet.if_iqdrops;
652                ip->ift_ib = ifnet.if_ibytes;
653                ip->ift_op = ifnet.if_opackets;
654                ip->ift_oe = ifnet.if_oerrors;
655                ip->ift_ob = ifnet.if_obytes;
656                ip->ift_co = ifnet.if_collisions;
657                ip->ift_dr = ifnet.if_snd.ifq_drops;
658        } else {
659                sum->ift_ip = 0;
660                sum->ift_ie = 0;
661                sum->ift_id = 0;
662                sum->ift_ib = 0;
663                sum->ift_op = 0;
664                sum->ift_oe = 0;
665                sum->ift_ob = 0;
666                sum->ift_co = 0;
667                sum->ift_dr = 0;
668                for (off = firstifnet, ip = iftot;
669                     off && SLIST_NEXT(ip, chain) != NULL;
670                     ip = SLIST_NEXT(ip, chain)) {
671                        if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
672                                off = 0;
673                                continue;
674                        }
675                        sum->ift_ip += ifnet.if_ipackets;
676                        sum->ift_ie += ifnet.if_ierrors;
677                        sum->ift_id += ifnet.if_iqdrops;
678                        sum->ift_ib += ifnet.if_ibytes;
679                        sum->ift_op += ifnet.if_opackets;
680                        sum->ift_oe += ifnet.if_oerrors;
681                        sum->ift_ob += ifnet.if_obytes;
682                        sum->ift_co += ifnet.if_collisions;
683                        sum->ift_dr += ifnet.if_snd.ifq_drops;
684                        off = (u_long)TAILQ_NEXT(&ifnet, if_link);
685                }
686                if (!first) {
687                        show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
688                        show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
689                        show_stat("lu", 5, sum->ift_id - total->ift_id, 1);
690                        show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
691                        show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
692                        show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
693                        show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
694                        show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
695                        if (dflag)
696                                show_stat("LSu", 5,
697                                    sum->ift_dr - total->ift_dr, 1);
698                }
699                *total = *sum;
700        }
701        if (!first)
702                putchar('\n');
703        fflush(stdout);
704        if ((noutputs != 0) && (--noutputs == 0))
705                exit(0);
706#ifdef __rtems__
707        {
708        sigset_t oldmask, desired, empty;
709
710        sigemptyset(&empty);
711        sigemptyset(&desired);
712        sigaddset(&desired, SIGALRM);
713        sigprocmask(SIG_BLOCK, &desired, &oldmask);
714        while (!signalled)
715                sigsuspend(&desired);
716        signalled = NO;
717        sigprocmask(SIG_SETMASK, &oldmask, NULL);
718        }
719#else /* __rtems__ */
720        oldmask = sigblock(sigmask(SIGALRM));
721        while (!signalled)
722                sigpause(0);
723        signalled = NO;
724        sigsetmask(oldmask);
725#endif /* __rtems__ */
726        line++;
727        first = 0;
728        if (line == 21)
729                goto banner;
730        else
731                goto loop;
732        /*NOTREACHED*/
733}
734
735/*
736 * Set a flag to indicate that a signal from the periodic itimer has been
737 * caught.
738 */
739static void
740catchalarm(int signo __unused)
741{
742        signalled = YES;
743}
Note: See TracBrowser for help on using the repository browser.