Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions crates/libcgroups/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ default = ["v1", "v2", "systemd"]
v1 = []
v2 = []
# The systemd manager attaches the eBPF device filter itself, over the one systemd derives
# from DeviceAllow, so it cannot be built without it. This makes "systemd cgroupsv2_devices"
# the same build as "systemd"; scripts/features_test.sh lists only the latter.
systemd = ["v2", "cgroupsv2_devices", "nix/socket", "nix/uio"]
# from DeviceAllow, so "systemd" includes it. This makes "systemd cgroupsv2_devices" the same
# build as "systemd"; scripts/features_test.sh lists only the latter.
systemd = ["systemd_base", "cgroupsv2_devices"]
# The systemd manager without the eBPF device filter, for builds that cannot take libbpf's C
# dependencies. Device rules are then expressed only through DevicePolicy/DeviceAllow, and a
# rule set containing a deny rule, which DeviceAllow cannot express, is refused.
systemd_base = ["v2", "nix/socket", "nix/uio"]
cgroupsv2_devices = ["rbpf", "libbpf-sys", "errno", "libc", "nix/dir"]

[dependencies]
Expand Down
35 changes: 25 additions & 10 deletions crates/libcgroups/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use std::time::Duration;
use nix::sys::statfs::{CGROUP2_SUPER_MAGIC, TMPFS_MAGIC, statfs};
use nix::unistd::Pid;
use oci_spec::runtime::LinuxResources;
#[cfg(any(feature = "cgroupsv2_devices", feature = "v1"))]
#[cfg(any(
feature = "cgroupsv2_devices",
feature = "v1",
feature = "systemd_base"
))]
use oci_spec::runtime::{
LinuxDevice, LinuxDeviceBuilder, LinuxDeviceCgroup, LinuxDeviceCgroupBuilder, LinuxDeviceType,
};
Expand All @@ -18,7 +22,7 @@ use super::{systemd, v1, v2};
pub const CGROUP_PROCS: &str = "cgroup.procs";
pub const DEFAULT_CGROUP_ROOT: &str = "/sys/fs/cgroup";

#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
#[inline]
fn is_true_root() -> Result<bool, WrappedIoError> {
if !nix::unistd::geteuid().is_root() {
Expand Down Expand Up @@ -126,12 +130,15 @@ impl AnyCgroupManager {
/// Marks the cgroup manager as operating in a rootless environment.
///
/// The v1 manager ignores it.
#[cfg_attr(not(any(feature = "v2", feature = "systemd")), allow(unused_variables))]
#[cfg_attr(
not(any(feature = "v2", feature = "systemd_base")),
allow(unused_variables)
)]
pub fn with_rootless(self, rootless: bool) -> Self {
match self {
#[cfg(feature = "v2")]
AnyCgroupManager::V2(m) => AnyCgroupManager::V2(m.with_rootless(rootless)),
#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
AnyCgroupManager::Systemd(m) => {
AnyCgroupManager::Systemd(Box::new(m.with_rootless(rootless)))
}
Expand All @@ -158,10 +165,10 @@ impl AnyCgroupManager {
/// The systemd manager needs them to create the unit, since systemd takes some
/// properties, such as OOMPolicy, only at that point. `apply` is still what applies
/// the resources. The v1 and v2 managers ignore it.
#[cfg_attr(not(feature = "systemd"), allow(unused_variables))]
#[cfg_attr(not(feature = "systemd_base"), allow(unused_variables))]
pub fn with_initial_resources(self, resources: Option<&LinuxResources>) -> Self {
match self {
#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
AnyCgroupManager::Systemd(m) => {
AnyCgroupManager::Systemd(Box::new(m.with_initial_resources(resources)))
}
Expand Down Expand Up @@ -487,7 +494,7 @@ fn create_v2_cgroup_manager(
Err(v2::manager::V2ManagerError::NotEnabled)
}

#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
fn create_systemd_cgroup_manager(
root_path: &Path,
cgroup_path: &Path,
Expand All @@ -514,7 +521,7 @@ fn create_systemd_cgroup_manager(
)
}

#[cfg(not(feature = "systemd"))]
#[cfg(not(feature = "systemd_base"))]
fn create_systemd_cgroup_manager(
_root_path: &Path,
_cgroup_path: &Path,
Expand Down Expand Up @@ -605,7 +612,11 @@ pub(crate) fn clean_cgroup_path(path: &Path) -> PathBuf {
cleaned
}

#[cfg(any(feature = "cgroupsv2_devices", feature = "v1"))]
#[cfg(any(
feature = "cgroupsv2_devices",
feature = "v1",
feature = "systemd_base"
))]
pub(crate) fn default_allow_devices() -> Vec<LinuxDeviceCgroup> {
vec![
LinuxDeviceCgroupBuilder::default()
Expand Down Expand Up @@ -657,7 +668,11 @@ pub(crate) fn default_allow_devices() -> Vec<LinuxDeviceCgroup> {
]
}

#[cfg(any(feature = "cgroupsv2_devices", feature = "v1"))]
#[cfg(any(
feature = "cgroupsv2_devices",
feature = "v1",
feature = "systemd_base"
))]
pub(crate) fn default_devices() -> Vec<LinuxDevice> {
vec![
LinuxDeviceBuilder::default()
Expand Down
4 changes: 2 additions & 2 deletions crates/libcgroups/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ mod test;

pub mod common;
pub mod stats;
#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
pub mod systemd;
#[cfg(not(feature = "systemd"))]
#[cfg(not(feature = "systemd_base"))]
#[path = "stub/systemd/mod.rs"]
pub mod systemd;
pub mod test_manager;
Expand Down
6 changes: 3 additions & 3 deletions crates/libcgroups/src/systemd/dbus_native/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ mod tests {
}

#[test]
#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
fn test_dbus_connection_new() {
let uid: u32 = getuid().into();

Expand All @@ -886,7 +886,7 @@ mod tests {
}

#[test]
#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
fn test_dbus_function_calls() -> Result<()> {
use crate::systemd::dbus_native::serialize::Variant;

Expand Down Expand Up @@ -924,7 +924,7 @@ mod tests {
}

#[test]
#[cfg(feature = "systemd")]
#[cfg(feature = "systemd_base")]
fn test_dbus_function_calls_errors() {
use crate::systemd::dbus_native::utils::DbusError;

Expand Down
Loading
Loading