new version
[rrq/overlay-boot.git] / asm / reaper.asm
1 ;;; This is a "repear" program that merely waits for child processes
2 ;;; to appear and "reap" them, i.e. read their exit status to let them
3 ;;; exit. The program allows an arbitrary first argument that is
4 ;;; indended for identifying the reaper process.
5
6         format elf64 executable
7         entry main
8
9 ;;; Using mixed Data and Code segment for size squeeze
10
11         segment readable writable executable
12         
13 ;;; Main entry point
14 main:
15         ;; if (getpid() != 1) exit 1;
16         xor rax, rax
17         mov al, 39
18         ; rax=39
19         syscall
20
21         dec rax
22         jnz exit        ; exit(1)
23
24         ;; Continue here for pid 1 only
25 pid_1:
26         ;; Block all signals
27         ;; sigprocmask(SIG_BLOCK, &set, 0);
28         ; rax is already 0
29         mov rdi, rax            ; SIG_BLOCK = 0
30         lea rsi, [set]          ; *set
31         mov rdx, rax            ; 0
32         mov r10, rax            ; 0
33         mov al, 14                      ; sys_rt_sigprocmask
34         ; rax=14, rdi=0, rsi=[set], rdx=0, r10=0
35         syscall
36
37         ;; loop waiting for children, and reading their exit status
38 reaper:
39         ;; first clear status area again
40         cld
41         xor rax, rax
42         lea rdi, [status]
43         mov rdx, rdi
44         mov rcx, rax
45         mov cl, status$end-status
46         rep stosq
47         
48         ;; waitid( idtype, id, *status, options )
49         mov rdi, rax            ; idtype_t idtype = P_ALL = 0
50         mov rsi, rax            ; int id = 0
51         ;rdx = [status]         ; siginfo_t* = &status
52         mov al, 4                       ; int options = WEXITED = 4
53         mov r10, rax
54         mov al, 247                     ; waitid
55         ; rax=247, rdi=0, rsi=0, rdx=[status], r10=4
56         syscall
57
58         add rax, 10 ; -10 = ECHILD (no child processes)
59         jnz reaper
60         
61 exit:   ; exit(1)
62         xor rax, rax
63         mov rdi, rax
64         inc rdi
65         mov al, 60
66         syscall         ; exit(1) program
67
68         ;; sigset_t set -- bit mask for sigprocmask
69 set:
70         dq -1   ; all bits set
71         
72 ;;; siginfo_t status -- all ignored
73 status:
74         rept 16 { dq ? }
75 status$end: