Debian Codegraph @

ex/restart.d/dbus.service

raw
1 #!/bin/sh
2
3 # by Vladimir Kudrya
4 # https://github.com/Vladimir-csp/
5 #
6 # This script is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version. See <http://www.gnu.org/licenses/>.
10
11 # This is a replacement for original dbus.service script by Thomas Liske <[email protected]>
12 # Dbus dependencies are enumerated dynamically and restarted after dbus and systemd daemon reexec
13
14 # delay in semi-interactive mode
15 DELAY_SECONDS=10
16
17 # enable xtrace if we should be verbose
18 if [ "$NR_VERBOSE" = '1' ]; then
19 set -x
20 fi
21
22 # choose interactivity mode
23 if [ "${DEBIAN_FRONTEND:-other}" = "noninteractive" ]; then
24 CONSOLE_MODE=skip
25 elif [ -t 0 ]; then
26 CONSOLE_MODE=interactive
27 else
28 CONSOLE_MODE=delay
29 fi
30
31 if [ "$(id -ru)" != "0" ]; then
32 echo "Not root, exiting" >&2
33 exit 1
34 fi
35
36 # get init system process name
37 INIT_EXEC="$(ps -p 1 -o comm=)"
38
39 case "$INIT_EXEC" in
40 systemd | */systemd) true ;;
41 *)
42 echo "Init system is not systemd ($INIT_EXEC), doing nothing"
43 exit 0
44 ;;
45 esac
46
47 # start from the name of the hook, get real unit ID (for example, dbus-broker replaces dbus)
48 DBUS_SERVICE_NAME=$(
49 systemctl show --value -p Id "${0##*/}"
50 )
51
52 # if DM is active, return canonical ID
53 DISPLAY_MANAGER=$(
54 systemctl -q is-active display-manager.service \
55 && systemctl show --value -p Id display-manager.service
56 )
57
58 # get active dependencies
59 ACTIVE_DEPS=''
60 while read -r service; do
61 case "$service" in
62 "$DISPLAY_MANAGER" | dbus.service | "$DBUS_SERVICE_NAME") continue ;;
63 *.service)
64 case " $ACTIVE_DEPS " in
65 *" $service "*) true ;;
66 *) ACTIVE_DEPS="${ACTIVE_DEPS}${ACTIVE_DEPS:+ }${service}" ;;
67 esac
68 ;;
69 esac
70 done <<- EOF
71 $(
72 systemctl list-dependencies -l --reverse --plain --all \
73 --type=service \
74 --state=active,reloading,failed,activating \
75 dbus.socket dbus.service "$DBUS_SERVICE_NAME"
76 )
77 EOF
78
79 # get active services with BusName
80 current_busname=''
81 current_id=''
82 while IFS='=' read -r option value; do
83 case "${option}" in
84 BusName)
85 current_busname="$value"
86 continue
87 ;;
88 Id)
89 current_id="$value"
90 continue
91 ;;
92 '')
93 if [ -z "$current_busname" ] || [ -z "$current_id" ]; then
94 current_busname=''
95 current_id=''
96 continue
97 fi
98 service=$current_id
99 current_busname=''
100 current_id=''
101 case "$service" in
102 "$DISPLAY_MANAGER" | dbus.service | "$DBUS_SERVICE_NAME") continue ;;
103 esac
104 case " $ACTIVE_DEPS " in
105 *" $service "*) true ;;
106 *) ACTIVE_DEPS="${ACTIVE_DEPS}${ACTIVE_DEPS:+ }${service}" ;;
107 esac
108 ;;
109 *)
110 current_busname=''
111 current_id=''
112 ;;
113 esac
114 done <<- EOF
115 $(
116 systemctl show --plain --all \
117 --type=service \
118 --state=active,reloading,failed,activating \
119 --property=BusName,Id
120 )
121 =end
122 EOF
123
124 # get logind users
125 USERS=''
126 while read -r _uid user _linger _state; do
127 USERS="${USERS}${USERS:+ }${user}"
128 done <<- EOF
129 $(loginctl list-users --no-legend)
130 EOF
131
132 # get current dbus service PIDs
133 DBUS_PIDS=''
134 while read -r pid unit; do
135 case "$unit" in
136 "${DBUS_SERVICE_NAME}") DBUS_PIDS="${DBUS_PIDS}${DBUS_PIDS:+ }${pid}" ;;
137 esac
138 done <<- EOF
139 $(ps -eo pid,unit)
140 EOF
141
142 # assemble commands
143 COMMANDS=''
144 # terminate users
145 [ -n "$USERS" ] && COMMANDS="${COMMANDS}${COMMANDS:+ ; }loginctl terminate-user $USERS"
146 # stop display manager
147 [ -n "$DISPLAY_MANAGER" ] && COMMANDS="${COMMANDS}${COMMANDS:+ ; }systemctl stop $DISPLAY_MANAGER"
148 # restart dbus
149 COMMANDS="${COMMANDS}${COMMANDS:+ ; }sleep 1 ; systemctl restart $DBUS_SERVICE_NAME"
150 # kill previous dbus pids
151 [ -n "$DBUS_PIDS" ] && COMMANDS="${COMMANDS}${COMMANDS:+ ; }sleep 1 ; kill $DBUS_PIDS 2>/dev/null"
152 # restart active dependencies
153 COMMANDS="${COMMANDS}${COMMANDS:+ ; }sleep 1 ; systemctl restart $ACTIVE_DEPS"
154 # start display manager
155 [ -n "$DISPLAY_MANAGER" ] && COMMANDS="${COMMANDS}${COMMANDS:+ ; }systemctl start $DISPLAY_MANAGER"
156 # normalize to single line
157 COMMANDS=$(echo "$COMMANDS" | tr '\n' ' ' | tr -s ' ')
158
159 shcat() {
160 while IFS='' read -r line; do
161 printf '%s\n' "$line"
162 done
163 }
164
165 # shellcheck disable=SC2086
166 case "$CONSOLE_MODE" in
167 interactive) shcat ;;
168 delay) shcat >&2 ;;
169 *) shcat > /dev/null ;;
170 esac <<- EOF
171
172 !!! $DBUS_SERVICE_NAME restart will be performed !!!
173
174 Users to be terminated:
175 $(printf ' %s\n' $USERS)
176
177 Services to be restarted:
178 $(printf ' %s\n' $ACTIVE_DEPS)
179
180 Display manager to be restarted: ${DISPLAY_MANAGER:-no active DM found}
181
182 Command set to run as transient unit restart-dbus.service:
183 $(
184 IFS=';'
185 # consequent commands have a space after preceding semicolons, compensate for that
186 printf '%s' ' '
187 printf ' %s\n' $COMMANDS
188 )
189
190 Logs can be viewed by: journalctl -u restart-dbus
191 EOF
192
193 # if interactive, ask for continuation
194 case "$CONSOLE_MODE" in
195 interactive)
196 printf '\n%s' "Press Enter to continue > "
197 read -r _PRESSENTER
198 ;;
199 delay)
200 printf '\n%s\n' "Restart in $DELAY_SECONDS seconds..." >&2
201 sleep $DELAY_SECONDS
202 echo "Restarting..." >&2
203 ;;
204 esac
205
206 # run restart sequence as transient unit...
207 # it is possible to view its output in the log if any:
208 # journalctl -u restart-dbus
209 systemd-run -G --unit=restart-dbus --description="Transient dbus restarter" sh -c "$COMMANDS"