]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add test cases for ExecStart= via-shell prefix 37071/head
authorMike Yuan <me@yhndnzj.com>
Wed, 9 Apr 2025 15:35:33 +0000 (17:35 +0200)
committerMike Yuan <me@yhndnzj.com>
Wed, 7 May 2025 16:32:20 +0000 (18:32 +0200)
test/integration-tests/TEST-07-PID1/TEST-07-PID1.units/prefix-shell.service [new file with mode: 0755]
test/units/TEST-07-PID1.prefix-shell.sh [new file with mode: 0755]
test/units/TEST-74-AUX-UTILS.run.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 (executable)
index 0000000..fcbf8e9
--- /dev/null
@@ -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 (executable)
index 0000000..c095ce7
--- /dev/null
@@ -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"
index 3229a522b7e43b4bca200ea18569e3f5f8001408..19b4f113bb4df26bb71a7670d11cca6d58ff581b 100755 (executable)
@@ -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