code wiki / _hdl_build / nx_apistack_configpush.nx

nx_apistack_configpush.nx source

↩ module page · 31 lines · 1786 B

1// nx_apistack_configpush.nx -- CAP-API-CONFIGPUSH: declarative config push + hot-reload over /api. A caller POSTs new 2// config; cp_apply writes it and returns its GENERATION (FNV of the bytes). A daemon compares cp_current_gen against 3// its last-seen gen to detect a change and hot-reload -- config-as-data, reproducible, no restart, git-less (the 4// sovereign exceed over cloud config services). Same content -> same gen (idempotent reload). license_tier: ORIGINAL 5import "nx_syscalls.nx" 6const K_MAGIC_3750763034362895579: i64 = 3750763034362895579 7const K_MAGIC_1099511628211: i64 = 1099511628211 8const K_MAGIC_262144: i64 = 262144 9 10func cp_fnv(s: *u8, n: i64) -> i64 { 11 var h: i64 = 0 - K_MAGIC_3750763034362895579; var i: i64 = 0 12 while i < n { h = (h ^ ((s[i] as i64) & 0xff)) * K_MAGIC_1099511628211; i = i + 1 } 13 return h 14} 15func cp_read_file(path: *u8, out: *u8, cap: i64) -> i64 { 16 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } 17 var total: i64 = 0; var go: i64 = 1 18 while go == 1 { let tail: *u8 = (out as i64 + total) as *u8; let nr: i64 = sys_read(fd, tail, cap - total); if nr <= 0 { go = 0 } if nr > 0 { total = total + nr } if total >= cap { go = 0 } } 19 sys_close(fd); return total 20} 21// apply a new config; returns its generation (FNV). 0 = write failed. 22func cp_apply(path: *u8, content: *u8, n: i64) -> i64 { 23 let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 } 24 sys_write(fd, content, n); sys_close(fd) 25 return cp_fnv(content, n) 26} 27// current generation of the on-disk config (FNV of its bytes) -- the daemon polls this to hot-reload on change. 28func cp_current_gen(path: *u8) -> i64 { 29 let buf: *u8 = sys_mmap(K_MAGIC_262144); let n: i64 = cp_read_file(path, buf, K_MAGIC_262144) 30 return cp_fnv(buf, n) 31}