Skip to contentSkip to conversation

Refactor pidns16 test using new LTP API

PATCH Superseded Andrea Cervesato via ltp 2 participants 2 messages Aug 16, 2022 – Nov 1, 2022

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Refactor pidns16 test using new LTP API

---
 testcases/kernel/containers/pidns/pidns16.c | 197 +++++++-------------
 1 file changed, 68 insertions(+), 129 deletions(-)
testcases/kernel/containers/pidns/pidns16.c +68 -129
index 2ee61065a..d11e8eaa1 100644
@@ -1,157 +1,96 @@
+// 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  <[email protected]> Verifying cont init kill -USR1
-*
-*******************************************************************************/
-#include "config.h"
+ * Copyright (c) International Business Machines Corp., 2007
+ *               04/11/08  Veerendra C  <[email protected]>
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [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/namespaces_constants.h"
 
-char *TCID = "pidns16";
-int TST_TOTAL = 3;
+static volatile int exp_pid;
+static volatile int passed;
 
-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;
-	}
+	tst_res(TINFO, "Received signal %s from PID %d", tst_strsig(si->si_signo), si->si_pid);
 
-	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);
+	if (si->si_signo != SIGUSR1)
+		return;
 
-	c++;
+	if (si->si_pid == exp_pid)
+		passed = 1;
 }
 
-/*
- * child_fn() - Inside container
- */
-int child_fn(void *ttype)
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
 {
 	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.");
+	if (cpid != 1 || ppid != 0) {
+		tst_res(TFAIL, "Got unexpected result of cpid=%d ppid=%d", cpid, ppid);
+		return 1;
+	}
 
-	/* 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);
-}
 
-static void setup(void)
-{
-	tst_require_root();
-	check_newpid();
-}
+	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
 
-/***********************************************************************
-*   M A I N
-***********************************************************************/
-int main()
-{
-	int status;
-	pid_t cpid;
+	passed = 0;
+	exp_pid = 0;
 
-	setup();
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
-	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
+	if (passed)
+		tst_res(TPASS, "Container resumed after receiving SIGUSR1 from parent namespace");
+	else
+		tst_res(TFAIL, "Container did not receive the SIGUSR1 signal from parent");
 
-	if (cpid < 0) {
-		tst_resm(TBROK, "clone() failed.");
-	}
+	passed = 0;
+	exp_pid = 1;
 
-	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.");
+	SAFE_KILL(cpid, SIGUSR1);
 
-	if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10))
-		tst_resm(TPASS, "container init continued successfuly, "
-			 "after handling signal -USR1");
+	if (passed)
+		tst_res(TPASS, "Container resumed after receiving SIGUSR1 from container namespace");
 	else
-		tst_resm(TFAIL, "c-init failed to continue after "
-			 "passing kill -USR1");
-	tst_exit();
+		tst_res(TFAIL, "Container did not receive the SIGUSR1 signal from container");
+
+	return 0;
+}
+
+static void run(void)
+{
+	int ret;
+
+	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, NULL);
+	if (ret < 0)
+		tst_brk(TBROK | TERRNO, "clone failed");
+
+	TST_CHECKPOINT_WAIT(0);
+
+	SAFE_KILL(ret, SIGUSR1);
+
+	TST_CHECKPOINT_WAKE(0);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.needs_root = 1,
+	.needs_checkpoints = 1,
+};
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hello,

Quoted 95 lines · Andrea Cervesato via ltp
---
 testcases/kernel/containers/pidns/pidns16.c | 197 +++++++-------------
 1 file changed, 68 insertions(+), 129 deletions(-)
testcases/kernel/containers/pidns/pidns16.c +18 -60
index 2ee61065a..d11e8eaa1 100644
@@ -1,157 +1,96 @@
+// 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  <[email protected]> Verifying cont init kill -USR1
-*
-*******************************************************************************/
-#include "config.h"
+ * Copyright (c) International Business Machines Corp., 2007
+ *               04/11/08  Veerendra C  <[email protected]>
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [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/namespaces_constants.h"
 
-char *TCID = "pidns16";
-int TST_TOTAL = 3;
+static volatile int exp_pid;
+static volatile int passed;
 
-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;

testcases/kernel/containers/pidns/pidns16.c line 27

Quoted 10 lines
testcases/kernel/containers/pidns/pidns16.c +1 -8
-		break;
-	case 2:
-		expected_pid = CHILD_PID;
-		break;
-	default:
-		tst_resm(TBROK, "child should NOT be signalled 3+ times");
-		return;
-	}
+	tst_res(TINFO, "Received signal %s from PID %d",
 tst_strsig(si->si_signo), si->si_pid);

I missed this on pidns12. We are calling tst_res in a signal handler which Cyril warned against

"The bigger problem though is that we call tst_brk() and tst_res() in the handler, which is not safe at all. The are couple things that can go wrong here, mostly glibc locks can get deadlocked for the underlying FILE* and if the underlying fprintf() calls malloc() the malloc data structures may end up corrupted. It's quite rare for this to happen, but there have been a few tests that were failing with small probability due to these mistakes."

We should only save the values in the sighandler and check them in the main child function.

Quoted 25 lines
testcases/kernel/containers/pidns/pidns16.c +6 -12
-	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);
+	if (si->si_signo != SIGUSR1)
+		return;
 
-	c++;
+	if (si->si_pid == exp_pid)
+		passed = 1;
 }
 
-/*
- * child_fn() - Inside container
- */
-int child_fn(void *ttype)
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
 {
 	struct sigaction sa;
-	pid_t pid, ppid;
+	pid_t cpid, ppid;
 

testcases/kernel/containers/pidns/pidns16.c line 45

Quoted 10 lines
testcases/kernel/containers/pidns/pidns16.c +4 -4
-	/* 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.");
+	if (cpid != 1 || ppid != 0) {
+		tst_res(TFAIL, "Got unexpected result of cpid=%d ppid=%d", cpid, ppid);
+		return 1;

Same with TST_EXP_EQ

Quoted 98 lines
testcases/kernel/containers/pidns/pidns16.c +39 -45
+	}
 
-	/* 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);
-}
 
-static void setup(void)
-{
-	tst_require_root();
-	check_newpid();
-}
+	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
 
-/***********************************************************************
-*   M A I N
-***********************************************************************/
-int main()
-{
-	int status;
-	pid_t cpid;
+	passed = 0;
+	exp_pid = 0;
 
-	setup();
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
-	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
+	if (passed)
+		tst_res(TPASS, "Container resumed after receiving SIGUSR1 from parent namespace");
+	else
+		tst_res(TFAIL, "Container did not receive the SIGUSR1 signal from parent");
 
-	if (cpid < 0) {
-		tst_resm(TBROK, "clone() failed.");
-	}
+	passed = 0;
+	exp_pid = 1;
 
-	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.");
+	SAFE_KILL(cpid, SIGUSR1);
 
-	if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10))
-		tst_resm(TPASS, "container init continued successfuly, "
-			 "after handling signal -USR1");
+	if (passed)
+		tst_res(TPASS, "Container resumed after receiving SIGUSR1 from container namespace");
 	else
-		tst_resm(TFAIL, "c-init failed to continue after "
-			 "passing kill -USR1");
-	tst_exit();
+		tst_res(TFAIL, "Container did not receive the SIGUSR1 signal from container");
+
+	return 0;
+}
+
+static void run(void)
+{
+	int ret;
+
+	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, NULL);
+	if (ret < 0)
+		tst_brk(TBROK | TERRNO, "clone failed");
+
+	TST_CHECKPOINT_WAIT(0);
+
+	SAFE_KILL(ret, SIGUSR1);
+
+	TST_CHECKPOINT_WAKE(0);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.needs_root = 1,
+	.needs_checkpoints = 1,
+};
Signature · 1 line
2.35.3
Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp