• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: Test O_NONBLOCK reading from fifo, should result in proper
4  *		retry and a positive read results. Buggy result would be
5  *		-EAGAIN being returned to the user.
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 
11 #include "liburing.h"
12 #include "helpers.h"
13 
main(int argc,char * argv[])14 int main(int argc, char *argv[])
15 {
16 	struct io_uring_sqe *sqe;
17 	struct io_uring_cqe *cqe;
18 	struct io_uring ring;
19 	char buf[32];
20 	int fds[2];
21 	int flags;
22 	int ret;
23 
24 	io_uring_queue_init(1, &ring, 0);
25 
26 	if (pipe(fds) < 0) {
27 		perror("pipe");
28 		return T_EXIT_FAIL;
29 	}
30 
31 	flags = fcntl(fds[0], F_GETFL, 0);
32 	if (flags < 0) {
33 		perror("fcntl get");
34 		return T_EXIT_FAIL;
35 	}
36 	flags |= O_NONBLOCK;
37 	ret = fcntl(fds[0], F_SETFL, flags);
38 	if (ret < 0) {
39 		perror("fcntl set");
40 		return T_EXIT_FAIL;
41 	}
42 
43 	sqe = io_uring_get_sqe(&ring);
44 	io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);
45 	io_uring_submit(&ring);
46 
47 	usleep(10000);
48 
49 	ret = write(fds[1], "Hello\n", 6);
50 	if (ret < 0) {
51 		perror("pipe write");
52 		return T_EXIT_FAIL;
53 	}
54 
55 	ret = io_uring_wait_cqe(&ring, &cqe);
56 	if (ret < 0) {
57 		fprintf(stderr, "wait=%d\n", ret);
58 		return T_EXIT_FAIL;
59 	}
60 
61 	if (cqe->res < 0) {
62 		fprintf(stderr, "cqe res %d\n", cqe->res);
63 		return T_EXIT_FAIL;
64 	}
65 
66 	io_uring_cqe_seen(&ring, cqe);
67 	io_uring_queue_exit(&ring);
68 	return T_EXIT_PASS;
69 }
70