xref: /linux/tools/testing/selftests/bpf/progs/test_send_signal_kern.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
3 #include <vmlinux.h>
4 #include <linux/version.h>
5 #include <bpf/bpf_helpers.h>
6 
7 struct task_struct *bpf_task_from_pid(int pid) __ksym;
8 void bpf_task_release(struct task_struct *p) __ksym;
9 int bpf_send_signal_task(struct task_struct *task, int sig, enum pid_type type, u64 value) __ksym;
10 
11 __u32 sig = 0, pid = 0, status = 0, signal_thread = 0, target_pid = 0;
12 
13 static __always_inline int bpf_send_signal_test(void *ctx)
14 {
15 	struct task_struct *target_task = NULL;
16 	int ret;
17 	u64 value;
18 
19 	if (status != 0 || pid == 0)
20 		return 0;
21 
22 	if ((bpf_get_current_pid_tgid() >> 32) == pid) {
23 		if (target_pid) {
24 			target_task = bpf_task_from_pid(target_pid);
25 			if (!target_task)
26 				return 0;
27 			value = 8;
28 		}
29 
30 		if (signal_thread) {
31 			if (target_pid)
32 				ret = bpf_send_signal_task(target_task, sig, PIDTYPE_PID, value);
33 			else
34 				ret = bpf_send_signal_thread(sig);
35 		} else {
36 			if (target_pid)
37 				ret = bpf_send_signal_task(target_task, sig, PIDTYPE_TGID, value);
38 			else
39 				ret = bpf_send_signal(sig);
40 		}
41 		if (ret == 0)
42 			status = 1;
43 	}
44 
45 	if (target_task)
46 		bpf_task_release(target_task);
47 
48 	return 0;
49 }
50 
51 SEC("tracepoint/syscalls/sys_enter_nanosleep")
52 int send_signal_tp(void *ctx)
53 {
54 	return bpf_send_signal_test(ctx);
55 }
56 
57 SEC("tracepoint/sched/sched_switch")
58 int send_signal_tp_sched(void *ctx)
59 {
60 	return bpf_send_signal_test(ctx);
61 }
62 
63 SEC("perf_event")
64 int send_signal_perf(void *ctx)
65 {
66 	return bpf_send_signal_test(ctx);
67 }
68 
69 char __license[] SEC("license") = "GPL";
70