From owner-svn-src-stable-10@freebsd.org Sun Jan 17 14:33:42 2016 Return-Path: Delivered-To: svn-src-stable-10@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 536F2A86BBA; Sun, 17 Jan 2016 14:33:42 +0000 (UTC) (envelope-from tuexen@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 1633E1BDE; Sun, 17 Jan 2016 14:33:42 +0000 (UTC) (envelope-from tuexen@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u0HEXfLb048467; Sun, 17 Jan 2016 14:33:41 GMT (envelope-from tuexen@FreeBSD.org) Received: (from tuexen@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u0HEXfIP048465; Sun, 17 Jan 2016 14:33:41 GMT (envelope-from tuexen@FreeBSD.org) Message-Id: <201601171433.u0HEXfIP048465@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: tuexen set sender to tuexen@FreeBSD.org using -f From: Michael Tuexen Date: Sun, 17 Jan 2016 14:33:41 +0000 (UTC) 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 X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 Jan 2016 14:33:42 -0000 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 #include #include +#define TCPSTATES /* load state names */ +#include #include #include #include @@ -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;