Skip to content

Commit

Permalink
cgroup: fix the issue of remove cgroup
Browse files Browse the repository at this point in the history
It should remove the child cgroups recursively
and then remove the parent cgroup, otherwise,
it would remove failed.

Signed-off-by: fupan.lfp <fupan.lfp@antfin.com>
  • Loading branch information
lifupan committed Jan 25, 2021
1 parent 8448548 commit ddf3a5c
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ where
return Ok(());
}

fs::remove_dir(self.get_path()).map_err(|e| Error::with_cause(ErrorKind::RemoveFailed, e))
remove_dir(self.get_path())
}

/// Attach a task to this controller.
Expand Down Expand Up @@ -379,6 +379,30 @@ where
}
}

// remove_dir aims to remove cgroup path. It does so recursively,
// by removing any subdirectories (sub-cgroups) first.
fn remove_dir(dir: &PathBuf) -> Result<()> {
// try the fast path first.
if fs::remove_dir(dir).is_ok() {
return Ok(());
}

if dir.exists() {
if dir.is_dir() {
for entry in fs::read_dir(dir).map_err(|e| Error::with_cause(ReadFailed, e))? {
let entry = entry.map_err(|e| Error::with_cause(ReadFailed, e))?;
let path = entry.path();
if path.is_dir() {
remove_dir(&path)?;
}
}
fs::remove_dir(dir).map_err(|e| Error::with_cause(RemoveFailed, e))?;
}
}

Ok(())
}

#[doc(hidden)]
pub trait ControllIdentifier {
fn controller_type() -> Controllers;
Expand Down

0 comments on commit ddf3a5c

Please sign in to comment.