gmain: Use waitid() on pidfds rather than a global SIGCHLD handler

When the system supports it (as all Linux kernels ≥ 5.3 should), it’s
preferable to use `pidfd_open()` and `waitid()` to be notified of
child processes exiting or being signalled, rather than installing a
default `SIGCHLD` handler.

A default `SIGCHLD` handler is global, and can never interact well with
other code (from the application or other libraries) which also wants to
install a `SIGCHLD` handler.

This use of `pidfd_open()` is racy (the PID may be reused between
`g_child_watch_source_new()` being called and `pidfd_open()` being
called), so it doesn’t improve behaviour there. For that, we’d need
continuous use of pidfds throughout GLib, from fork/spawn time until
here. See #1866 for that.

Signed-off-by: Philip Withnall <>

Helps: #1866
Fixes: #2216
13 jobs for wip/pwithnall/2216-pidfd-sigchld in 9 minutes and 18 seconds (queued for 4 seconds)