summaryrefslogtreecommitdiffstats
path: root/core/modules/idleaction/data/opt/openslx/scripts/idleaction-scheduled_action
blob: 38be4f7911a3537fb0a522642e6153dd13eefcff (plain) (blame)
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
#!/bin/ash

export PATH="$PATH:/opt/openslx/bin:/opt/openslx/sbin"

if ! touch "/run"; then
	echo "Only root can call this" >&2
	exit 1
fi

if [ "$1" = "--detach" ]; then
	shift # Legacy, ignored
fi

if [ $# -lt 1 ]; then
	echo "$0 <reboot|poweroff|kexec-reboot> [--delay minutes] [--time <now as hh:mm>] [--uuid thismachine]" >&2
	exit 2
fi

MODE=$1

DELAY=
TS=
WANTUUID=
shift

while [ $# -gt 0 ]; do
	if [ "$1" = "--time" ]; then
		TS="$2"
		shift
	elif [ "$1" = "--delay" ]; then
		DELAY="$2"
		shift
	elif [ "$1" = "--uuid" ]; then
		WANTUUID="$2"
		shift
	else
		break
	fi
	shift
done
# Legacy cmdline format
if [ -n "$1" ] && [ -z "$DELAY" ]; then
	DELAY="$1"
	shift
fi
if [ -n "$1" ] && [ -z "$WANTUUID" ]; then
	WANTUUID="$1"
	shift
fi

if [ -n "$WANTUUID" ] && [ -s "/etc/system-uuid" ]; then
	WANTUUID="$( echo "$WANTUUID" | tr 'a-f' 'A-F' )"
	ISUUID="$( cat /etc/system-uuid )"
	if [ "$ISUUID" != "$WANTUUID" ]; then
		echo "ERROR: Sanity check failed: System-UUID mismatch." >&2
		echo "Is: '$ISUUID', want: '$WANTUUID'" >&2
		exit 1
	fi
fi

if [ "$MODE" != "reboot" ] && [ "$MODE" != "poweroff" ] && [ "$MODE" != "kexec-reboot" ]; then
	slxlog --echo "idleaction-failed-call" "Invalid call to idleaction-scheduled_action. Mode '$MODE' unknown." >&2
	exit 3
fi

if [ -n "$TS" ]; then
	# Sanity check: If passing time, this has to be from when the request was thought to be triggered.
	# Mostly useful if you remotely trigger this and want to make sure your request arrives in time.
	# Sleep first so the clock has time to get adjusted in case of bootup or wakeup
	sleep 10
	NOW=$(date +%s)
	SHOULD=$(date -d "today $TS" +%s)
	DIFF=$(( NOW - SHOULD ))
	DIFF=${DIFF#-} # DIFF = Abs($DIFF)
	if [ "$DIFF" -gt 90 ]; then
		echo "Timestamp mismatch, ignoring request." >&2
		exit 4
	fi
fi

# Fire away
[ -z "$DELAY" ] && DELAY=0
DELAY="$(( DELAY * 60 ))"
idle-daemon --send "${MODE%-reboot} $DELAY"