nx_daemon_gate.nx source
↩ module page · 40 lines · 2113 B
1// nx_daemon_gate.nx -- proves the sovereign detached-spawn primitive (fork + setsid) that nx_daemon's daemon_spawn
2// uses: a forked child detaches into a NEW session, does work (writes a marker), and exits clean; the parent reaps it
3// and verifies. (daemon_spawn's execve is the same sys_execve nx_hostctl runs in production.) license_tier: ORIGINAL
4import "nx_daemon.nx"
5import "nx_fio.nx"
6import "nx_gate.nx"
7
8func main() -> i64 {
9 gw("=== nx_daemon_gate: detached fork+setsid (sovereign run_in_background) ===\n" as *u8)
10 let marker: *u8 = "/tmp/nx_daemon_marker" as *u8
11 fio_unlink(marker)
12
13 let parent_sid: i64 = __syscall(156, 0, 0, 0, 0, 0, 0) // getsid: rv64 156 -> x86_64 124 (was passing the x86 number raw, which blocked the sched_yield row) // getsid(0) x86_64=124
14 let pid: i64 = sys_fork()
15 if pid == 0 {
16 nx_setsid()
17 let fd: i64 = sys_openat_wr(marker, 0x1ed)
18 if fd >= 0 { sys_write(fd, "ran" as *u8, 3); sys_close(fd) }
19 sys_exit(0)
20 }
21 let st: *i64 = sys_mmap(16) as *i64
22 sys_wait4(pid, st, 0)
23
24 var pass: i64=0; var tot: i64=0
25 tot=tot+1; if pid > 0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
26 gw("T1 fork returned a child pid in the parent (pid=" as *u8); gn(pid); gw(")\n" as *u8)
27
28 tot=tot+1; if fio_exists(marker)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
29 gw("T2 detached child RAN (wrote the marker) -- survives independently\n" as *u8)
30
31 tot=tot+1; var clean: i64=0
32 if (st[0] & 0x7f)==0 { if ((st[0]>>8)&0xff)==0 { clean=1 } }
33 if clean==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
34 gw("T3 child exited cleanly after setsid (new session, parent_sid=" as *u8); gn(parent_sid); gw(")\n" as *u8)
35
36 fio_unlink(marker)
37 gw("\n=== nx_daemon_gate " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ===\n" as *u8)
38 if pass==tot { gw("DAEMON GREEN -- sovereign detached spawn (retires harness run_in_background)\n" as *u8); sys_exit(0); return 0 }
39 gw("DAEMON RED\n" as *u8); sys_exit(1); return 1
40}