cyapa - Introduce better three-finger button emulation
[dragonfly.git] / sys / kern / sys_generic.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
dc71b7ab 18 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
35 * $FreeBSD: src/sys/kern/sys_generic.c,v 1.55.2.10 2001/03/17 10:39:32 peter Exp $
36 */
37
38#include "opt_ktrace.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/sysproto.h>
e5857bf7 43#include <sys/event.h>
984263bc
MD
44#include <sys/filedesc.h>
45#include <sys/filio.h>
46#include <sys/fcntl.h>
47#include <sys/file.h>
48#include <sys/proc.h>
49#include <sys/signalvar.h>
50#include <sys/socketvar.h>
51#include <sys/uio.h>
52#include <sys/kernel.h>
ba023347 53#include <sys/kern_syscall.h>
984263bc 54#include <sys/malloc.h>
a0c5fc96 55#include <sys/mapped_ioctl.h>
984263bc 56#include <sys/poll.h>
a0c5fc96 57#include <sys/queue.h>
984263bc 58#include <sys/resourcevar.h>
8b5c39bb 59#include <sys/socketops.h>
984263bc
MD
60#include <sys/sysctl.h>
61#include <sys/sysent.h>
62#include <sys/buf.h>
63#ifdef KTRACE
64#include <sys/ktrace.h>
65#endif
66#include <vm/vm.h>
67#include <vm/vm_page.h>
684a93c4 68
dadab5e9 69#include <sys/file2.h>
684a93c4 70#include <sys/mplock2.h>
ac62ea3c 71#include <sys/spinlock2.h>
984263bc
MD
72
73#include <machine/limits.h>
74
75static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
a0c5fc96 76static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
984263bc
MD
77static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
78MALLOC_DEFINE(M_IOV, "iov", "large iov's");
79
e5857bf7 80typedef struct kfd_set {
8acdf1cf 81 fd_mask fds_bits[2];
e5857bf7
SG
82} kfd_set;
83
84enum select_copyin_states {
85 COPYIN_READ, COPYIN_WRITE, COPYIN_EXCEPT, COPYIN_DONE };
86
87struct select_kevent_copyin_args {
88 kfd_set *read_set;
89 kfd_set *write_set;
90 kfd_set *except_set;
91 int active_set; /* One of select_copyin_states */
92 struct lwp *lwp; /* Pointer to our lwp */
93 int num_fds; /* Number of file descriptors (syscall arg) */
94 int proc_fds; /* Processed fd's (wraps) */
95 int error; /* Returned to userland */
96};
97
7fbfbe29
SG
98struct poll_kevent_copyin_args {
99 struct lwp *lwp;
100 struct pollfd *fds;
101 int nfds;
102 int pfds;
103 int error;
104};
105
a3c18566 106static struct lwkt_token mioctl_token = LWKT_TOKEN_INITIALIZER(mioctl_token);
3c499555 107
ae7cb1b5 108static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
8acdf1cf 109 struct timespec *ts, int *res);
7fbfbe29
SG
110static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
111 int *res);
e54488bb
MD
112static int dofileread(int, struct file *, struct uio *, int, size_t *);
113static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
7f83ed38 114
984263bc
MD
115/*
116 * Read system call.
f832287e
MD
117 *
118 * MPSAFE
984263bc 119 */
984263bc 120int
753fd850 121sys_read(struct read_args *uap)
984263bc 122{
dadab5e9 123 struct thread *td = curthread;
ba023347
DRJ
124 struct uio auio;
125 struct iovec aiov;
984263bc
MD
126 int error;
127
e54488bb
MD
128 if ((ssize_t)uap->nbyte < 0)
129 error = EINVAL;
130
ba023347
DRJ
131 aiov.iov_base = uap->buf;
132 aiov.iov_len = uap->nbyte;
133 auio.uio_iov = &aiov;
134 auio.uio_iovcnt = 1;
135 auio.uio_offset = -1;
136 auio.uio_resid = uap->nbyte;
137 auio.uio_rw = UIO_READ;
138 auio.uio_segflg = UIO_USERSPACE;
139 auio.uio_td = td;
140
e54488bb 141 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
984263bc
MD
142 return(error);
143}
144
145/*
7f83ed38 146 * Positioned (Pread) read system call
f832287e
MD
147 *
148 * MPSAFE
984263bc 149 */
984263bc 150int
b09fd398 151sys_extpread(struct extpread_args *uap)
984263bc 152{
dadab5e9 153 struct thread *td = curthread;
ba023347
DRJ
154 struct uio auio;
155 struct iovec aiov;
984263bc 156 int error;
9ba76b73 157 int flags;
984263bc 158
e54488bb
MD
159 if ((ssize_t)uap->nbyte < 0)
160 return(EINVAL);
161
ba023347
DRJ
162 aiov.iov_base = uap->buf;
163 aiov.iov_len = uap->nbyte;
164 auio.uio_iov = &aiov;
165 auio.uio_iovcnt = 1;
166 auio.uio_offset = uap->offset;
167 auio.uio_resid = uap->nbyte;
168 auio.uio_rw = UIO_READ;
169 auio.uio_segflg = UIO_USERSPACE;
170 auio.uio_td = td;
171
9ba76b73
MD
172 flags = uap->flags & O_FMASK;
173 if (uap->offset != (off_t)-1)
174 flags |= O_FOFFSET;
175
e54488bb 176 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
984263bc
MD
177 return(error);
178}
179
7f83ed38
MD
180/*
181 * Scatter read system call.
f832287e
MD
182 *
183 * MPSAFE
7f83ed38 184 */
984263bc 185int
753fd850 186sys_readv(struct readv_args *uap)
984263bc 187{
dadab5e9 188 struct thread *td = curthread;
984263bc 189 struct uio auio;
ba023347
DRJ
190 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
191 int error;
984263bc 192
ba023347 193 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 194 &auio.uio_resid);
ba023347
DRJ
195 if (error)
196 return (error);
197 auio.uio_iov = iov;
198 auio.uio_iovcnt = uap->iovcnt;
199 auio.uio_offset = -1;
984263bc
MD
200 auio.uio_rw = UIO_READ;
201 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 202 auio.uio_td = td;
984263bc 203
e54488bb 204 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
ba023347
DRJ
205
206 iovec_free(&iov, aiov);
984263bc
MD
207 return (error);
208}
209
7f83ed38
MD
210
211/*
212 * Scatter positioned read system call.
f832287e
MD
213 *
214 * MPSAFE
7f83ed38
MD
215 */
216int
b09fd398 217sys_extpreadv(struct extpreadv_args *uap)
7f83ed38
MD
218{
219 struct thread *td = curthread;
220 struct uio auio;
221 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
222 int error;
9ba76b73 223 int flags;
7f83ed38
MD
224
225 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 226 &auio.uio_resid);
7f83ed38
MD
227 if (error)
228 return (error);
229 auio.uio_iov = iov;
230 auio.uio_iovcnt = uap->iovcnt;
231 auio.uio_offset = uap->offset;
232 auio.uio_rw = UIO_READ;
233 auio.uio_segflg = UIO_USERSPACE;
234 auio.uio_td = td;
235
9ba76b73
MD
236 flags = uap->flags & O_FMASK;
237 if (uap->offset != (off_t)-1)
238 flags |= O_FOFFSET;
239
e54488bb 240 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
7f83ed38
MD
241
242 iovec_free(&iov, aiov);
243 return(error);
244}
245
f832287e
MD
246/*
247 * MPSAFE
248 */
984263bc 249int
e54488bb 250kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
984263bc 251{
dadab5e9
MD
252 struct thread *td = curthread;
253 struct proc *p = td->td_proc;
41c20dac 254 struct file *fp;
7f83ed38 255 int error;
984263bc 256
ba023347
DRJ
257 KKASSERT(p);
258
228b401d 259 fp = holdfp(p->p_fd, fd, FREAD);
ba023347 260 if (fp == NULL)
984263bc 261 return (EBADF);
9ba76b73 262 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
ba023347 263 error = ESPIPE;
7f83ed38
MD
264 } else {
265 error = dofileread(fd, fp, auio, flags, res);
984263bc 266 }
9f87144f 267 fdrop(fp);
7f83ed38
MD
268 return(error);
269}
270
271/*
272 * Common code for readv and preadv that reads data in
273 * from a file using the passed in uio, offset, and flags.
f832287e
MD
274 *
275 * MPALMOSTSAFE - ktrace needs help
7f83ed38
MD
276 */
277static int
e54488bb 278dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
7f83ed38 279{
7f83ed38 280 int error;
e54488bb 281 size_t len;
7f83ed38 282#ifdef KTRACE
fc9ae81d 283 struct thread *td = curthread;
7f83ed38
MD
284 struct iovec *ktriov = NULL;
285 struct uio ktruio;
286#endif
287
984263bc
MD
288#ifdef KTRACE
289 /*
290 * if tracing, save a copy of iovec
291 */
dadab5e9 292 if (KTRPOINT(td, KTR_GENIO)) {
ba023347
DRJ
293 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
294
884717e1 295 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
ba023347
DRJ
296 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
297 ktruio = *auio;
984263bc
MD
298 }
299#endif
ba023347 300 len = auio->uio_resid;
87de5057 301 error = fo_read(fp, auio, fp->f_cred, flags);
ba023347
DRJ
302 if (error) {
303 if (auio->uio_resid != len && (error == ERESTART ||
984263bc
MD
304 error == EINTR || error == EWOULDBLOCK))
305 error = 0;
306 }
984263bc
MD
307#ifdef KTRACE
308 if (ktriov != NULL) {
309 if (error == 0) {
310 ktruio.uio_iov = ktriov;
ba023347 311 ktruio.uio_resid = len - auio->uio_resid;
f832287e 312 get_mplock();
9fb04d14 313 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
f832287e 314 rel_mplock();
984263bc 315 }
884717e1 316 kfree(ktriov, M_TEMP);
984263bc
MD
317 }
318#endif
ba023347
DRJ
319 if (error == 0)
320 *res = len - auio->uio_resid;
7f83ed38
MD
321
322 return(error);
984263bc
MD
323}
324
325/*
326 * Write system call
f832287e
MD
327 *
328 * MPSAFE
984263bc 329 */
984263bc 330int
753fd850 331sys_write(struct write_args *uap)
984263bc 332{
dadab5e9 333 struct thread *td = curthread;
ba023347
DRJ
334 struct uio auio;
335 struct iovec aiov;
984263bc
MD
336 int error;
337
e54488bb
MD
338 if ((ssize_t)uap->nbyte < 0)
339 error = EINVAL;
340
ba023347
DRJ
341 aiov.iov_base = (void *)(uintptr_t)uap->buf;
342 aiov.iov_len = uap->nbyte;
343 auio.uio_iov = &aiov;
344 auio.uio_iovcnt = 1;
345 auio.uio_offset = -1;
346 auio.uio_resid = uap->nbyte;
347 auio.uio_rw = UIO_WRITE;
348 auio.uio_segflg = UIO_USERSPACE;
349 auio.uio_td = td;
350
e54488bb 351 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
dadab5e9 352
984263bc
MD
353 return(error);
354}
355
356/*
357 * Pwrite system call
f832287e
MD
358 *
359 * MPSAFE
984263bc 360 */
984263bc 361int
b09fd398 362sys_extpwrite(struct extpwrite_args *uap)
984263bc 363{
dadab5e9 364 struct thread *td = curthread;
ba023347
DRJ
365 struct uio auio;
366 struct iovec aiov;
984263bc 367 int error;
9ba76b73 368 int flags;
984263bc 369
e54488bb
MD
370 if ((ssize_t)uap->nbyte < 0)
371 error = EINVAL;
372
ba023347
DRJ
373 aiov.iov_base = (void *)(uintptr_t)uap->buf;
374 aiov.iov_len = uap->nbyte;
375 auio.uio_iov = &aiov;
376 auio.uio_iovcnt = 1;
377 auio.uio_offset = uap->offset;
378 auio.uio_resid = uap->nbyte;
379 auio.uio_rw = UIO_WRITE;
380 auio.uio_segflg = UIO_USERSPACE;
381 auio.uio_td = td;
382
9ba76b73
MD
383 flags = uap->flags & O_FMASK;
384 if (uap->offset != (off_t)-1)
385 flags |= O_FOFFSET;
e54488bb 386 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
984263bc
MD
387 return(error);
388}
389
f832287e
MD
390/*
391 * MPSAFE
392 */
ba023347 393int
753fd850 394sys_writev(struct writev_args *uap)
ba023347 395{
dadab5e9 396 struct thread *td = curthread;
984263bc 397 struct uio auio;
ba023347
DRJ
398 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
399 int error;
984263bc 400
ba023347 401 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 402 &auio.uio_resid);
ba023347
DRJ
403 if (error)
404 return (error);
405 auio.uio_iov = iov;
406 auio.uio_iovcnt = uap->iovcnt;
407 auio.uio_offset = -1;
984263bc
MD
408 auio.uio_rw = UIO_WRITE;
409 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 410 auio.uio_td = td;
ba023347 411
e54488bb 412 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
ba023347
DRJ
413
414 iovec_free(&iov, aiov);
984263bc
MD
415 return (error);
416}
417
7f83ed38 418
984263bc 419/*
7f83ed38 420 * Gather positioned write system call
f832287e
MD
421 *
422 * MPSAFE
984263bc 423 */
984263bc 424int
b09fd398 425sys_extpwritev(struct extpwritev_args *uap)
7f83ed38
MD
426{
427 struct thread *td = curthread;
428 struct uio auio;
429 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
430 int error;
9ba76b73 431 int flags;
7f83ed38
MD
432
433 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 434 &auio.uio_resid);
7f83ed38
MD
435 if (error)
436 return (error);
437 auio.uio_iov = iov;
438 auio.uio_iovcnt = uap->iovcnt;
439 auio.uio_offset = uap->offset;
440 auio.uio_rw = UIO_WRITE;
441 auio.uio_segflg = UIO_USERSPACE;
442 auio.uio_td = td;
443
9ba76b73
MD
444 flags = uap->flags & O_FMASK;
445 if (uap->offset != (off_t)-1)
446 flags |= O_FOFFSET;
447
e54488bb 448 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
7f83ed38
MD
449
450 iovec_free(&iov, aiov);
451 return(error);
452}
453
f832287e
MD
454/*
455 * MPSAFE
456 */
7f83ed38 457int
e54488bb 458kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
984263bc 459{
dadab5e9
MD
460 struct thread *td = curthread;
461 struct proc *p = td->td_proc;
41c20dac 462 struct file *fp;
7f83ed38 463 int error;
984263bc 464
dadab5e9 465 KKASSERT(p);
dadab5e9 466
228b401d 467 fp = holdfp(p->p_fd, fd, FWRITE);
ba023347 468 if (fp == NULL)
984263bc 469 return (EBADF);
9ba76b73 470 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
ba023347 471 error = ESPIPE;
7f83ed38
MD
472 } else {
473 error = dofilewrite(fd, fp, auio, flags, res);
984263bc 474 }
7f83ed38 475
9f87144f 476 fdrop(fp);
7f83ed38
MD
477 return (error);
478}
479
480/*
481 * Common code for writev and pwritev that writes data to
482 * a file using the passed in uio, offset, and flags.
f832287e
MD
483 *
484 * MPALMOSTSAFE - ktrace needs help
7f83ed38
MD
485 */
486static int
e54488bb 487dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
7f83ed38
MD
488{
489 struct thread *td = curthread;
7278a846 490 struct lwp *lp = td->td_lwp;
7f83ed38 491 int error;
e54488bb 492 size_t len;
7f83ed38
MD
493#ifdef KTRACE
494 struct iovec *ktriov = NULL;
495 struct uio ktruio;
496#endif
497
984263bc
MD
498#ifdef KTRACE
499 /*
500 * if tracing, save a copy of iovec and uio
501 */
dadab5e9 502 if (KTRPOINT(td, KTR_GENIO)) {
ba023347
DRJ
503 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
504
884717e1 505 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
ba023347
DRJ
506 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
507 ktruio = *auio;
984263bc
MD
508 }
509#endif
ba023347 510 len = auio->uio_resid;
87de5057 511 error = fo_write(fp, auio, fp->f_cred, flags);
ba023347
DRJ
512 if (error) {
513 if (auio->uio_resid != len && (error == ERESTART ||
984263bc
MD
514 error == EINTR || error == EWOULDBLOCK))
515 error = 0;
7f83ed38 516 /* Socket layer is responsible for issuing SIGPIPE. */
89233cfd 517 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
9fb04d14 518 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
984263bc 519 }
984263bc
MD
520#ifdef KTRACE
521 if (ktriov != NULL) {
522 if (error == 0) {
523 ktruio.uio_iov = ktriov;
ba023347 524 ktruio.uio_resid = len - auio->uio_resid;
f832287e 525 get_mplock();
9fb04d14 526 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
f832287e 527 rel_mplock();
984263bc 528 }
884717e1 529 kfree(ktriov, M_TEMP);
984263bc
MD
530 }
531#endif
ba023347
DRJ
532 if (error == 0)
533 *res = len - auio->uio_resid;
7f83ed38
MD
534
535 return(error);
984263bc
MD
536}
537
538/*
539 * Ioctl system call
3919ced0 540 *
3c499555 541 * MPSAFE
984263bc 542 */
984263bc 543int
753fd850 544sys_ioctl(struct ioctl_args *uap)
a0c5fc96 545{
3919ced0
MD
546 int error;
547
3919ced0 548 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
3919ced0 549 return (error);
a0c5fc96
JS
550}
551
552struct ioctl_map_entry {
553 const char *subsys;
554 struct ioctl_map_range *cmd_ranges;
555 LIST_ENTRY(ioctl_map_entry) entries;
556};
557
25b5b94d
SS
558/*
559 * The true heart of all ioctl syscall handlers (native, emulation).
560 * If map != NULL, it will be searched for a matching entry for com,
561 * and appropriate conversions/conversion functions will be utilized.
3c499555
MD
562 *
563 * MPSAFE
25b5b94d 564 */
a0c5fc96 565int
87baaf0c
MD
566mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
567 struct sysmsg *msg)
984263bc 568{
dadab5e9
MD
569 struct thread *td = curthread;
570 struct proc *p = td->td_proc;
87de5057 571 struct ucred *cred;
41c20dac 572 struct file *fp;
a0c5fc96 573 struct ioctl_map_range *iomc = NULL;
984263bc 574 int error;
1fd87d54 575 u_int size;
a0c5fc96 576 u_long ocom = com;
984263bc
MD
577 caddr_t data, memp;
578 int tmp;
579#define STK_PARAMS 128
580 union {
581 char stkbuf[STK_PARAMS];
582 long align;
583 } ubuf;
584
dadab5e9 585 KKASSERT(p);
9910d07b 586 cred = td->td_ucred;
984263bc 587
228b401d
MD
588 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
589 if (fp == NULL)
a0c5fc96
JS
590 return(EBADF);
591
592 if (map != NULL) { /* obey translation map */
593 u_long maskcmd;
594 struct ioctl_map_entry *e;
595
596 maskcmd = com & map->mask;
597
12586b82 598 lwkt_gettoken(&mioctl_token);
a0c5fc96
JS
599 LIST_FOREACH(e, &map->mapping, entries) {
600 for (iomc = e->cmd_ranges; iomc->start != 0 ||
25b5b94d
SS
601 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
602 iomc->mapfunc != NULL;
a0c5fc96
JS
603 iomc++) {
604 if (maskcmd >= iomc->start &&
605 maskcmd <= iomc->end)
606 break;
607 }
608
609 /* Did we find a match? */
610 if (iomc->start != 0 || iomc->maptocmd != 0 ||
25b5b94d 611 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
a0c5fc96
JS
612 break;
613 }
12586b82 614 lwkt_reltoken(&mioctl_token);
a0c5fc96
JS
615
616 if (iomc == NULL ||
617 (iomc->start == 0 && iomc->maptocmd == 0
25b5b94d 618 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
6ea70f76 619 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
a0c5fc96
JS
620 map->sys, fd, maskcmd,
621 (int)((maskcmd >> 8) & 0xff),
622 (int)(maskcmd & 0xff));
228b401d
MD
623 error = EINVAL;
624 goto done;
a0c5fc96 625 }
984263bc 626
25b5b94d
SS
627 /*
628 * If it's a non-range one to one mapping, maptocmd should be
629 * correct. If it's a ranged one to one mapping, we pass the
630 * original value of com, and for a range mapped to a different
631 * range, we always need a mapping function to translate the
632 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
633 */
634 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
635 com = iomc->maptocmd;
636 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
637 if (iomc->mapfunc != NULL)
638 com = iomc->mapfunc(iomc->start, iomc->end,
639 iomc->start, iomc->end,
640 com, com);
641 } else {
642 if (iomc->mapfunc != NULL) {
643 com = iomc->mapfunc(iomc->start, iomc->end,
644 iomc->maptocmd, iomc->maptoend,
645 com, ocom);
646 } else {
6ea70f76 647 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
25b5b94d
SS
648 map->sys, fd, maskcmd,
649 (int)((maskcmd >> 8) & 0xff),
650 (int)(maskcmd & 0xff));
228b401d
MD
651 error = EINVAL;
652 goto done;
25b5b94d
SS
653 }
654 }
a0c5fc96
JS
655 }
656
657 switch (com) {
984263bc 658 case FIONCLEX:
228b401d
MD
659 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
660 goto done;
984263bc 661 case FIOCLEX:
228b401d
MD
662 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
663 goto done;
984263bc
MD
664 }
665
666 /*
667 * Interpret high order word to find amount of data to be
668 * copied to/from the user's address space.
669 */
670 size = IOCPARM_LEN(com);
228b401d
MD
671 if (size > IOCPARM_MAX) {
672 error = ENOTTY;
673 goto done;
674 }
984263bc 675
13886075 676 if ((com & IOC_VOID) == 0 && size > sizeof(ubuf.stkbuf)) {
efda3bd0 677 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
984263bc
MD
678 data = memp;
679 } else {
d0d4a734 680 memp = NULL;
984263bc
MD
681 data = ubuf.stkbuf;
682 }
13886075
MD
683 if (com & IOC_VOID) {
684 *(caddr_t *)data = uspc_data;
685 } else if (com & IOC_IN) {
a0c5fc96 686 if (size != 0) {
e54488bb 687 error = copyin(uspc_data, data, (size_t)size);
13886075 688 if (error)
228b401d 689 goto done;
984263bc 690 } else {
a0c5fc96 691 *(caddr_t *)data = uspc_data;
984263bc 692 }
a0c5fc96 693 } else if ((com & IOC_OUT) != 0 && size) {
984263bc
MD
694 /*
695 * Zero the buffer so the user always
696 * gets back something deterministic.
697 */
e54488bb 698 bzero(data, (size_t)size);
984263bc
MD
699 }
700
701 switch (com) {
984263bc
MD
702 case FIONBIO:
703 if ((tmp = *(int *)data))
3c499555 704 atomic_set_int(&fp->f_flag, FNONBLOCK);
984263bc 705 else
3c499555 706 atomic_clear_int(&fp->f_flag, FNONBLOCK);
9ba76b73 707 error = 0;
984263bc
MD
708 break;
709
710 case FIOASYNC:
711 if ((tmp = *(int *)data))
3c499555 712 atomic_set_int(&fp->f_flag, FASYNC);
984263bc 713 else
3c499555 714 atomic_clear_int(&fp->f_flag, FASYNC);
87baaf0c 715 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
984263bc
MD
716 break;
717
718 default:
a0c5fc96
JS
719 /*
720 * If there is a override function,
721 * call it instead of directly routing the call
722 */
25b5b94d 723 if (map != NULL && iomc->wrapfunc != NULL)
87de5057 724 error = iomc->wrapfunc(fp, com, ocom, data, cred);
a0c5fc96 725 else
87baaf0c 726 error = fo_ioctl(fp, com, data, cred, msg);
984263bc
MD
727 /*
728 * Copy any data to user, size was
729 * already set and checked above.
730 */
a0c5fc96 731 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
e54488bb 732 error = copyout(data, uspc_data, (size_t)size);
984263bc
MD
733 break;
734 }
13886075 735done:
a0c5fc96 736 if (memp != NULL)
efda3bd0 737 kfree(memp, M_IOCTLOPS);
9f87144f 738 fdrop(fp);
a0c5fc96
JS
739 return(error);
740}
741
3c499555
MD
742/*
743 * MPSAFE
744 */
a0c5fc96
JS
745int
746mapped_ioctl_register_handler(struct ioctl_map_handler *he)
747{
748 struct ioctl_map_entry *ne;
749
750 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
751 he->subsys != NULL && *he->subsys != '\0');
752
3c499555
MD
753 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP,
754 M_WAITOK | M_ZERO);
a0c5fc96
JS
755
756 ne->subsys = he->subsys;
757 ne->cmd_ranges = he->cmd_ranges;
758
12586b82 759 lwkt_gettoken(&mioctl_token);
a0c5fc96 760 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
12586b82 761 lwkt_reltoken(&mioctl_token);
a0c5fc96
JS
762
763 return(0);
764}
765
3c499555
MD
766/*
767 * MPSAFE
768 */
a0c5fc96
JS
769int
770mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
771{
772 struct ioctl_map_entry *ne;
3c499555 773 int error = EINVAL;
a0c5fc96
JS
774
775 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
776
12586b82 777 lwkt_gettoken(&mioctl_token);
a0c5fc96 778 LIST_FOREACH(ne, &he->map->mapping, entries) {
3c499555
MD
779 if (ne->cmd_ranges == he->cmd_ranges) {
780 LIST_REMOVE(ne, entries);
781 kfree(ne, M_IOCTLMAP);
782 error = 0;
783 break;
784 }
a0c5fc96 785 }
12586b82 786 lwkt_reltoken(&mioctl_token);
3c499555 787 return(error);
984263bc
MD
788}
789
790static int nselcoll; /* Select collisions since boot */
791int selwait;
792SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
fe24d605
MD
793static int nseldebug;
794SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
984263bc
MD
795
796/*
797 * Select system call.
3919ced0 798 *
e5857bf7 799 * MPSAFE
984263bc 800 */
984263bc 801int
753fd850 802sys_select(struct select_args *uap)
ae7cb1b5
NT
803{
804 struct timeval ktv;
e5857bf7 805 struct timespec *ktsp, kts;
ae7cb1b5
NT
806 int error;
807
808 /*
809 * Get timeout if any.
810 */
811 if (uap->tv != NULL) {
812 error = copyin(uap->tv, &ktv, sizeof (ktv));
813 if (error)
814 return (error);
e5857bf7
SG
815 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
816 ktsp = &kts;
ae7cb1b5 817 } else {
e5857bf7 818 ktsp = NULL;
ae7cb1b5
NT
819 }
820
821 /*
822 * Do real work.
823 */
e5857bf7 824 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
8acdf1cf 825 &uap->sysmsg_result);
ae7cb1b5
NT
826
827 return (error);
828}
829
830
831/*
832 * Pselect system call.
833 */
834int
835sys_pselect(struct pselect_args *uap)
836{
837 struct thread *td = curthread;
838 struct lwp *lp = td->td_lwp;
e5857bf7 839 struct timespec *ktsp, kts;
ae7cb1b5
NT
840 sigset_t sigmask;
841 int error;
842
843 /*
e5857bf7 844 * Get timeout if any.
ae7cb1b5
NT
845 */
846 if (uap->ts != NULL) {
847 error = copyin(uap->ts, &kts, sizeof (kts));
848 if (error)
849 return (error);
e5857bf7 850 ktsp = &kts;
ae7cb1b5 851 } else {
e5857bf7 852 ktsp = NULL;
ae7cb1b5
NT
853 }
854
855 /*
856 * Install temporary signal mask if any provided.
857 */
858 if (uap->sigmask != NULL) {
859 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
860 if (error)
861 return (error);
73241316 862 lwkt_gettoken(&lp->lwp_proc->p_token);
ae7cb1b5
NT
863 lp->lwp_oldsigmask = lp->lwp_sigmask;
864 SIG_CANTMASK(sigmask);
865 lp->lwp_sigmask = sigmask;
73241316 866 lwkt_reltoken(&lp->lwp_proc->p_token);
ae7cb1b5
NT
867 }
868
869 /*
870 * Do real job.
871 */
e5857bf7 872 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
8acdf1cf 873 &uap->sysmsg_result);
ae7cb1b5
NT
874
875 if (uap->sigmask != NULL) {
73241316 876 lwkt_gettoken(&lp->lwp_proc->p_token);
ae7cb1b5
NT
877 /* doselect() responsible for turning ERESTART into EINTR */
878 KKASSERT(error != ERESTART);
879 if (error == EINTR) {
880 /*
881 * We can't restore the previous signal mask now
882 * because it could block the signal that interrupted
883 * us. So make a note to restore it after executing
884 * the handler.
885 */
4643740a 886 lp->lwp_flags |= LWP_OLDMASK;
ae7cb1b5
NT
887 } else {
888 /*
889 * No handler to run. Restore previous mask immediately.
890 */
891 lp->lwp_sigmask = lp->lwp_oldsigmask;
892 }
73241316 893 lwkt_reltoken(&lp->lwp_proc->p_token);
ae7cb1b5
NT
894 }
895
896 return (error);
897}
898
e5857bf7 899static int
8acdf1cf 900select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
e5857bf7
SG
901{
902 struct select_kevent_copyin_args *skap = NULL;
8acdf1cf 903 struct kevent *kev;
e5857bf7
SG
904 int fd;
905 kfd_set *fdp = NULL;
906 short filter = 0;
907 u_int fflags = 0;
908
909 skap = (struct select_kevent_copyin_args *)arg;
910
8acdf1cf
MD
911 if (*events == maxevents)
912 return (0);
913
e5857bf7
SG
914 while (skap->active_set < COPYIN_DONE) {
915 switch (skap->active_set) {
916 case COPYIN_READ:
8acdf1cf
MD
917 /*
918 * Register descriptors for the read filter
919 */
e5857bf7
SG
920 fdp = skap->read_set;
921 filter = EVFILT_READ;
57b24f4e 922 fflags = NOTE_OLDAPI;
8acdf1cf
MD
923 if (fdp)
924 break;
925 ++skap->active_set;
926 skap->proc_fds = 0;
927 /* fall through */
e5857bf7 928 case COPYIN_WRITE:
8acdf1cf
MD
929 /*
930 * Register descriptors for the write filter
931 */
e5857bf7
SG
932 fdp = skap->write_set;
933 filter = EVFILT_WRITE;
57b24f4e 934 fflags = NOTE_OLDAPI;
8acdf1cf
MD
935 if (fdp)
936 break;
937 ++skap->active_set;
938 skap->proc_fds = 0;
939 /* fall through */
e5857bf7 940 case COPYIN_EXCEPT:
8acdf1cf
MD
941 /*
942 * Register descriptors for the exception filter
943 */
e5857bf7 944 fdp = skap->except_set;
73c344d3 945 filter = EVFILT_EXCEPT;
57b24f4e 946 fflags = NOTE_OLDAPI | NOTE_OOB;
8acdf1cf
MD
947 if (fdp)
948 break;
949 ++skap->active_set;
950 skap->proc_fds = 0;
951 /* fall through */
952 case COPYIN_DONE:
953 /*
954 * Nothing left to register
955 */
956 return(0);
957 /* NOT REACHED */
e5857bf7
SG
958 }
959
8acdf1cf
MD
960 while (skap->proc_fds < skap->num_fds) {
961 fd = skap->proc_fds;
e5857bf7 962 if (FD_ISSET(fd, fdp)) {
8acdf1cf
MD
963 kev = &kevp[*events];
964 EV_SET(kev, fd, filter,
965 EV_ADD|EV_ENABLE,
966 fflags, 0,
b4372719
MD
967 (void *)(uintptr_t)
968 skap->lwp->lwp_kqueue_serial);
e5857bf7 969 FD_CLR(fd, fdp);
8acdf1cf 970 ++*events;
22690e50
SG
971
972 if (nseldebug)
973 kprintf("select fd %d filter %d serial %d\n",
974 fd, filter, skap->lwp->lwp_kqueue_serial);
e5857bf7 975 }
8acdf1cf
MD
976 ++skap->proc_fds;
977 if (*events == maxevents)
e5857bf7
SG
978 return (0);
979 }
e5857bf7
SG
980 skap->active_set++;
981 skap->proc_fds = 0;
982 }
983
984 return (0);
985}
986
987static int
988select_copyout(void *arg, struct kevent *kevp, int count, int *res)
989{
990 struct select_kevent_copyin_args *skap;
991 struct kevent kev;
73c344d3 992 int i = 0;
e5857bf7
SG
993
994 skap = (struct select_kevent_copyin_args *)arg;
995
e5857bf7 996 for (i = 0; i < count; ++i) {
22690e50
SG
997 /*
998 * Filter out and delete spurious events
999 */
b4372719
MD
1000 if ((u_int)(uintptr_t)kevp[i].udata !=
1001 skap->lwp->lwp_kqueue_serial) {
8acdf1cf 1002 kev = kevp[i];
e5857bf7 1003 kev.flags = EV_DISABLE|EV_DELETE;
8acdf1cf 1004 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
22690e50 1005 if (nseldebug)
c9a1a153
SW
1006 kprintf("select fd %ju mismatched serial %d\n",
1007 (uintmax_t)kevp[i].ident,
1008 skap->lwp->lwp_kqueue_serial);
22690e50
SG
1009 continue;
1010 }
1011
1012 /*
1013 * Handle errors
1014 */
1015 if (kevp[i].flags & EV_ERROR) {
23a031e3
SZ
1016 int error = kevp[i].data;
1017
1018 switch (error) {
22690e50
SG
1019 case EBADF:
1020 /*
1021 * A bad file descriptor is considered a
1022 * fatal error for select, bail out.
1023 */
23a031e3
SZ
1024 skap->error = error;
1025 *res = -1;
1026 return error;
1027
22690e50
SG
1028 default:
1029 /*
1030 * Select silently swallows any unknown errors
1031 * for descriptors in the read or write sets.
4fbb9324
SG
1032 *
1033 * ALWAYS filter out EOPNOTSUPP errors from
1034 * filters (at least until all filters support
1035 * EVFILT_EXCEPT)
59737d5e
MP
1036 *
1037 * We also filter out ENODEV since dev_dkqfilter
1038 * returns ENODEV if EOPNOTSUPP is returned in an
1039 * inner call.
1040 *
1041 * XXX: fix this
22690e50
SG
1042 */
1043 if (kevp[i].filter != EVFILT_READ &&
4fbb9324 1044 kevp[i].filter != EVFILT_WRITE &&
59737d5e
MP
1045 error != EOPNOTSUPP &&
1046 error != ENODEV) {
23a031e3
SZ
1047 skap->error = error;
1048 *res = -1;
1049 return error;
22690e50
SG
1050 }
1051 break;
1052 }
1053 if (nseldebug)
23a031e3 1054 kprintf("select fd %ju filter %d error %d\n",
c9a1a153 1055 (uintmax_t)kevp[i].ident,
23a031e3 1056 kevp[i].filter, error);
e5857bf7
SG
1057 continue;
1058 }
1059
73c344d3
SG
1060 switch (kevp[i].filter) {
1061 case EVFILT_READ:
e5857bf7
SG
1062 FD_SET(kevp[i].ident, skap->read_set);
1063 break;
73c344d3 1064 case EVFILT_WRITE:
e5857bf7
SG
1065 FD_SET(kevp[i].ident, skap->write_set);
1066 break;
73c344d3 1067 case EVFILT_EXCEPT:
e5857bf7
SG
1068 FD_SET(kevp[i].ident, skap->except_set);
1069 break;
1070 }
1071
8acdf1cf 1072 ++*res;
e5857bf7
SG
1073 }
1074
1075 return (0);
1076}
1077
8acdf1cf
MD
1078/*
1079 * Copy select bits in from userland. Allocate kernel memory if the
1080 * set is large.
1081 */
1082static int
1083getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1084{
1085 int error;
1086
1087 if (in_set) {
1088 if (bytes < sizeof(*tmp_set))
1089 *out_set = tmp_set;
1090 else
1091 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1092 error = copyin(in_set, *out_set, bytes);
1093 } else {
1094 *out_set = NULL;
1095 error = 0;
1096 }
1097 return (error);
1098}
1099
1100/*
1101 * Copy returned select bits back out to userland.
1102 */
1103static int
1104putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1105{
1106 int error;
1107
1108 if (in_set) {
1109 error = copyout(in_set, out_set, bytes);
1110 } else {
1111 error = 0;
1112 }
1113 return (error);
1114}
1115
4979e190
MD
1116static int
1117dotimeout_only(struct timespec *ts)
1118{
1119 return(nanosleep1(ts, NULL));
1120}
1121
ae7cb1b5
NT
1122/*
1123 * Common code for sys_select() and sys_pselect().
1124 *
e5857bf7 1125 * in, out and ex are userland pointers. ts must point to validated
ae7cb1b5
NT
1126 * kernel-side timeout value or NULL for infinite timeout. res must
1127 * point to syscall return value.
1128 */
1129static int
e5857bf7 1130doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
8acdf1cf 1131 struct timespec *ts, int *res)
984263bc 1132{
41c20dac 1133 struct proc *p = curproc;
e5857bf7
SG
1134 struct select_kevent_copyin_args *kap, ka;
1135 int bytes, error;
8acdf1cf
MD
1136 kfd_set read_tmp;
1137 kfd_set write_tmp;
1138 kfd_set except_tmp;
984263bc 1139
8acdf1cf 1140 *res = 0;
ae7cb1b5 1141 if (nd < 0)
984263bc 1142 return (EINVAL);
0c134065 1143 if (nd == 0 && ts)
4979e190
MD
1144 return (dotimeout_only(ts));
1145
679058fb
MD
1146 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1147 nd = p->p_fd->fd_nfiles;
984263bc 1148
e5857bf7
SG
1149 kap = &ka;
1150 kap->lwp = curthread->td_lwp;
1151 kap->num_fds = nd;
1152 kap->proc_fds = 0;
1153 kap->error = 0;
8acdf1cf 1154 kap->active_set = COPYIN_READ;
e6f31a83 1155
8acdf1cf
MD
1156 /*
1157 * Calculate bytes based on the number of __fd_mask[] array entries
1158 * multiplied by the size of __fd_mask.
1159 */
1160 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
984263bc 1161
d0d4a734
MD
1162 /* kap->read_set = NULL; not needed */
1163 kap->write_set = NULL;
1164 kap->except_set = NULL;
1165
8acdf1cf
MD
1166 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1167 if (error == 0)
1168 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1169 if (error == 0)
1170 error = getbits(bytes, except, &kap->except_set, &except_tmp);
e5857bf7 1171 if (error)
8acdf1cf
MD
1172 goto done;
1173
1174 /*
1175 * NOTE: Make sure the max events passed to kern_kevent() is
1176 * effectively unlimited. (nd * 3) accomplishes this.
1177 *
1178 * (*res) continues to increment as returned events are
1179 * loaded in.
1180 */
679058fb 1181 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
8acdf1cf
MD
1182 select_copyin, select_copyout, ts);
1183 if (error == 0)
1184 error = putbits(bytes, kap->read_set, read);
1185 if (error == 0)
1186 error = putbits(bytes, kap->write_set, write);
1187 if (error == 0)
1188 error = putbits(bytes, kap->except_set, except);
984263bc 1189
8acdf1cf 1190 /*
22690e50
SG
1191 * An error from an individual event that should be passed
1192 * back to userland (EBADF)
8acdf1cf
MD
1193 */
1194 if (kap->error)
1195 error = kap->error;
e5857bf7 1196
8acdf1cf
MD
1197 /*
1198 * Clean up.
1199 */
1200done:
1201 if (kap->read_set && kap->read_set != &read_tmp)
1202 kfree(kap->read_set, M_SELECT);
1203 if (kap->write_set && kap->write_set != &write_tmp)
1204 kfree(kap->write_set, M_SELECT);
1205 if (kap->except_set && kap->except_set != &except_tmp)
1206 kfree(kap->except_set, M_SELECT);
1207
22690e50 1208 kap->lwp->lwp_kqueue_serial += kap->num_fds;
73c344d3 1209
e5857bf7 1210 return (error);
984263bc
MD
1211}
1212
1213/*
1214 * Poll system call.
3919ced0 1215 *
7fbfbe29 1216 * MPSAFE
984263bc 1217 */
984263bc 1218int
753fd850 1219sys_poll(struct poll_args *uap)
984263bc 1220{
7fbfbe29
SG
1221 struct timespec ts, *tsp;
1222 int error;
984263bc 1223
ab2eb4eb 1224 if (uap->timeout != INFTIM) {
7fbfbe29
SG
1225 ts.tv_sec = uap->timeout / 1000;
1226 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1227 tsp = &ts;
984263bc 1228 } else {
7fbfbe29 1229 tsp = NULL;
984263bc 1230 }
e6f31a83 1231
7fbfbe29
SG
1232 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1233
984263bc
MD
1234 return (error);
1235}
1236
1237static int
7fbfbe29 1238poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
984263bc 1239{
7fbfbe29
SG
1240 struct poll_kevent_copyin_args *pkap;
1241 struct pollfd *pfd;
1242 struct kevent *kev;
1243 int kev_count;
1244
1245 pkap = (struct poll_kevent_copyin_args *)arg;
1246
1247 while (pkap->pfds < pkap->nfds) {
1248 pfd = &pkap->fds[pkap->pfds];
1249
1250 /* Clear return events */
1251 pfd->revents = 0;
1252
ffdd7c7c
SG
1253 /* Do not check if fd is equal to -1 */
1254 if (pfd->fd == -1) {
1255 ++pkap->pfds;
1256 continue;
1257 }
1258
7fbfbe29
SG
1259 kev_count = 0;
1260 if (pfd->events & (POLLIN | POLLRDNORM))
1261 kev_count++;
1262 if (pfd->events & (POLLOUT | POLLWRNORM))
1263 kev_count++;
1264 if (pfd->events & (POLLPRI | POLLRDBAND))
1265 kev_count++;
1266
1267 if (*events + kev_count > maxevents)
1268 return (0);
1269
fe24d605
MD
1270 /*
1271 * NOTE: A combined serial number and poll array index is
1272 * stored in kev->udata.
1273 */
7fbfbe29 1274 kev = &kevp[*events];
fe24d605 1275 if (pfd->events & (POLLIN | POLLRDNORM)) {
7fbfbe29 1276 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
57b24f4e 1277 NOTE_OLDAPI, 0, (void *)(uintptr_t)
b4372719 1278 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
fe24d605
MD
1279 }
1280 if (pfd->events & (POLLOUT | POLLWRNORM)) {
7fbfbe29 1281 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
57b24f4e 1282 NOTE_OLDAPI, 0, (void *)(uintptr_t)
b4372719 1283 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
fe24d605
MD
1284 }
1285 if (pfd->events & (POLLPRI | POLLRDBAND)) {
7fbfbe29 1286 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
57b24f4e 1287 NOTE_OLDAPI | NOTE_OOB, 0,
b4372719
MD
1288 (void *)(uintptr_t)
1289 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
fe24d605
MD
1290 }
1291
1292 if (nseldebug) {
21ae0f4c
SG
1293 kprintf("poll index %d/%d fd %d events %08x serial %d\n",
1294 pkap->pfds, pkap->nfds-1, pfd->fd, pfd->events,
1295 pkap->lwp->lwp_kqueue_serial);
fe24d605 1296 }
7fbfbe29
SG
1297
1298 ++pkap->pfds;
1299 (*events) += kev_count;
1300 }
1301
1302 return (0);
1303}
1304
1305static int
1306poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1307{
1308 struct poll_kevent_copyin_args *pkap;
1309 struct pollfd *pfd;
1310 struct kevent kev;
8c4ed426 1311 int count_res;
984263bc 1312 int i;
fe24d605 1313 u_int pi;
7fbfbe29
SG
1314
1315 pkap = (struct poll_kevent_copyin_args *)arg;
1316
1317 for (i = 0; i < count; ++i) {
fe24d605
MD
1318 /*
1319 * Extract the poll array index and delete spurious events.
1320 * We can easily tell if the serial number is incorrect
1321 * by checking whether the extracted index is out of range.
1322 */
b4372719
MD
1323 pi = (u_int)(uintptr_t)kevp[i].udata -
1324 (u_int)pkap->lwp->lwp_kqueue_serial;
ffdd7c7c 1325
fe24d605
MD
1326 if (pi >= pkap->nfds) {
1327 kev = kevp[i];
1328 kev.flags = EV_DISABLE|EV_DELETE;
1329 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1330 if (nseldebug)
21ae0f4c
SG
1331 kprintf("poll index %d out of range against serial %d\n",
1332 pi, pkap->lwp->lwp_kqueue_serial);
fe24d605
MD
1333 continue;
1334 }
1335 pfd = &pkap->fds[pi];
1336 if (kevp[i].ident == pfd->fd) {
3c2a46a7
SG
1337 /*
1338 * A single descriptor may generate an error against
8c4ed426
MD
1339 * more than one filter, make sure to set the
1340 * appropriate flags but do not increment (*res)
1341 * more than once.
3c2a46a7 1342 */
8c4ed426 1343 count_res = (pfd->revents == 0);
fe24d605
MD
1344 if (kevp[i].flags & EV_ERROR) {
1345 switch(kevp[i].data) {
fe24d605 1346 case EBADF:
403191da 1347 case POLLNVAL:
fe24d605 1348 /* Bad file descriptor */
8c4ed426 1349 if (count_res)
3c2a46a7 1350 ++*res;
fe24d605 1351 pfd->revents |= POLLNVAL;
7fbfbe29 1352 break;
fe24d605 1353 default:
22690e50
SG
1354 /*
1355 * Poll silently swallows any unknown
1356 * errors except in the case of POLLPRI
1357 * (OOB/urgent data).
2b7d1884
SG
1358 *
1359 * ALWAYS filter out EOPNOTSUPP errors
1360 * from filters, common applications
1361 * set POLLPRI|POLLRDBAND and most
1362 * filters do not support EVFILT_EXCEPT.
59737d5e
MP
1363 *
1364 * We also filter out ENODEV since dev_dkqfilter
1365 * returns ENODEV if EOPNOTSUPP is returned in an
1366 * inner call.
1367 *
1368 * XXX: fix this
22690e50
SG
1369 */
1370 if (kevp[i].filter != EVFILT_READ &&
2b7d1884 1371 kevp[i].filter != EVFILT_WRITE &&
59737d5e
MP
1372 kevp[i].data != EOPNOTSUPP &&
1373 kevp[i].data != ENODEV) {
8c4ed426 1374 if (count_res == 0)
22690e50
SG
1375 ++*res;
1376 pfd->revents |= POLLERR;
1377 }
7fbfbe29
SG
1378 break;
1379 }
b4372719
MD
1380 if (nseldebug) {
1381 kprintf("poll index %d fd %d "
c9a1a153 1382 "filter %d error %jd\n",
fe24d605 1383 pi, pfd->fd,
b4372719 1384 kevp[i].filter,
c9a1a153 1385 (intmax_t)kevp[i].data);
b4372719 1386 }
fe24d605
MD
1387 continue;
1388 }
7fbfbe29 1389
fe24d605
MD
1390 switch (kevp[i].filter) {
1391 case EVFILT_READ:
9a5d20db
MD
1392#if 0
1393 /*
3bcb6e5e 1394 * NODATA on the read side can indicate a
9a5d20db
MD
1395 * half-closed situation and not necessarily
1396 * a disconnect, so depend on the user
1397 * issuing a read() and getting 0 bytes back.
1398 */
3bcb6e5e 1399 if (kevp[i].flags & EV_NODATA)
9a5d20db
MD
1400 pfd->revents |= POLLHUP;
1401#endif
aa622c3d
SZ
1402 if ((kevp[i].flags & EV_EOF) &&
1403 kevp[i].fflags != 0)
1404 pfd->revents |= POLLERR;
484efec7
SG
1405 if (pfd->events & POLLIN)
1406 pfd->revents |= POLLIN;
1407 if (pfd->events & POLLRDNORM)
1408 pfd->revents |= POLLRDNORM;
fe24d605
MD
1409 break;
1410 case EVFILT_WRITE:
9a5d20db
MD
1411 /*
1412 * As per the OpenGroup POLLHUP is mutually
1413 * exclusive with the writability flags. I
1414 * consider this a bit broken but...
1415 *
1416 * In this case a disconnect is implied even
1417 * for a half-closed (write side) situation.
1418 */
aa622c3d 1419 if (kevp[i].flags & EV_EOF) {
9a5d20db 1420 pfd->revents |= POLLHUP;
aa622c3d
SZ
1421 if (kevp[i].fflags != 0)
1422 pfd->revents |= POLLERR;
9a5d20db
MD
1423 } else {
1424 if (pfd->events & POLLOUT)
1425 pfd->revents |= POLLOUT;
1426 if (pfd->events & POLLWRNORM)
1427 pfd->revents |= POLLWRNORM;
1428 }
fe24d605
MD
1429 break;
1430 case EVFILT_EXCEPT:
9a5d20db 1431 /*
3bcb6e5e 1432 * EV_NODATA should never be tagged for this
9a5d20db
MD
1433 * filter.
1434 */
484efec7
SG
1435 if (pfd->events & POLLPRI)
1436 pfd->revents |= POLLPRI;
1437 if (pfd->events & POLLRDBAND)
1438 pfd->revents |= POLLRDBAND;
fe24d605
MD
1439 break;
1440 }
1441
1442 if (nseldebug) {
21ae0f4c
SG
1443 kprintf("poll index %d/%d fd %d revents %08x\n",
1444 pi, pkap->nfds, pfd->fd, pfd->revents);
fe24d605
MD
1445 }
1446
8c4ed426
MD
1447 if (count_res && pfd->revents)
1448 ++*res;
fe24d605 1449 } else {
b4372719 1450 if (nseldebug) {
c9a1a153
SW
1451 kprintf("poll index %d mismatch %ju/%d\n",
1452 pi, (uintmax_t)kevp[i].ident, pfd->fd);
b4372719 1453 }
fe24d605 1454 }
984263bc 1455 }
7fbfbe29 1456
984263bc
MD
1457 return (0);
1458}
1459
7fbfbe29
SG
1460static int
1461dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1462{
7fbfbe29
SG
1463 struct poll_kevent_copyin_args ka;
1464 struct pollfd sfds[64];
679058fb 1465 int bytes;
7fbfbe29
SG
1466 int error;
1467
1468 *res = 0;
1469 if (nfds < 0)
1470 return (EINVAL);
679058fb 1471
0c134065 1472 if (nfds == 0 && ts)
4979e190
MD
1473 return (dotimeout_only(ts));
1474
679058fb
MD
1475 /*
1476 * This is a bit arbitrary but we need to limit internal kmallocs.
1477 */
1478 if (nfds > maxfilesperproc * 2)
1479 nfds = maxfilesperproc * 2;
1480 bytes = sizeof(struct pollfd) * nfds;
7fbfbe29
SG
1481
1482 ka.lwp = curthread->td_lwp;
1483 ka.nfds = nfds;
1484 ka.pfds = 0;
1485 ka.error = 0;
1486
1487 if (ka.nfds < 64)
1488 ka.fds = sfds;
1489 else
1490 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1491
679058fb 1492 error = copyin(fds, ka.fds, bytes);
7fbfbe29 1493 if (error == 0)
3c2a46a7 1494 error = kern_kevent(&ka.lwp->lwp_kqueue, 0x7FFFFFFF, res, &ka,
7fbfbe29
SG
1495 poll_copyin, poll_copyout, ts);
1496
1497 if (error == 0)
679058fb 1498 error = copyout(ka.fds, fds, bytes);
7fbfbe29
SG
1499
1500 if (ka.fds != sfds)
1501 kfree(ka.fds, M_SELECT);
1502
fe24d605
MD
1503 ka.lwp->lwp_kqueue_serial += nfds;
1504
7fbfbe29
SG
1505 return (error);
1506}
1507
8b5c39bb
SG
1508static int
1509socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1510{
1511 return (0);
1512}
1513
1514static int
1515socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1516{
1517 ++*res;
1518 return (0);
1519}
1520
1521extern struct fileops socketops;
6cef7136
MD
1522
1523/*
1524 * NOTE: Callers of socket_wait() must already have a reference on the
1525 * socket.
1526 */
8b5c39bb
SG
1527int
1528socket_wait(struct socket *so, struct timespec *ts, int *res)
1529{
ac62ea3c 1530 struct thread *td = curthread;
8b5c39bb 1531 struct file *fp;
8b5c39bb
SG
1532 struct kqueue kq;
1533 struct kevent kev;
ac62ea3c 1534 int error, fd;
8b5c39bb 1535
ac62ea3c 1536 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
8b5c39bb
SG
1537 return (error);
1538
ac62ea3c
SG
1539 fp->f_type = DTYPE_SOCKET;
1540 fp->f_flag = FREAD | FWRITE;
8b5c39bb
SG
1541 fp->f_ops = &socketops;
1542 fp->f_data = so;
ac62ea3c 1543 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
8b5c39bb 1544
ac62ea3c
SG
1545 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1546 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
8b5c39bb
SG
1547 if ((error = kqueue_register(&kq, &kev)) != 0) {
1548 fdrop(fp);
1549 return (error);
1550 }
1551
1552 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1553 socket_wait_copyout, ts);
ac62ea3c
SG
1554
1555 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
1556 kqueue_register(&kq, &kev);
1557 fp->f_ops = &badfileops;
8b5c39bb
SG
1558 fdrop(fp);
1559
1560 return (error);
1561}
1562
984263bc
MD
1563/*
1564 * OpenBSD poll system call.
1565 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
3919ced0
MD
1566 *
1567 * MPSAFE
984263bc 1568 */
984263bc 1569int
753fd850 1570sys_openbsd_poll(struct openbsd_poll_args *uap)
984263bc 1571{
753fd850 1572 return (sys_poll((struct poll_args *)uap));
984263bc
MD
1573}
1574
1575/*ARGSUSED*/
1576int
b13267a5 1577seltrue(cdev_t dev, int events)
984263bc 1578{
984263bc
MD
1579 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1580}