[LTP] [PATCH v3] Add unshare(CLONE_NEWPID) test

Andrea Cervesato andrea.cervesato@suse.com
Tue Jul 15 09:25:47 CEST 2025


Hi!

On 7/15/25 4:14 AM, lufei wrote:
> 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.

>
> Signed-off-by: lufei <lufei@uniontech.com>
> ---
>   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
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 57338297a..82e222bf4 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1726,6 +1726,7 @@ unshare01 unshare01
>   unshare02 unshare02
>   unshare03 unshare03
>   unshare04 unshare04
> +unshare05 unshare05
>   
>   #
>   # These tests require an unmounted block device
> diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore
> index b1206e452..8ece5f988 100644
> --- a/testcases/kernel/syscalls/unshare/.gitignore
> +++ b/testcases/kernel/syscalls/unshare/.gitignore
> @@ -2,3 +2,4 @@
>   /unshare02
>   /unshare03
>   /unshare04
> +/unshare05
> diff --git a/testcases/kernel/syscalls/unshare/unshare05.c b/testcases/kernel/syscalls/unshare/unshare05.c
> new file mode 100644
> index 000000000..c85db36b9
> --- /dev/null
> +++ b/testcases/kernel/syscalls/unshare/unshare05.c
> @@ -0,0 +1,47 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2025 lufei <lufei@uniontech.com>
> + */
> +
> +/*\
> + * 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);
> +	}
> +}
> +
> +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



More information about the ltp mailing list