summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/brand/lx/syscall/lx_epoll.c
blob: 47688dad6aa8499d357bb72fab948c6d07d155e4 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * 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.
 */

#include <sys/types.h>
#include <sys/systm.h>
#include <sys/thread.h>
#include <sys/proc.h>
#include <sys/zone.h>
#include <sys/brand.h>
#include <sys/epoll.h>
#include <sys/devpoll.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunldi.h>
#include <sys/vnode.h>
#include <sys/lx_brand.h>
#include <sys/lx_types.h>
#include <sys/lx_signal.h>

static major_t devpoll_major = 0;

static boolean_t
lx_epoll_isvalid(file_t *fp)
{
	vnode_t *vp = fp->f_vnode;

	if (vp->v_type == VCHR && getmajor(vp->v_rdev) == devpoll_major)
		return (B_TRUE);
	return (B_FALSE);
}

long
lx_epoll_create1(int flags)
{
	int err, fd, rv;
	int fmode = FREAD | FWRITE;
	boolean_t cloexec = B_FALSE;
	vnode_t *vp = NULL;
	file_t *fp = NULL;

	if (flags & EPOLL_CLOEXEC) {
		cloexec = B_TRUE;
		flags &= ~EPOLL_CLOEXEC;
	}
	if (flags != 0) {
		/* No other flags accepted at this time */
		return (set_errno(EINVAL));
	}

	if (falloc((vnode_t *)NULL, fmode, &fp, &fd) != 0) {
		err = EMFILE;
		goto error;
	}
	if (ldi_vp_from_name("/devices/pseudo/poll@0:poll", &vp) != 0) {
		err = ENOENT;
		goto error;
	}
	if ((err = VOP_OPEN(&vp, fmode | FKLYR, CRED(), NULL)) != 0) {
		goto error;
	}
	err = VOP_IOCTL(vp, DP_EPOLLCOMPAT, 0, fmode, CRED(), &rv, NULL);
	if (err != 0) {
		(void) VOP_CLOSE(vp, fmode, 0, 0, CRED(), NULL);
		goto error;
	}

	devpoll_major = getmajor(vp->v_rdev);

	fp->f_vnode = vp;
	mutex_exit(&fp->f_tlock);
	setf(fd, fp);
	if (cloexec) {
		f_setfd(fd, FD_CLOEXEC);
	}
	return (fd);

error:
	if (fp != NULL) {
		setf(fd, NULL);
		unfalloc(fp);
	}
	if (vp != NULL) {
		VN_RELE(vp);
	}
	return (set_errno(err));
}

long
lx_epoll_create(int size)
{
	if (size <= 0) {
		return (set_errno(EINVAL));
	}

	return (lx_epoll_create1(0));
}


/* Match values from libc implementation */
#define	EPOLLIGNORED 	(EPOLLMSG | EPOLLWAKEUP)
#define	EPOLLSWIZZLED	\
	(EPOLLRDHUP | EPOLLONESHOT | EPOLLET | EPOLLWRBAND | EPOLLWRNORM)
#define	EPOLL_TIMEOUT_CLAMP(t)	(((t) < -1) ? -1 : (t))

long
lx_epoll_ctl(int fd, int op, int pfd, void *event)
{
	epoll_event_t epevent;
	dvpoll_epollfd_t dpevent[2];
	file_t *fp;
	iovec_t aiov;
	uio_t auio;
	uint32_t events, ev = 0;
	int error = 0, i = 0;

	dpevent[i].dpep_pollfd.fd = pfd;
	switch (op) {
	case EPOLL_CTL_DEL:
		dpevent[i].dpep_pollfd.events = POLLREMOVE;
		break;

	case EPOLL_CTL_MOD:
		/*
		 * In the modify case, we pass down two events:  one to
		 * remove the event and another to add it back.
		 */
		dpevent[i++].dpep_pollfd.events = POLLREMOVE;
		dpevent[i].dpep_pollfd.fd = pfd;
		/* FALLTHROUGH */

	case EPOLL_CTL_ADD:
		if (copyin(event, &epevent, sizeof (epevent)) != 0)
			return (set_errno(EFAULT));

		/*
		 * Mask off the events that we ignore, and then swizzle the
		 * events for which our values differ from their epoll(7)
		 * equivalents.
		 */
		events = epevent.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;

		dpevent[i].dpep_data = epevent.data.u64;
		dpevent[i].dpep_pollfd.events = ev;
		break;

	default:
		return (set_errno(EINVAL));
	}

	if ((fp = getf(fd)) == NULL) {
		return (set_errno(EBADF));
	} else if (!lx_epoll_isvalid(fp)) {
		releasef(fd);
		return (set_errno(EINVAL));
	}

	aiov.iov_base = (void *)dpevent;
	aiov.iov_len = sizeof (dvpoll_epollfd_t) * (i + 1);
	auio.uio_iov = &aiov;
	auio.uio_iovcnt = 1;
	auio.uio_resid = aiov.iov_len;
	auio.uio_segflg = UIO_SYSSPACE;
	auio.uio_loffset = 0;
	auio.uio_fmode = fp->f_flag;

	error = VOP_WRITE(fp->f_vnode, &auio, 1, fp->f_cred, NULL);

	releasef(fd);

	switch (error) {
	case 0:
		return (0);

	case EBADF:
	case EEXIST:
	case EINVAL:
	case ENOENT:
	case ENOMEM:
	case ENOSPC:
	case EPERM:
		/*
		 * Legal errors should pass straight through.
		 */
		return (set_errno(error));

	case ELOOP:
		/*
		 * In the case of descriptor loops, /dev/poll emits a more
		 * descriptive error than Linux epoll consumers would expect.
		 */
		return (set_errno(EINVAL));

	default:
		/*
		 * While devpoll itself should not emit unexpected errors, it
		 * is possible that a VOP_POLL handler might.  There is little
		 * choice but to map these unexpected errors to something which
		 * is valid for epoll_ctl.
		 */
		return (set_errno(ENOMEM));
	}
}

long
lx_epoll_wait(int fd, void *events, int maxevents, int timeout)
{
	struct dvpoll arg;
	file_t *fp;
	int rv = 0, error, flag;

	if (maxevents <= 0) {
		return (set_errno(EINVAL));
	}
	if ((fp = getf(fd)) == NULL) {
		return (set_errno(EBADF));
	} else if (!lx_epoll_isvalid(fp)) {
		releasef(fd);
		return (set_errno(EINVAL));
	}

	arg.dp_nfds = maxevents;
	arg.dp_timeout = EPOLL_TIMEOUT_CLAMP(timeout);
	arg.dp_fds = (pollfd_t *)events;
	flag = fp->f_flag | DATAMODEL_NATIVE | FKIOCTL;
	error = VOP_IOCTL(fp->f_vnode, DP_POLL, (uintptr_t)&arg, flag,
	    fp->f_cred, &rv, NULL);

	releasef(fd);
	if (error != 0) {
		return (set_errno(error));
	}
	return (rv);
}

long
lx_epoll_pwait(int fd, void *events, int maxevents, int timeout, void *sigmask)
{
	struct dvpoll arg;
	file_t *fp;
	int rv = 0, error, flag;
	k_sigset_t ksig;

	if (maxevents <= 0) {
		return (set_errno(EINVAL));
	}
	if ((fp = getf(fd)) == NULL) {
		return (set_errno(EBADF));
	} else if (!lx_epoll_isvalid(fp)) {
		releasef(fd);
		return (set_errno(EINVAL));
	}
	if (sigmask != NULL) {
		lx_sigset_t lsig;

		if (copyin(sigmask, &lsig, sizeof (lsig)) != 0) {
			releasef(fd);
			return (set_errno(EFAULT));
		}
		lx_ltos_sigset(&lsig, &ksig);
		arg.dp_setp = (sigset_t *)&ksig;
	} else {
		arg.dp_setp = NULL;
	}

	arg.dp_nfds = maxevents;
	arg.dp_timeout = EPOLL_TIMEOUT_CLAMP(timeout);
	arg.dp_fds = (pollfd_t *)events;
	flag = fp->f_flag | DATAMODEL_NATIVE | FKIOCTL;
	error = VOP_IOCTL(fp->f_vnode, DP_PPOLL, (uintptr_t)&arg, flag,
	    fp->f_cred, &rv, NULL);

	releasef(fd);
	if (error != 0) {
		return (set_errno(error));
	}
	return (rv);
}