git: 95968ea7ec8f - main - netstat(1): Complete libxo transition

From: Li-Wen Hsu <lwhsu_at_FreeBSD.org>
Date: Sat, 12 Oct 2024 18:30:17 UTC
The branch main has been updated by lwhsu:

URL: https://cgit.FreeBSD.org/src/commit/?id=95968ea7ec8f1cd3c1d03cc56c704cb2392d0d25

commit 95968ea7ec8f1cd3c1d03cc56c704cb2392d0d25
Author:     Yan-Hao Wang <yanhaowang@FreeBSD.org>
AuthorDate: 2024-10-12 15:03:00 +0000
Commit:     Li-Wen Hsu <lwhsu@FreeBSD.org>
CommitDate: 2024-10-12 18:21:46 +0000

    netstat(1): Complete libxo transition
    
    Reviewed by:    des
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D41427
---
 usr.bin/netstat/bpf.c           |  1 -
 usr.bin/netstat/common.c        |  5 +--
 usr.bin/netstat/if.c            |  5 +--
 usr.bin/netstat/inet.c          |  1 -
 usr.bin/netstat/inet6.c         |  1 -
 usr.bin/netstat/main.c          | 89 +++++++++++++++++++++++------------------
 usr.bin/netstat/mbuf.c          |  1 -
 usr.bin/netstat/mroute.c        |  1 -
 usr.bin/netstat/mroute6.c       |  1 -
 usr.bin/netstat/netgraph.c      |  1 -
 usr.bin/netstat/netisr.c        | 70 ++++++++++++++++----------------
 usr.bin/netstat/nhgrp.c         |  9 ++---
 usr.bin/netstat/nhops.c         | 11 +++--
 usr.bin/netstat/route.c         | 11 +++--
 usr.bin/netstat/route_netlink.c |  3 +-
 usr.bin/netstat/sctp.c          |  1 -
 usr.bin/netstat/unix.c          |  1 -
 17 files changed, 103 insertions(+), 109 deletions(-)

diff --git a/usr.bin/netstat/bpf.c b/usr.bin/netstat/bpf.c
index c0cea882df76..5d61da7b4d2b 100644
--- a/usr.bin/netstat/bpf.c
+++ b/usr.bin/netstat/bpf.c
@@ -39,7 +39,6 @@
 #include <net/bpfdesc.h>
 #include <arpa/inet.h>
 
-#include <err.h>
 #include <errno.h>
 #include <stdint.h>
 #include <stdio.h>
diff --git a/usr.bin/netstat/common.c b/usr.bin/netstat/common.c
index 64e26bf3c21e..00a3f405ed1e 100644
--- a/usr.bin/netstat/common.c
+++ b/usr.bin/netstat/common.c
@@ -50,7 +50,6 @@
 #include <string.h>
 #include <sysexits.h>
 #include <unistd.h>
-#include <err.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 #include "common.h"
@@ -100,7 +99,7 @@ prepare_ifmap(size_t *pifmap_size)
 	 * since we need #ifindex -> if_xname match
 	 */
 	if (getifaddrs(&ifap) != 0)
-		err(EX_OSERR, "getifaddrs");
+		xo_err(EX_OSERR, "getifaddrs");
 
 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
 
@@ -114,7 +113,7 @@ prepare_ifmap(size_t *pifmap_size)
 			size = roundup2(ifindex + 1, 32) *
 			    sizeof(struct ifmap_entry);
 			if ((ifmap = realloc(ifmap, size)) == NULL)
-				errx(2, "realloc(%d) failed", size);
+				xo_errx(EX_OSERR, "realloc(%d) failed", size);
 			memset(&ifmap[ifmap_size], 0,
 			    size - ifmap_size *
 			    sizeof(struct ifmap_entry));
diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c
index 172ea5324ccb..1603c7662bbd 100644
--- a/usr.bin/netstat/if.c
+++ b/usr.bin/netstat/if.c
@@ -49,7 +49,6 @@
 #include <net/if_pfsync.h>
 #endif
 
-#include <err.h>
 #include <errno.h>
 #include <ifaddrs.h>
 #include <libutil.h>
@@ -395,9 +394,9 @@ intpr(void (*pfunc)(char *), int af)
 		return sidewaysintpr();
 
 	if (getifaddrs(&ifap) != 0)
-		err(EX_OSERR, "getifaddrs");
+		xo_err(EX_OSERR, "getifaddrs");
 	if (aflag && getifmaddrs(&ifmap) != 0)
-		err(EX_OSERR, "getifmaddrs");
+		xo_err(EX_OSERR, "getifmaddrs");
 
 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
 		if (interface != NULL &&
diff --git a/usr.bin/netstat/inet.c b/usr.bin/netstat/inet.c
index 9ff7c687353f..5a5610ccb6c0 100644
--- a/usr.bin/netstat/inet.c
+++ b/usr.bin/netstat/inet.c
@@ -63,7 +63,6 @@
 #include <netinet/udp_var.h>
 
 #include <arpa/inet.h>
-#include <err.h>
 #include <errno.h>
 #include <libutil.h>
 #include <netdb.h>
diff --git a/usr.bin/netstat/inet6.c b/usr.bin/netstat/inet6.c
index 3adf5548aea4..5995be299425 100644
--- a/usr.bin/netstat/inet6.c
+++ b/usr.bin/netstat/inet6.c
@@ -54,7 +54,6 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 
-#include <err.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdbool.h>
diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c
index 6d19851b61fc..eaca38365ed8 100644
--- a/usr.bin/netstat/main.c
+++ b/usr.bin/netstat/main.c
@@ -46,7 +46,6 @@
 #endif
 
 #include <ctype.h>
-#include <err.h>
 #include <errno.h>
 #ifdef JAIL
 #include <jail.h>
@@ -61,6 +60,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>
+#include <sysexits.h>
 #include <unistd.h>
 #include "netstat.h"
 #include "nl_defs.h"
@@ -258,14 +258,14 @@ main(int argc, char *argv[])
 #ifdef INET
 			af = AF_INET;
 #else
-			errx(1, "IPv4 support is not compiled in");
+			xo_errx(EX_UNAVAILABLE, "IPv4 support is not compiled in");
 #endif
 			break;
 		case '6':
 #ifdef INET6
 			af = AF_INET6;
 #else
-			errx(1, "IPv6 support is not compiled in");
+			xo_errx(EX_UNAVAILABLE, "IPv6 support is not compiled in");
 #endif
 			break;
 		case 'A':
@@ -293,7 +293,7 @@ main(int argc, char *argv[])
 			fib = strtol(optarg, &endptr, 0);
 			if (*endptr != '\0' ||
 			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
-				xo_errx(1, "%s: invalid fib", optarg);
+				xo_errx(EX_DATAERR, "%s: invalid fib", optarg);
 			break;
 		case 'f':
 			if (strcmp(optarg, "inet") == 0)
@@ -317,7 +317,7 @@ main(int argc, char *argv[])
 			else if (strcmp(optarg, "link") == 0)
 				af = AF_LINK;
 			else {
-				xo_errx(1, "%s: unknown address family",
+				xo_errx(EX_DATAERR, "%s: unknown address family",
 				    optarg);
 			}
 			break;
@@ -345,7 +345,7 @@ main(int argc, char *argv[])
 				usage();
 			jail_name = optarg;
 #else
-			errx(1, "Jail support is not compiled in");
+			xo_errx(EX_UNAVAILABLE, "Jail support is not compiled in");
 #endif
 			break;
 		case 'L':
@@ -374,7 +374,7 @@ main(int argc, char *argv[])
 			break;
 		case 'p':
 			if ((tp = name2protox(optarg)) == NULL) {
-				xo_errx(1, "%s: unknown or uninstrumented "
+				xo_errx(EX_DATAERR, "%s: unknown or uninstrumented "
 				    "protocol", optarg);
 			}
 			pflag = 1;
@@ -448,9 +448,9 @@ main(int argc, char *argv[])
 	if (jail_name != NULL) {
 		jid = jail_getid(jail_name);
 		if (jid == -1)
-			errx(1, "Jail not found");
+			xo_errx(EX_UNAVAILABLE, "Jail not found");
 		if (jail_attach(jid) != 0)
-			errx(1, "Cannot attach to jail");
+			xo_errx(EX_UNAVAILABLE, "Cannot attach to jail");
 	}
 #endif
 
@@ -461,20 +461,21 @@ main(int argc, char *argv[])
 	live = (nlistf == NULL && memf == NULL);
 	if (!live) {
 		if (setgid(getgid()) != 0)
-			xo_err(-1, "setgid");
+			xo_err(EX_OSERR, "setgid");
 		/* Load all necessary kvm symbols */
 		kresolve_list(nl);
 	}
 
 	if (xflag && Tflag)
-		xo_errx(1, "-x and -T are incompatible, pick one.");
+		xo_errx(EX_USAGE, "-x and -T are incompatible, pick one.");
 
 	if (Bflag) {
 		if (!live)
 			usage();
 		bpf_stats(interface);
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 	if (mflag) {
 		if (!live) {
@@ -482,8 +483,9 @@ main(int argc, char *argv[])
 				mbpr(kvmd, nl[N_SFSTAT].n_value);
 		} else
 			mbpr(NULL, 0);
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 	if (Qflag) {
 		if (!live) {
@@ -491,8 +493,9 @@ main(int argc, char *argv[])
 				netisr_stats();
 		} else
 			netisr_stats();
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 #if 0
 	/*
@@ -513,8 +516,9 @@ main(int argc, char *argv[])
 		xo_set_version(NETSTAT_XO_VERSION);
 		intpr(NULL, af);
 		xo_close_container("statistics");
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 	if (rflag) {
 		xo_open_container("statistics");
@@ -527,24 +531,27 @@ main(int argc, char *argv[])
 		} else
 			routepr(fib, af);
 		xo_close_container("statistics");
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 	if (oflag) {
 		xo_open_container("statistics");
 		xo_set_version(NETSTAT_XO_VERSION);
 		nhops_print(fib, af);
 		xo_close_container("statistics");
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 	if (Oflag) {
 		xo_open_container("statistics");
 		xo_set_version(NETSTAT_XO_VERSION);
 		nhgrp_print(fib, af);
 		xo_close_container("statistics");
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 
 
@@ -568,8 +575,9 @@ main(int argc, char *argv[])
 #endif
 		}
 		xo_close_container("statistics");
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 
 	if (tp) {
@@ -579,8 +587,9 @@ main(int argc, char *argv[])
 		if (!first)
 			xo_close_list("socket");
 		xo_close_container("statistics");
-		xo_finish();
-		exit(0);
+		if (xo_finish() < 0)
+			xo_err(EX_IOERR, "stdout");
+		exit(EX_OK);
 	}
 
 	xo_open_container("statistics");
@@ -611,8 +620,9 @@ main(int argc, char *argv[])
 	if (!first)
 		xo_close_list("socket");
 	xo_close_container("statistics");
-	xo_finish();
-	exit(0);
+	if (xo_finish() < 0)
+		xo_err(EX_IOERR, "stdout");
+	exit(EX_OK);
 }
 
 static int
@@ -732,7 +742,7 @@ kvmd_init(void)
 
 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
 	if (setgid(getgid()) != 0)
-		xo_err(-1, "setgid");
+		xo_err(EX_OSERR, "setgid");
 
 	if (kvmd == NULL) {
 		xo_warnx("kvm not available: %s", errbuf);
@@ -757,10 +767,10 @@ kresolve_list(struct nlist *_nl)
 
 	if (kvm_nlist(kvmd, _nl) < 0) {
 		if (nlistf)
-			xo_errx(1, "%s: kvm_nlist: %s", nlistf,
+			xo_errx(EX_UNAVAILABLE, "%s: kvm_nlist: %s", nlistf,
 			    kvm_geterr(kvmd));
 		else
-			xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
+			xo_errx(EX_UNAVAILABLE, "kvm_nlist: %s", kvm_geterr(kvmd));
 	}
 
 	return (0);
@@ -774,10 +784,10 @@ kset_dpcpu(u_int cpuid)
 {
 
 	if ((kvmd == NULL) && (kvmd_init() != 0))
-		xo_errx(-1, "%s: kvm is not available", __func__);
+		xo_errx(EX_UNAVAILABLE, "%s: kvm is not available", __func__);
 
 	if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
-		xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
+		xo_errx(EX_UNAVAILABLE, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
 		    cpuid, kvm_geterr(kvmd)); 
 	return;
 }
@@ -834,7 +844,7 @@ kread_counters(u_long addr, void *buf, size_t size)
 
 	n = size / sizeof(uint64_t);
 	if ((counters = malloc(n * sizeof(u_long))) == NULL)
-		xo_err(-1, "malloc");
+		xo_err(EX_OSERR, "malloc");
 	if (kread(addr, counters, n * sizeof(u_long)) < 0) {
 		free(counters);
 		return (-1);
@@ -914,7 +924,7 @@ name2protox(const char *name)
 static void
 usage(void)
 {
-	(void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
+	xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
 "usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n"
 "               [-M core] [-N system]",
 "       netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n"
@@ -933,6 +943,5 @@ usage(void)
 "       netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]",
 "       netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]",
 "       netstat [-j jail] -Q");
-	xo_finish();
-	exit(1);
+	exit(EX_USAGE);
 }
diff --git a/usr.bin/netstat/mbuf.c b/usr.bin/netstat/mbuf.c
index ed700720ecb5..9a43e0115223 100644
--- a/usr.bin/netstat/mbuf.c
+++ b/usr.bin/netstat/mbuf.c
@@ -43,7 +43,6 @@
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 
-#include <err.h>
 #include <kvm.h>
 #include <memstat.h>
 #include <stdint.h>
diff --git a/usr.bin/netstat/mroute.c b/usr.bin/netstat/mroute.c
index c13fcdede80c..1577a6ae73ac 100644
--- a/usr.bin/netstat/mroute.c
+++ b/usr.bin/netstat/mroute.c
@@ -62,7 +62,6 @@
 #include <netinet/ip_mroute.h>
 #undef _NETSTAT_
 
-#include <err.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/usr.bin/netstat/mroute6.c b/usr.bin/netstat/mroute6.c
index 83cd9c18c761..2a98f26f8b27 100644
--- a/usr.bin/netstat/mroute6.c
+++ b/usr.bin/netstat/mroute6.c
@@ -81,7 +81,6 @@
 
 #include <netinet/in.h>
 
-#include <err.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/usr.bin/netstat/netgraph.c b/usr.bin/netstat/netgraph.c
index 4511b17ddbbe..3a4c11de1db8 100644
--- a/usr.bin/netstat/netgraph.c
+++ b/usr.bin/netstat/netgraph.c
@@ -53,7 +53,6 @@
 #include <stdbool.h>
 #include <string.h>
 #include <unistd.h>
-#include <err.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 
diff --git a/usr.bin/netstat/netisr.c b/usr.bin/netstat/netisr.c
index a3eb976d84bf..1708a8f01c79 100644
--- a/usr.bin/netstat/netisr.c
+++ b/usr.bin/netstat/netisr.c
@@ -40,12 +40,12 @@
 #include <net/netisr.h>
 #include <net/netisr_internal.h>
 
-#include <err.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>
+#include <sysexits.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 #include "nl_defs.h"
@@ -112,7 +112,7 @@ netisr_load_kvm_string(uintptr_t addr, char *dest, u_int limit)
 
 	for (i = 0; i < limit; i++) {
 		if (kread(addr + i, &dest[i], sizeof(dest[i])) != 0)
-			xo_errx(-1, "%s: kread()", __func__);
+			xo_errx(EX_OSERR, "%s: kread()", __func__);
 		if (dest[i] == '\0')
 			break;
 	}
@@ -167,9 +167,9 @@ netisr_load_sysctl_uint(const char *name, u_int *p)
 
 	retlen = sizeof(u_int);
 	if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
-		xo_err(-1, "%s", name);
+		xo_err(EX_OSERR, "%s", name);
 	if (retlen != sizeof(u_int))
-		xo_errx(-1, "%s: invalid len %ju", name, (uintmax_t)retlen);
+		xo_errx(EX_DATAERR, "%s: invalid len %ju", name, (uintmax_t)retlen);
 }
 
 static void
@@ -179,7 +179,7 @@ netisr_load_sysctl_string(const char *name, char *p, size_t len)
 
 	retlen = len;
 	if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
-		xo_err(-1, "%s", name);
+		xo_err(EX_OSERR, "%s", name);
 	p[len - 1] = '\0';
 }
 
@@ -212,13 +212,13 @@ netisr_load_kvm_proto(void)
 	 */
 	kread(nl[N_NETISR_MAXPROT].n_value, &maxprot, sizeof(u_int));
 	if (maxprot != NETISR_MAXPROT)
-		xo_errx(-1, "%s: NETISR_MAXPROT mismatch", __func__);
+		xo_errx(EX_DATAERR, "%s: NETISR_MAXPROT mismatch", __func__);
 	len = maxprot * sizeof(*np_array);
 	np_array = malloc(len);
 	if (np_array == NULL)
-		xo_err(-1, "%s: malloc", __func__);
+		xo_err(EX_OSERR, "%s: malloc", __func__);
 	if (kread(nl[N_NETISR_PROTO].n_value, np_array, len) != 0)
-		xo_errx(-1, "%s: kread(_netisr_proto)", __func__);
+		xo_errx(EX_DATAERR, "%s: kread(_netisr_proto)", __func__);
 
 	/*
 	 * Size and allocate memory to hold only live protocols.
@@ -231,7 +231,7 @@ netisr_load_kvm_proto(void)
 	}
 	proto_array = calloc(protocount, sizeof(*proto_array));
 	if (proto_array == NULL)
-		err(-1, "malloc");
+		xo_err(EX_OSERR, "malloc");
 	protocount = 0;
 	for (i = 0; i < maxprot; i++) {
 		npp = &np_array[i];
@@ -263,21 +263,21 @@ netisr_load_sysctl_proto(void)
 	size_t len;
 
 	if (sysctlbyname("net.isr.proto", NULL, &len, NULL, 0) < 0)
-		xo_err(-1, "net.isr.proto: query len");
+		xo_err(EX_OSERR, "net.isr.proto: query len");
 	if (len % sizeof(*proto_array) != 0)
-		xo_errx(-1, "net.isr.proto: invalid len");
+		xo_errx(EX_DATAERR, "net.isr.proto: invalid len");
 	proto_array = malloc(len);
 	if (proto_array == NULL)
-		xo_err(-1, "malloc");
+		xo_err(EX_OSERR, "malloc");
 	if (sysctlbyname("net.isr.proto", proto_array, &len, NULL, 0) < 0)
-		xo_err(-1, "net.isr.proto: query data");
+		xo_err(EX_OSERR, "net.isr.proto: query data");
 	if (len % sizeof(*proto_array) != 0)
-		xo_errx(-1, "net.isr.proto: invalid len");
+		xo_errx(EX_DATAERR, "net.isr.proto: invalid len");
 	proto_array_len = len / sizeof(*proto_array);
 	if (proto_array_len < 1)
-		xo_errx(-1, "net.isr.proto: no data");
+		xo_errx(EX_DATAERR, "net.isr.proto: no data");
 	if (proto_array[0].snp_version != sizeof(proto_array[0]))
-		xo_errx(-1, "net.isr.proto: invalid version");
+		xo_errx(EX_DATAERR, "net.isr.proto: invalid version");
 }
 
 static void
@@ -293,22 +293,22 @@ netisr_load_kvm_workstream(void)
 	len = numthreads * sizeof(*nws_array);
 	nws_array = malloc(len);
 	if (nws_array == NULL)
-		xo_err(-1, "malloc");
+		xo_err(EX_OSERR, "malloc");
 	if (kread(nl[N_NWS_ARRAY].n_value, nws_array, len) != 0)
-		xo_errx(-1, "%s: kread(_nws_array)", __func__);
+		xo_errx(EX_OSERR, "%s: kread(_nws_array)", __func__);
 	workstream_array = calloc(numthreads, sizeof(*workstream_array));
 	if (workstream_array == NULL)
-		xo_err(-1, "calloc");
+		xo_err(EX_OSERR, "calloc");
 	workstream_array_len = numthreads;
 	work_array = calloc(numthreads * proto_array_len, sizeof(*work_array));
 	if (work_array == NULL)
-		xo_err(-1, "calloc");
+		xo_err(EX_OSERR, "calloc");
 	counter = 0;
 	for (wsid = 0; wsid < numthreads; wsid++) {
 		cpuid = nws_array[wsid];
 		kset_dpcpu(cpuid);
 		if (kread(nl[N_NWS].n_value, &nws, sizeof(nws)) != 0)
-			xo_errx(-1, "%s: kread(nw)", __func__);
+			xo_errx(EX_OSERR, "%s: kread(nw)", __func__);
 		snwsp = &workstream_array[wsid];
 		snwsp->snws_version = sizeof(*snwsp);
 		snwsp->snws_wsid = cpuid;
@@ -348,22 +348,22 @@ netisr_load_sysctl_workstream(void)
 	size_t len;
 
 	if (sysctlbyname("net.isr.workstream", NULL, &len, NULL, 0) < 0)
-		xo_err(-1, "net.isr.workstream: query len");
+		xo_err(EX_OSERR, "net.isr.workstream: query len");
 	if (len % sizeof(*workstream_array) != 0)
-		xo_errx(-1, "net.isr.workstream: invalid len");
+		xo_errx(EX_DATAERR, "net.isr.workstream: invalid len");
 	workstream_array = malloc(len);
 	if (workstream_array == NULL)
-		xo_err(-1, "malloc");
+		xo_err(EX_OSERR, "malloc");
 	if (sysctlbyname("net.isr.workstream", workstream_array, &len, NULL,
 	    0) < 0)
-		xo_err(-1, "net.isr.workstream: query data");
+		xo_err(EX_OSERR, "net.isr.workstream: query data");
 	if (len % sizeof(*workstream_array) != 0)
-		xo_errx(-1, "net.isr.workstream: invalid len");
+		xo_errx(EX_DATAERR, "net.isr.workstream: invalid len");
 	workstream_array_len = len / sizeof(*workstream_array);
 	if (workstream_array_len < 1)
-		xo_errx(-1, "net.isr.workstream: no data");
+		xo_errx(EX_DATAERR, "net.isr.workstream: no data");
 	if (workstream_array[0].snws_version != sizeof(workstream_array[0]))
-		xo_errx(-1, "net.isr.workstream: invalid version");
+		xo_errx(EX_DATAERR, "net.isr.workstream: invalid version");
 }
 
 static void
@@ -372,21 +372,21 @@ netisr_load_sysctl_work(void)
 	size_t len;
 
 	if (sysctlbyname("net.isr.work", NULL, &len, NULL, 0) < 0)
-		xo_err(-1, "net.isr.work: query len");
+		xo_err(EX_OSERR, "net.isr.work: query len");
 	if (len % sizeof(*work_array) != 0)
-		xo_errx(-1, "net.isr.work: invalid len");
+		xo_errx(EX_DATAERR, "net.isr.work: invalid len");
 	work_array = malloc(len);
 	if (work_array == NULL)
-		xo_err(-1, "malloc");
+		xo_err(EX_OSERR, "malloc");
 	if (sysctlbyname("net.isr.work", work_array, &len, NULL, 0) < 0)
-		xo_err(-1, "net.isr.work: query data");
+		xo_err(EX_OSERR, "net.isr.work: query data");
 	if (len % sizeof(*work_array) != 0)
-		xo_errx(-1, "net.isr.work: invalid len");
+		xo_errx(EX_DATAERR, "net.isr.work: invalid len");
 	work_array_len = len / sizeof(*work_array);
 	if (work_array_len < 1)
-		xo_errx(-1, "net.isr.work: no data");
+		xo_errx(EX_DATAERR, "net.isr.work: no data");
 	if (work_array[0].snw_version != sizeof(work_array[0]))
-		xo_errx(-1, "net.isr.work: invalid version");
+		xo_errx(EX_DATAERR, "net.isr.work: invalid version");
 }
 
 static void
diff --git a/usr.bin/netstat/nhgrp.c b/usr.bin/netstat/nhgrp.c
index 00b051a04d21..7cbaa1af94e3 100644
--- a/usr.bin/netstat/nhgrp.c
+++ b/usr.bin/netstat/nhgrp.c
@@ -45,7 +45,6 @@
 #include <string.h>
 #include <sysexits.h>
 #include <unistd.h>
-#include <err.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 #include "common.h"
@@ -209,12 +208,12 @@ dump_nhgrp_sysctl(int fibnum, int af, struct nhops_dump *nd)
 	mib[5] = 0;
 	mib[6] = fibnum;
 	if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
-		err(EX_OSERR, "sysctl: net.route.0.%d.nhgrpdump.%d estimate",
+		xo_err(EX_OSERR, "sysctl: net.route.0.%d.nhgrpdump.%d estimate",
 		    af, fibnum);
 	if ((buf = malloc(needed)) == NULL)
-		errx(2, "malloc(%lu)", (unsigned long)needed);
+		xo_errx(EX_OSERR, "malloc(%lu)", (unsigned long)needed);
 	if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
-		err(1, "sysctl: net.route.0.%d.nhgrpdump.%d", af, fibnum);
+		xo_err(EX_OSERR, "sysctl: net.route.0.%d.nhgrpdump.%d", af, fibnum);
 	lim  = buf + needed;
 
 	/*
@@ -337,7 +336,7 @@ nhgrp_print(int fibnum, int af)
 	if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
 		numfibs = 1;
 	if (fibnum < 0 || fibnum > numfibs - 1)
-		errx(EX_USAGE, "%d: invalid fib", fibnum);
+		xo_errx(EX_USAGE, "%d: invalid fib", fibnum);
 
 	ifmap = prepare_ifmap(&ifmap_size);
 	prepare_nh_map(fibnum, af);
diff --git a/usr.bin/netstat/nhops.c b/usr.bin/netstat/nhops.c
index 1a5d8eee74b1..48ec2006994e 100644
--- a/usr.bin/netstat/nhops.c
+++ b/usr.bin/netstat/nhops.c
@@ -58,7 +58,6 @@
 #include <string.h>
 #include <sysexits.h>
 #include <unistd.h>
-#include <err.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 #include "common.h"
@@ -213,7 +212,7 @@ nhop_map_update(struct nhop_map *map, uint32_t idx, char *gw, char *ifname)
 
 		sz = new_size * (sizeof(struct nhop_entry));
 		if ((map->ptr = realloc(map->ptr, sz)) == NULL)
-			errx(2, "realloc(%zu) failed", sz);
+			xo_errx(EX_OSERR, "realloc(%zu) failed", sz);
 
 		memset(&map->ptr[map->size], 0, (new_size - map->size) * sizeof(struct nhop_entry));
 		map->size = new_size;
@@ -352,12 +351,12 @@ dump_nhops_sysctl(int fibnum, int af, struct nhops_dump *nd)
 	mib[5] = 0;
 	mib[6] = fibnum;
 	if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
-		err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d estimate", af,
+		xo_err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d estimate", af,
 		    fibnum);
 	if ((buf = malloc(needed)) == NULL)
-		errx(2, "malloc(%lu)", (unsigned long)needed);
+		xo_errx(EX_OSERR, "malloc(%lu)", (unsigned long)needed);
 	if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
-		err(1, "sysctl: net.route.0.%d.nhdump.%d", af, fibnum);
+		xo_err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d", af, fibnum);
 	lim  = buf + needed;
 
 	/*
@@ -463,7 +462,7 @@ nhops_print(int fibnum, int af)
 	if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
 		numfibs = 1;
 	if (fibnum < 0 || fibnum > numfibs - 1)
-		errx(EX_USAGE, "%d: invalid fib", fibnum);
+		xo_errx(EX_USAGE, "%d: invalid fib", fibnum);
 
 	ifmap = prepare_ifmap(&ifmap_size);
 
diff --git a/usr.bin/netstat/route.c b/usr.bin/netstat/route.c
index 482fb07ff950..f32ddee8403f 100644
--- a/usr.bin/netstat/route.c
+++ b/usr.bin/netstat/route.c
@@ -57,7 +57,6 @@
 #include <string.h>
 #include <sysexits.h>
 #include <unistd.h>
-#include <err.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 #include "common.h"
@@ -124,14 +123,14 @@ routepr(int fibnum, int af)
 	if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
 		numfibs = 1;
 	if (fibnum < 0 || fibnum > numfibs - 1)
-		errx(EX_USAGE, "%d: invalid fib", fibnum);
+		xo_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_err(EX_OSERR, "clock_gettime() failed");
 
 	xo_open_container("route-information");
 	xo_emit("{T:Routing tables}");
@@ -259,12 +258,12 @@ p_rtable_sysctl(int fibnum, int af)
 	mib[5] = 0;
 	mib[6] = fibnum;
 	if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
-		err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
+		xo_err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
 		    fibnum);
 	if ((buf = malloc(needed)) == NULL)
-		errx(2, "malloc(%lu)", (unsigned long)needed);
+		xo_errx(EX_OSERR, "malloc(%lu)", (unsigned long)needed);
 	if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
-		err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
+		xo_err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
 	lim  = buf + needed;
 	xo_open_container("route-table");
 	xo_open_list("rt-family");
diff --git a/usr.bin/netstat/route_netlink.c b/usr.bin/netstat/route_netlink.c
index 74864fab76a7..e7b2a1964602 100644
--- a/usr.bin/netstat/route_netlink.c
+++ b/usr.bin/netstat/route_netlink.c
@@ -62,7 +62,6 @@
 #include <string.h>
 #include <sysexits.h>
 #include <unistd.h>
-#include <err.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 #include "common.h"
@@ -104,7 +103,7 @@ prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
 		if (link.ifi_index >= ifmap_size) {
 			size_t size = roundup2(link.ifi_index + 1, 32) * sizeof(struct ifmap_entry);
 			if ((ifmap = realloc(ifmap, size)) == NULL)
-				errx(2, "realloc(%zu) failed", size);
+				xo_errx(EX_OSERR, "realloc(%zu) failed", size);
 			memset(&ifmap[ifmap_size], 0,
 			    size - ifmap_size *
 			    sizeof(struct ifmap_entry));
diff --git a/usr.bin/netstat/sctp.c b/usr.bin/netstat/sctp.c
index e9fd494e292b..c3abac407327 100644
--- a/usr.bin/netstat/sctp.c
+++ b/usr.bin/netstat/sctp.c
@@ -44,7 +44,6 @@
 #include <netinet/sctp_constants.h>
 #include <arpa/inet.h>
 
-#include <err.h>
 #include <errno.h>
 #include <libutil.h>
 #include <netdb.h>
diff --git a/usr.bin/netstat/unix.c b/usr.bin/netstat/unix.c
index 4d6fd9de8af3..ca9671e812ac 100644
--- a/usr.bin/netstat/unix.c
+++ b/usr.bin/netstat/unix.c
@@ -48,7 +48,6 @@
 #include <netinet/in.h>
 
 #include <errno.h>
-#include <err.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>