base-files: send informational UDP message each second waiting
[openwrt/staging/noltari.git] / package / base-files / files / lib / preinit / 30_failsafe_wait
1 #!/bin/sh
2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2010 Vertical Communications
4
5 fs_wait_for_key () {
6 local timeout=$3
7 local timer
8 local do_keypress
9 local keypress_true="$(mktemp)"
10 local keypress_wait="$(mktemp)"
11 local keypress_sec="$(mktemp)"
12 if [ -z "$keypress_wait" ]; then
13 keypress_wait=/tmp/.keypress_wait
14 touch $keypress_wait
15 fi
16 if [ -z "$keypress_true" ]; then
17 keypress_true=/tmp/.keypress_true
18 touch $keypress_true
19 fi
20 if [ -z "$keypress_sec" ]; then
21 keypress_sec=/tmp/.keypress_sec
22 touch $keypress_sec
23 fi
24
25 trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT
26 trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1
27
28 [ -n "$timeout" ] || timeout=1
29 [ $timeout -ge 1 ] || timeout=1
30 timer=$timeout
31 lock $keypress_wait
32 {
33 while [ $timer -gt 0 ]; do
34 pi_failsafe_net_message=true \
35 preinit_net_echo "Please press button now to enter failsafe"
36 echo "$timer" >$keypress_sec
37 timer=$(($timer - 1))
38 sleep 1
39 done
40 lock -u $keypress_wait
41 rm -f $keypress_wait
42 } &
43
44 [ "$pi_preinit_no_failsafe" != "y" ] && echo "Press the [$1] key and hit [enter] $2"
45 echo "Press the [1], [2], [3] or [4] key and hit [enter] to select the debug level"
46 # if we're on the console we wait for input
47 {
48 while [ -r $keypress_wait ]; do
49 timer="$(cat $keypress_sec)"
50
51 [ -n "$timer" ] || timer=1
52 timer="${timer%%\ *}"
53 [ $timer -ge 1 ] || timer=1
54 do_keypress=""
55 {
56 read -t "$timer" do_keypress
57 case "$do_keypress" in
58 $1)
59 echo "true" >$keypress_true
60 ;;
61 1 | 2 | 3 | 4)
62 echo "$do_keypress" >/tmp/debug_level
63 ;;
64 *)
65 continue;
66 ;;
67 esac
68 lock -u $keypress_wait
69 rm -f $keypress_wait
70 }
71 done
72 }
73 lock -w $keypress_wait
74
75 keypressed=1
76 [ "$(cat $keypress_true)" = "true" ] && keypressed=0
77
78 rm -f $keypress_true
79 rm -f $keypress_wait
80 rm -f $keypress_sec
81
82 return $keypressed
83 }
84
85 failsafe_wait() {
86 FAILSAFE=
87 [ "$pi_preinit_no_failsafe" = "y" ] && {
88 fs_wait_for_key "" "" $fs_failsafe_wait_timeout
89 return
90 }
91 grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE
92 if [ "$FAILSAFE" != "true" ]; then
93 fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true
94 [ -f "/tmp/failsafe_button" ] && FAILSAFE=true && echo "- failsafe button "`cat /tmp/failsafe_button`" was pressed -"
95 [ "$FAILSAFE" = "true" ] && export FAILSAFE && touch /tmp/failsafe
96 fi
97 }
98
99 boot_hook_add preinit_main failsafe_wait