basic/
basic.rs

1//! Basic sandbox example
2
3use sandbox_rs::{SandboxBuilder, SeccompProfile};
4use std::time::Duration;
5
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("=== Sandbox RS - Basic Example ===\n");
8
9    // Create a sandbox with basic configuration
10    println!("[1] Creating sandbox with memory limit...");
11    let mut sandbox = SandboxBuilder::new("example-1")
12        .memory_limit(50 * 1024 * 1024) // 50MB
13        .cpu_limit_percent(50) // 50% of one CPU
14        .timeout(Duration::from_secs(5))
15        .seccomp_profile(SeccompProfile::Minimal)
16        .build()?;
17
18    println!("[*] Sandbox created: {}", sandbox.id());
19    println!("[*] Root: {}", sandbox.root().display());
20    println!(
21        "[*] Status: {}\n",
22        if sandbox.is_running() {
23            "running"
24        } else {
25            "idle"
26        }
27    );
28
29    // Try to run a simple command
30    println!("[2] Running 'echo hello' in sandbox...");
31    let result = sandbox.run("/bin/echo", &["hello", "world"])?;
32
33    println!("[*] Execution result:");
34    println!("Exit code: {}", result.exit_code);
35    println!("Wall time: {} ms", result.wall_time_ms);
36    println!("Memory peak: {} bytes", result.memory_peak);
37    println!("CPU time: {} μs", result.cpu_time_us);
38    println!("Timed out: {}\n", result.timed_out);
39
40    Ok(())
41}