]> granicus.if.org Git - psmisc/commitdiff
pstree sorts properly on names
authorCraig Small <csmall@users.sourceforge.net>
Fri, 20 Jul 2012 12:32:27 +0000 (22:32 +1000)
committerCraig Small <csmall@users.sourceforge.net>
Fri, 20 Jul 2012 12:32:27 +0000 (22:32 +1000)
pstree added temporary parents to processes, so if a child process
was lexigraphically before the parents or its "aunties and uncles"
then they were sorted with the parent temporary name "?".

This patch reorders the children in the parents list when they
are renamed.

ChangeLog
src/pstree.c

index dafefa292c7f0ed1b18b280e0ab28fee35c27c5a..1c5d57b22b725569bce79b5d8d51c807b62b293a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+Changes in 22.20
+================
+       * pstree sorts properly on names Debian #682014
 Changes in 22.19
 ================
        * killall with no args exits again SF #3536526
index dd7045b5f18b09743b00bb213ad5b2a972384615..9d9e8d524e9ca94bd0bcaa21ec3fba6b5ff9c568 100644 (file)
@@ -296,8 +296,8 @@ static void add_child(PROC * parent, PROC * child)
         if (by_pid) {
             if ((*walk)->child->pid > child->pid)
                 break;
-        } else if ((cmp = strcmp((*walk)->child->comm, child->comm)) > 0)
-            break;
+        } else if ((cmp = strcmp((*walk)->child->comm, child->comm)) > 0) {
+            break; }
         else if (!cmp && (*walk)->child->uid > child->uid)
             break;
     new->next = *walk;
@@ -335,6 +335,28 @@ static void set_args(PROC * this, const char *args, int size)
         this->argv[i] = start = strchr(start, 0) + 1;
 }
 
+static void
+rename_proc(PROC *this, const char *comm, uid_t uid)
+{
+    PROC *tmp_child, *parent;
+       CHILD **walk;
+
+    strncpy(this->comm, comm, COMM_LEN+2);
+    this->comm[COMM_LEN+1] = '\0';
+    this->uid = uid;
+
+       /* Re-sort children in parent, now we have a name */
+       if (!by_pid && this->parent) {
+           parent = this->parent;
+        for (walk = &parent->children; *walk; walk = &(*walk)->next) {
+                 if ( ((*walk)->next != NULL) && strcmp((*walk)->child->comm, (*walk)->next->child->comm) > 0 ) {
+                       tmp_child = (*walk)->child;
+                       (*walk)->child = (*walk)->next->child;
+                       (*walk)->next->child = tmp_child;
+                 }
+               }
+       }
+}
 #ifdef WITH_SELINUX
 static void
 add_proc(const char *comm, pid_t pid, pid_t ppid, pid_t pgid, uid_t uid,
@@ -354,9 +376,7 @@ add_proc(const char *comm, pid_t pid, pid_t ppid, pid_t pgid, uid_t uid,
         this = new_proc(comm, pid, uid);
 #endif                                /*WITH_SELINUX */
     else {
-        strncpy(this->comm, comm, COMM_LEN+2);
-        this->comm[COMM_LEN+1] = '\0';
-        this->uid = uid;
+           rename_proc(this, comm, uid);
     }
     if (args)
         set_args(this, args, size);
@@ -778,35 +798,6 @@ static void read_proc(void)
   }
 }
 
-
-#if 0
-
-/* Could use output of  ps achlx | awk '{ print $3,$4,$2,$13 }'  */
-
-static void read_stdin(void)
-{
-    char comm[PATH_MAX + 1];
-    char *cmd;
-    int pid, ppid, uid;
-
-    while (scanf("%d %d %d %s\n", &pid, &ppid, &uid, comm) == 4) {
-        if (cmd = strrchr(comm, '/'))
-            cmd++;
-        else
-            cmd = comm;
-        if (*cmd == '-')
-            cmd++;
-#ifdef WITH_SELINUX
-        add_proc(cmd, pid, ppid, uid, NULL, 0, NULL);
-#else                                /*WITH_SELINUX */
-        add_proc(cmd, pid, ppid, uid, NULL, 0);
-#endif                                /*WITH_SELINUX */
-    }
-}
-
-#endif
-
-
 static void usage(void)
 {
     fprintf(stderr,