tags:

views:

83

answers:

2

Is it possible to change a process parent?
ex: parent A has Child B can I make the parent of B is the Init process without killing A?

+5  A: 

Not from outside of process B.

From inside process B, you can call fork which makes a copy of your process, then let the original exit. When that happens the new copy B2 will not be a child of A, its parent will be set to 1 (the init process).

Ben Voigt
+3  A: 

Calling ptrace(PTRACE_ATTACH, pid, x, y) where pid is the pid of B (in your example) and x and y don't matter (probably set them to NULL) will make the calling process the parent of B for many (but not all) purposes (with restrictions based on user ID of the processes, of course, to keep you from taking over someone else's processes unless you are root).

After calling ptrace(PTRACE_ATTACH, the child will still get either its original parent or init's pid as its parent pid from getppid(), but the tracing process will be able to call wait and get SIGCHLD from process B.

There is a lot of stuff going on here, so you should read man 2 ptrace and make sure you understand the details pretty well.

nategoose