public inbox for linux-kselftest@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests: kselftest: avoid mktemp -u in runner
@ 2026-04-05 19:25 CaoRuichuang
  0 siblings, 0 replies; only message in thread
From: CaoRuichuang @ 2026-04-05 19:25 UTC (permalink / raw)
  To: shuah; +Cc: skhan, linux-kselftest, CaoRuichuang

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 <create0818@163.com>
---
 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)


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-04-05 19:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-05 19:25 [PATCH] selftests: kselftest: avoid mktemp -u in runner CaoRuichuang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox