summaryrefslogtreecommitdiff
path: root/package/apcupsd/files/apccontrol
blob: e402c513d3d32202f424eeffec34768b61d3e0fd (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
#!/bin/sh
#
# A custom apccontrol for use in embedded systems: just make sure there's no
# data in-flight and wait for the blackout to shut us down.

# these filesystems are not relevant
IGNORE_FS="tmpfs proc sysfs devtmpfs devpts nfsd"

get_rw_mounts() {
	local excl='\((ro,\|type \('
	local sep=""
	for fs in $IGNORE_FS; do
		excl+="${sep}$fs"
		sep='\|'
	done
	excl+='\)\)'
	mount | grep -v "$excl" | while read dev on mnt opts; do
		echo "$mnt"
	done
}

log() {
	logger -s -t "$(basename $0)" -p daemon.crit "$*"
}
__mount() { # (ro/rw, txt, mnt)
	local opt=$1
	local txt=$2
	local mnt="$3"

	mount -o remount,$opt "$mnt"
	rc=$?
	case $rc in
	0) log "remounted $mnt $txt"
	*) log "failed to remount $mnt $txt: rc=$rc"
	esac
	return $rc
}
mount_ro() {
	__mount ro read-only "$1"
}
mount_rw() {
	__mount rw read-write "$1"
}

romounts="/tmp/apcupsd.romounts"

case "$1" in
	emergency|failing)
		log "UPS error condition happening"
		;& # fall through
	doshutdown)
		log "bracing for upcoming blackout"

		rm -f "$romounts"
		sync
		get_rw_mounts | while read mnt; do
			mount_ro "$mnt" && echo "$mnt" >>"$romounts"
		done
		;;
	mainsback)
		log "returning to routine after near blackout"

		touch "$romounts"
		while read mnt; do
			mount_rw "$mnt"
		done <"$romounts"
		rm "$romounts"
		;;
	*)
		log "Called for $1"
		;;
esac

exit 0