select: Don't allow unwanted/leftover fds being returned.
[dragonfly.git] / sys / kern / sys_generic.c
blobf10b06aaab2602cbca2d1a95593ede4188674fd2
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.
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.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
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.
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 $
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/event.h>
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>
53 #include <sys/kern_syscall.h>
54 #include <sys/malloc.h>
55 #include <sys/mapped_ioctl.h>
56 #include <sys/poll.h>
57 #include <sys/queue.h>
58 #include <sys/resourcevar.h>
59 #include <sys/socketops.h>
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>
69 #include <sys/file2.h>
70 #include <sys/spinlock2.h>
72 #include <machine/limits.h>
74 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
75 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
76 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
77 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
79 typedef struct kfd_set {
80 fd_mask fds_bits[2];
81 } kfd_set;
83 enum select_copyin_states {
84 COPYIN_READ, COPYIN_WRITE, COPYIN_EXCEPT, COPYIN_DONE };
86 struct select_kevent_copyin_args {
87 kfd_set *read_set;
88 kfd_set *write_set;
89 kfd_set *except_set;
90 kfd_set *oread_set; /* orig set, points into read_set */
91 kfd_set *owrite_set; /* orig set, points into write_set */
92 kfd_set *oexcept_set; /* orig set, points into except_set */
93 int active_set; /* One of select_copyin_states */
94 struct lwp *lwp; /* Pointer to our lwp */
95 int num_fds; /* Number of file descriptors (syscall arg) */
96 int proc_fds; /* Processed fd's (wraps) */
97 int error; /* Returned to userland */
100 struct poll_kevent_copyin_args {
101 struct lwp *lwp;
102 struct pollfd *fds;
103 int nfds;
104 int pfds;
105 int error;
108 static struct lwkt_token mioctl_token = LWKT_TOKEN_INITIALIZER(mioctl_token);
110 static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
111 struct timespec *ts, int *res);
112 static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
113 int *res, int flags);
114 static int dofileread(int, struct file *, struct uio *, int, size_t *);
115 static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
118 * Read system call.
120 * MPSAFE
123 sys_read(struct read_args *uap)
125 struct thread *td = curthread;
126 struct uio auio;
127 struct iovec aiov;
128 int error;
130 if ((ssize_t)uap->nbyte < 0)
131 error = EINVAL;
133 aiov.iov_base = uap->buf;
134 aiov.iov_len = uap->nbyte;
135 auio.uio_iov = &aiov;
136 auio.uio_iovcnt = 1;
137 auio.uio_offset = -1;
138 auio.uio_resid = uap->nbyte;
139 auio.uio_rw = UIO_READ;
140 auio.uio_segflg = UIO_USERSPACE;
141 auio.uio_td = td;
143 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
144 return(error);
148 * Positioned (Pread) read system call
150 * MPSAFE
153 sys_extpread(struct extpread_args *uap)
155 struct thread *td = curthread;
156 struct uio auio;
157 struct iovec aiov;
158 int error;
159 int flags;
161 if ((ssize_t)uap->nbyte < 0)
162 return(EINVAL);
164 aiov.iov_base = uap->buf;
165 aiov.iov_len = uap->nbyte;
166 auio.uio_iov = &aiov;
167 auio.uio_iovcnt = 1;
168 auio.uio_offset = uap->offset;
169 auio.uio_resid = uap->nbyte;
170 auio.uio_rw = UIO_READ;
171 auio.uio_segflg = UIO_USERSPACE;
172 auio.uio_td = td;
174 flags = uap->flags & O_FMASK;
175 if (uap->offset != (off_t)-1)
176 flags |= O_FOFFSET;
178 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
179 return(error);
183 * Scatter read system call.
185 * MPSAFE
188 sys_readv(struct readv_args *uap)
190 struct thread *td = curthread;
191 struct uio auio;
192 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
193 int error;
195 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
196 &auio.uio_resid);
197 if (error)
198 return (error);
199 auio.uio_iov = iov;
200 auio.uio_iovcnt = uap->iovcnt;
201 auio.uio_offset = -1;
202 auio.uio_rw = UIO_READ;
203 auio.uio_segflg = UIO_USERSPACE;
204 auio.uio_td = td;
206 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
208 iovec_free(&iov, aiov);
209 return (error);
214 * Scatter positioned read system call.
216 * MPSAFE
219 sys_extpreadv(struct extpreadv_args *uap)
221 struct thread *td = curthread;
222 struct uio auio;
223 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
224 int error;
225 int flags;
227 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
228 &auio.uio_resid);
229 if (error)
230 return (error);
231 auio.uio_iov = iov;
232 auio.uio_iovcnt = uap->iovcnt;
233 auio.uio_offset = uap->offset;
234 auio.uio_rw = UIO_READ;
235 auio.uio_segflg = UIO_USERSPACE;
236 auio.uio_td = td;
238 flags = uap->flags & O_FMASK;
239 if (uap->offset != (off_t)-1)
240 flags |= O_FOFFSET;
242 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
244 iovec_free(&iov, aiov);
245 return(error);
249 * MPSAFE
252 kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
254 struct thread *td = curthread;
255 struct proc *p = td->td_proc;
256 struct file *fp;
257 int error;
259 KKASSERT(p);
261 fp = holdfp(p->p_fd, fd, FREAD);
262 if (fp == NULL)
263 return (EBADF);
264 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
265 error = ESPIPE;
266 } else {
267 error = dofileread(fd, fp, auio, flags, res);
269 fdrop(fp);
270 return(error);
274 * Common code for readv and preadv that reads data in
275 * from a file using the passed in uio, offset, and flags.
277 * MPALMOSTSAFE - ktrace needs help
279 static int
280 dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
282 int error;
283 size_t len;
284 #ifdef KTRACE
285 struct thread *td = curthread;
286 struct iovec *ktriov = NULL;
287 struct uio ktruio;
288 #endif
290 #ifdef KTRACE
292 * if tracing, save a copy of iovec
294 if (KTRPOINT(td, KTR_GENIO)) {
295 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
297 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
298 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
299 ktruio = *auio;
301 #endif
302 len = auio->uio_resid;
303 error = fo_read(fp, auio, fp->f_cred, flags);
304 if (error) {
305 if (auio->uio_resid != len && (error == ERESTART ||
306 error == EINTR || error == EWOULDBLOCK))
307 error = 0;
309 #ifdef KTRACE
310 if (ktriov != NULL) {
311 if (error == 0) {
312 ktruio.uio_iov = ktriov;
313 ktruio.uio_resid = len - auio->uio_resid;
314 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
316 kfree(ktriov, M_TEMP);
318 #endif
319 if (error == 0)
320 *res = len - auio->uio_resid;
322 return(error);
326 * Write system call
328 * MPSAFE
331 sys_write(struct write_args *uap)
333 struct thread *td = curthread;
334 struct uio auio;
335 struct iovec aiov;
336 int error;
338 if ((ssize_t)uap->nbyte < 0)
339 error = EINVAL;
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;
351 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
353 return(error);
357 * Pwrite system call
359 * MPSAFE
362 sys_extpwrite(struct extpwrite_args *uap)
364 struct thread *td = curthread;
365 struct uio auio;
366 struct iovec aiov;
367 int error;
368 int flags;
370 if ((ssize_t)uap->nbyte < 0)
371 error = EINVAL;
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;
383 flags = uap->flags & O_FMASK;
384 if (uap->offset != (off_t)-1)
385 flags |= O_FOFFSET;
386 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
387 return(error);
391 * MPSAFE
394 sys_writev(struct writev_args *uap)
396 struct thread *td = curthread;
397 struct uio auio;
398 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
399 int error;
401 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
402 &auio.uio_resid);
403 if (error)
404 return (error);
405 auio.uio_iov = iov;
406 auio.uio_iovcnt = uap->iovcnt;
407 auio.uio_offset = -1;
408 auio.uio_rw = UIO_WRITE;
409 auio.uio_segflg = UIO_USERSPACE;
410 auio.uio_td = td;
412 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
414 iovec_free(&iov, aiov);
415 return (error);
420 * Gather positioned write system call
422 * MPSAFE
425 sys_extpwritev(struct extpwritev_args *uap)
427 struct thread *td = curthread;
428 struct uio auio;
429 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
430 int error;
431 int flags;
433 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
434 &auio.uio_resid);
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;
444 flags = uap->flags & O_FMASK;
445 if (uap->offset != (off_t)-1)
446 flags |= O_FOFFSET;
448 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
450 iovec_free(&iov, aiov);
451 return(error);
455 * MPSAFE
458 kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
460 struct thread *td = curthread;
461 struct proc *p = td->td_proc;
462 struct file *fp;
463 int error;
465 KKASSERT(p);
467 fp = holdfp(p->p_fd, fd, FWRITE);
468 if (fp == NULL)
469 return (EBADF);
470 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
471 error = ESPIPE;
472 } else {
473 error = dofilewrite(fd, fp, auio, flags, res);
476 fdrop(fp);
477 return (error);
481 * Common code for writev and pwritev that writes data to
482 * a file using the passed in uio, offset, and flags.
484 * MPALMOSTSAFE - ktrace needs help
486 static int
487 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
489 struct thread *td = curthread;
490 struct lwp *lp = td->td_lwp;
491 int error;
492 size_t len;
493 #ifdef KTRACE
494 struct iovec *ktriov = NULL;
495 struct uio ktruio;
496 #endif
498 #ifdef KTRACE
500 * if tracing, save a copy of iovec and uio
502 if (KTRPOINT(td, KTR_GENIO)) {
503 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
505 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
506 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
507 ktruio = *auio;
509 #endif
510 len = auio->uio_resid;
511 error = fo_write(fp, auio, fp->f_cred, flags);
512 if (error) {
513 if (auio->uio_resid != len && (error == ERESTART ||
514 error == EINTR || error == EWOULDBLOCK))
515 error = 0;
516 /* Socket layer is responsible for issuing SIGPIPE. */
517 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
518 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
520 #ifdef KTRACE
521 if (ktriov != NULL) {
522 if (error == 0) {
523 ktruio.uio_iov = ktriov;
524 ktruio.uio_resid = len - auio->uio_resid;
525 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
527 kfree(ktriov, M_TEMP);
529 #endif
530 if (error == 0)
531 *res = len - auio->uio_resid;
533 return(error);
537 * Ioctl system call
539 * MPSAFE
542 sys_ioctl(struct ioctl_args *uap)
544 int error;
546 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
547 return (error);
550 struct ioctl_map_entry {
551 const char *subsys;
552 struct ioctl_map_range *cmd_ranges;
553 LIST_ENTRY(ioctl_map_entry) entries;
557 * The true heart of all ioctl syscall handlers (native, emulation).
558 * If map != NULL, it will be searched for a matching entry for com,
559 * and appropriate conversions/conversion functions will be utilized.
561 * MPSAFE
564 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
565 struct sysmsg *msg)
567 struct thread *td = curthread;
568 struct proc *p = td->td_proc;
569 struct ucred *cred;
570 struct file *fp;
571 struct ioctl_map_range *iomc = NULL;
572 int error;
573 u_int size;
574 u_long ocom = com;
575 caddr_t data, memp;
576 int tmp;
577 #define STK_PARAMS 128
578 union {
579 char stkbuf[STK_PARAMS];
580 long align;
581 } ubuf;
583 KKASSERT(p);
584 cred = td->td_ucred;
585 memp = NULL;
587 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
588 if (fp == NULL)
589 return(EBADF);
591 if (map != NULL) { /* obey translation map */
592 u_long maskcmd;
593 struct ioctl_map_entry *e;
595 maskcmd = com & map->mask;
597 lwkt_gettoken(&mioctl_token);
598 LIST_FOREACH(e, &map->mapping, entries) {
599 for (iomc = e->cmd_ranges; iomc->start != 0 ||
600 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
601 iomc->mapfunc != NULL;
602 iomc++) {
603 if (maskcmd >= iomc->start &&
604 maskcmd <= iomc->end)
605 break;
608 /* Did we find a match? */
609 if (iomc->start != 0 || iomc->maptocmd != 0 ||
610 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
611 break;
613 lwkt_reltoken(&mioctl_token);
615 if (iomc == NULL ||
616 (iomc->start == 0 && iomc->maptocmd == 0
617 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
618 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
619 map->sys, fd, maskcmd,
620 (int)((maskcmd >> 8) & 0xff),
621 (int)(maskcmd & 0xff));
622 error = EINVAL;
623 goto done;
627 * If it's a non-range one to one mapping, maptocmd should be
628 * correct. If it's a ranged one to one mapping, we pass the
629 * original value of com, and for a range mapped to a different
630 * range, we always need a mapping function to translate the
631 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
633 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
634 com = iomc->maptocmd;
635 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
636 if (iomc->mapfunc != NULL)
637 com = iomc->mapfunc(iomc->start, iomc->end,
638 iomc->start, iomc->end,
639 com, com);
640 } else {
641 if (iomc->mapfunc != NULL) {
642 com = iomc->mapfunc(iomc->start, iomc->end,
643 iomc->maptocmd, iomc->maptoend,
644 com, ocom);
645 } else {
646 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
647 map->sys, fd, maskcmd,
648 (int)((maskcmd >> 8) & 0xff),
649 (int)(maskcmd & 0xff));
650 error = EINVAL;
651 goto done;
656 switch (com) {
657 case FIONCLEX:
658 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
659 goto done;
660 case FIOCLEX:
661 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
662 goto done;
666 * Interpret high order word to find amount of data to be
667 * copied to/from the user's address space.
669 size = IOCPARM_LEN(com);
670 if (size > IOCPARM_MAX) {
671 error = ENOTTY;
672 goto done;
675 if ((com & IOC_VOID) == 0 && size > sizeof(ubuf.stkbuf)) {
676 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
677 data = memp;
678 } else {
679 memp = NULL;
680 data = ubuf.stkbuf;
682 if (com & IOC_VOID) {
683 *(caddr_t *)data = uspc_data;
684 } else if (com & IOC_IN) {
685 if (size != 0) {
686 error = copyin(uspc_data, data, (size_t)size);
687 if (error)
688 goto done;
689 } else {
690 *(caddr_t *)data = uspc_data;
692 } else if ((com & IOC_OUT) != 0 && size) {
694 * Zero the buffer so the user always
695 * gets back something deterministic.
697 bzero(data, (size_t)size);
700 switch (com) {
701 case FIONBIO:
702 if ((tmp = *(int *)data))
703 atomic_set_int(&fp->f_flag, FNONBLOCK);
704 else
705 atomic_clear_int(&fp->f_flag, FNONBLOCK);
706 error = 0;
707 break;
709 case FIOASYNC:
710 if ((tmp = *(int *)data))
711 atomic_set_int(&fp->f_flag, FASYNC);
712 else
713 atomic_clear_int(&fp->f_flag, FASYNC);
714 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
715 break;
717 default:
719 * If there is a override function,
720 * call it instead of directly routing the call
722 if (map != NULL && iomc->wrapfunc != NULL)
723 error = iomc->wrapfunc(fp, com, ocom, data, cred);
724 else
725 error = fo_ioctl(fp, com, data, cred, msg);
727 * Copy any data to user, size was
728 * already set and checked above.
730 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
731 error = copyout(data, uspc_data, (size_t)size);
732 break;
734 done:
735 if (memp != NULL)
736 kfree(memp, M_IOCTLOPS);
737 fdrop(fp);
738 return(error);
742 * MPSAFE
745 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
747 struct ioctl_map_entry *ne;
749 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
750 he->subsys != NULL && *he->subsys != '\0');
752 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP,
753 M_WAITOK | M_ZERO);
755 ne->subsys = he->subsys;
756 ne->cmd_ranges = he->cmd_ranges;
758 lwkt_gettoken(&mioctl_token);
759 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
760 lwkt_reltoken(&mioctl_token);
762 return(0);
766 * MPSAFE
769 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
771 struct ioctl_map_entry *ne;
772 int error = EINVAL;
774 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
776 lwkt_gettoken(&mioctl_token);
777 LIST_FOREACH(ne, &he->map->mapping, entries) {
778 if (ne->cmd_ranges == he->cmd_ranges) {
779 LIST_REMOVE(ne, entries);
780 kfree(ne, M_IOCTLMAP);
781 error = 0;
782 break;
785 lwkt_reltoken(&mioctl_token);
786 return(error);
789 static int nselcoll; /* Select collisions since boot */
790 int selwait;
791 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
792 static int nseldebug;
793 SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
796 * Select system call.
798 * MPSAFE
801 sys_select(struct select_args *uap)
803 struct timeval ktv;
804 struct timespec *ktsp, kts;
805 int error;
808 * Get timeout if any.
810 if (uap->tv != NULL) {
811 error = copyin(uap->tv, &ktv, sizeof (ktv));
812 if (error)
813 return (error);
814 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
815 ktsp = &kts;
816 } else {
817 ktsp = NULL;
821 * Do real work.
823 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
824 &uap->sysmsg_result);
826 return (error);
831 * Pselect system call.
834 sys_pselect(struct pselect_args *uap)
836 struct thread *td = curthread;
837 struct lwp *lp = td->td_lwp;
838 struct timespec *ktsp, kts;
839 sigset_t sigmask;
840 int error;
843 * Get timeout if any.
845 if (uap->ts != NULL) {
846 error = copyin(uap->ts, &kts, sizeof (kts));
847 if (error)
848 return (error);
849 ktsp = &kts;
850 } else {
851 ktsp = NULL;
855 * Install temporary signal mask if any provided.
857 if (uap->sigmask != NULL) {
858 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
859 if (error)
860 return (error);
861 lwkt_gettoken(&lp->lwp_proc->p_token);
862 lp->lwp_oldsigmask = lp->lwp_sigmask;
863 SIG_CANTMASK(sigmask);
864 lp->lwp_sigmask = sigmask;
865 lwkt_reltoken(&lp->lwp_proc->p_token);
869 * Do real job.
871 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
872 &uap->sysmsg_result);
874 if (uap->sigmask != NULL) {
875 lwkt_gettoken(&lp->lwp_proc->p_token);
876 /* doselect() responsible for turning ERESTART into EINTR */
877 KKASSERT(error != ERESTART);
878 if (error == EINTR) {
880 * We can't restore the previous signal mask now
881 * because it could block the signal that interrupted
882 * us. So make a note to restore it after executing
883 * the handler.
885 lp->lwp_flags |= LWP_OLDMASK;
886 } else {
888 * No handler to run. Restore previous mask immediately.
890 lp->lwp_sigmask = lp->lwp_oldsigmask;
892 lwkt_reltoken(&lp->lwp_proc->p_token);
895 return (error);
898 static int
899 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
901 struct select_kevent_copyin_args *skap = NULL;
902 struct kevent *kev;
903 int fd;
904 kfd_set *fdp = NULL;
905 short filter = 0;
906 u_int fflags = 0;
908 skap = (struct select_kevent_copyin_args *)arg;
910 if (*events == maxevents)
911 return (0);
913 while (skap->active_set < COPYIN_DONE) {
914 switch (skap->active_set) {
915 case COPYIN_READ:
917 * Register descriptors for the read filter
919 fdp = skap->read_set;
920 filter = EVFILT_READ;
921 fflags = NOTE_OLDAPI;
922 if (fdp)
923 break;
924 ++skap->active_set;
925 skap->proc_fds = 0;
926 /* fall through */
927 case COPYIN_WRITE:
929 * Register descriptors for the write filter
931 fdp = skap->write_set;
932 filter = EVFILT_WRITE;
933 fflags = NOTE_OLDAPI;
934 if (fdp)
935 break;
936 ++skap->active_set;
937 skap->proc_fds = 0;
938 /* fall through */
939 case COPYIN_EXCEPT:
941 * Register descriptors for the exception filter
943 fdp = skap->except_set;
944 filter = EVFILT_EXCEPT;
945 fflags = NOTE_OLDAPI | NOTE_OOB;
946 if (fdp)
947 break;
948 ++skap->active_set;
949 skap->proc_fds = 0;
950 /* fall through */
951 case COPYIN_DONE:
953 * Nothing left to register
955 return(0);
956 /* NOT REACHED */
959 while (skap->proc_fds < skap->num_fds) {
960 fd = skap->proc_fds;
961 if (FD_ISSET(fd, fdp)) {
962 kev = &kevp[*events];
963 EV_SET(kev, fd, filter,
964 EV_ADD|EV_ENABLE,
965 fflags, 0,
966 (void *)(uintptr_t)
967 skap->lwp->lwp_kqueue_serial);
968 FD_CLR(fd, fdp);
969 ++*events;
971 if (nseldebug)
972 kprintf("select fd %d filter %d serial %d\n",
973 fd, filter, skap->lwp->lwp_kqueue_serial);
975 ++skap->proc_fds;
976 if (*events == maxevents)
977 return (0);
979 skap->active_set++;
980 skap->proc_fds = 0;
983 return (0);
986 static int
987 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
989 struct select_kevent_copyin_args *skap;
990 struct kevent kev;
991 int i = 0;
993 skap = (struct select_kevent_copyin_args *)arg;
995 for (i = 0; i < count; ++i) {
996 kfd_set *fd_set = NULL;
999 * Filter out and delete spurious events
1001 if ((u_int)(uintptr_t)kevp[i].udata !=
1002 skap->lwp->lwp_kqueue_serial) {
1003 if (nseldebug) {
1004 kprintf("select fd %ju mismatched serial %d\n",
1005 (uintmax_t)kevp[i].ident,
1006 skap->lwp->lwp_kqueue_serial);
1008 } else {
1009 switch (kevp[i].filter) {
1010 case EVFILT_READ:
1011 if (__predict_true(skap->oread_set != NULL &&
1012 FD_ISSET(kevp[i].ident, skap->oread_set)))
1013 fd_set = skap->read_set;
1014 break;
1016 case EVFILT_WRITE:
1017 if (__predict_true(skap->owrite_set != NULL &&
1018 FD_ISSET(kevp[i].ident, skap->owrite_set)))
1019 fd_set = skap->write_set;
1020 break;
1022 case EVFILT_EXCEPT:
1023 if (__predict_true(skap->oexcept_set != NULL &&
1024 FD_ISSET(kevp[i].ident, skap->oexcept_set)))
1025 fd_set = skap->except_set;
1026 break;
1028 if (__predict_false(fd_set == NULL) && nseldebug) {
1029 kprintf("select leftover fd %ju, "
1030 "serial wrapped\n",
1031 (uintmax_t)kevp[i].ident);
1034 if (fd_set == NULL) {
1035 kev = kevp[i];
1036 kev.flags = EV_DISABLE|EV_DELETE;
1037 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
1038 continue;
1042 * Handle errors
1044 if (kevp[i].flags & EV_ERROR) {
1045 int error = kevp[i].data;
1047 switch (error) {
1048 case EBADF:
1050 * A bad file descriptor is considered a
1051 * fatal error for select, bail out.
1053 skap->error = error;
1054 *res = -1;
1055 return error;
1057 default:
1059 * Select silently swallows any unknown errors
1060 * for descriptors in the read or write sets.
1062 * ALWAYS filter out EOPNOTSUPP errors from
1063 * filters (at least until all filters support
1064 * EVFILT_EXCEPT)
1066 * We also filter out ENODEV since dev_dkqfilter
1067 * returns ENODEV if EOPNOTSUPP is returned in an
1068 * inner call.
1070 * XXX: fix this
1072 if (kevp[i].filter != EVFILT_READ &&
1073 kevp[i].filter != EVFILT_WRITE &&
1074 error != EOPNOTSUPP &&
1075 error != ENODEV) {
1076 skap->error = error;
1077 *res = -1;
1078 return error;
1080 break;
1082 if (nseldebug)
1083 kprintf("select fd %ju filter %d error %d\n",
1084 (uintmax_t)kevp[i].ident,
1085 kevp[i].filter, error);
1086 continue;
1088 FD_SET(kevp[i].ident, fd_set);
1090 ++*res;
1093 return (0);
1097 * Copy select bits in from userland. Allocate kernel memory if the
1098 * set is large.
1100 static int
1101 getbits(int bytes, fd_set *in_set, kfd_set **out_set0, kfd_set **orig_set0,
1102 kfd_set *tmp_set)
1104 kfd_set *out_set = NULL, *orig_set = NULL;
1105 int error = 0;
1107 if (in_set) {
1108 if (bytes < sizeof(kfd_set)) {
1109 out_set = tmp_set;
1110 orig_set = tmp_set + 1;
1111 } else {
1112 out_set = kmalloc(bytes * 2, M_SELECT, M_WAITOK);
1113 orig_set = (kfd_set *)(((uint8_t *)out_set) + bytes);
1115 error = copyin(in_set, out_set, bytes);
1116 if (!error)
1117 memcpy(orig_set, out_set, bytes);
1119 *out_set0 = out_set;
1120 *orig_set0 = orig_set;
1121 return (error);
1125 * Copy returned select bits back out to userland.
1127 static int
1128 putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1130 int error;
1132 if (in_set) {
1133 error = copyout(in_set, out_set, bytes);
1134 } else {
1135 error = 0;
1137 return (error);
1140 static int
1141 dotimeout_only(struct timespec *ts)
1143 return(nanosleep1(ts, NULL));
1147 * Common code for sys_select() and sys_pselect().
1149 * in, out and ex are userland pointers. ts must point to validated
1150 * kernel-side timeout value or NULL for infinite timeout. res must
1151 * point to syscall return value.
1153 static int
1154 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1155 struct timespec *ts, int *res)
1157 struct proc *p = curproc;
1158 struct select_kevent_copyin_args *kap, ka;
1159 int bytes, error;
1160 kfd_set read_tmp[2];
1161 kfd_set write_tmp[2];
1162 kfd_set except_tmp[2];
1164 *res = 0;
1165 if (nd < 0)
1166 return (EINVAL);
1167 if (nd == 0 && ts)
1168 return (dotimeout_only(ts));
1170 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1171 nd = p->p_fd->fd_nfiles;
1173 kap = &ka;
1174 kap->lwp = curthread->td_lwp;
1175 kap->num_fds = nd;
1176 kap->proc_fds = 0;
1177 kap->error = 0;
1178 kap->active_set = COPYIN_READ;
1181 * Calculate bytes based on the number of __fd_mask[] array entries
1182 * multiplied by the size of __fd_mask.
1184 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1186 /* kap->read_set = NULL; not needed */
1187 kap->write_set = NULL;
1188 kap->except_set = NULL;
1190 error = getbits(bytes, read, &kap->read_set, &kap->oread_set, read_tmp);
1191 if (error == 0) {
1192 error = getbits(bytes, write, &kap->write_set, &kap->owrite_set,
1193 write_tmp);
1195 if (error == 0) {
1196 error = getbits(bytes, except, &kap->except_set,
1197 &kap->oexcept_set, except_tmp);
1199 if (error)
1200 goto done;
1203 * NOTE: Make sure the max events passed to kern_kevent() is
1204 * effectively unlimited. (nd * 3) accomplishes this.
1206 * (*res) continues to increment as returned events are
1207 * loaded in.
1209 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1210 select_copyin, select_copyout, ts, 0);
1211 if (error == 0)
1212 error = putbits(bytes, kap->read_set, read);
1213 if (error == 0)
1214 error = putbits(bytes, kap->write_set, write);
1215 if (error == 0)
1216 error = putbits(bytes, kap->except_set, except);
1219 * An error from an individual event that should be passed
1220 * back to userland (EBADF)
1222 if (kap->error)
1223 error = kap->error;
1226 * Clean up.
1228 done:
1229 if (kap->read_set && kap->read_set != read_tmp)
1230 kfree(kap->read_set, M_SELECT);
1231 if (kap->write_set && kap->write_set != write_tmp)
1232 kfree(kap->write_set, M_SELECT);
1233 if (kap->except_set && kap->except_set != except_tmp)
1234 kfree(kap->except_set, M_SELECT);
1236 kap->lwp->lwp_kqueue_serial += kap->num_fds;
1238 return (error);
1242 * Poll system call.
1244 * MPSAFE
1247 sys_poll(struct poll_args *uap)
1249 struct timespec ts, *tsp;
1250 int error;
1252 if (uap->timeout != INFTIM) {
1253 if (uap->timeout < 0)
1254 return (EINVAL);
1255 ts.tv_sec = uap->timeout / 1000;
1256 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1257 tsp = &ts;
1258 } else {
1259 tsp = NULL;
1262 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result, 0);
1264 return (error);
1268 * Ppoll system call.
1270 * MPSAFE
1273 sys_ppoll(struct ppoll_args *uap)
1275 struct thread *td = curthread;
1276 struct lwp *lp = td->td_lwp;
1277 struct timespec *ktsp, kts;
1278 sigset_t sigmask;
1279 int error;
1282 * Get timeout if any.
1284 if (uap->ts != NULL) {
1285 error = copyin(uap->ts, &kts, sizeof (kts));
1286 if (error)
1287 return (error);
1288 ktsp = &kts;
1289 } else {
1290 ktsp = NULL;
1294 * Install temporary signal mask if any provided.
1296 if (uap->sigmask != NULL) {
1297 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
1298 if (error)
1299 return (error);
1300 lwkt_gettoken(&lp->lwp_proc->p_token);
1301 lp->lwp_oldsigmask = lp->lwp_sigmask;
1302 SIG_CANTMASK(sigmask);
1303 lp->lwp_sigmask = sigmask;
1304 lwkt_reltoken(&lp->lwp_proc->p_token);
1307 error = dopoll(uap->nfds, uap->fds, ktsp, &uap->sysmsg_result,
1308 ktsp != NULL ? KEVENT_TIMEOUT_PRECISE : 0);
1310 if (uap->sigmask != NULL) {
1311 lwkt_gettoken(&lp->lwp_proc->p_token);
1312 /* dopoll() responsible for turning ERESTART into EINTR */
1313 KKASSERT(error != ERESTART);
1314 if (error == EINTR) {
1316 * We can't restore the previous signal mask now
1317 * because it could block the signal that interrupted
1318 * us. So make a note to restore it after executing
1319 * the handler.
1321 lp->lwp_flags |= LWP_OLDMASK;
1322 } else {
1324 * No handler to run. Restore previous mask immediately.
1326 lp->lwp_sigmask = lp->lwp_oldsigmask;
1328 lwkt_reltoken(&lp->lwp_proc->p_token);
1331 return (error);
1334 static int
1335 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1337 struct poll_kevent_copyin_args *pkap;
1338 struct pollfd *pfd;
1339 struct kevent *kev;
1340 int kev_count;
1342 pkap = (struct poll_kevent_copyin_args *)arg;
1344 while (pkap->pfds < pkap->nfds) {
1345 pfd = &pkap->fds[pkap->pfds];
1347 /* Clear return events */
1348 pfd->revents = 0;
1350 /* Do not check if fd is equal to -1 */
1351 if (pfd->fd == -1) {
1352 ++pkap->pfds;
1353 continue;
1356 kev_count = 0;
1357 if (pfd->events & (POLLIN | POLLRDNORM))
1358 kev_count++;
1359 if (pfd->events & (POLLOUT | POLLWRNORM))
1360 kev_count++;
1361 if (pfd->events & (POLLPRI | POLLRDBAND))
1362 kev_count++;
1364 if (*events + kev_count > maxevents)
1365 return (0);
1368 * NOTE: A combined serial number and poll array index is
1369 * stored in kev->udata.
1371 kev = &kevp[*events];
1372 if (pfd->events & (POLLIN | POLLRDNORM)) {
1373 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1374 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1375 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1377 if (pfd->events & (POLLOUT | POLLWRNORM)) {
1378 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1379 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1380 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1382 if (pfd->events & (POLLPRI | POLLRDBAND)) {
1383 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1384 NOTE_OLDAPI | NOTE_OOB, 0,
1385 (void *)(uintptr_t)
1386 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1389 if (nseldebug) {
1390 kprintf("poll index %d/%d fd %d events %08x serial %d\n",
1391 pkap->pfds, pkap->nfds-1, pfd->fd, pfd->events,
1392 pkap->lwp->lwp_kqueue_serial);
1395 ++pkap->pfds;
1396 (*events) += kev_count;
1399 return (0);
1402 static int
1403 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1405 struct poll_kevent_copyin_args *pkap;
1406 struct pollfd *pfd;
1407 struct kevent kev;
1408 int count_res;
1409 int i;
1410 u_int pi;
1412 pkap = (struct poll_kevent_copyin_args *)arg;
1414 for (i = 0; i < count; ++i) {
1416 * Extract the poll array index and delete spurious events.
1417 * We can easily tell if the serial number is incorrect
1418 * by checking whether the extracted index is out of range.
1420 pi = (u_int)(uintptr_t)kevp[i].udata -
1421 (u_int)pkap->lwp->lwp_kqueue_serial;
1423 if (pi >= pkap->nfds) {
1424 kev = kevp[i];
1425 kev.flags = EV_DISABLE|EV_DELETE;
1426 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1427 if (nseldebug)
1428 kprintf("poll index %d out of range against serial %d\n",
1429 pi, pkap->lwp->lwp_kqueue_serial);
1430 continue;
1432 pfd = &pkap->fds[pi];
1433 if (kevp[i].ident == pfd->fd) {
1435 * A single descriptor may generate an error against
1436 * more than one filter, make sure to set the
1437 * appropriate flags but do not increment (*res)
1438 * more than once.
1440 count_res = (pfd->revents == 0);
1441 if (kevp[i].flags & EV_ERROR) {
1442 switch(kevp[i].data) {
1443 case EBADF:
1444 case POLLNVAL:
1445 /* Bad file descriptor */
1446 if (count_res)
1447 ++*res;
1448 pfd->revents |= POLLNVAL;
1449 break;
1450 default:
1452 * Poll silently swallows any unknown
1453 * errors except in the case of POLLPRI
1454 * (OOB/urgent data).
1456 * ALWAYS filter out EOPNOTSUPP errors
1457 * from filters, common applications
1458 * set POLLPRI|POLLRDBAND and most
1459 * filters do not support EVFILT_EXCEPT.
1461 * We also filter out ENODEV since dev_dkqfilter
1462 * returns ENODEV if EOPNOTSUPP is returned in an
1463 * inner call.
1465 * XXX: fix this
1467 if (kevp[i].filter != EVFILT_READ &&
1468 kevp[i].filter != EVFILT_WRITE &&
1469 kevp[i].data != EOPNOTSUPP &&
1470 kevp[i].data != ENODEV) {
1471 if (count_res == 0)
1472 ++*res;
1473 pfd->revents |= POLLERR;
1475 break;
1477 if (nseldebug) {
1478 kprintf("poll index %d fd %d "
1479 "filter %d error %jd\n",
1480 pi, pfd->fd,
1481 kevp[i].filter,
1482 (intmax_t)kevp[i].data);
1484 continue;
1487 switch (kevp[i].filter) {
1488 case EVFILT_READ:
1489 #if 0
1491 * NODATA on the read side can indicate a
1492 * half-closed situation and not necessarily
1493 * a disconnect, so depend on the user
1494 * issuing a read() and getting 0 bytes back.
1496 if (kevp[i].flags & EV_NODATA)
1497 pfd->revents |= POLLHUP;
1498 #endif
1499 if ((kevp[i].flags & EV_EOF) &&
1500 kevp[i].fflags != 0)
1501 pfd->revents |= POLLERR;
1502 if (pfd->events & POLLIN)
1503 pfd->revents |= POLLIN;
1504 if (pfd->events & POLLRDNORM)
1505 pfd->revents |= POLLRDNORM;
1506 break;
1507 case EVFILT_WRITE:
1509 * As per the OpenGroup POLLHUP is mutually
1510 * exclusive with the writability flags. I
1511 * consider this a bit broken but...
1513 * In this case a disconnect is implied even
1514 * for a half-closed (write side) situation.
1516 if (kevp[i].flags & EV_EOF) {
1517 pfd->revents |= POLLHUP;
1518 if (kevp[i].fflags != 0)
1519 pfd->revents |= POLLERR;
1520 } else {
1521 if (pfd->events & POLLOUT)
1522 pfd->revents |= POLLOUT;
1523 if (pfd->events & POLLWRNORM)
1524 pfd->revents |= POLLWRNORM;
1526 break;
1527 case EVFILT_EXCEPT:
1529 * EV_NODATA should never be tagged for this
1530 * filter.
1532 if (pfd->events & POLLPRI)
1533 pfd->revents |= POLLPRI;
1534 if (pfd->events & POLLRDBAND)
1535 pfd->revents |= POLLRDBAND;
1536 break;
1539 if (nseldebug) {
1540 kprintf("poll index %d/%d fd %d revents %08x\n",
1541 pi, pkap->nfds, pfd->fd, pfd->revents);
1544 if (count_res && pfd->revents)
1545 ++*res;
1546 } else {
1547 if (nseldebug) {
1548 kprintf("poll index %d mismatch %ju/%d\n",
1549 pi, (uintmax_t)kevp[i].ident, pfd->fd);
1554 return (0);
1557 static int
1558 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res, int flags)
1560 struct poll_kevent_copyin_args ka;
1561 struct pollfd sfds[64];
1562 int bytes;
1563 int error;
1565 *res = 0;
1566 if (nfds < 0)
1567 return (EINVAL);
1569 if (nfds == 0 && ts)
1570 return (dotimeout_only(ts));
1573 * This is a bit arbitrary but we need to limit internal kmallocs.
1575 if (nfds > maxfilesperproc * 2)
1576 nfds = maxfilesperproc * 2;
1577 bytes = sizeof(struct pollfd) * nfds;
1579 ka.lwp = curthread->td_lwp;
1580 ka.nfds = nfds;
1581 ka.pfds = 0;
1582 ka.error = 0;
1584 if (ka.nfds < 64)
1585 ka.fds = sfds;
1586 else
1587 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1589 error = copyin(fds, ka.fds, bytes);
1590 if (error == 0)
1591 error = kern_kevent(&ka.lwp->lwp_kqueue, 0x7FFFFFFF, res, &ka,
1592 poll_copyin, poll_copyout, ts, flags);
1594 if (error == 0)
1595 error = copyout(ka.fds, fds, bytes);
1597 if (ka.fds != sfds)
1598 kfree(ka.fds, M_SELECT);
1600 ka.lwp->lwp_kqueue_serial += nfds;
1602 return (error);
1605 static int
1606 socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1608 return (0);
1611 static int
1612 socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1614 ++*res;
1615 return (0);
1618 extern struct fileops socketops;
1621 * NOTE: Callers of socket_wait() must already have a reference on the
1622 * socket.
1625 socket_wait(struct socket *so, struct timespec *ts, int *res)
1627 struct thread *td = curthread;
1628 struct file *fp;
1629 struct kqueue kq;
1630 struct kevent kev;
1631 int error, fd;
1633 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
1634 return (error);
1636 fp->f_type = DTYPE_SOCKET;
1637 fp->f_flag = FREAD | FWRITE;
1638 fp->f_ops = &socketops;
1639 fp->f_data = so;
1640 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
1641 fsetfdflags(td->td_proc->p_fd, fd, UF_EXCLOSE);
1643 bzero(&kq, sizeof(kq));
1644 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1645 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
1646 if ((error = kqueue_register(&kq, &kev)) != 0) {
1647 fdrop(fp);
1648 return (error);
1651 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1652 socket_wait_copyout, ts, 0);
1654 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE|EV_DISABLE, 0, 0, NULL);
1655 kqueue_register(&kq, &kev);
1656 fp->f_ops = &badfileops;
1657 fdrop(fp);
1659 return (error);
1663 * OpenBSD poll system call.
1664 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1666 * MPSAFE
1669 sys_openbsd_poll(struct openbsd_poll_args *uap)
1671 return (sys_poll((struct poll_args *)uap));
1674 /*ARGSUSED*/
1676 seltrue(cdev_t dev, int events)
1678 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));