Skip to content

Commit eca1a30

Browse files
committed
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 <[email protected]>
1 parent 5d74a1d commit eca1a30

File tree

8 files changed

+18
-88
lines changed

8 files changed

+18
-88
lines changed

src/lib.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,11 @@ pub mod tests {
7272
child
7373
}
7474

75-
pub fn systemd_version() -> Option<usize> {
75+
pub fn systemd_version() -> Option<String> {
7676
let output = Command::new("systemd").arg("--version").output().ok()?; // Return None if command execution fails
77-
7877
if !output.status.success() {
7978
return None;
8079
}
81-
82-
let stdout = String::from_utf8_lossy(&output.stdout);
83-
84-
// The first line is typically like "systemd 254 (254.5-1-arch)"
85-
let first_line = stdout.lines().next()?;
86-
let mut words = first_line.split_whitespace();
87-
88-
words.next()?; // Skip the "systemd" word
89-
let version_str = words.next()?; // The version number as string
90-
91-
version_str.parse::<usize>().ok()
80+
Some(String::from_utf8_lossy(&output.stdout).to_string())
9281
}
9382
}

src/manager/systemd.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,21 @@ impl SystemdManager<'_> {
9191
&self.unit
9292
}
9393

94-
fn set_cpuset(
95-
&self,
96-
props: &mut Vec<Property>,
97-
linux_cpu: &LinuxCpu,
98-
systemd_version: usize,
99-
) -> Result<()> {
94+
fn set_cpuset(&self, props: &mut Vec<Property>, linux_cpu: &LinuxCpu) -> Result<()> {
10095
if let Some(cpus) = linux_cpu.cpus().as_ref() {
101-
let (id, value) = cpuset::cpus(cpus, systemd_version)?;
96+
let (id, value) = cpuset::cpus(cpus)?;
10297
props.push((id, value.into()));
10398
}
10499

105100
if let Some(mems) = linux_cpu.mems().as_ref() {
106-
let (id, value) = cpuset::mems(mems, systemd_version)?;
101+
let (id, value) = cpuset::mems(mems)?;
107102
props.push((id, value.into()));
108103
}
109104

110105
Ok(())
111106
}
112107

113-
fn set_cpu(
114-
&self,
115-
props: &mut Vec<Property>,
116-
linux_cpu: &LinuxCpu,
117-
systemd_version: usize,
118-
) -> Result<()> {
108+
fn set_cpu(&self, props: &mut Vec<Property>, linux_cpu: &LinuxCpu) -> Result<()> {
119109
if let Some(shares) = linux_cpu.shares() {
120110
let shares = if self.v2() {
121111
conv::cpu_shares_to_cgroup_v2(shares)
@@ -130,7 +120,7 @@ impl SystemdManager<'_> {
130120
let quota = linux_cpu.quota().unwrap_or(0);
131121

132122
if period != 0 {
133-
let (id, value) = cpu::period(period, systemd_version)?;
123+
let (id, value) = cpu::period(period)?;
134124
props.push((id, value.into()));
135125
}
136126

@@ -272,11 +262,9 @@ impl Manager for SystemdManager<'_> {
272262
fn set(&mut self, resources: &LinuxResources) -> Result<()> {
273263
let mut props = vec![];
274264

275-
let systemd_version = self.systemd_client.systemd_version()?;
276-
277265
if let Some(linux_cpu) = resources.cpu() {
278-
self.set_cpuset(&mut props, linux_cpu, systemd_version)?;
279-
self.set_cpu(&mut props, linux_cpu, systemd_version)?;
266+
self.set_cpuset(&mut props, linux_cpu)?;
267+
self.set_cpu(&mut props, linux_cpu)?;
280268
}
281269

282270
if let Some(linux_memory) = resources.memory() {

src/systemd/cpu.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
// SPDX-License-Identifier: Apache-2.0 or MIT
44
//
55

6-
use crate::systemd::error::{Error, Result};
7-
use crate::systemd::{
8-
CPU_QUOTA_PERIOD_US, CPU_QUOTA_PER_SEC_US, CPU_SHARES, CPU_SYSTEMD_VERSION, CPU_WEIGHT,
9-
};
6+
use crate::systemd::error::Result;
7+
use crate::systemd::{CPU_QUOTA_PERIOD_US, CPU_QUOTA_PER_SEC_US, CPU_SHARES, CPU_WEIGHT};
108

119
/// Returns the property for CPU shares.
1210
///
@@ -21,11 +19,7 @@ pub fn shares(shares: u64, v2: bool) -> Result<(&'static str, u64)> {
2119
}
2220

2321
/// Returns the property for CPU period.
24-
pub fn period(period: u64, systemd_version: usize) -> Result<(&'static str, u64)> {
25-
if systemd_version < CPU_SYSTEMD_VERSION {
26-
return Err(Error::ObsoleteSystemd);
27-
}
28-
22+
pub fn period(period: u64) -> Result<(&'static str, u64)> {
2923
Ok((CPU_QUOTA_PERIOD_US, period))
3024
}
3125

src/systemd/cpuset.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,19 @@
66
use bit_vec::BitVec;
77

88
use crate::systemd::error::{Error, Result};
9-
use crate::systemd::{ALLOWED_CPUS, ALLOWED_MEMORY_NODES, CPUSET_SYSTEMD_VERSION};
9+
use crate::systemd::{ALLOWED_CPUS, ALLOWED_MEMORY_NODES};
1010

1111
const BYTE_IN_BITS: usize = 8;
1212

1313
/// Returns the property for cpuset CPUs.
14-
pub fn cpus(cpus: &str, systemd_version: usize) -> Result<(&'static str, Vec<u8>)> {
15-
if systemd_version < CPUSET_SYSTEMD_VERSION {
16-
return Err(Error::ObsoleteSystemd);
17-
}
18-
14+
pub fn cpus(cpus: &str) -> Result<(&'static str, Vec<u8>)> {
1915
let mask = convert_list_to_mask(cpus)?;
20-
2116
Ok((ALLOWED_CPUS, mask))
2217
}
2318

2419
/// Returns the property for cpuset memory nodes.
25-
pub fn mems(mems: &str, systemd_version: usize) -> Result<(&'static str, Vec<u8>)> {
26-
if systemd_version < CPUSET_SYSTEMD_VERSION {
27-
return Err(Error::ObsoleteSystemd);
28-
}
29-
20+
pub fn mems(mems: &str) -> Result<(&'static str, Vec<u8>)> {
3021
let mask = convert_list_to_mask(mems)?;
31-
3222
Ok((ALLOWED_MEMORY_NODES, mask))
3323
}
3424

src/systemd/dbus/client.rs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,9 @@ impl SystemdClient<'_> {
144144
}
145145

146146
/// Get the systemd version.
147-
pub fn systemd_version(&self) -> Result<usize> {
147+
pub fn systemd_version(&self) -> Result<String> {
148148
let sys_proxy = systemd_manager_proxy()?;
149-
150-
// Parse 249 from "249.11-0ubuntu3.16"
151-
let version = sys_proxy.version()?;
152-
let version = version
153-
.split('.')
154-
.next()
155-
.and_then(|v| v.parse::<usize>().ok())
156-
.ok_or(Error::CorruptedSystemdVersion(version))?;
157-
158-
Ok(version)
149+
Ok(sys_proxy.version()?)
159150
}
160151

161152
/// Check if the unit exists.
@@ -216,7 +207,7 @@ pub mod tests {
216207
use crate::systemd::props::PropertiesBuilder;
217208
use crate::systemd::utils::expand_slice;
218209
use crate::systemd::{DEFAULT_DESCRIPTION, DESCRIPTION, PIDS};
219-
use crate::tests::{spawn_sleep_inf, spawn_yes, systemd_version};
210+
use crate::tests::{spawn_sleep_inf, spawn_yes};
220211

221212
const TEST_SLICE: &str = "cgroupsrs-test.slice";
222213

@@ -504,19 +495,6 @@ pub mod tests {
504495
child.wait().unwrap();
505496
}
506497

507-
#[test]
508-
fn test_systemd_version() {
509-
skip_if_no_systemd!();
510-
511-
let unit = test_unit();
512-
let props = PropertiesBuilder::default_cgroup(TEST_SLICE, &unit).build();
513-
let cgroup = SystemdClient::new(&unit, props).unwrap();
514-
let version = cgroup.systemd_version().unwrap();
515-
516-
let expected_version = systemd_version().unwrap();
517-
assert_eq!(version, expected_version, "Systemd version mismatch");
518-
}
519-
520498
#[test]
521499
fn test_exists() {
522500
skip_if_no_systemd!();

src/systemd/dbus/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@ pub enum Error {
1212

1313
#[error("dbus error: {0}")]
1414
Dbus(#[from] zbus::Error),
15-
16-
#[error("corrupted systemd version: {0}")]
17-
CorruptedSystemdVersion(String),
1815
}

src/systemd/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ pub enum Error {
1010
#[error("invalid argument")]
1111
InvalidArgument,
1212

13-
#[error("obsolete systemd, please upgrade your systemd")]
14-
ObsoleteSystemd,
15-
1613
#[error("resource not supported by cgroups v1")]
1714
CgroupsV1NotSupported,
1815
}

src/systemd/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,3 @@ pub const DEFAULT_SLICE: &str = "system.slice";
2020

2121
pub const SLICE_SUFFIX: &str = ".slice";
2222
pub const SCOPE_SUFFIX: &str = ".scope";
23-
24-
pub const CPU_SYSTEMD_VERSION: usize = 242;
25-
pub const CPUSET_SYSTEMD_VERSION: usize = 244;

0 commit comments

Comments
 (0)