vmtest.sh currently uses mktemp -u to precompute Unix socket paths for the namespace bridge helpers. That only returns an unused pathname and leaves a time-of-check/time-of-use window before socat binds or connects to it. Create a private temporary directory with mktemp -d and place the socket path inside it instead. This removes the pathname race while keeping cleanup straightforward. Signed-off-by: Cao Ruichuang --- v3: - restore the missing patch description - add Bobby Eshleman to Cc tools/testing/selftests/vsock/vmtest.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh index 86e338886b3..c345fa539d3 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -718,6 +718,7 @@ test_ns_diff_global_host_connect_to_global_vm_ok() { local pids pid pidfile local ns0 ns1 port declare -a pids + local unixdir local unixfile ns0="global0" ns1="global1" @@ -736,7 +737,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() { oops_before=$(vm_dmesg_oops_count "${ns0}") warn_before=$(vm_dmesg_warn_count "${ns0}") - unixfile=$(mktemp -u /tmp/XXXX.sock) + unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX) + unixfile="${unixdir}/sock" ip netns exec "${ns1}" \ socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \ UNIX-CONNECT:"${unixfile}" & @@ -758,6 +760,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() { terminate_pids "${pids[@]}" terminate_pidfiles "${pidfile}" + rm "${unixfile}" + rmdir "${unixdir}" if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then return "${KSFT_FAIL}" @@ -814,6 +818,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() { local ns0="global0" local ns1="global1" local port=12345 + local unixdir local unixfile local dmesg_rc local pidfile @@ -826,7 +831,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() { log_host "Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port}" - unixfile=$(mktemp -u /tmp/XXXX.sock) + unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX) + unixfile="${unixdir}/sock" ip netns exec "${ns0}" \ socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" & @@ -845,7 +851,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() { if ! vm_start "${pidfile}" "${ns0}"; then log_host "failed to start vm (cid=${cid}, ns=${ns0})" terminate_pids "${pids[@]}" - rm -f "${unixfile}" + rm "${unixfile}" + rmdir "${unixdir}" return "${KSFT_FAIL}" fi @@ -862,7 +869,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() { terminate_pidfiles "${pidfile}" terminate_pids "${pids[@]}" - rm -f "${unixfile}" + rm "${unixfile}" + rmdir "${unixdir}" if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then return "${KSFT_FAIL}" -- 2.39.5 (Apple Git-154)