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