Skip to content

Commit 5fa9d1c

Browse files
authored
Fix XFCE execution for non-root local users via xfce-start (#3)
* ​Fix XFCE execution and DBus panics for non-root local users via xfce-start ### Description **The Problem:** Setting XFCE_USER to a non-root user fails to initialize the desktop session due to a cascade of three conflicts: 1. **Socket Access Denial:** startxfce4 triggers a Permission denied error because the X11 and PulseAudio sockets are strictly root-owned. 2. **VirGL Crash (Signal 134):** Dropping privileges via su -l strips essential container environment variables. This severs the VirGL/Termux-X11 bridge and immediately aborts the process. 3. **DBus Panics:** Using su -p to preserve graphics variables inadvertently retains HOME=/root and XDG_RUNTIME_DIR=/run/user/0. Lacking write access to /root/, the standard user triggers severe GLib-CRITICAL DBus errors and fails to build the desktop. **The Solution:** This patch refactors /usr/local/bin/xfce-start to securely transition the execution environment without breaking hardware acceleration bridges: * **Targeted Socket Handoff:** Explicitly assigns ownership (chown) of the X11, PulseAudio, VirGL sockets, and .X5-lock file to the target user. * **Environment Preservation:** Transitions from su -l to su -p to retain vital container graphics and audio variables. * **Dynamic Path Override:** Intercepts and dynamically rewrites $HOME and $XDG_RUNTIME_DIR based on the target UID, ensuring a clean DBus initialization isolated from /root. * **Code Cleanup (Whitelist Deprecation):** Because su -p natively preserves the required container environment, explicitly building and passing a $WHITELIST string via the -w flag is no longer necessary. The obsolete sed parsing logic for /run/droidspaces.env has been safely removed to streamline the script. * **Zero Regressions:** Fully preserves the default root fallback path for deployments not utilizing XFCE_USER. ### Tested Environments **Android Target:** * **Device:** Samsung Galaxy S21+ * **SoC:** Exynos 2100 * **Android Version:** 16 * **Kernel Version:** 5.4.302-Floppy-v1.1.2-KN-g9719914d4da2 **Linux Target (Container):** * **Distribution:** Debian 13 (Trixie) * **Architecture:** ARM64 * **Init System:** systemd ### Regression Check * [X] Default root execution path functions without modification. * [X] XFCE launches successfully as root when XFCE_USER is empty or unset. * [X] No modern/restricted kernel syscalls are utilized (fully 3.10 compliant). * Clean up comment in xfce-start script Removed unnecessary comment about whitelist in environment variable parsing. * Using su -l with dynamic whitelist * XDG_RUNTIME_DIR initialization - Creates and chmods `/run/user/$(id -u)` (mode `0700`) inside the `su` subshell for non-root users, and `/run/user/0` for the root fallback, satisfying the XDG Base Directory Specification and preventing DBus/XFCE session errors. * ## Improve XDG_RUNTIME_DIR handling in xfce-start USER_UID and TARGET_XDG resolved before su chown "$XFCE_USER" (owner only, no group assumption) TARGET_XDG interpolated into the subshell command string rather than relying on id -u inside it mkdir/chmod moved before socket handoff for logical ordering
1 parent 0d2b65d commit 5fa9d1c

1 file changed

Lines changed: 36 additions & 6 deletions

File tree

scripts/xfce-start

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,54 @@
33
ENV_FILE=/run/droidspaces.env
44
CONFIG=/run/droidspaces/container.config
55

6+
# 1. Parse Initial Environment Variables
67
if [ -f "$ENV_FILE" ]; then
78
. "$ENV_FILE"
8-
WHITELIST=$(sed -n 's/^export \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$ENV_FILE" | tr '\n' ',' | sed 's/,$//')
99
else
1010
export DISPLAY=:5
11-
WHITELIST=DISPLAY
1211
if grep -q 'enable_pulseaudio=1' "$CONFIG" 2>/dev/null; then
1312
export PULSE_SERVER=unix:/tmp/.pulse-socket
14-
WHITELIST="$WHITELIST,PULSE_SERVER"
1513
fi
1614
if grep -q 'enable_virgl=1' "$CONFIG" 2>/dev/null; then
1715
export GALLIUM_DRIVER=virpipe
18-
WHITELIST="$WHITELIST,GALLIUM_DRIVER"
1916
fi
2017
fi
2118

22-
if [ -n "$XFCE_USER" ]; then
23-
exec su -l -w "$WHITELIST" "$XFCE_USER" -c 'exec /usr/bin/startxfce4'
19+
# 2. Check User and Launch Desktop
20+
if [ -n "$XFCE_USER" ] && [ "$XFCE_USER" != "root" ]; then
21+
22+
# Fetch the target user's numeric UID and prepare XDG runtime dir
23+
USER_UID=$(id -u "$XFCE_USER")
24+
TARGET_XDG="/run/user/$USER_UID"
25+
26+
# Unconditionally create/heal the runtime directory and force correct permissions
27+
mkdir -p "$TARGET_XDG"
28+
chown "$XFCE_USER" "$TARGET_XDG"
29+
chmod 700 "$TARGET_XDG"
30+
31+
# Grant ownership of sockets and X11 lock file to the target user
32+
[ -S /tmp/.X11-unix/X5 ] && chown "$XFCE_USER" /tmp/.X11-unix/X5
33+
[ -f /tmp/.X5-lock ] && chown "$XFCE_USER" /tmp/.X5-lock
34+
[ -S /tmp/.pulse-socket ] && chown "$XFCE_USER" /tmp/.pulse-socket
35+
[ -S /tmp/.virgl_test ] && chown "$XFCE_USER" /tmp/.virgl_test
36+
37+
# Build dynamic env whitelist, excluding root-specific or session-poisoning vars
38+
SAFE_ENV=$(env | awk -F= '{print $1}' \
39+
| grep -vE '^(HOME|USER|LOGNAME|PWD|SHELL|XDG_RUNTIME_DIR|MAIL|SHLVL|_)$' \
40+
| tr '\n' ',' | sed 's/,$//')
41+
42+
# Drop privileges and launch XFCE under a clean login shell
43+
exec su -l -w "$SAFE_ENV" "$XFCE_USER" -c '
44+
export XDG_RUNTIME_DIR='"$TARGET_XDG"'
45+
exec /usr/bin/startxfce4'
2446
else
47+
# Warn and fall back to root execution
48+
if [ "$XFCE_USER" = "root" ]; then
49+
echo "Warning: XFCE_USER=root, running desktop as root" >&2
50+
fi
51+
52+
mkdir -p /run/user/0
53+
chmod 700 /run/user/0
54+
export XDG_RUNTIME_DIR=/run/user/0
2555
exec /usr/bin/startxfce4
2656
fi

0 commit comments

Comments
 (0)