Deleted Added
full compact
main.c (171135) main.c (171465)
1/*
2 * Copyright (c) 1983, 1988, 1993
3 * Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 30 unchanged lines hidden (view full) ---

39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
43#endif /* not lint */
44#endif
45
46#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1983, 1988, 1993
3 * Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 30 unchanged lines hidden (view full) ---

39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
43#endif /* not lint */
44#endif
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/usr.bin/netstat/main.c 171135 2007-07-01 12:08:08Z gnn $");
47__FBSDID("$FreeBSD: head/usr.bin/netstat/main.c 171465 2007-07-16 17:15:55Z jhb $");
48
49#include <sys/param.h>
50#include <sys/file.h>
51#include <sys/protosw.h>
52#include <sys/socket.h>
48
49#include <sys/param.h>
50#include <sys/file.h>
51#include <sys/protosw.h>
52#include <sys/socket.h>
53#include <sys/socketvar.h>
53
54#include <netinet/in.h>
55
56#include <netgraph/ng_socket.h>
57
58#include <ctype.h>
59#include <err.h>
60#include <errno.h>

--- 75 unchanged lines hidden (view full) ---

136#define N_MBLO 31
137 { .n_name = "_mbuf_lowm" },
138#define N_CLLO 32
139 { .n_name = "_clust_lowm" },
140#define N_CARPSTAT 33
141 { .n_name = "_carpstats" },
142#define N_PFSYNCSTAT 34
143 { .n_name = "_pfsyncstats" },
54
55#include <netinet/in.h>
56
57#include <netgraph/ng_socket.h>
58
59#include <ctype.h>
60#include <err.h>
61#include <errno.h>

--- 75 unchanged lines hidden (view full) ---

137#define N_MBLO 31
138 { .n_name = "_mbuf_lowm" },
139#define N_CLLO 32
140 { .n_name = "_clust_lowm" },
141#define N_CARPSTAT 33
142 { .n_name = "_carpstats" },
143#define N_PFSYNCSTAT 34
144 { .n_name = "_pfsyncstats" },
144#define N_AHSTAT 36
145#define N_AHSTAT 35
145 { .n_name = "_ahstat" },
146 { .n_name = "_ahstat" },
146#define N_ESPSTAT 37
147#define N_ESPSTAT 36
147 { .n_name = "_espstat" },
148 { .n_name = "_espstat" },
148#define N_IPCOMPSTAT 38
149#define N_IPCOMPSTAT 37
149 { .n_name = "_ipcompstat" },
150 { .n_name = "_ipcompstat" },
151#define N_TCPSTAT 38
152 { .n_name = "_tcpstat" },
153#define N_UDPSTAT 39
154 { .n_name = "_udpstat" },
155#define N_IPSTAT 40
156 { .n_name = "_ipstat" },
157#define N_ICMPSTAT 41
158 { .n_name = "_icmpstat" },
159#define N_IGMPSTAT 42
160 { .n_name = "_igmpstat" },
161#define N_PIMSTAT 43
162 { .n_name = "_pimstat" },
163#define N_TCBINFO 44
164 { .n_name = "_tcbinfo" },
165#define N_UDBINFO 45
166 { .n_name = "_udbinfo" },
167#define N_DIVCBINFO 46
168 { .n_name = "_divcbinfo" },
169#define N_RIPCBINFO 47
170 { .n_name = "_ripcbinfo" },
171#define N_UNP_COUNT 48
172 { .n_name = "_unp_count" },
173#define N_UNP_GENCNT 49
174 { .n_name = "_unp_gencnt" },
175#define N_UNP_DHEAD 50
176 { .n_name = "_unp_dhead" },
177#define N_UNP_SHEAD 51
178 { .n_name = "_unp_shead" },
179#define N_RIP6STAT 52
180 { .n_name = "_rip6stat" },
181#define N_SCTPSTAT 53
182 { .n_name = "_sctpstat" },
150 { .n_name = NULL },
151};
152
153struct protox {
183 { .n_name = NULL },
184};
185
186struct protox {
154 u_char pr_index; /* index into nlist of cb head */
155 u_char pr_sindex; /* index into nlist of stat block */
187 int pr_index; /* index into nlist of cb head */
188 int pr_sindex; /* index into nlist of stat block */
156 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
189 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
157 void (*pr_cblocks)(u_long, const char *, int);
190 void (*pr_cblocks)(u_long, const char *, int, int);
158 /* control blocks printing routine */
191 /* control blocks printing routine */
159 void (*pr_stats)(u_long, const char *, int);
192 void (*pr_stats)(u_long, const char *, int, int);
160 /* statistics printing routine */
161 void (*pr_istats)(char *); /* per/if statistics printing routine */
162 const char *pr_name; /* well-known name */
163 u_long pr_usesysctl; /* non-zero if we use sysctl, not kvm */
193 /* statistics printing routine */
194 void (*pr_istats)(char *); /* per/if statistics printing routine */
195 const char *pr_name; /* well-known name */
196 u_long pr_usesysctl; /* non-zero if we use sysctl, not kvm */
197 int pr_protocol;
164} protox[] = {
198} protox[] = {
165 { -1, -1, 1, protopr,
166 tcp_stats, NULL, "tcp", IPPROTO_TCP },
167 { -1, -1, 1, protopr,
168 udp_stats, NULL, "udp", IPPROTO_UDP },
199 { N_TCBINFO, N_TCPSTAT, 1, protopr,
200 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
201 { N_UDBINFO, N_UDPSTAT, 1, protopr,
202 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
169#ifdef SCTP
203#ifdef SCTP
170 { -1, -1, 1, sctp_protopr,
171 sctp_stats, NULL, "sctp", IPPROTO_SCTP },
204 { -1, N_SCTPSTAT, 1, sctp_protopr,
205 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
172#endif
206#endif
173 { -1, -1, 1, protopr,
174 NULL, NULL, "divert",IPPROTO_DIVERT },
175 { -1, -1, 1, protopr,
176 ip_stats, NULL, "ip", IPPROTO_RAW },
177 { -1, -1, 1, protopr,
178 icmp_stats, NULL, "icmp", IPPROTO_ICMP },
179 { -1, -1, 1, protopr,
180 igmp_stats, NULL, "igmp", IPPROTO_IGMP },
207 { N_DIVCBINFO, -1, 1, protopr,
208 NULL, NULL, "divert", 1, IPPROTO_DIVERT },
209 { N_RIPCBINFO, N_IPSTAT, 1, protopr,
210 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
211 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
212 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
213 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
214 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
181#ifdef IPSEC
182 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */
215#ifdef IPSEC
216 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */
183 ipsec_stats, NULL, "ipsec", 0},
217 ipsec_stats, NULL, "ipsec", 0, 0},
184 { -1, N_AHSTAT, 1, NULL,
218 { -1, N_AHSTAT, 1, NULL,
185 ah_stats, NULL, "ah", 0},
219 ah_stats, NULL, "ah", 0, 0},
186 { -1, N_ESPSTAT, 1, NULL,
220 { -1, N_ESPSTAT, 1, NULL,
187 esp_stats, NULL, "esp", 0},
221 esp_stats, NULL, "esp", 0, 0},
188 { -1, N_IPCOMPSTAT, 1, NULL,
222 { -1, N_IPCOMPSTAT, 1, NULL,
189 ipcomp_stats, NULL, "ipcomp", 0},
223 ipcomp_stats, NULL, "ipcomp", 0, 0},
190#endif
224#endif
191 { -1, -1, 1, protopr,
192 pim_stats, NULL, "pim", IPPROTO_PIM },
193 { -1, N_CARPSTAT, 1, 0,
194 carp_stats, NULL, "carp", 0},
195 { -1, -1, 1, NULL,
196 pfsync_stats, NULL, "pfsync", 1},
225 { N_RIPCBINFO, N_PIMSTAT, 1, protopr,
226 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
227 { -1, N_CARPSTAT, 1, NULL,
228 carp_stats, NULL, "carp", 1, 0 },
229 { -1, N_PFSYNCSTAT, 1, NULL,
230 pfsync_stats, NULL, "pfsync", 1, 0 },
197 { -1, -1, 0, NULL,
231 { -1, -1, 0, NULL,
198 NULL, NULL, NULL, 0 }
232 NULL, NULL, NULL, 0, 0 }
199};
200
201#ifdef INET6
202struct protox ip6protox[] = {
233};
234
235#ifdef INET6
236struct protox ip6protox[] = {
203 { -1, -1, 1, protopr,
204 tcp_stats, NULL, "tcp", IPPROTO_TCP },
205 { -1, -1, 1, protopr,
206 udp_stats, NULL, "udp", IPPROTO_UDP },
207 { -1, N_IP6STAT, 1, protopr,
208 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW },
209 { -1, N_ICMP6STAT, 1, protopr,
210 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 },
237 { N_TCBINFO, N_TCPSTAT, 1, protopr,
238 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
239 { N_UDBINFO, N_UDPSTAT, 1, protopr,
240 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
241 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
242 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
243 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
244 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
211#ifdef IPSEC
212 { -1, N_IPSEC6STAT, 1, NULL,
245#ifdef IPSEC
246 { -1, N_IPSEC6STAT, 1, NULL,
213 ipsec_stats, NULL, "ipsec6",0 },
247 ipsec_stats, NULL, "ipsec6", 0, 0 },
214#endif
215#ifdef notyet
216 { -1, N_PIM6STAT, 1, NULL,
248#endif
249#ifdef notyet
250 { -1, N_PIM6STAT, 1, NULL,
217 pim6_stats, NULL, "pim6", 0 },
251 pim6_stats, NULL, "pim6", 1, 0 },
218#endif
252#endif
219 { -1, -1, 1, NULL,
220 rip6_stats, NULL, "rip6", 0 },
253 { -1, N_RIP6STAT, 1, NULL,
254 rip6_stats, NULL, "rip6", 1, 0 },
221 { -1, -1, 0, NULL,
255 { -1, -1, 0, NULL,
222 NULL, NULL, NULL, 0 }
256 NULL, NULL, NULL, 0, 0 }
223};
224#endif /*INET6*/
225
226#ifdef IPSEC
227struct protox pfkeyprotox[] = {
228 { -1, N_PFKEYSTAT, 1, NULL,
257};
258#endif /*INET6*/
259
260#ifdef IPSEC
261struct protox pfkeyprotox[] = {
262 { -1, N_PFKEYSTAT, 1, NULL,
229 pfkey_stats, NULL, "pfkey", 0 },
263 pfkey_stats, NULL, "pfkey", 0, 0 },
230 { -1, -1, 0, NULL,
264 { -1, -1, 0, NULL,
231 NULL, NULL, NULL, 0 }
265 NULL, NULL, NULL, 0, 0 }
232};
233#endif
234
235struct protox atalkprotox[] = {
236 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
266};
267#endif
268
269struct protox atalkprotox[] = {
270 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
237 ddp_stats, NULL, "ddp", 0 },
271 ddp_stats, NULL, "ddp", 0, 0 },
238 { -1, -1, 0, NULL,
272 { -1, -1, 0, NULL,
239 NULL, NULL, NULL, 0 }
273 NULL, NULL, NULL, 0, 0 }
240};
241
242struct protox netgraphprotox[] = {
243 { N_NGSOCKS, -1, 1, netgraphprotopr,
274};
275
276struct protox netgraphprotox[] = {
277 { N_NGSOCKS, -1, 1, netgraphprotopr,
244 NULL, NULL, "ctrl", 0 },
278 NULL, NULL, "ctrl", 0, 0 },
245 { N_NGSOCKS, -1, 1, netgraphprotopr,
279 { N_NGSOCKS, -1, 1, netgraphprotopr,
246 NULL, NULL, "data", 0 },
280 NULL, NULL, "data", 0, 0 },
247 { -1, -1, 0, NULL,
281 { -1, -1, 0, NULL,
248 NULL, NULL, NULL, 0 }
282 NULL, NULL, NULL, 0, 0 }
249};
250
251#ifdef IPX
252struct protox ipxprotox[] = {
253 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
283};
284
285#ifdef IPX
286struct protox ipxprotox[] = {
287 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
254 ipx_stats, NULL, "ipx", 0 },
288 ipx_stats, NULL, "ipx", 0, 0 },
255 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
289 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
256 spx_stats, NULL, "spx", 0 },
290 spx_stats, NULL, "spx", 0, 0 },
257 { -1, -1, 0, NULL,
291 { -1, -1, 0, NULL,
258 NULL, NULL, 0, 0 }
292 NULL, NULL, 0, 0, 0 }
259};
260#endif
261
262struct protox *protoprotox[] = {
263 protox,
264#ifdef INET6
265 ip6protox,
266#endif

--- 33 unchanged lines hidden (view full) ---

300int zflag; /* zero stats */
301
302int interval; /* repeat interval for i/f stats */
303
304char *interface; /* desired i/f for stats, or NULL for all i/fs */
305int unit; /* unit number for above */
306
307int af; /* address family */
293};
294#endif
295
296struct protox *protoprotox[] = {
297 protox,
298#ifdef INET6
299 ip6protox,
300#endif

--- 33 unchanged lines hidden (view full) ---

334int zflag; /* zero stats */
335
336int interval; /* repeat interval for i/f stats */
337
338char *interface; /* desired i/f for stats, or NULL for all i/fs */
339int unit; /* unit number for above */
340
341int af; /* address family */
342int live; /* true if we are examining a live system */
308
309int
310main(int argc, char *argv[])
311{
312 struct protox *tp = NULL; /* for printing cblocks & stats */
313 int ch;
314
315 af = AF_UNSPEC;

--- 132 unchanged lines hidden (view full) ---

448 }
449 }
450#endif
451
452 /*
453 * Discard setgid privileges if not the running kernel so that bad
454 * guys can't print interesting stuff from kernel memory.
455 */
343
344int
345main(int argc, char *argv[])
346{
347 struct protox *tp = NULL; /* for printing cblocks & stats */
348 int ch;
349
350 af = AF_UNSPEC;

--- 132 unchanged lines hidden (view full) ---

483 }
484 }
485#endif
486
487 /*
488 * Discard setgid privileges if not the running kernel so that bad
489 * guys can't print interesting stuff from kernel memory.
490 */
456 if (nlistf != NULL || memf != NULL)
491 live = (nlistf == NULL && memf == NULL);
492 if (!live)
457 setgid(getgid());
458
459 if (Bflag) {
493 setgid(getgid());
494
495 if (Bflag) {
496 if (!live)
497 usage();
460 bpf_stats(interface);
461 exit(0);
462 }
463 if (mflag) {
464 if (memf != NULL) {
498 bpf_stats(interface);
499 exit(0);
500 }
501 if (mflag) {
502 if (memf != NULL) {
465 if (kread(0, 0, 0) == 0)
503 if (kread(0, NULL, 0) == 0)
466 mbpr(kvmd, nl[N_MBSTAT].n_value);
467 } else
468 mbpr(NULL, 0);
469 exit(0);
470 }
471#if 0
472 /*
473 * Keep file descriptors open to avoid overhead
474 * of open/close on each call to get* routines.
475 */
476 sethostent(1);
477 setnetent(1);
478#else
479 /*
480 * This does not make sense any more with DNS being default over
481 * the files. Doing a setXXXXent(1) causes a tcp connection to be
482 * used for the queries, which is slower.
483 */
484#endif
504 mbpr(kvmd, nl[N_MBSTAT].n_value);
505 } else
506 mbpr(NULL, 0);
507 exit(0);
508 }
509#if 0
510 /*
511 * Keep file descriptors open to avoid overhead
512 * of open/close on each call to get* routines.
513 */
514 sethostent(1);
515 setnetent(1);
516#else
517 /*
518 * This does not make sense any more with DNS being default over
519 * the files. Doing a setXXXXent(1) causes a tcp connection to be
520 * used for the queries, which is slower.
521 */
522#endif
523 kread(0, NULL, 0);
485 if (iflag && !sflag) {
524 if (iflag && !sflag) {
486 kread(0, 0, 0);
487 intpr(interval, nl[N_IFNET].n_value, NULL);
488 exit(0);
489 }
490 if (rflag) {
525 intpr(interval, nl[N_IFNET].n_value, NULL);
526 exit(0);
527 }
528 if (rflag) {
491 kread(0, 0, 0);
492 if (sflag)
493 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
494 else
495 routepr(nl[N_RTREE].n_value);
496 exit(0);
497 }
498 if (gflag) {
529 if (sflag)
530 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
531 else
532 routepr(nl[N_RTREE].n_value);
533 exit(0);
534 }
535 if (gflag) {
499 kread(0, 0, 0);
500 if (sflag) {
501 if (af == AF_INET || af == AF_UNSPEC)
502 mrt_stats(nl[N_MRTSTAT].n_value);
503#ifdef INET6
504 if (af == AF_INET6 || af == AF_UNSPEC)
505 mrt6_stats(nl[N_MRT6STAT].n_value);
506#endif
507 } else {

--- 5 unchanged lines hidden (view full) ---

513 mroute6pr(nl[N_MF6CTABLE].n_value,
514 nl[N_MIF6TABLE].n_value);
515#endif
516 }
517 ifmalist_dump();
518 exit(0);
519 }
520
536 if (sflag) {
537 if (af == AF_INET || af == AF_UNSPEC)
538 mrt_stats(nl[N_MRTSTAT].n_value);
539#ifdef INET6
540 if (af == AF_INET6 || af == AF_UNSPEC)
541 mrt6_stats(nl[N_MRT6STAT].n_value);
542#endif
543 } else {

--- 5 unchanged lines hidden (view full) ---

549 mroute6pr(nl[N_MF6CTABLE].n_value,
550 nl[N_MIF6TABLE].n_value);
551#endif
552 }
553 ifmalist_dump();
554 exit(0);
555 }
556
521 kread(0, 0, 0);
522 if (tp) {
523 printproto(tp, tp->pr_name);
524 exit(0);
525 }
526 if (af == AF_INET || af == AF_UNSPEC)
527 for (tp = protox; tp->pr_name; tp++)
528 printproto(tp, tp->pr_name);
529#ifdef INET6
530 if (af == AF_INET6 || af == AF_UNSPEC)
531 for (tp = ip6protox; tp->pr_name; tp++)
532 printproto(tp, tp->pr_name);
533#endif /*INET6*/
534#ifdef IPSEC
535 if (af == PF_KEY || af == AF_UNSPEC)
536 for (tp = pfkeyprotox; tp->pr_name; tp++)
537 printproto(tp, tp->pr_name);
538#endif /*IPSEC*/
539#ifdef IPX
540 if (af == AF_IPX || af == AF_UNSPEC) {
557 if (tp) {
558 printproto(tp, tp->pr_name);
559 exit(0);
560 }
561 if (af == AF_INET || af == AF_UNSPEC)
562 for (tp = protox; tp->pr_name; tp++)
563 printproto(tp, tp->pr_name);
564#ifdef INET6
565 if (af == AF_INET6 || af == AF_UNSPEC)
566 for (tp = ip6protox; tp->pr_name; tp++)
567 printproto(tp, tp->pr_name);
568#endif /*INET6*/
569#ifdef IPSEC
570 if (af == PF_KEY || af == AF_UNSPEC)
571 for (tp = pfkeyprotox; tp->pr_name; tp++)
572 printproto(tp, tp->pr_name);
573#endif /*IPSEC*/
574#ifdef IPX
575 if (af == AF_IPX || af == AF_UNSPEC) {
541 kread(0, 0, 0);
542 for (tp = ipxprotox; tp->pr_name; tp++)
543 printproto(tp, tp->pr_name);
544 }
545#endif /* IPX */
546 if (af == AF_APPLETALK || af == AF_UNSPEC)
547 for (tp = atalkprotox; tp->pr_name; tp++)
548 printproto(tp, tp->pr_name);
549 if (af == AF_NETGRAPH || af == AF_UNSPEC)
550 for (tp = netgraphprotox; tp->pr_name; tp++)
551 printproto(tp, tp->pr_name);
552 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
576 for (tp = ipxprotox; tp->pr_name; tp++)
577 printproto(tp, tp->pr_name);
578 }
579#endif /* IPX */
580 if (af == AF_APPLETALK || af == AF_UNSPEC)
581 for (tp = atalkprotox; tp->pr_name; tp++)
582 printproto(tp, tp->pr_name);
583 if (af == AF_NETGRAPH || af == AF_UNSPEC)
584 for (tp = netgraphprotox; tp->pr_name; tp++)
585 printproto(tp, tp->pr_name);
586 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
553 unixpr();
587 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
588 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
554 exit(0);
555}
556
557/*
558 * Print out protocol statistics or control blocks (per sflag).
559 * If the interface was not specifically requested, and the symbol
560 * is not in the namelist, ignore this one.
561 */
562static void
563printproto(tp, name)
564 struct protox *tp;
565 const char *name;
566{
589 exit(0);
590}
591
592/*
593 * Print out protocol statistics or control blocks (per sflag).
594 * If the interface was not specifically requested, and the symbol
595 * is not in the namelist, ignore this one.
596 */
597static void
598printproto(tp, name)
599 struct protox *tp;
600 const char *name;
601{
567 void (*pr)(u_long, const char *, int);
602 void (*pr)(u_long, const char *, int, int);
568 u_long off;
569
570 if (sflag) {
571 if (iflag) {
572 if (tp->pr_istats)
573 intpr(interval, nl[N_IFNET].n_value,
574 tp->pr_istats);
575 else if (pflag)
576 printf("%s: no per-interface stats routine\n",
577 tp->pr_name);
578 return;
603 u_long off;
604
605 if (sflag) {
606 if (iflag) {
607 if (tp->pr_istats)
608 intpr(interval, nl[N_IFNET].n_value,
609 tp->pr_istats);
610 else if (pflag)
611 printf("%s: no per-interface stats routine\n",
612 tp->pr_name);
613 return;
579 }
580 else {
614 } else {
581 pr = tp->pr_stats;
582 if (!pr) {
583 if (pflag)
584 printf("%s: no stats routine\n",
585 tp->pr_name);
586 return;
615 pr = tp->pr_stats;
616 if (!pr) {
617 if (pflag)
618 printf("%s: no stats routine\n",
619 tp->pr_name);
620 return;
587 }
588 off = tp->pr_usesysctl ? tp->pr_usesysctl
589 : nl[tp->pr_sindex].n_value;
621 }
622 if (tp->pr_usesysctl && live)
623 off = 0;
624 else if (tp->pr_sindex < 0) {
625 if (pflag)
626 printf(
627 "%s: stats routine doesn't work on cores\n",
628 tp->pr_name);
629 return;
630 } else
631 off = nl[tp->pr_sindex].n_value;
590 }
591 } else {
592 pr = tp->pr_cblocks;
593 if (!pr) {
594 if (pflag)
595 printf("%s: no PCB routine\n", tp->pr_name);
596 return;
597 }
632 }
633 } else {
634 pr = tp->pr_cblocks;
635 if (!pr) {
636 if (pflag)
637 printf("%s: no PCB routine\n", tp->pr_name);
638 return;
639 }
598 off = tp->pr_usesysctl ? tp->pr_usesysctl
599 : nl[tp->pr_index].n_value;
640 if (tp->pr_usesysctl && live)
641 off = 0;
642 else if (tp->pr_index < 0) {
643 if (pflag)
644 printf(
645 "%s: PCB routine doesn't work on cores\n",
646 tp->pr_name);
647 return;
648 } else
649 off = nl[tp->pr_index].n_value;
600 }
650 }
601 if (pr != NULL && (off || af != AF_UNSPEC))
602 (*pr)(off, name, af);
651 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
652 af != AF_UNSPEC))
653 (*pr)(off, name, af, tp->pr_protocol);
603}
604
605/*
606 * Read kernel memory, return 0 on success.
607 */
608int
654}
655
656/*
657 * Read kernel memory, return 0 on success.
658 */
659int
609kread(u_long addr, char *buf, int size)
660kread(u_long addr, void *buf, size_t size)
610{
661{
611 if (kvmd == 0) {
612 /*
613 * XXX.
614 */
615 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
662 char errbuf[_POSIX2_LINE_MAX];
663
664 if (kvmd == NULL) {
665 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
616 setgid(getgid());
617 if (kvmd != NULL) {
618 if (kvm_nlist(kvmd, nl) < 0) {
666 setgid(getgid());
667 if (kvmd != NULL) {
668 if (kvm_nlist(kvmd, nl) < 0) {
619 if(nlistf)
669 if (nlistf)
620 errx(1, "%s: kvm_nlist: %s", nlistf,
621 kvm_geterr(kvmd));
622 else
623 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
624 }
625
626 if (nl[0].n_type == 0) {
670 errx(1, "%s: kvm_nlist: %s", nlistf,
671 kvm_geterr(kvmd));
672 else
673 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
674 }
675
676 if (nl[0].n_type == 0) {
627 if(nlistf)
677 if (nlistf)
628 errx(1, "%s: no namelist", nlistf);
629 else
630 errx(1, "no namelist");
631 }
632 } else {
678 errx(1, "%s: no namelist", nlistf);
679 else
680 errx(1, "no namelist");
681 }
682 } else {
633 warnx("kvm not available");
683 warnx("kvm not available: %s", errbuf);
634 return(-1);
635 }
636 }
637 if (!buf)
638 return (0);
684 return(-1);
685 }
686 }
687 if (!buf)
688 return (0);
639 if (kvm_read(kvmd, addr, buf, size) != size) {
689 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
640 warnx("%s", kvm_geterr(kvmd));
690 warnx("%s", kvm_geterr(kvmd));
691 abort();
641 return (-1);
642 }
643 return (0);
644}
645
646const char *
647plural(uintmax_t n)
648{

--- 80 unchanged lines hidden ---
692 return (-1);
693 }
694 return (0);
695}
696
697const char *
698plural(uintmax_t n)
699{

--- 80 unchanged lines hidden ---