blob: b74421e21dea21bca7315f4e3473786dcf00eb6c [file] [log] [blame]
#!/bin/sh
#
# Runs poweroff after printing a message to the log, setting the LEDs to red
# to indicate a failure, and waiting for logs to upload.
#
if [ "$#" = 0 ]; then
echo "Usage: $0 <message>" >&2
exit 1
fi
exec >&2
echo "<1>poweroff:" "$@" >/dev/kmsg
if [ -e /config/NO-POWEROFF ]; then
echo "<1>poweroff: disabled by /config/NO-POWEROFF." >/dev/kmsg
exit 0
fi
power_off()
{
QUIET=1 stop harddisk ifplugd wan
QUIET=1 powerdown wan
echo "Powering off."
poweroff
}
timeout()
{
echo "Timed out; powering off immediately."
power_off
}
# This runs just in case we encounter trouble during the next steps.
# This background task is double-nested so that it doesn't count toward
# the 'wait' command below.
trap timeout USR1
pid=$$
( ( sleep 30; kill -USR1 $pid ) & )
: >/tmp/gpio/ledcontrol/halted
echo "Waiting for logs to sync."
(
upload-logs-and-wait
echo "Logs uploaded."
) &
wait
power_off