Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/sandbox/get_or_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,12 @@ impl Sandbox {
close(std::io::stdin().as_raw_fd())
.context(format!("failed to close stdin"))?;

/* Exit on SIGTERM, with the signal number as our exit status. libc's
* exit has the C calling convention a signal handler needs, which
* std::process::exit doesn't. */
let exit: unsafe extern "C" fn(libc::c_int) -> ! = libc::exit;
unsafe {
libc::signal(
libc::SIGTERM,
std::process::exit as libc::sighandler_t,
);
libc::signal(libc::SIGTERM, exit as libc::sighandler_t);
}

// sleep forever, this process hangs around only to host the namespaces so other processes
Expand Down
34 changes: 34 additions & 0 deletions tests/ai_test_sigterm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
mod fixtures;

use anyhow::Result;
use fixtures::*;
use rstest::*;
use std::path::Path;
use std::time::{Duration, Instant};

#[rstest]
fn ai_test_sigterm_stops_sandbox(mut sandbox: SandboxManager) -> Result<()> {
sandbox.run(&["true"])?;

sandbox.run(&["config", "sandbox_dir"])?;
let pid_file = format!("{}.pid", sandbox.last_stdout.trim());
let pid = std::fs::read_to_string(&pid_file)?.trim().to_string();
let proc_dir = format!("/proc/{}", pid);
assert!(Path::new(&proc_dir).exists());

// The sandbox's PID 1 exits when asked to terminate
let status = std::process::Command::new("sudo")
.args(["kill", "-TERM", &pid])
.status()?;
assert!(status.success());
let deadline = Instant::now() + Duration::from_secs(5);
while Path::new(&proc_dir).exists() && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(10));
}
assert!(!Path::new(&proc_dir).exists());

// and the sandbox starts again the next time it's used
sandbox.run(&["echo", "restarted"])?;
assert_eq!(sandbox.last_stdout.trim(), "restarted");
Ok(())
}
Loading