1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: test io_uring poll handling
4 *
5 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <signal.h>
12 #include <poll.h>
13 #include <sys/wait.h>
14
15 #include "liburing.h"
16
sig_alrm(int sig)17 static void sig_alrm(int sig)
18 {
19 fprintf(stderr, "Timed out!\n");
20 exit(1);
21 }
22
main(int argc,char * argv[])23 int main(int argc, char *argv[])
24 {
25 struct io_uring_cqe *cqe;
26 struct io_uring_sqe *sqe;
27 struct io_uring ring;
28 int pipe1[2];
29 pid_t p;
30 int ret;
31
32 if (argc > 1)
33 return 0;
34
35 if (pipe(pipe1) != 0) {
36 perror("pipe");
37 return 1;
38 }
39
40 p = fork();
41 switch (p) {
42 case -1:
43 perror("fork");
44 exit(2);
45 case 0: {
46 struct sigaction act;
47
48 ret = io_uring_queue_init(1, &ring, 0);
49 if (ret) {
50 fprintf(stderr, "child: ring setup failed: %d\n", ret);
51 return 1;
52 }
53
54 memset(&act, 0, sizeof(act));
55 act.sa_handler = sig_alrm;
56 act.sa_flags = SA_RESTART;
57 sigaction(SIGALRM, &act, NULL);
58 alarm(1);
59
60 sqe = io_uring_get_sqe(&ring);
61 if (!sqe) {
62 fprintf(stderr, "get sqe failed\n");
63 return 1;
64 }
65
66 io_uring_prep_poll_add(sqe, pipe1[0], POLLIN);
67 io_uring_sqe_set_data(sqe, sqe);
68
69 ret = io_uring_submit(&ring);
70 if (ret <= 0) {
71 fprintf(stderr, "child: sqe submit failed: %d\n", ret);
72 return 1;
73 }
74
75 do {
76 ret = io_uring_wait_cqe(&ring, &cqe);
77 if (ret < 0) {
78 fprintf(stderr, "child: wait completion %d\n", ret);
79 break;
80 }
81 io_uring_cqe_seen(&ring, cqe);
82 } while (ret != 0);
83
84 if (ret < 0)
85 return 1;
86 if (cqe->user_data != (unsigned long) sqe) {
87 fprintf(stderr, "child: cqe doesn't match sqe\n");
88 return 1;
89 }
90 if ((cqe->res & POLLIN) != POLLIN) {
91 fprintf(stderr, "child: bad return value %ld\n",
92 (long) cqe->res);
93 return 1;
94 }
95 exit(0);
96 }
97 default:
98 do {
99 errno = 0;
100 ret = write(pipe1[1], "foo", 3);
101 } while (ret == -1 && errno == EINTR);
102
103 if (ret != 3) {
104 fprintf(stderr, "parent: bad write return %d\n", ret);
105 return 1;
106 }
107 return 0;
108 }
109 }
110