xref: /linux/tools/testing/selftests/bpf/progs/test_ringbuf_n.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2024 Andrea Righi <andrea.righi@canonical.com>
3 
4 #include <linux/bpf.h>
5 #include <sched.h>
6 #include <unistd.h>
7 #include <bpf/bpf_helpers.h>
8 #include "bpf_misc.h"
9 
10 char _license[] SEC("license") = "GPL";
11 
12 #define TASK_COMM_LEN 16
13 
14 struct sample {
15 	int pid;
16 	long value;
17 	char comm[16];
18 };
19 
20 struct {
21 	__uint(type, BPF_MAP_TYPE_RINGBUF);
22 } ringbuf SEC(".maps");
23 
24 int pid = 0;
25 long value = 0;
26 
27 SEC("fentry/" SYS_PREFIX "sys_getpgid")
28 int test_ringbuf_n(void *ctx)
29 {
30 	int cur_pid = bpf_get_current_pid_tgid() >> 32;
31 	struct sample *sample;
32 
33 	if (cur_pid != pid)
34 		return 0;
35 
36 	sample = bpf_ringbuf_reserve(&ringbuf, sizeof(*sample), 0);
37 	if (!sample)
38 		return 0;
39 
40 	sample->pid = pid;
41 	sample->value = value;
42 	bpf_get_current_comm(sample->comm, sizeof(sample->comm));
43 
44 	bpf_ringbuf_submit(sample, 0);
45 
46 	return 0;
47 }
48