I checked the man page for waitpid, but the ERROR section indicates:
ECHILD (for waitpid() or waitid()) The process specified by pid (waitpid()) or idtype and id (waitid()) does not exist or is not a child of the calling process. (This can hap‐pen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.)
Why then does calling waitpid on a non-child process after attaching to it with the ptrace system call not produce an ECHILD error?
Other answers state that ptrace and waitpid can wait for non-child processes to exit, but do not explain why.
For example, The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004 Edition states in rationale for
wait/waitpid:Alas the specification does not provide the
ptracecall:What I understand from this is that
wait/waitpidprovide such possible functionality whose implementation is left to OS designers. For example this answer may help you to understand what “trick“ is used in Linux kernel: How does ptrace work in Linux?. Thus thewait/waitpidcan be notified of state events from attached processes but those same processes have a preserved parenthood relation.Linux man page for
ptracesays: