Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Jun 2012 22:11:31 +0000 (UTC)
From:      Andrew Thompson <thompsa@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org
Subject:   svn commit: r237671 - stable/9/usr.bin/sockstat
Message-ID:  <201206272211.q5RMBVV0035784@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: thompsa
Date: Wed Jun 27 22:11:31 2012
New Revision: 237671
URL: http://svn.freebsd.org/changeset/base/237671

Log:
  MFC r235870
  
   Allow the socket list to be limited to a specific jail id.

Modified:
  stable/9/usr.bin/sockstat/sockstat.1
  stable/9/usr.bin/sockstat/sockstat.c
Directory Properties:
  stable/9/usr.bin/sockstat/   (props changed)

Modified: stable/9/usr.bin/sockstat/sockstat.1
==============================================================================
--- stable/9/usr.bin/sockstat/sockstat.1	Wed Jun 27 22:09:04 2012	(r237670)
+++ stable/9/usr.bin/sockstat/sockstat.1	Wed Jun 27 22:11:31 2012	(r237671)
@@ -27,7 +27,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd January 24, 2012
+.Dd May 16, 2012
 .Dt SOCKSTAT 1
 .Os
 .Sh NAME
@@ -36,6 +36,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl 46cLlu
+.Op Fl j Ar jid
 .Op Fl p Ar ports
 .Op Fl P Ar protocols
 .Sh DESCRIPTION
@@ -57,6 +58,8 @@ Show
 (IPv6) sockets.
 .It Fl c
 Show connected sockets.
+.It Fl j Ar jid
+Show only sockets belonging to the specified jail ID.
 .It Fl L
 Only show Internet sockets if the local or foreign addresses are not
 in the loopback network prefix

Modified: stable/9/usr.bin/sockstat/sockstat.c
==============================================================================
--- stable/9/usr.bin/sockstat/sockstat.c	Wed Jun 27 22:09:04 2012	(r237670)
+++ stable/9/usr.bin/sockstat/sockstat.c	Wed Jun 27 22:11:31 2012	(r237671)
@@ -62,6 +62,7 @@ __FBSDID("$FreeBSD$");
 static int	 opt_4;		/* Show IPv4 sockets */
 static int	 opt_6;		/* Show IPv6 sockets */
 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_u;		/* Show Unix domain sockets */
@@ -549,6 +550,27 @@ getprocname(pid_t pid)
 }
 
 static int
+getprocjid(pid_t pid)
+{
+	static struct kinfo_proc proc;
+	size_t len;
+	int mib[4];
+
+	mib[0] = CTL_KERN;
+	mib[1] = KERN_PROC;
+	mib[2] = KERN_PROC_PID;
+	mib[3] = (int)pid;
+	len = sizeof proc;
+	if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
+		/* Do not warn if the process exits before we get its jid. */
+		if (errno != ESRCH)
+			warn("sysctl()");
+		return (-1);
+	}
+	return (proc.ki_jid);
+}
+
+static int
 check_ports(struct sock *s)
 {
 	int port;
@@ -643,6 +665,8 @@ display(void)
 	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
 		if (xf->xf_data == NULL)
 			continue;
+		if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
+			continue;
 		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
 		for (s = sockhash[hash]; s != NULL; s = s->next)
 			if ((void *)s->socket == xf->xf_data)
@@ -668,6 +692,8 @@ display(void)
 		pos += xprintf("%d ", xf->xf_fd);
 		displaysock(s, pos);
 	}
+	if (opt_j >= 0)
+		return;
 	for (hash = 0; hash < HASHSIZE; hash++) {
 		for (s = sockhash[hash]; s != NULL; s = s->next) {
 			if (s->shown)
@@ -716,7 +742,8 @@ main(int argc, char *argv[])
 	int protos_defined = -1;
 	int o, i;
 
-	while ((o = getopt(argc, argv, "46cLlp:P:uv")) != -1)
+	opt_j = -1;
+	while ((o = getopt(argc, argv, "46cj:Llp:P:uv")) != -1)
 		switch (o) {
 		case '4':
 			opt_4 = 1;
@@ -727,6 +754,9 @@ main(int argc, char *argv[])
 		case 'c':
 			opt_c = 1;
 			break;
+		case 'j':
+			opt_j = atoi(optarg);
+			break;
 		case 'L':
 			opt_L = 1;
 			break;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201206272211.q5RMBVV0035784>