From owner-freebsd-bugs@FreeBSD.ORG Sun Mar 13 12:31:33 2005 Return-Path: Delivered-To: freebsd-bugs@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 93E1F16A4CE for ; Sun, 13 Mar 2005 12:31:33 +0000 (GMT) Received: from smtphost.cis.strath.ac.uk (smtphost.cis.strath.ac.uk [130.159.196.96]) by mx1.FreeBSD.org (Postfix) with ESMTP id AADA743D4C for ; Sun, 13 Mar 2005 12:31:32 +0000 (GMT) (envelope-from chodgins@cis.strath.ac.uk) Received: from [192.168.0.4] (chrishodgins.force9.co.uk [84.92.20.141]) j2DCVMNU012564; Sun, 13 Mar 2005 12:31:22 GMT Message-ID: <423433E2.5080209@cis.strath.ac.uk> Date: Sun, 13 Mar 2005 12:36:50 +0000 From: Chris Hodgins User-Agent: Mozilla Thunderbird 1.0 (X11/20050204) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Chris Hodgins References: <200503130600.j2D60I0F024611@freefall.freebsd.org> In-Reply-To: <200503130600.j2D60I0F024611@freefall.freebsd.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-CIS-MailScanner-Information: Please contact support@cis.strath.ac.uk for more information X-CIS-MailScanner: Found to be clean X-CIS-MailScanner-SpamCheck: not spam, SpamAssassin (score=-4.9, required 6, autolearn=not spam, BAYES_00 -4.90) X-CIS-MailScanner-From: chodgins@cis.strath.ac.uk cc: freebsd-bugs@freebsd.org Subject: Re: bin/78763: [PATCH] Added jail support to ps X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Mar 2005 12:31:33 -0000 Chris Hodgins wrote: > The following reply was made to PR bin/78763; it has been noted by GNATS. > > From: Chris Hodgins > To: Christopher Hodgins > 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 > > Christopher Hodgins wrote: > >>Number: 78763 > >>Category: bin > >>Synopsis: [PATCH] Added jail support to ps > >>Confidential: no > >>Severity: non-critical > >>Priority: low > >>Responsible: freebsd-bugs > >>State: open > >>Quarter: > >>Keywords: > >>Date-Required: > >>Class: update > >>Submitter-Id: current-users > >>Arrival-Date: Sun Mar 13 04:00:11 GMT 2005 > >>Closed-Date: > >>Last-Modified: > >>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: > >>Unformatted: > > > > 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 > This catches a problem with an error message appearing twice. Use this patch instead. Chris --- ps.c.orig Sun Mar 13 02:43:25 2005 +++ ps.c Sun Mar 13 12:24:30 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,20 @@ 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. + */ + opterr = 0; + if ((ch = getopt(argc, argv, "J:")) != -1) + if (ch != '?') + attach_to_jail(optarg); + + opterr = 1; + optind = 1; + optreset = 1; + while ((ch = getopt(argc, argv, PS_ARGS)) != -1) switch ((char)ch) { case 'A': @@ -265,6 +280,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 +650,20 @@ exit(eval); } +static void attach_to_jail(const char *jailid) { +int jid; +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 +1236,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);