From c63dad411b2afbf47c5a99c671a8b9a974b63fda Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Wed, 10 Oct 2018 14:23:06 -0400 Subject: [PATCH] Hide most of the previously undocumented methods in a sealed trait --- src/blkio.rs | 4 +- src/cpu.rs | 4 +- src/cpuacct.rs | 4 +- src/cpuset.rs | 4 +- src/devices.rs | 4 +- src/freezer.rs | 4 +- src/hugetlb.rs | 4 +- src/lib.rs | 125 +++++++++++++++++++++++++++++----------------- src/memory.rs | 4 +- src/net_cls.rs | 4 +- src/net_prio.rs | 4 +- src/perf_event.rs | 4 +- src/pid.rs | 4 +- src/rdma.rs | 4 +- 14 files changed, 106 insertions(+), 71 deletions(-) diff --git a/src/blkio.rs b/src/blkio.rs index 1d415a5..6864df3 100644 --- a/src/blkio.rs +++ b/src/blkio.rs @@ -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, } -impl Controller for BlkIoController { +impl ControllerInternal for BlkIoController { fn control_type(&self) -> Controllers { Controllers::BlkIo } diff --git a/src/cpu.rs b/src/cpu.rs index 7d5e675..de123fb 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -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 } diff --git a/src/cpuacct.rs b/src/cpuacct.rs index 0dffce5..687ad13 100644 --- a/src/cpuacct.rs +++ b/src/cpuacct.rs @@ -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 } diff --git a/src/cpuset.rs b/src/cpuset.rs index 2ef4ba2..55a50a9 100644 --- a/src/cpuset.rs +++ b/src/cpuset.rs @@ -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 } diff --git a/src/devices.rs b/src/devices.rs index 4781e53..355ea63 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -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 } diff --git a/src/freezer.rs b/src/freezer.rs index a59e187..a4d8275 100644 --- a/src/freezer.rs +++ b/src/freezer.rs @@ -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 } diff --git a/src/hugetlb.rs b/src/hugetlb.rs index 9965d42..87272ac 100644 --- a/src/hugetlb.rs +++ b/src/hugetlb.rs @@ -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 } diff --git a/src/lib.rs b/src/lib.rs index 352dc64..6d6ea68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { + 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; +} + +impl 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 { - 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| { diff --git a/src/memory.rs b/src/memory.rs index 7f32b84..98365c3 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -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 } diff --git a/src/net_cls.rs b/src/net_cls.rs index 52e24d6..c3e7e9e 100644 --- a/src/net_cls.rs +++ b/src/net_cls.rs @@ -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 } diff --git a/src/net_prio.rs b/src/net_prio.rs index cf21351..4bd2672 100644 --- a/src/net_prio.rs +++ b/src/net_prio.rs @@ -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 } diff --git a/src/perf_event.rs b/src/perf_event.rs index 33fb41e..9f930c5 100644 --- a/src/perf_event.rs +++ b/src/perf_event.rs @@ -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 } diff --git a/src/pid.rs b/src/pid.rs index 3de1c2a..3cff49b 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -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 } diff --git a/src/rdma.rs b/src/rdma.rs index 165da6b..5c8879e 100644 --- a/src/rdma.rs +++ b/src/rdma.rs @@ -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 }