From fa94a1174fc906a11f4eb480529986e8c3fb96fe Mon Sep 17 00:00:00 2001 From: Zhang Tianyang Date: Sat, 26 Feb 2022 17:53:02 +0800 Subject: [PATCH] cgroup: support get cgroup relative paths by process pid support get cgroup relative paths by process pid Signed-off-by: Zhang Tianyang --- src/cgroup.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/cgroup.rs b/src/cgroup.rs index c28f022..f37c07b 100644 --- a/src/cgroup.rs +++ b/src/cgroup.rs @@ -359,9 +359,18 @@ fn create_v2_cgroup(root: PathBuf, path: &str) -> Result<()> { } pub fn get_cgroups_relative_paths() -> Result> { + let path = "/proc/self/cgroup".to_string(); + get_cgroups_relative_paths_by_path(path) +} + +pub fn get_cgroups_relative_paths_by_pid(pid: u32) -> Result> { + let path = format!("/proc/{}/cgroup", pid); + get_cgroups_relative_paths_by_path(path) +} + +fn get_cgroups_relative_paths_by_path(path: String) -> Result> { let mut m = HashMap::new(); - let content = - fs::read_to_string("/proc/self/cgroup").map_err(|e| Error::with_cause(ReadFailed, e))?; + let content = fs::read_to_string(path).map_err(|e| Error::with_cause(ReadFailed, e))?; for l in content.lines() { let fl: Vec<&str> = l.split(':').collect(); if fl.len() != 3 {