Start using function pointers
This commit is contained in:
parent
dec755a2a5
commit
769132af53
@ -11,5 +11,5 @@ fn main() {
|
||||
//namespace::create_ns::create_ns();
|
||||
let ns_name = "test-newns".to_string();
|
||||
block_on(NetworkNamespace::add(ns_name.clone()));
|
||||
namespace::bind_interface::run_in_namespace(&ns_name);
|
||||
namespace::bind_interface::run_in_namespace(|| {namespace::bind_interface::set_lo_up().unwrap();},&ns_name);
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ use std::process::exit;
|
||||
use std::os::unix::io::RawFd;
|
||||
use std::os::fd::FromRawFd;
|
||||
|
||||
pub fn run_in_namespace(ns_name: &String) -> Result<(), ()> {
|
||||
pub fn run_in_namespace<F>(f: F,ns_name: &String) -> Result<(), ()> where F:FnMut() + Copy {
|
||||
// Configure networking in the child namespace:
|
||||
// Fork a process that is set to the newly created namespace
|
||||
// Here set the veth ip addr, routing tables etc.
|
||||
@ -34,12 +34,12 @@ pub fn run_in_namespace(ns_name: &String) -> Result<(), ()> {
|
||||
|
||||
unsafe {
|
||||
match clone(
|
||||
Box::new(|| run_child(&ns_name.clone())),
|
||||
Box::new(|| run_child(f,&ns_name.clone())),
|
||||
&mut tmp_stack,
|
||||
flags,
|
||||
Some(Signal::SIGCHLD as i32)) {
|
||||
Ok(pid) => Ok(()),
|
||||
Err(e) => {
|
||||
Ok(_pid) => Ok(()),
|
||||
Err(_e) => {
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
@ -47,8 +47,8 @@ pub fn run_in_namespace(ns_name: &String) -> Result<(), ()> {
|
||||
|
||||
}
|
||||
|
||||
fn run_child(ns_name: &String) -> isize {
|
||||
let res = split_namespace(ns_name);
|
||||
fn run_child<F>(mut f: F, ns_name: &String) -> isize where F:FnMut() {
|
||||
let res = prepare_namespace(ns_name);
|
||||
|
||||
match res {
|
||||
Err(_) => {
|
||||
@ -57,12 +57,13 @@ fn run_child(ns_name: &String) -> isize {
|
||||
}
|
||||
Ok(()) => {
|
||||
log::debug!("Child exited normally");
|
||||
f();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn split_namespace(ns_name: &String) -> Result<(), ()> {
|
||||
fn prepare_namespace(ns_name: &String) -> Result<(), ()> {
|
||||
// First create the network namespace
|
||||
// NetworkNamespace::add(ns_name.to_string()).await.map_err(|e| {
|
||||
// log::error!("Can not create namespace {}", e);
|
||||
@ -114,10 +115,16 @@ fn split_namespace(ns_name: &String) -> Result<(), ()> {
|
||||
// TODO do not exit for EINVAL error
|
||||
// unmount_path(&sys_path)?;
|
||||
// consider the case that a sysfs is not present
|
||||
let stat_sys = statvfs(&sys_path)
|
||||
let stat_sys = match statvfs(&sys_path)
|
||||
.map_err(|e| {
|
||||
log::error!("Can not stat sys: {}", e);
|
||||
}).unwrap();
|
||||
}){
|
||||
Ok(stat) => stat,
|
||||
Err(_e) => {
|
||||
log::error!("Error in stat sys");
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
if stat_sys.flags().contains(FsFlags::ST_RDONLY) {
|
||||
mount_flags.insert(MsFlags::MS_RDONLY);
|
||||
}
|
||||
@ -128,8 +135,6 @@ fn split_namespace(ns_name: &String) -> Result<(), ()> {
|
||||
()
|
||||
}
|
||||
|
||||
set_lo_up().unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user