[LTP] [PATCH v2] syscalls/unshare: New test: CLONE_NEWNS unshares fs info
lufei
lufei@uniontech.com
Wed Apr 23 14:05:16 CEST 2025
Add test case unshare04, to verify unshare(CLONE_NEWNS) also unshares
filesystem information.
Signed-off-by: lufei <lufei@uniontech.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/unshare/.gitignore | 1 +
testcases/kernel/syscalls/unshare/unshare04.c | 82 +++++++++++++++++++
3 files changed, 84 insertions(+)
create mode 100644 testcases/kernel/syscalls/unshare/unshare04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 844ae7a13..57338297a 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1725,6 +1725,7 @@ unlinkat01 unlinkat01
unshare01 unshare01
unshare02 unshare02
unshare03 unshare03
+unshare04 unshare04
#
# These tests require an unmounted block device
diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore
index e5b5c261d..b1206e452 100644
--- a/testcases/kernel/syscalls/unshare/.gitignore
+++ b/testcases/kernel/syscalls/unshare/.gitignore
@@ -1,3 +1,4 @@
/unshare01
/unshare02
/unshare03
+/unshare04
diff --git a/testcases/kernel/syscalls/unshare/unshare04.c b/testcases/kernel/syscalls/unshare/unshare04.c
new file mode 100644
index 000000000..d76e6a836
--- /dev/null
+++ b/testcases/kernel/syscalls/unshare/unshare04.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 lufei <lufei@uniontech.com>
+ */
+
+/*\
+ * This test case is to verify unshare(CLONE_NEWNS) also unshares filesystem
+ * information.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include "tst_test.h"
+#include "lapi/sched.h"
+
+#define TMP "/tmp1"
+
+static char *cwd;
+static char *tmpdir;
+static char *c_cwd;
+static char *p_cwd;
+static size_t size = 1024;
+
+static void setup(void)
+{
+ cwd = SAFE_MALLOC(size);
+ SAFE_GETCWD(cwd, size);
+
+ tmpdir = tst_tmpdir_genpath(TMP);
+ SAFE_MKDIR(tmpdir, 0700);
+
+ c_cwd = SAFE_MALLOC(size);
+ p_cwd = SAFE_MALLOC(size);
+}
+
+static void cleanup(void)
+{
+ free(c_cwd);
+ free(p_cwd);
+ free(cwd);
+}
+
+
+static void run(void)
+{
+ struct tst_clone_args args = {
+ .flags = CLONE_FS,
+ .exit_signal = SIGCHLD,
+ };
+
+ if (!SAFE_CLONE(&args)) {
+
+ TST_EXP_PASS(unshare(CLONE_NEWNS));
+
+ SAFE_CHDIR(tmpdir);
+ SAFE_GETCWD(c_cwd, size);
+
+ if (strcmp(cwd, c_cwd) == 0)
+ tst_res(TFAIL, "current dir not changed");
+ else
+ tst_res(TPASS, "current dir changed to %s", c_cwd);
+ } else {
+ SAFE_WAIT(NULL);
+
+ SAFE_GETCWD(p_cwd, size);
+
+ if (strcmp(cwd, p_cwd) == 0)
+ tst_res(TPASS, "cwd unshared");
+ else
+ tst_res(TFAIL, "cwd not unshare as expected");
+ }
+}
+
+static struct tst_test test = {
+ .forks_child = 1,
+ .needs_root = 1,
+ .needs_tmpdir = 1,
+ .test_all = run,
+ .setup = setup,
+ .cleanup = cleanup,
+};
--
2.39.3
More information about the ltp
mailing list