dot/xinitrc: Restart window manager if it crashes.
authorMark Wooding <mwooding@good.com>
Fri, 13 Dec 2013 19:03:05 +0000 (19:03 +0000)
committerMark Wooding <mwooding@good.com>
Fri, 13 Dec 2013 19:08:14 +0000 (19:08 +0000)
For some reason, e16 sometimes crashes and leaves the input focus in a
broken state.  Restarting the window manager, e.g., from a console,
makes everything work again, but it's annoying.

Introduce a new function `manage' for restarting programs if they crash,
and use it to run the window manager.

dot/xinitrc

index 210a76d..7aba2b9 100755 (executable)
@@ -25,6 +25,33 @@ run () {
   esac
 }
 
+manage () {
+  local when=$(date +%s) now
+  local fail=0 rc report
+
+  while :; do
+    "$@"; rc=$?
+    case $rc in
+      0) info "manage $1: successful exit"; break ;;
+      143) info "manage $1: terminated"; break ;;
+    esac
+    now=$(date +%s)
+    report="rc = $rc"
+    if (( $now - $when > 5 )); then
+      fail=0
+    else
+      report="$report, early failure"
+      fail=$(( $fail + 1 ))
+      if (( $fail >= 5 )); then
+       info "manage $1: exit ($report), giving up after $fail failures"
+       break
+      fi
+    fi
+    info "manage $1: exit ($report), restarting"
+    when=$now
+  done
+}
+
 ## Program choice
 pick_program () {
   local what=$1; shift
@@ -117,7 +144,7 @@ case "$wm,$vnc" in
 esac
 
 start-e16 () {
-  run bginit $wm $wmopts
+  run bginit manage $wm $wmopts
   win=nil
   for i in $(seq 10); do
     sleep 1
@@ -143,7 +170,7 @@ start-window-manager () {
       start-$wm $wmopts
       ;;
     *)
-      run bginit $wm $wmopts
+      run bginit manage $wm $wmopts
       ;;
   esac
 }