1
0

common-pidfds.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright (c) 2017-2025, Feral Interactive and the GameMode contributors
  3. Copyright (c) 2019, Red Hat
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Feral Interactive nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #define _GNU_SOURCE
  28. #include <build-config.h>
  29. #include "common-helpers.h"
  30. #include "common-pidfds.h"
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <stdbool.h>
  34. #include <stddef.h>
  35. #include <stdint.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/stat.h>
  40. #include <sys/types.h>
  41. #include <unistd.h>
  42. #if !HAVE_FN_PIDFD_OPEN
  43. #include <sys/syscall.h>
  44. #ifndef __NR_pidfd_open
  45. #define __NR_pidfd_open 434
  46. #endif
  47. static int pidfd_open(pid_t pid, unsigned int flags)
  48. {
  49. return (int)syscall(__NR_pidfd_open, pid, flags);
  50. }
  51. #else
  52. #include <sys/pidfd.h>
  53. #endif
  54. /* pidfd functions */
  55. int open_pidfds(pid_t *pids, int *fds, int count)
  56. {
  57. int i = 0;
  58. for (i = 0; i < count; i++) {
  59. int pid = pids[i];
  60. int fd = pidfd_open(pid, 0);
  61. if (fd < 0)
  62. break;
  63. fds[i] = fd;
  64. }
  65. return i;
  66. }
  67. static int parse_pid(const char *str, pid_t *pid)
  68. {
  69. unsigned long long int v;
  70. char *end;
  71. pid_t p;
  72. errno = 0;
  73. v = strtoull(str, &end, 0);
  74. if (end == str)
  75. return -ENOENT;
  76. else if (errno != 0)
  77. return -errno;
  78. p = (pid_t)v;
  79. if (p < 1 || (unsigned long long int)p != v)
  80. return -ERANGE;
  81. if (pid)
  82. *pid = p;
  83. return 0;
  84. }
  85. static int parse_status_field_pid(const char *val, pid_t *pid)
  86. {
  87. const char *t;
  88. t = strrchr(val, '\t');
  89. if (t == NULL)
  90. return -ENOENT;
  91. return parse_pid(t, pid);
  92. }
  93. static int pidfd_to_pid(int fdinfo, int pidfd, pid_t *pid)
  94. {
  95. autofree char *key = NULL;
  96. autofree char *val = NULL;
  97. char name[256] = {
  98. 0,
  99. };
  100. bool found = false;
  101. FILE *f = NULL;
  102. size_t keylen = 0;
  103. size_t vallen = 0;
  104. ssize_t n;
  105. int fd;
  106. int r = 0;
  107. *pid = 0;
  108. buffered_snprintf(name, "%d", pidfd);
  109. fd = openat(fdinfo, name, O_RDONLY | O_CLOEXEC | O_NOCTTY);
  110. if (fd != -1)
  111. f = fdopen(fd, "r");
  112. if (f == NULL)
  113. return -errno;
  114. do {
  115. n = getdelim(&key, &keylen, ':', f);
  116. if (n == -1) {
  117. r = errno;
  118. break;
  119. }
  120. n = getdelim(&val, &vallen, '\n', f);
  121. if (n == -1) {
  122. r = errno;
  123. break;
  124. }
  125. // TODO: strstrip (key);
  126. if (!strncmp(key, "Pid", 3)) {
  127. r = parse_status_field_pid(val, pid);
  128. found = r > -1;
  129. }
  130. } while (r == 0 && !found);
  131. fclose(f);
  132. if (r < 0)
  133. return r;
  134. else if (!found)
  135. return -ENOENT;
  136. return 0;
  137. }
  138. int pidfds_to_pids(int *fds, pid_t *pids, int count)
  139. {
  140. int fdinfo = -1;
  141. int r = 0;
  142. int i;
  143. fdinfo = open_fdinfo_dir();
  144. if (fdinfo == -1)
  145. return -1;
  146. for (i = 0; i < count && r == 0; i++)
  147. r = pidfd_to_pid(fdinfo, fds[i], &pids[i]);
  148. (void)close(fdinfo);
  149. if (r != 0)
  150. errno = -r;
  151. return i;
  152. }
  153. /* misc directory helpers */
  154. int open_fdinfo_dir(void)
  155. {
  156. return open("/proc/self/fdinfo", O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY);
  157. }