#!/bin/sh # Start the GNOME session as root, with no display manager and no logind: # gnome-session runs the brintos session, gnome-shell on Wayland drawing on the # canvas through the browser's GL transport, and the settings daemons the # session names. Exits 0 once gnome-shell answers on the session bus and leaves # the session running, with the settings daemons let go to start; otherwise ends # the session, prints why in one line, and exits 1. brintos-session-start runs it. GNOME_SESSION=brintos GNOME_AUTOSTART=/usr/share/brintos-gnome-session/autostart GNOME_LOG=/var/log/gnome-session.log GNOME_TIMEOUT=2400 [ -r /etc/default/brintos-session ] && . /etc/default/brintos-session [ -n "$BRINTOS_SESSION_CONF" ] && . "$BRINTOS_SESSION_CONF" # gnome-shell runs headless and presents its virtual monitor on the canvas; # without the GL transport it has no EGL display. [ -c /dev/hwjs-transport ] || { echo "no GL transport: /dev/hwjs-transport is absent"; exit 1; } # /var/log points into /var/volatile, as for the X session. if [ ! -d "${GNOME_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 "${GNOME_LOG%/*}/" ] || GNOME_LOG=/tmp/${GNOME_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 /; the session wants the user's. pw=$(awk -F: -v u="$uid" '$3 == u { print $1 ":" $6 ":" $7; exit }' /etc/passwd) if [ -n "$pw" ]; then export USER="${pw%%:*}" LOGNAME="${pw%%:*}" r=${pw#*:} if [ -z "$HOME" ] || [ "$HOME" = / ]; then h=${r%%:*} [ -n "$h" ] && [ -d "$h" ] && export HOME="$h" fi [ -n "$SHELL" ] || export SHELL="${r#*:}" fi cd "${HOME:-/}" 2>/dev/null || cd / [ -r /etc/profile.d/locale.sh ] && . /etc/profile.d/locale.sh # The session's own environment. The shell picks its own Wayland socket and # tells the session manager, which passes it to what it starts after. unset DISPLAY WAYLAND_DISPLAY SESSION_MANAGER DESKTOP_AUTOSTART_ID GNOME_SESSION_AUTOSTART_DIR export XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=GNOME XDG_SESSION_DESKTOP=$GNOME_SESSION export MUTTER_GLR_PRESENTER=1 # mutter registers the shell with the session manager only once its UI is up. export GNOME_SESSION_PHASE_TIMEOUT=$GNOME_TIMEOUT # The session bus at the standard path, which every client finds without # being told. One that answers is kept; a stale socket is replaced. BUS=$XDG_RUNTIME_DIR/bus export DBUS_SESSION_BUS_ADDRESS=unix:path=$BUS if ! dbus-send --session --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus \ org.freedesktop.DBus.GetId >/dev/null 2>&1; then rm -f "$BUS" dbus-daemon --session --nofork --nopidfile --address="$DBUS_SESSION_BUS_ADDRESS" \ "$GNOME_LOG.bus" 2>&1 & i=0 until [ -S "$BUS" ] || [ $i -ge 60 ]; do sleep 1 i=$((i + 1)) done [ -S "$BUS" ] || { echo "no session bus after 60s: $(tail -n 1 "$GNOME_LOG.bus" 2>/dev/null)"; exit 1; } fi if dbus-send --session --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus \ org.freedesktop.DBus.NameHasOwner string:org.gnome.SessionManager 2>/dev/null | grep -q 'boolean true'; then echo "a GNOME session is already running on $BUS" exit 1 fi why() { l=$(grep -h -E 'Unrecoverable failure|failed to register|CRITICAL|ERROR|[Ff]ail|[Ee]rror|[Cc]annot' "$GNOME_LOG" 2>/dev/null | grep -v -e '^[[:space:]]*$' -e 'hwjs-arena' | tail -n 1) [ -n "$l" ] || l=$(grep -v '^[[:space:]]*$' "$GNOME_LOG" 2>/dev/null | tail -n 1) printf '%s' "$l" | tr -d '\r' | cut -c 1-200 } st=$XDG_RUNTIME_DIR/.gnome-session-exit.$$ pf=$XDG_RUNTIME_DIR/.gnome-session-pid.$$ # gnome-session starts the settings daemons once this exists. up=/run/brintos-gnome-session.up rm -f "$st" "$pf" "$up" # The Wayland sockets present before the start; any other one is the shell's. pre= for s in "$XDG_RUNTIME_DIR"/wayland-*; do [ -S "$s" ] && pre="$pre ${s##*/}" done # session_pids: the processes in session $sid. session_pids() { for d in /proc/[0-9]*; do { read -r l < "$d/stat"; } 2>/dev/null || continue set -- ${l##*) } [ "${4:-}" = "$sid" ] && echo "${d#/proc/}" done } # stop: end gnome-session and everything it started -- its process group, and # what left the group but not the session setsid made -- then remove the # sockets a killed gnome-shell leaves behind, so weston can start after it. # The session bus stays up. stop() { pg=$(cat "$pf" 2>/dev/null) if [ -n "$pg" ]; then kill -TERM -- "-$pg" 2>/dev/null i=0 while [ ! -s "$st" ] && [ $i -lt 15 ]; do sleep 1 i=$((i + 1)) done kill -KILL -- "-$pg" 2>/dev/null sid=$pg i=0 while p=$(session_pids) && [ -n "$p" ] && [ $i -lt 10 ]; do kill -KILL $p 2>/dev/null sleep 1 i=$((i + 1)) done fi for s in "$XDG_RUNTIME_DIR"/wayland-*; do [ -S "$s" ] || continue case " $pre " in *" ${s##*/} "*) continue ;; esac rm -f "$s" "$s.lock" done rm -f "$st" "$pf" "$up" } # gnome-session leads a process group of its own, which stop() ends whole. ( setsid sh -c 'echo $$ > "$1"; shift; exec "$@"' sh "$pf" \ /usr/libexec/gnome-session-binary --session="$GNOME_SESSION" \ --autostart="$GNOME_AUTOSTART" --disable-acceleration-check \ "$GNOME_LOG" 2>&1 echo $? > "$st" ) /dev/null 2>&1 & shell_version() { dbus-send --session --print-reply --reply-timeout=20000 --dest=org.gnome.Shell /org/gnome/Shell \ org.freedesktop.DBus.Properties.Get string:org.gnome.Shell string:ShellVersion 2>/dev/null | sed -n 's/.*string "\(.*\)"/\1/p' } read -r t0 _ < /proc/uptime t0=${t0%.*} while :; do if [ -s "$st" ]; then echo "gnome-session exited with status $(cat "$st"): $(why); log: $GNOME_LOG" rm -f "$st" "$pf" exit 1 fi v=$(shell_version) if [ -n "$v" ]; then read -r t _ < /proc/uptime echo "gnome-session $GNOME_SESSION, gnome-shell $v on the GL transport, up in $(( ${t%.*} - t0 ))s" : > "$up" # brintos-session ends the session through its process group. mv -f "$pf" "$XDG_RUNTIME_DIR/.brintos-gnome-session.pgid" exit 0 fi read -r t _ < /proc/uptime if [ $(( ${t%.*} - t0 )) -ge "$GNOME_TIMEOUT" ]; then echo "gnome-shell did not answer on the session bus after ${GNOME_TIMEOUT}s: $(why); log: $GNOME_LOG" stop exit 1 fi sleep 5 done