Skip to content

Commit

Permalink
selftests/bpf: Fix vmtest.sh -h to not require root
Browse files Browse the repository at this point in the history
Set the exit trap only after argument parsing is done. This way argument
parse failure or `-h` will not require sudo.

Reasoning is that it's confusing that a help message would require root
access.

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Daniel Müller <deso@posteo.net>
Link: https://lore.kernel.org/bpf/6a802aa37758e5a7e6aa5de294634f5518005e2b.1660064925.git.dxu@dxuuu.xyz
  • Loading branch information
Daniel Xu authored and Daniel Borkmann committed Aug 9, 2022
1 parent a00ed84 commit d020b23
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions tools/testing/selftests/bpf/vmtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ update_kconfig()
fi
}

catch()
{
local exit_code=$1
local exit_status_file="${OUTPUT_DIR}/${EXIT_STATUS_FILE}"
# This is just a cleanup and the directory may
# have already been unmounted. So, don't let this
# clobber the error code we intend to return.
unmount_image || true
if [[ -f "${exit_status_file}" ]]; then
exit_code="$(cat ${exit_status_file})"
fi
exit ${exit_code}
}

main()
{
local script_dir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
Expand Down Expand Up @@ -353,6 +367,8 @@ main()
done
shift $((OPTIND -1))

trap 'catch "$?"' EXIT

if [[ $# -eq 0 && "${debug_shell}" == "no" ]]; then
echo "No command specified, will run ${DEFAULT_COMMAND} in the vm"
else
Expand Down Expand Up @@ -409,20 +425,4 @@ main()
fi
}

catch()
{
local exit_code=$1
local exit_status_file="${OUTPUT_DIR}/${EXIT_STATUS_FILE}"
# This is just a cleanup and the directory may
# have already been unmounted. So, don't let this
# clobber the error code we intend to return.
unmount_image || true
if [[ -f "${exit_status_file}" ]]; then
exit_code="$(cat ${exit_status_file})"
fi
exit ${exit_code}
}

trap 'catch "$?"' EXIT

main "$@"

0 comments on commit d020b23

Please sign in to comment.