code wiki / _hdl_build / nx_hostctl_syscalls_test.nx

nx_hostctl_syscalls_test.nx source

↩ module page · 50 lines · 2640 B

1// nx_hostctl_syscalls_test.nx -- ENGINEER gate for the control-plane syscalls (sys_mkdir / sys_chmod / 2// sys_kill). Real kernel, no mocks: make a dir + create a file in it; chmod it; fork a sleeping child, 3// SIGKILL it, and reap it. These replace the shell glue (mkdir/chmod/pkill) in the sovereign deploy. 4// license_tier: ORIGINAL 5import "nx_syscalls.nx" 6 7func t_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func t_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(1,b,k); return 0 } 9 10func main() -> i64 { 11 var pass: i64 = 0 12 var total: i64 = 0 13 14 // 1. sys_mkdir -- create a doc-root dir, prove usable by creating a file inside it 15 sys_mkdir("/tmp/hc_sct_dir" as *u8, 0x1ed) 16 let fd: i64 = sys_openat_wr("/tmp/hc_sct_dir/probe" as *u8, 0x1a4) 17 total = total + 1 18 if fd >= 0 { sys_write(fd, "x" as *u8, 1); sys_close(fd); t_puts(" PASS sys_mkdir: dir created + usable\n" as *u8); pass = pass + 1 } 19 else { t_puts(" FAIL sys_mkdir: cannot create file in dir\n" as *u8) } 20 21 // 2. nx_chmod -- +x semantics on the probe file 22 let cr: i64 = nx_chmod("/tmp/hc_sct_dir/probe" as *u8, 0x1ed) 23 total = total + 1 24 if cr == 0 { t_puts(" PASS sys_chmod: rc=0\n" as *u8); pass = pass + 1 } else { t_puts(" FAIL sys_chmod: rc!=0\n" as *u8) } 25 26 // 3. nx_kill -- fork a SHORT-sleeping child (3s so a broken kill can't hang the gate), 27 // SIGKILL it, and confirm it was terminated by signal 9 (not a normal exit = kill no-op). 28 let kid: i64 = sys_fork() 29 if kid == 0 { 30 sys_sleep_ms(3000) 31 sys_exit(0) 32 return 0 33 } 34 sys_sleep_ms(500) 35 let killrc: i64 = nx_kill(kid, 9) 36 let stbox: *i64 = sys_mmap(8) as *i64; stbox[0] = 0 37 let reaped: i64 = sys_wait4(kid, stbox, 0) 38 let sig: i64 = stbox[0] & 0x7f 39 total = total + 1 40 if killrc == 0 { 41 if reaped == kid { 42 if sig == 9 { t_puts(" PASS nx_kill: child terminated by SIGKILL(9) + reaped\n" as *u8); pass = pass + 1 } 43 else { t_puts(" FAIL nx_kill: child exited normally (kill was a no-op)\n" as *u8) } 44 } else { t_puts(" FAIL nx_kill: wait4 did not reap child\n" as *u8) } 45 } else { t_puts(" FAIL nx_kill: rc!=0\n" as *u8) } 46 47 t_puts("---- control-plane syscalls gate: passed " as *u8); t_pn(pass); t_puts(" / " as *u8); t_pn(total); t_puts(" ----\n" as *u8) 48 if pass == total { sys_exit(0); return 0 } 49 sys_exit(1); return 1 50}