oxedyne/fe2o3/fe2o3_sys/tests/supervise.rs
11.8 KiB, 5 runs
created by r1870400018:75885, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Tests for `fe2o3_sys::supervise` against real processes, a real cgroup and the kernel's own |
| 2 | //! account of them. |
| 3 | //! |
| 4 | //! The cgroup tests need a cgroup v2 subtree delegated to the caller with the `memory` controller, |
| 5 | //! which a test run gets from |
| 6 | //! `systemd-run --user --scope -p Delegate=yes <this test binary>`: the test moves the scope's own |
| 7 | //! processes into a leaf, as [`CgroupRoot::from_own_cgroup`] does, and confines its children under |
| 8 | //! the scope. Where the subtree is not delegated they say so and pass, unless |
| 9 | //! `FE2O3_SYS_SUPERVISE_REQUIRE` is set, when they fail. |
| 10 | //! |
| 11 | //! Every child is a copy of this test binary run as a helper: one that fills memory, one that arms |
| 12 | //! its parent's death and sleeps, and one that stands as the parent of that one and is then killed. |
| 13 | |
| 14 | #![cfg(all(target_os = "linux", feature = "supervise"))] |
| 15 | |
| 16 | use oxedyne_fe2o3_core::prelude::*; |
| 17 | use oxedyne_fe2o3_sys::signal::{ |
| 18 | to_process, |
| 19 | Proc, |
| 20 | Signal, |
| 21 | }; |
| 22 | use oxedyne_fe2o3_sys::supervise::{ |
| 23 | arm_parent_death, |
| 24 | CgroupRoot, |
| 25 | Confined, |
| 26 | Limit, |
| 27 | RestartBudget, |
| 28 | Scoped, |
| 29 | SliceKills, |
| 30 | start_slice, |
| 31 | }; |
| 32 | |
| 33 | use std::{ |
| 34 | env, |
| 35 | fs, |
| 36 | path::{ |
| 37 | Path, |
| 38 | PathBuf, |
| 39 | }, |
| 40 | process::Stdio, |
| 41 | sync::OnceLock, |
| 42 | thread, |
| 43 | time::{ |
| 44 | Duration, |
| 45 | Instant, |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | const HELPER: &str = "FE2O3_SYS_SUPERVISE_HELPER"; |
| 50 | const MODE: &str = "FE2O3_SYS_SUPERVISE_MODE"; |
| 51 | const REQUIRE: &str = "FE2O3_SYS_SUPERVISE_REQUIRE"; |
| 52 | const LEAF: &str = "supervisor"; |
| 53 | const MIB: u64 = 1024 * 1024; |
| 54 | const NAP: Duration = Duration::from_millis(20); |
| 55 | const PATIENCE: Duration = Duration::from_secs(10); |
| 56 | |
| 57 | fn io<T>(r: std::io::Result<T>, what: &str) -> Outcome<T> { |
| 58 | match r { |
| 59 | Ok(v) => Ok(v), |
| 60 | Err(e) => Err(err!("{}: {}.", what, e; IO, File)), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // A directory of this test's own, under the target directory rather than the shared temporary one. |
| 65 | struct Dir(PathBuf); |
| 66 | |
| 67 | impl Dir { |
| 68 | fn new(what: &str) -> Outcome<Self> { |
| 69 | let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR")) |
| 70 | .join(format!("fe2o3_sys_supervise_{}_{}", what, std::process::id())); |
| 71 | let _ = fs::remove_dir_all(&path); |
| 72 | res!(io(fs::create_dir_all(&path), "make the test directory")); |
| 73 | Ok(Self(path)) |
| 74 | } |
| 75 | |
| 76 | fn file(&self, name: &str) -> PathBuf { |
| 77 | self.0.join(name) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl Drop for Dir { |
| 82 | fn drop(&mut self) { |
| 83 | let _ = fs::remove_dir_all(&self.0); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn until<F: Fn() -> bool>(what: &str, ask: F) -> Outcome<()> { |
| 88 | let begun = Instant::now(); |
| 89 | while !ask() { |
| 90 | if begun.elapsed() > PATIENCE { |
| 91 | return Err(err!("Waited {} s for {}.", PATIENCE.as_secs(), what; Test, Timeout)); |
| 92 | } |
| 93 | thread::sleep(NAP); |
| 94 | } |
| 95 | Ok(()) |
| 96 | } |
| 97 | |
| 98 | // The delegated root, once for the whole binary, or why there is none. |
| 99 | fn root() -> Option<&'static CgroupRoot> { |
| 100 | static ROOT: OnceLock<std::result::Result<CgroupRoot, String>> = OnceLock::new(); |
| 101 | let got = ROOT.get_or_init(|| match CgroupRoot::from_own_cgroup(LEAF) { |
| 102 | Ok(r) => Ok(r), |
| 103 | Err(e) => Err(format!("{}", e)), |
| 104 | }); |
| 105 | match got { |
| 106 | Ok(r) => Some(r), |
| 107 | Err(e) => { |
| 108 | assert!(env::var(REQUIRE).is_err(), "no delegated cgroup subtree: {}", e); |
| 109 | eprintln!("SKIP: no delegated cgroup subtree with memory ({}); run under \ |
| 110 | systemd-run --user --scope -p Delegate=yes", e); |
| 111 | None |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // This test binary as a helper in the given mode, confined to `limit`. |
| 117 | fn confine(dir: &Path, mode: &str, limit: &Limit) -> Outcome<Confined> { |
| 118 | let exe = res!(io(env::current_exe(), "find this test binary")); |
| 119 | let mut conf = res!(Confined::new(&exe, limit)); |
| 120 | conf.command() |
| 121 | .args(["--exact", "helper", "--nocapture", "--test-threads=1"]) |
| 122 | .env(HELPER, dir) |
| 123 | .env(MODE, mode) |
| 124 | .stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); |
| 125 | Ok(conf) |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn helper() -> Outcome<()> { |
| 130 | let dir = match env::var(HELPER) { |
| 131 | Ok(d) => PathBuf::from(d), |
| 132 | Err(_) => return Ok(()), |
| 133 | }; |
| 134 | match env::var(MODE).as_deref() { |
| 135 | // Touches every page of more than the cap allows, a mebibyte at a time. |
| 136 | Ok("fill") => { |
| 137 | let mut held = Vec::new(); |
| 138 | for _ in 0..512 { |
| 139 | held.push(vec![7u8; MIB as usize]); |
| 140 | } |
| 141 | res!(io(fs::write(dir.join("filled"), format!("{}", held.len())), "note the fill")); |
| 142 | } |
| 143 | Ok("quiet") => { |
| 144 | res!(io(fs::write(dir.join("quiet"), "ok"), "note the quiet run")); |
| 145 | } |
| 146 | // Arms its parent's death, says so, and sleeps long past the test. |
| 147 | Ok("child") => { |
| 148 | res!(arm_parent_death(Signal::Kill)); |
| 149 | res!(io(fs::write(dir.join("armed"), "ok"), "note the arming")); |
| 150 | thread::sleep(Duration::from_secs(60)); |
| 151 | } |
| 152 | // Starts the child, notes its number, and waits to be killed. |
| 153 | Ok("parent") => { |
| 154 | let conf = res!(confine(&dir, "child", &Limit::rlimit(1024 * MIB))); |
| 155 | let sup = res!(conf.spawn()); |
| 156 | res!(io(fs::write(dir.join("child_pid"), format!("{}", sup.pid())), "note the child")); |
| 157 | thread::sleep(Duration::from_secs(60)); |
| 158 | } |
| 159 | _ => return Err(err!("No helper mode."; Test, Invalid, Input)), |
| 160 | } |
| 161 | Ok(()) |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn the_fourth_restart_in_the_window_is_refused_and_one_after_it_is_allowed() { |
| 166 | let t0 = Instant::now(); |
| 167 | let mut budget = RestartBudget::new(3, Duration::from_secs(600)); |
| 168 | assert!(budget.try_restart(t0)); |
| 169 | assert!(budget.try_restart(t0 + Duration::from_secs(60))); |
| 170 | assert!(budget.try_restart(t0 + Duration::from_secs(120))); |
| 171 | assert_eq!(budget.remaining(t0 + Duration::from_secs(130)), 0); |
| 172 | assert!(!budget.try_restart(t0 + Duration::from_secs(180)), "a fourth restart in ten minutes"); |
| 173 | // A refusal is not counted, so the window frees as the first restart ages out of it. |
| 174 | assert!(!budget.try_restart(t0 + Duration::from_secs(599))); |
| 175 | assert!(budget.try_restart(t0 + Duration::from_secs(601))); |
| 176 | assert!(!budget.try_restart(t0 + Duration::from_secs(602))); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn a_child_over_its_memory_max_is_oom_killed_and_reported_so() -> Outcome<()> { |
| 181 | let root = match root() { |
| 182 | Some(r) => r, |
| 183 | None => return Ok(()), |
| 184 | }; |
| 185 | let dir = res!(Dir::new("oom")); |
| 186 | let limit = Limit::cgroup(root, "fill", 32 * MIB); |
| 187 | let sup = res!(res!(confine(&dir.0, "fill", &limit)).spawn()); |
| 188 | let cg = match sup.cgroup() { |
| 189 | Some(p) => p.to_path_buf(), |
| 190 | None => return Err(err!("A confined child has no cgroup."; Test, Missing)), |
| 191 | }; |
| 192 | // The kernel's own account: the child sits in its own cgroup, under the cap. |
| 193 | let procs = res!(io(fs::read_to_string(cg.join("cgroup.procs")), "read the child's cgroup")); |
| 194 | let max = res!(io(fs::read_to_string(cg.join("memory.max")), "read memory.max")); |
| 195 | assert_eq!(max.trim(), format!("{}", 32 * MIB)); |
| 196 | let ended = res!(sup.wait()); |
| 197 | assert!(ended.oom_killed(), "{:?} (cgroup held {:?})", ended, procs); |
| 198 | assert!(!ended.status.success()); |
| 199 | assert!(!dir.file("filled").exists(), "the child filled past its cap"); |
| 200 | assert!(!cg.exists(), "the child's cgroup outlived it"); |
| 201 | Ok(()) |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn a_child_under_its_memory_max_ends_and_is_not_reported_oom_killed() -> Outcome<()> { |
| 206 | let root = match root() { |
| 207 | Some(r) => r, |
| 208 | None => return Ok(()), |
| 209 | }; |
| 210 | let dir = res!(Dir::new("quiet")); |
| 211 | let limit = Limit::cgroup(root, "quiet", 256 * MIB); |
| 212 | let ended = res!(res!(res!(confine(&dir.0, "quiet", &limit)).spawn()).wait()); |
| 213 | assert!(ended.status.success(), "{:?}", ended); |
| 214 | assert!(!ended.oom_killed()); |
| 215 | assert!(dir.file("quiet").exists()); |
| 216 | Ok(()) |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn without_a_cgroup_the_rlimit_stops_a_child_over_its_cap() -> Outcome<()> { |
| 221 | let dir = res!(Dir::new("rlimit")); |
| 222 | // The cap covers the address space, so it must leave room for the binary and its threads. |
| 223 | let cap = Limit::rlimit(300 * MIB); |
| 224 | let quiet = res!(res!(res!(confine(&dir.0, "quiet", &cap)).spawn()).wait()); |
| 225 | assert!(quiet.status.success(), "a child within its cap failed: {:?}", quiet); |
| 226 | let ended = res!(res!(res!(confine(&dir.0, "fill", &cap)).spawn()).wait()); |
| 227 | assert!(!ended.status.success(), "{:?}", ended); |
| 228 | assert!(!ended.oom_killed()); |
| 229 | assert!(!dir.file("filled").exists(), "the child filled past its cap"); |
| 230 | Ok(()) |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn a_child_that_arms_its_parents_death_dies_with_its_parent() -> Outcome<()> { |
| 235 | let dir = res!(Dir::new("pdeath")); |
| 236 | let parent = res!(res!(confine(&dir.0, "parent", &Limit::rlimit(1024 * MIB))).spawn()); |
| 237 | res!(until("the child to arm", || dir.file("armed").exists())); |
| 238 | let text = res!(io(fs::read_to_string(dir.file("child_pid")), "read the child's number")); |
| 239 | let pid: u32 = match text.trim().parse() { |
| 240 | Ok(p) => p, |
| 241 | Err(e) => return Err(err!("Child pid {:?}: {}.", text, e; Test, Invalid, Input)), |
| 242 | }; |
| 243 | let child = match Proc::of(pid) { |
| 244 | Some(p) => p, |
| 245 | None => return Err(err!("The child {} is not running.", pid; Test, Missing)), |
| 246 | }; |
| 247 | res!(to_process(parent.pid(), Signal::Kill)); |
| 248 | let ended = res!(parent.wait()); |
| 249 | assert!(!ended.status.success()); |
| 250 | res!(until("the child to die with its parent", || !child.running())); |
| 251 | Ok(()) |
| 252 | } |
| 253 | |
| 254 | // ── Scoped ────────────────────────────────────────────────────────────────────────────────────── |
| 255 | |
| 256 | const TEST_SLICE: &str = "fe2o3_scope_test.slice"; // underscores: a dash would nest it in another slice |
| 257 | |
| 258 | /// The test slice's counter, or `None`, said, where there is no user manager with memory. |
| 259 | fn test_slice() -> Option<PathBuf> { |
| 260 | match oxedyne_fe2o3_sys::supervise::manager_has_memory() { |
| 261 | Ok(true) => (), |
| 262 | other => { |
| 263 | eprintln!("skipped: no user manager with the memory controller ({:?})", other); |
| 264 | return None; |
| 265 | }, |
| 266 | } |
| 267 | match start_slice(TEST_SLICE) { |
| 268 | Ok(p) => Some(p), |
| 269 | Err(e) => { |
| 270 | eprintln!("skipped: the test slice could not be started: {}", e); |
| 271 | None |
| 272 | }, |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /// **A scoped child over its cap is reported as memory, from the slice's counter.** A shell says |
| 277 | /// hello, then holds about 100 MB in a variable under a 32 MiB cap. |
| 278 | #[test] |
| 279 | fn a_scoped_child_over_its_cap_is_reported_as_memory() -> Outcome<()> { |
| 280 | let events = match test_slice() { |
| 281 | Some(p) => p, |
| 282 | None => return Ok(()), |
| 283 | }; |
| 284 | let mut scoped = res!(Scoped::new("sh", "fe2o3-scope-test", TEST_SLICE, &events, 32 << 20)); |
| 285 | scoped.command() |
| 286 | .args(["-c", "printf '\\006'; x=$(head -c 100000000 /dev/zero | tr '\\0' a); echo ${#x}"]) |
| 287 | .stdin(Stdio::null()) |
| 288 | .stdout(Stdio::piped()); |
| 289 | let mut child = res!(scoped.spawn()); |
| 290 | assert!(res!(child.hello()), "the shell's hello was not read"); |
| 291 | let ended = res!(child.wait()); |
| 292 | assert!(ended.oom_killed(), "not reported as memory: {:?}", ended); |
| 293 | Ok(()) |
| 294 | } |
| 295 | |
| 296 | /// **A scope that never started is told by its missing hello.** The slice named is a service, which |
| 297 | /// `systemd-run` refuses; the program never runs, and no kill is counted. |
| 298 | #[test] |
| 299 | fn a_scope_that_never_started_says_no_hello() -> Outcome<()> { |
| 300 | let events = match test_slice() { |
| 301 | Some(p) => p, |
| 302 | None => return Ok(()), |
| 303 | }; |
| 304 | let mut scoped = res!(Scoped::new("sh", "fe2o3-scope-test", "not-a-slice.service", &events, 32 << 20)); |
| 305 | scoped.command().args(["-c", "printf '\\006'"]).stdin(Stdio::null()).stdout(Stdio::piped()) |
| 306 | .stderr(Stdio::null()); |
| 307 | let mut child = res!(scoped.spawn()); |
| 308 | assert!(!res!(child.hello()), "a hello from a scope that was refused"); |
| 309 | let ended = res!(child.wait()); |
| 310 | assert!(!ended.status.success()); |
| 311 | assert_eq!(ended.kills, SliceKills::None); |
| 312 | Ok(()) |
| 313 | } |