Skip to content

Commit a3ea181

Browse files
committed
cgroups: Add v2 support for load_with_relative_paths
1 parent 5d74a1d commit a3ea181

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/fs/cgroup.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl Cgroup {
127127
path: P,
128128
relative_paths: HashMap<String, String>,
129129
) -> Result<Cgroup> {
130-
let cg = Cgroup::load_with_relative_paths(hier, path, relative_paths);
130+
let cg = Cgroup::load_with_relative_paths(hier, path, &relative_paths);
131131
cg.create()?;
132132
Ok(cg)
133133
}
@@ -185,38 +185,45 @@ impl Cgroup {
185185
///
186186
/// Returns a handle to the control group (that possibly does not exist until `create()` has
187187
/// been called on the cgroup.
188-
///
189-
/// Note that this method is only meaningful for cgroup v1, call it is equivalent to call `load` in the v2 mode
190188
pub fn load_with_relative_paths<P: AsRef<Path>>(
191189
hier: Box<dyn Hierarchy>,
192190
path: P,
193-
relative_paths: HashMap<String, String>,
191+
relative_paths: &HashMap<String, String>,
194192
) -> Cgroup {
195-
// relative_paths only valid for cgroup v1
196-
if hier.v2() {
197-
return Self::load(hier, path);
198-
}
199-
200193
let path = path.as_ref();
201194
let mut subsystems = hier.subsystems();
202-
if path.as_os_str() != "" {
203-
subsystems = subsystems
204-
.into_iter()
205-
.map(|x| {
206-
let cn = x.controller_name();
207-
if relative_paths.contains_key(&cn) {
208-
let rp = relative_paths.get(&cn).unwrap();
209-
let valid_path = rp.trim_start_matches('/').to_string();
210-
let mut p = PathBuf::from(valid_path);
195+
subsystems = subsystems
196+
.into_iter()
197+
.map(|x| {
198+
let cn = if hier.v2() {
199+
"".to_string()
200+
} else {
201+
x.controller_name()
202+
};
203+
if relative_paths.contains_key(&cn) {
204+
let rp = relative_paths.get(&cn).unwrap();
205+
let valid_path = rp.trim_start_matches('/').to_string();
206+
let mut p = PathBuf::from(valid_path);
207+
if path.as_os_str() != "" {
211208
p.push(path);
212-
x.enter(p.as_ref())
213-
} else {
209+
}
210+
x.enter(p.as_ref())
211+
} else {
212+
if path.as_os_str() != "" {
214213
x.enter(path)
214+
} else {
215+
x
215216
}
216-
})
217-
.collect::<Vec<_>>();
218-
}
217+
}
218+
})
219+
.collect::<Vec<_>>();
220+
221+
let rp = relative_paths.get("").unwrap();
222+
let valid_path = rp.trim_start_matches('/').to_string();
223+
let mut pb = PathBuf::from(valid_path);
224+
pb.push(path);
219225

226+
let path = if hier.v2() { pb.as_path() } else { path };
220227
Cgroup {
221228
subsystems,
222229
hier,

0 commit comments

Comments
 (0)