From 384a4d33ff97e6f2bc574de395edc39627cf5cdf Mon Sep 17 00:00:00 2001 From: tuexen Date: Sun, 17 Jan 2016 14:33:40 +0000 Subject: 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. --- usr.bin/sockstat/sockstat.1 | 7 +++++-- usr.bin/sockstat/sockstat.c | 34 ++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/sockstat/sockstat.1 b/usr.bin/sockstat/sockstat.1 index 040213d..6d217dc 100644 --- a/usr.bin/sockstat/sockstat.1 +++ b/usr.bin/sockstat/sockstat.1 @@ -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 diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c index 8a8afc0..3c089b0 100644 --- a/usr.bin/sockstat/sockstat.c +++ b/usr.bin/sockstat/sockstat.c @@ -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 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_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; -- cgit v1.1