pidfd.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/mount.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include "../kselftest.h"
  17. #ifndef P_PIDFD
  18. #define P_PIDFD 3
  19. #endif
  20. #ifndef CLONE_NEWTIME
  21. #define CLONE_NEWTIME 0x00000080
  22. #endif
  23. #ifndef CLONE_PIDFD
  24. #define CLONE_PIDFD 0x00001000
  25. #endif
  26. #ifndef __NR_pidfd_open
  27. #define __NR_pidfd_open -1
  28. #endif
  29. #ifndef __NR_pidfd_send_signal
  30. #define __NR_pidfd_send_signal -1
  31. #endif
  32. #ifndef __NR_clone3
  33. #define __NR_clone3 -1
  34. #endif
  35. #ifndef __NR_pidfd_getfd
  36. #define __NR_pidfd_getfd -1
  37. #endif
  38. #ifndef PIDFD_NONBLOCK
  39. #define PIDFD_NONBLOCK O_NONBLOCK
  40. #endif
  41. /*
  42. * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
  43. * That means, when it wraps around any pid < 300 will be skipped.
  44. * So we need to use a pid > 300 in order to test recycling.
  45. */
  46. #define PID_RECYCLE 1000
  47. /*
  48. * Define a few custom error codes for the child process to clearly indicate
  49. * what is happening. This way we can tell the difference between a system
  50. * error, a test error, etc.
  51. */
  52. #define PIDFD_PASS 0
  53. #define PIDFD_FAIL 1
  54. #define PIDFD_ERROR 2
  55. #define PIDFD_SKIP 3
  56. #define PIDFD_XFAIL 4
  57. static inline int wait_for_pid(pid_t pid)
  58. {
  59. int status, ret;
  60. again:
  61. ret = waitpid(pid, &status, 0);
  62. if (ret == -1) {
  63. if (errno == EINTR)
  64. goto again;
  65. ksft_print_msg("waitpid returned -1, errno=%d\n", errno);
  66. return -1;
  67. }
  68. if (!WIFEXITED(status)) {
  69. ksft_print_msg(
  70. "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n",
  71. WIFSIGNALED(status), WTERMSIG(status));
  72. return -1;
  73. }
  74. ret = WEXITSTATUS(status);
  75. ksft_print_msg("waitpid WEXITSTATUS=%d\n", ret);
  76. return ret;
  77. }
  78. static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
  79. {
  80. return syscall(__NR_pidfd_open, pid, flags);
  81. }
  82. static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
  83. unsigned int flags)
  84. {
  85. return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
  86. }
  87. static inline int sys_pidfd_getfd(int pidfd, int fd, int flags)
  88. {
  89. return syscall(__NR_pidfd_getfd, pidfd, fd, flags);
  90. }
  91. static inline int sys_memfd_create(const char *name, unsigned int flags)
  92. {
  93. return syscall(__NR_memfd_create, name, flags);
  94. }
  95. #endif /* __PIDFD_H */