I am trying to wait for the process that I cloned. However, when the parent does syscall before waitid, I get -1 ECHILDwhen using strace. This is despite the fact that the clone call returns the PID for the created child, as shown here:
clone(child_stack=0x7ffe2b412d10, flags=CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWPID) = 3735
waitid(P_PID, 3735, NULL, WEXITED, NULL) = -1 ECHILD (No child processes)
If I create a loop that repeatedly calls waitid, it eventually gives the expected result of waiting for the child. This makes me believe that there is some kind of race condition when the child has not started correctly yet, but has received PID.
The following is the appropriate assembler code:
_start:
mov rax, SYS_CLONE
mov rdi, CLONE_FLAGS
mov rsi, rsp
mov rdx, 0
mov r10, 0
syscall
cmp rax, 0
je _clone
mov rdi, PPID
mov rsi, rax ; pid
mov rdx, 0
mov r10, 4 ; exited
mov rax, SYS_WAITID
syscall
mov rdi, OK_EXIT
jmp _exit
_clone:
mov rax, SYS_EXECVE
mov rdi, [rsp + 16]
lea rsi, [rsp + 16]
lea rdx, [rsp + 40]
syscall
mov rdi, rax
jmp _exit
_exit:
mov rax, SYS_EXIT
syscall
, NULL (siginfo_t *infop) waitid, , , , , . ? , , ?