]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/if.c
e3f438e7c13fb46b781857806e600a3f4f1059d4
[apple/network_cmds.git] / netstat.tproj / if.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #ifndef lint
62 /*
63 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
64 */
65 static const char rcsid[] =
66 "$Id: if.c,v 1.7 2006/01/16 04:53:59 lindak Exp $";
67 #endif /* not lint */
68
69 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
72 #include <sys/time.h>
73
74 #include <net/if.h>
75 #include <net/if_var.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_mib.h>
79 #include <net/ethernet.h>
80 #include <net/route.h>
81
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84
85 #include <arpa/inet.h>
86
87 #include <signal.h>
88 #include <stdio.h>
89 #include <string.h>
90 #include <unistd.h>
91 #include <stdlib.h>
92 #include <err.h>
93
94 #include "netstat.h"
95
96 #define YES 1
97 #define NO 0
98
99 #define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
100
101 #define NEXT_SA(p) (struct sockaddr *) \
102 ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(uint32_t)) : \
103 sizeof(uint32_t)))
104
105 static void sidewaysintpr ();
106 static void catchalarm (int);
107
108 #ifdef INET6
109 char *netname6 (struct sockaddr_in6 *, struct sockaddr *);
110 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
111 #endif
112
113 /*
114 * Display a formatted value, or a '-' in the same space.
115 */
116 static void
117 show_stat(const char *fmt, int width, u_int64_t value, short showvalue)
118 {
119 char newfmt[32];
120
121 /* Construct the format string */
122 if (showvalue) {
123 sprintf(newfmt, "%%%d%s", width, fmt);
124 printf(newfmt, value);
125 } else {
126 sprintf(newfmt, "%%%ds", width);
127 printf(newfmt, "-");
128 }
129 }
130
131 size_t
132 get_rti_info(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
133 {
134 int i;
135 size_t len = 0;
136
137 for (i = 0; i < RTAX_MAX; i++) {
138 if (addrs & (1 << i)) {
139 rti_info[i] = sa;
140 if (sa->sa_len < sizeof(struct sockaddr))
141 len += sizeof(struct sockaddr);
142 else
143 len += sa->sa_len;
144 sa = NEXT_SA(sa);
145 } else {
146 rti_info[i] = NULL;
147 }
148 }
149 return len;
150 }
151
152 static void
153 multipr(int family, char *buf, char *lim)
154 {
155 char *next;
156
157 for (next = buf; next < lim; ) {
158 struct ifma_msghdr2 *ifmam = (struct ifma_msghdr2 *)next;
159 struct sockaddr *rti_info[RTAX_MAX];
160 struct sockaddr *sa;
161 const char *fmt = 0;
162
163 next += ifmam->ifmam_msglen;
164 if (ifmam->ifmam_type == RTM_IFINFO2)
165 break;
166 else if (ifmam->ifmam_type != RTM_NEWMADDR2)
167 continue;
168 get_rti_info(ifmam->ifmam_addrs, (struct sockaddr*)(ifmam + 1), rti_info);
169 sa = rti_info[RTAX_IFA];
170
171 if (sa->sa_family != family)
172 continue;
173 switch (sa->sa_family) {
174 case AF_INET: {
175 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
176
177 fmt = routename(sin->sin_addr.s_addr);
178 break;
179 }
180 #ifdef INET6
181 case AF_INET6: {
182 struct sockaddr_in6 sin6;
183
184 memcpy(&sin6, sa, sizeof(struct sockaddr_in6));
185
186 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
187 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) {
188 sin6.sin6_scope_id = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
189 sin6.sin6_addr.s6_addr[2] = 0;
190 sin6.sin6_addr.s6_addr[3] = 0;
191 }
192
193 printf("%23s %-19.19s(refs: %d)\n", "",
194 inet_ntop(AF_INET6, &sin6.sin6_addr,
195 ntop_buf, sizeof(ntop_buf)),
196 ifmam->ifmam_refcount);
197 break;
198 }
199 #endif /* INET6 */
200 case AF_LINK: {
201 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
202
203 switch (sdl->sdl_type) {
204 case IFT_ETHER:
205 case IFT_FDDI:
206 fmt = ether_ntoa((struct ether_addr *)
207 LLADDR(sdl));
208 break;
209 }
210 break;
211 }
212 }
213 if (fmt)
214 printf("%23s %s\n", "", fmt);
215 }
216 }
217
218 /*
219 * Print a description of the network interfaces.
220 */
221 void
222 intpr(void (*pfunc)(char *))
223 {
224 u_int64_t opackets = 0;
225 u_int64_t ipackets = 0;
226 u_int64_t obytes = 0;
227 u_int64_t ibytes = 0;
228 u_int64_t oerrors = 0;
229 u_int64_t ierrors = 0;
230 u_int64_t collisions = 0;
231 uint32_t mtu = 0;
232 short timer = 0;
233 int drops = 0;
234 struct sockaddr *sa = NULL;
235 char name[32];
236 short network_layer;
237 short link_layer;
238 int mib[6];
239 char *buf = NULL, *lim, *next;
240 size_t len;
241 struct if_msghdr *ifm;
242 struct sockaddr *rti_info[RTAX_MAX];
243 unsigned int ifindex = 0;
244
245 if (interval) {
246 sidewaysintpr();
247 return;
248 }
249
250 if (interface != 0)
251 ifindex = if_nametoindex(interface);
252
253 mib[0] = CTL_NET; // networking subsystem
254 mib[1] = PF_ROUTE; // type of information
255 mib[2] = 0; // protocol (IPPROTO_xxx)
256 mib[3] = 0; // address family
257 mib[4] = NET_RT_IFLIST2; // operation
258 mib[5] = 0;
259 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
260 return;
261 if ((buf = malloc(len)) == NULL) {
262 printf("malloc failed\n");
263 exit(1);
264 }
265 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
266 if (buf)
267 free(buf);
268 return;
269 }
270 if (!pfunc) {
271 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
272 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
273 if (bflag)
274 printf(" %10.10s","Ibytes");
275 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
276 if (bflag)
277 printf(" %10.10s","Obytes");
278 printf(" %5s", "Coll");
279 if (tflag)
280 printf(" %s", "Time");
281 if (dflag)
282 printf(" %s", "Drop");
283 putchar('\n');
284 }
285 lim = buf + len;
286 for (next = buf; next < lim; ) {
287 char *cp;
288 int n, m;
289
290 network_layer = 0;
291 link_layer = 0;
292 ifm = (struct if_msghdr *)next;
293 next += ifm->ifm_msglen;
294
295 if (ifm->ifm_type == RTM_IFINFO2) {
296 struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
297 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1);
298
299 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
300 name[sdl->sdl_nlen] = 0;
301 if (interface != 0 && if2m->ifm_index != ifindex)
302 continue;
303 cp = index(name, '\0');
304
305 if (pfunc) {
306 (*pfunc)(name);
307 continue;
308 }
309
310 if ((if2m->ifm_flags & IFF_UP) == 0)
311 *cp++ = '*';
312 *cp = '\0';
313
314 /*
315 * Get the interface stats. These may get
316 * overriden below on a per-interface basis.
317 */
318 opackets = if2m->ifm_data.ifi_opackets;
319 ipackets = if2m->ifm_data.ifi_ipackets;
320 obytes = if2m->ifm_data.ifi_obytes;
321 ibytes = if2m->ifm_data.ifi_ibytes;
322 oerrors =if2m->ifm_data.ifi_oerrors;
323 ierrors = if2m->ifm_data.ifi_ierrors;
324 collisions = if2m->ifm_data.ifi_collisions;
325 timer = if2m->ifm_timer;
326 drops = if2m->ifm_snd_drops;
327 mtu = if2m->ifm_data.ifi_mtu;
328
329 get_rti_info(if2m->ifm_addrs, (struct sockaddr*)(if2m + 1), rti_info);
330 sa = rti_info[RTAX_IFP];
331 } else if (ifm->ifm_type == RTM_NEWADDR) {
332 struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
333
334 if (interface != 0 && ifam->ifam_index != ifindex)
335 continue;
336 get_rti_info(ifam->ifam_addrs, (struct sockaddr*)(ifam + 1), rti_info);
337 sa = rti_info[RTAX_IFA];
338 } else {
339 continue;
340 }
341 printf("%-5.5s %-5u ", name, mtu);
342
343 if (sa == 0) {
344 printf("%-13.13s ", "none");
345 printf("%-15.15s ", "none");
346 } else {
347 switch (sa->sa_family) {
348 case AF_UNSPEC:
349 printf("%-13.13s ", "none");
350 printf("%-15.15s ", "none");
351 break;
352
353 case AF_INET: {
354 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
355 struct sockaddr_in mask;
356
357 mask.sin_addr.s_addr = 0;
358 memcpy(&mask,
359 rti_info[RTAX_NETMASK],
360 ((struct sockaddr_in *)rti_info[RTAX_NETMASK])->sin_len);
361
362 printf("%-13.13s ",
363 netname(sin->sin_addr.s_addr & mask.sin_addr.s_addr,
364 ntohl(mask.sin_addr.s_addr)));
365
366 printf("%-15.15s ",
367 routename(sin->sin_addr.s_addr));
368
369 network_layer = 1;
370 break;
371 }
372 #ifdef INET6
373 case AF_INET6: {
374 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
375 struct sockaddr *mask = (struct sockaddr *)rti_info[RTAX_NETMASK];
376
377 printf("%-11.11s ",
378 netname6(sin6,
379 mask));
380 printf("%-17.17s ",
381 (char *)inet_ntop(AF_INET6,
382 &sin6->sin6_addr,
383 ntop_buf, sizeof(ntop_buf)));
384
385 network_layer = 1;
386 break;
387 }
388 #endif /*INET6*/
389 case AF_LINK: {
390 struct sockaddr_dl *sdl =
391 (struct sockaddr_dl *)sa;
392 char linknum[10];
393 cp = (char *)LLADDR(sdl);
394 n = sdl->sdl_alen;
395 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
396 m = printf("%-11.11s ", linknum);
397 goto hexprint;
398 }
399
400 default:
401 m = printf("(%d)", sa->sa_family);
402 for (cp = sa->sa_len + (char *)sa;
403 --cp > sa->sa_data && (*cp == 0);) {}
404 n = cp - sa->sa_data + 1;
405 cp = sa->sa_data;
406 hexprint:
407 while (--n >= 0)
408 m += printf("%02x%c", *cp++ & 0xff,
409 n > 0 ? ':' : ' ');
410 m = 30 - m;
411 while (m-- > 0)
412 putchar(' ');
413
414 link_layer = 1;
415 break;
416 }
417 }
418
419 show_stat("llu", 8, ipackets, link_layer|network_layer);
420 printf(" ");
421 show_stat("llu", 5, ierrors, link_layer);
422 printf(" ");
423 if (bflag) {
424 show_stat("llu", 10, ibytes, link_layer|network_layer);
425 printf(" ");
426 }
427 show_stat("llu", 8, opackets, link_layer|network_layer);
428 printf(" ");
429 show_stat("llu", 5, oerrors, link_layer);
430 printf(" ");
431 if (bflag) {
432 show_stat("llu", 10, obytes, link_layer|network_layer);
433 printf(" ");
434 }
435 show_stat("llu", 5, collisions, link_layer);
436 if (tflag) {
437 printf(" ");
438 show_stat("ll", 3, timer, link_layer);
439 }
440 if (dflag) {
441 printf(" ");
442 show_stat("ll", 3, drops, link_layer);
443 }
444 putchar('\n');
445
446 if (aflag)
447 multipr(sa->sa_family, next, lim);
448 }
449 }
450
451 struct iftot {
452 SLIST_ENTRY(iftot) chain;
453 char ift_name[16]; /* interface name */
454 u_int64_t ift_ip; /* input packets */
455 u_int64_t ift_ie; /* input errors */
456 u_int64_t ift_op; /* output packets */
457 u_int64_t ift_oe; /* output errors */
458 u_int64_t ift_co; /* collisions */
459 u_int64_t ift_dr; /* drops */
460 u_int64_t ift_ib; /* input bytes */
461 u_int64_t ift_ob; /* output bytes */
462 };
463
464 u_char signalled; /* set if alarm goes off "early" */
465
466 /*
467 * Print a running summary of interface statistics.
468 * Repeat display every interval seconds, showing statistics
469 * collected over that interval. Assumes that interval is non-zero.
470 * First line printed at top of screen is always cumulative.
471 * XXX - should be rewritten to use ifmib(4).
472 */
473 static void
474 sidewaysintpr()
475 {
476 struct iftot *total, *sum, *interesting;
477 register int line;
478 int first;
479 int name[6];
480 size_t len;
481 unsigned int ifcount, i;
482 struct ifmibdata *ifmdall = 0;
483 int interesting_row;
484 sigset_t sigset, oldsigset;
485 struct itimerval timer_interval;
486
487
488 /* Common OID prefix */
489 name[0] = CTL_NET;
490 name[1] = PF_LINK;
491 name[2] = NETLINK_GENERIC;
492
493 len = sizeof(int);
494 name[3] = IFMIB_SYSTEM;
495 name[4] = IFMIB_IFCOUNT;
496 if (sysctl(name, 5, &ifcount, &len, 0, 0) == 1)
497 err(1, "sysctl IFMIB_IFCOUNT");
498
499 len = ifcount * sizeof(struct ifmibdata);
500 ifmdall = malloc(len);
501 if (ifmdall == 0)
502 err(1, "malloc failed");
503 name[3] = IFMIB_IFALLDATA;
504 name[4] = 0;
505 name[5] = IFDATA_GENERAL;
506 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
507 err(1, "sysctl IFMIB_IFALLDATA");
508
509 interesting = NULL;
510 interesting_row = 0;
511 for (i = 0; i < ifcount; i++) {
512 struct ifmibdata *ifmd = ifmdall + i;
513
514 if (interface && strcmp(ifmd->ifmd_name, interface) == 0) {
515 if ((interesting = calloc(ifcount, sizeof(struct iftot))) == NULL)
516 err(1, "malloc failed");
517 interesting_row = i + 1;
518 snprintf(interesting->ift_name, 16, "(%s)", ifmd->ifmd_name);;
519 }
520 }
521 if ((total = calloc(1, sizeof(struct iftot))) == NULL)
522 err(1, "malloc failed");
523
524 if ((sum = calloc(1, sizeof(struct iftot))) == NULL)
525 err(1, "malloc failed");
526
527 /* create a timer that fires repeatedly every interval seconds */
528 timer_interval.it_value.tv_sec = interval;
529 timer_interval.it_value.tv_usec = 0;
530 timer_interval.it_interval.tv_sec = interval;
531 timer_interval.it_interval.tv_usec = 0;
532 (void)signal(SIGALRM, catchalarm);
533 signalled = NO;
534 (void)setitimer(ITIMER_REAL, &timer_interval, NULL);
535 first = 1;
536 banner:
537 printf("%17s %14s %16s", "input",
538 interesting ? interesting->ift_name : "(Total)", "output");
539 putchar('\n');
540 printf("%10s %5s %10s %10s %5s %10s %5s",
541 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
542 if (dflag)
543 printf(" %5.5s", "drops");
544 putchar('\n');
545 fflush(stdout);
546 line = 0;
547 loop:
548 if (interesting != NULL) {
549 struct ifmibdata ifmd;
550
551 len = sizeof(struct ifmibdata);
552 name[3] = IFMIB_IFDATA;
553 name[4] = interesting_row;
554 name[5] = IFDATA_GENERAL;
555 if (sysctl(name, 6, &ifmd, &len, (void *)0, 0) == -1)
556 err(1, "sysctl IFDATA_GENERAL %d", interesting_row);
557
558 if (!first) {
559 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
560 ifmd.ifmd_data.ifi_ipackets - interesting->ift_ip,
561 ifmd.ifmd_data.ifi_ierrors - interesting->ift_ie,
562 ifmd.ifmd_data.ifi_ibytes - interesting->ift_ib,
563 ifmd.ifmd_data.ifi_opackets - interesting->ift_op,
564 ifmd.ifmd_data.ifi_oerrors - interesting->ift_oe,
565 ifmd.ifmd_data.ifi_obytes - interesting->ift_ob,
566 ifmd.ifmd_data.ifi_collisions - interesting->ift_co);
567 if (dflag)
568 printf(" %5llu", ifmd.ifmd_snd_drops - interesting->ift_dr);
569 }
570 interesting->ift_ip = ifmd.ifmd_data.ifi_ipackets;
571 interesting->ift_ie = ifmd.ifmd_data.ifi_ierrors;
572 interesting->ift_ib = ifmd.ifmd_data.ifi_ibytes;
573 interesting->ift_op = ifmd.ifmd_data.ifi_opackets;
574 interesting->ift_oe = ifmd.ifmd_data.ifi_oerrors;
575 interesting->ift_ob = ifmd.ifmd_data.ifi_obytes;
576 interesting->ift_co = ifmd.ifmd_data.ifi_collisions;
577 interesting->ift_dr = ifmd.ifmd_snd_drops;
578 } else {
579 unsigned int latest_ifcount;
580
581 len = sizeof(int);
582 name[3] = IFMIB_SYSTEM;
583 name[4] = IFMIB_IFCOUNT;
584 if (sysctl(name, 5, &latest_ifcount, &len, 0, 0) == 1)
585 err(1, "sysctl IFMIB_IFCOUNT");
586 if (latest_ifcount > ifcount) {
587 ifcount = latest_ifcount;
588 len = ifcount * sizeof(struct ifmibdata);
589 free(ifmdall);
590 ifmdall = malloc(len);
591 if (ifmdall == 0)
592 err(1, "malloc failed");
593 } else if (latest_ifcount > ifcount) {
594 ifcount = latest_ifcount;
595 len = ifcount * sizeof(struct ifmibdata);
596 }
597 len = ifcount * sizeof(struct ifmibdata);
598 name[3] = IFMIB_IFALLDATA;
599 name[4] = 0;
600 name[5] = IFDATA_GENERAL;
601 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
602 err(1, "sysctl IFMIB_IFALLDATA");
603
604 sum->ift_ip = 0;
605 sum->ift_ie = 0;
606 sum->ift_ib = 0;
607 sum->ift_op = 0;
608 sum->ift_oe = 0;
609 sum->ift_ob = 0;
610 sum->ift_co = 0;
611 sum->ift_dr = 0;
612 for (i = 0; i < ifcount; i++) {
613 struct ifmibdata *ifmd = ifmdall + i;
614
615 sum->ift_ip += ifmd->ifmd_data.ifi_ipackets;
616 sum->ift_ie += ifmd->ifmd_data.ifi_ierrors;
617 sum->ift_ib += ifmd->ifmd_data.ifi_ibytes;
618 sum->ift_op += ifmd->ifmd_data.ifi_opackets;
619 sum->ift_oe += ifmd->ifmd_data.ifi_oerrors;
620 sum->ift_ob += ifmd->ifmd_data.ifi_obytes;
621 sum->ift_co += ifmd->ifmd_data.ifi_collisions;
622 sum->ift_dr += ifmd->ifmd_snd_drops;
623 }
624 if (!first) {
625 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
626 sum->ift_ip - total->ift_ip,
627 sum->ift_ie - total->ift_ie,
628 sum->ift_ib - total->ift_ib,
629 sum->ift_op - total->ift_op,
630 sum->ift_oe - total->ift_oe,
631 sum->ift_ob - total->ift_ob,
632 sum->ift_co - total->ift_co);
633 if (dflag)
634 printf(" %5llu", sum->ift_dr - total->ift_dr);
635 }
636 *total = *sum;
637 }
638 if (!first)
639 putchar('\n');
640 fflush(stdout);
641 sigemptyset(&sigset);
642 sigaddset(&sigset, SIGALRM);
643 (void)sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
644 if (!signalled) {
645 sigemptyset(&sigset);
646 sigsuspend(&sigset);
647 }
648 (void)sigprocmask(SIG_SETMASK, &oldsigset, NULL);
649
650 signalled = NO;
651 line++;
652 first = 0;
653 if (line == 21)
654 goto banner;
655 else
656 goto loop;
657 /*NOTREACHED*/
658 }
659
660 void
661 intervalpr(void (*pr)(uint32_t, char *, int), uint32_t off, char *name , int af)
662 {
663 struct itimerval timer_interval;
664 sigset_t sigset, oldsigset;
665
666 /* create a timer that fires repeatedly every interval seconds */
667 timer_interval.it_value.tv_sec = interval;
668 timer_interval.it_value.tv_usec = 0;
669 timer_interval.it_interval.tv_sec = interval;
670 timer_interval.it_interval.tv_usec = 0;
671 (void) signal(SIGALRM, catchalarm);
672 signalled = NO;
673 (void) setitimer(ITIMER_REAL, &timer_interval, NULL);
674
675 for (;;) {
676 pr(off, name, af);
677
678 fflush(stdout);
679 sigemptyset(&sigset);
680 sigaddset(&sigset, SIGALRM);
681 (void) sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
682 if (!signalled) {
683 sigemptyset(&sigset);
684 sigsuspend(&sigset);
685 }
686 (void) sigprocmask(SIG_SETMASK, &oldsigset, NULL);
687 signalled = NO;
688 }
689 }
690
691 /*
692 * Called if an interval expires before sidewaysintpr has completed a loop.
693 * Sets a flag to not wait for the alarm.
694 */
695 static void
696 catchalarm(int signo )
697 {
698 signalled = YES;
699 }