diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2013-10-22 16:40:29 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2013-10-22 16:40:29 +0000 |
commit | 1e8e86830088bc4916f73acb3ff1c803a22d0711 (patch) | |
tree | 559b7726aea1589415802d71831be8c99d19b13d /usr.bin/netstat | |
parent | d5e889686338eef83539b6067cc6fd1e27214fc8 (diff) |
- add UNIX-domain socket info to struct kinfo_file2
- convert netstat from kvm_getfiles() to kvm_getfile2() using that
- delete kvm_getfiles() and KERN_FILE as no longer used (bump libkvm's major)
- rename kvm_getfile2() to kvm_getfiles(), kinfo_file2 to kinfo_file
and KERN_FILE2 to KERN_FILE.
ok deraadt@, millert@
ports scan sthen@
Diffstat (limited to 'usr.bin/netstat')
-rw-r--r-- | usr.bin/netstat/main.c | 17 | ||||
-rw-r--r-- | usr.bin/netstat/netstat.h | 6 | ||||
-rw-r--r-- | usr.bin/netstat/unix.c | 78 |
3 files changed, 42 insertions, 59 deletions
diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 88c0c694a64..9b2b62e4310 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.95 2013/03/20 15:23:37 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.96 2013/10/22 16:40:28 guenther Exp $ */ /* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */ /* @@ -31,7 +31,6 @@ */ #include <sys/param.h> -#include <sys/file.h> #include <sys/protosw.h> #include <sys/socket.h> #include <sys/sysctl.h> @@ -42,6 +41,7 @@ #include <ctype.h> #include <err.h> #include <errno.h> +#include <fcntl.h> #include <kvm.h> #include <limits.h> #include <netdb.h> @@ -162,6 +162,7 @@ main(int argc, char *argv[]) u_int tableid; int Tflag = 0; int repeatcount = 0; + int need_nlist; hideroot = getuid(); @@ -331,6 +332,10 @@ main(int argc, char *argv[]) } #endif + need_nlist = !mflag && (pflag || nlistf != NULL || memf != NULL || + (!iflag && !sflag && (rflag ? Aflag : + (gflag || af != AF_UNIX || Pflag)))); + /* * Discard setgid privileges if not the running kernel so that bad * guys can't print interesting stuff from kernel memory. @@ -341,8 +346,8 @@ main(int argc, char *argv[]) if (setresgid(gid, gid, gid) == -1) err(1, "setresgid"); - if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, - buf)) == NULL) { + if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY | + (need_nlist ? 0 : KVM_NO_FILES), buf)) == NULL) { fprintf(stderr, "%s: kvm_openfiles: %s\n", __progname, buf); exit(1); } @@ -351,7 +356,7 @@ main(int argc, char *argv[]) if (setresgid(gid, gid, gid) == -1) err(1, "setresgid"); - if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) { + if (need_nlist && (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)) { if (nlistf) fprintf(stderr, "%s: %s: no namelist\n", __progname, nlistf); @@ -429,7 +434,7 @@ main(int argc, char *argv[]) printproto(tp, tp->pr_name, AF_INET6, tableid, pcbaddr); if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) - unixpr(nl[N_UNIXSW].n_value, pcbaddr); + unixpr(kvmd, pcbaddr); exit(0); } diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index bad8b97e271..539d9775d00 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -1,4 +1,4 @@ -/* $OpenBSD: netstat.h,v 1.65 2013/03/20 15:23:37 deraadt Exp $ */ +/* $OpenBSD: netstat.h,v 1.66 2013/10/22 16:40:28 guenther Exp $ */ /* $NetBSD: netstat.h,v 1.6 1996/05/07 02:55:05 thorpej Exp $ */ /* @@ -32,6 +32,8 @@ * from: @(#)netstat.h 8.2 (Berkeley) 1/4/94 */ +#include <kvm.h> + /* What is the max length of a pointer printed with %p (including 0x)? */ #define PLEN (LONG_BIT / 4 + 2) @@ -132,7 +134,7 @@ void nsprotopr(u_long, char *); void intpr(int, int); -void unixpr(u_long, u_long); +void unixpr(kvm_t *, u_long); void mroutepr(u_long, u_long, u_long); void mrt_stats(void); diff --git a/usr.bin/netstat/unix.c b/usr.bin/netstat/unix.c index 2a95b272499..01454785a60 100644 --- a/usr.bin/netstat/unix.c +++ b/usr.bin/netstat/unix.c @@ -1,4 +1,4 @@ -/* $OpenBSD: unix.c,v 1.20 2013/08/18 16:32:24 guenther Exp $ */ +/* $OpenBSD: unix.c,v 1.21 2013/10/22 16:40:28 guenther Exp $ */ /* $NetBSD: unix.c,v 1.13 1995/10/03 21:42:48 thorpej Exp $ */ /*- @@ -42,9 +42,8 @@ #include <sys/un.h> #include <sys/unpcb.h> #define _KERNEL -struct uio; -struct proc; #include <sys/file.h> +#undef _KERNEL #include <netinet/in.h> @@ -54,65 +53,42 @@ struct proc; #include <kvm.h> #include "netstat.h" -static void unixdomainpr(struct socket *, caddr_t, u_long); - -static struct file *file, *fileNFILE; -static int fcnt; -extern kvm_t *kvmd; +static void unixdomainpr(const struct kinfo_file *, u_long); void -unixpr(u_long off, u_long pcbaddr) +unixpr(kvm_t *kvmd, u_long pcbaddr) { - struct file *fp; + struct kinfo_file *kf; struct socket sock, *so = &sock; - char *filebuf; - struct protosw *unixsw = (struct protosw *)off; + int i, fcnt; - filebuf = kvm_getfiles(kvmd, KERN_FILE, 0, &fcnt); - if (filebuf == NULL) { + kf = kvm_getfiles(kvmd, KERN_FILE_BYFILE, 0, sizeof(*kf), &fcnt); + if (kf == NULL) { printf("Out of memory (file table).\n"); return; } - file = (struct file *)(filebuf + sizeof(fp)); - fileNFILE = file + fcnt; - for (fp = file; fp < fileNFILE; fp++) { - if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET) - continue; - if (kread((u_long)fp->f_data, so, sizeof (*so))) - continue; - /* kludge */ - if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2) - if (so->so_pcb) - unixdomainpr(so, fp->f_data, pcbaddr); + 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 char *socktype[] = +static const char *socktype[] = { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" }; static void -unixdomainpr(struct socket *so, caddr_t soaddr, u_long pcbaddr) +unixdomainpr(const struct kinfo_file *kf, u_long pcbaddr) { - struct unpcb unpcb, *unp = &unpcb; - struct mbuf mbuf, *m; - struct sockaddr_un *sa = NULL; static int first = 1; if (Pflag) { - if (pcbaddr == (u_long)soaddr) + if (pcbaddr == kf->f_data) socket_dump(pcbaddr); return; } - if (kread((u_long)so->so_pcb, unp, sizeof (*unp))) - return; - if (unp->unp_addr) { - m = &mbuf; - if (kread((u_long)unp->unp_addr, m, sizeof (*m))) - m = NULL; - sa = (struct sockaddr_un *)(m->m_dat); - } else - m = NULL; if (first) { printf("Active UNIX domain sockets\n"); printf("%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n", @@ -121,17 +97,17 @@ unixdomainpr(struct socket *so, caddr_t soaddr, u_long pcbaddr) PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref"); first = 0; } - printf("%*p %-6.6s %6ld %6ld %*p %*p %*p %*p", - PLEN, hideroot ? 0 : soaddr, - socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc, - PLEN, hideroot ? 0 : unp->unp_vnode, - PLEN, hideroot ? 0 : unp->unp_conn, - PLEN, hideroot ? 0 : unp->unp_refs, - PLEN, hideroot ? 0 : unp->unp_nextref); - if (m) - printf(" %.*s", - (int)(m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path))), - sa->sun_path); + +#define FAKE_PTR(p) (PLEN - ((p) ? 0 : 2)), p, ((p) ? "" : "x0") + printf("%#*llx%s %-6.6s %6ld %6ld %#*llx%s %#*llx%s %#*llx%s %#*llx%s", + FAKE_PTR(kf->f_data), socktype[kf->so_type], + kf->so_rcv_cc, kf->so_snd_cc, + FAKE_PTR(kf->v_un), + FAKE_PTR(kf->unp_conn), + FAKE_PTR(kf->unp_refs), + FAKE_PTR(kf->unp_nextref)); + if (kf->unp_path[0] != '\0') + printf(" %.*s", KI_UNPPATHLEN, kf->unp_path); putchar('\n'); } |