diff --git a/board/common/overlay/etc/init.d/base b/board/common/overlay/etc/init.d/base index ef900a8955..7b08c9384d 100755 --- a/board/common/overlay/etc/init.d/base +++ b/board/common/overlay/etc/init.d/base @@ -6,6 +6,8 @@ board_name=$(cat /etc/board) test -n "$os_debug" || source /etc/init.d/conf +source /etc/init.d/panic + msg_begin() { echo -n " * $1: " } @@ -22,8 +24,3 @@ msg_background() { test -n "$1" && echo $1 || echo "pending" } -panic_action() { - logger -t panic -s "rebooting" - /sbin/reboot -} - diff --git a/board/common/overlay/etc/init.d/panic b/board/common/overlay/etc/init.d/panic new file mode 100755 index 0000000000..e0d288d0d6 --- /dev/null +++ b/board/common/overlay/etc/init.d/panic @@ -0,0 +1,28 @@ +#!/bin/bash + +_PANIC_COUNTER_FILE="/var/lib/panic_counter" +_PANIC_REBOOT_DELAY_FACTOR=10 +_PANIC_REBOOT_DELAY_MAX=3600 # reboot at least once an hour in case of panic + + +panic_action() { + # read counter from file + panic_counter=$(cat ${_PANIC_COUNTER_FILE} 2>/dev/null || echo 0) + + # write increased counter back to file + echo $((panic_counter + 1)) > ${_PANIC_COUNTER_FILE} + + delay=$((_PANIC_REBOOT_DELAY_FACTOR * panic_counter)) + if [ "${delay}" -gt "${_PANIC_REBOOT_DELAY_MAX}" ]; then + delay=${_PANIC_REBOOT_DELAY_MAX} + fi + + if [ "${delay}" -gt 0 ]; then + logger -t panic -s "rebooting in ${delay} seconds (caused by $1)" + sleep ${delay} + fi + + logger -t panic -s "rebooting (caused by $1)" + /sbin/reboot +} + diff --git a/board/common/overlay/sbin/reboot b/board/common/overlay/sbin/reboot index 5278c19d34..3b214bdd85 100755 --- a/board/common/overlay/sbin/reboot +++ b/board/common/overlay/sbin/reboot @@ -1,7 +1,15 @@ #!/bin/bash +# carry on with the script in case of error set +e + +# write buffers to disk /bin/sync -(sleep 10 && /usr/bin/killall -STOP watchdog) & + +# allow the shutdown script 20 seconds to shut down, +# after which we stop feeding the watchdog +(sleep 20 && /usr/bin/killall -STOP watchdog) & + +# actual reboot command /bin/busybox reboot