Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1195588

Browse files
authored
Merge pull request #82 from lahwaacz/checkservices
checkservices: add auditd.service to ignore list and resolve shellcheck warnings
2 parents fcc2824 + 24bcbf4 commit 1195588

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

admin/checkservices

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ fi
4545

4646
# default options
4747
AUTOCONFIRM=0 # autoconfirmation
48-
DBUS=1 # relauch when dbus
4948
FAILED=1 # display failed service at the end
5049
PACDIFF=1 # run pacdiff
5150
RELOAD=1 # reload systemd
@@ -56,7 +55,12 @@ USER_SLICE=0 # act on users services
5655
MACHINE_SLICE=0 # act on machine services
5756

5857
# ignored service list
59-
IGNORED_SERVICES=("getty@tty.*.service" "systemd-logind.service" "dbus-broker.service")
58+
IGNORED_SERVICES=(
59+
"getty@tty.*.service"
60+
"systemd-logind.service"
61+
"dbus-broker.service"
62+
"auditd.service"
63+
)
6064

6165
# print $* as an arrow line
6266
arrow() {
@@ -77,21 +81,21 @@ error() {
7781
# return : 0 - found
7882
# 1 - not found
7983
in_array() {
80-
local needle=$1; shift
84+
local needle="$1"; shift
8185
local item
8286
for item in "$@"; do
83-
[[ $item = $needle ]] && return 0 # Found
87+
[[ $item = "$needle" ]] && return 0 # Found
8488
done
8589
return 1 # Not Found
8690
}
8791

8892
# ask for confirmation
8993
# return 0 when confirmed, otherwise 1
9094
confirm() {
91-
(( $AUTOCONFIRM == 1 )) && return 0
95+
(( AUTOCONFIRM == 1 )) && return 0
9296
local -i try
9397
local ans
94-
for try in 5 4 3 2 1; do
98+
for ((try=1; try<=5; try++)); do
9599
printf '%s [Yes|No] ' "$1"
96100
read -r ans || return 1
97101
case $ans in
@@ -105,7 +109,9 @@ confirm() {
105109

106110
# get running systemd services
107111
get_services() {
108-
systemctl --no-legend --full --type service --state running | tr -d '' | awk '{print $1}' | grep -v $(printf -- '-e %s ' "${IGNORED_SERVICES[@]}")
112+
local -a grep_patterns
113+
read -r -a grep_patterns <<< "$(printf -- '-e %s ' "${IGNORED_SERVICES[@]}")"
114+
systemctl --no-legend --full --type service --state running | tr -d '' | awk '{print $1}' | grep -v "${grep_patterns[@]}"
109115
}
110116

111117
# get systemd services with updated mapped files
@@ -128,10 +134,10 @@ get_broken_maps() {
128134
done
129135
[[ -z "$pidfile" ]] && error "Unable to find pid file for $service." && continue
130136
# skip non system units
131-
(( $USER_SLICE == 0 )) && [[ "$unit_path" =~ /user\.slice/ ]] && continue
132-
(( $MACHINE_SLICE == 0 )) && [[ "$unit_path" =~ /machine\.slice/ ]] && continue
137+
(( USER_SLICE == 0 )) && [[ "$unit_path" =~ /user\.slice/ ]] && continue
138+
(( MACHINE_SLICE == 0 )) && [[ "$unit_path" =~ /machine\.slice/ ]] && continue
133139
# parse pidfile
134-
pids=( $(< "$pidfile") )
140+
mapfile -t pids < "$pidfile"
135141
if (( "${#pids[*]}" == 0 )); then
136142
error "Unable to parse pid file for $service."
137143
continue
@@ -145,7 +151,7 @@ get_broken_maps() {
145151
# only file mapped as executable
146152
deleted="$(grep -F '(deleted)' "$maps_path"|sed -nr 's|^\S+ ..x. \S+ \S+ \S+ \s+||p'|grep -v "/memfd:")"
147153
if [[ $deleted ]]; then
148-
printf "%s\n" $service
154+
printf "%s\n" "$service"
149155
break
150156
fi
151157
done
@@ -161,12 +167,13 @@ get_dbus_names() {
161167
# get systemd services not registered on dbus system bus
162168
get_missing_dbus() {
163169
local service busname
164-
local -a registered=($(get_dbus_names))
170+
local -a registered
171+
mapfile -t registered < <(get_dbus_names)
165172
for service in $(get_services); do
166173
# get the service registered bus name
167174
busname="$(systemctl --property BusName --value show "$service")"
168175
if [[ "$busname" ]] && ! in_array "$busname" "${registered[@]}"; then
169-
echo $service
176+
echo "$service"
170177
fi
171178
done
172179
}
@@ -192,10 +199,10 @@ restart_services() {
192199
for service; do
193200
echo "systemctl restart $service"
194201
systemctl restart "$service" &
195-
if (( $SERIALIZE )); then
202+
if (( SERIALIZE )); then
196203
wait
197204
# display status directly when serialize and not quiet
198-
(( $STATUS )) && systemctl --no-pager --lines=0 status "$service"
205+
(( STATUS )) && systemctl --no-pager --lines=0 status "$service"
199206
else
200207
# register pids
201208
registered_pids[$!]="$service"
@@ -204,25 +211,25 @@ restart_services() {
204211

205212
# display status as soon as available when not serialized
206213
while (( ${#registered_pids[*]} )); do
207-
# wait for process at least one process to finish
214+
# wait for at least one process to finish
208215
wait -n
209216

210-
running_pids=( $(jobs -p) )
217+
mapfile -t running_pids < <(jobs -p)
211218

212219
# count registered pid for loop protection
213220
last_registered_pids_count=${#registered_pids[*]}
214221

215222
for pid in "${!registered_pids[@]}"; do
216223
in_array "$pid" "${running_pids[@]}" && continue
217224
# show units status
218-
(( $STATUS )) && systemctl --no-pager --lines=0 status "${registered_pids[$pid]}"
219-
unset registered_pids[$pid]
225+
(( STATUS )) && systemctl --no-pager --lines=0 status "${registered_pids[$pid]}"
226+
unset "registered_pids[$pid]"
220227
break
221228
done
222229

223230
# ensure we are not at 1st infinite loop
224231
# if we didn't remove a process something wrong happen
225-
if (( $last_registered_pids_count == ${#registered_pids[*]} )); then
232+
if (( last_registered_pids_count == ${#registered_pids[*]} )); then
226233
error "Unable to wait processes to finish"
227234
error "Registered PIDs: ${registered_pids[*]}"
228235
error "Running PIDs: ${running_pids[*]}"
@@ -276,10 +283,10 @@ argparse() {
276283
U) USER_SLICE=0;; u) USER_SLICE=1;;
277284
M) MACHINE_SLICE=0;; m) MACHINE_SLICE=1;;
278285
Z) SERIALIZE=0;; z) SERIALIZE=1;;
279-
i) if [[ "$OPTARG" == *.service ]]; then
286+
i) if [[ "$OPTARG" == *.service ]]; then
280287
IGNORED_SERVICES+=("$OPTARG")
281-
else
282-
usage
288+
else
289+
usage
283290
fi
284291
;;
285292
*) usage;;
@@ -298,19 +305,20 @@ main() {
298305
argparse "$@"
299306

300307
# from now, we need to be root
301-
(( $UID != 0 )) && error 'You need to be root' && exit 1
308+
(( UID != 0 )) && error 'You need to be root' && exit 1
302309

303310
# call pacdiff to ensure config files are updated before restart
304-
if (( $PACDIFF )); then
311+
if (( PACDIFF )); then
305312
arrow 'Run pacdiff'
306313
pacdiff
307314
fi
308315

309316
# ensure systemd has been reloaded or reexectued
310-
(( $RELOAD )) && reload_systemd
317+
(( RELOAD )) && reload_systemd
311318

312319
arrow 'Services with broken maps files'
313-
local -a broken_services=($(get_broken_maps))
320+
local -a broken_services
321+
mapfile -t broken_services < <(get_broken_maps)
314322
echo "Found: ${#broken_services[@]}"
315323
if (( ${#broken_services[@]} )); then
316324
if (( RESTART )); then
@@ -325,7 +333,8 @@ main() {
325333
fi
326334

327335
arrow 'Services missing on the system bus'
328-
local -a missing_services=($(get_missing_dbus))
336+
local -a missing_services
337+
mapfile -t missing_services < <(get_missing_dbus)
329338
echo "Found: ${#missing_services[@]}"
330339
if (( ${#missing_services[@]} )); then
331340
if (( RESTART )); then
@@ -340,7 +349,7 @@ main() {
340349
fi
341350

342351
# list only failed systemd units
343-
if (( $FAILED )); then
352+
if (( FAILED )); then
344353
arrow "List failed units"
345354
systemctl --failed --all --no-pager --no-legend --full list-units
346355
fi

0 commit comments

Comments
 (0)