2016-11-17 04:54:38 +01:00
|
|
|
extern crate syscall;
|
|
|
|
|
2016-11-17 20:12:02 +01:00
|
|
|
use std::os::unix::process::CommandExt;
|
|
|
|
use std::process::Command;
|
2016-11-17 04:54:38 +01:00
|
|
|
|
|
|
|
pub fn main() {
|
2016-11-17 20:12:02 +01:00
|
|
|
let names = [
|
|
|
|
"file",
|
|
|
|
"rand",
|
|
|
|
"tcp",
|
|
|
|
"udp"
|
|
|
|
];
|
|
|
|
|
|
|
|
let command = "sh";
|
|
|
|
|
2016-11-17 06:14:02 +01:00
|
|
|
let pid = unsafe { syscall::clone(0).unwrap() };
|
2016-11-17 04:54:38 +01:00
|
|
|
if pid == 0 {
|
2016-11-17 20:12:02 +01:00
|
|
|
let mut name_ptrs = Vec::new();
|
|
|
|
for name in names.iter() {
|
|
|
|
name_ptrs.push([name.as_ptr() as usize, name.len()]);
|
2016-11-17 04:54:38 +01:00
|
|
|
}
|
2016-11-17 06:14:02 +01:00
|
|
|
|
2016-11-17 20:12:02 +01:00
|
|
|
syscall::setns(&name_ptrs).unwrap();
|
2016-11-17 06:14:02 +01:00
|
|
|
|
2016-11-17 20:24:46 +01:00
|
|
|
println!("Container enter: {}", 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);
|
2016-11-17 04:54:38 +01:00
|
|
|
} else {
|
|
|
|
let mut status = 0;
|
|
|
|
syscall::waitpid(pid, &mut status, 0).unwrap();
|
|
|
|
|
2016-11-17 20:24:46 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Container exited: {:X}", status);
|
2016-11-17 04:54:38 +01:00
|
|
|
}
|
|
|
|
}
|