• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test multishot read on stdin. Not that this REQUIRES input
4  *		to be received on stdin, and hence if invoked with no
5  *		arguments, or without the single argument being 'stdin',
6  *		the test will just return SKIPPED. Can't be run from the
7  *		standard test harness, as it's interactive.
8  *
9  * To run, do run "test/read-mshot-stdin.t stdin" and then input text on
10  * the console, followed by enter / line feed. If it works as it should,
11  * it'll output the received CQE data. If an error is detected, it'll
12  * abort with an error.
13  */
14 #include <errno.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 
22 #include "liburing.h"
23 #include "helpers.h"
24 
25 #define BUF_SIZE	32
26 #define NR_BUFS		64
27 #define BUF_BGID	1
28 
29 #define BR_MASK		(NR_BUFS - 1)
30 
test_stdin(void)31 static int test_stdin(void)
32 {
33 	struct io_uring_buf_ring *br;
34 	struct io_uring_params p = { };
35 	struct io_uring_sqe *sqe;
36 	struct io_uring_cqe *cqe;
37 	struct io_uring ring;
38 	int ret, i, last_bid;
39 	char *buf, *ptr;
40 
41 	p.flags = IORING_SETUP_CQSIZE;
42 	p.cq_entries = NR_BUFS;
43 	ret = io_uring_queue_init_params(1, &ring, &p);
44 	if (ret) {
45 		if (ret == -EINVAL)
46 			return T_EXIT_SKIP;
47 		fprintf(stderr, "ring setup failed: %d\n", ret);
48 		return T_EXIT_FAIL;
49 	}
50 
51 	if (posix_memalign((void **) &buf, 4096, NR_BUFS * BUF_SIZE))
52 		return T_EXIT_FAIL;
53 
54 	br = io_uring_setup_buf_ring(&ring, NR_BUFS, BUF_BGID, 0, &ret);
55 	if (!br) {
56 		if (ret == -EINVAL)
57 			return T_EXIT_SKIP;
58 		fprintf(stderr, "Buffer ring register failed %d\n", ret);
59 		return T_EXIT_FAIL;
60 	}
61 
62 	ptr = buf;
63 	for (i = 0; i < NR_BUFS; i++) {
64 		io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1, BR_MASK, i);
65 		ptr += BUF_SIZE;
66 	}
67 	io_uring_buf_ring_advance(br, NR_BUFS);
68 
69 	sqe = io_uring_get_sqe(&ring);
70 	io_uring_prep_read_multishot(sqe, STDIN_FILENO, 0, 0, BUF_BGID);
71 
72 	ret = io_uring_submit(&ring);
73 	if (ret != 1) {
74 		fprintf(stderr, "submit: %d\n", ret);
75 		return T_EXIT_FAIL;
76 	}
77 
78 	last_bid = -1;
79 	do {
80 		int bid;
81 
82 		ret = io_uring_wait_cqe(&ring, &cqe);
83 		if (ret) {
84 			fprintf(stderr, "wait cqe failed %d\n", ret);
85 			return T_EXIT_FAIL;
86 		}
87 		if (cqe->res && !(cqe->flags & IORING_CQE_F_BUFFER)) {
88 			fprintf(stderr, "BUF flag not set %x\n", cqe->flags);
89 			return T_EXIT_FAIL;
90 		}
91 		bid = cqe->flags >> 16;
92 		printf("CQE res %d, bid %d, flags %x\n", cqe->res, bid, cqe->flags);
93 		if (cqe->res > 0 && last_bid != -1 && last_bid + 1 != bid) {
94 			fprintf(stderr, "Got bid %d, wanted %d\n", bid, last_bid + 1);
95 			return T_EXIT_FAIL;
96 		}
97 		if (!(cqe->flags & IORING_CQE_F_MORE)) {
98 			io_uring_cqe_seen(&ring, cqe);
99 			break;
100 		}
101 
102 		last_bid = bid;
103 		io_uring_cqe_seen(&ring, cqe);
104 	}while (1);
105 
106 	io_uring_free_buf_ring(&ring, br, NR_BUFS, BUF_BGID);
107 	io_uring_queue_exit(&ring);
108 	free(buf);
109 	return T_EXIT_PASS;
110 }
111 
main(int argc,char * argv[])112 int main(int argc, char *argv[])
113 {
114 	if (argc == 1)
115 		return T_EXIT_SKIP;
116 	else if (argc > 2)
117 		return T_EXIT_SKIP;
118 	if (!strcmp(argv[1], "stdin"))
119 		return test_stdin();
120 	return T_EXIT_SKIP;
121 }
122