From 543e2d55756daece4cfce071d42245d5565a5694 Mon Sep 17 00:00:00 2001 From: claudio Date: Thu, 12 Feb 2015 01:49:02 +0000 Subject: Switch the printing of open sockets from ugly KVM internals to use kvm_getfiles. This allows to run netstat without any extra privileges and removes another setgid program from base.. There is still kvm reader code in there which is used for debugging purposes (crashdump). netstat should still behave mostly the same. deraadt@ and sthen@ agree that it is time to get this puppy in. --- usr.bin/netstat/unix.c | 45 ++++++++++++--------------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) (limited to 'usr.bin/netstat/unix.c') diff --git a/usr.bin/netstat/unix.c b/usr.bin/netstat/unix.c index 27ed2816812..fbf98ca2eaa 100644 --- a/usr.bin/netstat/unix.c +++ b/usr.bin/netstat/unix.c @@ -1,4 +1,4 @@ -/* $OpenBSD: unix.c,v 1.25 2015/01/20 18:26:57 deraadt Exp $ */ +/* $OpenBSD: unix.c,v 1.26 2015/02/12 01:49:02 claudio Exp $ */ /* $NetBSD: unix.c,v 1.13 1995/10/03 21:42:48 thorpej Exp $ */ /*- @@ -53,40 +53,17 @@ #include #include "netstat.h" -static void unixdomainpr(const struct kinfo_file *, u_long); - -void -unixpr(kvm_t *kvmd, u_long pcbaddr) -{ - struct kinfo_file *kf; - int i, fcnt; - - kf = kvm_getfiles(kvmd, KERN_FILE_BYFILE, 0, sizeof(*kf), &fcnt); - if (kf == NULL) { - printf("Out of memory (file table).\n"); - return; - } - for (i = 0; i < fcnt; i++) { - if (kf[i].f_count != 0 && kf[i].f_type == DTYPE_SOCKET && - kf[i].so_family == AF_LOCAL && (kf[i].so_pcb != 0 || - kf[i].unp_path[0] != '\0')) - unixdomainpr(&kf[i], pcbaddr); - } -} - static const char *socktype[] = { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" }; -static void -unixdomainpr(const struct kinfo_file *kf, u_long pcbaddr) +void +unixdomainpr(struct kinfo_file *kf) { static int first = 1; - if (Pflag) { - if (pcbaddr == kf->f_data) - socket_dump(pcbaddr); - return; - } + /* XXX should fix kinfo_file instead but not now */ + if (kf->so_pcb == -1) + kf->so_pcb = 0; if (first) { printf("Active UNIX domain sockets\n"); @@ -97,9 +74,8 @@ unixdomainpr(const struct kinfo_file *kf, u_long pcbaddr) first = 0; } -#define FAKE_PTR(p) (PLEN - ((p) ? 0 : 2)), p, ((p) ? "" : "x0") printf("%#*llx%s %-6.6s %6llu %6llu %#*llx%s %#*llx%s %#*llx%s %#*llx%s", - FAKE_PTR(kf->f_data), socktype[kf->so_type], + FAKE_PTR(kf->so_pcb), socktype[kf->so_type], kf->so_rcv_cc, kf->so_snd_cc, FAKE_PTR(kf->v_un), FAKE_PTR(kf->unp_conn), @@ -122,11 +98,14 @@ unpcb_dump(u_long off) return; kread(off, &unp, sizeof(unp)); + if (vflag) + socket_dump((u_long)unp.unp_socket); + #define p(fmt, v, sep) printf(#v " " fmt sep, unp.v); #define pll(fmt, v, sep) printf(#v " " fmt sep, (long long) unp.v); #define pull(fmt, v, sep) printf(#v " " fmt sep, (unsigned long long) unp.v); -#define pp(fmt, v, sep) printf(#v " " fmt sep, hideroot ? 0 : unp.v); - printf("unpcb %#lx\n ", hideroot ? 0 : off); +#define pp(fmt, v, sep) printf(#v " " fmt sep, unp.v); + printf("unpcb %#lx\n ", off); pp("%p", unp_socket, "\n "); pp("%p", unp_vnode, ", "); pull("%llu", unp_ino, "\n "); -- cgit v1.2.3-59-g8ed1b