]> granicus.if.org Git - procps-ng/commitdiff
pgrep: Look at all ancestors in --ignore-ancestors
authorChris Down <chris@chrisdown.name>
Wed, 31 Aug 2022 08:59:47 +0000 (09:59 +0100)
committerChris Down <chris@chrisdown.name>
Wed, 31 Aug 2022 09:25:49 +0000 (10:25 +0100)
Previously we mistakenly only checked one previous level of the
hierarchy.

Signed-off-by: Chris Down <chris@chrisdown.name>
src/pgrep.c

index 5d13c28d1796748c72bd5c2183bb0458e37b315c..c4ad5da381808eae31cc6d3e6f1232887b7839a3 100644 (file)
@@ -199,7 +199,6 @@ static int __attribute__ ((__noreturn__)) usage(int opt)
 static struct el *get_our_ancestors(void)
 {
 #define PIDS_GETINT(e) PIDS_VAL(EU_##e, s_int, stack, info)
-    struct pids_info *info = NULL;
     struct el *list = NULL;
     int i = 0;
     int size = 0;
@@ -207,27 +206,28 @@ static struct el *get_our_ancestors(void)
     pid_t search_pid = getpid();
     struct pids_stack *stack;
 
-    if (procps_pids_new(&info, Items, 15) < 0)
-        xerrx(EXIT_FATAL, _("Unable to create pid info structure"));
-
     while (!done) {
-        if (search_pid == 0)
-            break;
+        struct pids_info *info = NULL;
+
+        if (procps_pids_new(&info, Items, 15) < 0)
+            xerrx(EXIT_FATAL, _("Unable to create pid info structure"));
 
         if (i == size) {
             grow_size(size);
             list = xrealloc(list, (1 + size) * sizeof(*list));
         }
 
+        done = 1;
         while ((stack = procps_pids_get(info, PIDS_FETCH_TASKS_ONLY))) {
             if (PIDS_GETINT(PID) == search_pid) {
                 list[++i].num = PIDS_GETINT(PPID);
                 search_pid = list[i].num;
+                done = 0;
                 break;
             }
-
-            done = 1;
         }
+
+        procps_pids_unref(&info);
     }
 
     if (i == 0) {