Expose deletion error

So that users can retry or do some aftercare.

Fixes: #18

Signed-off-by: Tim Zhang <tim@hyper.sh>
This commit is contained in:
Tim Zhang
2020-11-09 15:11:36 +08:00
parent 0f76570677
commit 121f78d8e8
3 changed files with 17 additions and 20 deletions

View File

@@ -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.

View File

@@ -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(),

View File

@@ -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<HashMap<String, HashMap
Ok(h)
}
/// fs::remove_dir_all or fs::remove_dir can't work with cgroup directory sometimes.
/// with error: `Os { code: 1, kind: PermissionDenied, message: "Operation not permitted" }`
pub fn libc_rmdir(p: &str) {
// with int return value
let _ = unsafe { libc::rmdir(p.as_ptr() as *const libc::c_char) };
}
/// read and parse an i64 data
pub fn read_i64_from(mut file: File) -> Result<i64> {
let mut string = String::new();