initial
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Sun, 20 Mar 2022 05:44:58 +0000 (16:44 +1100)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Sun, 20 Mar 2022 05:44:58 +0000 (16:44 +1100)
reaper/Makefile [new file with mode: 0644]
reaper/reaper.asm [new file with mode: 0644]

diff --git a/reaper/Makefile b/reaper/Makefile
new file mode 100644 (file)
index 0000000..87ca419
--- /dev/null
@@ -0,0 +1,6 @@
+reaper: reaper.asm
+       fasm $^ $@
+       chmod a+x $@
+
+clean:
+       rm reaper
diff --git a/reaper/reaper.asm b/reaper/reaper.asm
new file mode 100644 (file)
index 0000000..b570481
--- /dev/null
@@ -0,0 +1,69 @@
+;;; Compact subhost repear program to merely wait for children to
+;;; "reap" them, i.e. let them exit.
+
+       format elf64 executable
+       entry main
+
+       ;; A data segment
+       segment readable writable
+
+       ;; sigset_t set -- bit mask for sigprocmask
+set:   dq -1   ; all bits set
+       
+;;; siginfo_t status -- all ignored
+status:        rept 16 { dq 0 }
+
+status$end:
+       
+;;; A code segment
+       segment readable executable
+       
+;;; Main entry point
+main:
+       ;; if (getpid() != 1) exit 1;
+       mov rax, qword 39
+       syscall
+       cmp rax, 1
+       je pid_1
+       
+       ;; exit(1)
+       mov rdi, qword 1
+       mov rax, qword 60
+        syscall                ; exit program
+
+       ;; Continue here for pid 1 only
+pid_1:
+       ;; Block all signals
+       ;; sigprocmask(SIG_BLOCK, &set, 0);
+       xor rdi, rdi            ; SIG_BLOCK = 0
+       lea rsi, [set]          ; *set
+       xor rdx, rdx            ; 0
+       xor r10, r10            ; 0
+       mov rax, qword 14       ; sys_rt_sigprocmask
+       syscall
+
+       ;; loop waiting for children, and reading their exit status
+reaper:
+       ;; first clear status area again
+       cld
+       xor rax, rax
+       lea rsi, [status$end]
+       lea rdi, [status]
+clear: stosq
+       cmp rdi,rsi
+       jl clear
+       
+       ;; waitid( idtype, id, *status, options )
+       xor rdi, rdi      ;  idtype_t idtype = P_ALL = 0
+       xor rsi, rsi      ; int id = 0
+       lea rdx, [status] ; siginfo_t* = &status
+       mov r10, 4        ; int options = WEXITED = 4
+       mov rax, 247      ; waitid
+       syscall
+
+       cmp rax,-10       ; -10 = ECHILD (no child processes)
+       jne reaper
+
+       mov rdi, rax
+       mov rax, qword 60
+        syscall        ; exit program