Skip to content

Commit 92795fe

Browse files
committed
Add ex4.bpf.c
1 parent 8428516 commit 92795fe

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

examples/c-form/ex4.bpf.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <linux/bpf.h>
2+
#include <bpf/bpf_helpers.h>
3+
4+
#define u64 unsigned long long
5+
6+
// Define the map
7+
struct {
8+
__uint(type, BPF_MAP_TYPE_HASH);
9+
__type(key, u64);
10+
__type(value, u64);
11+
__uint(max_entries, 1);
12+
} last SEC(".maps");
13+
14+
// Handler for syscall entry
15+
SEC("tracepoint/syscalls/sys_enter_execve")
16+
int hello(void *ctx) {
17+
bpf_printk("entered");
18+
bpf_printk("multi constant support");
19+
return 0;
20+
}
21+
22+
// Handler for syscall exit
23+
SEC("tracepoint/syscalls/sys_exit_execve")
24+
long hello_again(void *ctx) {
25+
bpf_printk("exited");
26+
27+
// Create a key for map lookup
28+
u64 key = 0;
29+
30+
// Simple lookup without conditionals
31+
u64 *tsp = bpf_map_lookup_elem(&last, &key);
32+
if (tsp != NULL) {
33+
u64 delta = bpf_ktime_get_ns() - *tsp;
34+
if (delta < 1000000000) {
35+
// output if time is less than 1 second
36+
bpf_trace_printk("%d\\n", delta / 1000000);
37+
}
38+
bpf_map_delete_elem(&last, &key);
39+
}
40+
// Get current timestamp
41+
u64 ts = bpf_ktime_get_ns();
42+
bpf_map_update_elem(&last, &key, &ts, BPF_ANY);
43+
return 0;
44+
}
45+
46+
char LICENSE[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)