code wiki / (root) / nx_svcreg.nx

nx_svcreg.nx source

↩ module page · 53 lines · 3363 B

1// nx_svcreg.nx -- DECLARATIVE SERVICE REGISTRY (systemd-unit / s6-service-dir model, sovereign): each service is 2// declared as DATA -- name / elf / port / health-path -- on the canonical nx_registry (seg_store: additive, history 3// kept, auditable). nx_runsv supervises a service BY NAME from this registry instead of hardcoded args (R5 of the 4// supervisor roadmap). Record (tab-separated): name <TAB> elf <TAB> port <TAB> health. Composes nx_registry + nx_tabrec 5// -- no new storage. license_tier: ORIGINAL 6import "nx_registry.nx" 7import "nx_tabrec.nx" 8import "nx_gate.nx" 9const SVC_MAGIC_2048: i64 = 2048 10const SVC_MAGIC_65536: i64 = 65536 11 12const SVC_KP: *u8 = "svc:" as *u8 13const SVC_IDX: *u8 = "__svcidx__" as *u8 14const SVC_PREFIX: *u8 = "knowledge/svcreg-" as *u8 15 16func svc_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8) { if a[i]!=b[i] { return 0 } i=i+1 } if b[i]!=(0 as u8) { return 0 } return 1 } 17 18func svc_register_pfx(prefix: *u8, name: *u8, elf: *u8, port: *u8, health: *u8) -> i64 { 19 let rec: *u8 = sys_mmap(SVC_MAGIC_2048); var o: i64=0 20 o=tr_cat(rec,o,name); o=tr_tab(rec,o); o=tr_cat(rec,o,elf); o=tr_tab(rec,o); o=tr_cat(rec,o,port); o=tr_tab(rec,o); o=tr_cat(rec,o,health) 21 return reg_put(prefix, SVC_KP, SVC_IDX, name, rec, o) 22} 23func svc_get_pfx(prefix: *u8, name: *u8, ptrout: *i64, lenout: *i64) -> i64 { return reg_get(prefix, SVC_KP, name, ptrout, lenout) } 24func svc_list_pfx(prefix: *u8, idxbuf: *u8, cap: i64) -> i64 { return reg_index(prefix, SVC_IDX, idxbuf, cap) } 25func svc_register(name: *u8, elf: *u8, port: *u8, health: *u8) -> i64 { return svc_register_pfx(SVC_PREFIX,name,elf,port,health) } 26func svc_get(name: *u8, ptrout: *i64, lenout: *i64) -> i64 { return svc_get_pfx(SVC_PREFIX,name,ptrout,lenout) } 27func svc_list(idxbuf: *u8, cap: i64) -> i64 { return svc_list_pfx(SVC_PREFIX,idxbuf,cap) } 28 29func main(argc: i64, argv: *i64) -> i64 { 30 if argc < 2 { gw("usage: nx_svcreg register <name> <elf> <port> <health> | get <name> | list\n" as *u8); sys_exit(2); return 2 } 31 let op: *u8 = argv[1] as *u8 32 if svc_streq(op, "register" as *u8) == 1 { 33 if argc < 6 { gw("usage: nx_svcreg register <name> <elf> <port> <health>\n" as *u8); sys_exit(2); return 2 } 34 let id: i64 = svc_register(argv[2] as *u8, argv[3] as *u8, argv[4] as *u8, argv[5] as *u8) 35 gw("nx_svcreg: registered " as *u8); gw(argv[2] as *u8); gw(" -> segid " as *u8); gn(id); gw("\n" as *u8) 36 sys_exit(0); return 0 37 } 38 if svc_streq(op, "get" as *u8) == 1 { 39 if argc < 3 { gw("usage: nx_svcreg get <name>\n" as *u8); sys_exit(2); return 2 } 40 let box: *i64 = sys_mmap(16) as *i64; let lenbox: *i64 = sys_mmap(16) as *i64 41 let found: i64 = svc_get(argv[2] as *u8, box, lenbox) 42 if found != 1 { gw("nx_svcreg: not found: " as *u8); gw(argv[2] as *u8); gw("\n" as *u8); sys_exit(1); return 1 } 43 gw("nx_svcreg: " as *u8); sys_write(1, box[0] as *u8, lenbox[0]); gw("\n" as *u8) 44 sys_exit(0); return 0 45 } 46 if svc_streq(op, "list" as *u8) == 1 { 47 let idxbuf: *u8 = sys_mmap(SVC_MAGIC_65536) 48 let n: i64 = svc_list(idxbuf, SVC_MAGIC_65536) 49 gw("nx_svcreg: registered services:\n" as *u8); sys_write(1, idxbuf, n) 50 sys_exit(0); return 0 51 } 52 gw("nx_svcreg: unknown op (register|get|list)\n" as *u8); sys_exit(2); return 2 53}