Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
16f0efc
Documentation
LICENSES
arch
block
certs
crypto
drivers
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
accounting
arch
bpf
build
cgroup
crypto
debugging
firewire
firmware
gpio
hv
iio
include
io_uring
kvm
laptop
leds
lib
memory-model
nfsd
objtool
pci
pcmcia
perf
power
scripts
spi
testing
fault-injection
ktest
nvdimm
radix-tree
scatterlist
selftests
android
bpf
gnu
include
map_tests
prog_tests
.gitignore
bpf_obj_id.c
bpf_verif_scale.c
flow_dissector.c
flow_dissector_load_bytes.c
get_stack_raw_tp.c
global_data.c
l4lb_all.c
map_lock.c
obj_name.c
pkt_access.c
pkt_md_access.c
prog_run_xattr.c
queue_stack_map.c
raw_tp_writable_reject_nbd_invalid.c
raw_tp_writable_test_run.c
reference_tracking.c
send_signal.c
signal_pending.c
skb_ctx.c
spinlock.c
stacktrace_build_id.c
stacktrace_build_id_nmi.c
stacktrace_map.c
stacktrace_map_raw_tp.c
task_fd_query_rawtp.c
task_fd_query_tp.c
tcp_estats.c
tp_attach_query.c
xdp.c
xdp_adjust_tail.c
xdp_noinline.c
progs
verifier
.gitignore
Makefile
bpf_endian.h
bpf_helpers.h
bpf_rand.h
bpf_rlimit.h
bpf_util.h
cgroup_helpers.c
cgroup_helpers.h
config
flow_dissector_load.c
flow_dissector_load.h
get_cgroup_id_user.c
netcnt_common.h
tcp_client.py
tcp_server.py
test_align.c
test_btf.c
test_btf.h
test_btf_dump.c
test_cgroup_storage.c
test_dev_cgroup.c
test_flow_dissector.c
test_flow_dissector.sh
test_hashmap.c
test_iptunnel_common.h
test_kmod.sh
test_libbpf.sh
test_libbpf_open.c
test_lirc_mode2.sh
test_lirc_mode2_user.c
test_lpm_map.c
test_lru_map.c
test_lwt_ip_encap.sh
test_lwt_seg6local.sh
test_maps.c
test_maps.h
test_netcnt.c
test_offload.py
test_progs.c
test_progs.h
test_queue_stack_map.h
test_section_names.c
test_select_reuseport.c
test_select_reuseport_common.h
test_skb_cgroup_id.sh
test_skb_cgroup_id_user.c
test_sock.c
test_sock_addr.c
test_sock_addr.sh
test_sock_fields.c
test_socket_cookie.c
test_sockmap.c
test_sockmap_kern.h
test_sysctl.c
test_tag.c
test_tc_edt.sh
test_tc_tunnel.sh
test_tcp_check_syncookie.sh
test_tcp_check_syncookie_user.c
test_tcpbpf.h
test_tcpbpf_user.c
test_tcpnotify.h
test_tcpnotify_user.c
test_tunnel.sh
test_verifier.c
test_verifier_log.c
test_xdp_meta.sh
test_xdp_redirect.sh
test_xdp_vlan.sh
trace_helpers.c
trace_helpers.h
urandom_read.c
with_addr.sh
with_tunnels.sh
breakpoints
capabilities
cgroup
cpu-hotplug
cpufreq
drivers
efivarfs
exec
filesystems
firmware
ftrace
futex
gpio
ia64
intel_pstate
ipc
ir
kcmp
kexec
kmod
kselftest
kvm
lib
livepatch
locking
media_tests
membarrier
memfd
memory-hotplug
mount
mqueue
net
netfilter
networking
nsfs
ntb
pidfd
powerpc
prctl
proc
pstore
ptp
ptrace
rcutorture
rseq
rtc
safesetid
seccomp
sigaltstack
size
sparc64
splice
static_keys
sync
sysctl
tc-testing
timers
tmpfs
tpm2
uevent
user
vDSO
vm
watchdog
x86
zram
.gitignore
Makefile
gen_kselftest_tar.sh
kselftest.h
kselftest_harness.h
kselftest_install.sh
kselftest_module.h
kselftest_module.sh
lib.mk
vsock
thermal
time
usb
virtio
vm
wmi
Makefile
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
bpf
/
prog_tests
/
send_signal.c
Blame
Blame
Latest commit
Yonghong Song
and
Daniel Borkmann
tools/bpf: add selftest in test_progs for bpf_send_signal() helper
May 24, 2019
16f0efc
·
May 24, 2019
History
History
198 lines (161 loc) · 4.84 KB
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
bpf
/
prog_tests
/
send_signal.c
Top
File metadata and controls
Code
Blame
198 lines (161 loc) · 4.84 KB
Raw
// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> static volatile int sigusr1_received = 0; static void sigusr1_handler(int signum) { sigusr1_received++; } static int test_send_signal_common(struct perf_event_attr *attr, int prog_type, const char *test_name) { int err = -1, pmu_fd, prog_fd, info_map_fd, status_map_fd; const char *file = "./test_send_signal_kern.o"; struct bpf_object *obj = NULL; int pipe_c2p[2], pipe_p2c[2]; __u32 key = 0, duration = 0; char buf[256]; pid_t pid; __u64 val; if (CHECK(pipe(pipe_c2p), test_name, "pipe pipe_c2p error: %s\n", strerror(errno))) goto no_fork_done; if (CHECK(pipe(pipe_p2c), test_name, "pipe pipe_p2c error: %s\n", strerror(errno))) { close(pipe_c2p[0]); close(pipe_c2p[1]); goto no_fork_done; } pid = fork(); if (CHECK(pid < 0, test_name, "fork error: %s\n", strerror(errno))) { close(pipe_c2p[0]); close(pipe_c2p[1]); close(pipe_p2c[0]); close(pipe_p2c[1]); goto no_fork_done; } if (pid == 0) { /* install signal handler and notify parent */ signal(SIGUSR1, sigusr1_handler); close(pipe_c2p[0]); /* close read */ close(pipe_p2c[1]); /* close write */ /* notify parent signal handler is installed */ write(pipe_c2p[1], buf, 1); /* make sure parent enabled bpf program to send_signal */ read(pipe_p2c[0], buf, 1); /* wait a little for signal handler */ sleep(1); if (sigusr1_received) write(pipe_c2p[1], "2", 1); else write(pipe_c2p[1], "0", 1); /* wait for parent notification and exit */ read(pipe_p2c[0], buf, 1); close(pipe_c2p[1]); close(pipe_p2c[0]); exit(0); } close(pipe_c2p[1]); /* close write */ close(pipe_p2c[0]); /* close read */ err = bpf_prog_load(file, prog_type, &obj, &prog_fd); if (CHECK(err < 0, test_name, "bpf_prog_load error: %s\n", strerror(errno))) goto prog_load_failure; pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1, -1 /* group id */, 0 /* flags */); if (CHECK(pmu_fd < 0, test_name, "perf_event_open error: %s\n", strerror(errno))) { err = -1; goto close_prog; } err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); if (CHECK(err < 0, test_name, "ioctl perf_event_ioc_enable error: %s\n", strerror(errno))) goto disable_pmu; err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); if (CHECK(err < 0, test_name, "ioctl perf_event_ioc_set_bpf error: %s\n", strerror(errno))) goto disable_pmu; err = -1; info_map_fd = bpf_object__find_map_fd_by_name(obj, "info_map"); if (CHECK(info_map_fd < 0, test_name, "find map %s error\n", "info_map")) goto disable_pmu; status_map_fd = bpf_object__find_map_fd_by_name(obj, "status_map"); if (CHECK(status_map_fd < 0, test_name, "find map %s error\n", "status_map")) goto disable_pmu; /* wait until child signal handler installed */ read(pipe_c2p[0], buf, 1); /* trigger the bpf send_signal */ key = 0; val = (((__u64)(SIGUSR1)) << 32) | pid; bpf_map_update_elem(info_map_fd, &key, &val, 0); /* notify child that bpf program can send_signal now */ write(pipe_p2c[1], buf, 1); /* wait for result */ err = read(pipe_c2p[0], buf, 1); if (CHECK(err < 0, test_name, "reading pipe error: %s\n", strerror(errno))) goto disable_pmu; if (CHECK(err == 0, test_name, "reading pipe error: size 0\n")) { err = -1; goto disable_pmu; } err = CHECK(buf[0] != '2', test_name, "incorrect result\n"); /* notify child safe to exit */ write(pipe_p2c[1], buf, 1); disable_pmu: close(pmu_fd); close_prog: bpf_object__close(obj); prog_load_failure: close(pipe_c2p[0]); close(pipe_p2c[1]); wait(NULL); no_fork_done: return err; } static int test_send_signal_tracepoint(void) { const char *id_path = "/sys/kernel/debug/tracing/events/syscalls/sys_enter_nanosleep/id"; struct perf_event_attr attr = { .type = PERF_TYPE_TRACEPOINT, .sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN, .sample_period = 1, .wakeup_events = 1, }; __u32 duration = 0; int bytes, efd; char buf[256]; efd = open(id_path, O_RDONLY, 0); if (CHECK(efd < 0, "tracepoint", "open syscalls/sys_enter_nanosleep/id failure: %s\n", strerror(errno))) return -1; bytes = read(efd, buf, sizeof(buf)); close(efd); if (CHECK(bytes <= 0 || bytes >= sizeof(buf), "tracepoint", "read syscalls/sys_enter_nanosleep/id failure: %s\n", strerror(errno))) return -1; attr.config = strtol(buf, NULL, 0); return test_send_signal_common(&attr, BPF_PROG_TYPE_TRACEPOINT, "tracepoint"); } static int test_send_signal_nmi(void) { struct perf_event_attr attr = { .sample_freq = 50, .freq = 1, .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES, }; return test_send_signal_common(&attr, BPF_PROG_TYPE_PERF_EVENT, "perf_event"); } void test_send_signal(void) { int ret = 0; ret |= test_send_signal_tracepoint(); ret |= test_send_signal_nmi(); if (!ret) printf("test_send_signal:OK\n"); else printf("test_send_signal:FAIL\n"); }
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
You can’t perform that action at this time.