-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlockerd
More file actions
executable file
·454 lines (414 loc) · 11.7 KB
/
lockerd
File metadata and controls
executable file
·454 lines (414 loc) · 11.7 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/bin/sh
# type: int
# description
# digit width of the process id number
# default: 6
PIDWIDTH="6"
# use per OS pid length
os_type=$(uname -s)
pw=""
case "${os_type}" in
Linux)
pw=$(wc -c /proc/sys/kernel/pid_max | awk '{ print $1 }')
;;
NetBSD)
pidmax=30000
pw=$(printf '%s\n' "$pidmax" | wc -c)
;;
OpenBSD|FreeBSD|*BSD)
pidmax=99999
pw=$(printf '%s\n' "$pidmax" | wc -c)
;;
esac
if [ -n "$pw" ]; then
PIDWIDTH="$pw"
fi
PIDWIDTH="$(( PIDWIDTH + 2 ))"
# type: string
# description
# script base name through the idiom "${0##*/}"
myname="${0##*/}"
# type: int
# description
# current running pid of the script
mypid="$$"
# type: string
# description: usleep path if available
has_usleep=""
export LOCKERD_PID=$mypid
DBGOUT=""
TIME_TO_LOCK=""
xsslock_pid=""
NO_CONTINUE=""
logdir="${HOME}/.local/state/better-xsecurelock"
if [ ! -f "$logdir" ]; then
mkdir -p "$logdir"
fi
LOGFILE="${logdir}/${myname}"
# default 600 seconds
# but config value will
# be used if set
TIME=600
# ${HOME}/.config/better-xsecurelock/
CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/better-xsecurelock"
#config file
CONF="${CONFIG_DIR}/config"
# XDG_SESSION_ID wrapper, so that it can be passed by command line if passing by
# environment is not possible for whatever reason
SessionID=""
[ -n "$XDG_SESSION_ID" ] && SessionID="$XDG_SESSION_ID"
show_usage () {
printf '%s\n' "Usage: ${myname} [debug|-d] [-l LOGFILE] [-c CONFIG] | [-h]"
}
show_help () {
show_usage
printf '%s\n' "OPTIONS"
printf ' %s\n' "debug, -d"
printf '\t%s\n' "Show debug output."
printf ' %s\n' "logfile <LOGFILE>, -l <LOGFILE>"
printf '\t%s\n' "Specify LOGFILE, it must be a valid writeable file."
printf ' %s\n' "config <CONFIG>, -c <CONFIG>"
printf '\t%s\n' "Specify CONFIG file, it must be a valid readable file."
printf ' %s\n' "session-id <XDG_SESSION_ID>, -s <XDG_SESSION_ID>"
printf '\t%s\n' "Specify the XDG_SESSION_ID of the program, must be an int."
printf '\t%s\n' "Use only when XDG_SESSION_ID cannot be passed through ENV."
printf ' %s\n' "help, -h"
printf '\t%s\n' "Show this help message."
printf '%s\n' "SIGNALS"
printf ' %s\n' "In addition ${myname} can handle the following signals:"
printf ' %s' "HUP"
printf '\t%s\n' "Reload the config."
printf ' %s' "USR1"
printf '\t%s\n' "Reload the config."
printf ' %s' "USR2"
printf '\t%s\n' "Relaunch the program."
}
opts=""
# input parsing
while [ "$#" -gt 0 ]; do
case "$1" in
debug|-d)
DBGOUT=1
opts="${1} ${opts}"
;;
logfile|-l)
if [ -w "$2" ]; then
LOGFILE="$2"
opts="${1} ${2} ${opts}"
else
printf '%s: %s\n' "${myname}" \
"invalid logfile location '${2}', using default: ${LOGFILE}"
fi
shift
;;
config|-c)
if [ -r "$2" ]; then
CONF="$2"
opts="${1} ${2} ${opts}"
else
printf '%s: %s\n' "${myname}" \
"invalid config location '${2}', using default: ${CONF}"
fi
shift
;;
session-id|-s)
if is_int "$2"; then
SessionID="$2"
else
printf '%s: %s\n' "${myname}" \
"invalid value type."
fi
shift
;;
help|-h)
show_help
exit 0
;;
*)
printf '%s\n' "${myname}: error, invalid argument: ${1}"
show_usage
exit 1
;;
esac
shift
done
export CONF LOGFILE PIDWIDTH
# Usage: write_log "message text"
# Return: void
# Description:
# Write message to log file
write_log () {
name="$myname"
message="$*"
printf '[%s] %12s %*s: %s\n' \
"$(date +'%Y-%m-%d %H:%M:%S')" \
"$name" \
"$PIDWIDTH" "[$mypid]" \
"$message" \
>> "$LOGFILE"
}
# return type: boolean
# usage: is_int "value"
# description: check if passed value is a number
is_int() {
if [ -n "$1" ]; then
printf %d "$1" >/dev/null 2>&1
else
return 1
fi
}
# thin awk wrapper that will prefer mawk over gawk
u_awk () { awk "$@"; }
if command -v mawk >/dev/null; then
u_awk () { mawk "$@"; }
fi
# usage: pid_tree_search PID NAME
# PID: the parent pid among whose ps tree we will search
# NAME: the name of the program whose pid we want
# return type: integer
# return error: standard error return value 1
pid_tree_search () {
search_pid="$1"
search_name="$2"
word_length="${#search_name}"
rval=$(
pstree -Aps "${search_pid}" \
| u_awk \
-v name="$search_name" \
-v wlen="$word_length" \
'\
BEGIN { search=name"\\([[:digit:]]*\\)" } \
match( $0, search )\
{\
print substr($0,RSTART+wlen+1,RLENGTH-wlen-2) \
}\
'
)
if is_int "$rval"; then
printf '%s\n' "$rval"
else
return 1
fi
}
if [ -z "$SessionID" ]; then
write_log "could not determine Session ID, exiting..."
exit 1
fi
started_string="started."
if [ -n "$opts" ]; then
started_string="started with options: ${opts}"
fi
write_log "$started_string"
[ -n "$DBGOUT" ] && printf '%s %s\n' "$myname" "$started_string"
[ -n "$DBGOUT" ] && printf '%s %s\n' "$myname pid:" "$mypid"
# Usage: getval "KEY" file
# Return: string
# Description:
# Read a KEY=VALUE file and retrieve the Value of the passed KEY
getval(){
# Setting 'IFS' tells 'read' where to split the string.
while IFS='=' read -r key val; do
# Skip over lines containing comments.
# (Lines starting with '#').
[ "${key##\#*}" ] || continue
# '$key' stores the key.
# '$val' stores the value.
if [ "$key" = "$1" ]; then
printf '%s\n' "$val"
fi
done < "$2"
}
# Usage: load_config
# Return: void
# Description:
# Load values from the configuration file if it exists.
load_config () {
if [ -r "$CONF" ]; then
[ -n "$DBGOUT" ] && printf '%s: %s\n' "$myname" "config $CONF loaded"
TIME_TO_LOCK=$(getval "TIME_TO_LOCK" "$CONF")
[ -n "$TIME_TO_LOCK" ] && TIME=$TIME_TO_LOCK
fi
}
# Usage: set_time
# Return: void
# Description:
# Sets the screen saver activation and dpms parameters via xset.
set_time () {
load_config
[ -n "$DBGOUT" ] && printf '%s: %s\n' "${myname} time" "$TIME"
c_cycle="$(( TIME / 5 ))"
c_timeout="$(( TIME - c_cycle ))"
c_off="$(( TIME * 3 / 2 ))"
SAVER_TIMEOUT="$c_cycle"
export SAVER_TIMEOUT
# screensaver time
# <timeout> <cycle>
time_ss="$c_timeout $c_cycle"
# dpms time
# <standby> <suspend> <off>
time_dpms="0 0 $c_off"
[ -n "$DBGOUT" ] && printf '%20s: %s\n' "time screensaver" "$time_ss"
[ -n "$DBGOUT" ] && printf '%20s: %s\n' "time dpms" "$time_dpms"
# we want word splitting here
# shellcheck disable=SC2086
xset s ${time_ss}
# shellcheck disable=SC2086
xset dpms ${time_dpms}
write_log "set time s as: ${time_ss} and dpms as: ${time_dpms}"
}
# Usage: outHandler "SIG"
# Return: void
# Description:
# Handle signals to terminate the program, sets NO_CONTINUE to 1 so that the
# waiter cycle can terminate.
outHandler () {
[ "$DBGOUT" = 1 ] && printf '\n%s\n' "exiting on signal: $1"
# kill "$xsslock_pid"
NO_CONTINUE=1
# exit
}
# Usage: relHandler "SIG"
# Return: void
# Description:
# Handle signals to terminate the program, sets NO_CONTINUE to 2 so that the
# waiter cycle can terminate and the program can exec "$0" to reload.
relHandler () {
[ "$DBGOUT" = 1 ] && printf '\n%s\n' "reloading on signal: $1"
NO_CONTINUE=2
}
# Usage: sigHandler "SIG"
# Return: void
# Description:
# Handles external signals to the program to reload config and set the screen
# saver activation times.
# Writes to log.
sigHandler () {
[ -n "$DBGOUT" ] && printf '%s: %s\n' "${myname} received signal" "$1"
write_log "$1 received"
set_time
}
set_time
xss-lock -n dim-screen.sh -s "$SessionID" -l -v -- screenlocker 2>&1 \
| cat >> "$LOGFILE" &
# capture pid
# a simple $! is not enough thanks to using sed...
xsslock_pid=$(pid_tree_search "${mypid}" "xss-lock")
write_log "xss-lock pid captured as ${xsslock_pid}"
trap 'sigHandler "HUP"' HUP
trap 'sigHandler "USR1"' USR1
trap 'relHandler "USR2"' USR2
trap 'outHandler "EXIT"' EXIT
trap 'outHandler "TERM"' TERM
trap 'outHandler "INT"' INT
trap 'outHandler "QUIT"' QUIT
# Usage: fetch_screensaver_vars
# Return: string
# Description:
# Returns the current screen saver activation times in a string as
# TIMEOUTxCYCLE, example: 480x120
fetch_screensaver_vars () {
xset q | u_awk '/timeout/ { print $2"x"$4 }'
}
# Usage: find_suspenders_in_session
# Return: boolean
# Description:
# Searches the process list for instances of xdg-screensaver that are running
# with the suspend argument in the current desktop session.
find_suspenders_in_session () {
ps ax -o'cgroup,user,cmd=CMD' \
| grep -v "grep" \
| grep "0::/${SessionID}" \
| grep " $USER " \
| grep "xdg-screensaver" \
| grep -q "suspend"
}
# Usage: should_set_time
# Return: boolean
# Description:
# Returns 0 when the screen saver activation time is a "correct" value and
# there's no need to set it again.
# Returns 1 when the screen saver activation time is a "wrong" value and has
# to be set again.
should_set_time () {
retval=0
t_and_c=$(fetch_screensaver_vars)
ss_timeout="${t_and_c%%x*}"
ss_cycle="${t_and_c##*x}"
if [ "$ss_timeout" -ne 0 ]; then
if [ "$ss_timeout" -eq "$ss_cycle" ]; then
retval=1
fi
else
if ! find_suspenders_in_session; then
retval=1
fi
fi
return "$retval"
}
# usage: msleep int
# description: sleep for milliseconds
# return type: void
msleep () {
milisecs="$1"
if [ -n "$has_usleep" ]; then
microsecs="${milisecs}000"
case "$has_usleep" in
*/usleep)
usleep "$microsecs"
;;
*/busybox)
busybox usleep "$microsecs"
;;
esac
else
secs=$(awk -v s="${milisecs}" 'BEGIN {printf "%.3f", s/1000}')
sleep "$secs"
fi
}
has_usleep=$(command -v usleep)
[ -z "$has_usleep" ] && has_usleep=$(command -v busybox)
write_log "waiting for xss-lock"
# 5 cycles per second, 60 seconds per minute, 60 minutes
INTERVAL=$(( 5 * 60 * 60 ))
# 5 cycles per second, 20 seconds
cyc=$(( 5 * 20 ))
while [ -z "$NO_CONTINUE" ]; do
# is the count of cycle iterations the same as the interval?
if [ "$count" = "$INTERVAL" ]; then
write_log "running"
# reset the count to 0
count=0
fi
# check if we need to set time
if [ $(( count % cyc )) -eq 0 ]; then
if ! should_set_time ; then
write_log "incorrect time setting detected"
set_time
fi
fi
# ingrement the count
count=$(( count + 1 ))
# the duty cycle of this daemon is 5 iterations per second
# this is fast enough to feel responsive to signals, yet not hog
# resources, mainly cpu
msleep 200
done
write_log "no continue received"
if kill -0 "$xsslock_pid" >/dev/null; then
kill "$xsslock_pid"
write_log "xss-lock killed"
fi
[ -n "$DBGOUT" ] && printf '%s: %s\n' "${myname} killed" "xss-lock"
case "$NO_CONTINUE" in
1)
write_log "terminating."
exit 0
;;
2)
write_log "reloading."
if [ -n "$opts" ]; then
set -- ${opts}
fi
exec $0 "$@"
;;
esac