0

I have a parent and a child process where the child is

  1. spawned (via clone) by a short lived thread of the parent and
  2. lives in a PID namespace.

What I want to achieve is that the child process is terminated if the parent process is somehow terminated. I understand from this answer that I can set the parent-death signal from the child like so:

prctl(PR_SET_PDEATHSIG, SIGHUP);

However, this already triggers when the parent's thread from which the child was spawned terminates. What I need is a mechanism that only triggers when the parent process terminates.

I tried using waitpid from the child to monitor the parent process but since it is spawned in a PID namespace, the real PID of the parent is not accessible.

inorik
  • 147
  • 9
  • sooo `atexit(your_callback_to_kill_child)`? – KamilCuk Jun 17 '21 at 15:13
  • I’d like to catch all cases in which the parent terminates. Including crashes and other unusual scenarios. – inorik Jun 17 '21 at 16:20
  • `pidfd_open(ppid())` and `poll()` on it? – KamilCuk Jun 17 '21 at 16:36
  • The problem is that the child is executed in a PID namespace. Meaning `getppid()` just returns 0. – inorik Jun 18 '21 at 10:57
  • Well, then I believe you have to just pass to the child process the pid of parent. `clone()` has that `void *arg` argument. – KamilCuk Jun 18 '21 at 11:15
  • Or create a pipe from parent to child and poll the read-end in the child. The parent's end wil be closed when the process terminates resulting in an EOF condition for the child. – Bodo Jun 18 '21 at 14:50
  • Passing the parent PID will not work (PID namespace restriction) but I am looking into the pipe idea. – inorik Jun 21 '21 at 15:37

0 Answers0