mirror of https://github.com/fail2ban/fail2ban
BF: kill the entire process group upon timeout (Close #1129)
Requires also establishing a new process group for a child process, which changes previous behaviorpull/1136/head
parent
cf2feea987
commit
7112e4f6c6
|
@ -565,7 +565,9 @@ class CommandAction(ActionBase):
|
||||||
stderr = tempfile.TemporaryFile(suffix=".stderr", prefix="fai2ban_")
|
stderr = tempfile.TemporaryFile(suffix=".stderr", prefix="fai2ban_")
|
||||||
try:
|
try:
|
||||||
popen = subprocess.Popen(
|
popen = subprocess.Popen(
|
||||||
realCmd, stdout=stdout, stderr=stderr, shell=True)
|
realCmd, stdout=stdout, stderr=stderr, shell=True,
|
||||||
|
preexec_fn=os.setsid # so that killpg does not kill our process
|
||||||
|
)
|
||||||
stime = time.time()
|
stime = time.time()
|
||||||
retcode = popen.poll()
|
retcode = popen.poll()
|
||||||
while time.time() - stime <= timeout and retcode is None:
|
while time.time() - stime <= timeout and retcode is None:
|
||||||
|
@ -574,11 +576,12 @@ class CommandAction(ActionBase):
|
||||||
if retcode is None:
|
if retcode is None:
|
||||||
logSys.error("%s -- timed out after %i seconds." %
|
logSys.error("%s -- timed out after %i seconds." %
|
||||||
(realCmd, timeout))
|
(realCmd, timeout))
|
||||||
os.kill(popen.pid, signal.SIGTERM) # Terminate the process
|
pgid = os.getpgid(popen.pid)
|
||||||
|
os.killpg(pgid, signal.SIGTERM) # Terminate the process
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
retcode = popen.poll()
|
retcode = popen.poll()
|
||||||
if retcode is None: # Still going...
|
if retcode is None: # Still going...
|
||||||
os.kill(popen.pid, signal.SIGKILL) # Kill the process
|
os.killpg(pgid, signal.SIGKILL) # Kill the process
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
retcode = popen.poll()
|
retcode = popen.poll()
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
|
|
Loading…
Reference in New Issue