code wiki / (root) / nx_lock_reap_gate.nx

nx_lock_reap_gate.nx source

↩ module page · 68 lines · 4276 B

1// nx_lock_reap_gate.nx -- liar-killed GATE for the sovereign lock-reaper (R-ORCH-2a). 2// The load-bearing property is FAIL-SAFE: a lock is reaped ONLY when abandoned. Proves: stale+ 3// unowned -> REAP (and the real unlink removes it); a live-OWNER lock is NEVER reaped even when old 4// (the load-bearing neg-control -- never yank a lock a running process holds); a FRESH lock is 5// never reaped; an absent lock is a no-op; owner-detection finds a real live process (self via a 6// cmdline needle) and correctly reports absence for a bogus needle. Exit 0 only on all-PASS. 7// license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_lock_reap_core.nx" 10 11func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 12// MIGRATED to the shared emitter (debt 1785557603): the old body mmapped 32 bytes per call 13// and never freed them. nxi_out is the ONE shim, always balanced. Return contract preserved (0). 14func g_putn(v: i64) -> i64 { nxi_out(v); return 0 } 15func g_bool(name: *u8, got: i64, want: i64, passp: *i64) -> i64 { 16 g_puts("T " as *u8); g_puts(name); g_puts(" got=" as *u8); g_putn(got) 17 if got == want { g_puts(" PASS\n" as *u8); passp[0] = passp[0] + 1 } else { g_puts(" FAIL\n" as *u8) } 18 return 0 19} 20func g_write(path: *u8, s: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 420); if fd < 0 { return 0 - 1 } sys_write(fd, s, ccz_slen(s)); sys_close(fd); return 0 } 21func g_touch(path: *u8, e: i64) -> i64 { let t: *i64 = sys_mmap(32) as *i64; t[0] = e; t[1] = 0; t[2] = e; t[3] = 0; return sys_utimensat(path, t) } 22 23func main(argc: i64, argv: *i64) -> i64 { 24 let pass: *i64 = sys_mmap(16) as *i64 25 pass[0] = 0 26 let now: i64 = sys_now_realtime_sec() 27 28 // --- pure decision matrix (fail-safe) --- 29 g_bool("stale-unowned->REAP" as *u8, lr_should_reap(1, 0, 500, 120), 1, pass) 30 // load-bearing NEG-CONTROL: a live OWNER lock is NEVER reaped, even when very old 31 g_bool("owned-old->NO-REAP" as *u8, lr_should_reap(1, 1, 999999, 120), 0, pass) 32 g_bool("fresh-unowned->NO-REAP" as *u8, lr_should_reap(1, 0, 10, 120), 0, pass) 33 g_bool("absent->NO-REAP" as *u8, lr_should_reap(0, 0, 999, 120), 0, pass) 34 g_bool("unknown-age->NO-REAP" as *u8, lr_should_reap(1, 0, 0 - 1, 120), 0, pass) 35 36 // --- exists + age on real fixtures --- 37 g_write("/tmp/lr_gate.lock" as *u8, "x\n" as *u8) 38 g_bool("exists-present" as *u8, lr_exists("/tmp/lr_gate.lock" as *u8), 1, pass) 39 g_bool("exists-absent" as *u8, lr_exists("/tmp/lr_gate_absent.lock" as *u8), 0, pass) 40 g_touch("/tmp/lr_gate.lock" as *u8, now - 500) 41 var age: i64 = lr_age_s("/tmp/lr_gate.lock" as *u8, now) 42 var okage: i64 = 0 43 if age >= 480 { if age <= 520 { okage = 1 } } 44 g_bool("age-from-mtime" as *u8, okage, 1, pass) 45 46 // --- owner detection: a live process whose cmdline contains "nx_lock_reap_gate" = THIS gate --- 47 g_bool("owner-alive-self-needle" as *u8, lr_owner_alive("nx_lock_reap_gate" as *u8), 1, pass) 48 g_bool("owner-absent-bogus-needle" as *u8, lr_owner_alive("zzz_no_such_proc_needle_qq" as *u8), 0, pass) 49 50 // --- REAL reap: stale unowned lock (bogus needle -> no owner) gets removed --- 51 g_write("/tmp/lr_gate2.lock" as *u8, "y\n" as *u8) 52 g_touch("/tmp/lr_gate2.lock" as *u8, now - 500) 53 let d1: i64 = lr_should_reap(lr_exists("/tmp/lr_gate2.lock" as *u8), lr_owner_alive("zzz_no_such_qq" as *u8), lr_age_s("/tmp/lr_gate2.lock" as *u8, now), 120) 54 if d1 == 1 { lr_unlink("/tmp/lr_gate2.lock" as *u8) } 55 g_bool("real-reap-removed" as *u8, lr_exists("/tmp/lr_gate2.lock" as *u8), 0, pass) 56 57 // --- fail-safe: a FRESH lock is NOT reaped (still present after a reap pass) --- 58 g_write("/tmp/lr_gate3.lock" as *u8, "z\n" as *u8) 59 g_touch("/tmp/lr_gate3.lock" as *u8, now - 5) 60 let d2: i64 = lr_should_reap(lr_exists("/tmp/lr_gate3.lock" as *u8), 0, lr_age_s("/tmp/lr_gate3.lock" as *u8, now), 120) 61 if d2 == 1 { lr_unlink("/tmp/lr_gate3.lock" as *u8) } 62 g_bool("fresh-lock-preserved" as *u8, lr_exists("/tmp/lr_gate3.lock" as *u8), 1, pass) 63 64 g_puts("LOCKREAP-GATE pass=" as *u8); g_putn(pass[0]); g_puts("/12 verdict=" as *u8) 65 if pass[0] == 12 { g_puts("GREEN\n" as *u8); return 0 } 66 g_puts("RED\n" as *u8) 67 return 1 68}