bunpen: refactor: split fork_and_die_with_parent out as a standalone helper

This commit is contained in:
2024-12-19 07:48:31 +00:00
parent 8a8bb0f0bd
commit 066bf3c3d4

View File

@@ -16,8 +16,19 @@ use unix::signal;
// forward any signals, including SIGKILL, to the child.
fn fork_and_propagate() (void | os::exec::error | rt::errno) = {
let outer_pid = rt::getpid();
match (os::exec::fork()?) {
case let child_pid: os::exec::process => return wait_and_propagate(child_pid);
return match (fork_and_die_with_parent()?) {
case let child_pid: os::exec::process => yield wait_and_propagate(child_pid);
case =>
log::println("[namespace/fork] continuing as child");
};
};
// fork, but configured so that the child receives SIGKILL upon the parent's exit,
// ensuring that the child may never outlive the parent.
fn fork_and_die_with_parent() (os::exec::process | void | os::exec::error | rt::errno) = {
let outer_pid = rt::getpid();
return match (os::exec::fork()?) {
case let child_pid: os::exec::process => yield child_pid;
case =>
log::println("[namespace/fork] child: configuring parent death signal");
// configure that when the parent dies, we receive SIGTERM.
@@ -25,10 +36,10 @@ fn fork_and_propagate() (void | os::exec::error | rt::errno) = {
// when someone `kill`s the sandbox wrapper, it will properly kill the
// actual sandboxed application.
//
// in the normal mode of operation, SIGKILL is not sent. rather, the
// parent will receive SIGTERM, SIGHUP, etc, in `wait_and_propagate`,
// forward that to the child, the child gracefully exits, and the parent
// forwards the exit status.
// in the normal mode of operation (when used with `wait_and_propagate`),
// SIGKILL is not sent. rather, the parent will receive SIGTERM, SIGHUP,
// etc, in `wait_and_propagate`, forward that to the child, the child
// gracefully exits, and the parent forwards the exit status.
// SIGKILL is sent to the child only when the parent exits without waiting
// for the child, likely because it was killed by SIGKILL, itself.
rt::prctl(rt::PR_SET_PDEATHSIG, rt::SIGKILL: u64, 0, 0, 0)?;
@@ -39,7 +50,7 @@ fn fork_and_propagate() (void | os::exec::error | rt::errno) = {
// the parent exited between the `fork` and `prctl` operations
return errors::cancelled;
};
log::println("[namespace/fork] child: configured; continuing execution");
log::println("[namespace/fork] child: configured");
};
};