diff --git a/crates/forge-runner/src/check.rs b/crates/forge-runner/src/check.rs index 346f821..56f516f 100644 --- a/crates/forge-runner/src/check.rs +++ b/crates/forge-runner/src/check.rs @@ -14,6 +14,7 @@ use tokio::process::Command; use crate::config::{Limits, UsernsMode}; use crate::sandbox::SandboxRequest; +use crate::sandbox::helper::job_cgroup_base; use crate::sandbox::idmap::IdMap; /// The probe job. It prints the seccomp mode of the job, then succeeds. @@ -211,20 +212,20 @@ fn sandbox_lines(stderr: &str) -> Vec<&str> { .collect() } -/// The cgroup v2 controllers enabled for the job cgroup, which is a child of the runner's parent -/// slice (see the helper). Returns the enabled list, or why it could not be read. +/// The cgroup v2 controllers enabled for job cgroups, read where the helper puts them. Fails when +/// that cgroup is not ours to write, as in an SSH session's scope. fn delegated_controllers() -> Result, String> { - let text = fs::read_to_string("/proc/self/cgroup") - .map_err(|e| format!("cannot read /proc/self/cgroup: {e}"))?; - let own = text - .lines() - .find_map(|line| line.strip_prefix("0::")) - .ok_or_else(|| "cgroup v2 is not in use by this process".to_owned())?; - let own = PathBuf::from(own.trim()); - let base = own.parent().unwrap_or(&own); - let file = Path::new("/sys/fs/cgroup") - .join(base.strip_prefix("/").unwrap_or(base)) - .join("cgroup.subtree_control"); + let base = job_cgroup_base().map_err(|e| e.to_string())?; + let dir = Path::new("/sys/fs/cgroup").join(base.strip_prefix("/").unwrap_or(&base)); + let probe = dir.join(format!("forge-check-{}", std::process::id())); + fs::create_dir(&probe).map_err(|e| { + format!( + "cannot create a cgroup in {}: {e}; run under systemd --user, e.g. `systemd-run --user -P forge runner check`", + dir.display() + ) + })?; + let _ = fs::remove_dir(&probe); + let file = dir.join("cgroup.subtree_control"); let enabled = fs::read_to_string(&file).map_err(|e| format!("cannot read {}: {e}", file.display()))?; Ok(enabled.split_whitespace().map(str::to_owned).collect())