|
| 1 | +// Copyright (c) 2025 Ant Group |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 or MIT |
| 4 | +// |
| 5 | + |
| 6 | +use libc::SIGKILL; |
| 7 | +use zbus::blocking::Connection; |
| 8 | +use zbus::zvariant::Value; |
| 9 | +use zbus::Error as ZbusError; |
| 10 | + |
| 11 | +use crate::fs::hierarchies; |
| 12 | +use crate::systemd::dbus::{ |
| 13 | + SystemManager, BLOCK_IO_ACCOUNTING, CPU_ACCOUNTING, DEFAULT_DEPENDENCIES, DELEGATE, |
| 14 | + DESCRIPTION, IO_ACCOUNTING, MEMORY_ACCOUNTING, NO_SUCH_UNIT, PIDS, SLICE, TASKS_ACCOUNTING, |
| 15 | + UNIT_MODE_REPLACE, WANTS, WHO_ENUM_ALL, |
| 16 | +}; |
| 17 | +use crate::systemd::error::{Error, Result}; |
| 18 | +use crate::systemd::{utils, Property}; |
| 19 | +use crate::CgroupPid; |
| 20 | + |
| 21 | +pub struct SystemdCgroup { |
| 22 | + /// The name of slice |
| 23 | + slice: String, |
| 24 | + /// The name of the systemd unit (slice or scope) |
| 25 | + unit: String, |
| 26 | + /// Whether the systemd unit is using cgroup v2 |
| 27 | + v2: bool, |
| 28 | +} |
| 29 | + |
| 30 | +impl SystemdCgroup { |
| 31 | + pub fn new(slice: &str, unit: &str) -> Result<Self> { |
| 32 | + Ok(Self { |
| 33 | + slice: slice.to_string(), |
| 34 | + unit: unit.to_string(), |
| 35 | + v2: hierarchies::is_cgroup2_unified_mode(), |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl SystemdCgroup { |
| 41 | + fn system_manager(&self) -> Result<SystemManager> { |
| 42 | + let system_bus = Connection::system()?; |
| 43 | + let proxy = SystemManager::new(&system_bus)?; |
| 44 | + |
| 45 | + Ok(proxy) |
| 46 | + } |
| 47 | + |
| 48 | + /// Start a slice or a scope unit controlled and supervised by systemd. |
| 49 | + /// |
| 50 | + /// For more information, see: |
| 51 | + /// https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html |
| 52 | + /// https://www.freedesktop.org/software/systemd/man/latest/systemd.slice.html |
| 53 | + /// https://www.freedesktop.org/software/systemd/man/latest/systemd.scope.html |
| 54 | + pub fn start(&self, pid: CgroupPid) -> Result<()> { |
| 55 | + let mut properties: Vec<Property> = vec![ |
| 56 | + (CPU_ACCOUNTING, Value::Bool(true)), |
| 57 | + (DEFAULT_DEPENDENCIES, Value::Bool(false)), |
| 58 | + // MemoryAccount is for cgroupsv2 as documented in dbus. |
| 59 | + // However, "github.com/opencontainer/runc" uses it for all. |
| 60 | + // Shall we follow the same way? |
| 61 | + (MEMORY_ACCOUNTING, Value::Bool(true)), |
| 62 | + (TASKS_ACCOUNTING, Value::Bool(true)), |
| 63 | + (DESCRIPTION, Value::Str("kata-containers unit".into())), |
| 64 | + (PIDS, Value::Array(vec![pid.pid as u32].into())), |
| 65 | + ]; |
| 66 | + |
| 67 | + if self.v2() { |
| 68 | + properties.push((IO_ACCOUNTING, Value::Bool(true))); |
| 69 | + } else { |
| 70 | + properties.push((BLOCK_IO_ACCOUNTING, Value::Bool(true))); |
| 71 | + } |
| 72 | + |
| 73 | + if utils::is_slice_unit(&self.unit) { |
| 74 | + // If we create a slice, the parent is defined via a Wants=. |
| 75 | + properties.push((WANTS, Value::Str(self.slice.as_str().into()))); |
| 76 | + } else { |
| 77 | + // Otherwise it's a scope, which we put into a Slice=. |
| 78 | + properties.push((SLICE, Value::Str(self.slice.as_str().into()))); |
| 79 | + // Assume scopes always support delegation (supported since systemd v218). |
| 80 | + properties.push((DELEGATE, Value::Bool(true))); |
| 81 | + } |
| 82 | + |
| 83 | + let sysmgr = self.system_manager()?; |
| 84 | + |
| 85 | + sysmgr.start_transient_unit(&self.unit, UNIT_MODE_REPLACE, &properties, &[])?; |
| 86 | + |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | + |
| 90 | + /// Set properties for the unit through dbus `SetUnitProperties`. |
| 91 | + pub fn set_properties(&self, properties: &Vec<Property>) -> Result<()> { |
| 92 | + let sysmgr = self.system_manager()?; |
| 93 | + |
| 94 | + sysmgr.set_unit_properties(&self.unit, true, properties)?; |
| 95 | + |
| 96 | + Ok(()) |
| 97 | + } |
| 98 | + |
| 99 | + /// Kill the unit through dbus `KillUnit` with `SIGKILL` signal. |
| 100 | + pub fn kill(&self) -> Result<()> { |
| 101 | + let sysmgr = self.system_manager()?; |
| 102 | + |
| 103 | + let ret = sysmgr.kill_unit(&self.unit, WHO_ENUM_ALL, SIGKILL); |
| 104 | + |
| 105 | + // Ignore no such unit error |
| 106 | + if let Err(ZbusError::MethodError(err_name, _, _)) = &ret { |
| 107 | + if err_name.as_str() == NO_SUCH_UNIT { |
| 108 | + return Ok(()); |
| 109 | + } |
| 110 | + } |
| 111 | + ret?; |
| 112 | + |
| 113 | + Ok(()) |
| 114 | + } |
| 115 | + |
| 116 | + /// Freeze the unit through dbus `FreezeUnit`. |
| 117 | + pub fn freeze(&self) -> Result<()> { |
| 118 | + let sysmgr = self.system_manager()?; |
| 119 | + |
| 120 | + sysmgr.freeze_unit(&self.unit)?; |
| 121 | + |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | + |
| 125 | + /// Thaw the frozen unit through dbus `ThawUnit`. |
| 126 | + pub fn thaw(&self) -> Result<()> { |
| 127 | + let sysmgr = self.system_manager()?; |
| 128 | + |
| 129 | + sysmgr.thaw_unit(&self.unit)?; |
| 130 | + |
| 131 | + Ok(()) |
| 132 | + } |
| 133 | + |
| 134 | + /// Get the systemd version. |
| 135 | + pub fn systemd_version(&self) -> Result<usize> { |
| 136 | + let sysmgr = self.system_manager()?; |
| 137 | + |
| 138 | + let version = sysmgr.version()?; |
| 139 | + let version = version |
| 140 | + .parse::<usize>() |
| 141 | + .map_err(|_| Error::CorruptedSystemdVersion(version.clone()))?; |
| 142 | + |
| 143 | + Ok(version) |
| 144 | + } |
| 145 | + |
| 146 | + /// Check if the unit exists. |
| 147 | + pub fn exists(&self) -> Result<bool> { |
| 148 | + let sysmgr = self.system_manager()?; |
| 149 | + |
| 150 | + let ret = sysmgr.get_unit(&self.unit); |
| 151 | + |
| 152 | + if let Err(ZbusError::MethodError(err_name, _, _)) = &ret { |
| 153 | + if err_name.as_str() == NO_SUCH_UNIT { |
| 154 | + return Ok(false); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + ret?; |
| 159 | + |
| 160 | + Ok(true) |
| 161 | + } |
| 162 | + |
| 163 | + /// Add a process (tgid) to the unit through dbus |
| 164 | + /// `AttachProcessesToUnit`. |
| 165 | + pub fn add_process(&self, pid: CgroupPid, subcgroup: &str) -> Result<()> { |
| 166 | + let sysmgr = self.system_manager()?; |
| 167 | + |
| 168 | + sysmgr.attach_processes_to_unit(&self.unit, subcgroup, &[pid.pid as u32])?; |
| 169 | + |
| 170 | + Ok(()) |
| 171 | + } |
| 172 | + |
| 173 | + pub fn v2(&self) -> bool { |
| 174 | + self.v2 |
| 175 | + } |
| 176 | +} |
0 commit comments