8

I'm trying to properly emulate POSIX signals handling and job control for my pet operating system, but it's not clear to me what should happen to a session after the session leader exits.

I cannot find documentation related to what happens to the session and its process if, for example, a child kills the session leader while several background processes and a different foreground process are running.

My tests show that all the process in the session are killed, but how?
Do they receive a specific signal?
Is this case specified in the POSIX standard? And if so, can you provide some references?

  • Some related questions are https://unix.stackexchange.com/questions/405755/ , https://unix.stackexchange.com/questions/84737/ , https://unix.stackexchange.com/questions/18166/ , and https://unix.stackexchange.com/questions/282973/ . – JdeBP Nov 28 '17 at 10:16
  • @JdeBP interesting links, but it's still hard to say what is expected to happen if the session leader exits. Even considered that the session leader might not be a shell. – Giacomo Tesio Nov 28 '17 at 10:52

1 Answers1

10

You are not the only one puzzled by POSIX sessions; Lennart Poettering (he of systemd fame) is puzzled too.

As far as anybody can tell, when a session leader dies, init inherits the orphaned session and

  • All session member processes in the foreground process group (if any) receive a SIGHUP.

  • Session member processes who are not in the foreground group don't receive any signal.

See also:

If the terminal goes away by modem hangup, and the line was not local, then a SIGHUP is sent to the session leader. [...] When the session leader dies, a SIGHUP is sent to all processes in the foreground process group. [...] Thus, if the terminal goes away and the session leader is a job control shell, then it can handle things for its descendants, e.g. by sending them again a SIGHUP. If on the other hand the session leader is an innocent process that does not catch SIGHUP, it will die, and all foreground processes get a SIGHUP.

Andries Brower, The Linux Kernel, section 10.3 "Sessions".

AlexP
  • 10,455
  • As for "All session member processes in the foreground process group (if any) receive a SIGHUP.": do you know who send the SIGHUP? In https://unix.stackexchange.com/questions/84737/in-which-cases-is-sighup-not-sent-to-a-job-when-you-log-out it seems that bash send the signal... and this would invalidate part of your answer. – Giacomo Tesio Nov 28 '17 at 14:37
  • @GiacomoTesio: See added quotation from The Linux Kernel. – AlexP Nov 28 '17 at 16:09