* [LTP] [PATCH] syscalls/unshare: New test: CLONE_NEWNS unshares fs info
@ 2025-04-23 4:46 lufei
2025-04-23 9:10 ` Cyril Hrubis
2025-04-23 12:05 ` [LTP] [PATCH v2] " lufei
0 siblings, 2 replies; 6+ messages in thread
From: lufei @ 2025-04-23 4:46 UTC (permalink / raw)
To: ltp; +Cc: lufei
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 | 69 +++++++++++++++++++
3 files changed, 71 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..0bbb9d19d
--- /dev/null
+++ b/testcases/kernel/syscalls/unshare/unshare04.c
@@ -0,0 +1,69 @@
+// 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"
+
+#ifdef HAVE_UNSHARE
+
+#define TMP "/tmp"
+
+static void run(void)
+{
+ char *c_cwd;
+ char *p_cwd;
+ size_t size = 1024;
+
+ c_cwd = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS,
+ -1, 0);
+
+ p_cwd = SAFE_MALLOC(size);
+
+ struct tst_clone_args args = {
+ .flags = CLONE_FS,
+ .exit_signal = SIGCHLD,
+ };
+
+ if (!SAFE_CLONE(&args)) {
+
+ TST_EXP_PASS(unshare(CLONE_NEWNS));
+
+ SAFE_CHDIR(TMP);
+ SAFE_GETCWD(c_cwd, size);
+ } else {
+ SAFE_WAIT(NULL);
+
+ SAFE_GETCWD(p_cwd, size);
+
+ tst_res(TDEBUG, "parent cwd: %s", p_cwd);
+ tst_res(TDEBUG, "child cwd: %s", c_cwd);
+
+ //in parent, cwd stays in tst_tmpdir_path than TMP.
+ if (strcmp(p_cwd, c_cwd) == 0)
+ tst_res(TFAIL, "cwd not unshared as expected");
+ else
+ tst_res(TPASS, "cwd unshared");
+ }
+}
+
+static struct tst_test test = {
+ .forks_child = 1,
+ .needs_root = 1,
+ .needs_tmpdir = 1,
+ .test_all = run,
+};
+
+#else
+TST_TEST_TCONF("unshare syscall is undefined.");
+#endif
--
2.39.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH] syscalls/unshare: New test: CLONE_NEWNS unshares fs info
2025-04-23 4:46 [LTP] [PATCH] syscalls/unshare: New test: CLONE_NEWNS unshares fs info lufei
@ 2025-04-23 9:10 ` Cyril Hrubis
2025-04-23 12:05 ` [LTP] [PATCH v2] " lufei
1 sibling, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2025-04-23 9:10 UTC (permalink / raw)
To: lufei; +Cc: ltp
Hi!
> 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..0bbb9d19d
> --- /dev/null
> +++ b/testcases/kernel/syscalls/unshare/unshare04.c
> @@ -0,0 +1,69 @@
> +// 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"
> +
> +#ifdef HAVE_UNSHARE
This shouldn't be needed anymore, the minimal glibc we support at the
moment is 2.22, unshare() if I'm looking right is included in glibc 2.4
and newer. I suppose that removing the configure check for unshare() is
long overdue.
> +#define TMP "/tmp"
The test shouldn't expect or touch directories outside the test
temporary directory. If you need a diretory to chdir() into create one
in the test setup() instead.
> +static void run(void)
> +{
> + char *c_cwd;
> + char *p_cwd;
> + size_t size = 1024;
> +
> + c_cwd = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE,
> + MAP_SHARED | MAP_ANONYMOUS,
> + -1, 0);
> +
> + p_cwd = SAFE_MALLOC(size);
These allocations should be made in test setup and freed in the test
cleanup.
> + struct tst_clone_args args = {
> + .flags = CLONE_FS,
> + .exit_signal = SIGCHLD,
> + };
> +
> + if (!SAFE_CLONE(&args)) {
> +
> + TST_EXP_PASS(unshare(CLONE_NEWNS));
> +
> + SAFE_CHDIR(TMP);
> + SAFE_GETCWD(c_cwd, size);
> + } else {
> + SAFE_WAIT(NULL);
> +
> + SAFE_GETCWD(p_cwd, size);
> +
> + tst_res(TDEBUG, "parent cwd: %s", p_cwd);
> + tst_res(TDEBUG, "child cwd: %s", c_cwd);
> +
> + //in parent, cwd stays in tst_tmpdir_path than TMP.
> + if (strcmp(p_cwd, c_cwd) == 0)
> + tst_res(TFAIL, "cwd not unshared as expected");
> + else
> + tst_res(TPASS, "cwd unshared");
We are validating that the CWD wasn't changed in parent after we have
done unshare() and chdir() in the child. Why do we even compare the
parent CWD againts the child? Rather than that we have to compare the
CWD in the parent againts the CWD at the start of the test.
What we should do is:
- do chdir() in child
- check in child that CWD was changed
- wait child in parent
- check in parent that CWD wasn't changed
For that all we have to do is to do getcwd() in the test setup and then
use it for a comparsion both in the child and parent.
> + }
> +}
> +
> +static struct tst_test test = {
> + .forks_child = 1,
> + .needs_root = 1,
> + .needs_tmpdir = 1,
> + .test_all = run,
> +};
> +
> +#else
> +TST_TEST_TCONF("unshare syscall is undefined.");
> +#endif
> --
> 2.39.3
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
* [LTP] [PATCH v2] syscalls/unshare: New test: CLONE_NEWNS unshares fs info
2025-04-23 4:46 [LTP] [PATCH] syscalls/unshare: New test: CLONE_NEWNS unshares fs info lufei
2025-04-23 9:10 ` Cyril Hrubis
@ 2025-04-23 12:05 ` lufei
2025-04-25 9:07 ` Cyril Hrubis
2025-04-25 9:33 ` [LTP] [PATCH v3] " lufei
1 sibling, 2 replies; 6+ messages in thread
From: lufei @ 2025-04-23 12:05 UTC (permalink / raw)
To: ltp; +Cc: lufei
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH v2] syscalls/unshare: New test: CLONE_NEWNS unshares fs info
2025-04-23 12:05 ` [LTP] [PATCH v2] " lufei
@ 2025-04-25 9:07 ` Cyril Hrubis
2025-04-25 9:33 ` [LTP] [PATCH v3] " lufei
1 sibling, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2025-04-25 9:07 UTC (permalink / raw)
To: lufei; +Cc: ltp
Hi!
> +// 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);
There no need for absolute paths, the test starts with CWD pointing to
the newly created temporary directory. So all that we need to do is to
use relative paths with:
#define TESTDIR "test_dir"
SAFE_MKDIR(TESTDIR, 0700);
And then later do SAFE_CHDIR(TESTDIR) in the child.
> + c_cwd = SAFE_MALLOC(size);
> + p_cwd = SAFE_MALLOC(size);
We do not need two of these, we can use the same buffer both in child
and parent.
> +}
> +
> +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");
^
unshared
> + }
> +}
> +
> +static struct tst_test test = {
> + .forks_child = 1,
> + .needs_root = 1,
> + .needs_tmpdir = 1,
> + .test_all = run,
> + .setup = setup,
> + .cleanup = cleanup,
> +};
> --
> 2.39.3
>
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
* [LTP] [PATCH v3] syscalls/unshare: New test: CLONE_NEWNS unshares fs info
2025-04-23 12:05 ` [LTP] [PATCH v2] " lufei
2025-04-25 9:07 ` Cyril Hrubis
@ 2025-04-25 9:33 ` lufei
2025-04-25 12:10 ` Cyril Hrubis
1 sibling, 1 reply; 6+ messages in thread
From: lufei @ 2025-04-25 9:33 UTC (permalink / raw)
To: ltp; +Cc: lufei
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
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,
+};
--
2.39.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH v3] syscalls/unshare: New test: CLONE_NEWNS unshares fs info
2025-04-25 9:33 ` [LTP] [PATCH v3] " lufei
@ 2025-04-25 12:10 ` Cyril Hrubis
0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2025-04-25 12:10 UTC (permalink / raw)
To: lufei; +Cc: ltp
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.
*/
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-04-25 12:10 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-23 4:46 [LTP] [PATCH] syscalls/unshare: New test: CLONE_NEWNS unshares fs info lufei
2025-04-23 9:10 ` Cyril Hrubis
2025-04-23 12:05 ` [LTP] [PATCH v2] " lufei
2025-04-25 9:07 ` Cyril Hrubis
2025-04-25 9:33 ` [LTP] [PATCH v3] " lufei
2025-04-25 12:10 ` Cyril Hrubis
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.