run_in_netns() uses mktemp -u to generate both the network namespace name and 
the per-test log path. That pattern is inherently racy because it only prints a 
candidate name without creating anything.

Switch to mktemp() and mktemp -d so the temporary path is created atomically 
first, then derive the namespace name from the allocated path. Also return 
immediately when ip netns add fails instead of continuing in a broken state.

Signed-off-by: CaoRuichuang <[email protected]>
---
 tools/testing/selftests/kselftest/runner.sh | 24 +++++++++++++--------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/tools/testing/selftests/kselftest/runner.sh 
b/tools/testing/selftests/kselftest/runner.sh
index 3a62039fa..13cdbaf36 100644
--- a/tools/testing/selftests/kselftest/runner.sh
+++ b/tools/testing/selftests/kselftest/runner.sh
@@ -164,18 +164,24 @@ in_netns()
 
 run_in_netns()
 {
-       local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)
-       local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"
-       ip netns add $netns
-       if [ $? -ne 0 ]; then
+       local netns_tmp netns tmplog
+
+       netns_tmp=$(mktemp -d "/tmp/${BASENAME_TEST}-netns-XXXXXX")
+       netns=${netns_tmp##*/}
+       rmdir "$netns_tmp"
+       tmplog=$(mktemp "/tmp/${BASENAME_TEST}-log-XXXXXX")
+
+       if ! ip netns add "$netns"; then
                echo "# Warning: Create namespace failed for $BASENAME_TEST"
                echo "not ok $test_num selftests: $DIR: $BASENAME_TEST # Create 
NS failed"
+               rm -f "$tmplog"
+               return
        fi
-       ip -n $netns link set lo up
-       in_netns $netns &> $tmplog
-       ip netns del $netns &> /dev/null
-       cat $tmplog
-       rm -f $tmplog
+       ip -n "$netns" link set lo up
+       in_netns "$netns" >"$tmplog" 2>&1
+       ip netns del "$netns" >/dev/null 2>&1
+       cat "$tmplog"
+       rm -f "$tmplog"
 }
 
 run_many()
-- 
2.39.5 (Apple Git-154)


Reply via email to