1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
sigalrm_handler(int s)4 static void sigalrm_handler(int s) {}
5 static struct sigaction sigalrm_action = {
6 	.sa_handler = sigalrm_handler,
7 };
8 
test_signal_pending_by_type(enum bpf_prog_type prog_type)9 static void test_signal_pending_by_type(enum bpf_prog_type prog_type)
10 {
11 	struct bpf_insn prog[4096];
12 	struct itimerval timeo = {
13 		.it_value.tv_usec = 100000, /* 100ms */
14 	};
15 	__u32 duration = 0, retval;
16 	int prog_fd;
17 	int err;
18 	int i;
19 
20 	for (i = 0; i < ARRAY_SIZE(prog); i++)
21 		prog[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0);
22 	prog[ARRAY_SIZE(prog) - 1] = BPF_EXIT_INSN();
23 
24 	prog_fd = bpf_load_program(prog_type, prog, ARRAY_SIZE(prog),
25 				   "GPL", 0, NULL, 0);
26 	CHECK(prog_fd < 0, "test-run", "errno %d\n", errno);
27 
28 	err = sigaction(SIGALRM, &sigalrm_action, NULL);
29 	CHECK(err, "test-run-signal-sigaction", "errno %d\n", errno);
30 
31 	err = setitimer(ITIMER_REAL, &timeo, NULL);
32 	CHECK(err, "test-run-signal-timer", "errno %d\n", errno);
33 
34 	err = bpf_prog_test_run(prog_fd, 0xffffffff, &pkt_v4, sizeof(pkt_v4),
35 				NULL, NULL, &retval, &duration);
36 	CHECK(duration > 500000000, /* 500ms */
37 	      "test-run-signal-duration",
38 	      "duration %dns > 500ms\n",
39 	      duration);
40 
41 	signal(SIGALRM, SIG_DFL);
42 }
43 
test_signal_pending(enum bpf_prog_type prog_type)44 void test_signal_pending(enum bpf_prog_type prog_type)
45 {
46 	test_signal_pending_by_type(BPF_PROG_TYPE_SOCKET_FILTER);
47 	test_signal_pending_by_type(BPF_PROG_TYPE_FLOW_DISSECTOR);
48 }
49