79.13. turnstile-0.1.11
Per-user service manager launcher: login tracking, the runtime directory and a backend per user
| Version | 0.1.11, release 1 |
| Shelf | auth — Authentication, authorisation, accounts and directory services |
| Phase | 41_system (System), 187 of 1047 in build order |
| Licence | BSD-2-Clause |
| Homepage | https://github.com/chimera-linux/turnstile |
| Build time | 0.1 SBU |
| Installed size | 143.6 KiB in 7 files |
| Recipe | ports/core/auth/turnstile/kpkgbuild |
About
turnstile is the part of systemd --user that Plasma needs, and nothing else. Its daemon, turnstiled, counts each user’s logins across SDDM, the text consoles and anything else that runs the PAM module pam_turnstile: at the first login it creates /run/user/<uid> and starts that user’s service manager through a backend, and at the last logout it stops the services and removes the directory, unless the user lingers. The PAM module holds the login until the backend reports its services ready, then exports XDG_RUNTIME_DIR and DBUS_SESSION_BUS_ADDRESS into the session.
On KDOS the backend is ksvc, the script /usr/lib/turnstile/ksvc, which runs the scripts in /etc/kdos/user-services/ under ksvc: the session bus at $XDG_RUNTIME_DIR/bus, the account’s ssh-agent, PipeWire, WirePlumber, pipewire-pulse, mpd once it is configured, the user’s timers, and the stop of the user’s boxes at the last logout. Every account from uid 1000 gets them, and so does plasma-setup, the system account the first-boot wizard runs in; the greeter’s sddm account gets the session bus alone, and other system accounts nothing. Both it and /etc/turnstile/turnstiled.conf come from the tree’s fs/ overlay, not from this port. turnstiled itself runs as root under ksvc from an init script. See No systemd init, units or service manager.
Sources
turnstile-0.1.11.tar.gz(40.8 KiB)
sha25604904eff68a726bed281820b3497e018ede55a5d745f31ceb9a502f905d0ed56
upstream: https://github.com/chimera-linux/turnstile/archive/refs/tags/v0.1.11.tar.gz
archive copy: sources-1/auth--turnstile-0.1.11.tar.gz
Dependencies
Depends on: meson, ninja, pkgconf, pam, scdoc.
Required by (1): sddm.
Patches
build.sh applies these; kpkg applies nothing by itself.
The last logout’s clear of the runtime directory leaves every mount point in it alone (rundir-mounts.patch). KDOS’s pack daemon composes each box’s root as an overlay under /run/user/<uid>/kdos/boxes/<box>/root, whose upper directory is the box’s persistent state in the user’s home, so a clear that descended into it would write a whiteout for every file of the box.
A login that arrives while the previous manager is stopping waits for that manager to exit and for the next one to report ready (stopping-manager-readiness.patch). Upstream releases it on the stopping manager’s readiness when that manager was still coming up as the last session closed, and SDDM’s automatic login after loginctl terminate-user lands there: Plasma starts on the session bus that is being stopped, kded6 and plasmashell lose it, and the screen stays black. The same patch closes a stopped manager’s unreported readiness pipe when the next one starts, since an event on it would otherwise make turnstiled exit. The patch is to be offered upstream.
session-socket-cloexec.patch opens the connection pam_turnstile makes to turnstiled, which is the login, close-on-exec. turnstiled counts a login as ended only when the last copy of that connection closes, and without the flag every program the PAM caller executes inherits one: SDDM’s session, Plasma and every process started from it, a box’s kdos-appbox keeper, conmon and podman exec included. A box or any other process from the session still running at Log Out then holds the login open, and the last logout never stops the user services or clears /run/user/<uid>. With the flag only the PAM caller holds the login: sddm-helper opens the PAM session and then forks and executes the session, and closes the PAM session when the session process exits; util-linux login opens it, forks the shell and closes it when the shell exits. A PAM caller that executes the session in its own process, with no parent left to call pam_close_session, would end the login at once; no PAM stack on KDOS that loads pam_turnstile belongs to one. The X server probe socket the module opens is made close-on-exec as well. The patch is to be offered upstream.
rundir-mounts.patch— Never clear a runtime directory through a mount point inside itClearing a runtime directory at the last logout descends into every subdirectory, including one that is the root of another mount. A filesystem still mounted there (an overlay a root daemon composed for a container, a FUSE mount the user left behind) is then emptied through the mount: its files are deleted, and on an overlay the deletions are written to the upper directory as whiteouts, which loses the data the mount shows.
Leave a mount point, and every directory that holds one, in place, and report the directory as not empty so it is not removed. A mount root is a directory on another device than its parent, or one statx() marks with STATX_ATTR_MOUNT_ROOT, which also catches a bind mount of the same filesystem.
session-socket-cloexec.patch— Open the PAM session socket close-on-execThe connection pam_sm_open_session makes to turnstiled is the login: turnstiled counts the session as ended only when the last copy of that connection closes. Opened without SOCK_CLOEXEC, it is inherited by every program the PAM caller executes for the session (the display manager's session process, the desktop, and every process it starts), so any one of them still running after the user logs out holds the login open: the last logout never stops the user's service manager and the runtime directory is never cleared.
Open it close-on-exec, so only the PAM caller holds it, until pam_close_session or pam_end closes it. Open the X server probe socket the same way, so a caller that executes a program while the probe is open does not pass it on either.
stopping-manager-readiness.patch— Release no login on the readiness of a service manager that is stoppingWhen the last session closes while the service manager is still coming up, the manager is told to stop, but its readiness pipe stays open. A new login that arrives meanwhile waits in srv_pending for the old manager to exit. If the stopping manager then reports ready, the pipe handler runs the "ready" job, and its exit sends MSG_OK_DONE to every session of the login, which releases the new login against services that are being stopped. The session starts on a bus that disappears a moment later.
Drop the readiness of a manager once it is no longer the login's srv_pid; the next manager reports on its own pipe. Also, when a manager starts while the previous manager's pipe is still open (the previous manager ended without reporting), close that pipe and take it off the poll list. The pipe handler finds a login only by its current pipe, and an event on the old one would otherwise make turnstiled exit.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && kpkg install turnstile'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/auth/turnstile.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/auth/turnstile
echo '04904eff68a726bed281820b3497e018ede55a5d745f31ceb9a502f905d0ed56 turnstile-0.1.11.tar.gz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/turnstile
SRC=$SRC_ROOT/turnstile-0.1.11
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf turnstile-0.1.11.tar.gz -C "$SRC" --strip-components=1
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
patch -p1 -i "$PORT_SRC/rundir-mounts.patch"
patch -p1 -i "$PORT_SRC/stopping-manager-readiness.patch"
patch -p1 -i "$PORT_SRC/session-socket-cloexec.patch"
meson setup build \
--prefix=/usr --sysconfdir=/etc --libdir=lib \
--libexecdir=/usr/lib \
--buildtype=release \
-Ddinit=disabled \
-Drunit=disabled \
-Dmanage_rundir=true \
-Dstatedir=lib/elogind \
-Dlibrary=disabled \
-Dman=true
meson compile -C build
DESTDIR=$PKG meson install --no-rebuild -C build
rm "$PKG/etc/turnstile/turnstiled.conf"
rmdir "$PKG/etc/turnstile"
The switches, and why each is set:
--libexecdir=/usr/lib—turnstiledruns a backend named<backend>as<libexecdir>/turnstile/<backend>, so the KDOS backend is/usr/lib/turnstile/ksvc.-Ddinit=disabled,-Drunit=disabled— Install neither upstream backend nor its configuration. KDOS runs neither service manager; its backend is theksvcscript.-Dmanage_rundir=true— Compile inmanage_rundir = yes:turnstiledcreates/run/user/<uid>at a user’s first login and removes it at the last, and the PAM module exportsXDG_RUNTIME_DIR. elogind is built not to, so turnstile is the directory’s one owner; without this, and with elogind’s runtime-directory code patched out, a login gets no runtime directory and KWin, the greeter’s included, does not start. Lingering also works only with it on.-Dstatedir=lib/elogind— The state directory, relative to/var, whoselingersubdirectoryturnstiledreads: a file named after a user there keeps that user’s services running after the last logout (linger = maybe). elogind writes/var/lib/elogind/linger/<user>whenloginctl enable-lingerruns, so this makes the two read the same directory. turnstile keeps nothing else there.-Dlibrary=disabled— Build nolibturnstile, the client library for askingturnstiledabout sessions; nothing in the tree links it.-Dman=true— Buildturnstiled(8),pam_turnstile(8)andturnstiled.conf(5)with scdoc.
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/turnstile, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf turnstile-0.1.11-1.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/turnstile, and runs the triggers its paths call for.
Contents
Installed programs: turnstiled.
Installed directories: /usr/share/licenses/turnstile.
Short descriptions
turnstiled |
the main session management daemon |
Notes
The recipe deletes /etc/turnstile/turnstiled.conf, which the build generates from its options. KDOS’s own copy is in fs/ and sets backend = ksvc, manage_rundir = yes, export_dbus_address = yes and linger = maybe; a copy in the package would be installed over it, because packages are installed after fs/ is synced. default_backend is not passed: it changes only the generated file, and turnstiled’s own built-in backend is dinit whatever the build says, so without KDOS’s file every login would try to run a dinit backend that does not exist. The runtime-directory default does reach the daemon, through -Dmanage_rundir=true.
/etc/pam.d/turnstiled is upstream’s: the PAM stack of the session turnstiled opens for the user’s service manager. It includes pam_elogind, so elogind sees that session, and pam_limits, so the realtime and memory-lock limits in /etc/security/limits.d reach PipeWire, which the backend starts inside it, and not only the login shell.
turnstiled waits for the backend’s readiness report for at most login_timeout seconds and then ends the user’s service manager and closes the user’s session connections, so a backend that never reports ready shows up as a login that hangs for that long and then has no user services.