xref: /aosp_15_r20/external/liburing/test/sigfd-deadlock.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test that sigfd reading/polling works. A regression test for
4  * the upstream commit:
5  *
6  * fd7d6de22414 ("io_uring: don't recurse on tsk->sighand->siglock with signalfd")
7  */
8 #include <unistd.h>
9 #include <sys/signalfd.h>
10 #include <sys/epoll.h>
11 #include <poll.h>
12 #include <stdio.h>
13 #include "liburing.h"
14 
setup_signal(void)15 static int setup_signal(void)
16 {
17 	sigset_t mask;
18 	int sfd;
19 
20 	sigemptyset(&mask);
21 	sigaddset(&mask, SIGINT);
22 
23 	sigprocmask(SIG_BLOCK, &mask, NULL);
24 	sfd = signalfd(-1, &mask, SFD_NONBLOCK);
25 	if (sfd < 0)
26 		perror("signalfd");
27 	return sfd;
28 }
29 
test_uring(int sfd)30 static int test_uring(int sfd)
31 {
32 	struct io_uring_sqe *sqe;
33 	struct io_uring_cqe *cqe;
34 	struct io_uring ring;
35 	int ret;
36 
37 	io_uring_queue_init(32, &ring, 0);
38 
39 	sqe = io_uring_get_sqe(&ring);
40 	io_uring_prep_poll_add(sqe, sfd, POLLIN);
41 	io_uring_submit(&ring);
42 
43 	kill(getpid(), SIGINT);
44 
45 	io_uring_wait_cqe(&ring, &cqe);
46 	if (cqe->res & POLLIN) {
47 		ret = 0;
48 	} else {
49 		fprintf(stderr, "Unexpected poll mask %x\n", cqe->res);
50 		ret = 1;
51 	}
52 	io_uring_cqe_seen(&ring, cqe);
53 	io_uring_queue_exit(&ring);
54 	return ret;
55 }
56 
main(int argc,char * argv[])57 int main(int argc, char *argv[])
58 {
59 	int sfd, ret;
60 
61 	if (argc > 1)
62 		return 0;
63 
64 	sfd = setup_signal();
65 	if (sfd < 0)
66 		return 1;
67 
68 	ret = test_uring(sfd);
69 	if (ret)
70 		fprintf(stderr, "test_uring signalfd failed\n");
71 
72 	close(sfd);
73 	return ret;
74 }
75