From 7d4d4579a36c00e0fceaa14e413ce6d38b9e0818 Mon Sep 17 00:00:00 2001 From: jokemanfire Date: Sat, 2 Nov 2024 16:47:19 +0800 Subject: [PATCH] fix bug: if cgroup path has ":" This bug is occur in cgroup path "which has :" Signed-off-by: jokemanfire --- src/cgroup.rs | 19 ++++++++++--------- src/devices.rs | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/cgroup.rs b/src/cgroup.rs index 24f3cc1..c3640d1 100644 --- a/src/cgroup.rs +++ b/src/cgroup.rs @@ -620,15 +620,16 @@ fn get_cgroups_relative_paths_by_path(path: String) -> Result = l.split(':').collect(); - if fl.len() != 3 { - continue; - } - - let keys: Vec<&str> = fl[1].split(',').collect(); - for key in &keys { - m.insert(key.to_string(), fl[2].to_string()); + // cgroup path may have ":" , likes + // "2:cpu,cpuacct:/system.slice/containerd.service/test.slice:cri-containerd:96b37a2edf84351487f42039e137427f1812f678850675fac214caf597ee5e4a" + for line in content.lines() { + if let Some((first_value_part, remaining_path)) = + line.split_once(':').unwrap_or_default().1.split_once(':') + { + let keys: Vec<&str> = first_value_part.split(',').collect(); + keys.iter().for_each(|key| { + m.insert(key.to_string(), remaining_path.to_string()); + }); } } Ok(m) diff --git a/src/devices.rs b/src/devices.rs index 23b7f58..a842250 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -297,7 +297,7 @@ impl DevicesController { match res { Ok(_) => { s.lines().fold(Ok(Vec::new()), |acc, line| { - let ls = line.to_string().split(|c| c == ' ' || c == ':').map(|x| x.to_string()).collect::>(); + let ls = line.split(|c| c == ' ' || c == ':').map(|x| x.to_string()).collect::>(); if acc.is_err() || ls.len() != 4 { error!("allowed_devices: acc: {:?}, ls: {:?}", acc, ls); Err(Error::new(ParseError))