What mechanism prevents shutdown when another open session exists?

The system refuses to shut down or reboot while another (GUI) session is open. It simply logs me out, when I select shut down or restart from the system menu.

How is that accomplished?

I am not asking for a rationale (it is quite clear that shutting down might have adverse impacts on the work of another user) but for the mechanism behind this behavior.


Solution 1:

When you click the shutdown button (in Unity):

  1. The SessionView asks the SessionManager if the user is authorized to shutdown.

  2. The SessionManager uses systemd-logind to check /org/freedesktop/ConsoleKit/Manager for the authorization.

  3. ConsoleKit sets that property essentially by checking if multiple users are logged in. If multiple users are logged in, it denies authorization. In addition, depending on how the system has been set up, ConsoleKit may also use polkit or role-based access control to determine authorization.

  4. If SessionView does not receive authorization for the user to shutdown, it changes the action mode from shutdown to logout.

As an aside, note that this behavior (and how to implement it) is a decision made by Ubuntu. I had recently installed Arch with Gnome 3.8 to a partition, so logged in two users there to see how what happened when one of them shutdown. There was a message warning that other users were logged in and might lose work, but clicking OK let the shutdown continue.

[Edit based on OP comment]

The simplest way to change this behavior is to bypass it. In a terminal or a script, issue:

sudo shutdown -h 0 for immediate shutdown or sudo shutdown -r 0 for a restart.

This assumes the user who wants to shutdown has administrative privileges, but the user could also be granted admin privilege only for shutdown.

With a script you cold add in some of niceties like warning about other logged in users and allowing the user to change their mind about shutting down.

Someone else may have another idea (or could likely provide a fleshed-out script in response to a new question), but I can't think of anything easy (say, without recompiling a custom version of Unity) or safe beyond this solution.