From d018a24814a0c116f6417ee25f842904e17b4b58 Mon Sep 17 00:00:00 2001 From: Furisto <24721048+Furisto@users.noreply.github.com> Date: Mon, 25 Oct 2021 20:16:56 +0200 Subject: [PATCH] Move dbus code to libcgroups create --- crates/libcgroups/src/common.rs | 2 +- crates/libcgroups/src/lib.rs | 1 + .../libcgroups/src/systemd/controller_type.rs | 39 ++++++++++++ .../src/systemd}/dbus/client.rs | 8 +-- crates/libcgroups/src/systemd/dbus/mod.rs | 2 + .../src/systemd/dbus/systemd_api.rs} | 0 .../systemd_manager.rs => systemd/manager.rs} | 61 ++++++------------- crates/libcgroups/src/systemd/mod.rs | 3 + crates/libcgroups/src/v2/mod.rs | 6 +- crates/libcontainer/src/dbus/mod.rs | 2 - crates/libcontainer/src/lib.rs | 1 - 11 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 crates/libcgroups/src/systemd/controller_type.rs rename crates/{libcontainer/src => libcgroups/src/systemd}/dbus/client.rs (94%) create mode 100644 crates/libcgroups/src/systemd/dbus/mod.rs rename crates/{libcontainer/src/dbus/systemd_manager.rs => libcgroups/src/systemd/dbus/systemd_api.rs} (100%) rename crates/libcgroups/src/{v2/systemd_manager.rs => systemd/manager.rs} (82%) create mode 100644 crates/libcgroups/src/systemd/mod.rs delete mode 100644 crates/libcontainer/src/dbus/mod.rs diff --git a/crates/libcgroups/src/common.rs b/crates/libcgroups/src/common.rs index e87ca90e..60725d60 100644 --- a/crates/libcgroups/src/common.rs +++ b/crates/libcgroups/src/common.rs @@ -189,7 +189,7 @@ pub fn create_cgroup_manager>( bail!("systemd cgroup flag passed, but systemd support for managing cgroups is not available"); } log::info!("systemd cgroup manager will be used"); - return Ok(Box::new(v2::SystemDCGroupManager::new( + return Ok(Box::new(crate::systemd::manager::Manager::new( DEFAULT_CGROUP_ROOT.into(), cgroup_path.into(), )?)); diff --git a/crates/libcgroups/src/lib.rs b/crates/libcgroups/src/lib.rs index fe0778ca..cbd70126 100644 --- a/crates/libcgroups/src/lib.rs +++ b/crates/libcgroups/src/lib.rs @@ -8,6 +8,7 @@ mod test; pub mod common; pub mod stats; +pub mod systemd; pub mod test_manager; pub mod v1; pub mod v2; diff --git a/crates/libcgroups/src/systemd/controller_type.rs b/crates/libcgroups/src/systemd/controller_type.rs new file mode 100644 index 00000000..b9063a40 --- /dev/null +++ b/crates/libcgroups/src/systemd/controller_type.rs @@ -0,0 +1,39 @@ +use std::fmt::Display; + +pub enum ControllerType { + Cpu, + Io, + Memory, + Tasks, +} + +impl Display for ControllerType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let print = match self { + ControllerType::Cpu => "cpu", + ControllerType::Io => "io", + ControllerType::Memory => "memory", + ControllerType::Tasks => "tasks", + }; + + write!(f, "{}", print) + } +} + +impl AsRef for ControllerType { + fn as_ref(&self) -> &str { + match self { + ControllerType::Cpu => "cpu", + ControllerType::Io => "io", + ControllerType::Memory => "memory", + ControllerType::Tasks => "tasks", + } + } +} + +pub const CONTROLLER_TYPES: &[ControllerType] = &[ + ControllerType::Cpu, + ControllerType::Io, + ControllerType::Memory, + ControllerType::Tasks, +]; diff --git a/crates/libcontainer/src/dbus/client.rs b/crates/libcgroups/src/systemd/dbus/client.rs similarity index 94% rename from crates/libcontainer/src/dbus/client.rs rename to crates/libcgroups/src/systemd/dbus/client.rs index 4dc17d6e..bbcf65e9 100644 --- a/crates/libcontainer/src/dbus/client.rs +++ b/crates/libcgroups/src/systemd/dbus/client.rs @@ -1,6 +1,6 @@ -use crate::dbus::systemd_manager::OrgFreedesktopSystemd1Manager; +use crate::systemd::dbus::systemd_api::OrgFreedesktopSystemd1Manager; use anyhow::Result; -use dbus::arg::{RefArg, Variant, PropMap}; +use dbus::arg::{PropMap, Variant}; use dbus::blocking::Connection; use std::time::Duration; @@ -25,7 +25,7 @@ impl Client { unit_name: &str, mut properties_map: PropMap, ) -> Result<()> { - // To view and introspect the methods under the 'org.freedesktop.systemd1' destination + // To view and introspect the methods under the 'org.freedesktop.systemd1' destination // and object path under it use the following command: // `gdbus introspect --system --dest org.freedesktop.systemd1 --object-path /org/freedesktop/systemd1` let proxy = self.conn.with_proxy( @@ -62,7 +62,7 @@ impl Client { let mut properties = vec![]; for (name, variant) in &mut properties_map.iter() { - let s : &str = &*name; + let s: &str = &*name; properties.push((s, variant.to_owned())); } //proxy.start_transient_unit(unit_name, "replace", properties, vec![])?; diff --git a/crates/libcgroups/src/systemd/dbus/mod.rs b/crates/libcgroups/src/systemd/dbus/mod.rs new file mode 100644 index 00000000..e8bf4c52 --- /dev/null +++ b/crates/libcgroups/src/systemd/dbus/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod client; +pub(crate) mod systemd_api; diff --git a/crates/libcontainer/src/dbus/systemd_manager.rs b/crates/libcgroups/src/systemd/dbus/systemd_api.rs similarity index 100% rename from crates/libcontainer/src/dbus/systemd_manager.rs rename to crates/libcgroups/src/systemd/dbus/systemd_api.rs diff --git a/crates/libcgroups/src/v2/systemd_manager.rs b/crates/libcgroups/src/systemd/manager.rs similarity index 82% rename from crates/libcgroups/src/v2/systemd_manager.rs rename to crates/libcgroups/src/systemd/manager.rs index 740a21a8..f12d5fa4 100644 --- a/crates/libcgroups/src/v2/systemd_manager.rs +++ b/crates/libcgroups/src/systemd/manager.rs @@ -8,12 +8,9 @@ use anyhow::{anyhow, bail, Result}; use nix::unistd::Pid; use std::path::{Path, PathBuf}; +use super::controller_type::{ControllerType, CONTROLLER_TYPES}; #[cfg(feature = "cgroupsv2_devices")] use super::devices::Devices; -use super::{ - controller::Controller, controller_type::ControllerType, cpu::Cpu, cpuset::CpuSet, - freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, pids::Pids, -}; use crate::common::{self, CgroupManager, ControllerOpt, FreezerState, PathBufExt}; use crate::stats::Stats; @@ -21,16 +18,8 @@ const CGROUP_PROCS: &str = "cgroup.procs"; const CGROUP_CONTROLLERS: &str = "cgroup.controllers"; const CGROUP_SUBTREE_CONTROL: &str = "cgroup.subtree_control"; -// v2 systemd only supports cpu, io, memory and pids. -const CONTROLLER_TYPES: &[ControllerType] = &[ - ControllerType::Cpu, - ControllerType::Io, - ControllerType::Memory, - ControllerType::Pids, -]; - /// SystemDCGroupManager is a driver for managing cgroups via systemd. -pub struct SystemDCGroupManager { +pub struct Manager { root_path: PathBuf, cgroups_path: PathBuf, full_path: PathBuf, @@ -46,14 +35,14 @@ struct CgroupsPath { name: String, } -impl SystemDCGroupManager { +impl Manager { pub fn new(root_path: PathBuf, cgroups_path: PathBuf) -> Result { // TODO: create the systemd unit using a dbus client. let destructured_path = Self::destructure_cgroups_path(cgroups_path)?; let cgroups_path = Self::construct_cgroups_path(destructured_path)?; let full_path = root_path.join_safely(&cgroups_path)?; - Ok(SystemDCGroupManager { + Ok(Manager { root_path, cgroups_path, full_path, @@ -203,7 +192,7 @@ impl SystemDCGroupManager { "cpu" => controllers.push(ControllerType::Cpu), "io" => controllers.push(ControllerType::Io), "memory" => controllers.push(ControllerType::Memory), - "pids" => controllers.push(ControllerType::Pids), + "pids" => controllers.push(ControllerType::Tasks), _ => continue, } } @@ -220,7 +209,7 @@ impl SystemDCGroupManager { } } -impl CgroupManager for SystemDCGroupManager { +impl CgroupManager for Manager { fn add_task(&self, pid: Pid) -> Result<()> { // Dont attach any pid to the cgroup if -1 is specified as a pid if pid.as_raw() == -1 { @@ -234,12 +223,7 @@ impl CgroupManager for SystemDCGroupManager { fn apply(&self, controller_opt: &ControllerOpt) -> Result<()> { for controller in CONTROLLER_TYPES { match controller { - ControllerType::Cpu => Cpu::apply(controller_opt, &self.full_path)?, - ControllerType::CpuSet => CpuSet::apply(controller_opt, &self.full_path)?, - ControllerType::HugeTlb => HugeTlb::apply(controller_opt, &self.full_path)?, - ControllerType::Io => Io::apply(controller_opt, &self.full_path)?, - ControllerType::Memory => Memory::apply(controller_opt, &self.full_path)?, - ControllerType::Pids => Pids::apply(controller_opt, &self.full_path)?, + _ => {} } } @@ -253,13 +237,7 @@ impl CgroupManager for SystemDCGroupManager { } fn freeze(&self, state: FreezerState) -> Result<()> { - let controller_opt = ControllerOpt { - resources: &Default::default(), - freezer_state: Some(state), - oom_score_adj: None, - disable_oom_killer: false, - }; - Freezer::apply(&controller_opt, &self.full_path) + todo!(); } fn stats(&self) -> Result { @@ -278,7 +256,7 @@ mod tests { #[test] fn expand_slice_works() -> Result<()> { assert_eq!( - SystemDCGroupManager::expand_slice("test-a-b.slice")?, + Manager::expand_slice("test-a-b.slice")?, PathBuf::from("/test.slice/test-a.slice/test-a-b.slice"), ); @@ -287,13 +265,12 @@ mod tests { #[test] fn get_cgroups_path_works_with_a_complex_slice() -> Result<()> { - let cgroups_path = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from( - "test-a-b.slice:docker:foo", - )) - .expect(""); + let cgroups_path = + Manager::destructure_cgroups_path(PathBuf::from("test-a-b.slice:docker:foo")) + .expect(""); assert_eq!( - SystemDCGroupManager::construct_cgroups_path(cgroups_path)?, + Manager::construct_cgroups_path(cgroups_path)?, PathBuf::from("/test.slice/test-a.slice/test-a-b.slice/docker-foo.scope"), ); @@ -302,13 +279,11 @@ mod tests { #[test] fn get_cgroups_path_works_with_a_simple_slice() -> Result<()> { - let cgroups_path = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from( - "machine.slice:libpod:foo", - )) - .expect(""); + let cgroups_path = + Manager::destructure_cgroups_path(PathBuf::from("machine.slice:libpod:foo")).expect(""); assert_eq!( - SystemDCGroupManager::construct_cgroups_path(cgroups_path)?, + Manager::construct_cgroups_path(cgroups_path)?, PathBuf::from("/machine.slice/libpod-foo.scope"), ); @@ -318,10 +293,10 @@ mod tests { #[test] fn get_cgroups_path_works_with_scope() -> Result<()> { let cgroups_path = - SystemDCGroupManager::destructure_cgroups_path(PathBuf::from(":docker:foo")).expect(""); + Manager::destructure_cgroups_path(PathBuf::from(":docker:foo")).expect(""); assert_eq!( - SystemDCGroupManager::construct_cgroups_path(cgroups_path)?, + Manager::construct_cgroups_path(cgroups_path)?, PathBuf::from("/machine.slice/docker-foo.scope"), ); diff --git a/crates/libcgroups/src/systemd/mod.rs b/crates/libcgroups/src/systemd/mod.rs new file mode 100644 index 00000000..5b9cb884 --- /dev/null +++ b/crates/libcgroups/src/systemd/mod.rs @@ -0,0 +1,3 @@ +pub mod controller_type; +mod dbus; +pub mod manager; diff --git a/crates/libcgroups/src/v2/mod.rs b/crates/libcgroups/src/v2/mod.rs index c9b1d4a5..abda80a7 100644 --- a/crates/libcgroups/src/v2/mod.rs +++ b/crates/libcgroups/src/v2/mod.rs @@ -2,15 +2,13 @@ mod controller; pub mod controller_type; mod cpu; mod cpuset; +#[cfg(feature = "cgroupsv2_devices")] +pub mod devices; mod freezer; mod hugetlb; mod io; pub mod manager; mod memory; mod pids; -pub mod systemd_manager; mod unified; pub mod util; -pub use systemd_manager::SystemDCGroupManager; -#[cfg(feature = "cgroupsv2_devices")] -pub mod devices; diff --git a/crates/libcontainer/src/dbus/mod.rs b/crates/libcontainer/src/dbus/mod.rs deleted file mode 100644 index 7566395f..00000000 --- a/crates/libcontainer/src/dbus/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod client; -mod systemd_manager; diff --git a/crates/libcontainer/src/lib.rs b/crates/libcontainer/src/lib.rs index 6578b39c..80763609 100644 --- a/crates/libcontainer/src/lib.rs +++ b/crates/libcontainer/src/lib.rs @@ -1,7 +1,6 @@ pub mod apparmor; pub mod capabilities; pub mod container; -pub mod dbus; pub mod hooks; pub mod namespaces; pub mod notify_socket;