pidfd.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PIDFD_H
  3. #define __PIDFD_H
  4. #define _GNU_SOURCE
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <sched.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <syscall.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. /*
  17. * Remove the userspace definitions of the following preprocessor symbols
  18. * to avoid duplicate-definition warnings from the subsequent in-kernel
  19. * definitions.
  20. */
  21. #undef SCHED_NORMAL
  22. #undef SCHED_FLAG_KEEP_ALL
  23. #undef SCHED_FLAG_UTIL_CLAMP
  24. #include "kselftest.h"
  25. #include "../clone3/clone3_selftests.h"
  26. #ifndef FD_PIDFS_ROOT
  27. #define FD_PIDFS_ROOT -10002
  28. #endif
  29. #ifndef P_PIDFD
  30. #define P_PIDFD 3
  31. #endif
  32. #ifndef CLONE_NEWTIME
  33. #define CLONE_NEWTIME 0x00000080
  34. #endif
  35. #ifndef CLONE_PIDFD
  36. #define CLONE_PIDFD 0x00001000
  37. #endif
  38. #ifndef __NR_pidfd_open
  39. #define __NR_pidfd_open 434
  40. #endif
  41. #ifndef __NR_pidfd_send_signal
  42. #define __NR_pidfd_send_signal 424
  43. #endif
  44. #ifndef __NR_clone3
  45. #define __NR_clone3 435
  46. #endif
  47. #ifndef __NR_pidfd_getfd
  48. #define __NR_pidfd_getfd 438
  49. #endif
  50. #ifndef PIDFD_NONBLOCK
  51. #define PIDFD_NONBLOCK O_NONBLOCK
  52. #endif
  53. #ifndef PIDFD_SELF_THREAD
  54. #define PIDFD_SELF_THREAD -10000 /* Current thread. */
  55. #endif
  56. #ifndef PIDFD_SELF_THREAD_GROUP
  57. #define PIDFD_SELF_THREAD_GROUP -10001 /* Current thread group leader. */
  58. #endif
  59. #ifndef PIDFD_SELF
  60. #define PIDFD_SELF PIDFD_SELF_THREAD
  61. #endif
  62. #ifndef PIDFD_SELF_PROCESS
  63. #define PIDFD_SELF_PROCESS PIDFD_SELF_THREAD_GROUP
  64. #endif
  65. #ifndef PIDFS_IOCTL_MAGIC
  66. #define PIDFS_IOCTL_MAGIC 0xFF
  67. #endif
  68. #ifndef PIDFD_GET_CGROUP_NAMESPACE
  69. #define PIDFD_GET_CGROUP_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 1)
  70. #endif
  71. #ifndef PIDFD_GET_IPC_NAMESPACE
  72. #define PIDFD_GET_IPC_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 2)
  73. #endif
  74. #ifndef PIDFD_GET_MNT_NAMESPACE
  75. #define PIDFD_GET_MNT_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 3)
  76. #endif
  77. #ifndef PIDFD_GET_NET_NAMESPACE
  78. #define PIDFD_GET_NET_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 4)
  79. #endif
  80. #ifndef PIDFD_GET_PID_NAMESPACE
  81. #define PIDFD_GET_PID_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 5)
  82. #endif
  83. #ifndef PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE
  84. #define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 6)
  85. #endif
  86. #ifndef PIDFD_GET_TIME_NAMESPACE
  87. #define PIDFD_GET_TIME_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 7)
  88. #endif
  89. #ifndef PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE
  90. #define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8)
  91. #endif
  92. #ifndef PIDFD_GET_USER_NAMESPACE
  93. #define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9)
  94. #endif
  95. #ifndef PIDFD_GET_UTS_NAMESPACE
  96. #define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10)
  97. #endif
  98. #ifndef PIDFD_GET_INFO
  99. #define PIDFD_GET_INFO _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info)
  100. #endif
  101. #ifndef PIDFD_INFO_PID
  102. #define PIDFD_INFO_PID (1UL << 0) /* Always returned, even if not requested */
  103. #endif
  104. #ifndef PIDFD_INFO_CREDS
  105. #define PIDFD_INFO_CREDS (1UL << 1) /* Always returned, even if not requested */
  106. #endif
  107. #ifndef PIDFD_INFO_CGROUPID
  108. #define PIDFD_INFO_CGROUPID (1UL << 2) /* Always returned if available, even if not requested */
  109. #endif
  110. #ifndef PIDFD_INFO_EXIT
  111. #define PIDFD_INFO_EXIT (1UL << 3) /* Always returned if available, even if not requested */
  112. #endif
  113. #ifndef PIDFD_INFO_COREDUMP
  114. #define PIDFD_INFO_COREDUMP (1UL << 4)
  115. #endif
  116. #ifndef PIDFD_INFO_SUPPORTED_MASK
  117. #define PIDFD_INFO_SUPPORTED_MASK (1UL << 5)
  118. #endif
  119. #ifndef PIDFD_INFO_COREDUMP_SIGNAL
  120. #define PIDFD_INFO_COREDUMP_SIGNAL (1UL << 6)
  121. #endif
  122. #ifndef PIDFD_COREDUMPED
  123. #define PIDFD_COREDUMPED (1U << 0) /* Did crash and... */
  124. #endif
  125. #ifndef PIDFD_COREDUMP_SKIP
  126. #define PIDFD_COREDUMP_SKIP (1U << 1) /* coredumping generation was skipped. */
  127. #endif
  128. #ifndef PIDFD_COREDUMP_USER
  129. #define PIDFD_COREDUMP_USER (1U << 2) /* coredump was done as the user. */
  130. #endif
  131. #ifndef PIDFD_COREDUMP_ROOT
  132. #define PIDFD_COREDUMP_ROOT (1U << 3) /* coredump was done as root. */
  133. #endif
  134. #ifndef PIDFD_THREAD
  135. #define PIDFD_THREAD O_EXCL
  136. #endif
  137. struct pidfd_info {
  138. __u64 mask;
  139. __u64 cgroupid;
  140. __u32 pid;
  141. __u32 tgid;
  142. __u32 ppid;
  143. __u32 ruid;
  144. __u32 rgid;
  145. __u32 euid;
  146. __u32 egid;
  147. __u32 suid;
  148. __u32 sgid;
  149. __u32 fsuid;
  150. __u32 fsgid;
  151. __s32 exit_code;
  152. struct {
  153. __u32 coredump_mask;
  154. __u32 coredump_signal;
  155. };
  156. __u64 supported_mask;
  157. };
  158. /*
  159. * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
  160. * That means, when it wraps around any pid < 300 will be skipped.
  161. * So we need to use a pid > 300 in order to test recycling.
  162. */
  163. #define PID_RECYCLE 1000
  164. /*
  165. * Define a few custom error codes for the child process to clearly indicate
  166. * what is happening. This way we can tell the difference between a system
  167. * error, a test error, etc.
  168. */
  169. #define PIDFD_PASS 0
  170. #define PIDFD_FAIL 1
  171. #define PIDFD_ERROR 2
  172. #define PIDFD_SKIP 3
  173. #define PIDFD_XFAIL 4
  174. static inline int sys_waitid(int which, pid_t pid, siginfo_t *info, int options)
  175. {
  176. return syscall(__NR_waitid, which, pid, info, options, NULL);
  177. }
  178. static inline int wait_for_pid(pid_t pid)
  179. {
  180. int status, ret;
  181. again:
  182. ret = waitpid(pid, &status, 0);
  183. if (ret == -1) {
  184. if (errno == EINTR)
  185. goto again;
  186. ksft_print_msg("waitpid returned -1, errno=%d\n", errno);
  187. return -1;
  188. }
  189. if (!WIFEXITED(status)) {
  190. ksft_print_msg(
  191. "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n",
  192. WIFSIGNALED(status), WTERMSIG(status));
  193. return -1;
  194. }
  195. ret = WEXITSTATUS(status);
  196. return ret;
  197. }
  198. static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
  199. {
  200. return syscall(__NR_pidfd_open, pid, flags);
  201. }
  202. static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
  203. unsigned int flags)
  204. {
  205. return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
  206. }
  207. static inline int sys_pidfd_getfd(int pidfd, int fd, int flags)
  208. {
  209. return syscall(__NR_pidfd_getfd, pidfd, fd, flags);
  210. }
  211. static inline int sys_memfd_create(const char *name, unsigned int flags)
  212. {
  213. return syscall(__NR_memfd_create, name, flags);
  214. }
  215. static inline pid_t create_child(int *pidfd, unsigned flags)
  216. {
  217. struct __clone_args args = {
  218. .flags = CLONE_PIDFD | flags,
  219. .exit_signal = SIGCHLD,
  220. .pidfd = ptr_to_u64(pidfd),
  221. };
  222. return sys_clone3(&args, sizeof(struct __clone_args));
  223. }
  224. static inline ssize_t read_nointr(int fd, void *buf, size_t count)
  225. {
  226. ssize_t ret;
  227. do {
  228. ret = read(fd, buf, count);
  229. } while (ret < 0 && errno == EINTR);
  230. return ret;
  231. }
  232. static inline ssize_t write_nointr(int fd, const void *buf, size_t count)
  233. {
  234. ssize_t ret;
  235. do {
  236. ret = write(fd, buf, count);
  237. } while (ret < 0 && errno == EINTR);
  238. return ret;
  239. }
  240. static inline int sys_execveat(int dirfd, const char *pathname,
  241. char *const argv[], char *const envp[],
  242. int flags)
  243. {
  244. return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
  245. }
  246. #endif /* __PIDFD_H */