-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocking-systemd-inhibit
executable file
·48 lines (42 loc) · 1.35 KB
/
blocking-systemd-inhibit
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
#!/usr/bin/env bash
usage() {
echo "$0: [inhibit-args] -- [command] [arguments]"
exit 1
}
declare -a inhibit_args
declare -a command_args
inhibit_args=()
command_args=()
reached_command=false
for arg in "$@"; do
if [ "$arg" = "--" ]; then
reached_command=true
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ]; then
usage
elif [ "$reached_command" = true ]; then
command_args+=("$arg")
else
inhibit_args+=("$arg")
fi
done
# Stolen from https://github.com/systemd/systemd/issues/14045#issuecomment-1683764309
# After the system woke up, it may be still in 'sleep' state for a
# brief period. This causes sytemd-inhitit to fail with "Failed to
# inhibit: The operation inhibition has been requested for is
# already running". Therefore, we have to potentially try multiple
# times until we can take the sleep inhibit lock. See
# https://github.com/systemd/systemd/issues/14045
declare -ir WAIT_INHIBITED_SECS=180
for I in $(seq 1 ${WAIT_INHIBITED_SECS}); do
if [[ ${I} -eq ${WAIT_INHIBITED_SECS} ]]; then
>&2 echo "$0: Failed to take inhibit lock after ${I} seconds"
exit 1
fi
# Check if we would be able to take the inhibit lock.
if /usr/bin/systemd-inhibit "${inhibit_args[@]}" true; then
echo "$0: Inhibit lock free after ${I} seconds"
break
fi
sleep 1
done
exec /usr/bin/systemd-inhibit "${inhibit_args[@]}" "${command_args[@]}"