#!/bin/sh # Start a desktop session: GNOME, Weston or IceWM, or another session a # package declares. When the session chosen does not come up, the next one in # the order gnome, weston, icewm starts instead. Says on the console which # session started and why. Run once per boot from /etc/inittab under busybox # init, or from /.bashrc when bash is PID 1; brintos-session runs it with a # session name to switch sessions. # # The choice: the NAME argument; else brintos.session= on the kernel # command line; else SESSION in /etc/default/brintos-session. A name is a # session file in /usr/share/wayland-sessions or /usr/share/xsessions without # its .desktop suffix, or none. wayland and x11 are the older names of weston # and icewm. ARG=$1 SESSION=wayland GNOME_START= WAYLAND_START= X11_START= STAMP=/tmp/.brintos-session-started STATE=/tmp/brintos-session.log CURRENT=/tmp/brintos-session.current CONF=/etc/default/brintos-session CMDLINE=/proc/cmdline SESSION_DIRS="/usr/share/wayland-sessions /usr/share/xsessions" why="the built-in default" [ -r "$CONF" ] && . "$CONF" && why="SESSION=$SESSION in $CONF" if [ -n "$BRINTOS_SESSION_CONF" ]; then . "$BRINTOS_SESSION_CONF" && why="SESSION=$SESSION in $BRINTOS_SESSION_CONF" fi for x in $(cat "$CMDLINE" 2>/dev/null); do case $x in brintos.session=*) SESSION=${x#*=} why="brintos.session=$SESSION on the kernel command line" ;; esac done # The session runs in a session of its own, so brintos-session can end it # whole without reaching the shell or the init that started it. read -r l < /proc/self/stat set -- ${l##*) } [ "$4" = $$ ] || exec /usr/bin/setsid -w "$0" ${ARG:+"$ARG"} sid=$$ if [ -n "$ARG" ]; then SESSION=$ARG why="asked for by brintos-session" : > "$STAMP" 2>/dev/null else (set -C; : > "$STAMP") 2>/dev/null || exit 0 fi # The runtime directory every session uses, decided here so brintos-session # knows where its sockets are. if [ -z "$XDG_RUNTIME_DIR" ]; then XDG_RUNTIME_DIR=/run/user/$(id -u) mkdir -p "$XDG_RUNTIME_DIR" 2>/dev/null || XDG_RUNTIME_DIR=/tmp/runtime-$(id -u) fi export XDG_RUNTIME_DIR say() { echo "brintos-session: $*" >&2 echo "brintos-session: $*" >> "$STATE" } # record NAME: the running session, for brintos-session list and switch. record() { printf 'NAME=%s\nSID=%s\nXDG_RUNTIME_DIR=%s\n' "$1" "$sid" "$XDG_RUNTIME_DIR" > "$CURRENT" } # session_file NAME: the session file that declares NAME. session_file() { for d in $SESSION_DIRS; do [ -f "$d/$1.desktop" ] && { echo "$d/$1.desktop"; return 0; } done return 1 } # launcher NAME: the program that starts NAME. The three desktops have their # own: the Exec= of their session files is what a display manager runs once it # has set up what these set up themselves. Any other session names its # launcher with X-Brintos-Exec= in its session file. launcher() { case $1 in gnome) echo "${GNOME_START:-/usr/sbin/brintos-gnome-session}" ;; weston) echo "${WAYLAND_START:-/usr/sbin/brintos-wayland-start}" ;; icewm) echo "${X11_START:-/usr/sbin/brintos-x11-start}" ;; *) f=$(session_file "$1") || return 0 sed -n 's/^X-Brintos-Exec=//p' "$f" | head -n 1 ;; esac } x11() { l=$(launcher icewm) if [ ! -x "$l" ]; then rm -f "$CURRENT" say "no session started: $1, and $l is not installed" exit 1 fi say "starting the X11 session: $1" record icewm exec "$l" } wayland() { l=$(launcher weston) [ -x "$l" ] || x11 "$l is not installed" if out=$("$l" 2>&1); then record weston say "started the Wayland session (weston; X11 clients through Xwayland): $1" exit 0 fi x11 "the Wayland compositor did not start: $(printf '%s' "$out" | tail -n 1)" } case $SESSION in wayland) SESSION=weston ;; x11) SESSION=icewm ;; esac case $SESSION in none) rm -f "$CURRENT" say "no session started: $why" exit 0 ;; icewm) x11 "$why" ;; weston) wayland "$why" ;; gnome) l=$(launcher gnome) if [ ! -x "$l" ]; then say "the GNOME session did not start: $l is not installed; starting the Wayland session" elif out=$("$l" 2>&1); then record gnome say "started the GNOME session ($(printf '%s' "$out" | tail -n 1)): $why" exit 0 else say "the GNOME session did not start: $(printf '%s' "$out" | tail -n 1); starting the Wayland session" fi wayland "the GNOME session did not start" ;; esac l=$(launcher "$SESSION") [ -n "$l" ] || x11 "unknown session '$SESSION' ($why)" if [ ! -x "${l%% *}" ]; then say "the $SESSION session did not start: ${l%% *} is not installed; starting the Wayland session" elif out=$(sh -c "$l" 2>&1); then record "$SESSION" say "started the $SESSION session ($(printf '%s' "$out" | tail -n 1)): $why" exit 0 else say "the $SESSION session did not start: $(printf '%s' "$out" | tail -n 1); starting the Wayland session" fi wayland "the $SESSION session did not start"