aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/reg-reg-ring.c
blob: 2bce7300279e7f559132f155a53925ee927f9e84 (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 */
/*
 * Test io_uring_register with a registered ring (IORING_REGISTER_USE_REGISTERED_RING)
 *
 */
#include <stdio.h>

#include "helpers.h"

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

	if (argc > 1)
		return T_EXIT_SKIP;

	ret = io_uring_queue_init(8, &ring, 0);
	if (ret) {
		fprintf(stderr, "ring setup failed\n");
		return T_EXIT_FAIL;
	}

	if (!(ring.features & IORING_FEAT_REG_REG_RING)) {
		fprintf(stderr, "IORING_FEAT_REG_REG_RING not available in kernel\n");
		io_uring_queue_exit(&ring);
		return T_EXIT_SKIP;
	}

	ret = io_uring_close_ring_fd(&ring);
	if (ret != -EINVAL) {
		fprintf(stderr, "closing ring fd should EINVAL before register\n");
		goto err;
	}

	ret = io_uring_unregister_ring_fd(&ring);
	if (ret != -EINVAL) {
		fprintf(stderr, "unregistering not-registered ring fd should fail\n");
		goto err;
	}

	ret = io_uring_register_ring_fd(&ring);
	if (ret != 1) {
		fprintf(stderr, "registering ring fd failed\n");
		goto err;
	}

	ret = io_uring_register_ring_fd(&ring);
	if (ret != -EEXIST) {
		fprintf(stderr, "registering already-registered ring fd should fail\n");
		goto err;
	}

	/* Test a simple io_uring_register operation expected to work.
	 * io_uring_register_iowq_max_workers is arbitrary.
	 */
	values[0] = values[1] = 0;
	ret = io_uring_register_iowq_max_workers(&ring, values);
	if (ret || (values[0] == 0 && values[1] == 0)) {
		fprintf(stderr, "io_uring_register operation failed before closing ring fd\n");
		goto err;
	}

	ret = io_uring_close_ring_fd(&ring);
	if (ret != 1) {
		fprintf(stderr, "closing ring fd failed\n");
		goto err;
	}

	values[0] = values[1] = 0;
	ret = io_uring_register_iowq_max_workers(&ring, values);
	if (ret || (values[0] == 0 && values[1] == 0)) {
		fprintf(stderr, "io_uring_register operation failed after closing ring fd\n");
		goto err;
	}

	ret = io_uring_close_ring_fd(&ring);
	if (ret != -EBADF) {
		fprintf(stderr, "closing already-closed ring fd should fail\n");
		goto err;
	}

	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;

err:
	io_uring_queue_exit(&ring);
	return T_EXIT_FAIL;
}