mirror of
https://github.com/mii443/izoli.git
synced 2025-08-22 16:05:40 +00:00
add derive
This commit is contained in:
@ -5,7 +5,7 @@ use izolilib::{
|
|||||||
cgroup::CGroup, cgroup_option::CGroupOption, cpu_limit::CpuLimit,
|
cgroup::CGroup, cgroup_option::CGroupOption, cpu_limit::CpuLimit,
|
||||||
limit_value::CGroupLimitValue,
|
limit_value::CGroupLimitValue,
|
||||||
},
|
},
|
||||||
izolibox::IzoliBox,
|
izolibox::{IzoliBox, IzoliBoxOptions},
|
||||||
};
|
};
|
||||||
use nix::{sys::wait::waitpid, unistd::execvp};
|
use nix::{sys::wait::waitpid, unistd::execvp};
|
||||||
use tracing::Level;
|
use tracing::Level;
|
||||||
@ -23,12 +23,14 @@ fn main() {
|
|||||||
|
|
||||||
let izolibox = IzoliBox::new(
|
let izolibox = IzoliBox::new(
|
||||||
1,
|
1,
|
||||||
Some(CGroupOption {
|
IzoliBoxOptions {
|
||||||
cpu_max: Some(CpuLimit {
|
cgroup_option: Some(CGroupOption {
|
||||||
max: CGroupLimitValue::Max,
|
cpu_max: Some(CpuLimit {
|
||||||
period: 100000,
|
max: CGroupLimitValue::Max,
|
||||||
|
period: 100000,
|
||||||
|
}),
|
||||||
}),
|
}),
|
||||||
}),
|
},
|
||||||
);
|
);
|
||||||
let pid = izolibox
|
let pid = izolibox
|
||||||
.enter(Box::new(|| {
|
.enter(Box::new(|| {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::{fmt, str::FromStr};
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum CGroupLimitValue<T>
|
pub enum CGroupLimitValue<T>
|
||||||
where
|
where
|
||||||
T: FromStr + std::fmt::Display,
|
T: FromStr + std::fmt::Display,
|
||||||
|
@ -14,12 +14,17 @@ const STACK_SIZE: usize = 8192;
|
|||||||
|
|
||||||
pub struct IzoliBox {
|
pub struct IzoliBox {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
|
pub options: IzoliBoxOptions,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct IzoliBoxOptions {
|
||||||
pub cgroup_option: Option<CGroupOption>,
|
pub cgroup_option: Option<CGroupOption>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IzoliBox {
|
impl IzoliBox {
|
||||||
pub fn new(id: usize, cgroup_option: Option<CGroupOption>) -> Self {
|
pub fn new(id: usize, options: IzoliBoxOptions) -> Self {
|
||||||
Self { id, cgroup_option }
|
Self { id, options }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter(&self, callback: CloneCb<'_>) -> Result<Pid, nix::errno::Errno> {
|
pub fn enter(&self, callback: CloneCb<'_>) -> Result<Pid, nix::errno::Errno> {
|
||||||
@ -31,7 +36,7 @@ impl IzoliBox {
|
|||||||
| CloneFlags::CLONE_NEWPID
|
| CloneFlags::CLONE_NEWPID
|
||||||
| CloneFlags::CLONE_NEWNET;
|
| CloneFlags::CLONE_NEWNET;
|
||||||
|
|
||||||
if let Some(cgroup_option) = &self.cgroup_option {
|
if let Some(cgroup_option) = &self.options.cgroup_option {
|
||||||
let cgroup = CGroup::new(&format!("izoli/box_{}", self.id)).unwrap();
|
let cgroup = CGroup::new(&format!("izoli/box_{}", self.id)).unwrap();
|
||||||
cgroup.apply_options(cgroup_option).unwrap();
|
cgroup.apply_options(cgroup_option).unwrap();
|
||||||
cgroup.enter().unwrap();
|
cgroup.enter().unwrap();
|
||||||
|
Reference in New Issue
Block a user