8

I am trying to understand what happens when we log out of Linux (Ubuntu specifically):

  • How does a process initially request/notify the logging out process to perform logging out (by sending some signals, or some other IPC means)? What is the program run by the logging out process? What is usually the program run by the requesting/notifying process?

  • What processes will the logging out process kill and what not? (There are ways to make processes started after logging in survive logging out, and how do they manage to do that? Making a process not having a controlling terminal seems to be a way, but most of the processes killed by logging out in the following examples don't have a controlling terminal)

  • How does the logging out process kill those processes (by sending some signals, or some other IPC means)?

Consider three cases: virtual console and desktop environment and SSH. (The first two are provided by the OS, and the third isn't, though all are running in user space)

  1. When I log in virtual console, I get the following ancestry processes from the login shell:

    1 systemd
    721 login
    26284 bash
    

    After I log out, processes 721 login and below disappear.

  2. On Lubuntu 18.04, when I log in lightdm and LXDE, and run lxterminal from the desktop's panel and get the ancestor processes from the following command in the lxterminal window:

    $ ps -paus $$
    systemd,1 --system --deserialize 19
      `-lightdm,661
          `-lightdm,27302 --session-child 13 24
              `-lxsession,27309,testme -s Lubuntu -e LXDE
                  `-lxpanel,27399 --profile Lubuntu
                      `-lxterminal,27565
                          `-bash,27568
                              `-pstree,27594 -paus 27568
    

    When I log out, the processes lightdm,27302 and below disappear.

  3. After I ssh into Lubuntu 18.04:

    $ pstree -a -p   -s $$
    systemd,1 --system --deserialize 19
      └─sshd,669 -D
          └─sshd,22838 
              └─sshd,22979  
                  └─bash,22980
                      └─pstree,30610 -a -p -s 22980
    

    After I log out, all the processes starting from sshd,22838 to below disappear.

Thanks.

Tim
  • 101,790
  • related: https://unix.stackexchange.com/questions/490267/prevent-logoff-from-killing-tmux-session –  Dec 22 '18 at 04:10

2 Answers2

2

I don't know how systemd (especially /usr/lib/systemd/systemd-logind) is involved in OS logout process.

Following is my partial answers:

  1. The login process wait() on the login shell process, and when the login shell exits, login will receive SIGCHLD and wake up to resume its execution, which is to also exit.

  2. It seems that a logout request program such as lxsession-logout sends SIGTERM to the lxsession process (I said "seems" because I don't quite understand the source code). I don't know how lxsession responds to SIGTERM and why its descendants also die, as I can't find its disposition. I don't know why the parent lightdm,27302 of lxsession also die, and guess the parent lightdm,27302 may wait() on lxsession and die once wake up.

  3. I don't know what happens when I log out of SSH. I guess it is similar to CLI login (case 1), sshd,22979 might wait on bash,22980 and die. but I really don't know why there are so many sshd processes.

Tim
  • 101,790
-4

REMEMBER, I'M NOT GONNA TELL EVERY DETAIL, BECAUSE EVEN I DO, YOU ARE NOT ABLE TO UNDERSTAND. I ALSO KNOW YOU ARE NOT FAMINLIAR WITH EVERY CONCEPT, BUT IT'S GOOD FOR YOU TO READ MANPAGES.

How does a process initially request/notify the logging out process to perform logging out (by sending some signals, or some other IPC means)? What is the program run by the logging out process? What is usually the program run by the requesting/notifying process?

Suppose you mean "session manager" by "logging out process". By IPC with logind. /usr/lib/systemd/systemd-logind. Any program that can IPC with logind.

What processes will the logging out process kill and what not? (There are ways to make processes started after logging in survive logging out, and how do they manage to do that? Making a process not having a controlling terminal seems to be a way, but most of the processes killed by logging out in the following examples don't have a controlling terminal)

Processes inside this session would be killed, and if that's the last session of a user, and the user's not linger-enabled, the whole user slice is destroyed. By not killing it. Almost nothing to do with control-terminal, because logind doesn't use that kernel functionality.

How does the logging out process kill those processes (by sending some signals, or some other IPC means)?

Complex logic including pre-defined action in unit files and signals.

1,2,3 Looking at process tree doesn't work. logind doesn't care about that. Basically, systemd-style of services and sessions manangement is cgroup. See systemctl status.

  • Thanks. Does logging out of OS refer to logging out of a lxsession process or a systemd-login session? Is there some relation between a lxsession process and a systemd-login session? – Tim Dec 22 '18 at 05:19
  • 3
    You should answer the question, not the person. We may all greatly benefit from your deep knowledge and understanding of systemd's design and inner workings ;-) -- provided that you give some context (that not every system is running systemd), and phrase your insights in a less koan-like manner ("By IPC with login. Any program that can IPC with logind.") –  Dec 22 '18 at 05:19
  • I upvoted your reply just to make it more visible. – Tim Dec 22 '18 at 05:27
  • 3
    @Tim: That's a terrible reason to upvote a post. – palswim Dec 22 '18 at 07:38
  • The fancy blurring feature hurts my eyes. @palswim – Tim Dec 22 '18 at 13:24
  • 2
    @Tim: Not to derail the usefulness of the comments, but you can hover over the post with your mouse and it will unfade the text. – palswim Dec 22 '18 at 21:36
  • @palswim regardless, could you help with my post? – Tim Dec 22 '18 at 21:42