bin/78768: [Patch] Added jail support to top

Christopher Hodgins christopher.hodgins at gmail.com
Sat Mar 12 21:50:03 PST 2005


>Number:         78768
>Category:       bin
>Synopsis:       [Patch] Added jail support to top
>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 05:50:02 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 at paranoia:/usr/obj/usr/src/sys/paranoia  i386

>Description:
      These patches add jail support to top through the -j jid option.

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

Thanks
Chris
>How-To-Repeat:
      N/A
>Fix:
      
--- top.c.orig  Sun Mar 13 05:32:21 2005
+++ top.c       Sun Mar 13 03:55:12 2005
@@ -39,6 +39,7 @@
 #include <setjmp.h>
 #include <ctype.h>
 #include <sys/time.h>
+#include <unistd.h>

 /* includes specific to top */
 #include "display.h"           /* interface to display package */
@@ -137,6 +138,8 @@
 int (*d_header)() = i_header;
 int (*d_process)() = i_process;

+/* non-display routines that need to be pre-declared */
+static int attach_to_jail(const char *jailid);

 main(argc, argv)

@@ -275,7 +278,23 @@
            optind = 1;
        }

-       while ((i = getopt(ac, av, "SIHbinquvs:d:U:m:o:t")) != EOF)
+       /*
+        * 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 != '?') {
+               if (attach_to_jail(optarg) == -1)
+                   exit(1);
+           }
+       }
+
+       opterr = 1;
+       optind = 1;
+       optreset = 1;
+
+       while ((i = getopt(ac, av, "SIHbinquvs:d:U:m:o:tj:")) != EOF)
        {
            switch(i)
            {
@@ -387,11 +406,14 @@
              case 'H':
                ps.thread = !ps.thread;
                break;
+
+             case 'j':
+               break;

              default:
                fprintf(stderr, "\
 Top version %s\n\
-Usage: %s [-HISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n",
+Usage: %s [-HISbinqut] [-j jid] [-d x] [-s x] [-o field] [-U username] [number]\n",
                        version_string(), myname);
                exit(1);
            }
@@ -1045,6 +1067,21 @@
 #endif
     quit(0);
     /*NOTREACHED*/
+}
+
+static int 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);
+       return (-1);
+    } else if (jail_attach(jid) == -1) {
+       err(1, "jail_attach(): %d", jid);
+       return (-1);
+    }
+    return (0);
 }

 /*


--- top.1.orig  Sun Mar 13 05:33:04 2005
+++ top.1       Sun Mar 13 05:37:26 2005
@@ -12,6 +12,8 @@
 [
 .B \-SbiInqutv
 ] [
+.BI \-j jid
+] [
 .BI \-d count
 ] [
 .BI \-s time
@@ -143,6 +145,10 @@
 .IR username .
 This option currently only accepts usernames and will not understand
 uid numbers.
+.TP
+.BI \-j jid
+Show only those processes in jail
+.IR jid .
 .PP
 Both
 .I count

>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list