Linux Audio

Check our new training course

Loading...
v6.13.7
 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
 7struct task_struct *bpf_task_from_pid(int pid) __ksym;
 8void bpf_task_release(struct task_struct *p) __ksym;
 9int 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
13static __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
51SEC("tracepoint/syscalls/sys_enter_nanosleep")
52int send_signal_tp(void *ctx)
53{
54	return bpf_send_signal_test(ctx);
55}
56
57SEC("tracepoint/sched/sched_switch")
58int send_signal_tp_sched(void *ctx)
59{
60	return bpf_send_signal_test(ctx);
61}
62
63SEC("perf_event")
64int send_signal_perf(void *ctx)
65{
66	return bpf_send_signal_test(ctx);
67}
68
69char __license[] SEC("license") = "GPL";
v5.4
 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
 7struct {
 8	__uint(type, BPF_MAP_TYPE_ARRAY);
 9	__uint(max_entries, 1);
10	__type(key, __u32);
11	__type(value, __u64);
12} info_map SEC(".maps");
13
14struct {
15	__uint(type, BPF_MAP_TYPE_ARRAY);
16	__uint(max_entries, 1);
17	__type(key, __u32);
18	__type(value, __u64);
19} status_map SEC(".maps");
20
21SEC("send_signal_demo")
22int bpf_send_signal_test(void *ctx)
 
23{
24	__u64 *info_val, *status_val;
25	__u32 key = 0, pid, sig;
26	int ret;
 
27
28	status_val = bpf_map_lookup_elem(&status_map, &key);
29	if (!status_val || *status_val != 0)
30		return 0;
31
32	info_val = bpf_map_lookup_elem(&info_map, &key);
33	if (!info_val || *info_val == 0)
34		return 0;
35
36	sig = *info_val >> 32;
37	pid = *info_val & 0xffffFFFF;
38
39	if ((bpf_get_current_pid_tgid() >> 32) == pid) {
40		ret = bpf_send_signal(sig);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41		if (ret == 0)
42			*status_val = 1;
43	}
44
 
 
 
45	return 0;
46}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47char __license[] SEC("license") = "GPL";