summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_ringbuf_n.c
blob: 8669eb42dbe0391dffdcbb7cbce4f6e854f73a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2024 Andrea Righi <andrea.righi@canonical.com>

#include <linux/bpf.h>
#include <sched.h>
#include <unistd.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"

char _license[] SEC("license") = "GPL";

#define TASK_COMM_LEN 16

struct sample {
	int pid;
	long value;
	char comm[16];
};

struct {
	__uint(type, BPF_MAP_TYPE_RINGBUF);
} ringbuf SEC(".maps");

int pid = 0;
long value = 0;

SEC("fentry/" SYS_PREFIX "sys_getpgid")
int test_ringbuf_n(void *ctx)
{
	int cur_pid = bpf_get_current_pid_tgid() >> 32;
	struct sample *sample;

	if (cur_pid != pid)
		return 0;

	sample = bpf_ringbuf_reserve(&ringbuf, sizeof(*sample), 0);
	if (!sample)
		return 0;

	sample->pid = pid;
	sample->value = value;
	bpf_get_current_comm(sample->comm, sizeof(sample->comm));

	bpf_ringbuf_submit(sample, 0);

	return 0;
}