From 66a3b69440745521b516707f59618ef68185f1b3 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Wed, 9 Apr 2025 17:35:33 +0200 Subject: [PATCH] test: add test cases for ExecStart= via-shell prefix --- .../TEST-07-PID1.units/prefix-shell.service | 8 +++++++ test/units/TEST-07-PID1.prefix-shell.sh | 22 +++++++++++++++++++ test/units/TEST-74-AUX-UTILS.run.sh | 10 +++++++++ 3 files changed, 40 insertions(+) create mode 100755 test/integration-tests/TEST-07-PID1/TEST-07-PID1.units/prefix-shell.service create mode 100755 test/units/TEST-07-PID1.prefix-shell.sh diff --git a/test/integration-tests/TEST-07-PID1/TEST-07-PID1.units/prefix-shell.service b/test/integration-tests/TEST-07-PID1/TEST-07-PID1.units/prefix-shell.service new file mode 100755 index 00000000000..fcbf8e9b150 --- /dev/null +++ b/test/integration-tests/TEST-07-PID1/TEST-07-PID1.units/prefix-shell.service @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +[Service] +Type=oneshot +Environment=SHLVL=100 +ExecStartPre=-|false +ExecStart=|@echo with login shell $$SHELL: lvl $$SHLVL +ExecStart=:|"str='with normal shell'" printenv str +ExecStart=|echo YAY! >/tmp/TEST-07-PID1.prefix-shell.flag diff --git a/test/units/TEST-07-PID1.prefix-shell.sh b/test/units/TEST-07-PID1.prefix-shell.sh new file mode 100755 index 00000000000..c095ce77318 --- /dev/null +++ b/test/units/TEST-07-PID1.prefix-shell.sh @@ -0,0 +1,22 @@ +#!/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 + +systemd-run --wait --uid=nobody \ + -p ExecStartPre="|true" \ + -p ExecStartPre="|@echo a >/tmp/TEST-07-PID1.prefix-shell.flag" \ + true +assert_eq "$(cat /tmp/TEST-07-PID1.prefix-shell.flag)" "a" +rm /tmp/TEST-07-PID1.prefix-shell.flag + +systemctl start prefix-shell.service +assert_eq "$(cat /tmp/TEST-07-PID1.prefix-shell.flag)" "YAY!" + +journalctl --sync +journalctl -b -u prefix-shell.service --grep "with login shell .*: lvl 101" +journalctl -b -u prefix-shell.service --grep "with normal shell" diff --git a/test/units/TEST-74-AUX-UTILS.run.sh b/test/units/TEST-74-AUX-UTILS.run.sh index 3229a522b7e..19b4f113bb4 100755 --- a/test/units/TEST-74-AUX-UTILS.run.sh +++ b/test/units/TEST-74-AUX-UTILS.run.sh @@ -256,10 +256,20 @@ if [[ -e /usr/lib/pam.d/systemd-run0 ]] || [[ -e /etc/pam.d/systemd-run0 ]]; the # Validate that we actually went properly through PAM (XDG_SESSION_TYPE is set by pam_systemd) assert_eq "$(run0 ${tu:+"--user=$tu"} bash -c 'echo $XDG_SESSION_TYPE')" "unspecified" + # Test spawning via shell + assert_eq "$(run0 ${tu:+"--user=$tu"} --setenv=SHLVL=10 printenv SHLVL)" "10" + if [[ ! -v ASAN_OPTIONS ]]; then + assert_eq "$(run0 ${tu:+"--user=$tu"} --setenv=SHLVL=10 --via-shell echo \$SHLVL)" "11" + fi + if [[ -n "$tu" ]]; then # Validate that $SHELL is set to login shell of target user when cmdline is supplied (not invoking shell) TARGET_LOGIN_SHELL="$(getent passwd "$tu" | cut -d: -f7)" assert_eq "$(run0 --user="$tu" printenv SHELL)" "$TARGET_LOGIN_SHELL" + # ... or when the command is chained by login shell + if [[ ! -v ASAN_OPTIONS ]]; then + assert_eq "$(run0 --user="$tu" -i printenv SHELL)" "$TARGET_LOGIN_SHELL" + fi fi done # Let's chain a couple of run0 calls together, for fun -- 2.47.3