use sandbox_rs::{SandboxBuilder, SeccompProfile, StreamChunk};
use std::time::Duration;
use tempfile::tempdir;
fn main() -> sandbox_rs::Result<()> {
let tmp = tempdir().expect("Failed to create temp dir");
let mut sandbox = SandboxBuilder::new("non-blocking-example")
.memory_limit_str("256M")?
.cpu_limit_percent(50)
.seccomp_profile(SeccompProfile::Unrestricted)
.root(tmp.path())
.build()?;
println!("Running process with non-blocking stream reads...\n");
let (result, stream) = sandbox.run_with_stream(
"/bin/bash",
&[
"-c",
"for i in {1..3}; do echo \"Message $i\"; sleep 0.1; done",
],
)?;
println!("Non-blocking polling:");
let mut received_chunks = 0;
let mut polling_attempts = 0;
let mut final_exit_code = result.exit_code;
loop {
polling_attempts += 1;
match stream.try_recv()? {
Some(chunk) => match chunk {
StreamChunk::Stdout(line) => {
println!("[STDOUT] {}", line);
received_chunks += 1;
}
StreamChunk::Stderr(line) => {
eprintln!("[STDERR] {}", line);
received_chunks += 1;
}
StreamChunk::Exit {
exit_code,
signal: _,
} => {
println!("Process exited with code: {}", exit_code);
final_exit_code = exit_code;
break;
}
},
None => {
std::thread::sleep(Duration::from_millis(10));
}
}
if polling_attempts > 10000 {
println!("Safety timeout reached");
break;
}
}
let mut result = result;
result.exit_code = final_exit_code;
println!("\nStatistics:");
println!(" Chunks received: {}", received_chunks);
println!(" Polling attempts: {}", polling_attempts);
println!(" Exit code: {}", result.exit_code);
println!(" Wall time: {} ms", result.wall_time_ms);
result.check_seccomp_error()?;
Ok(())
}