Fight against zombies

This commit is contained in:
Mattéo Delabre 2021-01-27 12:42:01 +01:00
parent 30de38aa1e
commit cd6197caa3
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
1 changed files with 21 additions and 16 deletions

View File

@ -1,26 +1,31 @@
from itertools import product from itertools import product
import subprocess import signal
from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ' chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ '
# Prevent zombie processes
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
def check_pair(script, instr, outstr): def check_pair(script, instr, outstr):
process = Popen(
["/usr/bin/env", "bash", "-c", "--", script],
stdin=PIPE,
stdout=PIPE,
stderr=DEVNULL,
)
try: try:
result = subprocess.run( stdout, stderr = process.communicate(instr.encode(), timeout=5)
[ return process.returncode == 0 and stdout == outstr.encode()
"/usr/bin/env", "bash", "-c", except TimeoutExpired:
";\n".join(( try:
# Cleanup child processes on exit process.kill()
"trap 'kill -9 $(jobs -p)' SIGINT SIGTERM EXIT", stdout, stderr = process.communicate()
script, except ProcessLookupError:
)) pass
],
timeout=5,
input=instr.encode(),
capture_output=True,
)
return result.returncode == 0 and result.stdout == outstr.encode()
except subprocess.TimeoutExpired:
return False return False