You are not logged in.
I have a small problem... my code launches a child process via fork() and execvp(), and that child process (which I don't have control over the source code to) may or may not launch some of its own child processes, and those might or might not launch their own child processes, and so on.
The problem is this: in the event that my code's child process is killed (more or less violently, via SIGHUP/SIGINT/SIGTERM/SIGQUIT/whatever), I want to guarantee that none of the processes it spawned (directly or indirectly) are left hanging around.
Is there a way to ensure this? I don't think that just installing signal handlers would be sufficient, since (a) I would need access to the source code of the child processes to do that, and (b) there's no guarantee that they would get called in all cases, since e.g. SIGKILL cannot be handled by non-init processes.
Do I need to declare a "process group" to do this? If so, how does that work?
If a process's parent gets killed, the process will be adopted by init... So, if you're
just worried about them zombifying later when they exit, no need to worry, because
init will reap them... However, if you want to make sure the children die when their
parent dies, then yes you want a process group/session, and make their parent (your
direct child) the pgroup leader... In that case, when the pgroup leader dies, all others
in the pgroup will get hit with SIGHUP... Of course, if you have no control over the
code of those (grand)children, you have no way to make sure they don't block/ignore
SIGHUP, so that's really no guarantee of taking them down when the leader goes...
But, I think it's about the best you can really do...
Anyway, you want to just do a setsid() in your child before exec*()'ing... That'll give
you a brand new session and process group, with you being the leader... That'll be
inhereted by the exec*()'d process, so it'll become leader of the pgroup...
Thanks Rob, calling setsid() in the child process before calling execvp() did the trick. Well, that plus killing the child process with kill(-pid) instead of kill(pid), so that it's the entire process group that gets killed instead of just that process... in any case, it's working now and I'm happy :^)