summaryrefslogtreecommitdiff
path: root/recipes-core/multitech/reset-handler/reset-handler.sh
blob: f36136e458bcf09569c9680a48682a99f57c34bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash

# Copyright (C) 2014 Multi-Tech Systems

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

PERSISTENT_DIR="/var/persistent/"
ERASE_PERSISTENT="${PERSISTENT_DIR}/mts_do_erase_persistent"

name="reset-handler"
log="logger -t $name -s"
pid="$$"

short_signal=10    # SIGUSR1
long_signal=12     # SIGUSR2
extralong_signal=1 # SIGHUP
power_signal=30    # SIGPWR

do_reboot() {
	$log "Rebooting on button press"
	sleep 1
	reboot
}

do_shutdown() {
        $log "Shutting down"
        shutdown -h now
}

do_restore_defaults() {
	$log "Removing /var/config contents"
	rm -rf /var/config/*
	$log "Setting restore defaults on reboot"
	touch /var/config/force_defaults
	if [[ -x /usr/bin/rmpasswd ]]
	then
		$log "Resetting passwords"
		source rmpasswd
	fi
	if [[ -d ${PERSISTENT_DIR} ]] ; then
        	touch ${ERASE_PERSISTENT}
	fi
	$log "Rebooting"
	sleep 1
	reboot
}

log_exit() {
	$log "Exiting on SIGTERM"
	exit 0
}

idle_wait() {
	pipe=/var/tmp/$name.fifo
	rm -f $pipe
	mkfifo -m 400 $pipe

	# sneaky way to do nothing forever
	while true; do
		read < $pipe
	done
}

trap do_reboot $short_signal
trap do_restore_defaults $long_signal
trap do_restore_defaults $extralong_signal
trap log_exit TERM
trap do_shutdown $power_signal

$log "Enabling reset-monitor for pid $pid"
mts-io-sysfs store reset-monitor "$pid $short_signal $long_signal $extralong_signal"
# set long press to 5 seconds for reset to defaults
mts-io-sysfs store reset-monitor-intervals "5 30"
# set up SIGPWR shutdown
if [[ -f /sys/devices/platform/mts-io/supercap-monitor ]] ; then
    mts-io-sysfs store  supercap-monitor "$pid $power_signal"
fi

# wait for signals
idle_wait