Files
linux/tools/testing/selftests/bpf/progs/test_send_signal_kern.c
Puranjay Mohan 0e14189459 selftests/bpf: Augment send_signal test with remote signaling
Add testcases to test bpf_send_signal_task(). In these new test cases,
the main process triggers the BPF program and the forked process
receives the signals. The target process's signal handler receives a
cookie from the bpf program.

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20241016084136.10305-3-puranjay@kernel.org
2024-10-21 15:02:49 -07:00

70 lines
1.5 KiB
C

// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2019 Facebook
#include <vmlinux.h>
#include <linux/version.h>
#include <bpf/bpf_helpers.h>
struct task_struct *bpf_task_from_pid(int pid) __ksym;
void bpf_task_release(struct task_struct *p) __ksym;
int bpf_send_signal_task(struct task_struct *task, int sig, enum pid_type type, u64 value) __ksym;
__u32 sig = 0, pid = 0, status = 0, signal_thread = 0, target_pid = 0;
static __always_inline int bpf_send_signal_test(void *ctx)
{
struct task_struct *target_task = NULL;
int ret;
u64 value;
if (status != 0 || pid == 0)
return 0;
if ((bpf_get_current_pid_tgid() >> 32) == pid) {
if (target_pid) {
target_task = bpf_task_from_pid(target_pid);
if (!target_task)
return 0;
value = 8;
}
if (signal_thread) {
if (target_pid)
ret = bpf_send_signal_task(target_task, sig, PIDTYPE_PID, value);
else
ret = bpf_send_signal_thread(sig);
} else {
if (target_pid)
ret = bpf_send_signal_task(target_task, sig, PIDTYPE_TGID, value);
else
ret = bpf_send_signal(sig);
}
if (ret == 0)
status = 1;
}
if (target_task)
bpf_task_release(target_task);
return 0;
}
SEC("tracepoint/syscalls/sys_enter_nanosleep")
int send_signal_tp(void *ctx)
{
return bpf_send_signal_test(ctx);
}
SEC("tracepoint/sched/sched_switch")
int send_signal_tp_sched(void *ctx)
{
return bpf_send_signal_test(ctx);
}
SEC("perf_event")
int send_signal_perf(void *ctx)
{
return bpf_send_signal_test(ctx);
}
char __license[] SEC("license") = "GPL";