#!/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. Prints which session started # and why: to the console under busybox init, to the journal under systemd. # Run once per boot from /etc/inittab under busybox init, or from /.bashrc # when bash is PID 1; wabuntu-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/wabuntu-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_USER=wabuntu GNOME_START= # The unit that starts the GNOME session, when a display manager starts it. GNOME_DM= WAYLAND_START= X11_START= STAMP=/tmp/.wabuntu-session-started STATE=/tmp/wabuntu-session.log CURRENT=/tmp/wabuntu-session.current CONF=/etc/default/wabuntu-session CMDLINE=/proc/cmdline SESSION_DIRS="/usr/share/wayland-sessions /usr/share/xsessions" USER_DIRS_UPDATE=xdg-user-dirs-update why="the built-in default" [ -r "$CONF" ] && . "$CONF" && why="SESSION=$SESSION in $CONF" if [ -n "$WABUNTU_SESSION_CONF" ]; then . "$WABUNTU_SESSION_CONF" && why="SESSION=$SESSION in $WABUNTU_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 wabuntu-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 wabuntu-session" : > "$STAMP" 2>/dev/null else (set -C; : > "$STAMP") 2>/dev/null || exit 0 fi # The runtime directory every session uses, decided here so wabuntu-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 OWN_RUNTIME_DIR=$XDG_RUNTIME_DIR # own_runtime: the sessions other than GNOME run as the caller, in its runtime directory. own_runtime() { XDG_RUNTIME_DIR=$OWN_RUNTIME_DIR } # gnome_runtime: root starts the GNOME session as GNOME_USER, in that account's # runtime directory, as wabuntu-gnome-session picks it. gnome_runtime() { [ "$(id -u)" = 0 ] && [ -n "$GNOME_USER" ] || return 0 case $XDG_RUNTIME_DIR in /run/user/0 | /tmp/runtime-0) ;; *) return 0 ;; esac _u=$(awk -F: -v n="$GNOME_USER" '$1 == n { print $3; exit }' /etc/passwd) [ -n "$_u" ] && XDG_RUNTIME_DIR=/run/user/$_u } say() { echo "wabuntu-session: $*" >&2 echo "wabuntu-session: $*" >> "$STATE" } # record NAME: the running session, for wabuntu-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-Wabuntu-Exec= in its session file. launcher() { case $1 in gnome) echo "${GNOME_START:-/usr/sbin/wabuntu-gnome-session}" ;; weston) echo "${WAYLAND_START:-/usr/sbin/wabuntu-wayland-start}" ;; icewm) echo "${X11_START:-/usr/sbin/wabuntu-x11-start}" ;; *) f=$(session_file "$1") || return 0 sed -n 's/^X-Wabuntu-Exec=//p' "$f" | head -n 1 ;; esac } # The user's folders -- Documents, Downloads, Music and the rest -- and # ~/.config/user-dirs.dirs, where Tracker and the file chooser look them up: # created once, before the session starts, in the home of the user starting # it; init runs this with HOME=/, and then /etc/passwd names the home. The # GNOME session does it through its own autostart entry. user_dirs() { [ -z "$dirs_done" ] || return 0 dirs_done=1 command -v "$USER_DIRS_UPDATE" >/dev/null 2>&1 || return 0 h=$HOME [ -n "$h" ] && [ "$h" != / ] || h=$(awk -F: -v u="$(id -u)" '$3 == u { print $6; exit }' /etc/passwd) { [ -n "$h" ] && [ "$h" != / ] && [ -d "$h" ]; } || return 0 HOME=$h "$USER_DIRS_UPDATE" >/dev/null 2>&1 || say "$USER_DIRS_UPDATE failed in $h" } x11() { own_runtime user_dirs 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() { own_runtime l=$(launcher weston) [ -x "$l" ] || x11 "$l is not installed" user_dirs 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 [ "$SESSION" = gnome ] && gnome_runtime # Recorded before it is up: a session that does not come up can leave what it # started, and wabuntu-session switch ends that through this record before it # tries again. [ "$SESSION" = none ] || record "$SESSION" case $SESSION in none) rm -f "$CURRENT" say "no session started: $why" exit 0 ;; icewm) x11 "$why" ;; weston) wayland "$why" ;; gnome) if [ -n "$GNOME_DM" ]; then if systemctl start "$GNOME_DM"; then # systemd starts the display manager in a process session of its # own; that one, not this script's, is the running session. _m=$(systemctl show -p MainPID --value "$GNOME_DM" 2>/dev/null) [ "${_m:-0}" -gt 1 ] 2>/dev/null && { sid=$_m; record gnome; } say "started the GNOME session through $GNOME_DM: $why" exit 0 fi say "the GNOME session did not start: $GNOME_DM failed; starting the Wayland session" wayland "the GNOME session did not start" fi 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"