diff --git a/endpoints/api/suconfig.py b/endpoints/api/suconfig.py index 7221ac889..c2fca2502 100644 --- a/endpoints/api/suconfig.py +++ b/endpoints/api/suconfig.py @@ -3,6 +3,7 @@ import logging import os import signal +import subprocess from flask import abort @@ -136,6 +137,21 @@ class SuperUserSetupDatabase(ApiResource): abort(403) +# From: https://stackoverflow.com/a/44712205 +def get_process_id(name): + """Return process ids found by (partial) name or regex. + + >>> get_process_id('kthreadd') + [2] + >>> get_process_id('watchdog') + [10, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61] # ymmv + >>> get_process_id('non-existent process') + [] + """ + child = subprocess.Popen(['pgrep', name], stdout=subprocess.PIPE, shell=False) + response = child.communicate()[0] + return [int(pid) for pid in response.split()] + @resource('/v1/superuser/shutdown') @internal_only @@ -154,7 +170,7 @@ class SuperUserShutdown(ApiResource): if app.config.get('DEBUGGING') == True: return {} - os.kill(1, signal.SIGINT) + os.kill(get_process_id('my_init')[0], signal.SIGINT) return {} abort(403)