#!/bin/sh # brintos-session list | switch [--wait] NAME | stop [--wait] | default NAME # # list the sessions this image can start; * marks the running # one, and (default) the one a boot starts # switch NAME end the running session and start NAME, in this boot; # when NAME does not come up, start the one that ran before # stop end the running session # default NAME the session a boot starts: SESSION in # /etc/default/brintos-session # # switch and stop work in the background, because the shell they are typed in # may be one the session started; --wait waits for them and prints the result. # NAME is a session file in /usr/share/wayland-sessions or /usr/share/xsessions # without its .desktop suffix; wayland and x11 name weston and icewm. START=/usr/sbin/brintos-session-start CONF=/etc/default/brintos-session STATE=/tmp/brintos-session.log CURRENT=/tmp/brintos-session.current RESULT=/tmp/brintos-session.result STAMPS="/tmp/.brintos-session-started /tmp/.brintos-x11-started" GSD_UP=/run/brintos-gnome-session.up X_LOCKS="/tmp/.X*-lock" X_SOCKETS="/tmp/.X11-unix/X*" SESSION_DIRS="/usr/share/wayland-sessions /usr/share/xsessions" TICK=/tmp/brintos-session.tick STOP_TIMEOUT=15 SWITCH_TIMEOUT=3000 # A switch starts NAME up to SWITCH_TRIES times, waiting SWITCH_SETTLE seconds # after the old session ends, then SWITCH_BACKOFF seconds before the second # try, doubling up to SWITCH_BACKOFF_MAX; then the session that ran before, up # to RESTORE_TRIES times. The memory the old session's processes held can come # back only seconds after they exit. SWITCH_TRIES=6 SWITCH_SETTLE=3 SWITCH_BACKOFF=2 SWITCH_BACKOFF_MAX=30 RESTORE_TRIES=3 [ -n "$BRINTOS_SESSION_CONF" ] && . "$BRINTOS_SESSION_CONF" die() { echo "brintos-session: $*" >&2 exit 1 } # canon NAME: _c is NAME, or the session an older name names. canon() { case $1 in wayland) _c=weston ;; x11) _c=icewm ;; *) _c=$1 ;; esac } # Everything below that a switch runs reads files with the shell's own read, # not sed, cat or tr: a switch from a session that holds nearly all of the # host's memory may not be able to start a process. # ini KEY FILE: _v is the first KEY= value in FILE; fails when it has none. ini() { _v= while IFS= read -r _x || [ -n "$_x" ]; do case $_x in "$1="*) _v=${_x#*=}; return 0 ;; esac done < "$2" return 1 } # launcher NAME FILE: _l is the program that starts session NAME, declared # in FILE; fails when there is none or it is not installed. The three # desktops need their launchers as well as their session files; any other # session needs X-Brintos-Exec= in its file. launcher() { case $1 in gnome) _l=${GNOME_START:-/usr/sbin/brintos-gnome-session} ;; weston) _l=${WAYLAND_START:-/usr/sbin/brintos-wayland-start} ;; icewm) _l=${X11_START:-/usr/sbin/brintos-x11-start} ;; *) ini X-Brintos-Exec "$2"; _l=${_v%% *} ;; esac [ -n "$_l" ] && [ -x "$_l" ] } # sessions: NAMEtitle for every session this image can start. sessions() { seen=" " for d in $SESSION_DIRS; do for f in "$d"/*.desktop; do [ -f "$f" ] || continue n=${f##*/} n=${n%.desktop} case $seen in *" $n "*) continue ;; esac launcher "$n" "$f" || continue seen="$seen$n " ini Name "$f" printf '%s\t%s\n' "$n" "${_v:-$n}" done done } known() { [ "$1" = none ] && return 0 for d in $SESSION_DIRS; do [ -f "$d/$1.desktop" ] && launcher "$1" "$d/$1.desktop" && return 0 done return 1 } # sid_of PID: _sid is the session PID is in. sid_of() { { read -r l < "/proc/$1/stat"; } 2>/dev/null || return 1 set -- ${l##*) } _sid=$4 } # in_sids: PS is the processes in any of SIDS, but this one; a process that # has exited (Z, X) does not count. in_sids() { PS= for s in /proc/[0-9]*/stat; do { read -r l < "$s"; } 2>/dev/null || continue p=${s#/proc/} p=${p%/stat} [ "$p" = $$ ] && continue set -- ${l##*) } case $1 in Z|X) continue ;; esac for x in $SIDS; do [ "$4" = "$x" ] && { PS="$PS $p"; break; } done done return 0 } # running: RUNNING is the running session's NAME when any of its processes is # alive; fails when none is. CURRENT is read, not sourced: the caller's NAME # and SID stay its own. running() { RUNNING= [ -r "$CURRENT" ] || return 1 _n= SIDS= while IFS== read -r _k _x; do case $_k in NAME) _n=$_x ;; SID) SIDS=$_x ;; esac done < "$CURRENT" in_sids [ -n "$PS" ] && RUNNING=$_n [ -n "$RUNNING" ] } # uniq_sids: SIDS without repeats, this process's session and sessions 0 # and 1. uniq_sids() { _u= for s in $SIDS; do { [ "$s" = "$me" ] || [ "$s" -le 1 ]; } 2>/dev/null && continue case " $_u " in *" $s "*) ;; *) _u="$_u $s" ;; esac done SIDS=$_u } # started_by_sids PID: PID, or a process it descends from, is in one of SIDS. started_by_sids() { _p=$1 _n=0 while [ "$_p" -gt 1 ] 2>/dev/null && [ $_n -lt 64 ]; do { read -r l < "/proc/$_p/stat"; } 2>/dev/null || return 1 set -- ${l##*) } for x in $SIDS; do [ "$4" = "$x" ] && return 0 done _p=$2 _n=$((_n + 1)) done return 1 } # x_lock_display LOCK: _d is the display number LOCK is the lock of. x_lock_display() { _d=${1##*/} _d=${_d#.} _d=${_d#X} _d=${_d%-lock} } # stop_session: end the running session and everything it left: its own # session, the GNOME session's, the session bus's and the X servers it # started, each of which can make a session of its own; then the sockets and # stamps they leave. An X server the session did not start, and its lock and # socket, are left alone. stop_session() { [ -r "$CURRENT" ] || return 0 . "$CURRENT" SIDS=$SID [ -n "$XDG_RUNTIME_DIR" ] || XDG_RUNTIME_DIR=/run/user/0 if [ -r "$XDG_RUNTIME_DIR/.brintos-gnome-session.pgid" ]; then while read -r l || [ -n "$l" ]; do SIDS="$SIDS $l" done < "$XDG_RUNTIME_DIR/.brintos-gnome-session.pgid" fi # Only a dbus-daemon's arguments are read, and those need tr. for f in /proc/[0-9]*/comm; do { read -r c < "$f"; } 2>/dev/null || continue case $c in *dbus*) ;; *) continue ;; esac p=${f#/proc/} p=${p%/comm} c=$(tr '\0' ' ' < "/proc/$p/cmdline" 2>/dev/null) case $c in *dbus-daemon*"--session"*"unix:path=$XDG_RUNTIME_DIR/bus"* | *dbus-daemon*"/brintos-session.conf"*"unix:path=$XDG_RUNTIME_DIR/bus"*) sid_of "$p" && SIDS="$SIDS $_sid" ;; esac done me= sid_of $$ && me=$_sid uniq_sids # An X server is the session's when the session started it: its lock # names a process in one of the session's sessions or descended from one. # A lock whose process is gone is stale, and goes with the session's. XL= for xl in $X_LOCKS; do [ -r "$xl" ] || continue { read -r p < "$xl"; } 2>/dev/null || continue if [ ! -d "/proc/$p" ]; then XL="$XL $xl" elif started_by_sids "$p"; then XL="$XL $xl" sid_of "$p" && SIDS="$SIDS $_sid" fi done uniq_sids # Each round signals what is there now: a session manager can still be # starting a client while its server ends. i=0 while in_sids && [ -n "$PS" ] && [ $i -lt "$STOP_TIMEOUT" ]; do kill -TERM $PS 2>/dev/null nap 1 i=$((i + 1)) done i=0 while in_sids && [ -n "$PS" ] && [ $i -lt 5 ]; do kill -KILL $PS 2>/dev/null nap 1 i=$((i + 1)) done # What is left, read once more: a process that was exiting a moment ago # is gone now and does not count. LEFT= in_sids for p in $PS; do { read -r l < "/proc/$p/stat"; } 2>/dev/null || continue c=${l#*(} c=${c%%)*} set -- ${l##*) } LEFT="$LEFT $p($c $1)" done [ -z "$LEFT" ] || return 1 for xl in $XL; do x_lock_display "$xl" rm -f "$xl" "${X_SOCKETS%/*}/X$_d" done rm -f "$XDG_RUNTIME_DIR"/wayland-* "$XDG_RUNTIME_DIR/bus" "$XDG_RUNTIME_DIR/.brintos-gnome-session.pgid" \ $STAMPS "$GSD_UP" "$CURRENT" } # background CMD: run "brintos-session CMD" in a session of its own, so ending # the running session cannot end it; with --wait, wait and print its result. background() { w=$1 shift # Each waiting caller reads its own worker's result, never another's. r=$RESULT [ "$w" = --wait ] && r=$RESULT.wait.$$ rm -f "$r" BRINTOS_SESSION_RESULT=$r /usr/bin/setsid -f "$0" "$@" >"$STATE" 2>&1 if [ "$w" != --wait ]; then echo "brintos-session: $*: working in the background; see $STATE" return 0 fi t=0 until [ -s "$r" ]; do [ $t -ge $(((SWITCH_TRIES + RESTORE_TRIES) * (SWITCH_TIMEOUT + STOP_TIMEOUT + SWITCH_BACKOFF_MAX) + 60)) ] && die "$*: no result after ${t}s; see $STATE" sleep 1 t=$((t + 1)) done read -r rc < "$r" sed 1d "$r" rm -f "$r" return "$rc" } # finish RC LINE: the result background --wait reads. # Written without starting a process, in one write, so a switch that can no # longer fork still reports; then its files are removed. finish() { f=${BRINTOS_SESSION_RESULT:-$RESULT} printf '%s\n%s\n' "$1" "$2" > "$f" echo "brintos-session: $2" > /dev/console 2>/dev/null trap - EXIT rm -f /tmp/brintos-session.switch.$$.* 2>/dev/null exit "$1" } # nap SECONDS: wait without starting a process, on a FIFO nothing writes; a # switch waits while it may not be able to fork. nap() { if [ -p "$TICK" ]; then read -t "$1" _ <> "$TICK" else sleep "$1" fi return 0 } # say_state LINE: a line in the log the background worker writes. say_state() { echo "brintos-session: $*" >> "$STATE" } # last_line PATTERN FILE: _l is the last line of FILE containing PATTERN, # read without starting a process; fails when there is none. last_line() { _l= while IFS= read -r _x || [ -n "$_x" ]; do case $_x in *"$1"*) _l=$_x ;; esac done < "$2" 2>/dev/null [ -n "$_l" ] } # x_socket: an X server's socket is there, other than one in OTHER_X: the # sockets of X servers the session did not start, there before it started. x_socket() { for _s in $X_SOCKETS; do case " $OTHER_X " in *" $_s "*) continue ;; esac [ -e "$_s" ] && return 0 done return 1 } # done_ok LINE: NAME came up; the result says so, and after a failed switch, # that the session that ran before is running again. done_ok() { [ "$TRY" -gt 1 ] && set -- "$1 (try $TRY)" [ "$PHASE" = restore ] && finish 1 "$ALT did not start ($FAILED); $WANT is running again: $1" finish 0 "$1" } # next WHY: this try of NAME failed; try it again, or the session that ran # before, or give up. next() { trap - EXIT say_state "$WANT: try $TRY of $TRIES failed: $1" if [ "$TRY" -lt "$TRIES" ]; then exec "$0" _try "$WANT" "$ALT" $((TRY + 1)) "$PHASE" fi if [ "$PHASE" = switch ] && [ "$ALT" != none ] && [ "$ALT" != "$WANT" ]; then FAILED="$1" export FAILED exec "$0" _try "$ALT" "$WANT" 1 restore fi [ "$PHASE" = restore ] && finish 1 "$ALT did not start ($FAILED), and $WANT did not start again: $1" finish 1 "$WANT did not start after $TRY tries: $1" } cmd=$1 [ $# -gt 0 ] && shift case $cmd in list) running cur=$RUNNING canon "$(. "$CONF" 2>/dev/null; echo "$SESSION")" def=$_c sessions | while IFS=" " read -r n t; do m=" " [ "$n" = "$cur" ] && m="*" d= [ "$n" = "$def" ] && d=" (default)" printf '%s %-10s %s%s\n' "$m" "$n" "$t" "$d" done ;; switch|stop) w= [ "$1" = --wait ] && { w=--wait; shift; } if [ "$cmd" = switch ]; then [ $# -eq 1 ] || die "usage: brintos-session switch [--wait] NAME" canon "$1" n=$_c known "$n" || die "no session '$1'; brintos-session list shows the ones this image has" background "$w" _switch "$n" else background "$w" _stop fi ;; _switch) # Every step after the old session ends is an attempt of its own (_try), # which starts the next one with exec: a shell that cannot fork exits, and # it can neither fork nor sleep its way to a retry, but its exit trap can # exec, which needs no new process. [ -p "$TICK" ] || mkfifo "$TICK" 2>/dev/null running exec "$0" _try "$1" "${RUNNING:-none}" 1 switch ;; _try) # _try WANT ALT TRY PHASE: start WANT, the TRYth time. PHASE switch: WANT # was asked for and ALT ran before it; restore: WANT ran before and ALT # did not come up. Not NAME: stop_session reads the running one's. WANT=$1 ALT=$2 TRY=$3 PHASE=$4 TRIES=$SWITCH_TRIES [ "$PHASE" = restore ] && TRIES=$RESTORE_TRIES trap 'next "the switch could not start a process"' EXIT # What ran before can take longer to end than one stop gives it: a # process with many threads ends slowly. The next try stops it again. stop_session || next "what ran before did not end:$LEFT still running" OTHER_X= for _s in $X_SOCKETS; do [ -e "$_s" ] && OTHER_X="$OTHER_X $_s" done if [ "$PHASE$TRY" = switch1 ]; then nap "$SWITCH_SETTLE" else d=$SWITCH_BACKOFF i=2 while [ $i -lt "$TRY" ] && [ $d -lt "$SWITCH_BACKOFF_MAX" ]; do d=$((d * 2)) i=$((i + 1)) done [ $d -gt "$SWITCH_BACKOFF_MAX" ] && d=$SWITCH_BACKOFF_MAX nap "$d" fi say_state "starting $WANT (try $TRY of $TRIES)" # The X session's launcher runs for as long as the X server does, so its # start is the server's socket appearing, not the launcher returning. # The status file, not kill -0: an exited child stays a zombie until # waited for, and kill -0 succeeds on a zombie. # A file per try: a start that a later try ended still writes its status. out=/tmp/brintos-session.switch.$$.$PHASE$TRY : > "$out" : > "$out.rc" ( "$START" "$WANT" >"$out" 2>&1; echo $? > "$out.rc" ) /dev/null || [ -z "$rc" ]; do if last_line "starting the X11 session" "$out" && x_socket; then trap - EXIT done_ok "started the X11 session (${_l#*starting the X11 session: })" fi [ $t -ge "$SWITCH_TIMEOUT" ] && next "$WANT did not come up after ${t}s" nap 1 t=$((t + 1)) done _l= last_line "" "$out" line=${_l#brintos-session: } if [ "$rc" = 0 ]; then trap - EXIT done_ok "$line" fi next "${line:-$WANT exited with status $rc}" ;; _stop) running n=$RUNNING stop_session || finish 1 "the running session did not end:$LEFT still running" finish 0 "stopped ${n:-no session}" ;; default) [ $# -eq 1 ] || die "usage: brintos-session default NAME" canon "$1" n=$_c known "$n" || die "no session '$1'; brintos-session list shows the ones this image has" if grep -q '^SESSION=' "$CONF" 2>/dev/null; then sed -i "s/^SESSION=.*/SESSION=$n/" "$CONF" || die "cannot write $CONF" else echo "SESSION=$n" >> "$CONF" || die "cannot write $CONF" fi sync grep -qx "SESSION=$n" "$CONF" || die "SESSION=$n did not reach $CONF" echo "brintos-session: $n starts at the next boot" ;; *) sed -n '2,16s/^# \{0,1\}//p' "$0" >&2 exit 2 ;; esac