[LTP] [PATCH 2/2] containers/pidns*: Fix PID checks

Martin Doucha mdoucha@suse.cz
Wed May 10 14:38:50 CEST 2023


Some glibc versions cache the return value of getpid() which can result
in getpid() calls returning parent PID when the process was created
using direct clone() syscall. Use tst_getpid() to get the correct
child PID.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/kernel/containers/pidns/pidns01.c | 2 +-
 testcases/kernel/containers/pidns/pidns04.c | 2 +-
 testcases/kernel/containers/pidns/pidns06.c | 2 +-
 testcases/kernel/containers/pidns/pidns10.c | 2 +-
 testcases/kernel/containers/pidns/pidns12.c | 2 +-
 testcases/kernel/containers/pidns/pidns13.c | 4 ++--
 testcases/kernel/containers/pidns/pidns16.c | 2 +-
 testcases/kernel/containers/pidns/pidns17.c | 2 +-
 testcases/kernel/containers/pidns/pidns20.c | 2 +-
 testcases/kernel/containers/pidns/pidns30.c | 2 +-
 testcases/kernel/containers/pidns/pidns31.c | 2 +-
 11 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/testcases/kernel/containers/pidns/pidns01.c b/testcases/kernel/containers/pidns/pidns01.c
index 6b5ec48ac..107519971 100644
--- a/testcases/kernel/containers/pidns/pidns01.c
+++ b/testcases/kernel/containers/pidns/pidns01.c
@@ -21,7 +21,7 @@ static void child_func(void)
 {
 	pid_t cpid, ppid;
 
-	cpid = getpid();
+	cpid = tst_getpid();
 	ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
diff --git a/testcases/kernel/containers/pidns/pidns04.c b/testcases/kernel/containers/pidns/pidns04.c
index d337e1fe4..8df32899a 100644
--- a/testcases/kernel/containers/pidns/pidns04.c
+++ b/testcases/kernel/containers/pidns/pidns04.c
@@ -17,7 +17,7 @@
 
 static void child_func(void)
 {
-	pid_t cpid = getpid();
+	pid_t cpid = tst_getpid();
 	pid_t ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
diff --git a/testcases/kernel/containers/pidns/pidns06.c b/testcases/kernel/containers/pidns/pidns06.c
index ed440f043..8ded0a6b1 100644
--- a/testcases/kernel/containers/pidns/pidns06.c
+++ b/testcases/kernel/containers/pidns/pidns06.c
@@ -16,7 +16,7 @@
 
 static void child_func(int pid)
 {
-	pid_t cpid = getpid();
+	pid_t cpid = tst_getpid();
 	pid_t ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
diff --git a/testcases/kernel/containers/pidns/pidns10.c b/testcases/kernel/containers/pidns/pidns10.c
index a35c73f5d..f65b5887b 100644
--- a/testcases/kernel/containers/pidns/pidns10.c
+++ b/testcases/kernel/containers/pidns/pidns10.c
@@ -16,7 +16,7 @@
 
 static void child_func(void)
 {
-	pid_t cpid = getpid();
+	pid_t cpid = tst_getpid();
 	pid_t ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
diff --git a/testcases/kernel/containers/pidns/pidns12.c b/testcases/kernel/containers/pidns/pidns12.c
index ab035f33d..b527dbba9 100644
--- a/testcases/kernel/containers/pidns/pidns12.c
+++ b/testcases/kernel/containers/pidns/pidns12.c
@@ -29,7 +29,7 @@ static void child_func(void)
 {
 	struct sigaction sa;
 
-	TST_EXP_EQ_LI(getpid(), 1);
+	TST_EXP_EQ_LI(tst_getpid(), 1);
 	TST_EXP_EQ_LI(getppid(), 0);
 
 	sa.sa_flags = SA_SIGINFO;
diff --git a/testcases/kernel/containers/pidns/pidns13.c b/testcases/kernel/containers/pidns/pidns13.c
index 6a155027c..64038310d 100644
--- a/testcases/kernel/containers/pidns/pidns13.c
+++ b/testcases/kernel/containers/pidns/pidns13.c
@@ -42,7 +42,7 @@ static void child_signal_handler(int sig, siginfo_t *si,
 				 void *unused LTP_ATTRIBUTE_UNUSED)
 {
 	tst_res(TWARN, "cinit(pid %d): Caught signal! sig=%d, si_fd=%d, si_code=%d",
-		getpid(), sig, si->si_fd, si->si_code);
+		tst_getpid(), sig, si->si_fd, si->si_code);
 }
 
 static void child_fn(unsigned int cinit_no)
@@ -54,7 +54,7 @@ static void child_fn(unsigned int cinit_no)
 	pid_t pid, ppid;
 	int flags;
 
-	pid = tst_syscall(__NR_getpid);
+	pid = tst_getpid();
 	ppid = getppid();
 	if (pid != CHILD_PID || ppid != PARENT_PID)
 		tst_brk(TBROK, "cinit%u: pidns not created.", cinit_no);
diff --git a/testcases/kernel/containers/pidns/pidns16.c b/testcases/kernel/containers/pidns/pidns16.c
index 43a221bb4..22da53213 100644
--- a/testcases/kernel/containers/pidns/pidns16.c
+++ b/testcases/kernel/containers/pidns/pidns16.c
@@ -30,7 +30,7 @@ static void child_func(void)
 	struct sigaction sa;
 	pid_t cpid, ppid;
 
-	cpid = getpid();
+	cpid = tst_getpid();
 	ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
diff --git a/testcases/kernel/containers/pidns/pidns17.c b/testcases/kernel/containers/pidns/pidns17.c
index c517fa124..823cb90fc 100644
--- a/testcases/kernel/containers/pidns/pidns17.c
+++ b/testcases/kernel/containers/pidns/pidns17.c
@@ -24,7 +24,7 @@ static void child_func(void)
 	unsigned int i;
 	pid_t cpid, ppid;
 
-	cpid = getpid();
+	cpid = tst_getpid();
 	ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
diff --git a/testcases/kernel/containers/pidns/pidns20.c b/testcases/kernel/containers/pidns/pidns20.c
index 4d0924c4e..4d87a8303 100644
--- a/testcases/kernel/containers/pidns/pidns20.c
+++ b/testcases/kernel/containers/pidns/pidns20.c
@@ -32,7 +32,7 @@ static void child_func(void)
 	sigset_t newset;
 	pid_t cpid, ppid;
 
-	cpid = getpid();
+	cpid = tst_getpid();
 	ppid = getppid();
 
 	if (cpid != 1 || ppid != 0) {
diff --git a/testcases/kernel/containers/pidns/pidns30.c b/testcases/kernel/containers/pidns/pidns30.c
index c408484dd..8e95e7963 100644
--- a/testcases/kernel/containers/pidns/pidns30.c
+++ b/testcases/kernel/containers/pidns/pidns30.c
@@ -47,7 +47,7 @@ static void child_func(void)
 	struct sigevent notif;
 	mqd_t mqd_child;
 
-	cpid = getpid();
+	cpid = tst_getpid();
 	ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
diff --git a/testcases/kernel/containers/pidns/pidns31.c b/testcases/kernel/containers/pidns/pidns31.c
index ad843cb6c..91f8cf1d6 100644
--- a/testcases/kernel/containers/pidns/pidns31.c
+++ b/testcases/kernel/containers/pidns/pidns31.c
@@ -45,7 +45,7 @@ static void child_func(void)
 	pid_t cpid, ppid;
 	mqd_t mqd_child;
 
-	cpid = getpid();
+	cpid = tst_getpid();
 	ppid = getppid();
 
 	TST_EXP_EQ_LI(cpid, 1);
-- 
2.40.0



More information about the ltp mailing list