Blob Blame History Raw
From 29ac10530c97631d76aa34b2d2ed018ded60d6bf Mon Sep 17 00:00:00 2001
From: Aristeu Rozanski <arozansk@redhat.com>
Date: Thu, 25 Apr 2013 11:35:15 -0400
Subject: [PATCH 2/3] pstree: introduce namespace transition information

This patch adds a new option (-S, --ns-change) that will show
when a namespace was changed compared to parent's.

Signed-off-by: Aristeu Rozanski <arozansk@redhat.com>
Signed-off-by: Craig Small <csmall@enc.com.au>
---
 doc/pstree.1 |    4 ++++
 src/pstree.c |   27 ++++++++++++++++++++++++---
 2 files changed, 28 insertions(+), 3 deletions(-)

--- psmisc-22.20.orig/doc/pstree.1	2013-09-17 16:26:00.000000000 -0400
+++ psmisc-22.20/doc/pstree.1	2013-09-17 16:27:22.510167111 -0400
@@ -21,6 +21,7 @@ pstree \- display a tree of processes
 .RB [ \-N  , \ \-\-ns\-sort \fIns\fB
 .RB [ \-p  , \ \-\-show\-pids ]
 .RB [ \-s  , \ \-\-show\-parents ]
+.RB [ \-S  , \ \-\-ns-changes ]
 .RB [ \-u  , \ \-\-uid\-changes ]
 .RB [ \-Z  , \ \-\-security\-context ]
 .RB [ \-A  , \ \-\-ascii  , \ \-G  , \ \-\-vt100  , \ \-U  , \ \-\-unicode ]
@@ -134,6 +135,9 @@ process name.
 implicitly disables compaction.
 .IP \fB\-s\fP
 Show parent processes of the specified process.
+.IP \fB\-S\fP
+Show namespaces transitions.  Like \-N, the output is limited when running
+as a regular user.
 .IP \fB\-u\fP
 Show uid transitions.  Whenever the uid of a process differs from the
 uid of its parent, the new uid is shown in parentheses after the
--- psmisc-22.20.orig/src/pstree.c	2013-09-17 16:27:12.000000000 -0400
+++ psmisc-22.20/src/pstree.c	2013-09-17 16:28:04.156698164 -0400
@@ -133,7 +133,7 @@ static int *width = NULL;
 static int *more = NULL;
 
 static int print_args = 0, compact = 1, user_change = 0, pids = 0, pgids = 0,
-    show_parents = 0, by_pid = 0, trunc = 1, wait_end = 0;
+    show_parents = 0, by_pid = 0, trunc = 1, wait_end = 0, ns_change = 0;
 #ifdef WITH_SELINUX
 static int show_scontext = 0;
 #endif                                /*WITH_SELINUX */
@@ -545,11 +545,17 @@     if (pid != 0) {
 static int tree_equal(const PROC * a, const PROC * b)
 {
     const CHILD *walk_a, *walk_b;
+    int i;
 
     if (strcmp(a->comm, b->comm))
         return 0;
     if (user_change && a->uid != b->uid)
         return 0;
+    if (ns_change) {
+        for (i = 0; i < NUM_NS; i++)
+            if (a->ns[i] != b->ns[i])
+                return 0;
+    }
     for (walk_a = a->children, walk_b = b->children; walk_a && walk_b;
          walk_a = walk_a->next, walk_b = walk_b->next)
         if (!tree_equal(walk_a->child, walk_b->child))
@@ -630,6 +636,16 @@     if (swapped && current->argc < 0)
         else
             (void) out_int(current->uid);
     }
+    if (ns_change && current->parent) {
+        for (i = 0; i < NUM_NS; i++) {
+            if (current->ns[i] == 0 || current->parent->ns[i] == 0)
+                continue;
+            if (current->ns[i] != current->parent->ns[i]) {
+                out_char(info++ ? ',' : '(');
+                out_string(get_ns_name(i));
+            }
+        }
+    }
 #ifdef WITH_SELINUX
     if (show_scontext) {
         out_char(info++ ? ',' : '(');
@@ -1002,6 +1018,7 @@              "  -G, --vt100         use 
              "  --ns-sort=type      sort by namespace type (ipc, mnt, net, pid, user, uts)\n"
              "  -p, --show-pids     show PIDs; implies -c\n"
              "  -s, --show-parents  show parents of the selected process\n"
+             "  -S, --ns-changes    show namespace transitions\n"
              "  -u, --uid-changes   show uid transitions\n"
              "  -U, --unicode       use UTF-8 (Unicode) line drawing characters\n"
              "  -V, --version       display version information\n"));
@@ -1054,6 +1071,7 @@         {"numeric-sort", 0, NULL, 'n'},
         {"show-pids", 0, NULL, 'p'},
         {"show-pgids", 0, NULL, 'g'},
         {"show-parents", 0, NULL, 's'},
+        {"ns-changes", 0, NULL, 'S' },
         {"uid-changes", 0, NULL, 'u'},
         {"unicode", 0, NULL, 'U'},
         {"version", 0, NULL, 'V'},
@@ -1106,11 +1124,11 @@         /* problems with VT100 on some t
 
 #ifdef WITH_SELINUX
     while ((c =
-            getopt_long(argc, argv, "aAcGhH:nN:pglsuUVZ", options,
+            getopt_long(argc, argv, "aAcGhH:nN:pglsSuUVZ", options,
                         NULL)) != -1)
 #else                                /*WITH_SELINUX */
     while ((c =
-            getopt_long(argc, argv, "aAcGhH:nN:pglsuUV", options, NULL)) != -1)
+            getopt_long(argc, argv, "aAcGhH:nN:pglsSuUV", options, NULL)) != -1)
 #endif                                /*WITH_SELINUX */
         switch (c) {
         case 'a':
@@ -1174,6 +1192,9 @@             compact = 0;
         case 's':
             show_parents = 1;
             break;
+        case 'S':
+            ns_change = 1;
+            break;
         case 'u':
             user_change = 1;
             break;