summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/sys/epoll.c
blob: d497856083ea26ad738359d0529a512dcd6e52e6 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Joyent, Inc.
 * Copyright 2020 Oxide Computer Company
 */

#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/devpoll.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>

/*
 * Events that match their epoll(7) equivalents.
 */
#if EPOLLIN != POLLIN
#error value of EPOLLIN does not match value of POLLIN
#endif

#if EPOLLPRI != POLLPRI
#error value of EPOLLPRI does not match value of POLLPRI
#endif

#if EPOLLOUT != POLLOUT
#error value of EPOLLOUT does not match value of POLLOUT
#endif

#if EPOLLRDNORM != POLLRDNORM
#error value of EPOLLRDNORM does not match value of POLLRDNORM
#endif

#if EPOLLRDBAND != POLLRDBAND
#error value of EPOLLRDBAND does not match value of POLLRDBAND
#endif

#if EPOLLERR != POLLERR
#error value of EPOLLERR does not match value of POLLERR
#endif

#if EPOLLHUP != POLLHUP
#error value of EPOLLHUP does not match value of POLLHUP
#endif

/*
 * Events that we ignore entirely.  They can be set in events, but they will
 * never be returned.
 */
#define	EPOLLIGNORED	(EPOLLMSG | EPOLLWAKEUP | EPOLLEXCLUSIVE)

/*
 * Events that we swizzle into other bit positions.
 */
#define	EPOLLSWIZZLED	\
	(EPOLLRDHUP | EPOLLONESHOT | EPOLLET | EPOLLWRBAND | EPOLLWRNORM)

/*
 * The defined behavior for epoll_wait/epoll_pwait when using a timeout less
 * than 0 is to wait for events until they arrive (or interrupted by a signal).
 * While poll(4D) operates in this manner for a timeout of -1, using other
 * negative values results in an immediate timeout, as if it had been set to 0.
 * For that reason, negative values are clamped to -1.
 */
#define	EPOLL_TIMEOUT_CLAMP(t)	(((t) < -1) ? -1 : (t))

int
epoll_create(int size)
{
	int fd;

	/*
	 * From the epoll_create() man page:  "Since Linux 2.6.8, the size
	 * argument is ignored, but must be greater than zero."  You keep using
	 * that word "ignored"...
	 */
	if (size <= 0) {
		errno = EINVAL;
		return (-1);
	}

	if ((fd = open("/dev/poll", O_RDWR)) == -1)
		return (-1);

	if (ioctl(fd, DP_EPOLLCOMPAT, 0) == -1) {
		(void) close(fd);
		return (-1);
	}

	return (fd);
}

int
epoll_create1(int flags)
{
	int fd, oflags = O_RDWR;

	if (flags & EPOLL_CLOEXEC) {
		oflags |= O_CLOEXEC;
		flags ^= EPOLL_CLOEXEC;
	}
	/* Reject unrecognized flags */
	if (flags != 0) {
		errno = EINVAL;
		return (-1);
	}

	if ((fd = open("/dev/poll", oflags)) == -1)
		return (-1);

	if (ioctl(fd, DP_EPOLLCOMPAT, 0) == -1) {
		(void) close(fd);
		return (-1);
	}

	return (fd);
}

int
epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
	dvpoll_epollfd_t epoll[2];
	uint32_t events, ev = 0;
	int i = 0, res;

	epoll[i].dpep_pollfd.fd = fd;

	switch (op) {
	case EPOLL_CTL_DEL:
		ev = POLLREMOVE;
		break;

	case EPOLL_CTL_MOD:
		/* EPOLLEXCLUSIVE is prohibited for modify operations */
		if ((event->events & EPOLLEXCLUSIVE) != 0) {
			errno = EINVAL;
			return (-1);
		}
		/*
		 * In the modify case, we pass down two events:  one to
		 * remove the event and another to add it back.
		 */
		epoll[i++].dpep_pollfd.events = POLLREMOVE;
		epoll[i].dpep_pollfd.fd = fd;
		/* FALLTHROUGH */

	case EPOLL_CTL_ADD:
		/*
		 * Mask off the events that we ignore, and then swizzle the
		 * events for which our values differ from their epoll(7)
		 * equivalents.
		 */
		events = event->events;
		ev = events & ~(EPOLLIGNORED | EPOLLSWIZZLED);

		if (events & EPOLLRDHUP)
			ev |= POLLRDHUP;

		if (events & EPOLLET)
			ev |= POLLET;

		if (events & EPOLLONESHOT)
			ev |= POLLONESHOT;

		if (events & EPOLLWRNORM)
			ev |= POLLWRNORM;

		if (events & EPOLLWRBAND)
			ev |= POLLWRBAND;

		epoll[i].dpep_data = event->data.u64;
		break;

	default:
		errno = EOPNOTSUPP;
		return (-1);
	}

	epoll[i].dpep_pollfd.events = ev;
retry:
	res = write(epfd, epoll, sizeof (epoll[0]) * (i + 1));

	if (res == -1) {
		if (errno == EINTR) {
			/*
			 * Linux does not document EINTR as an allowed error
			 * for epoll_ctl.  The write must be retried if it is
			 * not done automatically via SA_RESTART.
			 */
			goto retry;
		}
		if (errno == ELOOP) {
			/*
			 * Convert the specific /dev/poll error about an fd
			 * loop into what is expected from the Linux epoll
			 * interface.
			 */
			errno = EINVAL;
		}
		return (-1);
	}
	return (0);
}

int
epoll_wait(int epfd, struct epoll_event *events,
    int maxevents, int timeout)
{
	struct dvpoll arg;

	if (maxevents <= 0) {
		errno = EINVAL;
		return (-1);
	}

	arg.dp_nfds = maxevents;
	arg.dp_timeout = EPOLL_TIMEOUT_CLAMP(timeout);
	arg.dp_fds = (pollfd_t *)events;

	return (ioctl(epfd, DP_POLL, &arg));
}

int
epoll_pwait(int epfd, struct epoll_event *events,
    int maxevents, int timeout, const sigset_t *sigmask)
{
	struct dvpoll arg;

	if (maxevents <= 0) {
		errno = EINVAL;
		return (-1);
	}

	arg.dp_nfds = maxevents;
	arg.dp_timeout = EPOLL_TIMEOUT_CLAMP(timeout);
	arg.dp_fds = (pollfd_t *)events;
	arg.dp_setp = (sigset_t *)sigmask;

	return (ioctl(epfd, DP_PPOLL, &arg));
}