Session manager на linux

xsm(1) — Linux man page

Synopsis

Description

xsm is a session manager. A session is a group of applications, each of which has a particular state. xsm allows you to create arbitrary sessions — for example, you might have a «light» session, a «development» session, or an «xterminal» session. Each session can have its own set of applications. Within a session, you can perform a «checkpoint» to save application state, or a «shutdown» to save state and exit the session. When you log back in to the system, you can load a specific session, and you can delete sessions you no longer want to keep.

Some session managers simply allow you to manually specify a list of applications to be started in a session. xsm is more powerful because it lets you run applications and have them automatically become part of the session. On a simple level, xsm is useful because it gives you this ability to easily define which applications are in a session. The true power of xsm, however, can be taken advantage of when more and more applications learn to save and restore their state.

Options

Setup

.xsession file

The last program executed by your .xsession file should be xsm. With this configuration, when the user chooses to shut down the session using xsm, the session will truly be over.

Since the goal of the session manager is to restart clients when logging into a session, your .xsession file, in general, should not directly start up applications. Rather, the applications should be started within a session. When xsm shuts down the session, xsm will know to restart these applications. Note however that there are some types of applications that are not «session aware». xsm allows you to manually add these applications to your session (see the section titled Client List).

SM_SAVE_DIR environment variable

Default Startup Applications

Each line in the startup file should contain a command to start an application. A sample startup file might look this:

Starting a Session

The session menu

The following operations can be performed from the session menu: Load Session Pressing this button will load the currently selected session. Alternatively, hitting the Return key will also load the currently selected session, or the user can double click a session from the list. Delete Session This operation will delete the currently selected session, along with all of the application checkpoint files associated with the session. After pressing this button, the user will be asked to press the button a second time in order to confirm the operation. Default/Fail Safe xsm will start up a set of default applications (as described above in the section titled Default Startup Applications). This is useful when the user wants to start a fresh session, or if the session configuration files were corrupted and the user wants a «fail safe» session. Cancel Pressing this button will cause xsm to exit. It can also be used to cancel a «Delete Session» operation.

Controlling a Session

The following options are available from xsm‘s main window: Client List Pressing this button brings up a window containing a list of all clients that are in the current session. For each client, the host machine that the client is running on is presented. As clients are added and removed from the session, this list is updated to reflect the changes. The user is able to control how these clients are restarted (see below).

By pressing the View Properties button, the user can view the session management properties associated with the currently selected client.

By pressing the Clone button, the user can start a copy of the selected application.

By pressing the Kill Client button, the user can remove a client from the session.

By selecting a restart hint from the Restart Hint menu, the user can control the restarting of a client. The following hints are available:

The Restart If Running hint indicates that the client should be restarted in the next session if it is connected to the session manager at the end of the current session.

The Restart Anyway hint indicates that the client should be restarted in the next session even if it exits before the current session is terminated.

The Restart Immediately hint is similar to the Restart Anyway hint, but in addition, the client is meant to run continuously. If the client exits, the session manager will try to restart it in the current session.

Читайте также:  Wordpress как вывести рубрику на страницу wordpress

The Restart Never hint indicates that the client should not be restarted in the next session.

Note that all X applications may not be «session aware». Applications that are not session aware are ones that do not support the X Session Management Protocol or they can not be detected by the Session Management Proxy (see the section titled THE PROXY). xsm allows the user to manually add such applications to the session. The bottom of the Client List window contains a text entry field in which application commands can be typed in. Each command should go on its own line. This information will be saved with the session at checkpoint or shutdown time. When the session is restarted, xsm will restart these applications in addition to the regular «session aware» applications.

Pressing the Done button removes the Client List window. Session Log. The Session Log window presents useful information about the session. For example, when a session is restarted, all of the restart commands will be displayed in the log window. Checkpoint By performing a checkpoint, all applications that are in the session are asked to save their state. Not every application will save its complete state, but at a minimum, the session manager is guaranteed that it will receive the command required to restart the application (along with all command line options). A window manager participating in the session should guarantee that the applications will come back up with the same window configurations.

If the session being checkpointed was never assigned a name, the user will be required to specify a session name. Otherwise, the user can perform the checkpoint using the current session name, or a new session name can be specified. If the session name specified already exists, the user will be given the opportunity to specify a different name or to overwrite the already existing session. Note that a session which is locked can not be overwritten.

When performing a checkpoint, the user must specify a Save Type which informs the applications in the session how much state they should save.

The Local type indicates that the application should save enough information to restore the state as seen by the user. It should not affect the state as seen by other users. For example, an editor would create a temporary file containing the contents of its editing buffer, the location of the cursor, etc.

The Global type indicates that the application should commit all of its data to permanent, globally accessible storage. For example, the editor would simply save the edited file.

The Both type indicates that the application should do both of these. For example, the editor would save the edited file, then create a temporary file with information such as the location of the cursor, etc.

In addition to the Save Type, the user must specify an Interact Style.

The None type indicates that the application should not interact with the user while saving state.

The Errors type indicates that the application may interact with the user only if an error condition arises.

The Any type indicates that the application may interact with the user for any purpose. Note that xsm will only allow one application to interact with the user at a time.

After the checkpoint is completed, xsm will, if necessary, display a window containing the list of applications which did not report a successful save of state. Shutdown A shutdown provides all of the options found in a checkpoint, but in addition, can cause the session to exit. Note that if the interaction style is Errors or Any, the user may cancel the shutdown. The user may also cancel the shutdown if any of the applications report an unsuccessful save of state.

The user may choose to shutdown the session with our without performing a checkpoint.

HOW XSM RESPONDS TO SIGNALS

xsm will respond to a SIGUSR1 signal by performing a checkpoint with the following options: no interaction, save type local. This signal can be used to perform a remote checkpoint of a session.

the Proxy

— The application maps a top level window containing the WM_CLIENT_LEADER property. This property provides a pointer to the client leader window which contains the WM_CLASS, WM_NAME, WM_COMMAND, and WM_CLIENT_MACHINE properties.

— The application maps a top level window which does not contain the WM_CLIENT_LEADER property. However, this top level window contains the WM_CLASS, WM_NAME, WM_COMMAND, and WM_CLIENT_MACHINE properties.

An application that support the WM_SAVE_YOURSELF protocol will receive a WM_SAVE_YOURSELF client message each time the session manager issues a checkpoint or shutdown. This allows the application to save state. If an application does not support the WM_SAVE_YOURSELF protocol, then the proxy will provide enough information to the session manager to restart the application (using WM_COMMAND), but no state will be restored.

Читайте также:  Temp file limit postgresql

Источник

Ponyhof

Dysfunctional Programming

Session-Management on Linux

One thing I always admired on linux is fast session-switching. A simple key-press (ctrl+alt+Fx) and I can ditch my current session to switch to another one. While in principle session-interaction is fairly simple, you can easily get it wrong (as can be seen with XMir). But how exactly does session management work today? And how did systemd-logind change this situation?

First, we need to define a session. A session is a group of processes running under control of a single user. Only a single session is active on a system (more precisely on a seat; discussed below) and a user can only interact with the active (or foreground) session. A session can start and stop processes, but it cannot escape from the session. In other words, all started processes will always belong to the session they were started in. Only system-daemons (which are not attached to a session) can spawn new sessions.

A session usually has different user-daemons which provide services for the session. For instance, the X-Server provides graphics access to other session-processes, pulseaudio provides audio-access. Then there is a central process which controls and monitors a session. Whenever a session is spawned, this process is the first process that is created. It initializes the session, spawns other management processes and optionally restarts them if they crash. It might also exit once the session was initialized if no process-monitoring is required. An example would be gnome-session.

1) Session Management

Historically, every process on a system has a session-id which defines the session a process belongs to. The setsid() system call allows to create a new session. However, with systemd-logind a new more-versatile concept was introduced. To understand this, we need to first look at seats .

Seats

Multi-seat setup (source: Wikipedia)

On normal systems only a single seat exists (called seat0). But more seats can be created on request. A seat is a virtual object that describe a physical interface to a system. The combination of a monitor plus keyboard and mouse form a usual seat. One user can interact with the system through this seat. If you attach another monitor plus keyboard and mouse, another user can interact with the system in parallel via this new seat (eg., called seat1). It is important to note that these seats are independent of each other. The pictures you get on the monitors are different and keyboard input from one seat doesn’t affect the other.

Any device connected to your system can be attached to a seat, but only to one seat at a time! If a device is attached to seat, it cannot be used by processes running on another seat. Obviously, input devices, sound-devices and monitors are usually attached to a seat. Network-interfaces, for example, can be left unattached so all seats can share an internet-connection. It is up to the system-administrator to decide which devices to attach to which seat, or whether to leave them as global devices that can be shared.

Bootup

During system-boot, systemd starts several daemons to manage a system. All these daemons are global and not attached to a seat or session. This also means, these daemons must not access devices attached to a seat. Among these daemons is also systemd-logind, which is responsible for session and seat management.

To allow user interaction, systemd needs to automatically spawn a new session for each seat. This is usually a login-session (like gdm, kdm or LightDM) but might also be configured as auto-login and spawn directly a user-session like gnome-session. In case of a login-session, the greeter draws login and password fields and after verifying the input, it instructs systemd to spawn a user-session. Note that the greeter cannot start the user-session itself as it already runs in a session (you cannot escape your session, remember?).

But what exactly does systemd do to control sessions?

Sessions

Via kernel interfaces (namely cgroups) systemd can reliably track processes and their childs. Whenever systemd is instructed to start a new session, it first verifies the caller is allowed to do that and then it spawns the first process for the session. Internally, it attaches a new session-id to this process, saves the seat this session is run on and some other maintenance data. Whenever this session process creates new processes, systemd can use cgroups to track these. So it always knows which session a process belongs to.

The first process for a session is responsible of initializing the session. In case of gnome-session, it spawns the X-Server, some gnome-specific maintenance daemons and then monitors the session in case something goes wrong. You can now interact with this session (normally through the X-Server) and do your daily work.

Читайте также:  Canon mf4700 картридж совместимый

A session can explicitly instruct systemd to close itself. However, this will only mark it as dead. Once all processes of a session exited, the session is automatically cleaned up and removed.

2) Case-Study: Text-mode sessions

Text-mode interface (source: Wikipedia)

As a concrete non-trivial scenario, I’ll explain how text-mode sessions integrate with this. First, we need to know that virtual-terminals are basically a combination of input devices and a monitor. They can be accessed via /dev/tty (where is between 1 and 63; the other /dev/ttyXY devices are no VTs!). If you read from them, you get input from connected keyboards, if you write to them, the kernel displays it as text on the monitor. Some rather complex control-sequences are available to do colors or more, but that’s beyond the scope. VTs are always bound to seat0. So only sessions on this seat can use VTs (which means, classic text-mode is only available on seat0).

Text-mode sessions are special in that they are not spawned by a login-session. systemd-logind spawns /bin/agetty right during boot for each VT (you can configure how many of the 63 possible should be started). agetty is running as system daemon and not in a session! However, it is bound to seat0, obviously. agetty initializes the VT and runs /bin/login. login then writes a greeter to the VT and reads username plus password from it. As you might notice, this is problematic as it accesses devices attached to a seat even though it’s not running in a session. But due to the design of VTs, this cannot be easily avoided.

Once login verified the user-input, it instructs systemd to start the given user-session in text-mode. It looks up the initial process in /etc/passwd (let’s assume it is /bin/bash) and runs it. So bash is the controlling process in this session. It also substitutes the X-Server in that it provides user-interaction (in text mode rather than graphics). It allows to switch between different processes (ctrl+Z, fg, bg and jobs) and allows starting other session-daemons during initialization (via .bashrc).

So while text-mode feels a lot different, it is in fact very similar to graphics mode and is internally handled almost equally.

3) Multi-Session

We now understand what a session is, how they are created and how a user interacts with them. However, we haven’t discussed what happens if there are multiple sessions on a seat.

On current systems, multiple sessions are only allowed on seat0 if VTs are enabled (which is usually the case). That means, on all other seats, once you spawned a session, you have to stick with it. You cannot ctrl+alt+Fx away from the session (the reason for this is historical and we’re about to change this). However, you can close the session and then start a new one.

But if we assume our seat supports multiple sessions, how does that work? systemd-logind is responsible to manage and track sessions. Therefore, it keeps an Active attribute for each session. It’s a boolean value that defines whether the session is currently active or not. systemd-logind takes care that always at most one session is active. If no session runs on a seat, no session might be active (also in other situations which we ignore here as it’s an implementation detail). Additionally, systemd-logind sets the correct access-restrictions on devices attached to a seat so only the active session can access these devices. This guarantees that no background session interferes with foreground activity.

To switch between sessions, each session has to listen for special keyboard input (normally ctrl+alt+Fx) and instruct systemd-logind to switch to another session once it’s pressed. Today, this is implemented in the X-Server or, if no x-server runs in the session, by the underlying VT. You can also send a dbus call to systemd-logind directly to perform the session-switch (see loginctl activate ).

So for proper multi-session support, a login-session spawns a user-session during login but stays active in background. A user can now switch between the user-session and the login-session and optionally spawn additional session by logging in again. Session processes can listen for dbus signals from systemd-logind to be notified when they are activated or deactivated. This allows them to go asleep while being in background and start up again once being put in foreground.

For an in-depth view on session-switching, see this followup article.

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector