pgrep: Look at all ancestors in --ignore-ancestors

Previously we mistakenly only checked one previous level of the
hierarchy.

Signed-off-by: Chris Down <chris@chrisdown.name>
This commit is contained in:
Chris Down 2022-08-31 09:59:47 +01:00
parent 8eee6cc48c
commit 2ba7e39f2b

View File

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