From e0b53a5dbbfdf8f7cb85bfa7f3c4f9650bc590ec Mon Sep 17 00:00:00 2001 From: Ryan Moeller Date: Thu, 11 Mar 2021 15:01:58 -0500 Subject: [PATCH] ZTS: Use ksh and current environment for user_run The current user_run often does not work as expected. Commands are run in a different shell, with a different environment, and all output is discarded. Simplify user_run to retain the current environment, eliminate eval, and feed the command string into ksh. Enhance the logging for user_run so we can see out and err. Reviewed-by: John Kennedy Reviewed-by: Brian Behlendorf Signed-off-by: Ryan Moeller Closes #11185 --- tests/zfs-tests/include/commands.cfg | 2 +- tests/zfs-tests/include/libtest.shlib | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/zfs-tests/include/commands.cfg b/tests/zfs-tests/include/commands.cfg index a43ddd016f..0db9724eea 100644 --- a/tests/zfs-tests/include/commands.cfg +++ b/tests/zfs-tests/include/commands.cfg @@ -33,6 +33,7 @@ export SYSTEM_FILES_COMMON='arp du echo egrep + env expr false file @@ -117,7 +118,6 @@ export SYSTEM_FILES_FREEBSD='chflags compress diskinfo dumpon - env fsck getextattr gpart diff --git a/tests/zfs-tests/include/libtest.shlib b/tests/zfs-tests/include/libtest.shlib index d494eda553..bd77bae7d9 100644 --- a/tests/zfs-tests/include/libtest.shlib +++ b/tests/zfs-tests/include/libtest.shlib @@ -41,7 +41,7 @@ # PATH may have been modified by sudo's secure_path behavior. # if [ -n "$STF_PATH" ]; then - PATH="$STF_PATH" + export PATH="$STF_PATH" fi # @@ -2766,10 +2766,6 @@ function add_user # ;; esac - echo "export PATH=\"$STF_PATH\"" >>$basedir/$user/.profile - echo "export PATH=\"$STF_PATH\"" >>$basedir/$user/.bash_profile - echo "export PATH=\"$STF_PATH\"" >>$basedir/$user/.login - return 0 } @@ -3393,8 +3389,17 @@ function user_run typeset user=$1 shift - log_note "user:$user $@" - eval su - \$user -c \"$@\" > $TEST_BASE_DIR/out 2>$TEST_BASE_DIR/err + log_note "user: $user" + log_note "cmd: $*" + + typeset out=$TEST_BASE_DIR/out + typeset err=$TEST_BASE_DIR/err + + sudo -Eu $user env PATH="$PATH" ksh <<<"$*" >$out 2>$err + typeset res=$? + log_note "out: $(<$out)" + log_note "err: $(<$err)" + return $res } #