nmtst: add nmtst_reexec_sudo() function

Interpret environment variable NMTST_DEBUG which allows
to specify 'sudo-cmd=CMD'. If the test program calls
nmtst_reexec_sudo(), it will `exec CMD "$0" "$@"`.

Signed-off-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
Thomas Haller
2014-04-23 16:54:54 +02:00
parent 9df3a23d26
commit 7a7dd9203d
2 changed files with 78 additions and 0 deletions

29
tools/test-sudo-wrapper.sh Executable file
View File

@@ -0,0 +1,29 @@
#!/bin/bash
CMD="$1"
shift;
# convert the libtool internal path
resolve_cmd() {
local C="$1"
local C2="$(echo "$C" | sed 's#^\(.*/\)\.libs/lt-\([^/]\+\)$#\1\2#')"
if [[ "$C2" != "$C" && ! -x "$C2" ]]; then
# such a file does not exist... back to $C
C2="$C"
fi
echo "$C2"
}
if [[ $UID == 0 ]]; then
# we are already root. Execute directly.
exec "$(resolve_cmd "$CMD")" "$@"
elif [[ "$NMTST_SUDO_NO_CALL_SELF" != "" ]]; then
# when setting $NMTST_SUDO_NO_CALL_SELF, pass the (resolved) command
# directly to sudo.
exec sudo "$(resolve_cmd "$CMD")" "$@"
else
# by default, call self again with sudo.
exec sudo -E "$0" "$CMD" "$@"
fi