未验证 提交 5ebce824 编写于 作者: L Lukáš Doktor

Merging pull request 1816

Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>

* https://github.com/avocado-framework/avocado:
  SIGTERM handler
  avocado.utils.process: recursive get_children_pids()
......@@ -25,6 +25,7 @@ from .dispatcher import CLICmdDispatcher
from .dispatcher import CLIDispatcher
from .output import STD_OUTPUT
from .parser import Parser
from ..utils import process
class AvocadoApp(object):
......@@ -38,6 +39,13 @@ class AvocadoApp(object):
# Catch all libc runtime errors to STDERR
os.environ['LIBC_FATAL_STDERR_'] = '1'
def sigterm_handler(signum, frame): # pylint: disable=W0613
children = process.get_children_pids(os.getpid())
for child in children:
process.kill_process_tree(int(child), sig=signal.SIGTERM)
raise SystemExit('Terminated')
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore ctrl+z
self.parser = Parser()
output.early_start()
......
......@@ -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):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册