Refactor pidns06 test using new LTP API
PATCH 1/1 Refactor pidns06 test using new LTP API
--- testcases/kernel/containers/pidns/pidns06.c | 152 +++++--------------- 1 file changed, 38 insertions(+), 114 deletions(-)
testcases/kernel/containers/pidns/pidns06.c +38 -114
@@ -1,133 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 /* -* Copyright (c) International Business Machines Corp., 2008 -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* This program is distributed in the hope that it will be useful -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -************************************************************************* -* Description: -* Testcase tries killing of the parent namespace pid by the container-init. -* It also tries killing of non-existent PID, by the container-init. -* Returns Success if Unable to kill, and proper error number is set. -* else Returns Failure -* -* Steps: -* 1. Parent process clone a process with flag CLONE_NEWPID -* 2. The pid of the parent namespace is passed to the container. -* 3. Container receieves the PID and passes SIGKILL to this PID. -* 4. If kill() is unsuccessful and the errno is set to 'No Such process' -* then sets PASS -* else, -* sets FAIL -* 5. It also verifies by passing SIGKILL to FAKE_PID -* 6. If kill() is unsuccessful and the errno is set to 'No Such process' -* then sets PASS -* else, -* sets FAIL -* -*******************************************************************************/ -#define _GNU_SOURCE 1 -#include <stdio.h> -#include <stdlib.h> -#include <sys/wait.h> -#include <assert.h> -#include <unistd.h> -#include <errno.h> -#include <signal.h> -#include "pidns_helper.h" -#include "test.h" +* Copyright (C) International Business Machines Corp., 2008 +* Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]> +*/ -#define CINIT_PID 1 -#define PARENT_PID 0 -#define FAKE_PID -1 +/*\ +* [Description] +* +* Clone a process with CLONE_NEWPID flag and check that parent process can't +* be killed from child namespace. +*/ -char *TCID ="pidns06" ; -int TST_TOTAL =1 ; +#include "tst_test.h" +#include "lapi/namespaces_constants.h" -/* -* kill_pid_in_childfun() -* Cont-init tries to kill the parent-process using parent's global Pid. -* Also checks passing SIGKILL to non existent PID in the container. -*/ -static int kill_pid_in_childfun (void *vtest) +static int child_func(LTP_ATTRIBUTE_UNUSED void *arg) { -int cpid, ppid, *par_pid; -int ret =0 ; +pid_t cpid, ppid; +int ret; + cpid =getpid (); ppid =getppid (); - par_pid = (int *)vtest; -/* Checking the values to make sure pidns is created correctly */ -if (cpid !=CINIT_PID || ppid !=PARENT_PID ) { -printf ("Unexpected result for Container: init " -"pid=%d ppid=%d\n" , cpid, ppid); -exit (1 ); +if (cpid !=1 || ppid !=0 ) { +tst_res (TFAIL ,"Got unexpected result of cpid=%d ppid=%d" , cpid, ppid); +return 1 ; } -/* -* While trying kill() of the pid of the parent namespace.. -* Check to see if the errno was set to the expected, value of 3 : ESRCH -*/ - ret =kill (*par_pid,SIGKILL ); -if (ret ==-1 && errno ==ESRCH ) { -printf ("Container: killing parent pid=%d failed as expected " -"with ESRCH\n" , *par_pid); - }else { -printf ("Container: killing parent pid=%d, didn't fail as " -"expected with ESRCH (%d) and a return value of -1. Got " -"%d (\"%s\") and a return value of %d instead.\n" , - *par_pid,ESRCH , errno,strerror (errno), ret); -exit (1 ); - } -/* -* While killing non-existent pid in the container, -* Check to see if the errno was set to the expected, value of 3 : ESRCH -*/ - ret =kill (FAKE_PID ,SIGKILL ); -if (ret ==-1 && errno ==ESRCH ) { -printf ("Container: killing non-existent pid failed as expected " -"with ESRCH\n" ); - }else { -printf ("Container: killing non-existent pid, didn't fail as " -"expected with ESRCH (%d) and a return value of -1. Got " -"%d (\"%s\") and a return value of %d instead.\n" , -ESRCH , errno,strerror (errno), ret); -exit (1 ); + ret =kill (*(int *)arg,SIGKILL ); + +if (ret !=-1 || errno !=ESRCH ) { +tst_res (TFAIL ,"kill() didn't fail with ESRCH" ); +return 1 ; } -exit (0 ); -} +tst_res (TPASS ,"Can't kill parent process from child namespace" ); -static void setup (void ) -{ -tst_require_root (); -check_newpid (); +return 0 ; } -int main (void ) +static void run (void ) { -int status; - -setup (); +int ret; +pid_t pid; -pid_t pid =getpid (); + pid =getpid (); -tst_resm (TINFO ,"Parent: Passing the pid of the process %d" , pid); -TEST (do_clone_unshare_test (T_CLONE ,CLONE_NEWPID , kill_pid_in_childfun, - (void *)&pid)); -if (TEST_RETURN ==-1 ) { -tst_brkm (TFAIL |TTERRNO ,NULL ,"clone failed" ); - }else if (wait (&status) ==-1 ) { -tst_brkm (TFAIL |TERRNO ,NULL ,"wait failed" ); - } - -tst_exit (); + ret =ltp_clone_quick (CLONE_NEWPID |SIGCHLD , child_func, &pid); +if (ret <0 ) +tst_brk (TBROK |TERRNO ,"clone failed" ); } + +static struct tst_test test = { + .test_all = run, + .needs_root =1 , +};
Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 163 lines · Andrea Cervesato via ltp
Signed-off-by Andrea Cervesato <[email protected]>--- testcases/kernel/containers/pidns/pidns06.c | 152 +++++--------------- 1 file changed, 38 insertions(+), 114 deletions(-)testcases/kernel/containers/pidns/pidns06.c +30
-105@@ -1,133 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 /* -* Copyright (c) International Business Machines Corp., 2008 -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* This program is distributed in the hope that it will be useful -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -************************************************************************* -* Description: -* Testcase tries killing of the parent namespace pid by the container-init. -* It also tries killing of non-existent PID, by the container-init. -* Returns Success if Unable to kill, and proper error number is set. -* else Returns Failure -* -* Steps: -* 1. Parent process clone a process with flag CLONE_NEWPID -* 2. The pid of the parent namespace is passed to the container. -* 3. Container receieves the PID and passes SIGKILL to this PID. -* 4. If kill() is unsuccessful and the errno is set to 'No Such process' -* then sets PASS -* else, -* sets FAIL -* 5. It also verifies by passing SIGKILL to FAKE_PID -* 6. If kill() is unsuccessful and the errno is set to 'No Such process' -* then sets PASS -* else, -* sets FAIL -* -*******************************************************************************/ -#define _GNU_SOURCE 1 -#include <stdio.h> -#include <stdlib.h> -#include <sys/wait.h> -#include <assert.h> -#include <unistd.h> -#include <errno.h> -#include <signal.h> -#include "pidns_helper.h" -#include "test.h" +* Copyright (C) International Business Machines Corp., 2008 +* Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]> +*/ -#define CINIT_PID 1 -#define PARENT_PID 0 -#define FAKE_PID -1 +/*\ +* [Description] +* +* Clone a process with CLONE_NEWPID flag and check that parent process can't +* be killed from child namespace. +*/ -char *TCID ="pidns06" ; -int TST_TOTAL =1 ; +#include "tst_test.h" +#include "lapi/namespaces_constants.h" -/* -* kill_pid_in_childfun() -* Cont-init tries to kill the parent-process using parent's global Pid. -* Also checks passing SIGKILL to non existent PID in the container. -*/ -static int kill_pid_in_childfun (void *vtest) +static int child_func(LTP_ATTRIBUTE_UNUSED void *arg) { -int cpid, ppid, *par_pid; -int ret =0 ; +pid_t cpid, ppid; +int ret; + cpid =getpid (); ppid =getppid (); - par_pid = (int *)vtest; -/* Checking the values to make sure pidns is created correctly */ -if (cpid !=CINIT_PID || ppid !=PARENT_PID ) { -printf ("Unexpected result for Container: init " -"pid=%d ppid=%d\n" , cpid, ppid); -exit (1 ); +if (cpid !=1 || ppid !=0 ) { +tst_res (TFAIL ,"Got unexpected result of cpid=%d ppid=%d" , cpid, ppid); +return 1 ; } -/* -* While trying kill() of the pid of the parent namespace.. -* Check to see if the errno was set to the expected, value of 3 : ESRCH -*/ - ret =kill (*par_pid,SIGKILL ); -if (ret ==-1 && errno ==ESRCH ) { -printf ("Container: killing parent pid=%d failed as expected " -"with ESRCH\n" , *par_pid); - }else { -printf ("Container: killing parent pid=%d, didn't fail as " -"expected with ESRCH (%d) and a return value of -1. Got " -"%d (\"%s\") and a return value of %d instead.\n" , - *par_pid,ESRCH , errno,strerror (errno), ret); -exit (1 ); - } -/* -* While killing non-existent pid in the container, -* Check to see if the errno was set to the expected, value of 3 : ESRCH -*/ - ret =kill (FAKE_PID ,SIGKILL ); -if (ret ==-1 && errno ==ESRCH ) { -printf ("Container: killing non-existent pid failed as expected " -"with ESRCH\n" ); - }else { -printf ("Container: killing non-existent pid, didn't fail as " -"expected with ESRCH (%d) and a return value of -1. Got " -"%d (\"%s\") and a return value of %d instead.\n" , -ESRCH , errno,strerror (errno), ret); -exit (1 ); + ret =kill (*(int *)arg,SIGKILL ); + +if (ret !=-1 || errno !=ESRCH ) { +tst_res (TFAIL ,"kill() didn't fail with ESRCH" ); +return 1 ; } -exit (0 ); -} +tst_res (TPASS ,"Can't kill parent process from child namespace" ); -static void setup (void ) -{ -tst_require_root (); -check_newpid (); +return 0 ; } -int main (void ) +static void run (void ) { -int status; - -setup (); +int ret; +pid_t pid; -pid_t pid =getpid (); + pid =getpid (); -tst_resm (TINFO ,"Parent: Passing the pid of the process %d" , pid);
testcases/kernel/containers/pidns/pidns06.c line 49
Quoted 10 lines
testcases/kernel/containers/pidns/pidns06.c +1
-9-TEST (do_clone_unshare_test (T_CLONE ,CLONE_NEWPID , kill_pid_in_childfun, - (void *)&pid)); -if (TEST_RETURN ==-1 ) { -tst_brkm (TFAIL |TTERRNO ,NULL ,"clone failed" ); - }else if (wait (&status) ==-1 ) { -tst_brkm (TFAIL |TERRNO ,NULL ,"wait failed" ); - } - -tst_exit (); + ret =ltp_clone_quick (CLONE_NEWPID |SIGCHLD , child_func, &pid);
tst_clone
Quoted 10 lines
testcases/kernel/containers/pidns/pidns06.c +7
-0+if (ret <0 ) +tst_brk (TBROK |TERRNO ,"clone failed" ); } + +static struct tst_test test = { + .test_all = run, + .needs_root =1 , +};Signature · 1 line
2.35.3
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hello,
I said I would merge these, but on closer inspection there are more opportunities to use existing macros which will make the test more concise and increase debugging info.
Quoted 73 lines · Andrea Cervesato via ltp
Signed-off-by Andrea Cervesato <[email protected]>--- testcases/kernel/containers/pidns/pidns06.c | 152 +++++--------------- 1 file changed, 38 insertions(+), 114 deletions(-)testcases/kernel/containers/pidns/pidns06.c +10
-50@@ -1,133 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 /* -* Copyright (c) International Business Machines Corp., 2008 -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* This program is distributed in the hope that it will be useful -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -************************************************************************* -* Description: -* Testcase tries killing of the parent namespace pid by the container-init. -* It also tries killing of non-existent PID, by the container-init. -* Returns Success if Unable to kill, and proper error number is set. -* else Returns Failure -* -* Steps: -* 1. Parent process clone a process with flag CLONE_NEWPID -* 2. The pid of the parent namespace is passed to the container. -* 3. Container receieves the PID and passes SIGKILL to this PID. -* 4. If kill() is unsuccessful and the errno is set to 'No Such process' -* then sets PASS -* else, -* sets FAIL -* 5. It also verifies by passing SIGKILL to FAKE_PID -* 6. If kill() is unsuccessful and the errno is set to 'No Such process' -* then sets PASS -* else, -* sets FAIL -* -*******************************************************************************/ -#define _GNU_SOURCE 1 -#include <stdio.h> -#include <stdlib.h> -#include <sys/wait.h> -#include <assert.h> -#include <unistd.h> -#include <errno.h> -#include <signal.h> -#include "pidns_helper.h" -#include "test.h" +* Copyright (C) International Business Machines Corp., 2008 +* Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]> +*/ -#define CINIT_PID 1 -#define PARENT_PID 0 -#define FAKE_PID -1 +/*\ +* [Description] +* +* Clone a process with CLONE_NEWPID flag and check that parent process can't +* be killed from child namespace. +*/ -char *TCID ="pidns06" ; -int TST_TOTAL =1 ;
testcases/kernel/containers/pidns/pidns06.c line 17
Quoted 10 lines
testcases/kernel/containers/pidns/pidns06.c +3
-6+#include "tst_test.h" +#include "lapi/namespaces_constants.h" -/* -* kill_pid_in_childfun() -* Cont-init tries to kill the parent-process using parent's global Pid. -* Also checks passing SIGKILL to non existent PID in the container. -*/ -static int kill_pid_in_childfun (void *vtest) +static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
arg is not unused.
Quoted 9 lines
testcases/kernel/containers/pidns/pidns06.c +3
-3{ -int cpid, ppid, *par_pid; -int ret =0 ; +pid_t cpid, ppid; +int ret; + cpid =getpid (); ppid =getppid (); - par_pid = (int *)vtest;
testcases/kernel/containers/pidns/pidns06.c line 28
Quoted 10 lines
testcases/kernel/containers/pidns/pidns06.c +3
-5-/* Checking the values to make sure pidns is created correctly */ -if (cpid !=CINIT_PID || ppid !=PARENT_PID ) { -printf ("Unexpected result for Container: init " -"pid=%d ppid=%d\n" , cpid, ppid); -exit (1 ); +if (cpid !=1 || ppid !=0 ) { +tst_res (TFAIL ,"Got unexpected result of cpid=%d ppid=%d" , cpid, ppid); +return 1 ; }
The TST_EXP* macros can be used to simplify this.
Quoted 21 lines
testcases/kernel/containers/pidns/pidns06.c +0
-20-/* -* While trying kill() of the pid of the parent namespace.. -* Check to see if the errno was set to the expected, value of 3 : ESRCH -*/ - ret =kill (*par_pid,SIGKILL ); -if (ret ==-1 && errno ==ESRCH ) { -printf ("Container: killing parent pid=%d failed as expected " -"with ESRCH\n" , *par_pid); - }else { -printf ("Container: killing parent pid=%d, didn't fail as " -"expected with ESRCH (%d) and a return value of -1. Got " -"%d (\"%s\") and a return value of %d instead.\n" , - *par_pid,ESRCH , errno,strerror (errno), ret); -exit (1 ); - } -/* -* While killing non-existent pid in the container, -* Check to see if the errno was set to the expected, value of 3 : ESRCH -*/ - ret =kill (FAKE_PID ,SIGKILL );
testcases/kernel/containers/pidns/pidns06.c line 30
Quoted 10 lines
testcases/kernel/containers/pidns/pidns06.c +1
-9-if (ret ==-1 && errno ==ESRCH ) { -printf ("Container: killing non-existent pid failed as expected " -"with ESRCH\n" ); - }else { -printf ("Container: killing non-existent pid, didn't fail as " -"expected with ESRCH (%d) and a return value of -1. Got " -"%d (\"%s\") and a return value of %d instead.\n" , -ESRCH , errno,strerror (errno), ret); -exit (1 ); + ret =kill (*(int *)arg,SIGKILL );
We have the TEST macro as well. Or TST_EXP_FAIL.
testcases/kernel/containers/pidns/pidns06.c line 31
Quoted 2 lines
testcases/kernel/containers/pidns/pidns06.c +2
-0+ +if (ret !=-1 || errno !=ESRCH ) {
errno could be set to some stale value if ret != -1 (the TEST macro sets it)
testcases/kernel/containers/pidns/pidns06.c line 33
Quoted 1 line
testcases/kernel/containers/pidns/pidns06.c +1
-0+tst_res (TFAIL ,"kill() didn't fail with ESRCH" );
TTERRNO can be used here with the TEST macro. Otherwise we don't know what errno was set.
Quoted 14 lines
testcases/kernel/containers/pidns/pidns06.c +3
-6+return 1 ; } -exit (0 ); -} +tst_res (TPASS ,"Can't kill parent process from child namespace" ); -static void setup (void ) -{ -tst_require_root (); -check_newpid (); +return 0 ; }
testcases/kernel/containers/pidns/pidns06.c line 121
Quoted 10 lines
testcases/kernel/containers/pidns/pidns06.c +3
-5-int main (void ) +static void run (void ) { -int status; - -setup (); +int ret; +pid_t pid; -pid_t pid =getpid ();
I don't think you need to change this.
testcases/kernel/containers/pidns/pidns06.c line 49
Quoted 13 lines
testcases/kernel/containers/pidns/pidns06.c +2
-10+ pid =getpid (); -tst_resm (TINFO ,"Parent: Passing the pid of the process %d" , pid); -TEST (do_clone_unshare_test (T_CLONE ,CLONE_NEWPID , kill_pid_in_childfun, - (void *)&pid)); -if (TEST_RETURN ==-1 ) { -tst_brkm (TFAIL |TTERRNO ,NULL ,"clone failed" ); - }else if (wait (&status) ==-1 ) { -tst_brkm (TFAIL |TERRNO ,NULL ,"wait failed" ); - } - -tst_exit (); + ret =ltp_clone_quick (CLONE_NEWPID |SIGCHLD , child_func, &pid);
I want this to be replaced with tst_clone, but as agreed, it can be defered to a later patch set.
Quoted 10 lines
testcases/kernel/containers/pidns/pidns06.c +7
-0+if (ret <0 ) +tst_brk (TBROK |TERRNO ,"clone failed" ); } + +static struct tst_test test = { + .test_all = run, + .needs_root =1 , +};Signature · 1 line
2.35.3
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp