blob: 2717d6cbd45f28d97209e1f6cc4e4f3107e58940 (
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#!/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
DETACH=
if [ "$1" = "--detach" ]; then
DETACH=yes
shift
fi
if [ $# -lt 1 ]; then
echo "$0 [--detach] <reboot|poweroff|kexec-reboot> [delay_minutes]" >&2
exit 2
fi
DELAY=
TS=
MODE=$1
shift
while [ $# -gt 0 ]; do
if [ "$1" == "--time" ]; then
TS=$2
shift
elif [ "$1" == "--delay" ]; then
DELAY=$2
shift
else
break
fi
shift
done
[ -n "$1" ] && [ -z "$DELAY" ] && DELAY=$1
if [ -n "$TS" ]; then
# Sanity check for trigger by cron
# Sleep first so the clock has time to get adjusted
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
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
NUM=$(who | wc -l)
if [ -z "$DELAY" ] && [ "$NUM" = "0" ]; then
# Easy way out - machine is idle and no delay
(
# Detach, close all in/out fds - needed for some ssh clients to rpevent them from hanging
exec < /dev/null &> /dev/null
sleep 1
$MODE
) &
disown
exit 0
fi
runaction () {
# Someone logged in or delay requested
[ -n "$DETACH" ] && sleep 1
if [ -n "$DELAY" ] && [ "$DELAY" -gt 0 ]; then
echo "Doing $MODE in $DELAY minutes!"
sleep $(( DELAY * 60 ))
fi
for MINUTES in 5 X X X X X X X 3 X X X 2 X X X 1 X X X; do
NUM=$(who | wc -l)
[ "$NUM" = "0" ] && break
if [ "$MINUTES" != "X" ]; then
USERS=0
if [ "${MODE#*-}" = "reboot" ]; then
MESSAGE="Das System wird in $MINUTES Minute(n) neugestartet, bitte beenden Sie Ihre Sitzung.
The system will reboot in $MINUTES minute(s). Please save your work and end the session."
else
MESSAGE="Dieser Rechner wird in $MINUTES Minute(n) abgeschaltet, bitte beenden Sie Ihre Sitzung.
The system will power off in $MINUTES minute(s). Please save your work and end the session."
fi
for d in $(who | awk '{print $2 " " $NF}' | sort -u); do
if [ "${d:0:1}" = ":" ]; then
# X11
export DISPLAY=$d
export XAUTHORITY=$(ps a | grep " $DISPLAY " | grep -o -- '-auth.*$' | grep -m1 -v grep | awk '{print $2}')
# TODO: Don't look... We need to pick a proper util for this
/opt/openslx/cups/printergui --error "$MESSAGE" &
unset DISPLAY XAUTHORITY
USERS=$(( $USERS + 1 ))
elif [ "${d:0:3}" = "tty" ]; then
# Regular tty
cat > "/dev/$d" <<-EOF
*
***************************
$MESSAGE
***************************
*
EOF
USERS=$(( $USERS + 1 ))
elif [ "${d:0:3}" = "pts" ]; then
# pts - xterm or ssh session
cat > "/dev/pts/${d:3}" <<-EOF
*
***************************
$MESSAGE
***************************
*
EOF
USERS=$(( $USERS + 1 ))
fi
done
FILE=$(mktemp)
who > "$FILE"
slxlog "idleaction-busy" "Postponed $MODE ($MINUTES minutes max.) - $USERS user(s) still active"
sleep 1
rm -- "$FILE"
fi
sleep 15
done
$MODE
}
if [ -n "$DETACH" ]; then
(
# Detach, see above
exec < /dev/null &> /dev/null
runaction
) &
disown
exit 0
fi
runaction
exit 0
|