/* * Print address family header before a section of the routing table. */ void pr_family(int af1) { const char *afname; switch (af1) { case AF_INET: afname = "Internet"; break; #ifdef INET6 case AF_INET6: afname = "Internet6"; break; #endif /*INET6*/ case AF_ISO: afname = "ISO"; break; case AF_CCITT: afname = "X.25"; break; case AF_NETGRAPH: afname = "Netgraph"; break; default: afname = NULL; break; } if (afname) xo_emit("\n{k:address-family/%s}:\n", afname); else xo_emit("\n{L:Protocol Family} {k:address-family/%d}:\n", af1); }
static void printtime(const char *field, time_t ftime) { char longstring[80]; char fmt[BUFSIZ]; static time_t now = 0; const char *format; static int d_first = -1; if (d_first < 0) d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); if (now == 0) now = time(NULL); #define SIXMONTHS ((365 / 2) * 86400) if (f_timeformat) /* user specified format */ format = f_timeformat; else if (f_sectime) /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */ format = d_first ? "%e %b %T %Y" : "%b %e %T %Y"; else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS) /* mmm dd hh:mm || dd mmm hh:mm */ format = d_first ? "%e %b %R" : "%b %e %R"; else /* mmm dd yyyy || dd mmm yyyy */ format = d_first ? "%e %b %Y" : "%b %e %Y"; strftime(longstring, sizeof(longstring), format, localtime(&ftime)); snprintf(fmt, sizeof(fmt), "{d:%s/%%hs} ", field); xo_attr("value", "%ld", (long) ftime); xo_emit(fmt, longstring); snprintf(fmt, sizeof(fmt), "{en:%s/%%ld} ", field); xo_emit(fmt, (long) ftime); }
static int p_sockaddr(const char *name, struct sockaddr *sa, struct sockaddr *mask, int flags, int width) { const char *cp; char buf[128]; int protrusion; cp = fmt_sockaddr(sa, mask, flags); if (width < 0) { snprintf(buf, sizeof(buf), "{:%s/%%s} ", name); xo_emit(buf, cp); protrusion = 0; } else { if (Wflag != 0 || numeric_addr) { snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ", -width, name); xo_emit(buf, cp); protrusion = strlen(cp) - width; if (protrusion < 0) protrusion = 0; } else { snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ", -width, name); xo_emit(buf, width, cp); protrusion = 0; } } return (protrusion); }
void procstat_env(struct procstat *procstat, struct kinfo_proc *kipp) { int i; char **envs; if (!hflag) { xo_emit("{T:/%5s %-16s %-53s}\n", "PID", "COMM", "ENVIRONMENT"); } envs = procstat_getenvv(procstat, kipp, 0); xo_emit("{k:process_id/%5d/%d} {:command/%-16s/%s}", kipp->ki_pid, kipp->ki_comm); if (envs == NULL) { xo_emit(" {d:env/-}\n"); return; } xo_open_list("environment"); for (i = 0; envs[i] != NULL; i++) xo_emit(" {l:env/%s}", envs[i]); xo_close_list("environment"); xo_emit("\n"); }
/* * Dump IPSEC statistics structure. */ static void ipsec_hist_new(const uint64_t *hist, size_t histmax, const struct val2str *name, const char *title, const char *cname) { int first; size_t proto; const struct val2str *p; first = 1; for (proto = 0; proto < histmax; proto++) { if (hist[proto] <= 0) continue; if (first) { xo_open_list(cname); xo_emit("\t{T:/%s histogram}:\n", title); first = 0; } xo_open_instance(cname); for (p = name; p && p->str; p++) { if (p->val == (int)proto) break; } if (p && p->str) { xo_emit("\t\t{k:name}: {:count/%ju}\n", p->str, (uintmax_t)hist[proto]); } else { xo_emit("\t\t#{k:name/%lu}: {:count/%ju}\n", (unsigned long)proto, (uintmax_t)hist[proto]); } xo_close_instance(cname); } if (!first) xo_close_list(cname); }
void inet6print(const char *container, struct in6_addr *in6, int port, const char *proto, int numeric) { struct servent *sp = 0; char line[80], *cp; int width; if (container) xo_open_container(container); sprintf(line, "%.*s.", Wflag ? 39 : (Aflag && !numeric) ? 12 : 16, inet6name(in6)); cp = strchr(line, '\0'); if (!numeric && port) GETSERVBYPORT6(port, proto, sp); if (sp || port == 0) sprintf(cp, "%.15s", sp ? sp->s_name : "*"); else sprintf(cp, "%d", ntohs((u_short)port)); width = Wflag ? 45 : Aflag ? 18 : 22; xo_emit("{d:target/%-*.*s} ", width, width, line); int alen = cp - line - 1, plen = strlen(cp) - 1; xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen, plen, cp); if (container) xo_close_container(container); }
void printstream(const DISPLAY *dp) { FTSENT *p; int chcnt; xo_open_list("entry"); for (p = dp->list, chcnt = 0; p; p = p->fts_link) { if (p->fts_number == NO_PRINT) continue; /* XXX strlen does not take octal escapes into account. */ if (strlen(p->fts_name) + chcnt + (p->fts_link ? 2 : 0) >= (unsigned)termwidth) { xo_emit("\n"); chcnt = 0; } xo_open_instance("file"); chcnt += printaname(p, dp->s_inode, dp->s_block); xo_close_instance("file"); if (p->fts_link) { xo_emit(", "); chcnt += 2; } } xo_close_list("entry"); if (chcnt) xo_emit("\n"); }
/* * pr_idle -- * Display the idle time. * Returns number of excess characters that were used for long idle time. */ int pr_idle(time_t idle) { /* If idle more than 36 hours, print as a number of days. */ if (idle >= 36 * 3600) { int days = idle / 86400; xo_emit(" {:idle/%dday%s} ", days, days > 1 ? "s" : " " ); if (days >= 100) return (2); if (days >= 10) return (1); } /* If idle more than an hour, print as HH:MM. */ else if (idle >= 3600) xo_emit(" {:idle/%2d:%02d/} ", (int)(idle / 3600), (int)((idle % 3600) / 60)); else if (idle / 60 == 0) xo_emit(" - "); /* Else print the minutes idle. */ else xo_emit(" {:idle/%2d} ", (int)(idle / 60)); return (0); /* not idle longer than 9 days */ }
/* * Display an individual arp entry */ static int get(char *host) { struct sockaddr_in *addr; int found; addr = getaddr(host); if (addr == NULL) return (1); xo_set_version(ARP_XO_VERSION); xo_open_container("arp"); xo_open_list("arp-cache"); found = search(addr->sin_addr.s_addr, print_entry); if (found == 0) { xo_emit("{d:hostname/%s} ({d:ip-address/%s}) -- no entry", host, inet_ntoa(addr->sin_addr)); if (rifname) xo_emit(" on {d:interface/%s}", rifname); xo_emit("\n"); } xo_close_list("arp-cache"); xo_close_container("arp"); xo_finish(); return (found == 0); }
/* * print [inode] [size] name * return # of characters printed, no trailing characters. */ static int printaname(const FTSENT *p, u_long inodefield, u_long sizefield) { struct stat *sp; int chcnt; #ifdef COLORLS int color_printed = 0; #endif sp = p->fts_statp; chcnt = 0; if (f_inode) chcnt += xo_emit("{t:inode/%*ju} ", (int)inodefield, (uintmax_t)sp->st_ino); if (f_size) chcnt += xo_emit("{t:size/%*jd} ", (int)sizefield, howmany(sp->st_blocks, blocksize)); #ifdef COLORLS if (f_color) color_printed = colortype(sp->st_mode); #endif chcnt += printname("name", p->fts_name); #ifdef COLORLS if (f_color && color_printed) endcolor(0); #endif if (f_type) chcnt += printtype(sp->st_mode); return (chcnt); }
void procstat_rusage(struct procstat *procstat, struct kinfo_proc *kipp) { struct kinfo_proc *kip; unsigned int count, i; if (!hflag) { xo_emit("{d:ta/%5s} ", "PID"); if (Hflag) xo_emit("{d:tb/%6s} ", "TID"); xo_emit("{d:tc/%-16s %-32s %14s}\n", "COMM", "RESOURCE", "VALUE "); } if (!Hflag) { print_rusage(kipp); return; } xo_emit("{e:process_id/%d}", kipp->ki_pid); xo_emit("{e:command/%s}", kipp->ki_comm); xo_open_container("threads"); kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, kipp->ki_pid, &count); if (kip == NULL) return; kinfo_proc_sort(kip, count); for (i = 0; i < count; i++) { print_rusage(&kip[i]); } xo_close_container("threads"); procstat_freeprocs(procstat, kip); }
/* * Print routing tables. */ void routepr(int fibnum, int af) { size_t intsize; int numfibs; if (live == 0) return; intsize = sizeof(int); if (fibnum == -1 && sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) fibnum = 0; if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) numfibs = 1; if (fibnum < 0 || fibnum > numfibs - 1) errx(EX_USAGE, "%d: invalid fib", fibnum); /* * Since kernel & userland use different timebase * (time_uptime vs time_second) and we are reading kernel memory * directly we should do rt_expire --> expire_time conversion. */ if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) err(EX_OSERR, "clock_gettime() failed"); xo_open_container("route-information"); xo_emit("{T:Routing tables}"); if (fibnum) xo_emit(" ({L:fib}: {:fib/%d})", fibnum); xo_emit("\n"); p_rtable_sysctl(fibnum, af); xo_close_container("route-information"); }
void procstat_args(struct procstat *procstat, struct kinfo_proc *kipp) { int i; char **args; if (!hflag) { xo_emit("{T:/%5s %-16s %-53s}\n", "PID", "COMM", "ARGS"); } args = procstat_getargv(procstat, kipp, 0); xo_emit("{k:process_id/%5d/%d} {:command/%-16s/%s}", kipp->ki_pid, kipp->ki_comm); if (args == NULL) { xo_emit(" {d:args/-}\n"); return; } xo_open_list("arguments"); for (i = 0; args[i] != NULL; i++) xo_emit(" {l:args/%s}", args[i]); xo_close_list("arguments"); xo_emit("\n"); }
static void print_prefix(struct kinfo_proc *kipp) { xo_emit("{d:process_id/%5d/%d} ", kipp->ki_pid); if (Hflag) xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid); xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); }
static void p_flags(int f, const char *format) { struct bits *p; xo_emit(format, fmt_flags(f)); xo_open_list("flags_pretty"); for (p = bits; p->b_mask; p++) if (p->b_mask & f) xo_emit("{le:flags_pretty/%s}", p->b_name); xo_close_list("flags_pretty"); }
/* * Dump raw ip6 statistics structure. */ void rip6_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { struct rip6stat rip6stat, zerostat; u_quad_t delivered; size_t len; len = sizeof(rip6stat); if (live) { if (zflag) memset(&zerostat, 0, len); if (sysctlbyname("net.inet6.ip6.rip6stats", &rip6stat, &len, zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { if (errno != ENOENT) xo_warn("sysctl: net.inet6.ip6.rip6stats"); return; } } else kread_counters(off, &rip6stat, len); xo_emit("{T:/%s}:\n", name); xo_open_container(name); #define p(f, m) if (rip6stat.f || sflag <= 1) \ xo_emit(m, (uintmax_t)rip6stat.f, plural(rip6stat.f)) p(rip6s_ipackets, "\t{:received-packets/%ju} " "{N:/message%s received}\n"); p(rip6s_isum, "\t{:input-checksum-computation/%ju} " "{N:/checksum calculation%s on inbound}\n"); p(rip6s_badsum, "\t{:received-bad-checksum/%ju} " "{N:/message%s with bad checksum}\n"); p(rip6s_nosock, "\t{:dropped-no-socket/%ju} " "{N:/message%s dropped due to no socket}\n"); p(rip6s_nosockmcast, "\t{:dropped-multicast-no-socket/%ju} " "{N:/multicast message%s dropped due to no socket}\n"); p(rip6s_fullsock, "\t{:dropped-full-socket-buffer/%ju} " "{N:/message%s dropped due to full socket buffers}\n"); delivered = rip6stat.rip6s_ipackets - rip6stat.rip6s_badsum - rip6stat.rip6s_nosock - rip6stat.rip6s_nosockmcast - rip6stat.rip6s_fullsock; if (delivered || sflag <= 1) xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n", (uintmax_t)delivered); p(rip6s_opackets, "\t{:sent-packets/%ju} " "{N:/datagram%s output}\n"); #undef p xo_close_container(name); }
/* * Dump ARP statistics structure. */ void arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { struct arpstat arpstat; if (fetch_stats("net.link.ether.arp.stats", off, &arpstat, sizeof(arpstat), kread_counters) != 0) return; xo_open_container(name); xo_emit("{T:/%s}:\n", name); #define p(f, m) if (arpstat.f || sflag <= 1) \ xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f)) #define p2(f, m) if (arpstat.f || sflag <= 1) \ xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f)) p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n"); p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n"); p(rxrequests, "{:received-requests/%ju} " "{N:/ARP request%s received}\n"); p2(rxreplies, "{:received-replies/%ju} " "{N:/ARP repl%s received}\n"); p(received, "{:received-packers/%ju} " "{N:/ARP packet%s received}\n"); p(dropped, "{:dropped-no-entry/%ju} " "{N:/total packet%s dropped due to no ARP entry}\n"); p(timeouts, "{:entries-timeout/%ju} " "{N:/ARP entry%s timed out}\n"); p(dupips, "{:dropped-duplicate-address/%ju} " "{N:/Duplicate IP%s seen}\n"); #undef p #undef p2 xo_close_container(name); }
/* * pr_attime -- * Print the time since the user logged in. */ int pr_attime(time_t *started, time_t *now) { static wchar_t buf[256]; struct tm tp, tm; time_t diff; const wchar_t *fmt; int len, width, offset = 0; tp = *localtime(started); tm = *localtime(now); diff = *now - *started; /* If more than a week, use day-month-year. */ if (diff > 86400 * 7) fmt = L"%d%b%y"; /* If not today, use day-hour-am/pm. */ else if (tm.tm_mday != tp.tm_mday || tm.tm_mon != tp.tm_mon || tm.tm_year != tp.tm_year) { /* The line below does not take DST into consideration */ /* else if (*now / 86400 != *started / 86400) { */ fmt = use_ampm ? L"%a%I%p" : L"%a%H"; } /* Default is hh:mm{am,pm}. */ else { fmt = use_ampm ? L"%l:%M%p" : L"%k:%M"; } (void)wcsftime(buf, sizeof(buf), fmt, &tp); len = wcslen(buf); width = wcswidth(buf, len); xo_attr("since", "%lu", (unsigned long) *started); xo_attr("delta", "%lu", (unsigned long) diff); if (len == width) xo_emit("{:login-time/%-7.7ls/%ls}", buf); else if (width < 7) xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, " "); else { xo_emit("{:login-time/%ls}", buf); offset = width - 7; } return (offset); }
static void bpf_flags(struct xbpf_d *bd, char *flagbuf) { *flagbuf++ = bd->bd_promisc ? 'p' : '-'; *flagbuf++ = bd->bd_immediate ? 'i' : '-'; *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f'; *flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' : ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's'); *flagbuf++ = bd->bd_feedback ? 'b' : '-'; *flagbuf++ = bd->bd_async ? 'a' : '-'; *flagbuf++ = bd->bd_locked ? 'l' : '-'; *flagbuf++ = '\0'; if (bd->bd_promisc) xo_emit("{e:promiscuous/}"); if (bd->bd_immediate) xo_emit("{e:immediate/}"); if (bd->bd_hdrcmplt) xo_emit("{e:header-complete/}"); xo_emit("{e:direction}", (bd->bd_direction == BPF_D_IN) ? "input" : (bd->bd_direction == BPF_D_OUT) ? "output" : "bidirectional"); if (bd->bd_feedback) xo_emit("{e:feedback/}"); if (bd->bd_async) xo_emit("{e:async/}"); if (bd->bd_locked) xo_emit("{e:locked/}"); }
static void procstat_print_signame(int sig) { char name[12]; int i; if (!nflag && sig < sys_nsig) { strlcpy(name, sys_signame[sig], sizeof(name)); for (i = 0; name[i] != 0; i++) name[i] = toupper(name[i]); xo_emit("{d:signal/%-7s/%s} ", name); xo_open_container(name); } else { xo_emit("{d:signal/%-7d/%d} ", sig); snprintf(name, 12, "%d", sig); xo_open_container(name); } }
/* * Print out status about a file system. */ static void prtstat(struct statfs *sfsp, struct maxwidths *mwp) { static long blocksize; static int headerlen, timesthrough = 0; static const char *header; int64_t used, availblks, inodes; const char *format; if (++timesthrough == 1) { mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); mwp->fstype = imax(mwp->fstype, (int)strlen("Type")); if (thousands) { /* make space for commas */ mwp->total += (mwp->total - 1) / 3; mwp->used += (mwp->used - 1) / 3; mwp->avail += (mwp->avail - 1) / 3; mwp->iused += (mwp->iused - 1) / 3; mwp->ifree += (mwp->ifree - 1) / 3; } if (hflag) { header = " Size"; mwp->total = mwp->used = mwp->avail = (int)strlen(header); } else { header = getbsize(&headerlen, &blocksize); mwp->total = imax(mwp->total, headerlen); } mwp->used = imax(mwp->used, (int)strlen("Used")); mwp->avail = imax(mwp->avail, (int)strlen("Avail")); xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem"); if (Tflag) xo_emit(" {T:/%-*s}", mwp->fstype, "Type"); xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} Capacity", mwp->total, header, mwp->used, "Used", mwp->avail, "Avail"); if (iflag) { mwp->iused = imax(hflag ? 0 : mwp->iused, (int)strlen(" iused")); mwp->ifree = imax(hflag ? 0 : mwp->ifree, (int)strlen("ifree")); xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}", mwp->iused - 2, "iused", mwp->ifree, "ifree"); }
static void p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm) { struct sockaddr *sa, *addr[RTAX_MAX]; char buffer[128]; char prettyname[128]; int i, protrusion; xo_open_instance(name); sa = (struct sockaddr *)(rtm + 1); for (i = 0; i < RTAX_MAX; i++) { if (rtm->rtm_addrs & (1 << i)) { addr[i] = sa; sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa)); } } protrusion = p_sockaddr("destination", addr[RTAX_DST], addr[RTAX_NETMASK], rtm->rtm_flags, wid_dst); protrusion = p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST, wid_gw - protrusion); snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ", wid_flags - protrusion); p_flags(rtm->rtm_flags, buffer); if (Wflag) { xo_emit("{t:use/%*lu} ", wid_pksent, rtm->rtm_rmx.rmx_pksent); if (rtm->rtm_rmx.rmx_mtu != 0) xo_emit("{t:mtu/%*lu} ", wid_mtu, rtm->rtm_rmx.rmx_mtu); else xo_emit("{P:/%*s} ", wid_mtu, ""); } memset(prettyname, 0, sizeof(prettyname)); if (rtm->rtm_index < ifmap_size) { strlcpy(prettyname, ifmap[rtm->rtm_index].ifname, sizeof(prettyname)); if (*prettyname == '\0') strlcpy(prettyname, "---", sizeof(prettyname)); } if (Wflag) xo_emit("{t:interface-name/%*s}", wid_if, prettyname); else xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if, prettyname); if (rtm->rtm_rmx.rmx_expire) { time_t expire_time; if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0) xo_emit(" {:expire-time/%*d}", wid_expire, (int)expire_time); } xo_emit("\n"); xo_close_instance(name); }
static void sctp_print_address(const char *container, union sctp_sockstore *address, int port, int num_port) { struct servent *sp = 0; char line[80], *cp; int width; if (container) xo_open_container(container); switch (address->sa.sa_family) { #ifdef INET case AF_INET: sprintf(line, "%.*s.", Wflag ? 39 : 16, inetname(&address->sin.sin_addr)); break; #endif #ifdef INET6 case AF_INET6: sprintf(line, "%.*s.", Wflag ? 39 : 16, inet6name(&address->sin6.sin6_addr)); break; #endif default: sprintf(line, "%.*s.", Wflag ? 39 : 16, ""); break; } cp = strchr(line, '\0'); if (!num_port && port) sp = getservbyport((int)port, "sctp"); if (sp || port == 0) sprintf(cp, "%.15s ", sp ? sp->s_name : "*"); else sprintf(cp, "%d ", ntohs((u_short)port)); width = Wflag ? 45 : 22; xo_emit("{d:target/%-*.*s} ", width, width, line); int alen = cp - line - 1, plen = strlen(cp) - 1; xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen, plen, cp); if (container) xo_close_container(container); }
static void print_rusage(struct kinfo_proc *kipp) { long *lp; unsigned int i; char *field, *threadid; print_prefix(kipp); xo_emit("{d:resource/%-14s} {d:usage/%29s}{P: }\n", "user time", format_time(&kipp->ki_rusage.ru_utime)); print_prefix(kipp); xo_emit("{d:resource/%-14s} {d:usage/%29s}{P: }\n", "system time", format_time(&kipp->ki_rusage.ru_stime)); if (Hflag) { asprintf(&threadid, "%d", kipp->ki_tid); if (threadid == NULL) xo_errc(1, ENOMEM, "Failed to allocate memory in print_rusage()"); xo_open_container(threadid); xo_emit("{e:thread_id/%d}", kipp->ki_tid); } else { xo_emit("{e:process_id/%d}", kipp->ki_pid); xo_emit("{e:command/%s}", kipp->ki_comm); } xo_emit("{e:user time/%s}", format_time(&kipp->ki_rusage.ru_utime)); xo_emit("{e:system time/%s}", format_time(&kipp->ki_rusage.ru_stime)); lp = &kipp->ki_rusage.ru_maxrss; for (i = 0; i < nitems(rusage_info); i++) { print_prefix(kipp); asprintf(&field, "{e:%s/%%D}", rusage_info[i].ri_name); if (field == NULL) xo_errc(1, ENOMEM, "Failed to allocate memory in print_rusage()"); xo_emit(field, *lp); free(field); xo_emit("{d:resource/%-32s} {d:usage/%14s}\n", rusage_info[i].ri_name, format_value(*lp, rusage_info[i].ri_humanize, rusage_info[i].ri_scale)); lp++; } if (Hflag) { xo_close_container(threadid); free(threadid); } }
/* * Pretty print an Internet address (net address + port). */ void inetprint(const char *container, struct in_addr *in, int port, const char *proto, int num_port, const int af1) { struct servent *sp = 0; char line[80], *cp; int width; size_t alen, plen; if (container) xo_open_container(container); if (Wflag) snprintf(line, sizeof(line), "%s.", inetname(in)); else snprintf(line, sizeof(line), "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in)); alen = strlen(line); cp = line + alen; if (!num_port && port) sp = getservbyport((int)port, proto); if (sp || port == 0) snprintf(cp, sizeof(line) - alen, "%.15s ", sp ? sp->s_name : "*"); else snprintf(cp, sizeof(line) - alen, "%d ", ntohs((u_short)port)); width = (Aflag && !Wflag) ? 18 : ((!Wflag || af1 == AF_INET) ? 22 : 45); if (Wflag) xo_emit("{d:target/%-*s} ", width, line); else xo_emit("{d:target/%-*.*s} ", width, width, line); plen = strlen(cp) - 1; alen--; xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen, plen, cp); if (container) xo_close_container(container); }
/* * print name in current style */ int printname(const char *field, const char *name) { char fmt[BUFSIZ]; char *s = getname(name); int rc; snprintf(fmt, sizeof(fmt), "{:%s/%%hs}", field); rc = xo_emit(fmt, s); free(s); return rc; }
static void procstat_print_sig(const sigset_t *set, int sig, char flag) { xo_emit("{d:sigmember/%c}", sigismember(set, sig) ? flag : '-'); switch (flag) { case 'B': xo_emit("{en:mask/%s}", sigismember(set, sig) ? "true" : "false"); break; case 'C': xo_emit("{en:catch/%s}", sigismember(set, sig) ? "true" : "false"); break; case 'P': xo_emit("{en:list/%s}", sigismember(set, sig) ? "true" : "false"); break; case 'I': xo_emit("{en:ignore/%s}", sigismember(set, sig) ? "true" : "false"); break; default: xo_emit("{en:unknown/%s}", sigismember(set, sig) ? "true" : "false"); break; } }
void ah_stats(u_long off, const char *name, int family __unused, int proto __unused) { struct ahstat ahstat; if (fetch_stats("net.inet.ah.stats", off, &ahstat, sizeof(ahstat), kread_counters) != 0) return; xo_emit("{T:/%s}:\n", name); print_ahstats(&ahstat); }
void esp_stats(u_long off, const char *name, int family __unused, int proto __unused) { struct espstat espstat; if (fetch_stats("net.inet.esp.stats", off, &espstat, sizeof(espstat), kread_counters) != 0) return; xo_emit("{T:/%s}:\n", name); print_espstats(&espstat); }
void mrt6_stats() { struct mrt6stat mrtstat; size_t len = sizeof mrtstat; if (sysctlbyname("net.inet6.ip6.mrt6stat", &mrtstat, &len, NULL, 0) < 0) { xo_warn("sysctl: net.inet6.ip6.mrt6stat"); return; } xo_open_container("multicast-statistics"); xo_emit("{T:IPv6 multicast forwarding}:\n"); #define p(f, m) if (mrtstat.f || sflag <= 1) \ xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f)) #define p2(f, m) if (mrtstat.f || sflag <= 1) \ xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f)) p(mrt6s_mfc_lookups, "\t{:cache-lookups/%ju} " "{N:/multicast forwarding cache lookup%s}\n"); p2(mrt6s_mfc_misses, "\t{:cache-misses/%ju} " "{N:/multicast forwarding cache miss%s}\n"); p(mrt6s_upcalls, "\t{:upcalls/%ju} " "{N:/upcall%s to multicast routing daemon}\n"); p(mrt6s_upq_ovflw, "\t{:upcall-overflows/%ju} " "{N:/upcall queue overflow%s}\n"); p(mrt6s_upq_sockfull, "\t{:upcalls-dropped-full-buffer/%ju} " "{N:/upcall%s dropped due to full socket buffer}\n"); p(mrt6s_cache_cleanups, "\t{:cache-cleanups/%ju} " "{N:/cache cleanup%s}\n"); p(mrt6s_no_route, "\t{:dropped-no-origin/%ju} " "{N:/datagram%s with no route for origin}\n"); p(mrt6s_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} " "{N:/datagram%s arrived with bad tunneling}\n"); p(mrt6s_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} " "{N:/datagram%s could not be tunneled}\n"); p(mrt6s_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} " "{N:/datagram%s arrived on wrong interface}\n"); p(mrt6s_drop_sel, "\t{:dropped-selectively/%ju} " "{N:/datagram%s selectively dropped}\n"); p(mrt6s_q_overflow, "\t{:dropped-queue-overflow/%ju} " "{N:/datagram%s dropped due to queue overflow}\n"); p(mrt6s_pkt2large, "\t{:dropped-too-large/%ju} " "{N:/datagram%s dropped for being too large}\n"); #undef p2 #undef p xo_close_container("multicast-statistics"); }