Sebastian Benoit([email protected]) on 2017.07.01 16:44:14 +0200:
> This makes netstat show only listening sockets for tcp sockets
> when invoked as netstat -l.
> 
> With it "netstat -l -finet -p tcp" is equivalent to
> "netstat -a -finet | grep LISTEN"
> 
> I (re)used -l because Linux has the same option.
> 
> FreeBSD does not have -l, but it does not error out when invoked with -l.
> 
> ok?

This diff includes the previous one.

It adds -v to the default display, to print PID/program name in an
additional last column.

I chose -v because that was not yet used with the default display.

In Linux this output is generated with the -p option, but we already use
that to select the protocol.

ok?

(benno_netstat_listen_and_pid.diff)

diff --git usr.bin/netstat/inet.c usr.bin/netstat/inet.c
index 979750dad8e..813887fe60a 100644
--- usr.bin/netstat/inet.c
+++ usr.bin/netstat/inet.c
@@ -149,7 +149,7 @@ protopr(kvm_t *kvmd, u_long pcbaddr, u_int tableid, int 
proto)
        struct kinfo_file *kf;
        int i, fcnt;
 
-       kf = kvm_getfiles(kvmd, KERN_FILE_BYFILE, DTYPE_SOCKET,
+       kf = kvm_getfiles(kvmd, KERN_FILE_BYPID, -1,
            sizeof(*kf), &fcnt);
        if (kf == NULL) {
                printf("Out of memory (file table).\n");
@@ -160,6 +160,8 @@ protopr(kvm_t *kvmd, u_long pcbaddr, u_int tableid, int 
proto)
        qsort(kf, fcnt, sizeof(*kf), kf_comp);
 
        for (i = 0; i < fcnt; i++) {
+               if (kf[i].f_type != DTYPE_SOCKET)
+                       continue;
                if (Pflag) {
                        switch (kf[i].so_family) {
                        case AF_INET:
@@ -226,6 +228,8 @@ netdomainpr(struct kinfo_file *kf, int proto)
        int isany = 0;
        int istcp = 0;
        int isip6 = 0;
+       int pen_len = 0;
+       int i;
 
        /* XXX should fix kinfo_file instead but not now */
        if (kf->so_pcb == -1)
@@ -294,9 +298,14 @@ netdomainpr(struct kinfo_file *kf, int proto)
        }
 
        /* filter listening sockets out unless -a is set */
-       if (!aflag && istcp && kf->t_state <= TCPS_LISTEN)
+       if (!(aflag || lflag) && istcp && kf->t_state <= TCPS_LISTEN)
+               return;
+       else if (!(aflag||lflag) && isany)
                return;
-       else if (!aflag && isany)
+
+       /* when -l is set, show only listening sockets */
+       if (!aflag && lflag && kf->so_type == SOCK_STREAM &&
+           kf->t_state != TCPS_LISTEN)
                return;
 
        if (af != kf->so_family || type != kf->so_type) {
@@ -305,6 +314,8 @@ netdomainpr(struct kinfo_file *kf, int proto)
                printf("Active Internet connections");
                if (aflag)
                        printf(" (including servers)");
+               else if (lflag)
+                       printf(" (only servers)");
                putchar('\n');
                if (Aflag) {
                        addrlen = 18;
@@ -315,9 +326,12 @@ netdomainpr(struct kinfo_file *kf, int proto)
                if (Bflag && istcp)
                        printf("%-6.6s %-6.6s %-6.6s ",
                            "Recv-W", "Send-W", "Cgst-W");
-               printf(" %-*.*s %-*.*s %s\n",
+               printf(" %-*.*s %-*.*s %s",
                    addrlen, addrlen, "Local Address",
                    addrlen, addrlen, "Foreign Address", "(state)");
+               if (vflag)
+                       printf("     PID/Program");
+               printf("\n");
        }
 
        if (Aflag)
@@ -340,11 +354,17 @@ netdomainpr(struct kinfo_file *kf, int proto)
        }
        if (istcp) {
                if (kf->t_state < 0 || kf->t_state >= TCP_NSTATES)
-                       printf(" %d", kf->t_state);
+                       pen_len = printf(" %d", kf->t_state);
                else
-                       printf(" %s", tcpstates[kf->t_state]);
+                       pen_len = printf(" %s", tcpstates[kf->t_state]);
        } else if (kf->so_type == SOCK_RAW) {
-               printf(" %u", kf->inp_proto);
+               pen_len = printf(" %u", kf->inp_proto);
+       }
+
+       if (vflag) {
+               for (i = 0; i < 12-pen_len; i++) /* lenght of ESTABLISHED */
+                       printf(" ");
+               printf(" %ld/%.9s", (long)kf->p_pid, kf->p_comm);
        }
        putchar('\n');
 }
diff --git usr.bin/netstat/main.c usr.bin/netstat/main.c
index e534b166634..b524089e72c 100644
--- usr.bin/netstat/main.c
+++ usr.bin/netstat/main.c
@@ -128,7 +128,7 @@ main(int argc, char *argv[])
        tableid = getrtable();
 
        while ((ch = getopt(argc, argv,
-           "AaBbc:dFf:ghI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1)
+           "AaBbc:dFf:ghI:iLlM:mN:np:P:qrsT:tuvW:w:")) != -1)
                switch (ch) {
                case 'A':
                        Aflag = 1;
@@ -441,7 +441,7 @@ static void
 usage(void)
 {
        (void)fprintf(stderr,
-           "usage: %s [-AaBn] [-f address_family] [-M core] [-N system]\n"
+           "usage: %s [-AaBln] [-f address_family] [-M core] [-N system]\n"
            "       %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N 
system]\n"
            "               [-T tableid]\n"
            "       %s [-bdhn] [-c count] [-I interface] [-M core] [-N system] 
[-w wait]\n"
diff --git usr.bin/netstat/netstat.1 usr.bin/netstat/netstat.1
index 2347af1470e..b3c9bb82704 100644
--- usr.bin/netstat/netstat.1
+++ usr.bin/netstat/netstat.1
@@ -38,7 +38,7 @@
 .Nd show network status
 .Sh SYNOPSIS
 .Nm netstat
-.Op Fl AaBn
+.Op Fl AaBln
 .Op Fl f Ar address_family
 .Op Fl p Ar protocol
 .Op Fl M Ar core
@@ -200,6 +200,8 @@ option) is present, show per-interface statistics on all 
interfaces
 for the specified
 .Ar address_family .
 .It Fl l
+With the default display,
+show only listening sockets.
 With the
 .Fl g
 option, display wider fields for the IPv6 multicast routing table
@@ -282,8 +284,11 @@ Limit statistics or address control block reports to the
 .Dv AF_UNIX
 address family.
 .It Fl v
-Show extra (verbose) detail for the routing tables
-.Pq Fl r ,
+With the default display,
+print PID and program name.
+With the
+.Fl r
+option, show extra (verbose) detail for the routing tables,
 or avoid truncation of long addresses.
 When used with the
 .Fl P
diff --git usr.bin/netstat/netstat.h usr.bin/netstat/netstat.h
index 7d117013f2f..75cc378289b 100644
--- usr.bin/netstat/netstat.h
+++ usr.bin/netstat/netstat.h
@@ -49,7 +49,8 @@ int   Fflag;          /* show routes whose gateways are in 
specified AF */
 int    gflag;          /* show group (multicast) routing or stats */
 int    hflag;          /* print human numbers */
 int    iflag;          /* show interfaces */
-int    lflag;          /* show routing table with use and ref */
+int    lflag;          /* show only listening sockets (only servers), */
+                       /* with -g, show routing table with use and ref */
 int    mflag;          /* show memory stats */
 int    nflag;          /* show addresses numerically */
 int    pflag;          /* show given protocol */

Reply via email to