code wiki / _hdl_build / nx_hostop_watchdog_gate.nx

nx_hostop_watchdog_gate.nx source

↩ module page · 38 lines · 2902 B

1// nx_hostop_watchdog_gate.nx -- R12 PROOF: the host-operator watchdogs the publisher daemon. Fresh heartbeat -> no 2// action; stale (hung) -> restart command; missing (not running) -> launch command. The full supervised lifecycle, 3// composing pub_daemon_hb_age + hostop_supervise_cmd. File-based. license_tier: ORIGINAL expect_exit: 0 4import "nx_syscalls.nx" 5import "nx_hostop.nx" 6import "nx_runpath.nx" 7 8func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 10func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" " as *u8); g_w(id); g_w(": " as *u8); if ok==1 { g_w("OK\n" as *u8); pass[0]=pass[0]+1 } else { g_w("FAIL\n" as *u8) } return 0 } 11func g_unlink(path: *u8) -> i64 { __syscall(87, path as i64, 0, 0, 0, 0, 0); return 0 } 12 13func main() -> i64 { 14 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 15 g_w("=== NX-HOSTOP-WATCHDOG GATE (host-operator watchdogs the publisher daemon) ===\n" as *u8) 16 let wsid: *u8 = sys_mmap(64); rp_wsid(wsid); rp_ensure(wsid) 17 let D: *u8 = sys_mmap(512); rp_path(wsid, "hwd" as *u8, D); __syscall(83, D as i64, 493, 0, 0, 0, 0) 18 let hb: *u8 = sys_mmap(700); pub_join(D, "daemon.hb" as *u8, hb); g_unlink(hb) 19 let dmn: *u8 = "nx_pub_daemon.elf" as *u8 20 let out: *u8 = sys_mmap(256) 21 22 let hbe: i64 = pub_daemon_heartbeat(hb) // fresh heartbeat now 23 let fresh: i64 = hostop_watchdog(hb, dmn, 30, hbe, out) // age 0 < 30 -> healthy -> 0 24 let stale: i64 = hostop_watchdog(hb, dmn, 30, hbe + 100, out) // age 100 > 30 -> hung -> restart 25 let stale_cmd: i64 = (pub_substr(out, 255, "startsite" as *u8)) & (pub_substr(out, 255, "nx_pub_daemon.elf" as *u8)) 26 g_unlink(hb) 27 let missing: i64 = hostop_watchdog(hb, dmn, 30, hbe, out) // no heartbeat -> not running -> launch 28 let missing_cmd: i64 = pub_substr(out, 255, "startsite" as *u8) 29 30 g_w(" fresh=" as *u8); g_n(fresh); g_w(" (0=healthy) stale=" as *u8); g_n(stale); g_w(" missing=" as *u8); g_n(missing); g_w(" cmd='" as *u8); g_w(out); g_w("'\n" as *u8) 31 g_row("WATCHDOG: fresh heartbeat -> no action (healthy, 0)" as *u8, (fresh==0) as i64, pass) 32 g_row("WATCHDOG: STALE heartbeat (hung) -> restart command (1 + startsite + daemon)" as *u8, ((stale==1) as i64) & stale_cmd, pass) 33 g_row("WATCHDOG: MISSING heartbeat (not running) -> launch command (1)" as *u8, ((missing==1) as i64) & missing_cmd, pass) 34 35 g_w("NX-HOSTOP-WATCHDOG rows=3 pass=" as *u8); g_n(pass[0]) 36 if pass[0]==3 { g_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 37 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1 38}