diff mbox series

[v3] syscalls/unshare: New test: CLONE_NEWNS unshares fs info

Message ID 20250425093355.14063-1-lufei@uniontech.com
State Accepted
Headers show
Series [v3] syscalls/unshare: New test: CLONE_NEWNS unshares fs info | expand

Checks

Context Check Description
ltpci/debian_stable_s390x-linux-gnu-gcc_s390x success success
ltpci/debian_stable_aarch64-linux-gnu-gcc_arm64 success success
ltpci/debian_stable_powerpc64le-linux-gnu-gcc_ppc64el success success
ltpci/debian_stable_gcc success success
ltpci/debian_stable_gcc success success
ltpci/quay-io-centos-centos_stream9_gcc success success
ltpci/ubuntu_jammy_gcc success success
ltpci/ubuntu_bionic_gcc success success
ltpci/opensuse-leap_latest_gcc success success
ltpci/debian_oldstable_clang success success
ltpci/fedora_latest_clang success success
ltpci/debian_testing_gcc success success
ltpci/debian_testing_clang success success
ltpci/alpine_latest_gcc success success
ltpci/opensuse-archive_42-2_gcc success success
ltpci/debian_oldstable_gcc success success

Commit Message

lufei April 25, 2025, 9:33 a.m. UTC
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 | 77 +++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare04.c

Comments

Cyril Hrubis April 25, 2025, 12:10 p.m. UTC | #1
Hi!
I've adjusted the test description a bit and pushed, thanks!


I've clarified what we are testing that CWD is being unshared with:


diff --git a/testcases/kernel/syscalls/unshare/unshare04.c b/testcases/kernel/syscalls/unshare/unshare04.c
index 5b3e5d98f..4305c5cb1 100644
--- a/testcases/kernel/syscalls/unshare/unshare04.c
+++ b/testcases/kernel/syscalls/unshare/unshare04.c
@@ -4,9 +4,8 @@
  */

 /*\
- * This test case is to verify unshare(CLONE_NEWNS) also unshares filesystem
- * information.
- *
+ * This test case is to verify unshare(CLONE_NEWNS) also unshares process
+ * working directory.
  */
diff mbox series

Patch

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..5b3e5d98f
--- /dev/null
+++ b/testcases/kernel/syscalls/unshare/unshare04.c
@@ -0,0 +1,77 @@ 
+// 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 TESTDIR "test_dir"
+
+static char *cwd;
+static char *buff;
+static size_t size = 1024;
+
+static void setup(void)
+{
+	cwd = SAFE_MALLOC(size);
+	SAFE_GETCWD(cwd, size);
+
+	SAFE_MKDIR(TESTDIR, 0700);
+
+	buff = SAFE_MALLOC(size);
+}
+
+static void cleanup(void)
+{
+	free(buff);
+	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(TESTDIR);
+		SAFE_GETCWD(buff, size);
+
+		if (strcmp(cwd, buff) == 0)
+			tst_res(TFAIL, "current dir not changed");
+		else
+			tst_res(TPASS, "current dir changed to %s", buff);
+	} else {
+		SAFE_WAIT(NULL);
+
+		SAFE_GETCWD(buff, size);
+
+		if (strcmp(cwd, buff) == 0)
+			tst_res(TPASS, "cwd unshared");
+		else
+			tst_res(TFAIL, "cwd not unshared as expected");
+	}
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};