mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
tree: add some simple error reporting
There's still plenty to wish for. Signed-off-by: Levente Kurusa <lkurusa@acm.org>
This commit is contained in:
+44
-23
@@ -4,8 +4,10 @@
|
||||
//! [Documentation/cgroups-v1/pids.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt)
|
||||
use std::path::PathBuf;
|
||||
use std::io::{Write, Read};
|
||||
use std::fs::File;
|
||||
|
||||
use {Resources, PidResources, Controller, ControllIdentifier, Subsystem, Controllers};
|
||||
use {CgroupError, Resources, PidResources, Controller, ControllIdentifier, Subsystem, Controllers};
|
||||
use CgroupError::*;
|
||||
|
||||
/// A controller that allows controlling the `pids` subsystem of a Cgroup.
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -42,7 +44,7 @@ impl Controller for PidController {
|
||||
|
||||
if pidres.update_values {
|
||||
/* apply pid_max */
|
||||
self.set_pid_max(pidres.maximum_number_of_processes);
|
||||
let _ = self.set_pid_max(pidres.maximum_number_of_processes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,6 +75,14 @@ impl<'a> From<&'a Subsystem> for &'a PidController {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_u64_from(mut file: File) -> Result<u64, CgroupError> {
|
||||
let mut string = String::new();
|
||||
match file.read_to_string(&mut string) {
|
||||
Ok(_) => string.trim().parse().map_err(|_| ParseError),
|
||||
Err(e) => Err(CgroupError::ReadError(e)),
|
||||
}
|
||||
}
|
||||
|
||||
impl PidController {
|
||||
/// Constructors a new `PidController` instance, with `oroot` serving as the controller's root
|
||||
/// directory.
|
||||
@@ -86,32 +96,44 @@ impl PidController {
|
||||
}
|
||||
|
||||
/// The number of times `fork` failed because the limit was hit.
|
||||
pub fn get_pid_events(self: &Self) -> i64 {
|
||||
pub fn get_pid_events(self: &Self) -> Result<u64, CgroupError> {
|
||||
self.open_path("pids.events", false).and_then(|mut file| {
|
||||
let mut string = String::new();
|
||||
let _ = file.read_to_string(&mut string);
|
||||
Some(string.split_whitespace().nth(1).unwrap().parse().unwrap_or(0))
|
||||
}).unwrap()
|
||||
match file.read_to_string(&mut string) {
|
||||
Ok(_) => {
|
||||
match string.split_whitespace().nth(1) {
|
||||
Some(elem) => match elem.parse() {
|
||||
Ok(val) => Ok(val),
|
||||
Err(_) => Err(CgroupError::ParseError),
|
||||
},
|
||||
None => Err(CgroupError::ParseError),
|
||||
}
|
||||
},
|
||||
Err(e) => Err(CgroupError::ReadError(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// The number of processes currently.
|
||||
pub fn get_pid_current(self: &Self) -> i64 {
|
||||
self.open_path("pids.current", false).and_then(|mut file| {
|
||||
let mut string = String::new();
|
||||
let _ = file.read_to_string(&mut string);
|
||||
Some(string.trim().parse().unwrap_or(0))
|
||||
}).unwrap()
|
||||
pub fn get_pid_current(self: &Self) -> Result<u64, CgroupError> {
|
||||
self.open_path("pids.current", false).and_then(read_u64_from)
|
||||
}
|
||||
|
||||
/// The maximum number of processes that can exist at one time in the control group.
|
||||
pub fn get_pid_max(self: &Self) -> Option<PidMax> {
|
||||
pub fn get_pid_max(self: &Self) -> Result<PidMax, CgroupError> {
|
||||
self.open_path("pids.max", false).and_then(|mut file| {
|
||||
let mut string = String::new();
|
||||
let _ = file.read_to_string(&mut string);
|
||||
if string.trim() == "max" {
|
||||
Some(PidMax::Max)
|
||||
} else {
|
||||
Some(PidMax::Value(string.trim().parse().unwrap_or(0)))
|
||||
let res = file.read_to_string(&mut string);
|
||||
match res {
|
||||
Ok(_) => if string.trim() == "max" {
|
||||
Ok(PidMax::Max)
|
||||
} else {
|
||||
match string.trim().parse() {
|
||||
Ok(val) => Ok(PidMax::Value(val)),
|
||||
Err(_) => Err(CgroupError::ParseError),
|
||||
}
|
||||
},
|
||||
Err(e) => Err(CgroupError::ReadError(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -121,17 +143,16 @@ impl PidController {
|
||||
/// Note that if `get_pid_current()` returns a higher number than what you
|
||||
/// are about to set (`max_pid`), then no processess will be killed. Additonally, attaching
|
||||
/// extra processes to a control group disregards the limit.
|
||||
pub fn set_pid_max(self: &Self, max_pid: PidMax) {
|
||||
pub fn set_pid_max(self: &Self, max_pid: PidMax) -> Result<(), CgroupError> {
|
||||
self.open_path("pids.max", true).and_then(|mut file| {
|
||||
let string_to_write = match max_pid {
|
||||
PidMax::Max => "max".to_string(),
|
||||
PidMax::Value(num) => num.to_string(),
|
||||
};
|
||||
match file.write_all(string_to_write.as_ref()) {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("error {:?}", e),
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(CgroupError::WriteError(e)),
|
||||
}
|
||||
Some(0i64)
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user