Files
cgroups-rs/src/lib.rs
Xuewei Niu c49bee218f systemd: Do not check systemd version
The systemd version is purely informational and should not be parsed, as
documented in the [1]. In practice, systemd version has different formats
on OpenShift and Ubuntu.

This commit skips the version check and allows all operations. The errors
will be thrown from dbus when performing unsupported operations on obsolete
versions of systemd.

1: https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.systemd1.html

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
2025-08-07 15:05:53 +08:00

83 lines
2.3 KiB
Rust

// Copyright (c) 2018 Levente Kurusa
// Copyright (c) 2020-2025 Ant Group
//
// SPDX-License-Identifier: Apache-2.0 or MIT
//
pub mod fs;
#[cfg(feature = "oci")]
pub mod manager;
#[cfg(feature = "oci")]
pub use manager::{FsManager, Manager, SystemdManager};
pub mod stats;
pub use stats::CgroupStats;
pub mod systemd;
/// The maximum value for CPU shares in cgroups v1
pub const CPU_SHARES_V1_MAX: u64 = 262144;
/// The maximum value for CPU weight in cgroups v2
pub const CPU_WEIGHT_V2_MAX: u64 = 10000;
/// The current state of the control group
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FreezerState {
/// The processes in the control group are _not_ frozen.
Thawed,
/// The processes in the control group are in the processes of being frozen.
Freezing,
/// The processes in the control group are frozen.
Frozen,
}
/// A structure representing a `pid`. Currently implementations exist for `u64` and
/// `std::process::Child`.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct CgroupPid {
/// The process identifier
pub pid: u64,
}
impl From<u64> for CgroupPid {
fn from(u: u64) -> CgroupPid {
CgroupPid { pid: u }
}
}
impl From<&std::process::Child> for CgroupPid {
fn from(u: &std::process::Child) -> CgroupPid {
CgroupPid { pid: u.id() as u64 }
}
}
#[cfg(test)]
pub mod tests {
use std::fs;
use std::process::{Child, Command, Stdio};
/// Start a mock subprocess that will sleep forever
pub fn spawn_sleep_inf() -> Child {
let child = Command::new("sleep")
.arg("infinity")
.spawn()
.expect("Failed to start mock subprocess");
child
}
pub fn spawn_yes() -> Child {
let devnull = fs::File::create("/dev/null").expect("cannot open /dev/null");
let child = Command::new("yes")
.stdout(Stdio::from(devnull))
.spawn()
.expect("Failed to start mock subprocess");
child
}
pub fn systemd_version() -> Option<String> {
let output = Command::new("systemd").arg("--version").output().ok()?; // Return None if command execution fails
if !output.status.success() {
return None;
}
Some(String::from_utf8_lossy(&output.stdout).to_string())
}
}