code wiki / (root) / nx_cgroup_v2_probe_test.nx

nx_cgroup_v2_probe_test.nx source

↩ module page · 69 lines · 2694 B

1// nx_cgroup_v2_probe_test.nx -- smoke for cgroups v2 host detection. 2// 3// Honest scope: this smoke MUST pass green on ANY host. On hosts 4// without cgroups v2 (Cygwin, Windows-mounted bash, macOS, Linux 5// with v1 only) -> available() returns 0; the smoke verifies that 6// path. On Linux hosts with cgroups v2 -> available() returns 1; 7// the smoke verifies the controllers file has non-empty contents. 8// 9// Either path is GREEN. Refusing to fail on the unavailable 10// path is the racing-line discipline: the probe is read-only; 11// "not available here" is a legitimate state, not an error. 12 13import "nx_syscalls.nx" 14import "nx_cgroup_v2_probe.nx" 15 16func main() -> i64 { 17 // ----- 1. Verdict is in the sealed set ----- 18 let avail: i64 = nx_cgroup_v2_available() 19 if nx_cg_verdict_is_valid(avail) != 1 { return 1 } 20 21 // ----- 2. Available path ----- 22 if avail == NX_CG_AVAILABLE { 23 // On a Linux host with cgroups v2, controllers file must 24 // be non-empty. 25 let buf: *u8 = sys_mmap(512) 26 let n: i64 = nx_cgroup_v2_controllers(buf, 512) 27 if n <= 0 { return 2 } 28 // First byte is an alphanumeric controller name char 29 // (e.g., 'c' for cpu, 'm' for memory, 'p' for pids). 30 let b0: i64 = (buf[0] as i64) & 255 31 // Accept letter range a-z OR A-Z. 32 if b0 >= 97 { 33 if b0 > 122 { return 3 } 34 } else { 35 if b0 >= 65 { 36 if b0 > 90 { return 4 } 37 } else { 38 return 5 // first byte not letter -- malformed 39 } 40 } 41 // Buffer should end in NUL or newline byte for safety. 42 // Just confirm n is in a plausible range. 43 if n > 511 { return 6 } 44 } 45 46 // ----- 3. Unavailable path ----- 47 // On Cygwin/Windows/macOS (this dev host), available() returns 48 // NX_CG_UNAVAILABLE (= 0). controllers() should also return -1. 49 if avail == NX_CG_UNAVAILABLE { 50 let buf: *u8 = sys_mmap(64) 51 let n: i64 = nx_cgroup_v2_controllers(buf, 64) 52 if n != -1 { return 7 } 53 } 54 55 // ----- 4. Bad-input gates on controllers ----- 56 let null_buf: *u8 = (0 as i64) as *u8 57 if nx_cgroup_v2_controllers(null_buf, 64) != -1 { return 8 } 58 let any_buf: *u8 = sys_mmap(64) 59 if nx_cgroup_v2_controllers(any_buf, 0) != -1 { return 9 } 60 if nx_cgroup_v2_controllers(any_buf, -1) != -1 { return 10 } 61 62 // ----- 5. Sealed-enum gate ----- 63 if nx_cg_verdict_is_valid(NX_CG_AVAILABLE) != 1 { return 11 } 64 if nx_cg_verdict_is_valid(NX_CG_UNAVAILABLE) != 1 { return 12 } 65 if nx_cg_verdict_is_valid(99) != 0 { return 13 } 66 if nx_cg_verdict_is_valid(-7) != 0 { return 14 } 67 68 return 0 69}