aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/fixed-buf-iter.c
blob: 1cdb327166fac848b6694e783f3734df2cc72cf3 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Test fixed buffers with non-iterators.
 *
 * Taken from: https://github.com/axboe/liburing/issues/549
 */
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>

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

#define BUF_SIZE    4096
#define BUFFERS     1
#define IN_FD       "/dev/urandom"
#define OUT_FD      "/dev/zero"

static int test(struct io_uring *ring)
{
	struct iovec iov[BUFFERS];
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret, fd_in, fd_out, i;

	fd_in = open(IN_FD, O_RDONLY, 0644);
	if (fd_in < 0) {
		perror("open in");
		return 1;
	}

	fd_out = open(OUT_FD, O_RDWR, 0644);
	if (fd_out < 0) {
		perror("open out");
		return 1;
	}

	for (i = 0; i < BUFFERS; i++) {
		iov[i].iov_base = malloc(BUF_SIZE);
		iov[i].iov_len = BUF_SIZE;
		memset(iov[i].iov_base, 0, BUF_SIZE);
	}

	ret = io_uring_register_buffers(ring, iov, BUFFERS);
	if (ret) {
		fprintf(stderr, "Error registering buffers: %s", strerror(-ret));
		return 1;
	}

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "Could not get SQE.\n");
		return 1;
	}

	io_uring_prep_read_fixed(sqe, fd_in, iov[0].iov_base, BUF_SIZE, 0, 0);
	io_uring_submit(ring);

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret < 0) {
		fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret));
		return 1;
	}

	if (cqe->res < 0) { 
		fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
		return 1;
	}
	io_uring_cqe_seen(ring, cqe);

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "Could not get SQE.\n");
		return 1;
	}
	io_uring_prep_write_fixed(sqe, fd_out, iov[0].iov_base, BUF_SIZE, 0, 0);
	io_uring_submit(ring);

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret < 0) {
		fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret));
		return 1;
	}
	if (cqe->res < 0) {
		fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
		return 1;
	}
	io_uring_cqe_seen(ring, cqe);
	return 0;
}

int main(int argc, char *argv[])
{
	struct io_uring ring;
	int ret;

	if (argc > 1)
		return T_EXIT_SKIP;

	ret = t_create_ring(8, &ring, 0);
	if (ret == T_SETUP_SKIP)
		return T_EXIT_SKIP;
	else if (ret < 0)
		return T_EXIT_FAIL;

	ret = test(&ring);
	if (ret) {
		fprintf(stderr, "Test failed\n");
		return T_EXIT_FAIL;
	}

	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
}