2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # shellcheck disable=SC2016
7 # shellcheck source=test/units/util.sh
8 .
"$(dirname "$0")"/util.sh
11 if [[ -v UNIT_NAME
&& -e "/usr/lib/systemd/system/$UNIT_NAME" ]]; then
12 rm -fvr "/usr/lib/systemd/system/$UNIT_NAME" "/etc/systemd/system/$UNIT_NAME.d" "+4"
15 rm -f /etc
/init.d
/issue-24990
21 # Create a simple unit file for testing
22 # Note: the service file is created under /usr on purpose to test
23 # the 'revert' verb as well
24 export UNIT_NAME
="systemctl-test-$RANDOM.service"
25 cat >"/usr/lib/systemd/system/$UNIT_NAME" <<\EOF
27 Description
=systemctl
test
30 ExecStart
=sleep infinity
35 ConfigurationDirectory
=%n
41 WantedBy
=multi-user.target
44 # Configure the preset setting for the unit file
45 mkdir
/run
/systemd
/system-preset
/
46 echo "disable $UNIT_NAME" >/run
/systemd
/system-preset
/99-systemd-test.preset
48 EDITOR
='true' script -ec 'systemctl edit "$UNIT_NAME"' /dev
/null
49 [ ! -e "/etc/systemd/system/$UNIT_NAME.d/override.conf" ]
51 printf '%s\n' '[Service]' 'ExecStart=' 'ExecStart=sleep 10d' >"+4"
52 EDITOR
='mv' script -ec 'systemctl edit "$UNIT_NAME"' /dev
/null
53 printf '%s\n' '[Service]' 'ExecStart=' 'ExecStart=sleep 10d' |
cmp - "/etc/systemd/system/$UNIT_NAME.d/override.conf"
55 printf '%b' '[Service]\n' 'ExecStart=\n' 'ExecStart=sleep 10d' >"+4"
56 EDITOR
='mv' script -ec 'systemctl edit "$UNIT_NAME"' /dev
/null
57 printf '%s\n' '[Service]' 'ExecStart=' 'ExecStart=sleep 10d' |
cmp - "/etc/systemd/system/$UNIT_NAME.d/override.conf"
59 # Double free when editing a template unit (#26483)
60 EDITOR
='true' script -ec 'systemctl edit user@0' /dev
/null
63 systemctl
--state help
64 systemctl
--signal help
68 systemctl list-dependencies systemd-journald
69 systemctl list-dependencies
--after systemd-journald
70 systemctl list-dependencies
--before systemd-journald
71 systemctl list-dependencies
--after --reverse systemd-journald
72 systemctl list-dependencies
--before --reverse systemd-journald
73 systemctl list-dependencies
--plain systemd-journald
77 systemctl list-units
--recursive
78 systemctl list-units
--type=socket
79 systemctl list-units
--type=service
,timer
80 # Compat: --type= allows load states for compatibility reasons
81 systemctl list-units
--type=loaded
82 systemctl list-units
--type=loaded
,socket
83 systemctl list-units
--legend=yes -a "systemd-*"
84 systemctl list-units
--state=active
85 systemctl list-units
--with-dependencies systemd-journald.service
86 systemctl list-units
--with-dependencies --after systemd-journald.service
87 systemctl list-units
--with-dependencies --before --reverse systemd-journald.service
88 systemctl list-sockets
89 systemctl list-sockets
--legend=no
-a "*journal*"
90 systemctl list-sockets
--show-types
91 systemctl list-sockets
--state=listening
92 systemctl list-timers
-a -l
94 systemctl list-jobs
--after
95 systemctl list-jobs
--before
96 systemctl list-jobs
--after --before
97 systemctl list-jobs
"*"
98 systemctl list-dependencies sysinit.target
--type=socket
,mount
99 systemctl list-dependencies multi-user.target
--state=active
100 systemctl list-dependencies sysinit.target
--state=mounted
--all
102 systemctl list-paths
--legend=no
-a "systemd*"
104 test_list_unit_files
() {
105 systemctl list-unit-files
"$@"
106 systemctl list-unit-files
"$@" "*journal*"
110 test_list_unit_files
--root=/
113 # Should return 4 for a missing unit file
114 assert_rc
4 systemctl
--quiet is-active not-found.service
115 assert_rc
4 systemctl
--quiet is-failed not-found.service
116 assert_rc
4 systemctl
--quiet is-enabled not-found.service
117 # is-active: return 3 when the unit exists but inactive
118 assert_rc
3 systemctl
--quiet is-active
"$UNIT_NAME"
119 # is-enabled: return 1 when the unit exists but disabled
120 assert_rc
1 systemctl
--quiet is-enabled
"$UNIT_NAME"
122 # Basic service management
123 systemctl start
--show-transaction "$UNIT_NAME"
124 systemctl status
-n 5 "$UNIT_NAME"
125 systemctl is-active
"$UNIT_NAME"
126 systemctl reload
-T "$UNIT_NAME"
127 systemctl restart
-T "$UNIT_NAME"
128 systemctl try-restart
--show-transaction "$UNIT_NAME"
129 systemctl try-reload-or-restart
--show-transaction "$UNIT_NAME"
130 systemctl
kill "$UNIT_NAME"
131 (! systemctl is-active
"$UNIT_NAME")
132 systemctl restart
"$UNIT_NAME"
133 systemctl is-active
"$UNIT_NAME"
134 systemctl restart
"$UNIT_NAME"
135 systemctl stop
"$UNIT_NAME"
136 (! systemctl is-active
"$UNIT_NAME")
138 assert_eq
"$(systemctl is-system-running)" "$(systemctl is-failed)"
140 # enable/disable/preset
141 test_enable_disable_preset
() {
142 (! systemctl is-enabled
"$@" "$UNIT_NAME")
143 systemctl
enable "$@" "$UNIT_NAME"
144 systemctl is-enabled
"$@" -l "$UNIT_NAME"
145 # We created a preset file for this unit above with a "disable" policy
146 systemctl preset
"$@" "$UNIT_NAME"
147 (! systemctl is-enabled
"$@" "$UNIT_NAME")
148 systemctl reenable
"$@" "$UNIT_NAME"
149 systemctl is-enabled
"$@" "$UNIT_NAME"
150 systemctl preset
"$@" --preset-mode=enable-only
"$UNIT_NAME"
151 systemctl is-enabled
"$@" "$UNIT_NAME"
152 systemctl preset
"$@" --preset-mode=disable-only
"$UNIT_NAME"
153 (! systemctl is-enabled
"$@" "$UNIT_NAME")
154 systemctl
enable "$@" --runtime "$UNIT_NAME"
155 [[ -e "/run/systemd/system/multi-user.target.wants/$UNIT_NAME" ]]
156 systemctl is-enabled
"$@" "$UNIT_NAME"
157 systemctl disable
"$@" "$UNIT_NAME"
158 # The unit should be still enabled, as we didn't use the --runtime switch
159 systemctl is-enabled
"$@" "$UNIT_NAME"
160 systemctl disable
"$@" --runtime "$UNIT_NAME"
161 (! systemctl is-enabled
"$@" "$UNIT_NAME")
164 test_enable_disable_preset
165 test_enable_disable_preset
--root=/
168 test_mask_unmask_revert
() {
169 systemctl disable
"$@" "$UNIT_NAME"
170 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == disabled
]]
171 systemctl mask
"$@" "$UNIT_NAME"
172 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == masked
]]
173 systemctl unmask
"$@" "$UNIT_NAME"
174 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == disabled
]]
175 systemctl mask
"$@" "$UNIT_NAME"
176 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == masked
]]
177 systemctl revert
"$@" "$UNIT_NAME"
178 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == disabled
]]
179 systemctl mask
"$@" --runtime "$UNIT_NAME"
180 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == masked-runtime
]]
181 # This should be a no-op without the --runtime switch
182 systemctl unmask
"$@" "$UNIT_NAME"
183 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == masked-runtime
]]
184 systemctl unmask
"$@" --runtime "$UNIT_NAME"
185 [[ "$(systemctl is-enabled "$@
" "$UNIT_NAME")" == disabled
]]
188 test_mask_unmask_revert
189 test_mask_unmask_revert
--root=/
191 # add-wants/add-requires
192 (! systemctl show
-P Wants
"$UNIT_NAME" |
grep "systemd-journald.service")
193 systemctl add-wants
"$UNIT_NAME" "systemd-journald.service"
194 systemctl show
-P Wants
"$UNIT_NAME" |
grep "systemd-journald.service"
195 (! systemctl show
-P Requires
"$UNIT_NAME" |
grep "systemd-journald.service")
196 systemctl add-requires
"$UNIT_NAME" "systemd-journald.service"
197 systemctl show
-P Requires
"$UNIT_NAME" |
grep "systemd-journald.service"
200 systemctl set-property
"$UNIT_NAME" IPAccounting
=yes MemoryMax
=1234567
201 systemctl
cat "$UNIT_NAME"
202 # These properties should be saved to a persistent storage
203 grep -r "IPAccounting=yes" "/etc/systemd/system.control/${UNIT_NAME}.d/"
204 grep -r "MemoryMax=1234567" "/etc/systemd/system.control/${UNIT_NAME}.d"
205 systemctl revert
"$UNIT_NAME"
206 (! grep -r "IPAccounting=" "/etc/systemd/system.control/${UNIT_NAME}.d/")
207 (! grep -r "MemoryMax=" "/etc/systemd/system.control/${UNIT_NAME}.d/")
208 # Same stuff, but with --runtime, which should use /run
209 systemctl set-property
--runtime "$UNIT_NAME" CPUAccounting
=no CPUQuota
=10%
210 systemctl
cat "$UNIT_NAME"
211 grep -r "CPUAccounting=no" "/run/systemd/system.control/${UNIT_NAME}.d/"
212 grep -r "CPUQuota=10%" "/run/systemd/system.control/${UNIT_NAME}.d/"
213 systemctl revert
"$UNIT_NAME"
214 (! grep -r "CPUAccounting=" "/run/systemd/system.control/${UNIT_NAME}.d/")
215 (! grep -r "CPUQuota=" "/run/systemd/system.control/${UNIT_NAME}.d/")
217 # Failed-unit related tests
218 (! systemd-run
--wait --unit "failed.service" /bin
/false
)
219 systemctl is-failed failed.service
220 systemctl
--state=failed |
grep failed.service
221 systemctl
--failed |
grep failed.service
222 systemctl reset-failed
"fail*.service"
223 (! systemctl is-failed failed.service
)
226 systemctl restart
"$UNIT_NAME"
227 systemctl stop
"$UNIT_NAME"
228 # Check if the directories from *Directory= directives exist
229 # (except RuntimeDirectory= in /run, which is removed when the unit is stopped)
230 for path
in /var
/lib
/var
/cache
/var
/log
/etc
; do
231 [[ -e "$path/$UNIT_NAME" ]]
234 for what
in "" configuration state cache logs runtime all
; do
235 systemctl clean
${what:+--what="$what"} "$UNIT_NAME"
237 # All respective directories should be removed
238 for path
in /run
/var
/lib
/var
/cache
/var
/log
/etc
; do
239 [[ ! -e "$path/$UNIT_NAME" ]]
243 for value
in pretty us µs utc us
+utc µs
+utc
; do
244 systemctl show
-P KernelTimestamp
--timestamp="$value"
247 # set-default/get-default
248 test_get_set_default
() {
249 target
="$(systemctl get-default "$@
")"
250 systemctl set-default
"$@" emergency.target
251 [[ "$(systemctl get-default "$@
")" == emergency.target
]]
252 systemctl set-default
"$@" "$target"
253 [[ "$(systemctl get-default "$@
")" == "$target" ]]
257 test_get_set_default
--root=/
260 systemctl show
--property ""
261 # Pick a heavily sandboxed unit for the best effect on coverage
262 systemctl show systemd-logind.service
264 # Ignore the exit code in this case, as it might try to load non-existing units
265 systemctl status
-a >/dev
/null ||
:
266 systemctl status
-a --state active
,running
,plugged
>/dev
/null
267 systemctl status
"systemd-*.timer"
268 systemctl status
"systemd-journald*.socket"
269 systemctl status
"sys-devices-*-ttyS0.device"
270 systemctl status
-- -.mount
274 systemctl restart
"$UNIT_NAME"
275 systemctl set-property
"$UNIT_NAME" Markers
=needs-restart
276 systemctl show
-P Markers
"$UNIT_NAME" |
grep needs-restart
277 systemctl reload-or-restart
--marked
278 (! systemctl show
-P Markers
"$UNIT_NAME" |
grep needs-restart
)
280 # --dry-run with destructive verbs
281 # kexec is skipped intentionally, as it requires a bit more involved setup
293 suspend-then-hibernate
296 for verb
in "${VERBS[@]}"; do
297 systemctl
--dry-run "$verb"
299 if [[ "$verb" =~
(halt|poweroff|reboot
) ]]; then
300 systemctl
--dry-run --message "Hello world" "$verb"
301 systemctl
--dry-run --no-wall "$verb"
302 systemctl
--dry-run -f "$verb"
303 systemctl
--dry-run -ff "$verb"
307 # Aux verbs & assorted checks
308 systemctl is-active
"*-journald.service"
309 systemctl
cat "*journal*"
310 systemctl
cat "$UNIT_NAME"
311 systemctl
help "$UNIT_NAME"
312 systemctl service-watchdogs
313 systemctl service-watchdogs
"$(systemctl service-watchdogs)"
315 # show/set-environment
316 # Make sure PATH is set
317 systemctl show-environment |
grep -q '^PATH='
318 # Let's add an entry and override a built-in one
319 systemctl set-environment PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:/usr
/sbin
:/usr
/bin
:/testaddition FOO
=BAR
320 # Check that both are set
321 systemctl show-environment |
grep -q '^PATH=.*testaddition$'
322 systemctl show-environment |
grep -q '^FOO=BAR$'
323 systemctl daemon-reload
324 # Check again after the reload
325 systemctl show-environment |
grep -q '^PATH=.*testaddition$'
326 systemctl show-environment |
grep -q '^FOO=BAR$'
327 # Check that JSON output is supported
328 systemctl show-environment
--output=json |
grep -q '^{.*"FOO":"BAR".*}$'
330 systemctl unset-environment FOO PATH
331 # Check that one is gone and the other reverted to the built-in
332 systemctl show-environment |
grep '^FOO=$' && exit 1
333 systemctl show-environment |
grep '^PATH=.*testaddition$' && exit 1
334 systemctl show-environment |
grep -q '^PATH='
335 # Check import-environment
336 export IMPORT_THIS
=hello
337 export IMPORT_THIS_TOO
=world
338 systemctl import-environment IMPORT_THIS IMPORT_THIS_TOO
339 systemctl show-environment |
grep "^IMPORT_THIS=$IMPORT_THIS"
340 systemctl show-environment |
grep "^IMPORT_THIS_TOO=$IMPORT_THIS_TOO"
341 systemctl unset-environment IMPORT_THIS IMPORT_THIS_TOO
342 (! systemctl show-environment |
grep "^IMPORT_THIS=")
343 (! systemctl show-environment |
grep "^IMPORT_THIS_TOO=")
345 # test for sysv-generator (issue #24990)
346 if [[ -x /usr
/lib
/systemd
/system-generators
/systemd-sysv-generator
]]; then
347 # This is configurable via -Dsysvinit-path=, but we can't get the value
348 # at runtime, so let's just support the two most common paths for now.
349 [[ -d /etc
/rc.d
/init.d
]] && SYSVINIT_PATH
="/etc/rc.d/init.d" || SYSVINIT_PATH
="/etc/init.d"
352 cat >"${SYSVINIT_PATH:?}/issue-24990" <<\EOF
356 # Provides:test1 test2
357 # Required-Start:test1 $remote_fs $network
358 # Required-Stop:test1 $remote_fs $network
360 # Short-Description: Test
365 echo "Starting issue-24990.service"
369 echo "Stopping issue-24990.service"
373 echo "Usage: service test {start|stop|restart|status}"
378 chmod +x
"$SYSVINIT_PATH/issue-24990"
379 systemctl daemon-reload
380 [[ -L /run
/systemd
/generator.late
/test1.service
]]
381 [[ -L /run
/systemd
/generator.late
/test2.service
]]
382 assert_eq
"$(readlink -f /run/systemd/generator.late/test1.service)" "/run/systemd/generator.late/issue-24990.service"
383 assert_eq
"$(readlink -f /run/systemd/generator.late/test2.service)" "/run/systemd/generator.late/issue-24990.service"
384 output
=$
(systemctl
cat issue-24990
)
385 assert_in
"SourcePath=$SYSVINIT_PATH/issue-24990" "$output"
386 assert_in
"Description=LSB: Test" "$output"
387 assert_in
"After=test1.service" "$output"
388 assert_in
"After=remote-fs.target" "$output"
389 assert_in
"After=network-online.target" "$output"
390 assert_in
"Wants=network-online.target" "$output"
391 assert_in
"ExecStart=$SYSVINIT_PATH/issue-24990 start" "$output"
392 assert_in
"ExecStop=$SYSVINIT_PATH/issue-24990 stop" "$output"
393 systemctl status issue-24990 ||
:
394 systemctl show issue-24990
395 assert_not_in
"issue-24990.service" "$(systemctl show --property=After --value)"
396 assert_not_in
"issue-24990.service" "$(systemctl show --property=Before --value)"
398 if ! systemctl is-active network-online.target
; then
399 systemctl start network-online.target
402 systemctl restart issue-24990
403 systemctl stop issue-24990
406 cat >"$SYSVINIT_PATH/issue-24990" <<\EOF
410 # Provides:test1 test2
411 # Required-Start:$remote_fs
412 # Required-Stop:$remote_fs
414 # Short-Description: Test
419 echo "Starting issue-24990.service"
423 echo "Stopping issue-24990.service"
427 echo "Usage: service test {start|stop|restart|status}"
432 chmod +x
"$SYSVINIT_PATH/issue-24990"
433 systemctl daemon-reload
434 [[ -L /run
/systemd
/generator.late
/test1.service
]]
435 [[ -L /run
/systemd
/generator.late
/test2.service
]]
436 assert_eq
"$(readlink -f /run/systemd/generator.late/test1.service)" "/run/systemd/generator.late/issue-24990.service"
437 assert_eq
"$(readlink -f /run/systemd/generator.late/test2.service)" "/run/systemd/generator.late/issue-24990.service"
438 output
=$
(systemctl
cat issue-24990
)
439 assert_in
"SourcePath=$SYSVINIT_PATH/issue-24990" "$output"
440 assert_in
"Description=LSB: Test" "$output"
441 assert_in
"After=remote-fs.target" "$output"
442 assert_in
"ExecStart=$SYSVINIT_PATH/issue-24990 start" "$output"
443 assert_in
"ExecStop=$SYSVINIT_PATH/issue-24990 stop" "$output"
444 systemctl status issue-24990 ||
:
445 systemctl show issue-24990
446 assert_not_in
"issue-24990.service" "$(systemctl show --property=After --value)"
447 assert_not_in
"issue-24990.service" "$(systemctl show --property=Before --value)"
449 systemctl restart issue-24990
450 systemctl stop issue-24990
453 # %J in WantedBy= causes ABRT (#26467)
454 cat >/run
/systemd
/system
/test-WantedBy.service
<<EOF
459 WantedBy=user-%i@%J.service
461 systemctl daemon-reload
462 systemctl
enable --now test-WantedBy.service ||
:
463 systemctl daemon-reload