1 | /*
|
---|
2 | This is an example that uses io_uring with attach mode.
|
---|
3 | It demonstrates the what happens if the original ring is deleted before the other.
|
---|
4 | It uses liburing for simplicity.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <string.h>
|
---|
10 |
|
---|
11 | #include <errno.h>
|
---|
12 | #include <fcntl.h>
|
---|
13 | #include <unistd.h>
|
---|
14 |
|
---|
15 | #include <liburing.h>
|
---|
16 |
|
---|
17 |
|
---|
18 | char buffer1[1024];
|
---|
19 | char buffer2[1024];
|
---|
20 |
|
---|
21 | static void test_read(struct io_uring * ring, int fd) {
|
---|
22 | printf("Testing ring %d\n", ring->ring_fd);
|
---|
23 | struct io_uring_sqe * sqe = io_uring_get_sqe(ring);
|
---|
24 | io_uring_prep_read(sqe, fd, buffer2, 1024, 0);
|
---|
25 |
|
---|
26 | io_uring_submit(ring);
|
---|
27 |
|
---|
28 | struct io_uring_cqe *cqe;
|
---|
29 | int ret = io_uring_wait_cqe(ring, &cqe);
|
---|
30 | if( ret < 0) {
|
---|
31 | fprintf(stderr, "ioring wait failed: (%d) %s\n", -ret, strerror(-ret));
|
---|
32 | exit(EXIT_FAILURE);
|
---|
33 | }
|
---|
34 | io_uring_cqe_seen(ring, cqe);
|
---|
35 |
|
---|
36 | ret = memcmp(buffer1, buffer2, 1024);
|
---|
37 | if(ret != 0) {
|
---|
38 | fprintf(stderr, "files are different!\n");
|
---|
39 | exit(EXIT_FAILURE);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | int main() {
|
---|
44 | const char * path = __FILE__;
|
---|
45 |
|
---|
46 | int fd = open(path, 0, O_RDONLY);
|
---|
47 | if( fd < 0) {
|
---|
48 | fprintf(stderr, "Can't open file: (%d) %s\n", errno, strerror(errno));
|
---|
49 | return 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | int ret = read(fd, buffer1, 1024);
|
---|
53 | if( ret < 0 ) {
|
---|
54 | fprintf(stderr, "Can't read file: (%d) %s\n", errno, strerror(errno));
|
---|
55 | return 1;
|
---|
56 | }
|
---|
57 |
|
---|
58 | struct io_uring first_ring;
|
---|
59 | io_uring_queue_init(8, &first_ring, 0);
|
---|
60 | printf("Created first ring: %d\n", first_ring.ring_fd);
|
---|
61 |
|
---|
62 | test_read(&first_ring, fd);
|
---|
63 |
|
---|
64 |
|
---|
65 | struct io_uring second_ring;
|
---|
66 | struct io_uring_params p;
|
---|
67 | memset(&p, '\0', sizeof(struct io_uring_params));
|
---|
68 | p.flags |= IORING_SETUP_ATTACH_WQ;
|
---|
69 | p.wq_fd = first_ring.ring_fd;
|
---|
70 | io_uring_queue_init_params(8, &second_ring, &p);
|
---|
71 | printf("Attached second ring: %d\n", second_ring.ring_fd);
|
---|
72 |
|
---|
73 | test_read(&second_ring, fd);
|
---|
74 |
|
---|
75 | printf("Deleting first ring: %d\n", first_ring.ring_fd);
|
---|
76 | io_uring_queue_exit(&first_ring);
|
---|
77 |
|
---|
78 | printf("Sleeping (for good measure)\n");
|
---|
79 |
|
---|
80 | usleep( 1000000 );
|
---|
81 |
|
---|
82 | test_read(&second_ring, fd);
|
---|
83 |
|
---|
84 | struct io_uring third_ring;
|
---|
85 | memset(&p, '\0', sizeof(struct io_uring_params));
|
---|
86 | p.flags |= IORING_SETUP_ATTACH_WQ;
|
---|
87 | p.wq_fd = second_ring.ring_fd;
|
---|
88 | io_uring_queue_init_params(8, &third_ring, &p);
|
---|
89 | printf("Attached third ring: %d\n", third_ring.ring_fd);
|
---|
90 |
|
---|
91 | test_read(&third_ring, fd);
|
---|
92 |
|
---|
93 | printf("Deleting second ring: %d\n", second_ring.ring_fd);
|
---|
94 | io_uring_queue_exit(&second_ring);
|
---|
95 |
|
---|
96 | printf("Sleeping (for good measure)\n");
|
---|
97 |
|
---|
98 | usleep( 1000000 );
|
---|
99 |
|
---|
100 | io_uring_queue_exit(&third_ring);
|
---|
101 |
|
---|
102 | close(fd);
|
---|
103 |
|
---|
104 | return 0;
|
---|
105 |
|
---|
106 | }
|
---|