pidfd-metadata.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <err.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <inttypes.h>
  7. #include <limits.h>
  8. #include <sched.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <sys/syscall.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <unistd.h>
  18. #ifndef CLONE_PIDFD
  19. #define CLONE_PIDFD 0x00001000
  20. #endif
  21. #ifndef __NR_pidfd_send_signal
  22. #define __NR_pidfd_send_signal -1
  23. #endif
  24. static int do_child(void *args)
  25. {
  26. printf("%d\n", getpid());
  27. _exit(EXIT_SUCCESS);
  28. }
  29. static pid_t pidfd_clone(int flags, int *pidfd)
  30. {
  31. size_t stack_size = 1024;
  32. char *stack[1024] = { 0 };
  33. #ifdef __ia64__
  34. return __clone2(do_child, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
  35. #else
  36. return clone(do_child, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
  37. #endif
  38. }
  39. static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
  40. unsigned int flags)
  41. {
  42. return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
  43. }
  44. static int pidfd_metadata_fd(pid_t pid, int pidfd)
  45. {
  46. int procfd, ret;
  47. char path[100];
  48. snprintf(path, sizeof(path), "/proc/%d", pid);
  49. procfd = open(path, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
  50. if (procfd < 0) {
  51. warn("Failed to open %s\n", path);
  52. return -1;
  53. }
  54. /*
  55. * Verify that the pid has not been recycled and our /proc/<pid> handle
  56. * is still valid.
  57. */
  58. ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
  59. if (ret < 0) {
  60. switch (errno) {
  61. case EPERM:
  62. /* Process exists, just not allowed to signal it. */
  63. break;
  64. default:
  65. warn("Failed to signal process\n");
  66. close(procfd);
  67. procfd = -1;
  68. }
  69. }
  70. return procfd;
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. int pidfd = -1, ret = EXIT_FAILURE;
  75. char buf[4096] = { 0 };
  76. pid_t pid;
  77. int procfd, statusfd;
  78. ssize_t bytes;
  79. pid = pidfd_clone(CLONE_PIDFD, &pidfd);
  80. if (pid < 0)
  81. err(ret, "CLONE_PIDFD");
  82. if (pidfd == -1) {
  83. warnx("CLONE_PIDFD is not supported by the kernel");
  84. goto out;
  85. }
  86. procfd = pidfd_metadata_fd(pid, pidfd);
  87. close(pidfd);
  88. if (procfd < 0)
  89. goto out;
  90. statusfd = openat(procfd, "status", O_RDONLY | O_CLOEXEC);
  91. close(procfd);
  92. if (statusfd < 0)
  93. goto out;
  94. bytes = read(statusfd, buf, sizeof(buf));
  95. if (bytes > 0)
  96. bytes = write(STDOUT_FILENO, buf, bytes);
  97. close(statusfd);
  98. ret = EXIT_SUCCESS;
  99. out:
  100. (void)wait(NULL);
  101. exit(ret);
  102. }