aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/ooo-file-unreg.c
blob: ee5faaf4f8f9c30cb1d55a7ffa2955cc2d483be1 (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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: Test that out-of-order file updates with inflight requests
 *		work as expected.
 *
 */
#include <stdio.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/poll.h>

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

int main(int argc, char *argv[])
{
	struct io_uring_sqe *sqe;
	int res, fds[2], sockid;
	struct io_uring ring;

	if (argc > 1)
		return T_EXIT_SKIP;

	res = io_uring_queue_init(1, &ring, 0);
	if (res) {
		fprintf(stderr, "queue_init: %d\n", res);
		return T_EXIT_FAIL;
	}

	res = io_uring_register_files_sparse(&ring, 2);
	if (res) {
		if (res == -EINVAL)
			return T_EXIT_SKIP;
		fprintf(stderr, "sparse reg: %d\n", res);
		return T_EXIT_FAIL;
	}

	fds[0] = socket(AF_INET, SOCK_DGRAM, 0);
	if (fds[0] < 0) {
		perror("socket");
		return T_EXIT_FAIL;
	}
	fds[1] = socket(AF_INET, SOCK_DGRAM, 0);
	if (fds[1] < 0) {
		perror("socket");
		return T_EXIT_FAIL;
	}

	res = io_uring_register_files_update(&ring, 0, fds, 2);
	if (res != 2) {
		fprintf(stderr, "files updates; %d\n", res);
		return T_EXIT_FAIL;
	}

	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_poll_add(sqe, 0, POLLIN);
	sqe->flags = IOSQE_FIXED_FILE;
	io_uring_submit(&ring);

	close(fds[0]);
	close(fds[1]);

	sockid = -1;
	res = io_uring_register_files_update(&ring, 1, &sockid, 1);
	if (res != 1) {
		fprintf(stderr, "files updates; %d\n", res);
		return T_EXIT_FAIL;
	}

	sockid = -1;
	res = io_uring_register_files_update(&ring, 0, &sockid, 1);
	if (res != 1) {
		fprintf(stderr, "files updates; %d\n", res);
		return T_EXIT_FAIL;
	}

	sleep(1);
	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
}