Skip to content

Commit ddf3a5c

Browse files
committed
cgroup: fix the issue of remove cgroup
It should remove the child cgroups recursively and then remove the parent cgroup, otherwise, it would remove failed. Signed-off-by: fupan.lfp <[email protected]>
1 parent 8448548 commit ddf3a5c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ where
330330
return Ok(());
331331
}
332332

333-
fs::remove_dir(self.get_path()).map_err(|e| Error::with_cause(ErrorKind::RemoveFailed, e))
333+
remove_dir(self.get_path())
334334
}
335335

336336
/// Attach a task to this controller.
@@ -379,6 +379,30 @@ where
379379
}
380380
}
381381

382+
// remove_dir aims to remove cgroup path. It does so recursively,
383+
// by removing any subdirectories (sub-cgroups) first.
384+
fn remove_dir(dir: &PathBuf) -> Result<()> {
385+
// try the fast path first.
386+
if fs::remove_dir(dir).is_ok() {
387+
return Ok(());
388+
}
389+
390+
if dir.exists() {
391+
if dir.is_dir() {
392+
for entry in fs::read_dir(dir).map_err(|e| Error::with_cause(ReadFailed, e))? {
393+
let entry = entry.map_err(|e| Error::with_cause(ReadFailed, e))?;
394+
let path = entry.path();
395+
if path.is_dir() {
396+
remove_dir(&path)?;
397+
}
398+
}
399+
fs::remove_dir(dir).map_err(|e| Error::with_cause(RemoveFailed, e))?;
400+
}
401+
}
402+
403+
Ok(())
404+
}
405+
382406
#[doc(hidden)]
383407
pub trait ControllIdentifier {
384408
fn controller_type() -> Controllers;

0 commit comments

Comments
 (0)