diff mbox series

[v3] Refactor pidns16 test using new LTP API

Message ID 20230207122455.28177-1-andrea.cervesato@suse.com
State Superseded
Headers show
Series [v3] Refactor pidns16 test using new LTP API | expand

Commit Message

Andrea Cervesato Feb. 7, 2023, 12:24 p.m. UTC
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
Using tst_clone API
More TST_* macro usage
Better TINFO messages

 testcases/kernel/containers/pidns/pidns16.c | 193 +++++++-------------
 1 file changed, 62 insertions(+), 131 deletions(-)

Comments

Andrea Cervesato Feb. 7, 2023, 12:29 p.m. UTC | #1
Hi,

counter flag is unused. Please consider v4 instead and ignore this one 
thanks

On 2/7/23 13:24, Andrea Cervesato wrote:
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
> Using tst_clone API
> More TST_* macro usage
> Better TINFO messages
>
>   testcases/kernel/containers/pidns/pidns16.c | 193 +++++++-------------
>   1 file changed, 62 insertions(+), 131 deletions(-)
>
> diff --git a/testcases/kernel/containers/pidns/pidns16.c b/testcases/kernel/containers/pidns/pidns16.c
> index 2ee61065a..121da8e63 100644
> --- a/testcases/kernel/containers/pidns/pidns16.c
> +++ b/testcases/kernel/containers/pidns/pidns16.c
> @@ -1,157 +1,88 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>   /*
> -* Copyright (c) International Business Machines Corp., 2007
> -* 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
> -*
> -***************************************************************************
> -
> -* * Test Assertion.
> -* *----------------
> -* * kill -USR1 container_init
> -* *	- from the parent process and also inside a container
> -* *	- Where init has defined a custom handler for USR1
> -* *	- Should call the handler and
> -* *	- Verify whether the signal handler is called from the proper process.
> -* *
> -* * Description:
> -* *  Create PID namespace container.
> -* *  Container init defines the handler for SIGUSR1 and waits indefinetly.
> -* *  Parent sends SIGUSR1 to container init.
> -* *  The signal handler is handled and the cont-init resumes normally.
> -* *  From the container, again the signal SIGUSR1 is sent.
> -* *  In the sig-handler check if it's invoked from correct pid(parent/container)
> -* *  If cont-init wakes up properly -
> -* *  it will return expected value at exit which is verified at the end.
> -* *
> -* * History:
> -* *  DATE	  NAME				   DESCRIPTION
> -* *  04/11/08  Veerendra C  <vechandr@in.ibm.com> Verifying cont init kill -USR1
> -*
> -*******************************************************************************/
> -#include "config.h"
> + * Copyright (c) International Business Machines Corp., 2007
> + *               04/11/08  Veerendra C  <vechandr@in.ibm.com>
> + * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Clone a process with CLONE_NEWPID flag and verifies that siginfo->si_pid is
> + * set to 0 if sender (parent process) sent the signal. Then send signal from
> + * container itself and check if siginfo->si_pid is set to 1.
> + */
>   
>   #define _GNU_SOURCE 1
> -#include <stdio.h>
> -#include <stdlib.h>
> -#include <sys/wait.h>
> -#include <sys/types.h>
>   #include <signal.h>
> -#include <unistd.h>
> -#include "pidns_helper.h"
> -#include "test.h"
> -
> -#define CHILD_PID	1
> -#define PARENT_PID	0
> +#include "tst_test.h"
> +#include "lapi/sched.h"
>   
> -char *TCID = "pidns16";
> -int TST_TOTAL = 3;
> +static int counter;
> +static int signal_pid;
>   
> -void child_signal_handler(int sig, siginfo_t * si, void *unused)
> +static void child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig, siginfo_t *si, LTP_ATTRIBUTE_UNUSED void *unused)
>   {
> -	static int c = 1;
> -	pid_t expected_pid;
> -
> -	/* Verifying from which process the signal handler is signalled */
> -
> -	switch (c) {
> -	case 1:
> -		expected_pid = PARENT_PID;
> -		break;
> -	case 2:
> -		expected_pid = CHILD_PID;
> -		break;
> -	default:
> -		tst_resm(TBROK, "child should NOT be signalled 3+ times");
> -		return;
> -	}
> -
> -	if (si->si_pid == expected_pid)
> -		tst_resm(TPASS, "child is signalled from expected pid %d",
> -			 expected_pid);
> -	else
> -		tst_resm(TFAIL, "child is signalled from unexpected pid %d,"
> -			 " expecting pid %d", si->si_pid, expected_pid);
> -
> -	c++;
> +	signal_pid = si->si_pid;
> +	counter++;
>   }
>   
> -/*
> - * child_fn() - Inside container
> - */
> -int child_fn(void *ttype)
> +static int child_func(void)
>   {
>   	struct sigaction sa;
> -	pid_t pid, ppid;
> +	pid_t cpid, ppid;
>   
> -	/* Set process id and parent pid */
> -	pid = getpid();
> +	cpid = getpid();
>   	ppid = getppid();
>   
> -	if ((pid != CHILD_PID) || (ppid != PARENT_PID))
> -		tst_resm(TBROK, "pidns is not created.");
> +	TST_EXP_EQ_LI(cpid, 1);
> +	TST_EXP_EQ_LI(ppid, 0);
> +
> +	tst_res(TINFO, "Catching SIGUSR1 signal");
>   
> -	/* Set signal handler for SIGUSR1, also mask other signals */
>   	sa.sa_flags = SA_SIGINFO;
> -	sigemptyset(&sa.sa_mask);
> +	SAFE_SIGFILLSET(&sa.sa_mask);
>   	sa.sa_sigaction = child_signal_handler;
> -	if (sigaction(SIGUSR1, &sa, NULL) == -1)
> -		tst_resm(TBROK, "%d: sigaction() failed", pid);
> -
> -	pause();
> -	tst_resm(TINFO, "Container: Resumed after receiving SIGUSR1 "
> -		 "from parentNS ");
> -	if (kill(pid, SIGUSR1) != 0) {
> -		tst_resm(TFAIL, "kill(SIGUSR1) fails.");
> -	}
> -	tst_resm(TINFO, "Container: Resumed after sending SIGUSR1 "
> -		 "from container itself");
> -	_exit(10);
> -}
> +	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
>   
> -static void setup(void)
> -{
> -	tst_require_root();
> -	check_newpid();
> +	TST_CHECKPOINT_WAKE_AND_WAIT(0);
> +
> +	TST_EXP_EQ_LI(signal_pid, 0);
> +
> +	tst_res(TINFO, "Sending SIGUSR1 from container itself");
> +
> +	SAFE_KILL(cpid, SIGUSR1);
> +
> +	TST_EXP_EQ_LI(signal_pid, 1);
> +
> +	return 0;
>   }
>   
> -/***********************************************************************
> -*   M A I N
> -***********************************************************************/
> -int main()
> +static void run(void)
>   {
> -	int status;
> -	pid_t cpid;
> -
> -	setup();
> +	const struct tst_clone_args args = { CLONE_NEWPID, SIGCHLD };
> +	pid_t pid;
>   
> -	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
> +	signal_pid = -1;
>   
> -	if (cpid < 0) {
> -		tst_resm(TBROK, "clone() failed.");
> +	pid = SAFE_CLONE(&args);
> +	if (!pid) {
> +		child_func();
> +		return;
>   	}
>   
> -	sleep(1);
> -	if (kill(cpid, SIGUSR1) != 0) {
> -		tst_resm(TFAIL, "kill(SIGUSR1) fails.");
> -	}
> -	sleep(1);
> -	if (waitpid(cpid, &status, 0) < 0)
> -		tst_resm(TWARN, "waitpid() failed.");
> -
> -	if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10))
> -		tst_resm(TPASS, "container init continued successfuly, "
> -			 "after handling signal -USR1");
> -	else
> -		tst_resm(TFAIL, "c-init failed to continue after "
> -			 "passing kill -USR1");
> -	tst_exit();
> +	TST_CHECKPOINT_WAIT(0);
> +
> +	tst_res(TINFO, "Sending SIGUSR1 from parent");
> +
> +	SAFE_KILL(pid, SIGUSR1);
> +
> +	TST_CHECKPOINT_WAKE(0);
>   }
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.needs_root = 1,
> +	.needs_checkpoints = 1,
> +	.forks_child = 1,
> +};
diff mbox series

Patch

diff --git a/testcases/kernel/containers/pidns/pidns16.c b/testcases/kernel/containers/pidns/pidns16.c
index 2ee61065a..121da8e63 100644
--- a/testcases/kernel/containers/pidns/pidns16.c
+++ b/testcases/kernel/containers/pidns/pidns16.c
@@ -1,157 +1,88 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
-* Copyright (c) International Business Machines Corp., 2007
-* 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
-*
-***************************************************************************
-
-* * Test Assertion.
-* *----------------
-* * kill -USR1 container_init
-* *	- from the parent process and also inside a container
-* *	- Where init has defined a custom handler for USR1
-* *	- Should call the handler and
-* *	- Verify whether the signal handler is called from the proper process.
-* *
-* * Description:
-* *  Create PID namespace container.
-* *  Container init defines the handler for SIGUSR1 and waits indefinetly.
-* *  Parent sends SIGUSR1 to container init.
-* *  The signal handler is handled and the cont-init resumes normally.
-* *  From the container, again the signal SIGUSR1 is sent.
-* *  In the sig-handler check if it's invoked from correct pid(parent/container)
-* *  If cont-init wakes up properly -
-* *  it will return expected value at exit which is verified at the end.
-* *
-* * History:
-* *  DATE	  NAME				   DESCRIPTION
-* *  04/11/08  Veerendra C  <vechandr@in.ibm.com> Verifying cont init kill -USR1
-*
-*******************************************************************************/
-#include "config.h"
+ * Copyright (c) International Business Machines Corp., 2007
+ *               04/11/08  Veerendra C  <vechandr@in.ibm.com>
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Clone a process with CLONE_NEWPID flag and verifies that siginfo->si_pid is
+ * set to 0 if sender (parent process) sent the signal. Then send signal from
+ * container itself and check if siginfo->si_pid is set to 1.
+ */
 
 #define _GNU_SOURCE 1
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/wait.h>
-#include <sys/types.h>
 #include <signal.h>
-#include <unistd.h>
-#include "pidns_helper.h"
-#include "test.h"
-
-#define CHILD_PID	1
-#define PARENT_PID	0
+#include "tst_test.h"
+#include "lapi/sched.h"
 
-char *TCID = "pidns16";
-int TST_TOTAL = 3;
+static int counter;
+static int signal_pid;
 
-void child_signal_handler(int sig, siginfo_t * si, void *unused)
+static void child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig, siginfo_t *si, LTP_ATTRIBUTE_UNUSED void *unused)
 {
-	static int c = 1;
-	pid_t expected_pid;
-
-	/* Verifying from which process the signal handler is signalled */
-
-	switch (c) {
-	case 1:
-		expected_pid = PARENT_PID;
-		break;
-	case 2:
-		expected_pid = CHILD_PID;
-		break;
-	default:
-		tst_resm(TBROK, "child should NOT be signalled 3+ times");
-		return;
-	}
-
-	if (si->si_pid == expected_pid)
-		tst_resm(TPASS, "child is signalled from expected pid %d",
-			 expected_pid);
-	else
-		tst_resm(TFAIL, "child is signalled from unexpected pid %d,"
-			 " expecting pid %d", si->si_pid, expected_pid);
-
-	c++;
+	signal_pid = si->si_pid;
+	counter++;
 }
 
-/*
- * child_fn() - Inside container
- */
-int child_fn(void *ttype)
+static int child_func(void)
 {
 	struct sigaction sa;
-	pid_t pid, ppid;
+	pid_t cpid, ppid;
 
-	/* Set process id and parent pid */
-	pid = getpid();
+	cpid = getpid();
 	ppid = getppid();
 
-	if ((pid != CHILD_PID) || (ppid != PARENT_PID))
-		tst_resm(TBROK, "pidns is not created.");
+	TST_EXP_EQ_LI(cpid, 1);
+	TST_EXP_EQ_LI(ppid, 0);
+
+	tst_res(TINFO, "Catching SIGUSR1 signal");
 
-	/* Set signal handler for SIGUSR1, also mask other signals */
 	sa.sa_flags = SA_SIGINFO;
-	sigemptyset(&sa.sa_mask);
+	SAFE_SIGFILLSET(&sa.sa_mask);
 	sa.sa_sigaction = child_signal_handler;
-	if (sigaction(SIGUSR1, &sa, NULL) == -1)
-		tst_resm(TBROK, "%d: sigaction() failed", pid);
-
-	pause();
-	tst_resm(TINFO, "Container: Resumed after receiving SIGUSR1 "
-		 "from parentNS ");
-	if (kill(pid, SIGUSR1) != 0) {
-		tst_resm(TFAIL, "kill(SIGUSR1) fails.");
-	}
-	tst_resm(TINFO, "Container: Resumed after sending SIGUSR1 "
-		 "from container itself");
-	_exit(10);
-}
+	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
 
-static void setup(void)
-{
-	tst_require_root();
-	check_newpid();
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
+
+	TST_EXP_EQ_LI(signal_pid, 0);
+
+	tst_res(TINFO, "Sending SIGUSR1 from container itself");
+
+	SAFE_KILL(cpid, SIGUSR1);
+
+	TST_EXP_EQ_LI(signal_pid, 1);
+
+	return 0;
 }
 
-/***********************************************************************
-*   M A I N
-***********************************************************************/
-int main()
+static void run(void)
 {
-	int status;
-	pid_t cpid;
-
-	setup();
+	const struct tst_clone_args args = { CLONE_NEWPID, SIGCHLD };
+	pid_t pid;
 
-	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
+	signal_pid = -1;
 
-	if (cpid < 0) {
-		tst_resm(TBROK, "clone() failed.");
+	pid = SAFE_CLONE(&args);
+	if (!pid) {
+		child_func();
+		return;
 	}
 
-	sleep(1);
-	if (kill(cpid, SIGUSR1) != 0) {
-		tst_resm(TFAIL, "kill(SIGUSR1) fails.");
-	}
-	sleep(1);
-	if (waitpid(cpid, &status, 0) < 0)
-		tst_resm(TWARN, "waitpid() failed.");
-
-	if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10))
-		tst_resm(TPASS, "container init continued successfuly, "
-			 "after handling signal -USR1");
-	else
-		tst_resm(TFAIL, "c-init failed to continue after "
-			 "passing kill -USR1");
-	tst_exit();
+	TST_CHECKPOINT_WAIT(0);
+
+	tst_res(TINFO, "Sending SIGUSR1 from parent");
+
+	SAFE_KILL(pid, SIGUSR1);
+
+	TST_CHECKPOINT_WAKE(0);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.needs_root = 1,
+	.needs_checkpoints = 1,
+	.forks_child = 1,
+};