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