Commit ed07c351 authored by alina's avatar alina

subprocess: kills the whole process group when exits to avoid leaving unkilled forked processes

parent 94ce2805
...@@ -96,7 +96,8 @@ class Server(object): ...@@ -96,7 +96,8 @@ class Server(object):
def clean(self): def clean(self):
try: try:
for pid in self._children: for pid in self._children:
os.kill(pid, signal.SIGTERM) # -PID to kill to whole process group
os.kill(-pid, signal.SIGTERM)
now = time.time() now = time.time()
ch = self._children ch = self._children
while time.time() - now < KILL_WAIT: while time.time() - now < KILL_WAIT:
...@@ -115,7 +116,8 @@ class Server(object): ...@@ -115,7 +116,8 @@ class Server(object):
for pid in ch: for pid in ch:
warning("Killing forcefully process %d." % pid) warning("Killing forcefully process %d." % pid)
os.kill(pid, signal.SIGKILL) # -PID to kill to whole process group
os.kill(-pid, signal.SIGKILL)
for pid in ch: for pid in ch:
try: try:
netns.subprocess_.poll(pid) netns.subprocess_.poll(pid)
...@@ -410,9 +412,10 @@ class Server(object): ...@@ -410,9 +412,10 @@ class Server(object):
self.reply(500, "Process does not exist.") self.reply(500, "Process does not exist.")
return return
if signal: if signal:
os.kill(pid, sig) # -PID to kill to whole process group
os.kill(-pid, sig)
else: else:
os.kill(pid, signal.SIGTERM) os.kill(-pid, signal.SIGTERM)
self.reply(200, "Process signalled.") self.reply(200, "Process signalled.")
def do_IF_LIST(self, cmdname, ifnr = None): def do_IF_LIST(self, cmdname, ifnr = None):
......
...@@ -316,6 +316,10 @@ def spawn(executable, argv = None, cwd = None, env = None, close_fds = False, ...@@ -316,6 +316,10 @@ def spawn(executable, argv = None, cwd = None, env = None, close_fds = False,
for i in close_fds: for i in close_fds:
os.close(i) os.close(i)
# changing process group id
# (it is necessary to kill the forked subprocesses)
os.setpgrp()
if user != None: if user != None:
# Change user # Change user
os.setgid(gid) os.setgid(gid)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment