code wiki / _hdl_build / nx_synced_push.nx
nx_synced_push.nx source
↩ module page · 53 lines · 2969 B
1// nx_synced_push.nx -- S-CLASS LIVE-SYNC: the ARBITER-WRAPPED NAS pusher. Every push to the ONE shared NAS
2// doc-root goes through a single kernel-enforced "nas_push" lease (nx_arbiter), so concurrent sibling
3// workstreams SERIALIZE on the vault->ssh chain instead of racing it -- the direct fix for the intermittent
4// "live-sync bullshit" (two siblings pushing at once corrupted each other's secret/staging). Thin composition
5// of two PROVEN pieces: nx_arbiter (the lease, race-gate-proven 380->0 lost updates) + the EXACT fork/exec
6// idiom nx_aw_push itself uses to spawn its vault children (p_run). Same argv contract as nx_aw_push:
7// argv[1] = src-spec file (one line: the local payload path) argv[2] = dst-cmd file (the remote write cmd)
8// CWD must be nxc2 (nx_aw_push forks _offc/*.elf by RELATIVE path). license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
11import "nx_arbiter.nx"
12
13const SP_PUSH_ELF: *u8 = "_offc/nx_aw_push.elf"
14const SP_RESOURCE: *u8 = "nas_push"
15
16// fork/exec nx_aw_push under a HELD lease, then release. returns nx_aw_push's exit code, or -1 if the lease
17// can't be taken within the bounded wait. The lease makes concurrent sibling pushes wait their turn instead
18// of shredding /tmp/nxpass + /tmp/nxsecret.out mid-handshake.
19func sp_push(src_spec: *u8, dst_cmd: *u8) -> i64 {
20 let lfd: i64 = fl_acquire(SP_RESOURCE, 600, 100) // up to ~60s for the shared NAS lease (graceful, bounded -- #14)
21 if lfd < 0 { return 0 - 1 }
22 let pid: i64 = sys_fork()
23 if pid == 0 {
24 let argv: *i64 = sys_mmap(64) as *i64
25 argv[0] = SP_PUSH_ELF as i64
26 argv[1] = src_spec as i64
27 argv[2] = dst_cmd as i64
28 argv[3] = 0
29 let envp: *i64 = sys_mmap(16) as *i64
30 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
31 sys_execve(SP_PUSH_ELF, argv, envp)
32 sys_exit(127)
33 }
34 let st: *i64 = sys_mmap(16) as *i64
35 sys_wait4(pid, st, 0)
36 fl_release(lfd)
37 return (st[0] >> 8) & 0xff
38}
39
40func sp_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
41// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
42// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
43// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
44// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
45func sp_n(v: i64) -> i64 { nxi_out(v); return 0 }
46
47func main(argc: i64, argv: *i64) -> i64 {
48 if argc < 3 { sp_w("nx_synced_push: need <src-spec-file> <dst-cmd-file>\n"); sys_exit(2); return 2 }
49 sp_w("[synced_push] acquiring nas_push lease (serializes vs sibling pushers)...\n")
50 let rc: i64 = sp_push(argv[1] as *u8, argv[2] as *u8)
51 sp_w("[synced_push] released lease; nx_aw_push exit code = "); sp_n(rc); sp_w("\n")
52 sys_exit(rc); return rc
53}