From owner-svn-src-all@FreeBSD.ORG Wed Feb 15 22:35:30 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6F9131065675; Wed, 15 Feb 2012 22:35:30 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 5D1A78FC0C; Wed, 15 Feb 2012 22:35:30 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q1FMZUuR085321; Wed, 15 Feb 2012 22:35:30 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q1FMZUqx085318; Wed, 15 Feb 2012 22:35:30 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <201202152235.q1FMZUqx085318@svn.freebsd.org> From: Jilles Tjoelker Date: Wed, 15 Feb 2012 22:35:30 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r231789 - stable/8/usr.bin/sockstat X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Feb 2012 22:35:30 -0000 Author: jilles Date: Wed Feb 15 22:35:30 2012 New Revision: 231789 URL: http://svn.freebsd.org/changeset/base/231789 Log: MFC r230512: sockstat: Also show sockets not associated with a descriptor. Sockets not associated with a file descriptor include TCP TIME_WAIT states and sockets created via the socket(9) API such as from rpc.lockd and the NFS client. PR: bin/164081 Modified: stable/8/usr.bin/sockstat/sockstat.1 stable/8/usr.bin/sockstat/sockstat.c Directory Properties: stable/8/usr.bin/sockstat/ (props changed) Modified: stable/8/usr.bin/sockstat/sockstat.1 ============================================================================== --- stable/8/usr.bin/sockstat/sockstat.1 Wed Feb 15 22:26:51 2012 (r231788) +++ stable/8/usr.bin/sockstat/sockstat.1 Wed Feb 15 22:35:30 2012 (r231789) @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd July 9, 2009 +.Dd January 24, 2012 .Dt SOCKSTAT 1 .Os .Sh NAME @@ -137,19 +137,10 @@ The address the foreign end of the socke .Xr getpeername 2 ) . .El .Pp -Note that TCP sockets in the -.Dv AF_INET -or -.Dv AF_INET6 -domains that are not in one of the -.Dv LISTEN , SYN_SENT , -or -.Dv ESTABLISHED -states may not be shown by -.Nm ; -use -.Xr netstat 1 -to examine them instead. +If a socket is associated with more than one file descriptor, +it is shown multiple times. +If a socket is not associated with any file descriptor, +the first four columns have no meaning. .Sh SEE ALSO .Xr fstat 1 , .Xr netstat 1 , Modified: stable/8/usr.bin/sockstat/sockstat.c ============================================================================== --- stable/8/usr.bin/sockstat/sockstat.c Wed Feb 15 22:26:51 2012 (r231788) +++ stable/8/usr.bin/sockstat/sockstat.c Wed Feb 15 22:35:30 2012 (r231789) @@ -86,6 +86,7 @@ static int *ports; struct sock { void *socket; void *pcb; + int shown; int vflag; int family; int proto; @@ -572,12 +573,67 @@ check_ports(struct sock *s) } static void +displaysock(struct sock *s, int pos) +{ + void *p; + int hash; + + while (pos < 29) + pos += xprintf(" "); + pos += xprintf("%s", s->protoname); + if (s->vflag & INP_IPV4) + pos += xprintf("4 "); + if (s->vflag & INP_IPV6) + pos += xprintf("6 "); + while (pos < 36) + pos += xprintf(" "); + switch (s->family) { + case AF_INET: + case AF_INET6: + pos += printaddr(s->family, &s->laddr); + if (s->family == AF_INET6 && pos >= 58) + pos += xprintf(" "); + while (pos < 58) + pos += xprintf(" "); + pos += printaddr(s->family, &s->faddr); + break; + case AF_UNIX: + /* server */ + if (s->laddr.ss_len > 0) { + pos += printaddr(s->family, &s->laddr); + break; + } + /* client */ + p = *(void **)&s->faddr; + if (p == NULL) { + pos += xprintf("(not connected)"); + break; + } + pos += xprintf("-> "); + for (hash = 0; hash < HASHSIZE; ++hash) { + for (s = sockhash[hash]; s != NULL; s = s->next) + if (s->pcb == p) + break; + if (s != NULL) + break; + } + if (s == NULL || s->laddr.ss_len == 0) + pos += xprintf("??"); + else + pos += printaddr(s->family, &s->laddr); + break; + default: + abort(); + } + xprintf("\n"); +} + +static void display(void) { struct passwd *pwd; struct xfile *xf; struct sock *s; - void *p; int hash, n, pos; printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n", @@ -595,6 +651,7 @@ display(void) continue; if (!check_ports(s)) continue; + s->shown = 1; pos = 0; if ((pwd = getpwuid(xf->xf_uid)) == NULL) pos += xprintf("%lu ", (u_long)xf->xf_uid); @@ -609,54 +666,19 @@ display(void) while (pos < 26) pos += xprintf(" "); pos += xprintf("%d ", xf->xf_fd); - while (pos < 29) - pos += xprintf(" "); - pos += xprintf("%s", s->protoname); - if (s->vflag & INP_IPV4) - pos += xprintf("4 "); - if (s->vflag & INP_IPV6) - pos += xprintf("6 "); - while (pos < 36) - pos += xprintf(" "); - switch (s->family) { - case AF_INET: - case AF_INET6: - pos += printaddr(s->family, &s->laddr); - if (s->family == AF_INET6 && pos >= 58) - pos += xprintf(" "); - while (pos < 58) - pos += xprintf(" "); - pos += printaddr(s->family, &s->faddr); - break; - case AF_UNIX: - /* server */ - if (s->laddr.ss_len > 0) { - pos += printaddr(s->family, &s->laddr); - break; - } - /* client */ - p = *(void **)&s->faddr; - if (p == NULL) { - pos += xprintf("(not connected)"); - break; - } - pos += xprintf("-> "); - for (hash = 0; hash < HASHSIZE; ++hash) { - for (s = sockhash[hash]; s != NULL; s = s->next) - if (s->pcb == p) - break; - if (s != NULL) - break; - } - if (s == NULL || s->laddr.ss_len == 0) - pos += xprintf("??"); - else - pos += printaddr(s->family, &s->laddr); - break; - default: - abort(); + displaysock(s, pos); + } + for (hash = 0; hash < HASHSIZE; hash++) { + for (s = sockhash[hash]; s != NULL; s = s->next) { + if (s->shown) + continue; + if (!check_ports(s)) + continue; + pos = 0; + pos += xprintf("%-8s %-10s %-5s %-2s ", + "?", "?", "?", "?"); + displaysock(s, pos); } - xprintf("\n"); } }