Debian – What Happens When ‘Shut Down’ is Pressed in GUI?

debiangnomegnome3polkitshutdown

I have been banging my head against this for quite a while now. It's related to this question. I would like to find out exactly what happens when I choose to shut down my Linux box from the GUI. This seems to be poorly (if at all) documented.

Ideally, I'm hoping for a DE- and OS-agnostic answer. Barring that, I'm interested in the specific case of Mandriva 2010.1 and Debian 6.x (Squeeze) and 7.0 (Wheezy) all running Gnome. (If you're paying close attention, yes that's Gnome 2 and Gnome 3)

Basically, I would like to know which command/script/sequence of scripts is started when I press "Shut Down" or "Restart" so I can modify their behavior. Some forum posts I looked at suggest hacking /etc/polkit-1/* but this directory structure is only a skeleton on my Debian (Squeeze) box, for example.

Can anyone help?


EDIT

What I have tried so far

  • Replaced the shutdown executable with a script of my own. This doesn't work: when I press shutdown Gnome logs out without executing my script.
  • Tried editing the Gnome 2 menu. No joy: the "Shutdown", "Log out" and "Lock Screen" options do not appear in the menu editor.
  • Looked at /usr/share/menu, nothing helpful there.

Possible avenues for the solution

  • straceing the GUI options (is this even possible?)
  • Looking at shutdown's source code
  • Looking at gnome-session's source code

Update

As per my comments on the answer below, I have looked into polkit actions under /usr/share/polkit-1/actions/ and found (in the file org.freedesktop.consolekit.policy) an action called org.freedesktop.consolekit.system.stop-multiple-users that throws the message

System policy prevents stopping the system when other users are logged in

I'm thinking (due to the org.freedesktop.* naming convention) that this is some kind of signal sent to the DM via D-BUS. Moreover, this message appears when trying to shutdown graphically while other users are logged in, so the mechanism that triggers it must be the same mechanism triggered when "Shut Down" or "Power Off" is selected from the GUI.

Can anyone confirm/refute? Is there a possibility of somehow intercepting this signal or modifying it?

Best Answer

It depends on your Display Manager! (i.e. KDM, GDM)

Please bear in mind your DM runs as root! (it needs root privileges in order to run your session process as the user you log in)

When you click shutdown in KDE or GNOME, your DE sends a signal to your DM to power off or restart after the session has terminated. Then, your DE tells every program to end and once all other process has terminated (or expired a timeout), the last process of your DE -- the session process -- terminates.

The session process is the first process started in an X11 session. When it's killed or it terminates, the session terminates. Have you ever seen that xterm when running X without DE? That is a session process. This process is called kdeinit in KDE and gnome-session in GNOME.

Once the session has terminated, control is returned to your DM (which has been waiting for the X process to end), and it checks what the DE told him to do. If it told it to power off or restart, it will do that. In other case, it will just start a new login screen in X.

This is also related with problems you may have had in the past, with some DE not being able to power off or restart, just to log out, when used in combination with some other DMs.

In any case, this is not so bad documented. GDM has a manual page "gdm-control(1)" of a command which allows you to tell it to shut down just like I told before (gdm-control). KDM has excellent documentation too and has a similar (a little more complex) utility named kdmctl.


Shutdown and restart is possible without PolicyKit, but PolicyKit serves many purposes needed on nowadays systems like mounting disks without being root, suspend or hibernate the computer. And it neither is bad documented!

Check out this if you want to know more about what is PolicyKit and how does it work: http://www.freedesktop.org/software/polkit/docs/latest/polkit.8.html

Related Question