Refactor pidns32 test using new LTP API
PATCH v2 1/1 Refactor pidns32 test using new LTP API
Signed-off-by Andrea Cervesato <[email protected]>
---
Using SAFE_CLONE
Using TST_EXP_* macros
testcases/kernel/containers/pidns/pidns32.c | 41 +++++++++++---------- 1 file changed, 21 insertions(+), 20 deletions(-)
testcases/kernel/containers/pidns/pidns32.c +21 -20
@@ -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 <[email protected]> +* Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]> */ /*\
@@ -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 , };
Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp
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.
Quoted 105 lines · Andrea Cervesato via ltp
Signed-off-by Andrea Cervesato <[email protected]>---
UsingSAFE_CLONE
Using TST_EXP_* macrostestcases/kernel/containers/pidns/pidns32.c | 41 +++++++++++---------- 1 file changed, 21 insertions(+), 20 deletions(-)testcases/kernel/containers/pidns/pidns32.c +21
-20@@ -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 <[email protected]> +* Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]> */ /*\ @@ -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 , };Signature · 1 line
2.35.3
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp