public inbox for linux-kselftest@vger.kernel.org
 help / color / mirror / Atom feed
From: CaoRuichuang <create0818@163.com>
To: shuah@kernel.org
Cc: skhan@linuxfoundation.org, linux-kselftest@vger.kernel.org,
	CaoRuichuang <create0818@163.com>
Subject: [PATCH] selftests: kselftest: avoid mktemp -u in runner
Date: Mon,  6 Apr 2026 03:25:29 +0800	[thread overview]
Message-ID: <20260405192529.81684-1-create0818@163.com> (raw)

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)


                 reply	other threads:[~2026-04-05 19:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260405192529.81684-1-create0818@163.com \
    --to=create0818@163.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox