nx_servicectl.nx source
↩ module page · 23 lines · 1192 B
1// nx_servicectl.nx -- generic sovereign service restart: kill ANY daemon by full /proc cmdline, then respawn it
2// DETACHED -- not just the NAS-known services nx_hostctl restarts. Composes the canonical nx_proc_ctl (kill) +
3// nx_daemon (detached spawn). Usage: nx_servicectl <cmdline-name> <elf-path>. No pkill, no harness backgrounding.
4// license_tier: ORIGINAL
5import "nx_proc_ctl.nx"
6import "nx_daemon.nx"
7import "nx_gate.nx"
8
9func main(argc: i64, argv: *i64) -> i64 {
10 if argc < 3 { gw("usage: nx_servicectl <cmdline-name> <elf-path>\n" as *u8); sys_exit(1); return 1 }
11 let name: *u8 = argv[1] as *u8
12 let elf: *u8 = argv[2] as *u8
13
14 let killed: i64 = nx_pctl_kill_by_cmdline(name, 15) // SIGTERM the old instance(s) by full cmdline
15 let av: *i64 = sys_mmap(16) as *i64
16 daemon_argv0(elf, av)
17 let pid: i64 = daemon_spawn(elf, av) // respawn detached (new session)
18
19 gw("nx_servicectl: restart /" as *u8); gw(name); gw(" -> killed=" as *u8); gn(killed)
20 gw(", respawned " as *u8); gw(elf); gw(" pid=" as *u8); gn(pid); gw("\n" as *u8)
21 if pid <= 0 { gw(" RESPAWN FAILED\n" as *u8); sys_exit(2); return 2 }
22 sys_exit(0); return 0
23}