diff --git a/crates/sandbox2/src/lib.rs b/crates/sandbox2/src/lib.rs index aca6840bfb..df335cd7fc 100644 --- a/crates/sandbox2/src/lib.rs +++ b/crates/sandbox2/src/lib.rs @@ -399,15 +399,19 @@ impl Sandbox { container.hostname(hn); } if let Some(s) = &self.config.cpu_weight { - container.cgroups_resources({ - let mut resources = hakoniwa::cgroups::Resources::default(); - resources.cpu({ - let mut cpu = hakoniwa::cgroups::Cpu::default(); - cpu.shares(*s); - cpu + if systemd_probably_works() { + container.cgroups_resources({ + let mut resources = hakoniwa::cgroups::Resources::default(); + resources.cpu({ + let mut cpu = hakoniwa::cgroups::Cpu::default(); + cpu.shares(*s); + cpu + }); + resources }); - resources - }); + } else { + tracing::debug!("Not configuring cgroups: systemd probably not available"); + } } Ok(Container { container }) @@ -736,3 +740,13 @@ impl Sandbox { fn hardlink_dir_contents(src_dir: &Path, dst_parent_dir: &Path) -> Result<(), Error> { common::hardlink_dir_contents(src_dir, dst_parent_dir).map_err(Error::HardlinkFailed) } + +fn systemd_probably_works() -> bool { + if std::env::var("DBUS_SESSION_BUS_ADDRESS").is_ok() { + return true; + } + if let Ok(dir) = std::env::var("XDG_RUNTIME_DIR") { + return std::fs::metadata(PathBuf::from(dir).join("bus")).is_ok(); + } + false +}