--- /dev/null
+# 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
--- /dev/null
+#!/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"
# 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