pids.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (C) 2020 Facebook */
  3. #include <errno.h>
  4. #include <linux/err.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <bpf/bpf.h>
  11. #include <bpf/hashmap.h>
  12. #include "main.h"
  13. #include "skeleton/pid_iter.h"
  14. #ifdef BPFTOOL_WITHOUT_SKELETONS
  15. int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
  16. {
  17. return -ENOTSUP;
  18. }
  19. void delete_obj_refs_table(struct hashmap *map) {}
  20. void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix) {}
  21. void emit_obj_refs_json(struct hashmap *map, __u32 id, json_writer_t *json_writer) {}
  22. #else /* BPFTOOL_WITHOUT_SKELETONS */
  23. #include "pid_iter.skel.h"
  24. static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
  25. {
  26. struct hashmap_entry *entry;
  27. struct obj_refs *refs;
  28. struct obj_ref *ref;
  29. int err, i;
  30. void *tmp;
  31. hashmap__for_each_key_entry(map, entry, e->id) {
  32. refs = entry->pvalue;
  33. for (i = 0; i < refs->ref_cnt; i++) {
  34. if (refs->refs[i].pid == e->pid)
  35. return;
  36. }
  37. tmp = realloc(refs->refs, (refs->ref_cnt + 1) * sizeof(*ref));
  38. if (!tmp) {
  39. p_err("failed to re-alloc memory for ID %u, PID %d, COMM %s...",
  40. e->id, e->pid, e->comm);
  41. return;
  42. }
  43. refs->refs = tmp;
  44. ref = &refs->refs[refs->ref_cnt];
  45. ref->pid = e->pid;
  46. memcpy(ref->comm, e->comm, sizeof(ref->comm));
  47. ref->comm[sizeof(ref->comm) - 1] = '\0';
  48. refs->ref_cnt++;
  49. return;
  50. }
  51. /* new ref */
  52. refs = calloc(1, sizeof(*refs));
  53. if (!refs) {
  54. p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
  55. e->id, e->pid, e->comm);
  56. return;
  57. }
  58. refs->refs = malloc(sizeof(*refs->refs));
  59. if (!refs->refs) {
  60. free(refs);
  61. p_err("failed to alloc memory for ID %u, PID %d, COMM %s...",
  62. e->id, e->pid, e->comm);
  63. return;
  64. }
  65. ref = &refs->refs[0];
  66. ref->pid = e->pid;
  67. memcpy(ref->comm, e->comm, sizeof(ref->comm));
  68. ref->comm[sizeof(ref->comm) - 1] = '\0';
  69. refs->ref_cnt = 1;
  70. refs->has_bpf_cookie = e->has_bpf_cookie;
  71. refs->bpf_cookie = e->bpf_cookie;
  72. err = hashmap__append(map, e->id, refs);
  73. if (err)
  74. p_err("failed to append entry to hashmap for ID %u: %s",
  75. e->id, strerror(errno));
  76. }
  77. static int __printf(2, 0)
  78. libbpf_print_none(__maybe_unused enum libbpf_print_level level,
  79. __maybe_unused const char *format,
  80. __maybe_unused va_list args)
  81. {
  82. return 0;
  83. }
  84. int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
  85. {
  86. struct pid_iter_entry *e;
  87. char buf[4096 / sizeof(*e) * sizeof(*e)];
  88. struct pid_iter_bpf *skel;
  89. int err, ret, fd = -1, i;
  90. *map = hashmap__new(hash_fn_for_key_as_id, equal_fn_for_key_as_id, NULL);
  91. if (IS_ERR(*map)) {
  92. p_err("failed to create hashmap for PID references");
  93. return -1;
  94. }
  95. set_max_rlimit();
  96. skel = pid_iter_bpf__open();
  97. if (!skel) {
  98. p_err("failed to open PID iterator skeleton");
  99. return -1;
  100. }
  101. skel->rodata->obj_type = type;
  102. if (!verifier_logs) {
  103. libbpf_print_fn_t default_print;
  104. /* Unless debug information is on, we don't want the output to
  105. * be polluted with libbpf errors if bpf_iter is not supported.
  106. */
  107. default_print = libbpf_set_print(libbpf_print_none);
  108. err = pid_iter_bpf__load(skel);
  109. libbpf_set_print(default_print);
  110. } else {
  111. err = pid_iter_bpf__load(skel);
  112. }
  113. if (err) {
  114. /* too bad, kernel doesn't support BPF iterators yet */
  115. err = 0;
  116. goto out;
  117. }
  118. err = pid_iter_bpf__attach(skel);
  119. if (err) {
  120. /* if we loaded above successfully, attach has to succeed */
  121. p_err("failed to attach PID iterator: %d", err);
  122. goto out;
  123. }
  124. fd = bpf_iter_create(bpf_link__fd(skel->links.iter));
  125. if (fd < 0) {
  126. err = -errno;
  127. p_err("failed to create PID iterator session: %d", err);
  128. goto out;
  129. }
  130. while (true) {
  131. ret = read(fd, buf, sizeof(buf));
  132. if (ret < 0) {
  133. if (errno == EAGAIN)
  134. continue;
  135. err = -errno;
  136. p_err("failed to read PID iterator output: %d", err);
  137. goto out;
  138. }
  139. if (ret == 0)
  140. break;
  141. if (ret % sizeof(*e)) {
  142. err = -EINVAL;
  143. p_err("invalid PID iterator output format");
  144. goto out;
  145. }
  146. ret /= sizeof(*e);
  147. e = (void *)buf;
  148. for (i = 0; i < ret; i++, e++) {
  149. add_ref(*map, e);
  150. }
  151. }
  152. err = 0;
  153. out:
  154. if (fd >= 0)
  155. close(fd);
  156. pid_iter_bpf__destroy(skel);
  157. return err;
  158. }
  159. void delete_obj_refs_table(struct hashmap *map)
  160. {
  161. struct hashmap_entry *entry;
  162. size_t bkt;
  163. if (!map)
  164. return;
  165. hashmap__for_each_entry(map, entry, bkt) {
  166. struct obj_refs *refs = entry->pvalue;
  167. free(refs->refs);
  168. free(refs);
  169. }
  170. hashmap__free(map);
  171. }
  172. void emit_obj_refs_json(struct hashmap *map, __u32 id,
  173. json_writer_t *json_writer)
  174. {
  175. struct hashmap_entry *entry;
  176. if (hashmap__empty(map))
  177. return;
  178. hashmap__for_each_key_entry(map, entry, id) {
  179. struct obj_refs *refs = entry->pvalue;
  180. int i;
  181. if (refs->ref_cnt == 0)
  182. break;
  183. if (refs->has_bpf_cookie)
  184. jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);
  185. jsonw_name(json_writer, "pids");
  186. jsonw_start_array(json_writer);
  187. for (i = 0; i < refs->ref_cnt; i++) {
  188. struct obj_ref *ref = &refs->refs[i];
  189. jsonw_start_object(json_writer);
  190. jsonw_int_field(json_writer, "pid", ref->pid);
  191. jsonw_string_field(json_writer, "comm", ref->comm);
  192. jsonw_end_object(json_writer);
  193. }
  194. jsonw_end_array(json_writer);
  195. break;
  196. }
  197. }
  198. void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
  199. {
  200. struct hashmap_entry *entry;
  201. if (hashmap__empty(map))
  202. return;
  203. hashmap__for_each_key_entry(map, entry, id) {
  204. struct obj_refs *refs = entry->pvalue;
  205. int i;
  206. if (refs->ref_cnt == 0)
  207. break;
  208. if (refs->has_bpf_cookie)
  209. printf("\n\tbpf_cookie %llu", (unsigned long long) refs->bpf_cookie);
  210. printf("%s", prefix);
  211. for (i = 0; i < refs->ref_cnt; i++) {
  212. struct obj_ref *ref = &refs->refs[i];
  213. printf("%s%s(%d)", i == 0 ? "" : ", ", ref->comm, ref->pid);
  214. }
  215. break;
  216. }
  217. }
  218. #endif