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