fix(desktop): make phoebus-session race-free and reap helpers
The session script started waybar/mako/swaybg before dwl created the Wayland socket and never started the polkit agent. It now starts dwl first, waits for wayland-0, establishes a session bus and runtime dir, starts the polkit authentication agent, and kills helpers when the compositor exits.
This commit is contained in:
parent
e82aaba5fb
commit
24b7259e56
2 changed files with 143 additions and 34 deletions
|
|
@ -329,6 +329,116 @@ pub fn kernel_profile_config(config: &InstallConfig) -> String {
|
||||||
o
|
o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The Wayland session entry point written to `/usr/local/bin/phoebus-session`.
|
||||||
|
///
|
||||||
|
/// Reliability properties (covered by tests):
|
||||||
|
/// * the session bus is established before any client starts;
|
||||||
|
/// * `xdg_runtime_dir` is derived when logind did not provide it;
|
||||||
|
/// * `dwl` starts first and the helper clients wait for the Wayland socket,
|
||||||
|
/// so they never race the compositor;
|
||||||
|
/// * the polkit authentication agent is started;
|
||||||
|
/// * helpers are killed when `dwl` exits, so logout leaves nothing behind.
|
||||||
|
pub const PHOEBUS_SESSION_SCRIPT: &str = r#"#!/bin/bash
|
||||||
|
# Phoebus Wayland session (generated by the installer).
|
||||||
|
#
|
||||||
|
# Start with: phoebus-session
|
||||||
|
#
|
||||||
|
# Hotkeys (dwl defaults):
|
||||||
|
# Mod+Shift+Return terminal Mod+d launcher
|
||||||
|
# Mod+Shift+q close window Mod+j / Mod+k focus next/prev
|
||||||
|
# Mod+1..9 workspace Mod+Shift+e quit dwl
|
||||||
|
|
||||||
|
set -u
|
||||||
|
|
||||||
|
# loginctl normally provides XDG_RUNTIME_DIR. Fall back to the conventional
|
||||||
|
# per-user path so a manual VT start still works.
|
||||||
|
if [ -z "${XDG_RUNTIME_DIR:-}" ]; then
|
||||||
|
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
|
||||||
|
fi
|
||||||
|
if [ ! -d "$XDG_RUNTIME_DIR" ]; then
|
||||||
|
echo "phoebus-session: XDG_RUNTIME_DIR $XDG_RUNTIME_DIR is missing" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
export XDG_SESSION_TYPE=wayland
|
||||||
|
export XDG_CURRENT_DESKTOP=phoebus
|
||||||
|
export XDG_SESSION_DESKTOP=phoebus
|
||||||
|
export MOZ_ENABLE_WAYLAND=1
|
||||||
|
export QT_QPA_PLATFORM=wayland
|
||||||
|
export GDK_BACKEND=wayland,x11
|
||||||
|
export _JAVA_AWT_WM_NONREPARENTING=1
|
||||||
|
|
||||||
|
# Every client must share one session bus. Re-exec once under a fresh bus when
|
||||||
|
# nothing has provided one (e.g. started directly from a VT).
|
||||||
|
if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ] && command -v dbus-run-session >/dev/null 2>&1; then
|
||||||
|
exec dbus-run-session -- "$0" "$@"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Use software rendering when there is no render node (e.g. a VM).
|
||||||
|
if [ -z "${WLR_RENDERER:-}" ] && [ ! -e /dev/dri/renderD128 ]; then
|
||||||
|
export WLR_RENDERER=pixman
|
||||||
|
export WLR_RENDERER_ALLOW_SOFTWARE=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
socket="${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY:-wayland-0}"
|
||||||
|
HELPERS=""
|
||||||
|
|
||||||
|
start_helper() {
|
||||||
|
"$@" >/dev/null 2>&1 &
|
||||||
|
HELPERS="$HELPERS $!"
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
trap - EXIT INT TERM
|
||||||
|
for pid in $HELPERS; do
|
||||||
|
kill "$pid" 2>/dev/null || true
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
trap cleanup EXIT INT TERM
|
||||||
|
|
||||||
|
# dwl creates the Wayland socket. Start it first, then wait for the socket so
|
||||||
|
# helpers do not race the compositor (they used to connect before wayland-0
|
||||||
|
# existed and exit immediately).
|
||||||
|
dwl &
|
||||||
|
DWL_PID=$!
|
||||||
|
|
||||||
|
waited=0
|
||||||
|
while [ ! -S "$socket" ]; do
|
||||||
|
if ! kill -0 "$DWL_PID" 2>/dev/null; then
|
||||||
|
echo "phoebus-session: dwl exited before creating $socket" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ "$waited" -ge 100 ]; then
|
||||||
|
echo "phoebus-session: timed out waiting for $socket" >&2
|
||||||
|
kill "$DWL_PID" 2>/dev/null || true
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
sleep 0.1
|
||||||
|
waited=$((waited + 1))
|
||||||
|
done
|
||||||
|
|
||||||
|
start_helper waybar
|
||||||
|
start_helper mako
|
||||||
|
if [ -n "${PHOEBUS_WALLPAPER:-}" ] && [ -f "$PHOEBUS_WALLPAPER" ]; then
|
||||||
|
start_helper swaybg -i "$PHOEBUS_WALLPAPER"
|
||||||
|
else
|
||||||
|
start_helper swaybg -c '#1e1e2e'
|
||||||
|
fi
|
||||||
|
|
||||||
|
# polkit authentication agent: required for graphical privilege prompts.
|
||||||
|
POLKIT_AGENT="$(command -v polkit-gnome-authentication-agent-1 2>/dev/null \
|
||||||
|
|| echo /usr/libexec/polkit-gnome-authentication-agent-1)"
|
||||||
|
if [ -x "$POLKIT_AGENT" ]; then
|
||||||
|
start_helper "$POLKIT_AGENT"
|
||||||
|
else
|
||||||
|
echo "phoebus-session: polkit agent not found; graphical authentication may fail" >&2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Run until the compositor exits, then the EXIT trap stops the helpers.
|
||||||
|
wait "$DWL_PID"
|
||||||
|
"#;
|
||||||
|
|
||||||
/// Btrfs mount options, matching the proven behavior.
|
/// Btrfs mount options, matching the proven behavior.
|
||||||
pub fn btrfs_mount_opts(compress: &str, ssd: bool) -> String {
|
pub fn btrfs_mount_opts(compress: &str, ssd: bool) -> String {
|
||||||
let mut opts = format!("noatime,compress={compress},space_cache=v2");
|
let mut opts = format!("noatime,compress={compress},space_cache=v2");
|
||||||
|
|
@ -533,6 +643,29 @@ mod tests {
|
||||||
assert_eq!(count, 1);
|
assert_eq!(count, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn session_script_is_race_free_and_cleans_up() {
|
||||||
|
let script = PHOEBUS_SESSION_SCRIPT;
|
||||||
|
// The compositor starts before the clients and the clients wait for
|
||||||
|
// the socket it creates.
|
||||||
|
let dwl = script.find("dwl &").expect("starts dwl in the background");
|
||||||
|
let socket_wait = script
|
||||||
|
.find("while [ ! -S \"$socket\" ]")
|
||||||
|
.expect("waits for the wayland socket");
|
||||||
|
let waybar = script.find("start_helper waybar").expect("starts waybar");
|
||||||
|
assert!(dwl < socket_wait && socket_wait < waybar);
|
||||||
|
// A session bus and runtime dir are established.
|
||||||
|
assert!(script.contains("dbus-run-session"));
|
||||||
|
assert!(script.contains("XDG_RUNTIME_DIR"));
|
||||||
|
// The polkit agent is started when present.
|
||||||
|
assert!(script.contains("POLKIT_AGENT"));
|
||||||
|
// Helpers are reaped when the compositor exits.
|
||||||
|
assert!(script.contains("cleanup()"));
|
||||||
|
assert!(script.contains("trap cleanup EXIT INT TERM"));
|
||||||
|
assert!(script.contains("wait \"$DWL_PID\""));
|
||||||
|
assert!(!script.contains("exec dwl"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn encrypted_cmdline_requires_uuid() {
|
fn encrypted_cmdline_requires_uuid() {
|
||||||
let cfg = InstallConfig::default();
|
let cfg = InstallConfig::default();
|
||||||
|
|
|
||||||
|
|
@ -55,36 +55,7 @@ pub fn write_session_script(exec: &Exec, config: &InstallConfig) -> Result<()> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let target = exec.target();
|
let target = exec.target();
|
||||||
let script = r#"#!/bin/bash
|
let script = sysgen::PHOEBUS_SESSION_SCRIPT;
|
||||||
# Phoebus Wayland session (generated by the installer).
|
|
||||||
#
|
|
||||||
# Start with: phoebus-session
|
|
||||||
#
|
|
||||||
# Hotkeys (dwl defaults):
|
|
||||||
# Mod+Shift+Return terminal Mod+d launcher
|
|
||||||
# Mod+Shift+q close window Mod+j / Mod+k focus next/prev
|
|
||||||
# Mod+1..9 workspace Mod+Shift+e quit dwl
|
|
||||||
|
|
||||||
export XDG_SESSION_TYPE=wayland
|
|
||||||
export XDG_CURRENT_DESKTOP=phoebus
|
|
||||||
export XDG_SESSION_DESKTOP=phoebus
|
|
||||||
|
|
||||||
# Use software rendering when there is no render node (e.g. a VM).
|
|
||||||
if [ -z "${WLR_RENDERER:-}" ] && [ ! -e /dev/dri/renderD128 ]; then
|
|
||||||
export WLR_RENDERER=pixman
|
|
||||||
export WLR_RENDERER_ALLOW_SOFTWARE=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
waybar >/dev/null 2>&1 &
|
|
||||||
mako >/dev/null 2>&1 &
|
|
||||||
if [ -n "${PHOEBUS_WALLPAPER:-}" ] && [ -f "$PHOEBUS_WALLPAPER" ]; then
|
|
||||||
swaybg -i "$PHOEBUS_WALLPAPER" >/dev/null 2>&1 &
|
|
||||||
else
|
|
||||||
swaybg -c '#1e1e2e' >/dev/null 2>&1 &
|
|
||||||
fi
|
|
||||||
|
|
||||||
exec dwl
|
|
||||||
"#;
|
|
||||||
let path = target.resolve("usr/local/bin/phoebus-session")?;
|
let path = target.resolve("usr/local/bin/phoebus-session")?;
|
||||||
target.write("usr/local/bin/phoebus-session", script)?;
|
target.write("usr/local/bin/phoebus-session", script)?;
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
@ -131,7 +102,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn session_script_is_written_and_execs_dwl() {
|
fn session_script_waits_for_the_socket_and_starts_polkit() {
|
||||||
let runner = RecordingRunner::new();
|
let runner = RecordingRunner::new();
|
||||||
let exec = make_exec(runner);
|
let exec = make_exec(runner);
|
||||||
let mut cfg = InstallConfig::default();
|
let mut cfg = InstallConfig::default();
|
||||||
|
|
@ -139,9 +110,14 @@ mod tests {
|
||||||
write_session_script(&exec, &cfg).unwrap();
|
write_session_script(&exec, &cfg).unwrap();
|
||||||
let path = exec.root.join("usr/local/bin/phoebus-session");
|
let path = exec.root.join("usr/local/bin/phoebus-session");
|
||||||
let text = std::fs::read_to_string(&path).unwrap();
|
let text = std::fs::read_to_string(&path).unwrap();
|
||||||
assert!(text.contains("exec dwl"));
|
// dwl is started first and waited on; helpers wait for the socket.
|
||||||
assert!(text.contains("waybar"));
|
assert!(text.contains("dwl &"));
|
||||||
assert!(text.contains("mako"));
|
assert!(text.contains("wait \"$DWL_PID\""));
|
||||||
|
assert!(!text.contains("exec dwl"), "exec would skip helper cleanup");
|
||||||
|
assert!(text.contains("wayland-0"));
|
||||||
|
assert!(text.contains("polkit-gnome-authentication-agent-1"));
|
||||||
|
assert!(text.contains("start_helper waybar"));
|
||||||
|
assert!(text.contains("trap cleanup EXIT INT TERM"));
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
assert_ne!(
|
assert_ne!(
|
||||||
std::fs::metadata(&path).unwrap().permissions().mode() & 0o111,
|
std::fs::metadata(&path).unwrap().permissions().mode() & 0o111,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue