From owner-freebsd-bugs@FreeBSD.ORG Thu Sep 18 17:20:02 2008 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4A6F91065678 for ; Thu, 18 Sep 2008 17:20:02 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 26F738FC1A for ; Thu, 18 Sep 2008 17:20:02 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.2/8.14.2) with ESMTP id m8IHK2NT051612 for ; Thu, 18 Sep 2008 17:20:02 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.2/8.14.1/Submit) id m8IHK1qc051611; Thu, 18 Sep 2008 17:20:01 GMT (envelope-from gnats) Resent-Date: Thu, 18 Sep 2008 17:20:01 GMT Resent-Message-Id: <200809181720.m8IHK1qc051611@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, James Bursa Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4007A106566C for ; Thu, 18 Sep 2008 17:17:03 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21]) by mx1.freebsd.org (Postfix) with ESMTP id 2EAAF8FC1E for ; Thu, 18 Sep 2008 17:17:03 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m8IHH2Sb052132 for ; Thu, 18 Sep 2008 17:17:02 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.2/8.14.1/Submit) id m8IHH2v6052131; Thu, 18 Sep 2008 17:17:02 GMT (envelope-from nobody) Message-Id: <200809181717.m8IHH2v6052131@www.freebsd.org> Date: Thu, 18 Sep 2008 17:17:02 GMT From: James Bursa To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: misc/127475: [PATCH] sockstat output columns run into each other X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Sep 2008 17:20:02 -0000 >Number: 127475 >Category: misc >Synopsis: [PATCH] sockstat output columns run into each other >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Thu Sep 18 17:20:01 UTC 2008 >Closed-Date: >Last-Modified: >Originator: James Bursa >Release: 7.0-RELEASE >Organization: >Environment: FreeBSD dev-compere-app02.usdmm.com 7.0-RELEASE FreeBSD 7.0-RELEASE #1 @375: Tue Jul 29 11:03:10 UTC 2008 root@lion.usdmm.com:/usr/obj/usr/src/sys/MINTELv1 i386 >Description: The output of sockstat looks bad when any username is longer than 8 characters. The columns aren't wide enough and no space is output between columns, so the output is difficult to read and can't be processed by other tools. For example: ~ $ sockstat -4l USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS compere-serviceshttpd.comp2353116tcp4*:8004 *:* compere-serviceshttpd.comp2348416tcp4*:8004 *:* compere-serviceshttpd.comp2348316tcp4*:8004 *:* compere-serviceshttpd.comp2347816tcp4*:8004 *:* compere-serviceshttpd.comp2323216tcp4*:8003 *:* .. Also the command is truncated to just 10 characters. >How-To-Repeat: >Fix: The attached patch makes the following changes: 1. expand the user and command columns to 18 and 20 characters 2. if the username is longer, output in full with a space always following 3. remove truncation of command name I also added sorting of the output by uid and then pid. The current output doesn't seem to have any order. Patch attached with submission follows: --- sockstat-original.c Tue Sep 16 23:13:17 2008 +++ sockstat.c Thu Sep 18 17:56:50 2008 @@ -570,6 +570,22 @@ return (0); } +static int +compare_xfiles(const void *a, const void *b) +{ + const struct xfile *xa = a; + const struct xfile *xb = b; + if (xa->xf_uid < xb->xf_uid) + return -1; + else if (xb->xf_uid < xa->xf_uid) + return 1; + if (xa->xf_pid < xb->xf_pid) + return -1; + else if (xb->xf_pid < xa->xf_pid) + return 1; + return 0; +} + static void display(void) { @@ -578,8 +594,13 @@ struct sock *s; void *p; int hash, n, pos; + char *user; + char uid[20]; + char protoname[20]; - printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n", + qsort(xfiles, nxfiles, sizeof xfiles[0], compare_xfiles); + + printf("%-18s %-20s %-5s %-2s %-6s %-21s %-21s\n", "USER", "COMMAND", "PID", "FD", "PROTO", "LOCAL ADDRESS", "FOREIGN ADDRESS"); setpassent(1); @@ -594,34 +615,25 @@ continue; if (!check_ports(s)) continue; + + if ((pwd = getpwuid(xf->xf_uid)) == NULL) { + snprintf(uid, sizeof uid, "%lu", (u_long)xf->xf_uid); + user = uid; + } else + user = pwd->pw_name; + snprintf(protoname, sizeof protoname, "%s%s%s", + s->protoname, + s->vflag & INP_IPV4 ? "4" : "", + s->vflag & INP_IPV6 ? "6" : ""); + printf("%-18s %-20s %-5lu %-2d %-6s ", + user, getprocname(xf->xf_pid), (u_long)xf->xf_pid, + xf->xf_fd, protoname); pos = 0; - if ((pwd = getpwuid(xf->xf_uid)) == NULL) - pos += xprintf("%lu", (u_long)xf->xf_uid); - else - pos += xprintf("%s", pwd->pw_name); - while (pos < 9) - pos += xprintf(" "); - pos += xprintf("%.10s", getprocname(xf->xf_pid)); - while (pos < 20) - pos += xprintf(" "); - pos += xprintf("%lu", (u_long)xf->xf_pid); - while (pos < 26) - pos += xprintf(" "); - pos += xprintf("%d", xf->xf_fd); - while (pos < 29) - pos += xprintf(" "); - pos += xprintf("%s", s->protoname); - if (s->vflag & INP_IPV4) - pos += xprintf("4"); - if (s->vflag & INP_IPV6) - pos += xprintf("6"); - while (pos < 36) - pos += xprintf(" "); switch (s->family) { case AF_INET: case AF_INET6: pos += printaddr(s->family, &s->laddr); - while (pos < 58) + while (pos < 22) pos += xprintf(" "); pos += printaddr(s->family, &s->faddr); break; >Release-Note: >Audit-Trail: >Unformatted: