• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
3  */
4 
5 #ifndef TST_PID_H__
6 #define TST_PID_H__
7 
8 #include <sys/types.h>
9 
10 /*
11  * Get a pid value not used by the OS
12  */
13 pid_t tst_get_unused_pid_(void (*cleanup_fn)(void));
14 
15 /*
16  * Returns number of free pids by subtraction of the number of pids
17  * currently used ('ps -eT') from maximum number of processes.
18  * The limit of processes come from kernel pid_max and cgroup session limits
19  * (e.g. configured by systemd user.slice).
20  */
21 int tst_get_free_pids_(void (*cleanup_fn)(void));
22 
23 #ifdef TST_TEST_H__
tst_get_unused_pid(void)24 static inline pid_t tst_get_unused_pid(void)
25 {
26 	return tst_get_unused_pid_(NULL);
27 }
28 
tst_get_free_pids(void)29 static inline int tst_get_free_pids(void)
30 {
31 	return tst_get_free_pids_(NULL);
32 }
33 #else
tst_get_unused_pid(void (* cleanup_fn)(void))34 static inline pid_t tst_get_unused_pid(void (*cleanup_fn)(void))
35 {
36 	return tst_get_unused_pid_(cleanup_fn);
37 }
38 
tst_get_free_pids(void (* cleanup_fn)(void))39 static inline int tst_get_free_pids(void (*cleanup_fn)(void))
40 {
41 	return tst_get_free_pids_(cleanup_fn);
42 }
43 #endif
44 
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 
53 #endif /* TST_PID_H__ */
54