Skip to contentSkip to conversation

Add tst_getpid() helper function

PATCH Superseded Martin Doucha 1 participant 2 messages May 10, 2023 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add tst_getpid() helper function

tst_getpid() returns current process ID using direct syscall to bypass PID caching in some glibc versions. The getpid() cache causes issues for example in child processes created by direct clone() syscall.

Also use the function in tst_test.c whenever the current test process may be a child created through unknown means.

---
 include/tst_pid.h | 8 ++++++++
 lib/tst_pid.c     | 6 ++++++
 lib/tst_test.c    | 8 ++++----
 3 files changed, 18 insertions(+), 4 deletions(-)
include/tst_pid.h +8 -0
index 8d999a655..774c845ce 100644
@@ -42,4 +42,12 @@ static inline int tst_get_free_pids(void (*cleanup_fn)(void))
 }
 #endif
 
+/*
+ * Direct getpid() syscall. Some glibc versions cache getpid() return value
+ * which can cause confusing issues for example in processes created by
+ * direct clone() syscall (without using the glibc wrapper). Use this function
+ * whenever the current process may be a child of the main test process.
+ */
+pid_t tst_getpid(void);
+
 #endif /* TST_PID_H__ */
lib/tst_pid.c +6 -0
index 2fa0c04cd..cfaa5db36 100644
@@ -30,6 +30,7 @@
 #include "tst_pid.h"
 #include "old_safe_file_ops.h"
 #include "tst_safe_macros.h"
+#include "lapi/syscalls.h"
 
 #define PID_MAX_PATH "/proc/sys/kernel/pid_max"
 #define THREADS_MAX_PATH "/proc/sys/kernel/threads-max"
@@ -160,3 +161,8 @@ int tst_get_free_pids_(void (*cleanup_fn) (void))
 	}
 	return max_pids - used_pids;
 }
+
+pid_t tst_getpid(void)
+{
+	return syscall(SYS_getpid);
+}
lib/tst_test.c +4 -4
index 04d6b13bf..04da456c6 100644
@@ -336,7 +336,7 @@ void tst_vbrk_(const char *file, const int lineno, int ttype,
 	 * specified but CLONE_THREAD is not. Use direct syscall to avoid
 	 * cleanup running in the child.
 	 */
-	if (syscall(SYS_getpid) == main_pid)
+	if (tst_getpid() == main_pid)
 		do_test_cleanup();
 
 	if (getpid() == lib_pid)
@@ -1316,7 +1316,7 @@ static void do_test_setup(void)
 	if (tst_test->setup)
 		tst_test->setup();
 
-	if (main_pid != getpid())
+	if (main_pid != tst_getpid())
 		tst_brk(TBROK, "Runaway child in setup()!");
 
 	if (tst_test->caps)
@@ -1379,7 +1379,7 @@ static void run_tests(void)
 		heartbeat();
 		tst_test->test_all();
 
-		if (getpid() != main_pid) {
+		if (tst_getpid() != main_pid) {
 			exit(0);
 		}
 
@@ -1395,7 +1395,7 @@ static void run_tests(void)
 		heartbeat();
 		tst_test->test(i);
 
-		if (getpid() != main_pid) {
+		if (tst_getpid() != main_pid) {
 			exit(0);
 		}
 
Signature · 5 lines
2.40.0


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

PATCH 2/2 containers/pidns*: Fix PID checks

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.

---
 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(-)
testcases/kernel/containers/pidns/pidns01.c +1 -1
index 6b5ec48ac..107519971 100644
@@ -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);
testcases/kernel/containers/pidns/pidns04.c +1 -1
index d337e1fe4..8df32899a 100644
@@ -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);
testcases/kernel/containers/pidns/pidns06.c +1 -1
index ed440f043..8ded0a6b1 100644
@@ -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);
testcases/kernel/containers/pidns/pidns10.c +1 -1
index a35c73f5d..f65b5887b 100644
@@ -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);
testcases/kernel/containers/pidns/pidns12.c +1 -1
index ab035f33d..b527dbba9 100644
@@ -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;
testcases/kernel/containers/pidns/pidns13.c +2 -2
index 6a155027c..64038310d 100644
@@ -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);
testcases/kernel/containers/pidns/pidns16.c +1 -1
index 43a221bb4..22da53213 100644
@@ -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);
testcases/kernel/containers/pidns/pidns17.c +1 -1
index c517fa124..823cb90fc 100644
@@ -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);
testcases/kernel/containers/pidns/pidns20.c +1 -1
index 4d0924c4e..4d87a8303 100644
@@ -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) {
testcases/kernel/containers/pidns/pidns30.c +1 -1
index c408484dd..8e95e7963 100644
@@ -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);
testcases/kernel/containers/pidns/pidns31.c +1 -1
index ad843cb6c..91f8cf1d6 100644
@@ -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);
Signature · 5 lines
2.40.0


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