From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE57A359F8B; Wed, 22 Oct 2025 16:10:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761149443; cv=none; b=ArxCUMumJNjN7SLKh/d4MEogqmJYgVPj7bydxBgTHeS+xqIYHtM+IvjQimQg88cYCpnTydiZOGMubkbXv5SLRPFlHBQ/ZAJWXH5BKmccCcjsLvDq2yFsp8LlpjPdpf0wi6cqMiMMOvevBStMbGXC/9Mz7YwOnOFW/jHHLiY/Fv0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761149443; c=relaxed/simple; bh=YEySNnwHHlcAx8fWB+paH4j8S+yvwldW7xKcAutT9lM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Au1yUX1WcmNdWQZ499PX3iUTDejc5OH2Nm2VdeUiDOvoxtsQwikS1j/krBQ9sx4aCklr3Zewbd5Ct+1vALONwyXxYrB45IB2nIjVd1QXELWriWVEmJBrjKYQGmh52atBc6P3Xl4gEVAST8cE4OMbgHme9xb40mvOSxX3QOlAckQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SYYCH3pi; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SYYCH3pi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89EB1C4CEE7; Wed, 22 Oct 2025 16:10:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1761149442; bh=YEySNnwHHlcAx8fWB+paH4j8S+yvwldW7xKcAutT9lM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SYYCH3piPyE/tqFlyW7bswylX36pM0Q8PLc5qrNfmZkr2hEgRZS1QqjBt0T1s46yJ MSZYvmrn6sORK88o844JWiPUO7WvDQovQ7egn54vaDAFcRMF9ajwS34D9xt6C8XuDf E2b9cMhMNTQlyLXyE2DaAitgdLQh3rb/Q2l6X9ipQAVsP3kAW0MiswPX5PWaixDbPd w3DE2LXiZrwCKtbcJ4sY+U1AbS8OwYsxS7qqraukV3VMlio8+c+OG1RiAc7GIMPcXd EPMeI8k08Cr2FXxQrqUAzfmrmodsP7hy2AeCjHZIspw9i2jTpmS4Y2ND6Ki7OZ4wIq 0AAD1Cd5WxDLg== From: Christian Brauner Date: Wed, 22 Oct 2025 18:06:30 +0200 Subject: [PATCH v2 52/63] selftests/namespaces: first inactive namespace resurrection test Precedence: bulk X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20251022-work-namespace-nstree-listns-v2-52-71a588572371@kernel.org> References: <20251022-work-namespace-nstree-listns-v2-0-71a588572371@kernel.org> In-Reply-To: <20251022-work-namespace-nstree-listns-v2-0-71a588572371@kernel.org> To: linux-fsdevel@vger.kernel.org, Josef Bacik , Jeff Layton Cc: Jann Horn , Mike Yuan , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , Lennart Poettering , Daan De Meyer , Aleksa Sarai , Amir Goldstein , Tejun Heo , Johannes Weiner , Thomas Gleixner , Alexander Viro , Jan Kara , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, bpf@vger.kernel.org, Eric Dumazet , Jakub Kicinski , netdev@vger.kernel.org, Arnd Bergmann , Christian Brauner X-Mailer: b4 0.15-dev-96507 X-Developer-Signature: v=1; a=openpgp-sha256; l=3689; i=brauner@kernel.org; h=from:subject:message-id; bh=YEySNnwHHlcAx8fWB+paH4j8S+yvwldW7xKcAutT9lM=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWT8ZHh+Vn5ZZ9yVPwyOwocOe3le1mf928+/p13Oe7LKG kOnV10HO0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACZyTYqRoZ3djvuJ+vmJJ09H 3FD2tYh/FWa6ULfw4Hl3ie1N/tq3lzMyNDWX5wueq9ot+/vQfF/rkI6tdVWeLV+P7z3Qn/Sc+cR pVgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Test basic SIOCGSKNS functionality. Create a socket and verify SIOCGSKNS returns the correct network namespace. Signed-off-by: Christian Brauner --- tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 9 ++- .../testing/selftests/namespaces/siocgskns_test.c | 72 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/namespaces/.gitignore b/tools/testing/selftests/namespaces/.gitignore index 17f9c675a60b..aeb5f2711ff6 100644 --- a/tools/testing/selftests/namespaces/.gitignore +++ b/tools/testing/selftests/namespaces/.gitignore @@ -4,3 +4,4 @@ init_ino_test ns_active_ref_test listns_test listns_permissions_test +siocgskns_test diff --git a/tools/testing/selftests/namespaces/Makefile b/tools/testing/selftests/namespaces/Makefile index 2dd22bc68b89..d456505189cd 100644 --- a/tools/testing/selftests/namespaces/Makefile +++ b/tools/testing/selftests/namespaces/Makefile @@ -2,11 +2,18 @@ CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) LDLIBS += -lcap -TEST_GEN_PROGS := nsid_test file_handle_test init_ino_test ns_active_ref_test listns_test listns_permissions_test +TEST_GEN_PROGS := nsid_test \ + file_handle_test \ + init_ino_test \ + ns_active_ref_test \ + listns_test \ + listns_permissions_test \ + siocgskns_test include ../lib.mk $(OUTPUT)/ns_active_ref_test: ../filesystems/utils.c $(OUTPUT)/listns_test: ../filesystems/utils.c $(OUTPUT)/listns_permissions_test: ../filesystems/utils.c +$(OUTPUT)/siocgskns_test: ../filesystems/utils.c diff --git a/tools/testing/selftests/namespaces/siocgskns_test.c b/tools/testing/selftests/namespaces/siocgskns_test.c new file mode 100644 index 000000000000..0c9098624cd4 --- /dev/null +++ b/tools/testing/selftests/namespaces/siocgskns_test.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../kselftest_harness.h" +#include "../filesystems/utils.h" +#include "wrappers.h" + +#ifndef SIOCGSKNS +#define SIOCGSKNS 0x894C +#endif + +#ifndef FD_NSFS_ROOT +#define FD_NSFS_ROOT -10003 +#endif + +#ifndef FILEID_NSFS +#define FILEID_NSFS 0xf1 +#endif + +/* + * Test basic SIOCGSKNS functionality. + * Create a socket and verify SIOCGSKNS returns the correct network namespace. + */ +TEST(siocgskns_basic) +{ + int sock_fd, netns_fd, current_netns_fd; + struct stat st1, st2; + + /* Create a TCP socket */ + sock_fd = socket(AF_INET, SOCK_STREAM, 0); + ASSERT_GE(sock_fd, 0); + + /* Use SIOCGSKNS to get network namespace */ + netns_fd = ioctl(sock_fd, SIOCGSKNS); + if (netns_fd < 0) { + close(sock_fd); + if (errno == ENOTTY || errno == EINVAL) + SKIP(return, "SIOCGSKNS not supported"); + ASSERT_GE(netns_fd, 0); + } + + /* Get current network namespace */ + current_netns_fd = open("/proc/self/ns/net", O_RDONLY); + ASSERT_GE(current_netns_fd, 0); + + /* Verify they match */ + ASSERT_EQ(fstat(netns_fd, &st1), 0); + ASSERT_EQ(fstat(current_netns_fd, &st2), 0); + ASSERT_EQ(st1.st_ino, st2.st_ino); + + close(sock_fd); + close(netns_fd); + close(current_netns_fd); +} + +TEST_HARNESS_MAIN -- 2.47.3