Skip to content

Commit 9591948

Browse files
mdouchametan-ucw
authored andcommitted
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]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent 4384104 commit 9591948

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

include/tst_pid.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ static inline int tst_get_free_pids(void (*cleanup_fn)(void))
4242
}
4343
#endif
4444

45+
/*
46+
* Direct getpid() syscall. Some glibc versions cache getpid() return value
47+
* which can cause confusing issues for example in processes created by
48+
* direct clone() syscall (without using the glibc wrapper). Use this function
49+
* whenever the current process may be a child of the main test process.
50+
*/
51+
pid_t tst_getpid(void);
52+
4553
#endif /* TST_PID_H__ */

lib/tst_pid.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "tst_pid.h"
3131
#include "old_safe_file_ops.h"
3232
#include "tst_safe_macros.h"
33+
#include "lapi/syscalls.h"
3334

3435
#define PID_MAX_PATH "/proc/sys/kernel/pid_max"
3536
#define THREADS_MAX_PATH "/proc/sys/kernel/threads-max"
@@ -160,3 +161,8 @@ int tst_get_free_pids_(void (*cleanup_fn) (void))
160161
}
161162
return max_pids - used_pids;
162163
}
164+
165+
pid_t tst_getpid(void)
166+
{
167+
return syscall(SYS_getpid);
168+
}

lib/tst_test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void tst_vbrk_(const char *file, const int lineno, int ttype,
336336
* specified but CLONE_THREAD is not. Use direct syscall to avoid
337337
* cleanup running in the child.
338338
*/
339-
if (syscall(SYS_getpid) == main_pid)
339+
if (tst_getpid() == main_pid)
340340
do_test_cleanup();
341341

342342
if (getpid() == lib_pid)
@@ -1316,7 +1316,7 @@ static void do_test_setup(void)
13161316
if (tst_test->setup)
13171317
tst_test->setup();
13181318

1319-
if (main_pid != getpid())
1319+
if (main_pid != tst_getpid())
13201320
tst_brk(TBROK, "Runaway child in setup()!");
13211321

13221322
if (tst_test->caps)
@@ -1379,7 +1379,7 @@ static void run_tests(void)
13791379
heartbeat();
13801380
tst_test->test_all();
13811381

1382-
if (getpid() != main_pid) {
1382+
if (tst_getpid() != main_pid) {
13831383
exit(0);
13841384
}
13851385

@@ -1395,7 +1395,7 @@ static void run_tests(void)
13951395
heartbeat();
13961396
tst_test->test(i);
13971397

1398-
if (getpid() != main_pid) {
1398+
if (tst_getpid() != main_pid) {
13991399
exit(0);
14001400
}
14011401

0 commit comments

Comments
 (0)