# Session manager

`dtsession` starts the user's desktop session, restores the clients saved in it, locks the screen and saves the session again at logout. It reads the files of a saved session, answers X clients and the clients of the X Session Management Protocol (XSMP) over ICE sockets, and receives ToolTalk requests. It is installed with the identity of the user who starts it, and it locks the screen only where it can check the user's password through PAM.

## Installed identity

`dtsession` is installed with the identity of the user who starts it: not setuid and not setgid. It used to be installed setuid root on a system without PAM, to read the password database when the screen was unlocked, and some systems made it setgid for the group that console management needed. Neither is needed any more: the password is checked through PAM (see Screen lock) and the console-management code that read kernel memory is gone.
- `SmPrivDropInstalledIdentity()` guards against an installation that does not follow that rule (a package that sets the bit, a copy made by hand). It is the first thing `main()` does, before the locale is set, before the environment is read and before the toolkit or the display are opened; earlier, the locale files were opened and parsed while the process still ran as root. When the effective user or group id differs from the real one, the real, effective and saved ids all become the user's, and the program then tries to take the installed ids back to be sure that it cannot. If a step is refused (a user at a process limit makes `setreuid` fail with `EAGAIN`, a kernel or security module can refuse it), has no effect, or leaves a way back, `dtsession` stops with a message instead of running the session with the installed identity. An installation that is not setuid or setgid makes no identity call at all, and neither does a `dtsession` started by root, which can take any identity.
- Nothing else in the program changes its identity. The functions that took the installed user id back to read the password database, the group switch for console management and the identity drop that the child processes made before they exec are removed, and a test fails when one of them comes back.
- Dead code that needed those functions is removed with them: `GetMemoryUtilization()`, which read `/dev/kmem` through the HP-UX kernel symbol table (its Linux variant returned a constant, and on no supported system does the HP-UX kernel exist), the `system` value of `contManagement` that asked for it, the `memThreshold` resource that nothing read, the never-compiled code for the AIX high-function terminal ring and the code for the Tru64 security library (`SIA`).

To check the guard, make a copy of `dtsession` setuid and run it under `strace -f -u <user> -e trace=openat,setresuid`: the first `setresuid` comes before the first open of a locale file. `make -C programs/dtsession check` runs the fake-kernel scenarios and a source guard for the order.

## Screen lock

The display is unlocked with the password of the user who started the session, checked through PAM, and with no other password.
- `SmPam.c` is the only code that checks a password. It starts the PAM service `dtsession`, authenticates, checks the account with `pam_acct_mgmt` and ends PAM. The code that read the password database and compared a hash from `crypt()` is removed, together with the setuid installation it needed.
- The user is the account of the real user ID of the process. It is not typed into the dialog and not taken from the environment. Before, the lock also accepted the root password and the passwords of the users named in the `keys` resource; both are gone, and so is the resource.
- The lock dialog names the same account ("Display locked by user ..."). It took the name from `LOGNAME`, which says anything the environment says, and ended `dtsession` with a null pointer when the variable was not set.
- The service name is fixed. It was the name `dtsession` was started with (`argv[0]`), which the invoker chooses; a `dtsession` started through the search path failed every unlock attempt. `config/dtsession.src` is installed as `/etc/pam.d/dtsession`. Its stack authenticates the user it is given with `pam_unix` and nothing else: no module that accepts a password for another account. `pam_unix` checks the password of the calling user without help from `dtsession`.
- An account that is not in order does not unlock the display: an expired, locked or refused account and an empty password are all refused, and so is every failed authentication. Any failure (PAM cannot be started, the account is unknown, a module fails) leaves the display locked. One result of the account check does not keep the user out: a password that expired while the user was logged in (`PAM_NEW_AUTHTOK_REQD`). The user is inside the session already, and the password is changed at the next login.
- The conversation with the PAM modules answers the password prompt and nothing else. A module that asks a visible question, such as a user name, fails the conversation.
- The display is locked only when it can be unlocked again. `dtsession` checks that when it starts and before every lock: it was built with PAM, the file of the PAM service (`/etc/pam.d/dtsession`, `/usr/local/etc/pam.d/dtsession` on FreeBSD, where `config/Makefile.am` installs it) can be read, and the user who started the session has an account. If one of them is missing, the error log says which (a build without PAM; the PAM service file, named in the message, is missing or cannot be read; no account for the user), the lock request is refused, and the rest of the session works. A missing service file used to show only when somebody tried to unlock: PAM then falls back to a stack that may refuse everybody, and the display stayed locked. Systems without PAM have no screen lock.
- An administrator ends a locked session from the console, by logging in on another terminal and terminating `dtsession` or the session.
- The typed password is overwritten in memory once it has been checked, when the line is killed with Escape, when characters are backed over and when the dialog times out; it used to stay in the heap buffer.

## Signals
- The shutdown holds `SIGTERM` while it runs and gives the signal mask back when it ends without exiting, which is what happens when a client cancels it; the next signal then starts a new shutdown. A signal that arrives during a shutdown is held until it ends, and a second logout request made while one is under way is dropped. The wait of the shutdown for the answers of the clients runs the event loop with `XtAppProcessEvent`: the answers, a client's cancel and the timeout arrive as callbacks, which `XtAppNextEvent` runs without returning until an X event comes in, so a cancelled shutdown of an idle display never ended. If the signal cannot be set up (the pipe, its flags or the handler), the failure is logged and `SIGTERM` gets its default action, even when the program was started with it ignored.
- `SIGTERM` (sent by the login manager when the session ends, or by `kill`) starts a normal shutdown. The handler used to run the whole shutdown: it sent ToolTalk notices, saved the session, allocated memory, ran an event loop and started the user's `sessionexit` script from inside the signal handler, which deadlocks or corrupts memory if the signal arrives while the program is in `malloc` or Xlib. The handler now writes one byte to a pipe and returns, and the event loop carries out the shutdown. Several signals cause one shutdown; a shutdown that a client cancels can be requested again. If the pipe cannot be created the signal keeps its default action and ends the process.

## Session files and directories
- Saving a session archives the directory it writes, "current" or "home", to its `.old` version and moves an older `.old` aside under a name made with `mkdtemp` (if no such name can be made, the older archive is dropped so that the save can go on). If a write fails, or the save is cancelled, the archive is put back from where it was made: the directory that was written and the one it was archived to are the ones the save chose, not ones derived from the kind of session, so a Home session that saves into "current" at logout no longer loses its Home directory, and the first save of a session, which archives nothing, no longer takes the user's `sessionetc` script for an archive. A cancelled save puts the archive back also when there was no older one. Pruning goes on only if the session was moved aside first, and runs the discard commands with the oldest archive in place.
- An entry that cannot be removed no longer stops the removal of the rest of a directory (as with `rm -rf`), and a copy of a session that fails is removed instead of being restored half-copied at the next start.
- Sessions are archived, pruned, restored and copied without a shell and without looking programs up in the search path. Removing a session directory ran `system("rm -rf <path>")` and copying one ran `system("cp -r <from> <to>")` with the unquoted path of the home directory, so a home directory with a space in its name removed or copied the wrong directories, and one with shell syntax in it ran that syntax. Moving a directory started `mv` through the search path in a `vfork` child. These steps are now done in process (`SmRemoveTree`, `SmCopyTree` and `rename`): a symbolic link in a session directory is removed or copied as a link, never followed, and a move onto an existing directory fails instead of nesting one inside the other.
- The error log is rotated again. The name of `errorlog` was built one character short, so it was never moved to `errorlog.old`; with a long home directory the buffers were resized through the wrong pointer. The files are now renamed instead of linked and unlinked, so a file system without hard links, or an empty `errorlog.old`, no longer loses the log.
- `libDtSvc` (`MsgLog.c` and `UErrNoBMS.c`) also caps `$HOME/.dt/errorlog` live during a running session at `2 MiB` by default, trimming in place under an advisory file lock to keep the newest \~`1 MiB` on a clean record boundary while preserving the file's inode. Trimming and minimum severity can be configured or disabled via environment variables (`DT_ERRORLOG_TRIM=0` or `DT_ERRORLOG_MAX_BYTES=0` to disable, `DT_ERRORLOG_MAX_BYTES=4M` to resize, `DT_ERRORLOG_LEVEL=debug|info|warning|error|off`, `DT_ERRORLOG_DEDUP=0|1`) or persistently in `$HOME/.dt/errorlog.conf` (`trim`, `max_bytes`, `level`, `dedup`). By default `DtMsgLogDebug` (and `DtIgnore`) diagnostics are suppressed unless `DT_ERRORLOG_LEVEL=debug` or `CDE_DEBUG=1` is set, and repeated identical tight-loop bursts are coalesced into a single summary line.
- The paths of the session files are put together in fixed buffers. A home directory so long that they would not fit is refused like a home directory that cannot be written, and `dtsession` starts with the system defaults instead of overflowing the buffers or, when the directories cannot be created at all, dereferencing a null path.

## Saved sessions and palettes

A saved session is read from files in the user's home directory, and the palette the color server loads from the files and resources named in `Dtstyle.paletteDirectories`. A damaged or hand-edited file used to crash `dtsession` while it started, or corrupt its memory.
- The quoting parser for the commands of a CDE 1.0 client file indexed one element past its table of nesting levels when quotes were nested ten deep.
- The per-screen buffers for hints, commands and remote commands were allocated with room for 5,000 bytes and grown by 5,000 bytes, whatever the length of the line that had to fit, and the check for growing them was off by one byte; a longer command or hint overran the heap. They are now grown to fit.
- An option without a value (`-screen` at the end of a line, `-cmd` with an empty command) dereferenced a null pointer, and the host or command of one line carried over to the next, which then started a local command as a remote one. The lines of the client file are sorted into the buffers by `ParseClientFile`, which starts every line from nothing.
- The font path list of the saved settings wrote one pointer past its array at 50 entries and went on through the freed array when it grew. The key mapping divided by a number of keysyms per code of zero, and the modifier map wrote `8 * maxKeyPerMod` bytes into a 5,000 byte buffer for any value the file named; it is accepted up to the 255 the protocol allows.
- Directories of `Dtstyle.paletteDirectories` are separated by blanks, but the list was walked without skipping them: the second directory of a list sent the color server into an endless loop. An entry longer than 255 bytes overran the stack, and the copy of a directory that held the palette was allocated with the length of the system directory. A palette with more colors than the table (eight), a first line longer than the read buffer or without a newline, or too few colors for a low color display read or wrote outside its buffers.
- The unused `MigrateResources` converter, which was not part of the build, is removed.

## Session management clients

`dtsession` listens on ICE sockets for clients of the X Session Management Protocol. A client has to present the user's ICE authority cookie; what it and the window manager (which answers the `GetWsmClients` request over ToolTalk) send is still checked.
- A client that restarts with the id it had when the session was saved registered with `RegisterClientProc`, which compared the id with those of all connections, including the one that was registering and any other that had no id yet: a null id went to `strcmp` and ended `dtsession`. Restoring a client that uses XSMP therefore ended the session manager. The connections without an id are skipped, and a host name without a transport part (`local/`, `tcp/`) is used as it is.
- Deleting the first property of a client freed the head of the property list without unlinking it and then went on with the freed record; any client that deleted its first property (`Program`, say) corrupted the session manager. The properties of the name are unlinked from wherever they are in the list.
- The reply with the clients of the window manager holds a count, the ids as consecutive strings and an array of workspace numbers, and the lists were walked as often as the count said. A reply with fewer ids or numbers than its count, ids without terminators or a negative count is not used; the client that asked to interact is then let interact as if the window manager had not answered. A reply that arrives after the save ended is ignored.

## Starting clients

`tests/check-client-start.py` runs the production `StartLocalClient` and `ForkWM` for real and checks what the sections below describe.

The clients of a saved session, the window manager and the resource loader are started in a child process that adds the saved environment, changes directory and execs. The child was made with `vfork()`, which shares the memory of `dtsession` with it until the exec: the `putenv` of the saved variables changed the environment of `dtsession` itself and so of every client started after it (a variable saved with one client reached the next), and a failed exec called `SM_EXIT`, which returns while the session is being saved, so the child went on as a second session manager. The children are made with `fork()` and a child whose exec fails ends with `_exit`; the window manager's child no longer falls through when the exec fails while a local window manager was started.

## ICE authority file

`tests/check-ice-authority.py` runs the production `writeIceauth` and `SetAuthentication` against a libICE stand-in that can refuse the lock.

`dtsession` gives its XSMP clients a cookie by merging it into `~/.ICEauthority` under the library's lock. When the lock could not be taken (another process holds it, or the file system cannot make the lock file) it removed the lock files, which belong to the process that holds the lock, and tried again with the file `/etc/dt.ICEauthority` (the path was missing its slash), so the cookies of the session would have gone into a system path where no client looks for them, and a buffer leaked each time. A lock that cannot be taken now ends the attempt and leaves the lock files alone; the user's file is the only one written. A cookie that cannot be made no longer leaves unset entries behind for the cleanup to free.

## Client lifetimes

`tests/check-xsmp.py` also covers these, with the records and the leaks checked under LeakSanitizer.
- A client that disconnected after asking to interact stayed in the list with its request: the reply of the window manager then told `SmsInteract` to let a connection that was gone interact, and with a restart hint of "never" the record that the cached request pointed to was freed while the reply was still to come. `CloseDownClient` clears the request and the cached pointer, `ProcessInteract` passes over clients without a connection and lets the clients that are still there interact when the one that asked has left.
- The record of a client that asked not to be restarted was meant to be removed when it disconnected, but the test for the record ran after its connection had been set to null and never matched, so every such client's record, id, host name and properties stayed for the life of the session (the properties' names and types were not freed even when a record was). The record is unlinked by pointer and freed.
- Registering a client left its previous id, the id it generated and the host name from SMlib allocated; the record keeps its own copy of the host name.

## Language and resolution independent resources

`tests/check-startup-resources.py` runs the production function with the input described here.

When the language or the display resolution of a saved session differs from the current one, `dtsession` looks for a font resource file in the session directory and in the system directories and merges it into the resources of the display. It compared the language of the environment and of the session with `strcmp` whether or not they were set, built the names of the system files with unbounded `strncat` calls from the language (a `LANG` of a few thousand characters overran the buffer), formatted a missing session directory (after the session directories could not be made) into the name, handed the toolkit an uninitialized font list when the resources had none and sized the copy of each font resource with a guess. An unset language is an empty one now; names that do not fit are names of no file; a font list or property that is not there is not used.

## Debug code

`tests/check-debug-code.py` is a source guard for what follows.

Two pieces of code that only existed in `DEBUG` builds are removed. The Xrm subtraction code appended the user's resource database to the fixed file `/tmp/dtsession.xrm`, opened without `O_NOFOLLOW` in a directory every user can write to, and `main()` spun in an endless loop when it was started with more than five arguments, waiting for a debugger. A test checks that no source names a file in `/tmp` and that none of them loops there.

## Tests

`programs/dtsession/tests` holds regression tests that cut the production code out of the sources, wrap it with stubs and failure injection and run it under AddressSanitizer and UndefinedBehaviorSanitizer:

```sh
make -C programs/dtsession check
```

`check-privileges.py` runs `SmPrivDropInstalledIdentity()` against a fake kernel that models the real, effective and saved ids and can refuse a call, report success without changing anything or leave a way back to the installed identity, for a `dtsession` installed setuid, setgid, both and not at all; it also guards the source (the identity is given up first in `main()` and no other code changes one). `check-pam-lock.py` compiles `SmPam.c` against a stand-in for the PAM library (a right and a wrong password, a start that fails, an authentication or an account check that fails, a password that expired during the session, a module that asks something else than the password, failing allocations, other program names and environments, a missing or unreadable service file) and once more without PAM. `check-lock-authentication.py` runs the password input, the start of the lock and the messages that say why it cannot be made, and guards the source: no other way to check a password, a PAM service that lets only the user in, the file that is looked for is the one that is installed, no setuid installation. `check-signals.py` raises `SIGTERM` against the production handler and checks that nothing of the shutdown runs inside it. `check-session-files.py` runs the error-log rotation, the directory moves, copies and removals and the session path set-up on real files, with names that contain spaces and shell syntax, a file system without hard links, an unwritable and an over-long home directory. `check-session-parsers.py` feeds crafted client files and settings to the production parsers and `RestoreSettings`, in a UTF-8 and the C locale. `check-palette-parser.py` does the same for the palette directory list and palette files. `check-xsmp.py` calls the XSMP registration, interaction and property callbacks with the messages of clients that restart, delete properties or send short lists, with SMlib and ToolTalk replaced by stand-ins.

To check the session end to end, start `Xsession` from a staged install under `Xvfb`, open the Style Manager, a Terminal and a File Manager, send `SIGTERM` to `dtsession` and start `Xsession` again with the same home directory: the first session ends through the event loop and writes `~/.dt/sessions/current`, and the second one restores the three windows. This was run on 2026-11-04 with the build of this change in the Linux guest.

To check the lock end to end, run the session under `Xvfb` as a throw-away user in a private mount namespace in which the staged `usr/dt` is mounted and the system's `/usr` stays as it is (so that `pam_unix` and its setuid helper work), with `config/dtsession.src` temporarily installed as `/etc/pam.d/dtsession`. `dtaction LockDisplay` covers the screen; a wrong password, an empty password and the password of another user leave it covered (the journal shows the failures of `pam_unix` for the service `dtsession`), and the user's password uncovers it. With `SmPam.c` built without PAM, or without `/etc/pam.d/dtsession`, the same request is refused, `~/.dt/errorlog` says why (and names the file) from the start of the session on, `dtsession` keeps running and a Terminal still opens. A root-owned setuid or setgid copy of `dtsession`, started as the user under `strace -f -u <user>`, shows `setreuid` (or `setregid`) to the user, the refused attempts to take the installed identity back, and only then the first open of a locale file. `chage -E` (expired account) and `passwd -l` (locked account) make the real `pam_unix` check refuse the right password, while `chage -d 0` (password expired) does not keep the user out; a wrong password and the password of another user are refused in every case. This was run on 2026-11-04 in the Linux guest with the build of this change.
