1 /*
2 * Test that closed pipe reads returns 0, instead of waiting for more
3 * data.
4 */
5 #include <errno.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <pthread.h>
10 #include <string.h>
11 #include "liburing.h"
12
13 #define BUFSIZE 512
14
15 struct data {
16 char *str;
17 int fds[2];
18 };
19
t(void * data)20 static void *t(void *data)
21 {
22 struct data *d = data;
23 int ret;
24
25 strcpy(d->str, "This is a test string");
26 ret = write(d->fds[1], d->str, strlen(d->str));
27 close(d->fds[1]);
28 if (ret < 0)
29 perror("write");
30
31 return NULL;
32 }
33
main(int argc,char * argv[])34 int main(int argc, char *argv[])
35 {
36 static char buf[BUFSIZE];
37 struct io_uring ring;
38 pthread_t thread;
39 struct data d;
40 int ret;
41
42 if (pipe(d.fds) < 0) {
43 perror("pipe");
44 return 1;
45 }
46 d.str = buf;
47
48 io_uring_queue_init(8, &ring, 0);
49
50 pthread_create(&thread, NULL, t, &d);
51
52 while (1) {
53 struct io_uring_sqe *sqe;
54 struct io_uring_cqe *cqe;
55
56 sqe = io_uring_get_sqe(&ring);
57 io_uring_prep_read(sqe, d.fds[0], buf, BUFSIZE, 0);
58 ret = io_uring_submit(&ring);
59 if (ret != 1) {
60 fprintf(stderr, "submit: %d\n", ret);
61 return 1;
62 }
63 ret = io_uring_wait_cqe(&ring, &cqe);
64 if (ret) {
65 fprintf(stderr, "wait: %d\n", ret);
66 return 1;
67 }
68
69 if (cqe->res < 0) {
70 fprintf(stderr, "Read error: %s\n", strerror(-cqe->res));
71 return 1;
72 }
73 if (cqe->res == 0)
74 break;
75 io_uring_cqe_seen(&ring, cqe);
76 }
77
78 pthread_join(thread, NULL);
79 io_uring_queue_exit(&ring);
80 return 0;
81 }
82