#!/bin/sh # Start weston with Xwayland for X11 clients: on the browser's GL transport # (the glr backend, Weston's GL renderer drawing through WebGL2) when the host # grants it, otherwise on the KMS device with pixman. Exits 0 once the # compositor's socket is up and leaves it running; otherwise prints why, in one # line, and exits 1. brintos-session-start runs it. WESTON_SOCKET=wayland-0 WESTON_LOG=/var/log/weston.log WESTON_ARGS= WESTON_BACKEND=auto WAYLAND_TIMEOUT=180 [ -r /etc/default/brintos-session ] && . /etc/default/brintos-session [ -n "$BRINTOS_SESSION_CONF" ] && . "$BRINTOS_SESSION_CONF" # /var/log points into /var/volatile, as for the X session. if [ ! -d "${WESTON_LOG%/*}/" ]; then mount -t tmpfs -o mode=0755,nodev,nosuid tmpfs /var/volatile 2>/dev/null && mkdir -p /var/volatile/log /var/volatile/tmp [ -d "${WESTON_LOG%/*}/" ] || WESTON_LOG=/tmp/${WESTON_LOG##*/} fi uid=$(id -u) if [ -z "$XDG_RUNTIME_DIR" ]; then XDG_RUNTIME_DIR=/run/user/$uid mkdir -p "$XDG_RUNTIME_DIR" 2>/dev/null || XDG_RUNTIME_DIR=/tmp/runtime-$uid fi mkdir -p "$XDG_RUNTIME_DIR" && chmod 0700 "$XDG_RUNTIME_DIR" || { echo "cannot create XDG_RUNTIME_DIR $XDG_RUNTIME_DIR"; exit 1; } export XDG_RUNTIME_DIR # The kernel starts init with HOME=/ in /, and the session inherits both. The # clients weston starts want the user's: they inherit its working directory as # well as HOME, and a file manager started with no argument opens the former. pw=$(awk -F: -v u="$uid" '$3 == u { print $1 ":" $6; exit }' /etc/passwd) if [ -n "$pw" ]; then export USER="${pw%%:*}" LOGNAME="${pw%%:*}" if [ -z "$HOME" ] || [ "$HOME" = / ]; then h=${pw#*:} [ -n "$h" ] && [ -d "$h" ] && export HOME="$h" fi fi cd "${HOME:-/}" 2>/dev/null || cd / [ -S "$XDG_RUNTIME_DIR/$WESTON_SOCKET" ] && { echo "$XDG_RUNTIME_DIR/$WESTON_SOCKET already exists: a compositor is running"; exit 1; } # The compositor's socket by name, for everything weston starts and what they # start in turn: a client weston launches gets a connected socket instead, and # a shell in weston-terminal or a program from the panel inherits neither. # weston is always given --backend, so it does not read this as a parent # compositor to nest in. export WAYLAND_DISPLAY=$WESTON_SOCKET mkdir -p /tmp/.X11-unix && chmod 1777 /tmp/.X11-unix # The image's locale, as a login shell gets it: init starts this script with # none. The session bus a client starts inherits that client's environment, and # every service the bus activates inherits the bus's; a GLib service that checks # the codeset (gnome-terminal-server) exits on ASCII. The X session has it from # /etc/profile, which its server script reads. [ -r /etc/profile.d/locale.sh ] && . /etc/profile.d/locale.sh # GTK programs the session starts skip the accessibility bridge, as in the X # session: the bridge asks D-Bus to start at-spi-bus-launcher, which aborts. # BRINTOS-WORKAROUND: brintos/hardwarejs#62 remove -- a dynamically linked program is refused every thread, so the accessibility bus launcher cannot run export NO_AT_BRIDGE=1 # GTK programs the session starts (pcmanfm, l3afpad) and those started from its # terminals try X11 first, so GTK3 draws through Xwayland: a GTK3 client on # Wayland can freeze the guest. GTK4 is built with no X11 backend and takes # the next one, Wayland. A program that sets GDK_BACKEND itself gets its own. # BRINTOS-WORKAROUND: brintos/glibc#25 revisit -- GLib threads in a GTK3 Wayland client park in a raw FUTEX_WAIT and every guest-side writer stops export GDK_BACKEND=x11,wayland # weston reads one file: the sessions is the base file plus every packages # fragment in weston.ini.d, in name order. set -- /etc/xdg/weston/weston.ini.d/*.ini if [ -e "$1" ]; then for f in /etc/xdg/weston/weston.ini "$@"; do cat "$f"; echo; done >"$XDG_RUNTIME_DIR/weston.ini" && WESTON_ARGS="--config=$XDG_RUNTIME_DIR/weston.ini $WESTON_ARGS" fi why() { l=$(grep -h -i -E 'fatal|fail|error|cannot|couldn' "$WESTON_LOG" "$WESTON_LOG.out" 2>/dev/null | grep -v '^[[:space:]]*$' | tail -n 1) [ -n "$l" ] || l=$(cat "$WESTON_LOG" "$WESTON_LOG.out" 2>/dev/null | grep -v '^[[:space:]]*$' | tail -n 1) printf '%s' "$l" | tr -d '\r' | cut -c 1-200 } # kids PID: the pids whose parent is PID, read without starting a process. kids() { for s in /proc/[0-9]*/stat; do { read -r l < "$s"; } 2>/dev/null || continue r=${l##*) } pp=${r#* } pp=${pp%% *} if [ "$pp" = "$1" ]; then s=${s#/proc/} echo "${s%/stat}" fi done } # stop: end the weston start() timed out on, and the clients it started, so # the next backend is not started beside it on the same seat and device. stop() { pid=$(cat "$pf" 2>/dev/null) if [ -n "$pid" ]; then k=$(kids "$pid") kill -TERM "$pid" $k 2>/dev/null i=0 while [ ! -s "$st" ] && [ $i -lt 10 ]; do sleep 1 i=$((i + 1)) done if [ ! -s "$st" ]; then kill -KILL "$pid" $k 2>/dev/null i=0 while [ ! -s "$st" ] && [ $i -lt 5 ]; do sleep 1 i=$((i + 1)) done fi [ -s "$st" ] && rm -f "$XDG_RUNTIME_DIR/$WESTON_SOCKET" "$XDG_RUNTIME_DIR/$WESTON_SOCKET.lock" fi rm -f "$st" "$pf" } # start_once BACKEND RENDERER: start weston and wait for its socket. 0 once it is # up; 1, with the reason on stdout, when weston exits or the time runs out. start_once() { # The status file, not kill -0: an exited child stays a zombie until # waited for, and kill -0 succeeds on a zombie. st=$XDG_RUNTIME_DIR/.weston-exit.$$ pf=$XDG_RUNTIME_DIR/.weston-pid.$$ rm -f "$st" "$pf" ( LIBSEAT_BACKEND=builtin weston --backend="$1" --renderer="$2" --shell=desktop \ --xwayland --socket="$WESTON_SOCKET" --idle-time=0 --log="$WESTON_LOG" \ $WESTON_ARGS "$WESTON_LOG.out" 2>&1 & echo $! > "$pf" wait $! echo $? > "$st" ) /dev/null 2>&1 & read -r t0 _ < /proc/uptime t0=${t0%.*} while :; do [ -S "$XDG_RUNTIME_DIR/$WESTON_SOCKET" ] && { rm -f "$pf"; return 0; } if [ -s "$st" ]; then echo "weston ($1) exited with status $(cat "$st"): $(why); log: $WESTON_LOG" rm -f "$st" "$pf" return 1 fi read -r t _ < /proc/uptime if [ $(( ${t%.*} - t0 )) -ge "$WAYLAND_TIMEOUT" ]; then echo "no compositor socket after ${WAYLAND_TIMEOUT}s ($1): $(why); log: $WESTON_LOG" stop return 1 fi sleep 1 done } # start BACKEND RENDERER: start_once, and once more when weston could not be # executed at all (status 126). # BRINTOS-WORKAROUND: brintos/linux-in-the-browser#238 remove -- one failed blob fetch reaches the guest as EIO, so an exec fails start() { r=$(start_once "$1" "$2") && return 0 case $r in *"exited with status 126:"*) start_once "$1" "$2" ;; *) echo "$r" return 1 ;; esac } # The GL transport exists only when the host grants it; the glr backend # refuses to start when its GL renderer cannot (eglInitialize fails), and the # DRM backend with pixman takes over. glr_why= case $WESTON_BACKEND in auto|glr) if [ -c /dev/hwjs-transport ]; then if out=$(start glr gl); then echo "weston on the GL transport (glr backend, GL renderer)" exit 0 fi glr_why="glr: $out; " mv -f "$WESTON_LOG" "$WESTON_LOG.glr" 2>/dev/null mv -f "$WESTON_LOG.out" "$WESTON_LOG.glr.out" 2>/dev/null else glr_why="no GL transport: /dev/hwjs-transport is absent; " fi [ "$WESTON_BACKEND" = glr ] && { echo "$glr_why" | sed 's/; $//'; exit 1; } ;; esac [ -c /dev/dri/card0 ] || { echo "${glr_why}no KMS device: /dev/dri/card0 is absent"; exit 1; } if out=$(start drm pixman); then echo "weston on the KMS device (drm backend, pixman)${glr_why:+; $glr_why}" | sed 's/; $//' exit 0 fi echo "${glr_why}$out" exit 1