redox/programs/contain/src/main.rs

49 lines
1.2 KiB
Rust
Raw Normal View History

extern crate syscall;
2016-11-17 20:12:02 +01:00
use std::os::unix::process::CommandExt;
use std::process::Command;
pub fn main() {
2016-11-17 20:12:02 +01:00
let names = [
"file",
"rand",
"tcp",
"udp"
];
let command = "sh";
2016-11-25 20:09:54 +01:00
let mut name_ptrs = Vec::new();
for name in names.iter() {
name_ptrs.push([name.as_ptr() as usize, name.len()]);
}
let new_ns = syscall::mkns(&name_ptrs).unwrap();
2016-11-17 06:14:02 +01:00
let pid = unsafe { syscall::clone(0).unwrap() };
if pid == 0 {
2016-11-25 20:09:54 +01:00
syscall::setrens(new_ns, new_ns).unwrap();
2016-11-17 06:14:02 +01:00
2016-11-25 20:09:54 +01:00
println!("Container {}: enter: {}", new_ns, command);
2016-11-17 06:14:02 +01:00
2016-11-17 20:12:02 +01:00
let err = Command::new(command).exec();
panic!("contain: failed to launch {}: {}", command, err);
} else {
let mut status = 0;
syscall::waitpid(pid, &mut status, 0).unwrap();
loop {
let mut c_status = 0;
let c_pid = syscall::waitpid(0, &mut c_status, syscall::WNOHANG).unwrap();
if c_pid == 0 {
break;
} else {
println!("Container zombie {}: {:X}", c_pid, c_status);
}
}
2016-11-25 20:09:54 +01:00
println!("Container {}: exit: {:X}", new_ns, status);
}
}