Files
chandra/session/chandra-session
Claude 2752b81e36
CI / test (push) Canceled after 1m11s
Session: wait for the document portal's mount before reopening apps; say when it's broken
V's session.log had kwi (a Flatpak) failing with "bwrap: Can't find source path
/run/user/1000/doc/by-app/org.kde.kwi". At login chandra-session restarts
xdg-desktop-portal, and xdg-document-portal stopped and started with it in the
same second (V's journal, 21:36:12); a Flatpak started in that gap, such as one
session restore reopens, finds no FUSE mount to bind into its sandbox.

When flatpak is installed, chandra-session now asks the document portal for its
mount point (which also starts it), waits up to 5 s for the FUSE mount and
checks it answers. If it doesn't, the reason goes to the log and into
CHANDRA_SESSION_ERRORS, which the shell shows as an error notice once the desktop
is up (DesktopShell::show_session_errors). A missing dbus-send is reported too.

Tested: the snippet against a real xdg-document-portal on a dbus-run-session bus
(mounted after 0 waits) and with an unreachable bus (error text); the notice in
compositor/host/launch_test.mbt and headless with build/wasmhost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiGJWeuHiTgtoyB4kb
2026-11-26 06:29:54 +00:00

235 lines
12 KiB
Bash
Executable File

#!/bin/sh
# Login-session entry point for Chandra (started by the display manager).
# Runs the compositor on the seat's display via DRM/KMS + libinput and logs to
# ~/.local/state/chandra/session.log. With --x11 (the "Chandra (X11)" entry in
# /usr/share/xsessions, or `exec chandra-session --x11` in ~/.xinitrc) it runs on the X server
# in $DISPLAY instead and covers the whole screen: for GPUs whose driver gives Chandra no KMS
# path, such as NVIDIA's proprietary one (docs/x11.md). By default that is the compositor as a
# WebAssembly component, run by chandra-host; if only the native binary was installed
# (session/install.py --native), it runs that instead.
share=/usr/local/share/chandra
# The desktop runs as the user who logged in, never as root: as root (say `run0 chandra-session`
# or `sudo chandra-session`) every app would run as root, its socket would live in root's runtime
# dir where your apps can't see it, and settings would land in /root. For one app that needs
# root, use `chandra-root APP` inside the session. CHANDRA_ALLOW_ROOT=1 overrides this.
if [ "$(id -u)" = 0 ] && [ -z "$CHANDRA_ALLOW_ROOT" ]; then
echo "chandra-session: refusing to run the desktop as root; start it as yourself" \
"(use chandra-root APP for single apps that need root)" >&2
exit 1
fi
# Started from a login without the usual session environment (run0 --user=..., su, a bare
# `env -i`): fill in HOME and the runtime directory from the system instead of falling back to
# /tmp, so the socket lands where apps and chandra-root look for it.
if [ -z "$HOME" ]; then
HOME=$(getent passwd "$(id -u)" | cut -d: -f6)
export HOME
fi
if [ -z "$XDG_RUNTIME_DIR" ] && [ -d "/run/user/$(id -u)" ] && [ -O "/run/user/$(id -u)" ]; then
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
fi
case "$0" in /*) self="$0" ;; *) self="$PWD/$0" ;; esac # for the dbus-run-session re-exec
# The display backend: drm (default) or x11. Kept in the environment so the dbus-run-session
# re-exec below (which drops the arguments) still knows.
if [ "${1:-}" = "--x11" ]; then
export CHANDRA_BACKEND=x11
fi
case "${CHANDRA_BACKEND:-drm}" in
x11)
backend="--x11 --login-session"
if [ -z "${DISPLAY:-}" ]; then
echo "chandra-session: --x11 needs DISPLAY (start it from an X session or ~/.xinitrc)" >&2
exit 1
fi
;;
*) backend=--drm ;;
esac
export XDG_SESSION_TYPE=wayland
export XDG_CURRENT_DESKTOP=chandra
export XDG_SESSION_DESKTOP=chandra
export MOZ_ENABLE_WAYLAND=1
export QT_QPA_PLATFORM=wayland
export SDL_VIDEODRIVER=wayland
# Qt's platform theme decides where Qt apps get their palette from. With XDG_CURRENT_DESKTOP=chandra
# Qt picks none of its desktop themes, so Qt 6 apps never saw the Dark mode switch (KDE Frameworks
# 6 apps such as KWrite then pick Breeze Light). Prefer KDE's own (plasma-integration: reads the
# kdeglobals colors chandra-kde-colors writes and repaints live when the switch flips), else Qt's
# xdgdesktopportal theme (Qt 6.5+ follows the portal's color-scheme, which the compositor serves,
# and KDE Frameworks apps then pick Breeze Dark/Light). A value set elsewhere (qt6ct, say) wins,
# unless its plugin isn't installed: QT_QPA_PLATFORMTHEME=gnome left over from a GNOME setup
# without QGnomePlatform makes Qt load no theme at all, and Dolphin then mixes a light palette with
# the dark kdeglobals colors (near-black rows with dark text).
qt_theme_plugin() { # QT_MAJOR GLOB -> success if a platform theme plugin matching GLOB exists
for d in /usr/lib/qt$1/plugins /usr/lib64/qt$1/plugins /usr/lib/*-linux-gnu*/qt$1/plugins \
/usr/lib/qt/plugins /usr/lib64/qt/plugins /usr/local/lib/qt$1/plugins; do
for f in "$d"/platformthemes/$2; do [ -e "$f" ] && return 0; done
done
return 1
}
qt_theme_installed() { # KEY -> failure if we know KEY's plugin and it is installed for neither Qt
case "$1" in
kde) g='KDEPlasmaPlatformTheme*.so' ;;
xdgdesktopportal|flatpak|snap) g='libqxdgdesktopportal.so' ;;
gtk3) g='libqgtk3.so' ;;
gnome) g='*gnomeplatform*.so' ;;
lxqt) g='libqtlxqt.so' ;;
qt6ct) g='libqt6ct.so' ;;
qt5ct) g='libqt5ct.so' ;;
*) return 0 ;; # a theme we don't know: trust it
esac
qt_theme_plugin 6 "$g" || qt_theme_plugin 5 "$g"
}
if [ -n "${QT_QPA_PLATFORMTHEME:-}" ] && ! qt_theme_installed "$QT_QPA_PLATFORMTHEME"; then
echo "chandra-session: QT_QPA_PLATFORMTHEME=$QT_QPA_PLATFORMTHEME has no plugin installed; picking one" >&2
unset QT_QPA_PLATFORMTHEME
fi
if [ -z "${QT_QPA_PLATFORMTHEME:-}" ]; then
if qt_theme_plugin 6 'KDEPlasmaPlatformTheme6.so'; then
export QT_QPA_PLATFORMTHEME=kde
elif qt_theme_plugin 6 'libqxdgdesktopportal.so'; then
export QT_QPA_PLATFORMTHEME=xdgdesktopportal
elif qt_theme_plugin 5 'KDEPlasmaPlatformTheme*.so'; then
export QT_QPA_PLATFORMTHEME=kde
fi
fi
mkdir -p "$HOME/.local/state/chandra"
log="$HOME/.local/state/chandra/session.log"
[ -n "$CHANDRA_SESSION_LOG_STARTED" ] || : >"$log"
export CHANDRA_SESSION_LOG_STARTED=1
# The compositor reads wit/ and config/ relative to its working directory.
cd "$share" || exit 1
sock="${XDG_RUNTIME_DIR:-/tmp}/wayland-moon-0"
rm -f "$sock"
if [ -x /usr/local/bin/chandra-host ]; then
set -- /usr/local/bin/chandra-host "$share/compositor.wasm"
else
set -- /usr/local/bin/chandra
fi
# Session bus. Use the per-user bus (systemd's $XDG_RUNTIME_DIR/bus) when there is one: that is
# where the secret service (gnome-keyring or oo7-daemon), unlocked with the login password by
# its PAM module, registers org.freedesktop.secrets. A fresh dbus-run-session bus would hide it, and apps would then
# activate a second, locked keyring daemon there (unlock prompts, "default keyring" dialogs).
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && [ -S "${XDG_RUNTIME_DIR:-/nonexistent}/bus" ]; then
export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus"
fi
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && command -v dbus-run-session >/dev/null 2>&1; then
# No bus yet (no systemd user session): start one and run this script again inside it, so the
# environment setup below happens on that bus.
exec dbus-run-session -- "$self"
fi
# D-Bus- and systemd-activated services (gcr-prompter for keyring unlock prompts, portals,
# notification daemons) start with the activation environment, not ours: without
# WAYLAND_DISPLAY they can't open a window, so a keyring unlock prompt silently fails and the
# app gets "locked collection" errors.
if command -v dbus-update-activation-environment >/dev/null 2>&1; then
dbus-update-activation-environment --systemd \
WAYLAND_DISPLAY="$(basename "$sock")" XDG_SESSION_TYPE XDG_CURRENT_DESKTOP \
XDG_SESSION_DESKTOP QT_QPA_PLATFORM QT_QPA_PLATFORMTHEME ${DISPLAY:+DISPLAY} >>"$log" 2>&1 ||
dbus-update-activation-environment \
WAYLAND_DISPLAY="$(basename "$sock")" XDG_SESSION_TYPE XDG_CURRENT_DESKTOP \
XDG_SESSION_DESKTOP QT_QPA_PLATFORM QT_QPA_PLATFORMTHEME >>"$log" 2>&1 || true
fi
# xdg-desktop-portal picks its backends once, at start, from $XDG_CURRENT_DESKTOP (here
# chandra-portals.conf: the compositor answers apps' dark/light question). One still running from
# an earlier session (systemd keeps the user's services between logins) would keep another
# desktop's choice, so Chrome and Firefox would not follow the Dark mode switch: restart it if it
# is running. Apps start it on demand otherwise.
if command -v systemctl >/dev/null 2>&1; then
systemctl --user try-restart xdg-desktop-portal.service >>"$log" 2>&1 || true
fi
# Flatpak apps need the document portal's FUSE mount: `flatpak run` binds
# $XDG_RUNTIME_DIR/doc/by-app/APP into the sandbox, and bwrap dies ("Can't find source path
# .../doc/by-app/org.kde.kwi") while nothing is mounted there. The portal restart above stops and
# starts xdg-document-portal too, so wait (up to 5 s) for its mount before the compositor reopens
# the last session's apps. A portal that doesn't come up is shown on screen when the desktop
# starts (CHANDRA_SESSION_ERRORS) and logged, not left for each Flatpak launch to hit.
session_errors=
doc_mounted() { # PATH -> success when a FUSE file system is mounted at PATH
awk -v m="$1" '$5 == m && / - fuse/ { f = 1 } END { exit !f }' /proc/self/mountinfo
}
if command -v flatpak >/dev/null 2>&1; then
doc="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/doc"
if ! command -v dbus-send >/dev/null 2>&1; then
session_errors="Flatpak apps can't be checked: dbus-send is not installed (package dbus-bin or dbus)"
elif ! out=$(dbus-send --session --print-reply=literal --dest=org.freedesktop.portal.Documents \
/org/freedesktop/portal/documents org.freedesktop.portal.Documents.GetMountPoint 2>&1); then
# Asking also starts xdg-document-portal when it isn't running.
session_errors="Flatpak apps can't start: the document portal (xdg-document-portal) did not answer: $out"
else
i=0
while [ $i -lt 50 ] && ! doc_mounted "$doc"; do sleep 0.1; i=$((i + 1)); done
if ! doc_mounted "$doc"; then
session_errors="Flatpak apps can't start: the document portal answered but nothing is mounted at $doc (see: journalctl --user -b -u xdg-document-portal)"
elif ! ls "$doc/by-app" >/dev/null 2>&1; then
session_errors="Flatpak apps can't start: the document portal's mount at $doc does not answer ($(ls "$doc/by-app" 2>&1 | head -1))"
else
echo "chandra-session: document portal mounted at $doc after $i waits" >>"$log"
fi
fi
[ -n "$session_errors" ] && echo "chandra-session: $session_errors" >>"$log"
fi
export CHANDRA_SESSION_ERRORS="$session_errors"
# Hand the PAM-started gnome-keyring daemon (already unlocked by the login password) its session
# components, so it owns org.freedesktop.secrets on our bus. (oo7-daemon needs none of this: it
# is started by systemd and unlocked over its PAM socket; see docs/keyring.md.) Harmless if it isn't installed,
# already running under systemd's socket units, or not unlocked by PAM (it then prompts once).
if command -v gnome-keyring-daemon >/dev/null 2>&1; then
eval "$(gnome-keyring-daemon --start --components=secrets 2>>"$log")"
[ -n "$SSH_AUTH_SOCK" ] && export SSH_AUTH_SOCK
fi
# A logind session without a seat (made by run0 --user=..., su - or ssh rather than by logging in
# on the VT) can't open the screen and input devices through logind; say so in the log, since
# the compositor's own error ("could not take control of the session") doesn't explain why.
if [ "$backend" = --drm ] && [ -n "$XDG_SESSION_ID" ] && command -v loginctl >/dev/null 2>&1; then
seat=$(loginctl show-session "$XDG_SESSION_ID" -p Seat --value 2>/dev/null)
if [ -z "$seat" ]; then
echo "chandra-session: login session $XDG_SESSION_ID has no seat (started by run0, su or" \
"ssh?); log in on the VT itself, or DRM/input access through logind will fail" >>"$log"
fi
fi
# Password prompts for `chandra-root APP`, run0 and pkexec started from the launcher or a shortcut
# (no terminal to ask in): the compositor starts Chandra's polkit agent
# (/usr/local/lib/chandra/chandra-polkit-agent) and draws its dialog itself (docs/auth-dialog.md).
# If that can't start, a notice on screen says why; nothing else takes over. Only when the user
# turns Settings > Session > "Chandra password dialog" off does the compositor start the first
# third-party agent installed, found here and passed as CHANDRA_POLKIT_FALLBACK.
# CHANDRA_POLKIT_AGENT=none starts no agent at all (say, when a systemd user unit already starts
# one); a path starts that agent instead of Chandra's.
fallback=
for a in \
/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 \
/usr/libexec/polkit-gnome-authentication-agent-1 \
/usr/lib/mate-polkit/polkit-mate-authentication-agent-1 \
/usr/libexec/polkit-mate-authentication-agent-1 \
/usr/lib/x86_64-linux-gnu/polkit-mate/polkit-mate-authentication-agent-1 \
/usr/lib/polkit-kde-authentication-agent-1 \
/usr/libexec/polkit-kde-authentication-agent-1 \
/usr/libexec/kf6/polkit-kde-authentication-agent-1 \
/usr/lib/x86_64-linux-gnu/libexec/polkit-kde-authentication-agent-1 \
/usr/lib/hyprpolkitagent/hyprpolkitagent \
/usr/libexec/hyprpolkitagent \
/usr/libexec/xfce-polkit \
/usr/lib/xfce-polkit/xfce-polkit \
"$(command -v lxpolkit 2>/dev/null)"; do
if [ -n "$a" ] && [ -x "$a" ]; then fallback=$a; break; fi
done
export CHANDRA_POLKIT_FALLBACK="$fallback"
exec "$@" $backend --socket "$sock" \
--theme "$share/config/themes/moon-night.json" >>"$log" 2>&1