Skip to content

Commit

Permalink
Reset signal dispositions before running exec
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlindholm authored and austrin committed Nov 9, 2019
1 parent fbda1cb commit eb7b607
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions problemtools/run/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,35 @@ def __run_wait(argv, infile, outfile, errfile, timelim, memlim):
pid = os.fork()
if pid == 0: # child
try:
# The Python interpreter internally sets some signal dispositions
# to SIG_IGN (notably SIGPIPE), and unless we reset them manually
# this leaks through to the program we exec. That can has some
# funny side effects, like programs not crashing as expected when
# trying to write to an interactive validator that has terminated
# and closed the read end of a pipe.
#
# This *shouldn't* cause any verdict changes given the setup for
# interactive problems, but reset them anyway, for sanity.
if hasattr(signal, "SIGPIPE"):
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
if hasattr(signal, "SIGXFZ"):
signal.signal(signal.SIGXFZ, signal.SIG_DFL)
if hasattr(signal, "SIGXFSZ"):
signal.signal(signal.SIGXFSZ, signal.SIG_DFL)

if timelim is not None:
limit.try_limit(resource.RLIMIT_CPU, timelim, timelim + 1)
if memlim is not None:
limit.try_limit(resource.RLIMIT_AS, memlim * (1024**2), resource.RLIM_INFINITY)
limit.try_limit(resource.RLIMIT_STACK,
resource.RLIM_INFINITY, resource.RLIM_INFINITY)

Program.__setfd(0, infile, os.O_RDONLY)
Program.__setfd(1, outfile,
os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
Program.__setfd(2, errfile,
os.O_WRONLY | os.O_CREAT | os.O_TRUNC)

os.execvp(argv[0], argv)
except Exception as exc:
print("Oops. Fatal error in child process:")
Expand Down

0 comments on commit eb7b607

Please sign in to comment.