#!/usr/bin/env bash # SPDX-License-Identifier: LGPL-2.1-or-later # shellcheck disable=SC2016 set -eux set -o pipefail # shellcheck source=test/units/util.sh . "$(dirname "$0")"/util.sh at_exit() { if [[ -v UNIT_NAME && -e "/usr/lib/systemd/system/$UNIT_NAME" ]]; then rm -fvr "/usr/lib/systemd/system/$UNIT_NAME" "/etc/systemd/system/$UNIT_NAME.d" "+4" fi rm -f /etc/init.d/issue-24990 return 0 } # Create a simple unit file for testing # Note: the service file is created under /usr on purpose to test # the 'revert' verb as well export UNIT_NAME="systemctl-test-$RANDOM.service" export UNIT_NAME2="systemctl-test-$RANDOM.service" cat >"/usr/lib/systemd/system/$UNIT_NAME" <<\EOF [Unit] Description=systemctl test [Service] ExecStart=sleep infinity ExecReload=true # For systemctl clean CacheDirectory=%n ConfigurationDirectory=%n LogsDirectory=%n RuntimeDirectory=%n StateDirectory=%n [Install] WantedBy=multi-user.target EOF # Configure the preset setting for the unit file mkdir /run/systemd/system-preset/ echo "disable $UNIT_NAME" >/run/systemd/system-preset/99-systemd-test.preset EDITOR='true' script -ec 'systemctl edit "$UNIT_NAME"' /dev/null [ ! -e "/etc/systemd/system/$UNIT_NAME.d/override.conf" ] printf '%s\n' '[Service]' 'ExecStart=' 'ExecStart=sleep 10d' >"+4" EDITOR='mv' script -ec 'systemctl edit "$UNIT_NAME"' /dev/null printf '%s\n' '[Service]' 'ExecStart=' 'ExecStart=sleep 10d' | cmp - "/etc/systemd/system/$UNIT_NAME.d/override.conf" printf '%b' '[Service]\n' 'ExecStart=\n' 'ExecStart=sleep 10d' >"+4" EDITOR='mv' script -ec 'systemctl edit "$UNIT_NAME"' /dev/null printf '%s\n' '[Service]' 'ExecStart=' 'ExecStart=sleep 10d' | cmp - "/etc/systemd/system/$UNIT_NAME.d/override.conf" systemctl edit "$UNIT_NAME" --stdin --drop-in=override2.conf </run/systemd/system/test-disable@.service </dev/null || : systemctl status -a --state active,running,plugged >/dev/null systemctl status "systemd-*.timer" systemctl status "systemd-journald*.socket" systemctl status "sys-devices-*-ttyS0.device" systemctl status -- -.mount systemctl status 1 # --marked systemctl restart "$UNIT_NAME" systemctl set-property "$UNIT_NAME" Markers=needs-restart systemctl show -P Markers "$UNIT_NAME" | grep needs-restart systemctl reload-or-restart --marked (! systemctl show -P Markers "$UNIT_NAME" | grep needs-restart) # --dry-run with destructive verbs # kexec is skipped intentionally, as it requires a bit more involved setup VERBS=( default emergency exit halt hibernate hybrid-sleep poweroff reboot rescue suspend suspend-then-hibernate ) for verb in "${VERBS[@]}"; do systemctl --dry-run "$verb" if [[ "$verb" =~ (halt|poweroff|reboot) ]]; then systemctl --dry-run --message "Hello world" "$verb" systemctl --dry-run --no-wall "$verb" systemctl --dry-run -f "$verb" systemctl --dry-run -ff "$verb" fi done # Aux verbs & assorted checks systemctl is-active "*-journald.service" systemctl cat "*udevd*" systemctl cat "$UNIT_NAME" systemctl help "$UNIT_NAME" systemctl service-watchdogs systemctl service-watchdogs "$(systemctl service-watchdogs)" # show/set-environment # Make sure PATH is set systemctl show-environment | grep -q '^PATH=' # Let's add an entry and override a built-in one systemctl set-environment PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/testaddition FOO=BAR # Check that both are set systemctl show-environment | grep -q '^PATH=.*testaddition$' systemctl show-environment | grep -q '^FOO=BAR$' systemctl daemon-reload # Check again after the reload systemctl show-environment | grep -q '^PATH=.*testaddition$' systemctl show-environment | grep -q '^FOO=BAR$' # Check that JSON output is supported systemctl show-environment --output=json | grep -q '^{.*"FOO":"BAR".*}$' # Drop both systemctl unset-environment FOO PATH # Check that one is gone and the other reverted to the built-in systemctl show-environment | grep '^FOO=$' && exit 1 systemctl show-environment | grep '^PATH=.*testaddition$' && exit 1 systemctl show-environment | grep -q '^PATH=' # Check import-environment export IMPORT_THIS=hello export IMPORT_THIS_TOO=world systemctl import-environment IMPORT_THIS IMPORT_THIS_TOO systemctl show-environment | grep "^IMPORT_THIS=$IMPORT_THIS" systemctl show-environment | grep "^IMPORT_THIS_TOO=$IMPORT_THIS_TOO" systemctl unset-environment IMPORT_THIS IMPORT_THIS_TOO (! systemctl show-environment | grep "^IMPORT_THIS=") (! systemctl show-environment | grep "^IMPORT_THIS_TOO=") # test for sysv-generator (issue #24990) if [[ -x /usr/lib/systemd/system-generators/systemd-sysv-generator ]]; then # This is configurable via -Dsysvinit-path=, but we can't get the value # at runtime, so let's just support the two most common paths for now. [[ -d /etc/rc.d/init.d ]] && SYSVINIT_PATH="/etc/rc.d/init.d" || SYSVINIT_PATH="/etc/init.d" # OpenSUSE leaves sysvinit-path enabled, which means systemd-sysv-generator is built # but may not create the directory if there's no services that use it. mkdir -p "$SYSVINIT_PATH" # invalid dependency cat >"${SYSVINIT_PATH:?}/issue-24990" <<\EOF #!/bin/bash ### BEGIN INIT INFO # Provides:test1 test2 # Required-Start:test1 $remote_fs $network # Required-Stop:test1 $remote_fs $network # Description:Test # Short-Description: Test ### END INIT INFO case "$1" in start) echo "Starting issue-24990.service" sleep 1000 & ;; stop) echo "Stopping issue-24990.service" sleep 10 & ;; *) echo "Usage: service test {start|stop|restart|status}" ;; esac EOF chmod +x "$SYSVINIT_PATH/issue-24990" systemctl daemon-reload [[ -L /run/systemd/generator.late/test1.service ]] [[ -L /run/systemd/generator.late/test2.service ]] assert_eq "$(readlink -f /run/systemd/generator.late/test1.service)" "/run/systemd/generator.late/issue-24990.service" assert_eq "$(readlink -f /run/systemd/generator.late/test2.service)" "/run/systemd/generator.late/issue-24990.service" output=$(systemctl cat issue-24990) assert_in "SourcePath=$SYSVINIT_PATH/issue-24990" "$output" assert_in "Description=LSB: Test" "$output" assert_in "After=test1.service" "$output" assert_in "After=remote-fs.target" "$output" assert_in "After=network-online.target" "$output" assert_in "Wants=network-online.target" "$output" assert_in "ExecStart=$SYSVINIT_PATH/issue-24990 start" "$output" assert_in "ExecStop=$SYSVINIT_PATH/issue-24990 stop" "$output" systemctl status issue-24990 || : systemctl show issue-24990 assert_not_in "issue-24990.service" "$(systemctl show --property=After --value)" assert_not_in "issue-24990.service" "$(systemctl show --property=Before --value)" if ! systemctl is-active network-online.target; then systemctl start network-online.target fi systemctl restart issue-24990 systemctl stop issue-24990 # valid dependency cat >"$SYSVINIT_PATH/issue-24990" <<\EOF #!/bin/bash ### BEGIN INIT INFO # Provides:test1 test2 # Required-Start:$remote_fs # Required-Stop:$remote_fs # Description:Test # Short-Description: Test ### END INIT INFO case "$1" in start) echo "Starting issue-24990.service" sleep 1000 & ;; stop) echo "Stopping issue-24990.service" sleep 10 & ;; *) echo "Usage: service test {start|stop|restart|status}" ;; esac EOF chmod +x "$SYSVINIT_PATH/issue-24990" systemctl daemon-reload [[ -L /run/systemd/generator.late/test1.service ]] [[ -L /run/systemd/generator.late/test2.service ]] assert_eq "$(readlink -f /run/systemd/generator.late/test1.service)" "/run/systemd/generator.late/issue-24990.service" assert_eq "$(readlink -f /run/systemd/generator.late/test2.service)" "/run/systemd/generator.late/issue-24990.service" output=$(systemctl cat issue-24990) assert_in "SourcePath=$SYSVINIT_PATH/issue-24990" "$output" assert_in "Description=LSB: Test" "$output" assert_in "After=remote-fs.target" "$output" assert_in "ExecStart=$SYSVINIT_PATH/issue-24990 start" "$output" assert_in "ExecStop=$SYSVINIT_PATH/issue-24990 stop" "$output" systemctl status issue-24990 || : systemctl show issue-24990 assert_not_in "issue-24990.service" "$(systemctl show --property=After --value)" assert_not_in "issue-24990.service" "$(systemctl show --property=Before --value)" systemctl restart issue-24990 systemctl stop issue-24990 fi # %J in WantedBy= causes ABRT (#26467) cat >/run/systemd/system/test-WantedBy.service <