Is there the possibility to set a parent process when creating a new process

processprocess-management

I'd like to set the parent of a newly started process, is that possible?

Example, let us assume that we start a new desktop environment session via a login manager, so our process tree would look something like this:

init
 \- login-manager
     \- de-session

Now I do have a script to launch my most essential applications which should start with the session, for various reasons I'd like to keep these as a script and not migrate them to the autostart manager of any DE. It looks something like this:

#!/usr/bin/env

application1 &
application2 &
application3 &

After running this automatically at the start of the session, our process tree looks like this:

init
 |- application1
 |- application2
 |- application3
 \- login-manager
     \- de-session

But what I'd actually like is to "reparent" these processes under the session, like this:

init
 \- login-manager
     \- de-session
         |- application1
         |- application2
         \- application3

So, is there any way to "reparent" a process under another one?

Best Answer

On a few systems, you can mark a process as a child subreaper, which makes it take init's role of adopting orphan processes for all its descendants.

On Linux, that's done with the PR_SET_CHILD_SUBREAPER prctl().

So, you could start your de-session as (here hardcoding the value of PR_SET_CHILD_SUBREAPER for Linux):

perl -e 'require "syscall.ph";
         syscall(&SYS_prctl,36,1) >= 0 or die "cannot set subreaper: $!";
         exec("de-session");'

But de-session might get confused when it receives SIGCHLD signals for processes it has never spawned. Your init has been designed to deal with those, but probably not your de-session, so you may find that you get an army of zombies as de-session never acknowledges the death of those processes it never wanted to inherit.

Related Question