Skip to content

Commit

Permalink
catch child process's exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
twd2 committed Mar 18, 2024
1 parent 408349a commit 2d9379c
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions jd4/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ def _handle_reset_child():
def _handle_compile(compiler_file, compiler_args, output_file, cgroup_file):
pid = fork()
if not pid:
chdir('/out')
os_close(STDIN_FILENO)
if output_file:
fd = os_open(output_file, O_WRONLY)
dup2(fd, STDOUT_FILENO)
dup2(fd, STDERR_FILENO)
os_close(fd)
if cgroup_file:
enter_cgroup(cgroup_file)
execve(compiler_file, compiler_args, SPAWN_ENV)
try:
chdir('/out')
os_close(STDIN_FILENO)
if output_file:
fd = os_open(output_file, O_WRONLY)
dup2(fd, STDOUT_FILENO)
dup2(fd, STDERR_FILENO)
os_close(fd)
if cgroup_file:
enter_cgroup(cgroup_file)
execve(compiler_file, compiler_args, SPAWN_ENV)
except:
exit(1)
return wait_and_reap_zombies(pid)


Expand All @@ -84,29 +87,32 @@ def _handle_execute(execute_file,
cgroup_file):
pid = fork()
if not pid:
chdir('/in/package')
if stdin_file:
fd = os_open(stdin_file, O_RDONLY)
dup2(fd, STDIN_FILENO)
os_close(fd)
if stdout_file:
fd = os_open(stdout_file, O_WRONLY)
dup2(fd, STDOUT_FILENO)
os_close(fd)
if stderr_file:
fd = os_open(stderr_file, O_WRONLY)
dup2(fd, STDERR_FILENO)
os_close(fd)
if extra_file:
fd = os_open(extra_file, O_RDONLY)
if fd == EXTRA_FILENO:
set_inheritable(fd, True)
else:
dup2(fd, EXTRA_FILENO)
try:
chdir('/in/package')
if stdin_file:
fd = os_open(stdin_file, O_RDONLY)
dup2(fd, STDIN_FILENO)
os_close(fd)
if stdout_file:
fd = os_open(stdout_file, O_WRONLY)
dup2(fd, STDOUT_FILENO)
os_close(fd)
if stderr_file:
fd = os_open(stderr_file, O_WRONLY)
dup2(fd, STDERR_FILENO)
os_close(fd)
if cgroup_file:
enter_cgroup(cgroup_file)
execve(execute_file, execute_args, SPAWN_ENV)
if extra_file:
fd = os_open(extra_file, O_RDONLY)
if fd == EXTRA_FILENO:
set_inheritable(fd, True)
else:
dup2(fd, EXTRA_FILENO)
os_close(fd)
if cgroup_file:
enter_cgroup(cgroup_file)
execve(execute_file, execute_args, SPAWN_ENV)
except:
exit(1)
return wait_and_reap_zombies(pid)

_HANDLERS = {
Expand Down

0 comments on commit 2d9379c

Please sign in to comment.