Add tst_getpid() helper function
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.
Signed-off-by Martin Doucha <[email protected]>
--- 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
@@ -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
@@ -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
@@ -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
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.
Signed-off-by Martin Doucha <[email protected]>
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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