From owner-freebsd-hackers Thu Jan 8 22:54:59 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.7/8.8.7) id WAA21706 for hackers-outgoing; Thu, 8 Jan 1998 22:54:59 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from wcc.wcc.net (wcc.wcc.net [208.6.232.10]) by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id WAA21681 for ; Thu, 8 Jan 1998 22:54:17 -0800 (PST) (envelope-from piquan@wcc.wcc.net) Received: from detlev.UUCP (ppp84.wcc.net [208.6.232.84]) by wcc.wcc.net (8.8.7/8.8.7) with ESMTP id AAA12478; Fri, 9 Jan 1998 00:51:03 -0600 (CST) Received: (from joelh@localhost) by detlev.UUCP (8.8.8/8.8.7) id AAA15353; Fri, 9 Jan 1998 00:54:13 -0600 (CST) (envelope-from joelh) Date: Fri, 9 Jan 1998 00:54:13 -0600 (CST) Message-Id: <199801090654.AAA15353@detlev.UUCP> To: freebsd-hackers@FreeBSD.ORG Subject: Adding process ID listing to netstat From: Joel Ray Holveck Reply-to: joelh@gnu.org Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk Would it be useful to others to allow netstat to give the process ID's that are using sockets? To give the basic idea, I have this implemented with the inet domain sockets. A patch to -current netstat is enclosed. To use it, just run netstat -P (or netstat -Pfinet or whatever you like). If it seems useful, I'll extend this to work on other domains' sockets, document it, and submit it for inclusion in -current. DISCLAIMER: I don't know anything about sockets and their internals. This means I could very well be referencing stray pointers under all sorts of bizarre conditions. This patch has been tested in at least two cases, on the same computer, within five minutes of each other. Best, joelh -------- cut here -------- *** inet.c.orig Fri Jan 9 00:07:37 1998 --- inet.c Fri Jan 9 00:37:11 1998 *************** *** 131,137 **** putchar('\n'); if (Aflag) printf("%-8.8s ", "PCB"); ! printf(Aflag ? "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" : "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n", "Proto", "Recv-Q", "Send-Q", --- 131,139 ---- putchar('\n'); if (Aflag) printf("%-8.8s ", "PCB"); ! if (Pflag) ! printf("%5.5s ", "pid"); ! printf((Aflag || Pflag) ? "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" : "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n", "Proto", "Recv-Q", "Send-Q", *************** *** 143,148 **** --- 145,152 ---- printf("%8x ", (int)inpcb.inp_ppcb); else printf("%8x ", (int)next); + if (Pflag) + printf("%5d ", sockb.so_rcv.sb_sel.si_pid); printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc); if (nflag) { *************** *** 481,487 **** char line[80], *cp; int width; ! sprintf(line, "%.*s.", (Aflag && !numeric) ? 12 : 16, inetname(in)); cp = index(line, '\0'); if (!numeric && port) sp = getservbyport((int)port, proto); --- 485,492 ---- char line[80], *cp; int width; ! sprintf(line, "%.*s.", ((Pflag || Aflag) && !numeric) ? 12 : 16, ! inetname(in)); cp = index(line, '\0'); if (!numeric && port) sp = getservbyport((int)port, proto); *************** *** 489,495 **** sprintf(cp, "%.15s", sp ? sp->s_name : "*"); else sprintf(cp, "%d", ntohs((u_short)port)); ! width = Aflag ? 18 : 22; printf(" %-*.*s", width, width, line); } --- 494,500 ---- sprintf(cp, "%.15s", sp ? sp->s_name : "*"); else sprintf(cp, "%d", ntohs((u_short)port)); ! width = (Pflag || Aflag) ? 18 : 22; printf(" %-*.*s", width, width, line); } *** main.c.orig Fri Jan 9 00:07:36 1998 --- main.c Fri Jan 9 00:30:14 1998 *************** *** 243,249 **** af = AF_UNSPEC; ! while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != -1) switch(ch) { case 'A': Aflag = 1; --- 243,249 ---- af = AF_UNSPEC; ! while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:nPp:rstuw:")) != -1) switch(ch) { case 'A': Aflag = 1; *************** *** 305,310 **** --- 305,313 ---- break; case 'n': nflag = 1; + break; + case 'P': + Pflag = 1; break; case 'p': if ((tp = name2protox(optarg)) == NULL) { *** netstat.h.orig Fri Jan 9 00:07:35 1998 --- netstat.h Fri Jan 9 00:24:40 1998 *************** *** 43,48 **** --- 43,49 ---- int iflag; /* show interfaces */ int mflag; /* show memory stats */ int nflag; /* show addresses numerically */ + int Pflag; /* show sockets' pid */ int pflag; /* show given protocol */ int rflag; /* show routing tables (or routing stats) */ int sflag; /* show protocol statistics */ -------- cut here -------- -- Joel Ray Holveck - joelh@gnu.org - http://www.wp.com/piquan Fourth law of programming: Anything that can go wrong wi sendmail: segmentation violation - core dumped