nx_dmn_research_fetch.nx source
↩ module page · 77 lines · 5261 B
1// nx_dmn_research_fetch.nx -- SOVEREIGN multi-source web-fetch to DETERMINE BEST PRACTICES for running the NISHI
2// PUBLISHER as a CONTINUOUS daemon (operator: "we want the publisher daemon to run as we add things, not start/stop
3// or whatever is best practices; use the s class nishi researcher to determine"). Mirrors nx_pub_research_fetch:
4// own TLS-1.3 (nx_https_fetch_follow) + Mozilla CA, saves each source to knowledge/fetched/dmn_*.raw so the design
5// CITES real sources (>=2-source no-hearsay rule), 100% sovereign (no curl/gcc). Idempotent have-skip.
6// Axes: (A) the DAEMON role, (B) SUPERVISION (run-not-manual; restart policy; self-heal), (C) EVENT-DRIVEN drain
7// (act as things arrive, not poll), (D) LIVENESS/WATCHDOG (detect hangs not just crashes), (E) GRACEFUL signals
8// (zero-downtime reload/stop as we add). expect_exit: 0 license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_x509_trust_store.nx"
11import "nx_trust_store_load_from_certdata.nx"
12import "nx_https_fetch_follow.nx"
13const K_MAGIC_4194304: i64 = 4194304
14const K_MAGIC_8388608: i64 = 8388608
15
16func gf_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17func gf_putn(v: i64) -> i64 {
18 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
19 var m: i64 = v
20 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
21 let d: *u8 = sys_mmap(24); var k: i64 = 0
22 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
23 var j: i64 = k - 1
24 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
25 return 0
26}
27func have_file(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
28func fetch_save(url: *u8, opath: *u8, store: *TrustStore, out: *u8, cap: i64) -> i64 {
29 if have_file(opath) == 1 { gf_puts(opath); gf_puts(" [have-skip]\n"); return 1 }
30 let status: *i64 = sys_mmap(8) as *i64
31 let n: i64 = nx_https_fetch_follow(url, store, out, cap, 6, status)
32 gf_puts(url); gf_puts(" status="); gf_putn(status[0]); gf_puts(" bytes="); gf_putn(n)
33 if n <= 0 { gf_puts(" FETCH-FAIL\n"); return 0 }
34 var gz: i64 = 0
35 if n >= 2 { if out[0] == 0x1f as u8 { if out[1] == 0x8b as u8 { gz = 1 } } }
36 if gz == 1 { gf_puts(" [GZIP-skip]\n"); return 0 }
37 let fd: i64 = sys_openat_wr(opath, 0x1a4)
38 if fd < 0 { gf_puts(" SAVE-FAIL\n"); return 0 }
39 sys_write(fd, out, n); sys_close(fd); gf_puts(" SAVED\n")
40 return 1
41}
42
43func main() -> i64 {
44 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
45 if r <= 0 { gf_puts("DMN: certdata load failed\n"); return 1 }
46 let store: *TrustStore = r as *TrustStore
47 gf_puts("CA roots="); gf_putn(trust_store_count(store)); gf_puts("\n")
48 let cap: i64 = K_MAGIC_8388608
49 let out: *u8 = sys_mmap(cap)
50 var ok: i64 = 0
51
52 gf_puts("== A. THE DAEMON ROLE (long-lived service; how a service should run) ==\n")
53 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Daemon_(computing)" as *u8, "knowledge/fetched/dmn_a_daemon.raw" as *u8, store, out, cap)
54 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Systemd" as *u8, "knowledge/fetched/dmn_a_systemd.raw" as *u8, store, out, cap)
55 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Init" as *u8, "knowledge/fetched/dmn_a_init.raw" as *u8, store, out, cap)
56
57 gf_puts("== B. SUPERVISION (run-not-manual; restart policy; self-heal) ==\n")
58 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Runit" as *u8, "knowledge/fetched/dmn_b_runit.raw" as *u8, store, out, cap)
59 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Crash-only_software" as *u8, "knowledge/fetched/dmn_b_crashonly.raw" as *u8, store, out, cap)
60 ok = ok + fetch_save("https://en.wikipedia.org/wiki/High_availability" as *u8, "knowledge/fetched/dmn_b_ha.raw" as *u8, store, out, cap)
61 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Fault_tolerance" as *u8, "knowledge/fetched/dmn_b_faulttolerance.raw" as *u8, store, out, cap)
62
63 gf_puts("== C. EVENT-DRIVEN (act as things ARRIVE, not fixed polling) ==\n")
64 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Inotify" as *u8, "knowledge/fetched/dmn_c_inotify.raw" as *u8, store, out, cap)
65 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Polling_(computer_science)" as *u8, "knowledge/fetched/dmn_c_polling.raw" as *u8, store, out, cap)
66
67 gf_puts("== D. LIVENESS / WATCHDOG (detect a HANG, not just a crash = the wedge) ==\n")
68 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Watchdog_timer" as *u8, "knowledge/fetched/dmn_d_watchdog.raw" as *u8, store, out, cap)
69 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Heartbeat_(computing)" as *u8, "knowledge/fetched/dmn_d_heartbeat.raw" as *u8, store, out, cap)
70
71 gf_puts("== E. GRACEFUL signals (clean reload/stop; zero-downtime as we add) ==\n")
72 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Unix_signal" as *u8, "knowledge/fetched/dmn_e_signal.raw" as *u8, store, out, cap)
73 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Graceful_exit" as *u8, "knowledge/fetched/dmn_e_graceful.raw" as *u8, store, out, cap)
74
75 gf_puts("READABLE SOURCES SAVED: "); gf_putn(ok); gf_puts(" / 13\n")
76 return 0
77}