Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 17 Jan 2016 14:33:41 +0000 (UTC)
From:      Michael Tuexen <tuexen@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r294225 - stable/10/usr.bin/sockstat
Message-ID:  <201601171433.u0HEXfIP048465@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: tuexen
Date: Sun Jan 17 14:33:40 2016
New Revision: 294225
URL: https://svnweb.freebsd.org/changeset/base/294225

Log:
  MFC r285630:
  Add a -s option which adds a column listing the connection state if
  applicable (currently only for TCP).
  
  This is work from des@ and MFCing was discussed with him.

Modified:
  stable/10/usr.bin/sockstat/sockstat.1
  stable/10/usr.bin/sockstat/sockstat.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/usr.bin/sockstat/sockstat.1
==============================================================================
--- stable/10/usr.bin/sockstat/sockstat.1	Sun Jan 17 14:16:26 2016	(r294224)
+++ stable/10/usr.bin/sockstat/sockstat.1	Sun Jan 17 14:33:40 2016	(r294225)
@@ -27,7 +27,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd June 20, 2015
+.Dd July 14, 2015
 .Dt SOCKSTAT 1
 .Os
 .Sh NAME
@@ -35,7 +35,7 @@
 .Nd list open sockets
 .Sh SYNOPSIS
 .Nm
-.Op Fl 46cLlu
+.Op Fl 46cLlsu
 .Op Fl j Ar jid
 .Op Fl p Ar ports
 .Op Fl P Ar protocols
@@ -83,6 +83,9 @@ The
 argument is a comma-separated list of protocol names,
 as they are defined in
 .Xr protocols 5 .
+.It Fl s
+Display the protocol state, if applicable.
+This is currently only implemented for TCP.
 .It Fl u
 Show
 .Dv AF_LOCAL

Modified: stable/10/usr.bin/sockstat/sockstat.c
==============================================================================
--- stable/10/usr.bin/sockstat/sockstat.c	Sun Jan 17 14:16:26 2016	(r294224)
+++ stable/10/usr.bin/sockstat/sockstat.c	Sun Jan 17 14:33:40 2016	(r294225)
@@ -45,6 +45,8 @@ __FBSDID("$FreeBSD$");
 #include <netinet/in_pcb.h>
 #include <netinet/sctp.h>
 #include <netinet/tcp.h>
+#define TCPSTATES /* load state names */
+#include <netinet/tcp_fsm.h>
 #include <netinet/tcp_seq.h>
 #include <netinet/tcp_var.h>
 #include <arpa/inet.h>
@@ -71,6 +73,7 @@ static int	 opt_c;		/* Show connected so
 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_s;		/* Show protocol state if applicable */
 static int	 opt_u;		/* Show Unix domain sockets */
 static int	 opt_v;		/* Verbose mode */
 
@@ -101,6 +104,7 @@ struct sock {
 	int vflag;
 	int family;
 	int proto;
+	int state;
 	const char *protoname;
 	struct addr *laddr;
 	struct addr *faddr;
@@ -538,9 +542,9 @@ gather_inet(int proto)
 	const char *varname, *protoname;
 	size_t len, bufsize;
 	void *buf;
-	int hash, retry, vflag;
+	int hash, retry, state, vflag;
 
-	vflag = 0;
+	state = vflag = 0;
 	if (opt_4)
 		vflag |= INP_IPV4;
 	if (opt_6)
@@ -604,6 +608,7 @@ gather_inet(int proto)
 			inp = &xtp->xt_inp;
 			so = &xtp->xt_socket;
 			protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
+			state = xtp->xt_tp.t_state;
 			break;
 		case IPPROTO_UDP:
 		case IPPROTO_DIVERT:
@@ -670,6 +675,8 @@ gather_inet(int proto)
 		sock->laddr = laddr;
 		sock->faddr = faddr;
 		sock->vflag = inp->inp_vflag;
+		if (proto == IPPROTO_TCP)
+			sock->state = xtp->xt_tp.t_state;
 		sock->protoname = protoname;
 		hash = (int)((uintptr_t)sock->socket % HASHSIZE);
 		sock->next = sockhash[hash];
@@ -977,7 +984,14 @@ displaysock(struct sock *s, int pos)
 			pos = 0;
 		}
 	}
-	xprintf("\n");
+	if (opt_s && s->proto == IPPROTO_TCP) {
+		while (pos < 80)
+			pos += xprintf(" ");
+		if (s->state >= 0 && s->state < TCP_NSTATES)
+			pos += xprintf("%s", tcpstates[s->state]);
+		else
+			pos += xprintf("?");
+	}
 }
 
 static void
@@ -988,9 +1002,12 @@ display(void)
 	struct sock *s;
 	int hash, n, pos;
 
-	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
+	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s",
 	    "USER", "COMMAND", "PID", "FD", "PROTO",
 	    "LOCAL ADDRESS", "FOREIGN ADDRESS");
+	if (opt_s)
+		printf(" %-12s", "STATE");
+	printf("\n");
 	setpassent(1);
 	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
 		if (xf->xf_data == NULL)
@@ -1019,6 +1036,7 @@ display(void)
 				pos += xprintf(" ");
 			pos += xprintf("%d ", xf->xf_fd);
 			displaysock(s, pos);
+			xprintf("\n");
 		}
 	}
 	if (opt_j >= 0)
@@ -1033,6 +1051,7 @@ display(void)
 			pos += xprintf("%-8s %-10s %-5s %-2s ",
 			    "?", "?", "?", "?");
 			displaysock(s, pos);
+			xprintf("\n");
 		}
 	}
 }
@@ -1061,7 +1080,7 @@ static void
 usage(void)
 {
 	fprintf(stderr,
-	    "Usage: sockstat [-46cLlu] [-j jid] [-p ports] [-P protocols]\n");
+	    "usage: sockstat [-46cLlsu] [-j jid] [-p ports] [-P protocols]\n");
 	exit(1);
 }
 
@@ -1072,7 +1091,7 @@ main(int argc, char *argv[])
 	int o, i;
 
 	opt_j = -1;
-	while ((o = getopt(argc, argv, "46cj:Llp:P:uv")) != -1)
+	while ((o = getopt(argc, argv, "46cj:Llp:P:suv")) != -1)
 		switch (o) {
 		case '4':
 			opt_4 = 1;
@@ -1098,6 +1117,9 @@ main(int argc, char *argv[])
 		case 'P':
 			protos_defined = parse_protos(optarg);
 			break;
+		case 's':
+			opt_s = 1;
+			break;
 		case 'u':
 			opt_u = 1;
 			break;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201601171433.u0HEXfIP048465>