aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/short-read.c
blob: e9b4b20827b282e4a85a881a914f8286ad5e806d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <poll.h>


#include "helpers.h"
#include "liburing.h"

#define BUF_SIZE 4096
#define FILE_SIZE 1024

int main(int argc, char *argv[])
{
	int ret, fd, save_errno;
	struct io_uring ring;
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	struct iovec vec;

	if (argc > 1)
		return 0;

	vec.iov_base = t_malloc(BUF_SIZE);
	vec.iov_len = BUF_SIZE;

	t_create_file(".short-read", FILE_SIZE);

	fd = open(".short-read", O_RDONLY);
	save_errno = errno;
	unlink(".short-read");
	errno = save_errno;
	if (fd < 0) {
		perror("file open");
		return 1;
	}

	ret = io_uring_queue_init(32, &ring, 0);
	if (ret) {
		fprintf(stderr, "queue init failed: %d\n", ret);
		return ret;
	}

	sqe = io_uring_get_sqe(&ring);
	if (!sqe) {
		fprintf(stderr, "sqe get failed\n");
		return 1;
	}
	io_uring_prep_readv(sqe, fd, &vec, 1, 0);

	ret = io_uring_submit(&ring);
	if (ret != 1) {
		fprintf(stderr, "submit failed: %d\n", ret);
 		return 1;
	}

	ret = io_uring_wait_cqes(&ring, &cqe, 1, 0, 0);
	if (ret) {
		fprintf(stderr, "wait_cqe failed: %d\n", ret);
		return 1;
	}

	if (cqe->res != FILE_SIZE) {
		fprintf(stderr, "Read failed: %d\n", cqe->res);
		return 1;
	}

	io_uring_cqe_seen(&ring, cqe);
	free(vec.iov_base);
	return 0;
}