Add "-n" flag to sockstat.

sockstat can "hang" on getpwuid() calls in situations when FreeBSD
is joined to a directory service (AD/LDAP etc) and the directory
service fail to answer in a timely manner when trying to resolve
numeric UIDs to user names.

Submitted by:	Caleb St. John <caleb@ixsystems.com>
MFC after:	1 week
This commit is contained in:
Alexander Motin 2020-12-30 13:40:37 -05:00
parent 697684325d
commit ccdd2b2b3c
2 changed files with 10 additions and 4 deletions

View File

@ -27,7 +27,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd September 13, 2020
.Dd December 30, 2020
.Dt SOCKSTAT 1
.Os
.Sh NAME
@ -35,7 +35,7 @@
.Nd list open sockets
.Sh SYNOPSIS
.Nm
.Op Fl 46CcLlSsUuvw
.Op Fl 46CcLlnSsUuvw
.Op Fl j Ar jid
.Op Fl p Ar ports
.Op Fl P Ar protocols
@ -71,6 +71,8 @@ or do not contain the IPv6 loopback address
.Li ::1 .
.It Fl l
Show listening sockets.
.It Fl n
Do not resolve numeric UIDs to user names.
.It Fl p Ar ports
Only show Internet sockets if the local or foreign port number
is on the specified list.

View File

@ -79,6 +79,7 @@ static int opt_c; /* Show connected sockets */
static int opt_j; /* Show specified jail */
static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
static int opt_l; /* Show listening sockets */
static int opt_n; /* Don't resolve UIDs to user names */
static int opt_q; /* Don't show header */
static int opt_S; /* Show protocol stack if applicable */
static int opt_s; /* Show protocol state if applicable */
@ -1205,7 +1206,7 @@ display(void)
continue;
s->shown = 1;
pos = 0;
if ((pwd = getpwuid(xf->xf_uid)) == NULL)
if (opt_n || (pwd = getpwuid(xf->xf_uid)) == NULL)
pos += xprintf("%lu ", (u_long)xf->xf_uid);
else
pos += xprintf("%s ", pwd->pw_name);
@ -1304,7 +1305,7 @@ main(int argc, char *argv[])
int o, i;
opt_j = -1;
while ((o = getopt(argc, argv, "46Ccj:Llp:P:qSsUuvw")) != -1)
while ((o = getopt(argc, argv, "46Ccj:Llnp:P:qSsUuvw")) != -1)
switch (o) {
case '4':
opt_4 = 1;
@ -1329,6 +1330,9 @@ main(int argc, char *argv[])
case 'l':
opt_l = 1;
break;
case 'n':
opt_n = 1;
break;
case 'p':
parse_ports(optarg);
break;