From nobody@FreeBSD.org  Sun Mar 13 03:59:49 2005
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id C0CD716A4CF
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 13 Mar 2005 03:59:49 +0000 (GMT)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 96CFB43D1D
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 13 Mar 2005 03:59:49 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id j2D3xng2098514
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 13 Mar 2005 03:59:49 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id j2D3xnMV098513;
	Sun, 13 Mar 2005 03:59:49 GMT
	(envelope-from nobody)
Message-Id: <200503130359.j2D3xnMV098513@www.freebsd.org>
Date: Sun, 13 Mar 2005 03:59:49 GMT
From: Christopher Hodgins <christopher.hodgins@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [PATCH] Added jail support to ps
X-Send-Pr-Version: www-2.3

>Number:         78763
>Category:       bin
>Synopsis:       [patch] [jail] Added jail support to ps(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    pjd
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Sun Mar 13 04:00:11 GMT 2005
>Closed-Date:    Fri May 02 10:06:17 CDT 2014
>Last-Modified:  Fri May  2 15:10:00 UTC 2014
>Originator:     Christopher Hodgins
>Release:        5.4-PRERELEASE
>Organization:
>Environment:
FreeBSD paranoia 5.4-PRERELEASE FreeBSD 5.4-PRERELEASE #0: Wed Mar  2 20:05:21 GMT 2005     root@paranoia:/usr/obj/usr/src/sys/paranoia  i386

>Description:
These patches add jail support to ps through the -J jid option.

This is my first patch so I would appreciate it if you could let me know if I have made any mistakes.

Thanks
Chris
>How-To-Repeat:

>Fix:
--- ps.c.orig   Sun Mar 13 02:43:25 2005
+++ ps.c        Sun Mar 13 02:55:06 2005
@@ -129,6 +129,7 @@
        } l;
 };

+static void     attach_to_jail(const char *jailid);
 static int      check_procfs(void);
 static int      addelem_gid(struct listinfo *, const char *);
 static int      addelem_pid(struct listinfo *, const char *);
@@ -160,7 +161,7 @@
                        "%cpu,%mem,command";
 static char Zfmt[] = "label";

-#define        PS_ARGS "AaCce" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ"
+#define        PS_ARGS "AaCce" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ"

 int
 main(int argc, char *argv[])
@@ -209,6 +210,18 @@
        init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty");
        init_list(&uidlist, addelem_uid, sizeof(uid_t), "user");
        memf = nlistf = _PATH_DEVNULL;
+
+       /*
+        * Must catch the Jail ID option first, so that all
+        * of the other options run inside of the jail.
+        */
+       if ((ch = getopt(argc, argv, "J:")) != -1)
+               if (ch != '?')
+                       attach_to_jail(optarg);
+
+       optind = 1;
+       optreset = 1;
+
        while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
                switch ((char)ch) {
                case 'A':
@@ -265,6 +278,8 @@
                case 'h':
                        prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
                        break;
+               case 'J':
+                       break;
                case 'j':
                        parsefmt(jfmt, 0);
                        _fmt = 1;
@@ -633,6 +648,20 @@
        exit(eval);
 }

+static void attach_to_jail(const char *jailid) {
+int jid = -1;
+char *ep;
+
+       jid = strtol(jailid, &ep, 10);
+       if (!jailid || *ep) {
+               errx(1, "illegal jid: %s", jailid);
+               optfatal = 1;
+       } else if (jail_attach(jid) == -1) {
+               err(1, "jail_attach(): %d", jid);
+               optfatal = 1;
+       }
+}
+
 static int
 addelem_gid(struct listinfo *inf, const char *elem)
 {
@@ -1205,7 +1234,7 @@

        (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
            "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]",
-           "          [-M core] [-N system]",
+           "          [-J jid] [-M core] [-N system]",
            "          [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]",            "       ps [-L]");
        exit(1);


--- ps.1.orig   Sun Mar 13 03:11:50 2005
+++ ps.1        Sun Mar 13 03:11:32 2005
@@ -140,6 +140,9 @@
 .It Fl h
 Repeat the information header as often as necessary to guarantee one
 header per page of information.
+.It Fl J
+Display information in the jail specified by
+.Cm jid .
 .It Fl j
 Print information associated with the following keywords:
 .Cm user , pid , ppid , pgid , sid , jobc , state , tt , time ,

>Release-Note:
>Audit-Trail:

From: Chris Hodgins <chodgins@cis.strath.ac.uk>
To: Christopher Hodgins <christopher.hodgins@gmail.com>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/78763: [PATCH] Added jail support to ps
Date: Sun, 13 Mar 2005 06:01:37 +0000

 The ps man page diff is missing the -J jid option from the argument list 
 at the start.  Here is the corrected diff:
 
 --- ps.1.orig   Sun Mar 13 03:11:50 2005
 +++ ps.1        Sun Mar 13 06:00:47 2005
 @@ -40,6 +40,7 @@
   .Op Fl aCcefHhjlmrSTuvwXxZ
   .Op Fl O Ar fmt | Fl o Ar fmt
   .Op Fl G Ar gid Ns Op , Ns Ar gid Ns Ar ...
 +.Op Fl J Ar jid
   .Op Fl M Ar core
   .Op Fl N Ar system
   .Op Fl p Ar pid Ns Op , Ns Ar pid Ns Ar ...
 @@ -140,6 +141,9 @@
   .It Fl h
   Repeat the information header as often as necessary to guarantee one
   header per page of information.
 +.It Fl J
 +Display information in the jail specified by
 +.Cm jid .
   .It Fl j
   Print information associated with the following keywords:
   .Cm user , pid , ppid , pgid , sid , jobc , state , tt , time ,
 
 
 Thanks
 Chris
Responsible-Changed-From-To: freebsd-bugs->pjd 
Responsible-Changed-By: pjd 
Responsible-Changed-When: Tue Mar 15 22:16:16 GMT 2005 
Responsible-Changed-Why:  
I'm working in this area, so I'll take it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=78763 

From: Pawel Jakub Dawidek <pjd@FreeBSD.org>
To: Christopher Hodgins <christopher.hodgins@gmail.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: bin/78763: [PATCH] Added jail support to ps
Date: Tue, 15 Mar 2005 23:16:06 +0100

 --r3ueNplNBnzMaJE3
 Content-Type: text/plain; charset=iso-8859-2
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 On Sun, Mar 13, 2005 at 03:59:49AM +0000, Christopher Hodgins wrote:
 +> >Description:
 +> These patches add jail support to ps through the -J jid option.
 
 Using jail_attach(2) here is an overkill and it needs root privileges,
 which is quite logical, but jls(8) doesn't and ps(1) also shows 'J' flag
 for all users.
 
 Your patch is probably based on killall(1) code, which uses jail_attach(2),
 but it needs root privileges to kill a process inside a jail.
 I don't like how killall(1) do it - jailed root can kill 'killall' process
 when it made jail_attach(2):)
 The excuse here is that it can be done in a different way now, because jid
 for the given process is not exported from the kernel.
 
 I've a WIP in perforce to add jail support for ps(1), but in a different
 way. My patch will also add a 'jid' keyword to '-o' option.
 
 --=20
 Pawel Jakub Dawidek                       http://www.wheel.pl
 pjd@FreeBSD.org                           http://www.FreeBSD.org
 FreeBSD committer                         Am I Evil? Yes, I Am!
 
 --r3ueNplNBnzMaJE3
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.2.4 (FreeBSD)
 
 iD8DBQFCN16mForvXbEpPzQRAqY4AJ4w720WNjlCTBjB0BDbeTc0K/ZZ/gCg4V8U
 uwjQWY4xcMzMnDIfzOJ5dI4=
 =JVY+
 -----END PGP SIGNATURE-----
 
 --r3ueNplNBnzMaJE3--

From: Chris Hodgins <christopher.hodgins@gmail.com>
To: Pawel Jakub Dawidek <pjd@freebsd.org>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/78763: [PATCH] Added jail support to ps
Date: Tue, 15 Mar 2005 22:32:27 +0000

 On Tue, 15 Mar 2005 23:16:06 +0100, Pawel Jakub Dawidek <pjd@freebsd.org> wrote:
 > On Sun, Mar 13, 2005 at 03:59:49AM +0000, Christopher Hodgins wrote:
 > +> >Description:
 > +> These patches add jail support to ps through the -J jid option.
 > 
 > Using jail_attach(2) here is an overkill and it needs root privileges,
 > which is quite logical, but jls(8) doesn't and ps(1) also shows 'J' flag
 > for all users.
 > 
 > Your patch is probably based on killall(1) code, which uses jail_attach(2),
 > but it needs root privileges to kill a process inside a jail.
 > I don't like how killall(1) do it - jailed root can kill 'killall' process
 > when it made jail_attach(2):)
 > The excuse here is that it can be done in a different way now, because jid
 > for the given process is not exported from the kernel.
 > 
 > I've a WIP in perforce to add jail support for ps(1), but in a different
 > way. My patch will also add a 'jid' keyword to '-o' option.
 
 Some interesting points I had not thought off.  Just a few questions:
 
 What is a "WIP in perforce"?
 How would you do it without using jail_attach(2)?  
 
 My code was based around the killall(1) code as you suggested.  I was
 just browsing and thought it might apply itself well to the others. 
 Unfortunetly I never took into account you needed to be root to use
 it. :)
 
 Chris
 
 
 > 
 > --
 > Pawel Jakub Dawidek                       http://www.wheel.pl
 > pjd@FreeBSD.org                           http://www.FreeBSD.org
 > FreeBSD committer                         Am I Evil? Yes, I Am!
 > 
 > 
 >

From: Pawel Jakub Dawidek <pjd@FreeBSD.org>
To: Chris Hodgins <christopher.hodgins@gmail.com>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/78763: [PATCH] Added jail support to ps
Date: Tue, 15 Mar 2005 23:35:20 +0100

 --OpjiOMsh+o5vh7q4
 Content-Type: text/plain; charset=iso-8859-2
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 On Tue, Mar 15, 2005 at 10:32:27PM +0000, Chris Hodgins wrote:
 +> What is a "WIP in perforce"?
 
 My work in progress stuff in FreeBSD perforce (http://perforce.freebsd.org).
 
 +> How would you do it without using jail_attach(2)? =20
 
 I add ki_jid to kinfo_proc structure which will be exported to userland,
 so you can easly match processes based on their jail ID.
 
 --=20
 Pawel Jakub Dawidek                       http://www.wheel.pl
 pjd@FreeBSD.org                           http://www.FreeBSD.org
 FreeBSD committer                         Am I Evil? Yes, I Am!
 
 --OpjiOMsh+o5vh7q4
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.2.4 (FreeBSD)
 
 iD8DBQFCN2MoForvXbEpPzQRApQ/AJwPlZ0FZVdJ/EBKD6bWZawQNfj6LACfdThi
 DSzYgcMcq1zN0EL6FfXQI8M=
 =IF//
 -----END PGP SIGNATURE-----
 
 --OpjiOMsh+o5vh7q4--

From: Chris Hodgins <christopher.hodgins@gmail.com>
To: Pawel Jakub Dawidek <pjd@freebsd.org>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/78763: [PATCH] Added jail support to ps
Date: Tue, 15 Mar 2005 22:47:28 +0000

 On Tue, 15 Mar 2005 23:35:20 +0100, Pawel Jakub Dawidek <pjd@freebsd.org> wrote:
 > On Tue, Mar 15, 2005 at 10:32:27PM +0000, Chris Hodgins wrote:
 > +> On Tue, 15 Mar 2005 23:16:06 +0100, Pawel Jakub Dawidek <pjd@freebsd.org> wrote:
 > +> > On Sun, Mar 13, 2005 at 03:59:49AM +0000, Christopher Hodgins wrote:
 > +> > +> >Description:
 > +> > +> These patches add jail support to ps through the -J jid option.
 > +> >
 > +> > Using jail_attach(2) here is an overkill and it needs root privileges,
 > +> > which is quite logical, but jls(8) doesn't and ps(1) also shows 'J' flag
 > +> > for all users.
 > +> >
 > +> > Your patch is probably based on killall(1) code, which uses jail_attach(2),
 > +> > but it needs root privileges to kill a process inside a jail.
 > +> > I don't like how killall(1) do it - jailed root can kill 'killall' process
 > +> > when it made jail_attach(2):)
 > +> > The excuse here is that it can be done in a different way now, because jid
 > +> > for the given process is not exported from the kernel.
 > +> >
 > +> > I've a WIP in perforce to add jail support for ps(1), but in a different
 > +> > way. My patch will also add a 'jid' keyword to '-o' option.
 > +>
 > +> Some interesting points I had not thought off.  Just a few questions:
 > +>
 > +> What is a "WIP in perforce"?
 > 
 > My work in progress stuff in FreeBSD perforce (http://perforce.freebsd.org).
 > 
 > +> How would you do it without using jail_attach(2)?
 > 
 > I add ki_jid to kinfo_proc structure which will be exported to userland,
 > so you can easly match processes based on their jail ID.
 > 
 
 Sounds interesting.  I will take a look.  
 
 What about programs like netstat, w, who for example.  Can we have
 jail options for them in a similar way?
 
 I am in two minds about this stuff.  Half of me thinks that we should
 add jail options to anything and anything where it would be useful. 
 The other half says, well won't this just mean we end up with lots of
 programs with different options for jails.  In ps I used a capital J
 instead of small j as the j option had been used.  There will be more
 like that I am sure.  What is really so bad about just using jexec?  I
 guess at least with jexec you get a simple interface for everything.
 
 Chris

From: Mateusz Guzik <mjguzik@gmail.com>
To: bug-followup@freebsd.org
Cc:  
Subject: Re: bin/78763: [patch] [jail] Added jail support to ps(1)
Date: Sun, 6 Jul 2008 21:56:39 +0200

 This PR can be closed, support for jids in ps is avaiable since FreeBSD
 6.0.

From: Pawel Jakub Dawidek <pjd@FreeBSD.org>
To: Mateusz Guzik <mjguzik@gmail.com>
Cc: bug-followup@freebsd.org
Subject: Re: bin/78763: [patch] [jail] Added jail support to ps(1)
Date: Mon, 7 Jul 2008 10:43:40 +0200

 --qp4W5+cUSnZs0RIF
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 On Sun, Jul 06, 2008 at 08:30:12PM +0000, Mateusz Guzik wrote:
 > The following reply was made to PR bin/78763; it has been noted by GNATS.
 >=20
 > From: Mateusz Guzik <mjguzik@gmail.com>
 > To: bug-followup@freebsd.org
 > Cc: =20
 > Subject: Re: bin/78763: [patch] [jail] Added jail support to ps(1)
 > Date: Sun, 6 Jul 2008 21:56:39 +0200
 >=20
 >  This PR can be closed, support for jids in ps is avaiable since FreeBSD
 >  6.0.
 
 There is no option to filter process list by JID.
 
 --=20
 Pawel Jakub Dawidek                       http://www.wheel.pl
 pjd@FreeBSD.org                           http://www.FreeBSD.org
 FreeBSD committer                         Am I Evil? Yes, I Am!
 
 --qp4W5+cUSnZs0RIF
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v2.0.4 (FreeBSD)
 
 iD8DBQFIcdc7ForvXbEpPzQRAhFqAJ9z2z4YPjwVVhLCaDwXflHAnIaonQCfbg17
 xDfu6KzuIkpKhYxxPjnUuJQ=
 =T8w5
 -----END PGP SIGNATURE-----
 
 --qp4W5+cUSnZs0RIF--

From: "Bjoern A. Zeeb" <bz@FreeBSD.org>
To: bug-followup@FreeBSD.org, christopher.hodgins@gmail.com
Cc:  
Subject: Re: bin/78763: [patch] [jail] Added jail support to ps(1)
Date: Sat, 7 Feb 2009 19:20:21 +0000 (UTC)

   This message is in MIME format.  The first part should be readable text,
   while the remaining parts are likely unreadable without MIME-aware tools.
 
 --0-695158555-1234034421=:93725
 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
 
 Hi,
 
 I attached a patch that permits on to only lists processes of a
 specific jail by jail ID using ps -J jid[,jid,..]  .
 
 Does that look right and work for you?
 
 /bz
 
 PS: pjd feel free to re-assign the PR to me if that sounds right and
 I'll handle it.
 
 -- 
 Bjoern A. Zeeb                      The greatest risk is not taking one.
 --0-695158555-1234034421=:93725
 Content-Type: TEXT/PLAIN; charset=US-ASCII; name=20090207-01-pr78763-ps-jid.diff
 Content-Transfer-Encoding: BASE64
 Content-ID: <20090207192021.P93725@maildrop.int.zabbadoz.net>
 Content-Description: 20090207-01-pr78763-ps-jid.diff
 Content-Disposition: attachment; filename=20090207-01-pr78763-ps-jid.diff
 
 SW5kZXg6IGJpbi9wcy9wcy5jDQo9PT09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CisJCW9w
 dGZhdGFsID0gMTsNCisJCXJldHVybiAoMCk7CQkvKiBEbyBub3QgYWRkIHRo
 aXMgdmFsdWUuICovDQorCX0NCisNCisJaWYgKGluZi0+Y291bnQgPj0gaW5m
 LT5tYXhjb3VudCkNCisJCWV4cGFuZF9saXN0KGluZik7DQorCWluZi0+bC5q
 aWRzWyhpbmYtPmNvdW50KSsrXSA9IHRlbXBpZDsNCisJcmV0dXJuICgxKTsN
 Cit9DQorDQogI2RlZmluZQlCU0RfUElEX01BWAk5OTk5OQkJLyogQ29weSBv
 ZiBQSURfTUFYIGZyb20gc3lzL3Byb2MuaC4gKi8NCiBzdGF0aWMgaW50DQog
 YWRkZWxlbV9waWQoc3RydWN0IGxpc3RpbmZvICppbmYsIGNvbnN0IGNoYXIg
 KmVsZW0pDQpAQCAtMTIwOSw3ICsxMjU3LDcgQEANCiANCiAJKHZvaWQpZnBy
 aW50ZihzdGRlcnIsICIlc1xuJXNcbiVzXG4lc1xuIiwNCiAJICAgICJ1c2Fn
 ZTogcHMgIiBTSU5HTEVfT1BUUyAiIFstTyBmbXQgfCAtbyBmbXRdIFstRyBn
 aWRbLGdpZC4uLl1dIiwNCi0JICAgICIgICAgICAgICAgWy1NIGNvcmVdIFst
 TiBzeXN0ZW1dIiwNCisJICAgICIgICAgICAgICAgWy1KIGppZFssamlkLi4u
 XV0gWy1NIGNvcmVdIFstTiBzeXN0ZW1dIiwNCiAJICAgICIgICAgICAgICAg
 Wy1wIHBpZFsscGlkLi4uXV0gWy10IHR0eVssdHR5Li4uXV0gWy1VIHVzZXJb
 LHVzZXIuLi5dXSIsDQogCSAgICAiICAgICAgIHBzIFstTF0iKTsNCiAJZXhp
 dCgxKTsNCg==
 
 --0-695158555-1234034421=:93725--

From: Miroslav Lachman <000.fbsd@quip.cz>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/78763: [patch] [jail] Added jail support to ps(1)
Date: Tue, 08 Nov 2011 21:28:09 +0100

 This is 6 years old PR and is still open - is there some fundamental 
 reason to not commit it to HEAD and be able to use `ps -J jid`? (same 
 way as with pgrep)
State-Changed-From-To: open->closed 
State-Changed-By: bdrewery 
State-Changed-When: Fri May 2 10:06:17 CDT 2014 
State-Changed-Why:  
I've committed something similar to bz's patch. Uses jail_getid() to 
validate the jid. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=78763 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/78763: commit references a PR
Date: Fri,  2 May 2014 15:05:52 +0000 (UTC)

 Author: bdrewery
 Date: Fri May  2 15:05:47 2014
 New Revision: 265229
 URL: http://svnweb.freebsd.org/changeset/base/265229
 
 Log:
   Add -J to filter by matching jail IDs and names.
   
   -J 0 can be used to show only host processes.
   
   Patch partially based on work by bz@
   
   PR:		bin/78763
   MFC after:	2 weeks
   Relnotes:	yes
 
 Modified:
   head/bin/ps/Makefile
   head/bin/ps/ps.1
   head/bin/ps/ps.c
 
 Modified: head/bin/ps/Makefile
 ==============================================================================
 --- head/bin/ps/Makefile	Fri May  2 14:47:53 2014	(r265228)
 +++ head/bin/ps/Makefile	Fri May  2 15:05:47 2014	(r265229)
 @@ -11,7 +11,7 @@ SRCS=	fmt.c keyword.c nlist.c print.c ps
  # on large systems.
  #
  CFLAGS+=-DLAZY_PS
 -DPADD=	${LIBM} ${LIBKVM}
 -LDADD=	-lm -lkvm
 +DPADD=	${LIBM} ${LIBKVM} ${LIBJAIL}
 +LDADD=	-lm -lkvm -ljail
  
  .include <bsd.prog.mk>
 
 Modified: head/bin/ps/ps.1
 ==============================================================================
 --- head/bin/ps/ps.1	Fri May  2 14:47:53 2014	(r265228)
 +++ head/bin/ps/ps.1	Fri May  2 15:05:47 2014	(r265229)
 @@ -29,7 +29,7 @@
  .\"     @(#)ps.1	8.3 (Berkeley) 4/18/94
  .\" $FreeBSD$
  .\"
 -.Dd December 27, 2013
 +.Dd May 2, 2014
  .Dt PS 1
  .Os
  .Sh NAME
 @@ -40,6 +40,7 @@
  .Op Fl aCcdefHhjlmrSTuvwXxZ
  .Op Fl O Ar fmt | Fl o Ar fmt
  .Op Fl G Ar gid Ns Op , Ns Ar gid Ns Ar ...
 +.Op Fl J Ar jid Ns Op , Ns Ar jid Ns Ar ...
  .Op Fl M Ar core
  .Op Fl N Ar system
  .Op Fl p Ar pid Ns Op , Ns Ar pid Ns Ar ...
 @@ -62,7 +63,7 @@ will also display processes that do not 
  .Pp
  A different set of processes can be selected for display by using any
  combination of the
 -.Fl a , G , p , T , t ,
 +.Fl a , G , J , p , T , t ,
  and
  .Fl U
  options.
 @@ -152,6 +153,20 @@ Print information associated with the fo
  .Cm user , pid , ppid , pgid , sid , jobc , state , tt , time ,
  and
  .Cm command .
 +.It Fl J
 +Display information about processes which match the specified jail IDs.
 +This may be either the
 +.Cm jid
 +or
 +.Cm name
 +of the jail.
 +Use
 +.Fl J
 +.Sy 0
 +to display only host processes.
 +This flag implies
 +.Fl x
 +by default.
  .It Fl L
  List the set of keywords available for the
  .Fl O
 
 Modified: head/bin/ps/ps.c
 ==============================================================================
 --- head/bin/ps/ps.c	Fri May  2 14:47:53 2014	(r265228)
 +++ head/bin/ps/ps.c	Fri May  2 15:05:47 2014	(r265229)
 @@ -50,6 +50,7 @@ static char sccsid[] = "@(#)ps.c	8.4 (Be
  __FBSDID("$FreeBSD$");
  
  #include <sys/param.h>
 +#include <sys/jail.h>
  #include <sys/proc.h>
  #include <sys/user.h>
  #include <sys/stat.h>
 @@ -62,6 +63,7 @@ __FBSDID("$FreeBSD$");
  #include <errno.h>
  #include <fcntl.h>
  #include <grp.h>
 +#include <jail.h>
  #include <kvm.h>
  #include <limits.h>
  #include <locale.h>
 @@ -124,6 +126,7 @@ struct listinfo {
  	const char	*lname;
  	union {
  		gid_t	*gids;
 +		int	*jids;
  		pid_t	*pids;
  		dev_t	*ttys;
  		uid_t	*uids;
 @@ -132,6 +135,7 @@ struct listinfo {
  };
  
  static int	 addelem_gid(struct listinfo *, const char *);
 +static int	 addelem_jid(struct listinfo *, const char *);
  static int	 addelem_pid(struct listinfo *, const char *);
  static int	 addelem_tty(struct listinfo *, const char *);
  static int	 addelem_uid(struct listinfo *, const char *);
 @@ -163,12 +167,12 @@ static char vfmt[] = "pid,state,time,sl,
  			"%cpu,%mem,command";
  static char Zfmt[] = "label";
  
 -#define	PS_ARGS	"AaCcde" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ"
 +#define	PS_ARGS	"AaCcde" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ"
  
  int
  main(int argc, char *argv[])
  {
 -	struct listinfo gidlist, pgrplist, pidlist;
 +	struct listinfo gidlist, jidlist, pgrplist, pidlist;
  	struct listinfo ruidlist, sesslist, ttylist, uidlist;
  	struct kinfo_proc *kp;
  	KINFO *kinfo = NULL, *next_KINFO;
 @@ -208,6 +212,7 @@ main(int argc, char *argv[])
  	prtheader = showthreads = wflag = xkeep_implied = 0;
  	xkeep = -1;			/* Neither -x nor -X. */
  	init_list(&gidlist, addelem_gid, sizeof(gid_t), "group");
 +	init_list(&jidlist, addelem_jid, sizeof(int), "jail id");
  	init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group");
  	init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id");
  	init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser");
 @@ -275,6 +280,11 @@ main(int argc, char *argv[])
  		case 'h':
  			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
  			break;
 +		case 'J':
 +			add_list(&jidlist, optarg);
 +			xkeep_implied = 1;
 +			nselectors++;
 +			break;
  		case 'j':
  			parsefmt(jfmt, 0);
  			_fmt = 1;
 @@ -538,6 +548,11 @@ main(int argc, char *argv[])
  					if (kp->ki_rgid == gidlist.l.gids[elem])
  						goto keepit;
  			}
 +			if (jidlist.count > 0) {
 +				for (elem = 0; elem < jidlist.count; elem++)
 +					if (kp->ki_jid == jidlist.l.jids[elem])
 +						goto keepit;
 +			}
  			if (pgrplist.count > 0) {
  				for (elem = 0; elem < pgrplist.count; elem++)
  					if (kp->ki_pgid ==
 @@ -666,6 +681,7 @@ main(int argc, char *argv[])
  		}
  	}
  	free_list(&gidlist);
 +	free_list(&jidlist);
  	free_list(&pidlist);
  	free_list(&pgrplist);
  	free_list(&ruidlist);
 @@ -727,6 +743,30 @@ addelem_gid(struct listinfo *inf, const 
  }
  
  static int
 +addelem_jid(struct listinfo *inf, const char *elem)
 +{
 +	int tempid;
 +
 +	if (*elem == '\0') {
 +		warnx("Invalid (zero-length) jail id");
 +		optfatal = 1;
 +		return (0);		/* Do not add this value. */
 +	}
 +
 +	tempid = jail_getid(elem);
 +	if (tempid < 0) {
 +		warnx("Invalid %s: %s", inf->lname, elem);
 +		optfatal = 1;
 +		return (0);
 +	}
 +
 +	if (inf->count >= inf->maxcount)
 +		expand_list(inf);
 +	inf->l.jids[(inf->count)++] = tempid;
 +	return (1);
 +}
 +
 +static int
  addelem_pid(struct listinfo *inf, const char *elem)
  {
  	char *endp;
 @@ -1373,7 +1413,7 @@ usage(void)
  
  	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
  	    "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]",
 -	    "          [-M core] [-N system]",
 +	    "          [-J jid[,jid...]] [-M core] [-N system]",
  	    "          [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]",
  	    "       ps [-L]");
  	exit(1);
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
