summaryrefslogtreecommitdiff
path: root/test/poll.c
blob: e2fde515c49a217f0e07f3d24e182ef37c566daf (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
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Description: test io_uring poll handling
 *
 * Based on 22.t from libaio
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/wait.h>

#include "../src/liburing.h"

int main(int argc, char *argv[])
{
	pid_t parent = getpid(), p;
	int pipe1[2], pipe2[2];
	struct io_uring cring, pring;
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	int ret;

	if (pipe(pipe1) != 0 || pipe(pipe2) != 0) {
		printf("pipe failed\n");
		return 1;
	}

	p = fork();
	switch (p) {
	case -1:
		printf("fork failed\n");
		exit(2);
	case 0:
		close(pipe1[1]);
		close(pipe2[0]);

		ret = io_uring_queue_init(1, &cring, 0);
		if (ret) {
			printf("child: ring setup failed\n");
			return 1;
		}

		sqe = io_uring_get_sqe(&cring);
		if (!sqe) {
			printf("child: get sqe failed\n");
			return 1;
		}

		memset(sqe, 0, sizeof(*sqe));
		sqe->opcode = IORING_OP_POLL;
		sqe->fd = pipe1[0];
		sqe->poll_events = POLLIN;
		sqe->user_data = (unsigned long) &sqe;

		ret = io_uring_submit(&cring);
		if (ret <= 0) {
			printf("child: sqe submit failed\n");
			return 1;
		}

		do {
			if (getppid() != parent) {
				printf("parent died\n");
				exit(2);
			}
			ret = io_uring_wait_completion(&cring, &cqe);
		} while (ret != 0);

		if (ret < 0) {
			printf("child: completion get failed\n");
			return 1;
		}

		do {
			errno = 0;
			ret = write(pipe2[1], "foo", 3);
		} while (ret == -1 && errno == EINTR);

		exit(0);
	default:
		close(pipe1[0]);
		close(pipe2[1]);

		ret = io_uring_queue_init(1, &pring, 0);
		if (ret) {
			printf("parent: ring setup failed\n");
			return 1;
		}

		sqe = io_uring_get_sqe(&pring);
		if (!sqe) {
			printf("parent: get sqe failed\n");
			return 1;
		}

		memset(sqe, 0, sizeof(*sqe));
		sqe->opcode = IORING_OP_POLL;
		sqe->fd = pipe2[0];
		sqe->poll_events = POLLIN;
		sqe->user_data = (unsigned long) &sqe;

		ret = io_uring_submit(&pring);
		if (ret <= 0) {
			printf("parent: sqe submit failed\n");
			return 1;
		}

		kill(p, SIGUSR1);

		ret = io_uring_wait_completion(&pring, &cqe);
		if (ret < 0) {
			printf("parent: cqe get failed\n");
			return 1;
		}
		if (cqe->user_data != (unsigned long) &sqe) {
			printf("parent: cqe wrong fd\n");
			return 1;
		}
		if ((cqe->res & POLLIN) != POLLIN) {
			printf("parent: cqe did not report readable fd\n");
			return 1;
		}

		return 0;
	}
}