sockstat: Also show sockets not associated with a file 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
MFC after:	2 weeks
No objection:	des
This commit is contained in:
Jilles Tjoelker 2012-01-24 21:33:34 +00:00
parent 53b95d1799
commit 61149f8d7c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=230512
2 changed files with 75 additions and 69 deletions

View File

@ -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 socket is bound to (see
.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 ,
@ -167,10 +158,3 @@ The
.Nm
command and this manual page were written by
.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
.Sh BUGS
Unlike
.Xr netstat 1 ,
.Nm
lists sockets by walking file descriptor tables and will not output
the ones owned by the kernel, e.g. NLM sockets created by
.Xr rpc.lockd 8 .

View File

@ -86,6 +86,7 @@ static int *ports;
struct sock {
void *socket;
void *pcb;
int shown;
int vflag;
int family;
int proto;
@ -571,43 +572,11 @@ check_ports(struct sock *s)
}
static void
display(void)
displaysock(struct sock *s, int pos)
{
struct passwd *pwd;
struct xfile *xf;
struct sock *s;
void *p;
int hash, n, pos;
int hash;
printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
"USER", "COMMAND", "PID", "FD", "PROTO",
"LOCAL ADDRESS", "FOREIGN ADDRESS");
setpassent(1);
for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
if (xf->xf_data == NULL)
continue;
hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
for (s = sockhash[hash]; s != NULL; s = s->next)
if ((void *)s->socket == xf->xf_data)
break;
if (s == NULL)
continue;
if (!check_ports(s))
continue;
pos = 0;
if ((pwd = getpwuid(xf->xf_uid)) == NULL)
pos += xprintf("%lu ", (u_long)xf->xf_uid);
else
pos += xprintf("%s ", pwd->pw_name);
while (pos < 9)
pos += xprintf(" ");
pos += xprintf("%.10s", getprocname(xf->xf_pid));
while (pos < 20)
pos += xprintf(" ");
pos += xprintf("%lu ", (u_long)xf->xf_pid);
while (pos < 26)
pos += xprintf(" ");
pos += xprintf("%d ", xf->xf_fd);
while (pos < 29)
pos += xprintf(" ");
pos += xprintf("%s", s->protoname);
@ -657,6 +626,59 @@ display(void)
}
xprintf("\n");
}
static void
display(void)
{
struct passwd *pwd;
struct xfile *xf;
struct sock *s;
int hash, n, pos;
printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
"USER", "COMMAND", "PID", "FD", "PROTO",
"LOCAL ADDRESS", "FOREIGN ADDRESS");
setpassent(1);
for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
if (xf->xf_data == NULL)
continue;
hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
for (s = sockhash[hash]; s != NULL; s = s->next)
if ((void *)s->socket == xf->xf_data)
break;
if (s == NULL)
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);
else
pos += xprintf("%s ", pwd->pw_name);
while (pos < 9)
pos += xprintf(" ");
pos += xprintf("%.10s", getprocname(xf->xf_pid));
while (pos < 20)
pos += xprintf(" ");
pos += xprintf("%lu ", (u_long)xf->xf_pid);
while (pos < 26)
pos += xprintf(" ");
pos += xprintf("%d ", xf->xf_fd);
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);
}
}
}
static int set_default_protos(void)