Skip to contentSkip to conversation

selftests/namespaces: verify initial namespace inode numbers

PATCH In review Christian Brauner 3 participants 3 messages Sep 19, 2025

Subsystems (2)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

PATCH 1/1 selftests/namespaces: verify initial namespace inode numbers

Make sure that all works correctly.

---
 tools/testing/selftests/namespaces/.gitignore      |  1 +
 tools/testing/selftests/namespaces/Makefile        |  2 +-
 tools/testing/selftests/namespaces/init_ino_test.c | 60 ++++++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
tools/testing/selftests/namespaces/.gitignore +1 -0
index 7639dbf58bbf..ccfb40837a73 100644
@@ -1,2 +1,3 @@
 nsid_test
 file_handle_test
+init_ino_test
tools/testing/selftests/namespaces/Makefile +1 -1
index f6c117ce2c2b..5fe4b3dc07d3 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
 
-TEST_GEN_PROGS := nsid_test file_handle_test
+TEST_GEN_PROGS := nsid_test file_handle_test init_ino_test
 
 include ../lib.mk
 
tools/testing/selftests/namespaces/init_ino_test.c new file +60 -0

60 lines not on this page · raw message

---
base-commit: 5a9b4dfe901cecd4e06692bb877b393459e4d50d
change-id: 20250919-work-namespace-selftests-7b478f415792

Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Christian Brauner

Make sure that all works correctly.

Looks sane!

Quoted 98 lines
---
 tools/testing/selftests/namespaces/.gitignore      |  1 +
 tools/testing/selftests/namespaces/Makefile        |  2 +-
 tools/testing/selftests/namespaces/init_ino_test.c | 60 ++++++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
tools/testing/selftests/namespaces/.gitignore +1 -0
index 7639dbf58bbf..ccfb40837a73 100644
@@ -1,2 +1,3 @@
 nsid_test
 file_handle_test
+init_ino_test
tools/testing/selftests/namespaces/Makefile +1 -1
index f6c117ce2c2b..5fe4b3dc07d3 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
 
-TEST_GEN_PROGS := nsid_test file_handle_test
+TEST_GEN_PROGS := nsid_test file_handle_test init_ino_test
 
 include ../lib.mk
 
tools/testing/selftests/namespaces/init_ino_test.c new file +60 -1
new file mode 100644
index 000000000000..ddd5008d46a6
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// Copyright (c) 2025 Christian Brauner <[email protected]>
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <linux/nsfs.h>
+
+#include "../kselftest_harness.h"
+
+struct ns_info {
+       const char *name;
+       const char *proc_path;
+       unsigned int expected_ino;
+};
+
+static struct ns_info namespaces[] = {
+       { "ipc",    "/proc/1/ns/ipc",    IPC_NS_INIT_INO },
+       { "uts",    "/proc/1/ns/uts",    UTS_NS_INIT_INO },
+       { "user",   "/proc/1/ns/user",   USER_NS_INIT_INO },
+       { "pid",    "/proc/1/ns/pid",    PID_NS_INIT_INO },
+       { "cgroup", "/proc/1/ns/cgroup", CGROUP_NS_INIT_INO },
+       { "time",   "/proc/1/ns/time",   TIME_NS_INIT_INO },
+       { "net",    "/proc/1/ns/net",    NET_NS_INIT_INO },
+       { "mnt",    "/proc/1/ns/mnt",    MNT_NS_INIT_INO },
+};
+
+TEST(init_namespace_inodes)
+{
+       struct stat st;
+
+       for (int i = 0; i < sizeof(namespaces) / sizeof(namespaces[0]); i++) {
+               int ret = stat(namespaces[i].proc_path, &st);
+
+               /* Some namespaces might not be available (e.g., time namespace on older kernels) */
+               if (ret < 0) {
+                       if (errno == ENOENT) {
+                               ksft_test_result_skip("%s namespace not available\n", namespaces[i].name);
+                               continue;
+                       }
+                       ASSERT_GE(ret, 0)
+                               TH_LOG("Failed to stat %s: %s", namespaces[i].proc_path, strerror(errno));
+               }
+
+               ASSERT_EQ(st.st_ino, namespaces[i].expected_ino) {
+                       TH_LOG("Namespace %s has inode 0x%lx, expected 0x%x",
+                              namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
+               }
+
+               ksft_print_msg("Namespace %s: inode 0x%lx matches expected 0x%x\n",
+                              namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
+       }
+}
+
+TEST_HARNESS_MAIN
 
---

base-commit: 5a9b4dfe901cecd4e06692bb877b393459e4d50d
change-id: 20250919-work-namespace-selftests-7b478f415792

Reviewed-by on v1 1/1
#2 lore raw
In reply to Christian Brauner
Quoted 3 lines · Christian Brauner

Make sure that all works correctly.

Looks good, feel free to take my

Quoted 98 lines
---
 tools/testing/selftests/namespaces/.gitignore      |  1 +
 tools/testing/selftests/namespaces/Makefile        |  2 +-
 tools/testing/selftests/namespaces/init_ino_test.c | 60 ++++++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
tools/testing/selftests/namespaces/.gitignore +1 -0
index 7639dbf58bbf..ccfb40837a73 100644
@@ -1,2 +1,3 @@
 nsid_test
 file_handle_test
+init_ino_test
tools/testing/selftests/namespaces/Makefile +1 -1
index f6c117ce2c2b..5fe4b3dc07d3 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
 
-TEST_GEN_PROGS := nsid_test file_handle_test
+TEST_GEN_PROGS := nsid_test file_handle_test init_ino_test
 
 include ../lib.mk
 
tools/testing/selftests/namespaces/init_ino_test.c new file +60 -1
new file mode 100644
index 000000000000..ddd5008d46a6
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// Copyright (c) 2025 Christian Brauner <[email protected]>
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <linux/nsfs.h>
+
+#include "../kselftest_harness.h"
+
+struct ns_info {
+	const char *name;
+	const char *proc_path;
+	unsigned int expected_ino;
+};
+
+static struct ns_info namespaces[] = {
+	{ "ipc",    "/proc/1/ns/ipc",    IPC_NS_INIT_INO },
+	{ "uts",    "/proc/1/ns/uts",    UTS_NS_INIT_INO },
+	{ "user",   "/proc/1/ns/user",   USER_NS_INIT_INO },
+	{ "pid",    "/proc/1/ns/pid",    PID_NS_INIT_INO },
+	{ "cgroup", "/proc/1/ns/cgroup", CGROUP_NS_INIT_INO },
+	{ "time",   "/proc/1/ns/time",   TIME_NS_INIT_INO },
+	{ "net",    "/proc/1/ns/net",    NET_NS_INIT_INO },
+	{ "mnt",    "/proc/1/ns/mnt",    MNT_NS_INIT_INO },
+};
+
+TEST(init_namespace_inodes)
+{
+	struct stat st;
+
+	for (int i = 0; i < sizeof(namespaces) / sizeof(namespaces[0]); i++) {
+		int ret = stat(namespaces[i].proc_path, &st);
+		
+		/* Some namespaces might not be available (e.g., time namespace on older kernels) */
+		if (ret < 0) {
+			if (errno == ENOENT) {
+				ksft_test_result_skip("%s namespace not available\n", namespaces[i].name);
+				continue;
+			}
+			ASSERT_GE(ret, 0)
+				TH_LOG("Failed to stat %s: %s", namespaces[i].proc_path, strerror(errno));
+		}
+
+		ASSERT_EQ(st.st_ino, namespaces[i].expected_ino) {
+			TH_LOG("Namespace %s has inode 0x%lx, expected 0x%x",
+			       namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
+		}
+
+		ksft_print_msg("Namespace %s: inode 0x%lx matches expected 0x%x\n",
+			       namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
+	}
+}
+
+TEST_HARNESS_MAIN
 
---

base-commit: 5a9b4dfe901cecd4e06692bb877b393459e4d50d
change-id: 20250919-work-namespace-selftests-7b478f415792

Signature · 4 lines
Aleksa Sarai
Senior Software Engineer (Containers)
SUSE Linux GmbH
https://www.cyphar.com/