aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/fixed-link.c
blob: d37788af03852c1ac8239cdc4cc68728a01b4b64 (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
#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 "helpers.h"
#include "liburing.h"

#define IOVECS_LEN 2

int main(int argc, char *argv[])
{
	struct iovec iovecs[IOVECS_LEN];
	struct io_uring ring;
	int i, fd, ret;

	if (argc > 1)
		return T_EXIT_SKIP;

	fd = open("/dev/zero", O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "Failed to open /dev/zero\n");
		return T_EXIT_FAIL;
	}

	if (io_uring_queue_init(32, &ring, 0) < 0) {
		fprintf(stderr, "Failed to init io_uring\n");
		close(fd);
		return T_EXIT_FAIL;
	}

	for (i = 0; i < IOVECS_LEN; ++i) {
		iovecs[i].iov_base = t_malloc(64);
		iovecs[i].iov_len = 64;
	}

	ret = io_uring_register_buffers(&ring, iovecs, IOVECS_LEN);
	if (ret) {
		fprintf(stderr, "Failed to register buffers\n");
		return T_EXIT_FAIL;
	}

	for (i = 0; i < IOVECS_LEN; ++i) {
		struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
		const char *str = "#include <errno.h>";

		iovecs[i].iov_len = strlen(str);
		io_uring_prep_read_fixed(sqe, fd, iovecs[i].iov_base, strlen(str), 0, i);
		if (i == 0)
			io_uring_sqe_set_flags(sqe, IOSQE_IO_LINK);
		io_uring_sqe_set_data(sqe, (void *)str);
	}

	ret = io_uring_submit_and_wait(&ring, IOVECS_LEN);
	if (ret < 0) {
		fprintf(stderr, "Failed to submit IO\n");
		return T_EXIT_FAIL;
	} else if (ret < 2) {
		fprintf(stderr, "Submitted %d, wanted %d\n", ret, IOVECS_LEN);
		return T_EXIT_FAIL;
	}

	for (i = 0; i < IOVECS_LEN; i++) {
		struct io_uring_cqe *cqe;

		ret = io_uring_wait_cqe(&ring, &cqe);
		if (ret) {
			fprintf(stderr, "wait_cqe=%d\n", ret);
			return T_EXIT_FAIL;
		}
		if (cqe->res != iovecs[i].iov_len) {
			fprintf(stderr, "read: wanted %ld, got %d\n",
					(long) iovecs[i].iov_len, cqe->res);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(&ring, cqe);
	}

	close(fd);
	io_uring_queue_exit(&ring);

	for (i = 0; i < IOVECS_LEN; ++i)
		free(iovecs[i].iov_base);

	return T_EXIT_PASS;
}