From 55505e0b3e5b5b9ff222fdbbd5f876fb1386e31e Mon Sep 17 00:00:00 2001 From: Xuewei Niu Date: Tue, 1 Aug 2023 11:50:09 +0800 Subject: [PATCH] Minor changes for cgroup and devices subsystem The changes include: - Expose `create()` and add `exists()` for `Cgroup`: The changes are allowed to load cgroup and test if the cgroup exists. If not exists, performing the `create()` directly to avoid performing `new()`. - Make path of devices cgroup error more details: The origin path is either `devices.allow` or `devices.deny`. It not shows which cgroup it belongs to. Signed-off-by: Xuewei Niu --- src/cgroup.rs | 9 ++++++++- src/devices.rs | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/cgroup.rs b/src/cgroup.rs index 988d9e1..fc09eac 100644 --- a/src/cgroup.rs +++ b/src/cgroup.rs @@ -74,7 +74,7 @@ impl Cgroup { } /// Create this control group. - fn create(&self) -> Result<()> { + pub fn create(&self) -> Result<()> { if self.hier.v2() { create_v2_cgroup(self.hier.root(), &self.path, &self.specified_controllers) } else { @@ -492,6 +492,13 @@ impl Cgroup { v.dedup(); v } + + /// Checks if the cgroup exists. + /// + /// Returns true if at least one subsystem exists. + pub fn exists(&self) -> bool { + self.subsystems().iter().any(|e| e.to_controller().exists()) + } } pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup"; diff --git a/src/devices.rs b/src/devices.rs index eef95f1..9a10772 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -239,7 +239,13 @@ impl DevicesController { let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms); self.open_path("devices.allow", true).and_then(|mut file| { file.write_all(final_str.as_ref()).map_err(|e| { - Error::with_cause(WriteFailed("devices.allow".to_string(), final_str), e) + Error::with_cause( + WriteFailed( + self.get_path().join("devices.allow").display().to_string(), + final_str, + ), + e, + ) }) }) } @@ -272,7 +278,13 @@ impl DevicesController { let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms); self.open_path("devices.deny", true).and_then(|mut file| { file.write_all(final_str.as_ref()).map_err(|e| { - Error::with_cause(WriteFailed("devices.deny".to_string(), final_str), e) + Error::with_cause( + WriteFailed( + self.get_path().join("devices.deny").display().to_string(), + final_str, + ), + e, + ) }) }) }