add add/remove_subtree_control

This commit is contained in:
mii443
2024-10-17 08:30:52 +00:00
parent d618ea6ea0
commit 9bcea10ce4
3 changed files with 57 additions and 4 deletions

View File

@ -1,7 +1,7 @@
use izolilib::cgroup::cgroup::CGroup;
use izolilib::cgroup::{cgroup::CGroup, controller::Controller};
fn main() {
let cgroup = CGroup::new("").unwrap();
let cgroup = CGroup::new("test").unwrap();
println!("{:?}", cgroup.get_root_path());
println!("{}", cgroup.check_status());
println!("{:?}", cgroup.read("cgroup.type"));

View File

@ -1,6 +1,6 @@
use std::{
fs::{self, File},
io::Read,
io::{Read, Write},
path::PathBuf,
str::FromStr,
};
@ -38,6 +38,14 @@ impl CGroup {
Ok(buf)
}
pub fn write(&self, name: &str, data: &str) -> Result<(), std::io::Error> {
let path = self.get_file_path(name);
let mut file = File::options().append(true).open(path)?;
file.write_all(data.as_bytes())?;
Ok(())
}
pub fn check_status(&self) -> bool {
let root = self.get_root_path();
@ -99,6 +107,35 @@ impl CGroup {
self.get_limit_value("cgroup.max.descendants")
}
// cgroup files write
pub fn add_subtree_control(&self, controllers: Vec<Controller>) -> Result<(), std::io::Error> {
let to_write = controllers
.iter()
.map(|controller| format!("+{}", controller))
.collect::<Vec<String>>()
.join(" ");
self.write("cgroup.subtree_control", &to_write)?;
Ok(())
}
pub fn remove_subtree_control(
&self,
controllers: Vec<Controller>,
) -> Result<(), std::io::Error> {
let to_write = controllers
.iter()
.map(|controller| format!("-{}", controller))
.collect::<Vec<String>>()
.join(" ");
self.write("cgroup.subtree_control", &to_write)?;
Ok(())
}
fn get_u32_list(&self, name: &str) -> Result<Vec<u32>, std::io::Error> {
let procs = self
.read(name)?

View File

@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{fmt, str::FromStr};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Controller {
@ -33,3 +33,19 @@ impl FromStr for Controller {
}
}
}
impl fmt::Display for Controller {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Controller::Cpu => write!(f, "cpu"),
Controller::Cpuset => write!(f, "cpuset"),
Controller::Memory => write!(f, "memory"),
Controller::Io => write!(f, "io"),
Controller::Hugetlb => write!(f, "hugetlb"),
Controller::Misc => write!(f, "misc"),
Controller::Pids => write!(f, "pids"),
Controller::Rdma => write!(f, "rdma"),
Controller::Unknown => write!(f, "unknown"),
}
}
}