rpcd: iwinfo plugin fixes
[openwrt.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                         echo "$timer" >$keypress_sec
35                         timer=$(($timer - 1))
36                         sleep 1
37                 done
38                 lock -u $keypress_wait
39                 rm -f $keypress_wait
40         } &
41
42         [ "$pi_preinit_no_failsafe" != "y" ] && echo "Press the [$1] key and hit [enter] $2"
43         echo "Press the [1], [2], [3] or [4] key and hit [enter] to select the debug level"
44         # if we're on the console we wait for input
45         {
46                 while [ -r $keypress_wait ]; do
47                         timer="$(cat $keypress_sec)"
48
49                         [ -n "$timer" ] || timer=1
50                         timer="${timer%%\ *}"
51                         [ $timer -ge 1 ] || timer=1
52                         do_keypress=""
53                         {
54                                 read -t "$timer" do_keypress
55                                 case "$do_keypress" in
56                                 $1)
57                                         echo "true" >$keypress_true
58                                         ;;
59                                 1 | 2 | 3 | 4)
60                                         echo "$do_keypress" >/tmp/debug_level
61                                         ;;
62                                 *)
63                                         continue;
64                                         ;;
65                                 esac
66                                 lock -u $keypress_wait
67                                 rm -f $keypress_wait
68                         }
69                 done
70         }
71         lock -w $keypress_wait
72
73         keypressed=1
74         [ "$(cat $keypress_true)" = "true" ] && keypressed=0
75
76         rm -f $keypress_true
77         rm -f $keypress_wait
78         rm -f $keypress_sec
79
80         return $keypressed
81 }
82
83 failsafe_wait() {
84         FAILSAFE=
85         [ "$pi_preinit_no_failsafe" == "y" ] && {
86                 fs_wait_for_key "" "" $fs_failsafe_wait_timeout
87                 return
88         }
89         grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE
90         if [ "$FAILSAFE" != "true" ]; then
91                 pi_failsafe_net_message=true
92                 preinit_net_echo "Please press button now to enter failsafe"
93                 pi_failsafe_net_message=false
94                 fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true
95                 [ -f "/tmp/failsafe_button" ] && FAILSAFE=true && echo "- failsafe button "`cat /tmp/failsafe_button`" was pressed -"
96                 [ "$FAILSAFE" = "true" ] && export FAILSAFE && touch /tmp/failsafe
97         fi
98 }
99
100 boot_hook_add preinit_main failsafe_wait