get_cgroup_id_kern.c 918 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018 Facebook
  3. #include <linux/bpf.h>
  4. #include "bpf_helpers.h"
  5. struct bpf_map_def SEC("maps") cg_ids = {
  6. .type = BPF_MAP_TYPE_ARRAY,
  7. .key_size = sizeof(__u32),
  8. .value_size = sizeof(__u64),
  9. .max_entries = 1,
  10. };
  11. struct bpf_map_def SEC("maps") pidmap = {
  12. .type = BPF_MAP_TYPE_ARRAY,
  13. .key_size = sizeof(__u32),
  14. .value_size = sizeof(__u32),
  15. .max_entries = 1,
  16. };
  17. SEC("tracepoint/syscalls/sys_enter_nanosleep")
  18. int trace(void *ctx)
  19. {
  20. __u32 pid = bpf_get_current_pid_tgid();
  21. __u32 key = 0, *expected_pid;
  22. __u64 *val;
  23. expected_pid = bpf_map_lookup_elem(&pidmap, &key);
  24. if (!expected_pid || *expected_pid != pid)
  25. return 0;
  26. val = bpf_map_lookup_elem(&cg_ids, &key);
  27. if (val)
  28. *val = bpf_get_current_cgroup_id();
  29. return 0;
  30. }
  31. char _license[] SEC("license") = "GPL";
  32. __u32 _version SEC("version") = 1; /* ignored by tracepoints, required by libbpf.a */