pidfd.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Wrapper for file descriptors that refers to a process functions.
  2. Copyright (C) 2022-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _PIDFD_H
  16. #include <fcntl.h>
  17. #include <bits/types/siginfo_t.h>
  18. #include <sys/ioctl.h>
  19. #define PIDFD_NONBLOCK O_NONBLOCK
  20. #define PIDFD_THREAD O_EXCL
  21. #define PIDFD_SIGNAL_THREAD (1UL << 0)
  22. #define PIDFD_SIGNAL_THREAD_GROUP (1UL << 1)
  23. #define PIDFD_SIGNAL_PROCESS_GROUP (1UL << 2)
  24. #define PIDFS_IOCTL_MAGIC 0xFF
  25. #define PIDFD_GET_CGROUP_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 1)
  26. #define PIDFD_GET_IPC_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 2)
  27. #define PIDFD_GET_MNT_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 3)
  28. #define PIDFD_GET_NET_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 4)
  29. #define PIDFD_GET_PID_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 5)
  30. #define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 6)
  31. #define PIDFD_GET_TIME_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 7)
  32. #define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8)
  33. #define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9)
  34. #define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10)
  35. /* Sentinels to avoid allocating a file descriptor to refer to own process. */
  36. #define PIDFD_SELF_THREAD -10000
  37. #define PIDFD_SELF_THREAD_GROUP -10001
  38. #define PIDFD_SELF PIDFD_SELF_THREAD
  39. #define PIDFD_SELF_PROCESS PIDFD_SELF_THREAD_GROUP
  40. /* Flags for pidfd_info. */
  41. /* Always returned, even if not requested */
  42. #define PIDFD_INFO_PID (1UL << 0)
  43. /* Always returned, even if not requested */
  44. #define PIDFD_INFO_CREDS (1UL << 1)
  45. /* Always returned if available, even if not requested */
  46. #define PIDFD_INFO_CGROUPID (1UL << 2)
  47. /* Only returned if requested. */
  48. #define PIDFD_INFO_EXIT (1UL << 3)
  49. /* Only returned if requested. */
  50. #define PIDFD_INFO_COREDUMP (1UL << 4)
  51. /* Value for coredump_mask in pidfd_info. Only valid if PIDFD_INFO_COREDUMP
  52. is set in mask. */
  53. /* Did crash and... */
  54. #define PIDFD_COREDUMPED (1U << 0)
  55. /* coredumping generation was skipped. */
  56. #define PIDFD_COREDUMP_SKIP (1U << 1)
  57. /* coredump was done as the user. */
  58. #define PIDFD_COREDUMP_USER (1U << 2)
  59. /* coredump was done as root. */
  60. #define PIDFD_COREDUMP_ROOT (1U << 3)
  61. struct pidfd_info
  62. {
  63. __uint64_t mask;
  64. __uint64_t cgroupid;
  65. __uint32_t pid;
  66. __uint32_t tgid;
  67. __uint32_t ppid;
  68. __uint32_t ruid;
  69. __uint32_t rgid;
  70. __uint32_t euid;
  71. __uint32_t egid;
  72. __uint32_t suid;
  73. __uint32_t sgid;
  74. __uint32_t fsuid;
  75. __uint32_t fsgid;
  76. __int32_t exit_code;
  77. __uint32_t coredump_mask;
  78. __uint32_t __spare1;
  79. };
  80. /* sizeof first published struct */
  81. #define PIDFD_INFO_SIZE_VER0 64
  82. #define PIDFD_GET_INFO _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info)
  83. /* Returns a file descriptor that refers to the process PID. The
  84. close-on-exec is set on the file descriptor. */
  85. extern int pidfd_open (__pid_t __pid, unsigned int __flags) __THROW;
  86. /* Duplicates an existing file descriptor TARGETFD in the process referred
  87. by the PIDFD file descriptor PIDFD.
  88. The FLAGS argument is reserved for future use, it must be specified
  89. as 0. */
  90. extern int pidfd_getfd (int __pidfd, int __targetfd,
  91. unsigned int __flags) __THROW;
  92. /* Sends the signal SIG to the target process referred by the PIDFD. If
  93. INFO points to a siginfo_t buffer, it will be populated. */
  94. extern int pidfd_send_signal (int __pidfd, int __sig, siginfo_t *__info,
  95. unsigned int __flags) __THROW;
  96. /* Query the process ID (PID) from process descriptor FD. Return the PID
  97. or -1 in case of an error. */
  98. extern pid_t pidfd_getpid (int __fd) __THROW;
  99. #endif /* _PIDFD_H */