From 06b1317cf19fb54713e144710e35f2cbbc8dd3dd Mon Sep 17 00:00:00 2001 From: Amador Pahim Date: Thu, 23 Feb 2017 12:37:49 +0100 Subject: [PATCH] avocado.utils.process: recursive get_children_pids() The current implementation of get_children_pids() returns only the first level of subprocesses. This patch adds the option to recursively create the list of subprocesses from all sub-levels. Signed-off-by: Amador Pahim --- avocado/utils/process.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/avocado/utils/process.py b/avocado/utils/process.py index 56ed691b..09dd1f2c 100644 --- a/avocado/utils/process.py +++ b/avocado/utils/process.py @@ -203,13 +203,28 @@ def process_in_ptree_is_defunct(ppid): return defunct -def get_children_pids(ppid): +def get_children_pids(ppid, recursive=False): """ Get all PIDs of children/threads of parent ppid param ppid: parent PID + param recursive: True to return all levels of subprocesses return: list of PIDs of all children/threads of ppid """ - return system_output("ps -L --ppid=%d -o lwp" % ppid, verbose=False).split('\n')[1:] + + cmd = "ps -L --ppid=%d -o lwp" + + # Getting first level of subprocesses + children = system_output(cmd % ppid, verbose=False).split('\n')[1:] + if not recursive: + return children + + # Recursion to get all levels of subprocesses + for child in children: + children.extend(system_output(cmd % int(child), + verbose=False, + ignore_status=True).split('\n')[1:]) + + return children def binary_from_shell_cmd(cmd): -- GitLab