1KDE session manager (ksmserver)
2--------------------------------
3
4Matthias Ettrich <ettrich@kde.org>
5Lubos Lunak <l.lunak@kde.org>
6
7ksmserver is KDE's session management server. It talks the
8standard X11R6 session management protocol (XSMP). Thus, in theory,
9it should be compatible with all session management compliant X11R6
10applications. Unfortunately, there aren't that many of them. To be
11precise, I have never seen a single commercial application that
12supports it and even within the official X11R6 distribution, 'xclock'
13is the only exception. Nevertheless we've chosen to support XSMP
14despite the complexity of the protocol in order to provide KDE users
15more interoperability with applications that were not explicitly
16written with KDE in mind. XSMP, as an official X standard, promised to
17be more likely to be supported by third parties than even a superior
18KDE-specific protocol. Let's see whether we were right and more
19applications will actually talk XSMP. At least all KDE applications do
20now.
21
22Here's a short overview on how session management works.
23
24Contents
25--------
26
27Starting the server
28KDE startup sequence
29Establishing the connection
30Authorization
31Requesting a shutdown
32
33
34Starting the server
35-------------------
36
37The server is usually started from the 'startkde' script. It supports the following options:
38
39 -r, --restore Restores the previous session if available
40 -w, --windowmanager <wm> Starts 'wm' in case no other window manager is
41 participating in the session. Default is 'kwin'
42
43The default 'startkde' launches 'ksmserver --restore'. The
44'windowmanager' option is useful for users that prefer a window
45manager other than kwin. Since the window manager has to participate
46in the session (it has to remember window positions and states), it is
47usually restarted when the session is restored. To be *really* sure
48that this happens, even if the wm might have crashed during the
49previous session, ksmserver ensures that. The option specifies, which
50windowmanager shall be launched for sure. But again: if the stored
51session contains a window manager, the restored one will be used, not
52the specified one. As a special feature, ksmserver always starts the
53specified window manager first, which results in a much nicer startup
54sequence (less flashy).
55
56KDE startup sequence
57--------------------
58
59Ksmserver controls the second part of the KDE startup sequence,
60after it gets control from the startkde script, which controls
61the first part of the startup sequence. All code related to startup
62should be in startup.cpp and going down in that source file should
63follow the startup order (but note that this is just a documentation
64which may get outdated, so in case of doubts the source wins ;) ).
65
66The startkde scripts already launches kdeinit, which in turns launches
67KDE daemons like dbus daemon, klauncher and kded. Kded loads autoloaded
68kded modules, i.e. those that have X-KDE-Kded-autoload=true in .desktop
69files. The exact way autoloading works is controlled by X-KDE-Kded-phase=,
70which may be 0, 1 or 2 (the default). Kded phase 0 means the module is
71always loaded by kded, even outside of KDE session. It should used only
72by kded modules which must be always running. Kded phase 1 modules are
73loaded right after kded startup, but only during KDE startup, i.e. it is
74for modules that are always needed by the KDE session. Phase 2 modules
75will be loaded later. [More information about kded modules could be
76found in kdelibs/kded/HOWTO]
77
78Startkde also launches kcminit, which performs initialization done by kcontrol
79modules. There are two kcminit phases, 0, 1, controlled
80by X-KDE-Init-Phase= in the .desktop file, which defaults to 1. Phase 0 kcminit
81modules should be only those that really need to be run early in the startup
82process (and those should probably actually use kstartupconfig in startkde
83to be done even before kdeinit and daemons). After executing phase 0
84modules kcminit returns and waits.
85
86When ksmserver is launched, the first thing it does is launching
87the window manager, as the WM is necessary before any windows are possibly
88shown. When the WM is ready, ksmserver tells klauncher to perform autostart
89phase 0 ($KDEHOME/share/autostart). There are 3 autostart phases, 0, 1 and 2,
90defined by X-KDE-autostart-phase, defaulting to 2. Phase 0 is reserved only
91for the actual visible base components of KDE, i.e. Plasma, in order to make
92the startup appear visually faster. Plasma uses D-Bus calls suspendStartup()
93and resumeStartup() to make ksmserver stay waiting for autostart phase 0
94until Plasma is ready (note: suspendStartup() needs to be called before
95the application registers with ksmserver, i.e. before KApplication ctor is called).
96
97Next step is telling the waiting kcminit to perform phase 1 - all kcminit
98modules that should be executed before KDE startup is considered done.
99After that ksmserver tells klauncher to perform autostart phase 1,
100i.e. launching normal components of KDE that should be available right
101after KDE startup, and after this session restore is performed,
102i.e. launching all applications that were running during last session
103saving (usually logout).
104
105By this time KDE session is considered to be more or less ready and
106ksmserver does the knotify startkde event (i.e. plays the login sound).
107It also tells klauncher to perform autostart phase 2, kded to load all
108remaining autoload (i.e. kded phase 2) modules, and it itself executes
109the user Autostart folder.
110
111Technical note: There's a reason why kded modules and items in autostart
112default to the latest phase. Before you explicitly use a different phase,
113read and understand what's above. You should also consider whether something
114really needs to be launched during KDE startup and can't be loaded on-demand
115when really needed. Abusing the phases will result in public spanking
116for making KDE startup slower.
117
118
119Establishing the connection
120---------------------------
121
122As required by the XSMP specification, the session management server
123propagates its network address in the SESSION_MANAGER environment
124variable. Probably not the best way to do it, but it's the way it
125is. All KDE (and plain Qt) applications simply read this variable and
126try to establish a connection to an XSMP server at this address. If
127the variable is undefined, nothing happens.
128
129This means, if you want to start a program that should not participate
130in the session, simply undefine SESSION_MANAGER in your terminal
131window and launch the application. If you want to see an application
132desparately trying to connect to something, try setting it to some
133bogus value.
134
135In addition, ksmserver propagates both its network address and its
136process id in ~/.kde/socket-$HOSTNAME/KSMserver-$DISPLAY. A
137utility function KWorkSpace::propagateSessionManager() reads this
138setting and sets SESSION_MANAGER accordingly, so that child processes
139can pick it up. The function is called by clients that are started
140outside the session (i.e. before ksmserver is started), but want to
141launch other processes that should participate in the session.
142
143Authorization
144-------------
145
146XSMP is built on top of the Inter Client Exchange (ICE) protocol,
147which comes standard as a part of X11R6 and later.
148Authorization is done using 'iceauth', with MIT-MAGIC-COOKIE as used
149by X. In order to be able to access the session management server, you
150need to be able to read ~/.ICEauthority. For security reasons, we do
151not provide any host-based authorization.
152
153
154Requesting a shutdown
155---------------------
156
157If an application wants to request a shutdown (i.e. a logout), it does
158this via an SmcRequestSaveYourself message to the server. In KDE, a
159utility function KWorkSpace::requestShutDown() does exactly
160this. It's for example called by KDE's panel or by the context menu of
161the desktop.
162