Skip to contentSkip to conversation

Add unshare(CLONE_NEWPID) test

PATCH In review lufei 2 participants 9 messages Jul 11, 2025 – Jul 15, 2025

lore
Share & tools
lore

lufei

author
#0 lore raw

PATCH 1/1 Add unshare(CLONE_NEWPID) test

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare05.c | 54 +++++++++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c
runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
 unshare02 unshare02
 unshare03 unshare03
 unshare04 unshare04
+unshare05 unshare05
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
 /unshare02
 /unshare03
 /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +54 -0

54 lines not on this page · raw message

Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 49 lines · lufei

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/unshare/.gitignore  |  1 +
  testcases/kernel/syscalls/unshare/unshare05.c | 54 +++++++++++++++++++
  3 files changed, 56 insertions(+)

create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c

runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
  unshare02 unshare02
  unshare03 unshare03
  unshare04 unshare04
+unshare05 unshare05
 
  #
  # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
  /unshare02
  /unshare03
  /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +11 -0
new file mode 100644
index 000000000..cfc5877da
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 lufei <[email protected]>
+ */
+
+/*\
+ * This test case verifies unshare(CLONE_NEWPID) creates a new PID namespace
+ * and that the first child process in the new namespace gets PID 1.
+ */
+
+#define _GNU_SOURCE

testcases/kernel/syscalls/unshare/unshare05.c line 21

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +10 -0
+
+#include "tst_test.h"
+#include "lapi/sched.h"
+
+static void run(void)
+{
+	struct tst_clone_args args = {
+		.flags = CLONE_NEWPID,
+		.exit_signal = SIGCHLD,
+	};

This should be created in tst_test.bufs and initialized inside setup().

testcases/kernel/syscalls/unshare/unshare05.c line 24

Quoted 3 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +3 -0
+
+	if (!SAFE_CLONE(&args)) {
+		TST_EXP_PASS(unshare(CLONE_NEWPID));

We can use SAFE_UNSHARE() since we need to break the test if unshare() is not passing.

Quoted 12 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +12 -0
+
+		pid_t child_pid = SAFE_FORK();
+
+		if (child_pid == 0) {
+			pid_t pid = getpid();
+
+			if (pid == 1)
+				tst_res(TPASS, "First child in new PID "
+						"namespace has PID 1");
+			else
+				tst_res(TFAIL, "First child in new PID "
+						"namespace has PID %d, "

testcases/kernel/syscalls/unshare/unshare05.c line 46

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +10 -0
+						"expected 1", pid);
+			exit(0);
+		} else {
+			SAFE_WAIT(NULL);
+		}
+
+		exit(0);
+	}
+
+	SAFE_WAIT(NULL);

All this part can be summed up like this:

if (!SAFE_FORK()) {
    TST_EXP_EQ_LI(getpid(), 1);
    exit(0);
}

And no need for wait() since LTP will take care of collecting child results after reaping it.

testcases/kernel/syscalls/unshare/unshare05.c line 53

Quoted 7 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +7 -0
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.min_kver = "3.8",

We support from 4.15, so we don't need to define the minimum kernel version here.
https://linux-test-project.readthedocs.io/en/latest/users/supported_systems.html

testcases/kernel/syscalls/unshare/unshare05.c line 54

Quoted 1 line
testcases/kernel/syscalls/unshare/unshare05.c new file +1 -0
+};

- Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw

PATCH 1/1 Add unshare(CLONE_NEWPID) test

In reply to lufei

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare05.c | 54 +++++++++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c
runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
 unshare02 unshare02
 unshare03 unshare03
 unshare04 unshare04
+unshare05 unshare05
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
 /unshare02
 /unshare03
 /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +54 -0

54 lines not on this page · raw message

Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw

PATCH v2 1/1 Add unshare(CLONE_NEWPID) test

In reply to lufei

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare05.c | 47 +++++++++++++++++++
 3 files changed, 49 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c
runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
 unshare02 unshare02
 unshare03 unshare03
 unshare04 unshare04
+unshare05 unshare05
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
 /unshare02
 /unshare03
 /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +47 -0

47 lines not on this page · raw message

Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hi!

Quoted 42 lines · lufei

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/unshare/.gitignore  |  1 +
  testcases/kernel/syscalls/unshare/unshare05.c | 47 +++++++++++++++++++
  3 files changed, 49 insertions(+)

create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c

runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
  unshare02 unshare02
  unshare03 unshare03
  unshare04 unshare04
+unshare05 unshare05
 
  #
  # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
  /unshare02
  /unshare03
  /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +4 -0
new file mode 100644
index 000000000..59fc782ab
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 lufei <[email protected]>
+ */

testcases/kernel/syscalls/unshare/unshare05.c line 14

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +10 -0
+
+/*\
+ * This test case verifies unshare(CLONE_NEWPID) creates a new PID namespace
+ * and that the first child process in the new namespace gets PID 1.
+ */
+
+#define _GNU_SOURCE
+
+#include "tst_test.h"
+#include "sched.h"

We have "lapi/sched.h"

Quoted 8 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +8 -0
+
+static struct tst_clone_args *args;
+
+static void setup(void)
+{
+	args->flags = CLONE_NEWPID;
+	args->exit_signal = SIGCHLD;
+}

testcases/kernel/syscalls/unshare/unshare05.c line 32

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +10 -0
+
+static void run(void)
+{
+	if (!SAFE_CLONE(args)) {
+		SAFE_UNSHARE(CLONE_NEWPID);
+
+		if (!SAFE_FORK()) {
+			TST_EXP_EQ_LI(getpid(), 1);
+			exit(0);
+		}

Here we need also exit(0);

testcases/kernel/syscalls/unshare/unshare05.c line 35

Quoted 3 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +3 -0
+	}
+
+	SAFE_WAIT(NULL);

This is not needed, LTP calls "tst_reap_children()" automatically.

testcases/kernel/syscalls/unshare/unshare05.c line 47

Quoted 12 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +12 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.forks_child = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.bufs = (struct tst_buffers []) {
+		{&args, .size = sizeof(*args)},
+		{},
+	}
+};

- Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#5 lore raw

PATCH v3 1/1 Add unshare(CLONE_NEWPID) test

In reply to lufei

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare05.c | 47 +++++++++++++++++++
 3 files changed, 49 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c
runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
 unshare02 unshare02
 unshare03 unshare03
 unshare04 unshare04
+unshare05 unshare05
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
 /unshare02
 /unshare03
 /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +47 -0

47 lines not on this page · raw message

Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi!

Quoted 2 lines · lufei

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

    Add unshare05 test

    Test if unshare(CLONE_NEWPID) assign pid 1 to the first child.

Quoted 73 lines
---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/unshare/.gitignore  |  1 +
  testcases/kernel/syscalls/unshare/unshare05.c | 47 +++++++++++++++++++
  3 files changed, 49 insertions(+)

create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c

runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
  unshare02 unshare02
  unshare03 unshare03
  unshare04 unshare04
+unshare05 unshare05
 
  #
  # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
  /unshare02
  /unshare03
  /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +37 -0
new file mode 100644
index 000000000..c85db36b9
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 lufei <[email protected]>
+ */
+
+/*\
+ * This test case verifies unshare(CLONE_NEWPID) creates a new PID namespace
+ * and that the first child process in the new namespace gets PID 1.
+ */
+
+#define _GNU_SOURCE
+
+#include "tst_test.h"
+#include "lapi/sched.h"
+
+static struct tst_clone_args *args;
+
+static void setup(void)
+{
+	args->flags = CLONE_NEWPID;
+	args->exit_signal = SIGCHLD;
+}
+
+static void run(void)
+{
+	if (!SAFE_CLONE(args)) {
+		SAFE_UNSHARE(CLONE_NEWPID);
+
+		if (!SAFE_FORK()) {
+			TST_EXP_EQ_LI(getpid(), 1);
+			exit(0);
+		}
+
+		exit(0);
+	}
+}
+

testcases/kernel/syscalls/unshare/unshare05.c line 47

Quoted 10 lines
testcases/kernel/syscalls/unshare/unshare05.c new file +10 -0
+static struct tst_test test = {
+	.setup = setup,
+	.forks_child = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.bufs = (struct tst_buffers []) {
+		{&args, .size = sizeof(*args)},
+		{},
+	}
+};

Sorry I made a mistake in the review. tst_reap_children() must be called for nested children. The correct way is the following (I reduced nesting statements as well here):

static void run(void)
{
    if (SAFE_CLONE(args))
        return;

    SAFE_UNSHARE(CLONE_NEWPID);

    if (!SAFE_FORK()) {
        TST_EXP_EQ_LI(getpid(), 1);
        exit(0);
    }

    tst_reap_children();
}

- Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#7 lore raw

PATCH v4 1/1 Add unshare(CLONE_NEWPID) test

In reply to lufei

Test unshare(CLONE_NEWPID) to make first child in new PID namespce get pid 1.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/unshare/.gitignore  |  1 +
 testcases/kernel/syscalls/unshare/unshare05.c | 48 +++++++++++++++++++
 3 files changed, 50 insertions(+)
 create mode 100644 testcases/kernel/syscalls/unshare/unshare05.c
runtest/syscalls +1 -0
index 57338297a..82e222bf4 100644
@@ -1726,6 +1726,7 @@ unshare01 unshare01
 unshare02 unshare02
 unshare03 unshare03
 unshare04 unshare04
+unshare05 unshare05
 
 #
 # These tests require an unmounted block device
testcases/kernel/syscalls/unshare/.gitignore +1 -0
index b1206e452..8ece5f988 100644
@@ -2,3 +2,4 @@
 /unshare02
 /unshare03
 /unshare04
+/unshare05
testcases/kernel/syscalls/unshare/unshare05.c new file +48 -0

48 lines not on this page · raw message

Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp