xref: /linux/tools/testing/selftests/bpf/progs/test_send_signal_kern.c (revision 4201c9260a8d3c4ef238e51692a7e9b4e1e29efe)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
3 #include <linux/bpf.h>
4 #include <linux/version.h>
5 #include "bpf_helpers.h"
6 
7 struct bpf_map_def SEC("maps") info_map = {
8 	.type = BPF_MAP_TYPE_ARRAY,
9 	.key_size = sizeof(__u32),
10 	.value_size = sizeof(__u64),
11 	.max_entries = 1,
12 };
13 
14 BPF_ANNOTATE_KV_PAIR(info_map, __u32, __u64);
15 
16 struct bpf_map_def SEC("maps") status_map = {
17 	.type = BPF_MAP_TYPE_ARRAY,
18 	.key_size = sizeof(__u32),
19 	.value_size = sizeof(__u64),
20 	.max_entries = 1,
21 };
22 
23 BPF_ANNOTATE_KV_PAIR(status_map, __u32, __u64);
24 
25 SEC("send_signal_demo")
26 int bpf_send_signal_test(void *ctx)
27 {
28 	__u64 *info_val, *status_val;
29 	__u32 key = 0, pid, sig;
30 	int ret;
31 
32 	status_val = bpf_map_lookup_elem(&status_map, &key);
33 	if (!status_val || *status_val != 0)
34 		return 0;
35 
36 	info_val = bpf_map_lookup_elem(&info_map, &key);
37 	if (!info_val || *info_val == 0)
38 		return 0;
39 
40 	sig = *info_val >> 32;
41 	pid = *info_val & 0xffffFFFF;
42 
43 	if ((bpf_get_current_pid_tgid() >> 32) == pid) {
44 		ret = bpf_send_signal(sig);
45 		if (ret == 0)
46 			*status_val = 1;
47 	}
48 
49 	return 0;
50 }
51 char __license[] SEC("license") = "GPL";
52