Hide most of the previously undocumented methods in a sealed trait

This commit is contained in:
Sam Wilson
2018-10-10 14:23:06 -04:00
committed by Levente Kurusa
parent 7826b798bd
commit c63dad411b
14 changed files with 106 additions and 71 deletions

View File

@@ -10,7 +10,7 @@ use error::*;
use error::ErrorKind::*;
use {
BlkIoResources, ControllIdentifier, Controller, Controllers, Resources, Subsystem,
BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem,
};
/// A controller that allows controlling the `blkio` subsystem of a Cgroup.
@@ -255,7 +255,7 @@ pub struct BlkIo {
pub weight_device: Vec<BlkIoData>,
}
impl Controller for BlkIoController {
impl ControllerInternal for BlkIoController {
fn control_type(&self) -> Controllers {
Controllers::BlkIo
}

View File

@@ -11,7 +11,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, CpuResources, Resources, Subsystem,
ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem,
};
/// A controller that allows controlling the `cpu` subsystem of a Cgroup.
@@ -34,7 +34,7 @@ pub struct Cpu {
pub stat: String,
}
impl Controller for CpuController {
impl ControllerInternal for CpuController {
fn control_type(&self) -> Controllers {
Controllers::Cpu
}

View File

@@ -9,7 +9,7 @@ use std::path::PathBuf;
use error::*;
use error::ErrorKind::*;
use {ControllIdentifier, Controller, Controllers, Resources, Subsystem};
use {ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
/// A controller that allows controlling the `cpuacct` subsystem of a Cgroup.
///
@@ -52,7 +52,7 @@ pub struct CpuAcct {
pub usage_user: u64,
}
impl Controller for CpuAcctController {
impl ControllerInternal for CpuAcctController {
fn control_type(&self) -> Controllers {
Controllers::CpuAcct
}

View File

@@ -10,7 +10,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, CpuResources, Resources, Subsystem,
ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem,
};
/// A controller that allows controlling the `cpuset` subsystem of a Cgroup.
@@ -79,7 +79,7 @@ pub struct CpuSet {
pub sched_relax_domain_level: u64,
}
impl Controller for CpuSetController {
impl ControllerInternal for CpuSetController {
fn control_type(&self) -> Controllers {
Controllers::CpuSet
}

View File

@@ -9,7 +9,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, DeviceResource, DeviceResources,
ControllIdentifier, ControllerInternal, Controllers, DeviceResource, DeviceResources,
Resources, Subsystem,
};
@@ -131,7 +131,7 @@ impl DevicePermissions {
}
}
impl Controller for DevicesController {
impl ControllerInternal for DevicesController {
fn control_type(&self) -> Controllers {
Controllers::Devices
}

View File

@@ -8,7 +8,7 @@ use std::path::PathBuf;
use error::*;
use error::ErrorKind::*;
use {ControllIdentifier, Controller, Controllers, Resources, Subsystem};
use {ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
/// A controller that allows controlling the `freezer` subsystem of a Cgroup.
///
@@ -34,7 +34,7 @@ pub enum FreezerState {
Frozen,
}
impl Controller for FreezerController {
impl ControllerInternal for FreezerController {
fn control_type(&self) -> Controllers {
Controllers::Freezer
}

View File

@@ -10,7 +10,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, HugePageResources, Resources,
ControllIdentifier, ControllerInternal, Controllers, HugePageResources, Resources,
Subsystem,
};
@@ -24,7 +24,7 @@ pub struct HugeTlbController {
path: PathBuf,
}
impl Controller for HugeTlbController {
impl ControllerInternal for HugeTlbController {
fn control_type(&self) -> Controllers {
Controllers::HugeTlb
}

View File

@@ -108,31 +108,95 @@ impl Controllers {
}
}
mod sealed {
use super::*;
pub trait ControllerInternal {
fn apply(&self, res: &Resources) -> Result<()>;
// meta stuff
fn control_type(&self) -> Controllers;
fn get_path(&self) -> &PathBuf;
fn get_path_mut(&mut self) -> &mut PathBuf;
fn get_base(&self) -> &PathBuf;
fn verify_path(&self) -> Result<()> {
if self.get_path().starts_with(self.get_base()) {
Ok(())
} else {
Err(Error::new(ErrorKind::InvalidPath))
}
}
fn open_path(&self, p: &str, w: bool) -> Result<File> {
let mut path = self.get_path().clone();
path.push(p);
self.verify_path()?;
if w {
match File::create(&path) {
Err(e) => return Err(Error::with_cause(ErrorKind::WriteFailed, e)),
Ok(file) => return Ok(file),
}
} else {
match File::open(&path) {
Err(e) => return Err(Error::with_cause(ErrorKind::ReadFailed, e)),
Ok(file) => return Ok(file),
}
}
}
#[doc(hidden)]
fn path_exists(&self, p: &str) -> bool {
if let Err(_) = self.verify_path() {
return false;
}
std::path::Path::new(p).exists()
}
}
}
pub(crate) use sealed::ControllerInternal;
/// A Controller is a subsystem attached to the control group.
///
/// Implementors are able to control certain aspects of a control group.
pub trait Controller {
#[doc(hidden)]
fn control_type(&self) -> Controllers;
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
/// kernel the information.
fn apply(&self, res: &Resources) -> Result<()>;
// meta stuff
#[doc(hidden)]
fn control_type(&self) -> Controllers;
#[doc(hidden)]
fn get_path(&self) -> &PathBuf;
#[doc(hidden)]
fn get_path_mut(&mut self) -> &mut PathBuf;
#[doc(hidden)]
fn get_base(&self) -> &PathBuf;
/// Create this controller
fn create(&self);
#[doc(hidden)]
fn verify_path(&self) -> Result<()> {
if self.get_path().starts_with(self.get_base()) {
Ok(())
} else {
Err(Error::new(ErrorKind::InvalidPath))
}
/// Does this controller already exist?
fn exists(&self) -> bool;
/// Delete the controller.
fn delete(&self);
/// Attach a task to this controller.
fn add_task(&self, pid: &CgroupPid) -> Result<()>;
/// Get the list of tasks that this controller has.
fn tasks(&self) -> Vec<CgroupPid>;
}
impl<T> Controller for T where T: ControllerInternal {
fn control_type(&self) -> Controllers {
ControllerInternal::control_type(self)
}
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
/// kernel the information.
fn apply(&self, res: &Resources) -> Result<()> {
ControllerInternal::apply(self, res)
}
/// Create this controller
@@ -157,35 +221,6 @@ pub trait Controller {
}
}
#[doc(hidden)]
fn open_path(&self, p: &str, w: bool) -> Result<File> {
let mut path = self.get_path().clone();
path.push(p);
self.verify_path()?;
if w {
match File::create(&path) {
Err(e) => return Err(Error::with_cause(ErrorKind::WriteFailed, e)),
Ok(file) => return Ok(file),
}
} else {
match File::open(&path) {
Err(e) => return Err(Error::with_cause(ErrorKind::ReadFailed, e)),
Ok(file) => return Ok(file),
}
}
}
#[doc(hidden)]
fn path_exists(&self, p: &str) -> bool {
if let Err(_) = self.verify_path() {
return false;
}
std::path::Path::new(p).exists()
}
/// Attach a task to this controller.
fn add_task(&self, pid: &CgroupPid) -> Result<()> {
self.open_path("tasks", true).and_then(|mut file| {

View File

@@ -10,7 +10,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, MemoryResources, Resources, Subsystem,
ControllIdentifier, ControllerInternal, Controllers, MemoryResources, Resources, Subsystem,
};
/// A controller that allows controlling the `memory` subsystem of a Cgroup.
@@ -393,7 +393,7 @@ pub struct Kmem {
pub slabinfo: String,
}
impl Controller for MemController {
impl ControllerInternal for MemController {
fn control_type(&self) -> Controllers {
Controllers::Mem
}

View File

@@ -10,7 +10,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, NetworkResources, Resources,
ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources,
Subsystem,
};
@@ -25,7 +25,7 @@ pub struct NetClsController {
path: PathBuf,
}
impl Controller for NetClsController {
impl ControllerInternal for NetClsController {
fn control_type(&self) -> Controllers {
Controllers::NetCls
}

View File

@@ -11,7 +11,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, NetworkResources, Resources,
ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources,
Subsystem,
};
@@ -26,7 +26,7 @@ pub struct NetPrioController {
path: PathBuf,
}
impl Controller for NetPrioController {
impl ControllerInternal for NetPrioController {
fn control_type(&self) -> Controllers {
Controllers::NetPrio
}

View File

@@ -6,7 +6,7 @@ use std::path::PathBuf;
use error::*;
use {ControllIdentifier, Controller, Controllers, Resources, Subsystem};
use {ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
/// A controller that allows controlling the `perf_event` subsystem of a Cgroup.
///
@@ -18,7 +18,7 @@ pub struct PerfEventController {
path: PathBuf,
}
impl Controller for PerfEventController {
impl ControllerInternal for PerfEventController {
fn control_type(&self) -> Controllers {
Controllers::PerfEvent
}

View File

@@ -10,7 +10,7 @@ use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, Controller, Controllers, PidResources, Resources, Subsystem,
ControllIdentifier, ControllerInternal, Controllers, PidResources, Resources, Subsystem,
};
/// A controller that allows controlling the `pids` subsystem of a Cgroup.
@@ -36,7 +36,7 @@ impl Default for PidMax {
}
}
impl Controller for PidController {
impl ControllerInternal for PidController {
fn control_type(&self) -> Controllers {
Controllers::Pids
}

View File

@@ -9,7 +9,7 @@ use std::path::PathBuf;
use error::*;
use error::ErrorKind::*;
use {ControllIdentifier, Controller, Controllers, Resources, Subsystem};
use {ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
/// A controller that allows controlling the `rdma` subsystem of a Cgroup.
///
@@ -21,7 +21,7 @@ pub struct RdmaController {
path: PathBuf,
}
impl Controller for RdmaController {
impl ControllerInternal for RdmaController {
fn control_type(&self) -> Controllers {
Controllers::Rdma
}