Skip to content

Commit d093090

Browse files
committed
selftests/namespaces: verify initial namespace inode numbers
Make sure that all works correctly. Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 1f84344 commit d093090

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
nsid_test
22
file_handle_test
3+
init_ino_test
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
33

4-
TEST_GEN_PROGS := nsid_test file_handle_test
4+
TEST_GEN_PROGS := nsid_test file_handle_test init_ino_test
55

66
include ../lib.mk
77

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright (c) 2025 Christian Brauner <brauner@kernel.org>
3+
4+
#define _GNU_SOURCE
5+
#include <fcntl.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <sys/stat.h>
9+
#include <unistd.h>
10+
#include <errno.h>
11+
#include <string.h>
12+
#include <linux/nsfs.h>
13+
14+
#include "../kselftest_harness.h"
15+
16+
struct ns_info {
17+
const char *name;
18+
const char *proc_path;
19+
unsigned int expected_ino;
20+
};
21+
22+
static struct ns_info namespaces[] = {
23+
{ "ipc", "/proc/1/ns/ipc", IPC_NS_INIT_INO },
24+
{ "uts", "/proc/1/ns/uts", UTS_NS_INIT_INO },
25+
{ "user", "/proc/1/ns/user", USER_NS_INIT_INO },
26+
{ "pid", "/proc/1/ns/pid", PID_NS_INIT_INO },
27+
{ "cgroup", "/proc/1/ns/cgroup", CGROUP_NS_INIT_INO },
28+
{ "time", "/proc/1/ns/time", TIME_NS_INIT_INO },
29+
{ "net", "/proc/1/ns/net", NET_NS_INIT_INO },
30+
{ "mnt", "/proc/1/ns/mnt", MNT_NS_INIT_INO },
31+
};
32+
33+
TEST(init_namespace_inodes)
34+
{
35+
struct stat st;
36+
37+
for (int i = 0; i < sizeof(namespaces) / sizeof(namespaces[0]); i++) {
38+
int ret = stat(namespaces[i].proc_path, &st);
39+
40+
/* Some namespaces might not be available (e.g., time namespace on older kernels) */
41+
if (ret < 0) {
42+
if (errno == ENOENT) {
43+
ksft_test_result_skip("%s namespace not available\n",
44+
namespaces[i].name);
45+
continue;
46+
}
47+
ASSERT_GE(ret, 0)
48+
TH_LOG("Failed to stat %s: %s",
49+
namespaces[i].proc_path, strerror(errno));
50+
}
51+
52+
ASSERT_EQ(st.st_ino, namespaces[i].expected_ino)
53+
TH_LOG("Namespace %s has inode 0x%lx, expected 0x%x",
54+
namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
55+
56+
ksft_print_msg("Namespace %s: inode 0x%lx matches expected 0x%x\n",
57+
namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
58+
}
59+
}
60+
61+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)