[LTP] [PATCH v2] Refactor pidns32 test using new LTP API

Richard Palethorpe rpalethorpe@suse.de
Tue Feb 14 12:50:17 CET 2023


Hello,

Merged with a few changes!

Again we are using waitpid on its own. It will wait the children and
we'll only check the exit status of one of them. So I replaced it with
tst_reap_children.

Also I used tst_atomic to read and write from level. Not sure if that is
necessary, but it looks cleaner.

Andrea Cervesato via ltp <ltp@lists.linux.it> writes:

> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
> Using SAFE_CLONE
> Using TST_EXP_* macros
>
>  testcases/kernel/containers/pidns/pidns32.c | 41 +++++++++++----------
>  1 file changed, 21 insertions(+), 20 deletions(-)
>
> diff --git a/testcases/kernel/containers/pidns/pidns32.c b/testcases/kernel/containers/pidns/pidns32.c
> index 3f7df788e..c9f8db3e5 100644
> --- a/testcases/kernel/containers/pidns/pidns32.c
> +++ b/testcases/kernel/containers/pidns/pidns32.c
> @@ -1,7 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0-or-later
>  /*
>   * Copyright (c) Huawei Technologies Co., Ltd., 2015
> - * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
>   */
>  
>  /*\
> @@ -12,32 +12,32 @@
>   */
>  
>  #define _GNU_SOURCE
> -
>  #include <sys/mman.h>
>  #include "tst_test.h"
>  #include "lapi/sched.h"
>  
>  #define MAXNEST 32
>  
> +static const struct tst_clone_args args = { CLONE_NEWPID, SIGCHLD };
>  static int *level;
>  
> -static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
> +static void child_func(void)
>  {
>  	pid_t cpid;
>  	int status;
>  
>  	if (*level == MAXNEST)
> -		return 0;
> +		return;
>  
>  	(*level)++;
>  
> -	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
> -	if (cpid < 0)
> -		tst_brk(TBROK | TERRNO, "clone failed");
> +	cpid = SAFE_CLONE(&args);
> +	if (!cpid) {
> +		child_func();
> +		return;
> +	}
>  
>  	SAFE_WAITPID(cpid, &status, 0);
> -
> -	return 0;
>  }
>  
>  static void setup(void)
> @@ -45,28 +45,29 @@ static void setup(void)
>  	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
>  }
>  
> -static void run(void)
> +static void cleanup(void)
>  {
> -	int ret, status;
> +	SAFE_MUNMAP(level, sizeof(int));
> +}
>  
> +static void run(void)
> +{
>  	*level = 1;
>  
> -	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
> -	if (ret < 0)
> -		tst_brk(TBROK | TERRNO, "clone failed");
> -
> -	SAFE_WAITPID(ret, &status, 0);
> -
> -	if (*level < MAXNEST) {
> -		tst_res(TFAIL, "Nested containers should be %d, but they are %d", MAXNEST, *level);
> +	if (!SAFE_CLONE(&args)) {
> +		child_func();
>  		return;
>  	}
>  
> -	tst_res(TPASS, "All %d containers have been nested", MAXNEST);
> +	tst_reap_children();
> +
> +	TST_EXP_EQ_LI(*level, MAXNEST);
>  }
>  
>  static struct tst_test test = {
>  	.test_all = run,
>  	.needs_root = 1,
>  	.setup = setup,
> +	.cleanup = cleanup,
> +	.forks_child = 1,
>  };
> -- 
> 2.35.3


-- 
Thank you,
Richard.


More information about the ltp mailing list