#!/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 --help --no-pager systemd-run --version systemd-run --no-ask-password true systemd-run --no-block --collect true export PARENT_FOO=bar touch /tmp/public-marker : "Transient service (system daemon)" systemd-run --wait --pipe \ bash -xec '[[ "$(/dev/null systemd-run --json=pretty /bin/true | jq . >/dev/null (! SHELL=/bin/false systemd-run --quiet --shell) (! systemd-run) (! systemd-run "") (! systemd-run --foo=bar) (! systemd-run --wait --pipe --slice=foo.service true) for opt in nice on-{active,boot,calendar,startup,unit-active,unit-inactive} property service-type setenv; do (! systemd-run "--$opt=" true) (! systemd-run "--$opt=''" true) done # Let's make sure that ProtectProc= properly moves submounts of the original /proc over to the new proc BOOT_ID="$("$TMP_KVER" mount --bind "$TMP_KVER" /proc/version UNIT_KVER="$(systemd-run -q --wait --pipe -p ProtectProc=invisible cat /proc/version)" assert_eq "$KVER" "$UNIT_KVER" umount /proc/version rm -f "$TMP_KVER" if [[ -e /usr/lib/pam.d/systemd-run0 ]] || [[ -e /etc/pam.d/systemd-run0 ]]; then # Check that invoking the tool under the run0 alias name works run0 ls / assert_eq "$(run0 echo foo)" "foo" # Check if we set some expected environment variables for arg in "" "--user=root" "--user=0" "--user=testuser"; do assert_eq "$(run0 ${arg:+"$arg"} bash -c 'echo $SUDO_USER')" "$USER" assert_eq "$(run0 ${arg:+"$arg"} bash -c 'echo $SUDO_UID')" "$(id -u "$USER")" assert_eq "$(run0 ${arg:+"$arg"} bash -c 'echo $SUDO_GID')" "$(id -u "$USER")" # Validate that we actually went properly through PAM (XDG_SESSION_TYPE is set by pam_systemd) assert_eq "$(run0 ${arg:+"$arg"} bash -c 'echo $XDG_SESSION_TYPE')" "unspecified" done # Let's chain a couple of run0 calls together, for fun readarray -t cmdline < <(printf "%.0srun0\n" {0..31}) assert_eq "$("${cmdline[@]}" bash -c 'echo $SUDO_USER')" "$USER" # Tests for working directory, especially for specifying "/" (see PR #34771). cd / assert_eq "$(run0 pwd)" "/" assert_eq "$(run0 -D /tmp pwd)" "/tmp" assert_eq "$(run0 --user=testuser pwd)" "/home/testuser" assert_eq "$(run0 -D /tmp --user=testuser pwd)" "/tmp" cd /tmp assert_eq "$(run0 pwd)" "/tmp" assert_eq "$(run0 -D / pwd)" "/" assert_eq "$(run0 --user=testuser pwd)" "/home/testuser" assert_eq "$(run0 -D / --user=testuser pwd)" "/" # Verify that all combinations of --pty/--pipe come to the sam results assert_eq "$(run0 echo -n foo)" "foo" assert_eq "$(run0 --pty echo -n foo)" "foo" assert_eq "$(run0 --pipe echo -n foo)" "foo" assert_eq "$(run0 --pipe --pty echo -n foo)" "foo" # Validate when we invoke run0 without a tty, that depending on --pty it either allocates a tty or not assert_neq "$(run0 --pty tty < /dev/null)" "not a tty" assert_eq "$(run0 --pipe tty < /dev/null)" "not a tty" fi # Tests whether intermediate disconnects corrupt us (modified testcase from https://github.com/systemd/systemd/issues/27204) assert_rc "37" systemd-run --unit=disconnecttest --wait --pipe --user -M testuser@.host bash -ec 'systemctl --user daemon-reexec; sleep 3; exit 37'