1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 static volatile int sigusr1_received = 0;
6 static void sigusr1_handler(int signum)
11 static int test_send_signal_common(struct perf_event_attr *attr,
13 const char *test_name)
15 int err = -1, pmu_fd, prog_fd, info_map_fd, status_map_fd;
16 const char *file = "./test_send_signal_kern.o";
17 struct bpf_object *obj = NULL;
18 int pipe_c2p[2], pipe_p2c[2];
19 __u32 key = 0, duration = 0;
24 if (CHECK(pipe(pipe_c2p), test_name,
25 "pipe pipe_c2p error: %s\n", strerror(errno)))
28 if (CHECK(pipe(pipe_p2c), test_name,
29 "pipe pipe_p2c error: %s\n", strerror(errno))) {
36 if (CHECK(pid < 0, test_name, "fork error: %s\n", strerror(errno))) {
45 /* install signal handler and notify parent */
46 signal(SIGUSR1, sigusr1_handler);
48 close(pipe_c2p[0]); /* close read */
49 close(pipe_p2c[1]); /* close write */
51 /* notify parent signal handler is installed */
52 write(pipe_c2p[1], buf, 1);
54 /* make sure parent enabled bpf program to send_signal */
55 read(pipe_p2c[0], buf, 1);
57 /* wait a little for signal handler */
61 write(pipe_c2p[1], "2", 1);
63 write(pipe_c2p[1], "0", 1);
65 /* wait for parent notification and exit */
66 read(pipe_p2c[0], buf, 1);
73 close(pipe_c2p[1]); /* close write */
74 close(pipe_p2c[0]); /* close read */
76 err = bpf_prog_load(file, prog_type, &obj, &prog_fd);
77 if (CHECK(err < 0, test_name, "bpf_prog_load error: %s\n",
79 goto prog_load_failure;
81 pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1,
82 -1 /* group id */, 0 /* flags */);
83 if (CHECK(pmu_fd < 0, test_name, "perf_event_open error: %s\n",
89 err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
90 if (CHECK(err < 0, test_name, "ioctl perf_event_ioc_enable error: %s\n",
94 err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
95 if (CHECK(err < 0, test_name, "ioctl perf_event_ioc_set_bpf error: %s\n",
100 info_map_fd = bpf_object__find_map_fd_by_name(obj, "info_map");
101 if (CHECK(info_map_fd < 0, test_name, "find map %s error\n", "info_map"))
104 status_map_fd = bpf_object__find_map_fd_by_name(obj, "status_map");
105 if (CHECK(status_map_fd < 0, test_name, "find map %s error\n", "status_map"))
108 /* wait until child signal handler installed */
109 read(pipe_c2p[0], buf, 1);
111 /* trigger the bpf send_signal */
113 val = (((__u64)(SIGUSR1)) << 32) | pid;
114 bpf_map_update_elem(info_map_fd, &key, &val, 0);
116 /* notify child that bpf program can send_signal now */
117 write(pipe_p2c[1], buf, 1);
119 /* wait for result */
120 err = read(pipe_c2p[0], buf, 1);
121 if (CHECK(err < 0, test_name, "reading pipe error: %s\n", strerror(errno)))
123 if (CHECK(err == 0, test_name, "reading pipe error: size 0\n")) {
128 err = CHECK(buf[0] != '2', test_name, "incorrect result\n");
130 /* notify child safe to exit */
131 write(pipe_p2c[1], buf, 1);
136 bpf_object__close(obj);
145 static int test_send_signal_tracepoint(void)
147 const char *id_path = "/sys/kernel/debug/tracing/events/syscalls/sys_enter_nanosleep/id";
148 struct perf_event_attr attr = {
149 .type = PERF_TYPE_TRACEPOINT,
150 .sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN,
158 efd = open(id_path, O_RDONLY, 0);
159 if (CHECK(efd < 0, "tracepoint",
160 "open syscalls/sys_enter_nanosleep/id failure: %s\n",
164 bytes = read(efd, buf, sizeof(buf));
166 if (CHECK(bytes <= 0 || bytes >= sizeof(buf), "tracepoint",
167 "read syscalls/sys_enter_nanosleep/id failure: %s\n",
171 attr.config = strtol(buf, NULL, 0);
173 return test_send_signal_common(&attr, BPF_PROG_TYPE_TRACEPOINT, "tracepoint");
176 static int test_send_signal_nmi(void)
178 struct perf_event_attr attr = {
181 .type = PERF_TYPE_HARDWARE,
182 .config = PERF_COUNT_HW_CPU_CYCLES,
185 return test_send_signal_common(&attr, BPF_PROG_TYPE_PERF_EVENT, "perf_event");
188 void test_send_signal(void)
192 ret |= test_send_signal_tracepoint();
193 ret |= test_send_signal_nmi();
195 printf("test_send_signal:OK\n");
197 printf("test_send_signal:FAIL\n");