Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2017 Facebook |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <uapi/linux/bpf.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 5 | #include <bpf/bpf_helpers.h> |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 6 | |
| 7 | struct syscalls_enter_open_args { |
| 8 | unsigned long long unused; |
| 9 | long syscall_nr; |
| 10 | long filename_ptr; |
| 11 | long flags; |
| 12 | long mode; |
| 13 | }; |
| 14 | |
| 15 | struct syscalls_exit_open_args { |
| 16 | unsigned long long unused; |
| 17 | long syscall_nr; |
| 18 | long ret; |
| 19 | }; |
| 20 | |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 21 | struct { |
| 22 | __uint(type, BPF_MAP_TYPE_ARRAY); |
| 23 | __type(key, u32); |
| 24 | __type(value, u32); |
| 25 | __uint(max_entries, 1); |
| 26 | } enter_open_map SEC(".maps"); |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 27 | |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 28 | struct { |
| 29 | __uint(type, BPF_MAP_TYPE_ARRAY); |
| 30 | __type(key, u32); |
| 31 | __type(value, u32); |
| 32 | __uint(max_entries, 1); |
| 33 | } exit_open_map SEC(".maps"); |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 34 | |
| 35 | static __always_inline void count(void *map) |
| 36 | { |
| 37 | u32 key = 0; |
| 38 | u32 *value, init_val = 1; |
| 39 | |
| 40 | value = bpf_map_lookup_elem(map, &key); |
| 41 | if (value) |
| 42 | *value += 1; |
| 43 | else |
| 44 | bpf_map_update_elem(map, &key, &init_val, BPF_NOEXIST); |
| 45 | } |
| 46 | |
| 47 | SEC("tracepoint/syscalls/sys_enter_open") |
| 48 | int trace_enter_open(struct syscalls_enter_open_args *ctx) |
| 49 | { |
Daniel T. Lee | fe33008 | 2019-12-05 17:01:14 +0900 | [diff] [blame] | 50 | count(&enter_open_map); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | SEC("tracepoint/syscalls/sys_enter_openat") |
| 55 | int trace_enter_open_at(struct syscalls_enter_open_args *ctx) |
| 56 | { |
| 57 | count(&enter_open_map); |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | SEC("tracepoint/syscalls/sys_exit_open") |
| 62 | int trace_enter_exit(struct syscalls_exit_open_args *ctx) |
| 63 | { |
Daniel T. Lee | fe33008 | 2019-12-05 17:01:14 +0900 | [diff] [blame] | 64 | count(&exit_open_map); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | SEC("tracepoint/syscalls/sys_exit_openat") |
| 69 | int trace_enter_exit_at(struct syscalls_exit_open_args *ctx) |
| 70 | { |
| 71 | count(&exit_open_map); |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 72 | return 0; |
| 73 | } |