diff --git a/src/cgroup.rs b/src/cgroup.rs index 8e6dd97..4579204 100644 --- a/src/cgroup.rs +++ b/src/cgroup.rs @@ -9,8 +9,6 @@ use crate::error::ErrorKind::*; use crate::error::*; -use crate::libc_rmdir; - use crate::{CgroupPid, ControllIdentifier, Controller, Hierarchy, Resources, Subsystem}; use std::collections::HashMap; @@ -148,17 +146,17 @@ impl<'b> Cgroup<'b> { /// system call will fail if there are any descendants. Thus, one should check whether it was /// actually removed, and remove the descendants first if not. In the future, this behavior /// will change. - pub fn delete(self) { + pub fn delete(&self) -> Result<()> { if self.v2() { if self.path != "" { let mut p = self.hier.root().clone(); - p.push(self.path); - libc_rmdir(p.to_str().unwrap()); + p.push(self.path.clone()); + return fs::remove_dir(p).map_err(|e| Error::with_cause(RemoveFailed, e)); } - return; + return Ok(()); } - self.subsystems.into_iter().for_each(|sub| match sub { + self.subsystems.iter().try_for_each(|sub| match sub { Subsystem::Pid(pidc) => pidc.delete(), Subsystem::Mem(c) => c.delete(), Subsystem::CpuSet(c) => c.delete(), @@ -173,7 +171,7 @@ impl<'b> Cgroup<'b> { Subsystem::HugeTlb(c) => c.delete(), Subsystem::Rdma(c) => c.delete(), Subsystem::Systemd(c) => c.delete(), - }); + }) } /// Apply a set of resource limits to the control group. diff --git a/src/error.rs b/src/error.rs index 1a273ab..a8cd65c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -19,6 +19,9 @@ pub enum ErrorKind { /// An error occured while trying to read from a control group file. ReadFailed, + /// An error occured while trying to remove a control group. + RemoveFailed, + /// An error occured while trying to parse a value from a control group file. /// /// In the future, there will be some information attached to this field. @@ -55,6 +58,7 @@ impl fmt::Display for Error { ErrorKind::Common(s) => s.clone(), ErrorKind::WriteFailed => "unable to write to a control group file".to_string(), ErrorKind::ReadFailed => "unable to read a control group file".to_string(), + ErrorKind::RemoveFailed => "unable to remove a control group".to_string(), ErrorKind::ParseError => "unable to parse control group file".to_string(), ErrorKind::InvalidOperation => "the requested operation is invalid".to_string(), ErrorKind::InvalidPath => "the given path is invalid".to_string(), diff --git a/src/lib.rs b/src/lib.rs index 9fe2bef..117e88e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ use log::*; use std::collections::HashMap; -use std::fs::File; +use std::fs::{self, File}; use std::io::{BufRead, BufReader, Read, Write}; use std::path::{Path, PathBuf}; @@ -246,7 +246,7 @@ pub trait Controller { fn exists(&self) -> bool; /// Delete the controller. - fn delete(&self); + fn delete(&self) -> Result<()>; /// Attach a task to this controller. fn add_task(&self, pid: &CgroupPid) -> Result<()>; @@ -295,10 +295,12 @@ where } /// Delete the controller. - fn delete(&self) { - if self.get_path().exists() { - libc_rmdir(self.get_path().to_str().unwrap()); + fn delete(&self) -> Result<()> { + if !self.get_path().exists() { + return Ok(()); } + + fs::remove_dir(self.get_path()).map_err(|e| Error::with_cause(ErrorKind::RemoveFailed, e)) } /// Attach a task to this controller. @@ -807,13 +809,6 @@ pub fn nested_keyed_to_hashmap(mut file: File) -> Result Result { let mut string = String::new();