title: Waiting for cloned child in assembly with waitid syscall
body: <p>I am trying to wait for a process I have cloned. However, when the parent makes the syscall to <code>waitid</code> I get an <code>-1 ECHILD</code> when using strace. This despite the clone call returning a PID for the created child as seen here:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>If I make a loop that repeatedly calls <code>waitid</code> it eventually gives the expected result of waiting for the child. That leads me to believe that there's some race condition where the child hasn't started properly yet, but has been given a PID. </p>
  
  <p>Below is the relevant assembly code:</p>
  
  <pre><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
  </code></pre>
  
  <p>Note that I'm passing NULL as the third (<code>siginfo_t *infop</code>) argument to <code>waitid</code> and I suspect that I need to setup that struct properly to make everything work, but I haven't found any examples on how to do that in assembly. How can I do that? Or am I mistaken and I just have to resort to the looping workaround I mentioned?</p>