aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/pipe-eof.c
blob: a917ae00a7b16b24cc9dc788ba85d7695efc60e1 (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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */

/*
 * Test that closed pipe reads returns 0, instead of waiting for more
 * data.
 */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include "liburing.h"

#define BUFSIZE	512

struct data {
	char *str;
	int fds[2];
};

static void *t(void *data)
{
	struct data *d = data;
	int ret;

	strcpy(d->str, "This is a test string");
	ret = write(d->fds[1], d->str, strlen(d->str));
	close(d->fds[1]);
	if (ret < 0)
		perror("write");

	return NULL;
}

int main(int argc, char *argv[])
{
	static char buf[BUFSIZE];
	struct io_uring ring;
	pthread_t thread;
	struct data d;
	int ret;

	if (pipe(d.fds) < 0) {
		perror("pipe");
		return 1;
	}
	d.str = buf;

	io_uring_queue_init(8, &ring, 0);

	pthread_create(&thread, NULL, t, &d);

	while (1) {
		struct io_uring_sqe *sqe;
		struct io_uring_cqe *cqe;

		sqe = io_uring_get_sqe(&ring);
		io_uring_prep_read(sqe, d.fds[0], buf, BUFSIZE, 0);
		ret = io_uring_submit(&ring);
		if (ret != 1) {
			fprintf(stderr, "submit: %d\n", ret);
			return 1;
		}
		ret = io_uring_wait_cqe(&ring, &cqe);
		if (ret) {
			fprintf(stderr, "wait: %d\n", ret);
			return 1;
		}

		if (cqe->res < 0) {
			fprintf(stderr, "Read error: %s\n", strerror(-cqe->res));
			return 1;
		}
		if (cqe->res == 0)
			break;
		io_uring_cqe_seen(&ring, cqe);
	}

	pthread_join(thread, NULL);
	io_uring_queue_exit(&ring);
	return 0;
}