GSubprocess: add API to reliably kill descendant sub-process tree
@chergert
Submitted by Christian Hergert Link to original bug (#762164)
Description
When spawning a process using GSubprocess or GSubprocessLauncher, it would be convenient if we could reliably kill all of the descendant processes. One example would be to spawn make', and then call g_subprocess_force_exit() on the make subprocess. It will not actually stop the
make' processes running, because they are all descendants of the make that was spawned.
One way to work around this, at least on Linux (and probably BSD), is to call setsid(3p)
in the child setup function, and then kill the process tree with kill(-pid, SIGKILL). However there are side effects to this, like the children continuing to run when the parent exits. Even prctl for PR_SET_PDEATHSIG isn't enough here as it would only kill the session leader.
So, to avoid having to discover the "right" way to do this in every application, I think it makes sense to add API for this to GSubprocessLauncher.
On Linux, it might use cgroups. On BSD, we might be stuck with setsid() and kill(-pid, SIGKILL), and who knows on Windows.
But I'd love some feedback and possible API suggestions. I'd be willing to implement it if need be.