test_current_task_under_cgroup_kern.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <linux/ptrace.h>
  8. #include <uapi/linux/bpf.h>
  9. #include <linux/version.h>
  10. #include <bpf/bpf_helpers.h>
  11. #include <uapi/linux/utsname.h>
  12. #include "trace_common.h"
  13. struct {
  14. __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
  15. __uint(key_size, sizeof(u32));
  16. __uint(value_size, sizeof(u32));
  17. __uint(max_entries, 1);
  18. } cgroup_map SEC(".maps");
  19. struct {
  20. __uint(type, BPF_MAP_TYPE_ARRAY);
  21. __type(key, u32);
  22. __type(value, u64);
  23. __uint(max_entries, 1);
  24. } perf_map SEC(".maps");
  25. /* Writes the last PID that called sync to a map at index 0 */
  26. SEC("kprobe/" SYSCALL(sys_sync))
  27. int bpf_prog1(struct pt_regs *ctx)
  28. {
  29. u64 pid = bpf_get_current_pid_tgid();
  30. int idx = 0;
  31. if (!bpf_current_task_under_cgroup(&cgroup_map, 0))
  32. return 0;
  33. bpf_map_update_elem(&perf_map, &idx, &pid, BPF_ANY);
  34. return 0;
  35. }
  36. char _license[] SEC("license") = "GPL";
  37. u32 _version SEC("version") = LINUX_VERSION_CODE;