misc/127475: [PATCH] sockstat output columns run into each other
James Bursa
jbursa at mintel.com
Thu Sep 18 17:20:02 UTC 2008
>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 at 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:
More information about the freebsd-bugs
mailing list