From 09ff168cea35d70bda783527e62eec41ce1a648d Mon Sep 17 00:00:00 2001 From: mii443 Date: Thu, 17 Oct 2024 01:36:05 +0000 Subject: [PATCH] wip --- src/bin/izoli.rs | 8 ++++++++ src/cgroup/cgroup.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/cgroup/mod.rs | 1 + 3 files changed, 49 insertions(+) create mode 100644 src/bin/izoli.rs create mode 100644 src/cgroup/cgroup.rs create mode 100644 src/cgroup/mod.rs diff --git a/src/bin/izoli.rs b/src/bin/izoli.rs new file mode 100644 index 0000000..8343580 --- /dev/null +++ b/src/bin/izoli.rs @@ -0,0 +1,8 @@ +use izolilib::cgroup::cgroup::CGroup; + +fn main() { + println!("test"); + let cgroup = CGroup::new("test").unwrap(); + println!("{:?}", cgroup.get_root_path()); + println!("{}", cgroup.check_status()); +} diff --git a/src/cgroup/cgroup.rs b/src/cgroup/cgroup.rs new file mode 100644 index 0000000..ae1051b --- /dev/null +++ b/src/cgroup/cgroup.rs @@ -0,0 +1,40 @@ +use std::{ + fs, + path::{Path, PathBuf}, +}; + +pub struct CGroup { + pub path: PathBuf, +} + +impl CGroup { + pub fn new(path: &str) -> Result { + let cgroup = CGroup { + path: PathBuf::from(path), + }; + + if !cgroup.check_status() { + cgroup.create()?; + } + + Ok(cgroup) + } + + fn create(&self) -> Result<(), std::io::Error> { + let root = self.get_root_path(); + fs::create_dir_all(root) + } + + pub fn check_status(&self) -> bool { + let root = self.get_root_path(); + + root.exists() && root.is_dir() + } + + pub fn get_root_path(&self) -> PathBuf { + let cgroup_root = PathBuf::from("/sys/fs/cgroup/"); + let root = cgroup_root.join(&self.path); + + root + } +} diff --git a/src/cgroup/mod.rs b/src/cgroup/mod.rs new file mode 100644 index 0000000..57f2888 --- /dev/null +++ b/src/cgroup/mod.rs @@ -0,0 +1 @@ +pub mod cgroup;