kernel - Incidental MPLOCK removal
[dragonfly.git] / sys / kern / sys_generic.c
blob5cc2fd136e847d70ee9d28e237c924e219546a3f
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 int active_set; /* One of select_copyin_states */
91 struct lwp *lwp; /* Pointer to our lwp */
92 int num_fds; /* Number of file descriptors (syscall arg) */
93 int proc_fds; /* Processed fd's (wraps) */
94 int error; /* Returned to userland */
97 struct poll_kevent_copyin_args {
98 struct lwp *lwp;
99 struct pollfd *fds;
100 int nfds;
101 int pfds;
102 int error;
105 static struct lwkt_token mioctl_token = LWKT_TOKEN_INITIALIZER(mioctl_token);
107 static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
108 struct timespec *ts, int *res);
109 static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
110 int *res, int flags);
111 static int dofileread(int, struct file *, struct uio *, int, size_t *);
112 static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
115 * Read system call.
117 * MPSAFE
120 sys_read(struct read_args *uap)
122 struct thread *td = curthread;
123 struct uio auio;
124 struct iovec aiov;
125 int error;
127 if ((ssize_t)uap->nbyte < 0)
128 error = EINVAL;
130 aiov.iov_base = uap->buf;
131 aiov.iov_len = uap->nbyte;
132 auio.uio_iov = &aiov;
133 auio.uio_iovcnt = 1;
134 auio.uio_offset = -1;
135 auio.uio_resid = uap->nbyte;
136 auio.uio_rw = UIO_READ;
137 auio.uio_segflg = UIO_USERSPACE;
138 auio.uio_td = td;
140 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
141 return(error);
145 * Positioned (Pread) read system call
147 * MPSAFE
150 sys_extpread(struct extpread_args *uap)
152 struct thread *td = curthread;
153 struct uio auio;
154 struct iovec aiov;
155 int error;
156 int flags;
158 if ((ssize_t)uap->nbyte < 0)
159 return(EINVAL);
161 aiov.iov_base = uap->buf;
162 aiov.iov_len = uap->nbyte;
163 auio.uio_iov = &aiov;
164 auio.uio_iovcnt = 1;
165 auio.uio_offset = uap->offset;
166 auio.uio_resid = uap->nbyte;
167 auio.uio_rw = UIO_READ;
168 auio.uio_segflg = UIO_USERSPACE;
169 auio.uio_td = td;
171 flags = uap->flags & O_FMASK;
172 if (uap->offset != (off_t)-1)
173 flags |= O_FOFFSET;
175 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
176 return(error);
180 * Scatter read system call.
182 * MPSAFE
185 sys_readv(struct readv_args *uap)
187 struct thread *td = curthread;
188 struct uio auio;
189 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
190 int error;
192 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
193 &auio.uio_resid);
194 if (error)
195 return (error);
196 auio.uio_iov = iov;
197 auio.uio_iovcnt = uap->iovcnt;
198 auio.uio_offset = -1;
199 auio.uio_rw = UIO_READ;
200 auio.uio_segflg = UIO_USERSPACE;
201 auio.uio_td = td;
203 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
205 iovec_free(&iov, aiov);
206 return (error);
211 * Scatter positioned read system call.
213 * MPSAFE
216 sys_extpreadv(struct extpreadv_args *uap)
218 struct thread *td = curthread;
219 struct uio auio;
220 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
221 int error;
222 int flags;
224 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
225 &auio.uio_resid);
226 if (error)
227 return (error);
228 auio.uio_iov = iov;
229 auio.uio_iovcnt = uap->iovcnt;
230 auio.uio_offset = uap->offset;
231 auio.uio_rw = UIO_READ;
232 auio.uio_segflg = UIO_USERSPACE;
233 auio.uio_td = td;
235 flags = uap->flags & O_FMASK;
236 if (uap->offset != (off_t)-1)
237 flags |= O_FOFFSET;
239 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
241 iovec_free(&iov, aiov);
242 return(error);
246 * MPSAFE
249 kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
251 struct thread *td = curthread;
252 struct proc *p = td->td_proc;
253 struct file *fp;
254 int error;
256 KKASSERT(p);
258 fp = holdfp(p->p_fd, fd, FREAD);
259 if (fp == NULL)
260 return (EBADF);
261 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
262 error = ESPIPE;
263 } else {
264 error = dofileread(fd, fp, auio, flags, res);
266 fdrop(fp);
267 return(error);
271 * Common code for readv and preadv that reads data in
272 * from a file using the passed in uio, offset, and flags.
274 * MPALMOSTSAFE - ktrace needs help
276 static int
277 dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
279 int error;
280 size_t len;
281 #ifdef KTRACE
282 struct thread *td = curthread;
283 struct iovec *ktriov = NULL;
284 struct uio ktruio;
285 #endif
287 #ifdef KTRACE
289 * if tracing, save a copy of iovec
291 if (KTRPOINT(td, KTR_GENIO)) {
292 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
294 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
295 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
296 ktruio = *auio;
298 #endif
299 len = auio->uio_resid;
300 error = fo_read(fp, auio, fp->f_cred, flags);
301 if (error) {
302 if (auio->uio_resid != len && (error == ERESTART ||
303 error == EINTR || error == EWOULDBLOCK))
304 error = 0;
306 #ifdef KTRACE
307 if (ktriov != NULL) {
308 if (error == 0) {
309 ktruio.uio_iov = ktriov;
310 ktruio.uio_resid = len - auio->uio_resid;
311 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
313 kfree(ktriov, M_TEMP);
315 #endif
316 if (error == 0)
317 *res = len - auio->uio_resid;
319 return(error);
323 * Write system call
325 * MPSAFE
328 sys_write(struct write_args *uap)
330 struct thread *td = curthread;
331 struct uio auio;
332 struct iovec aiov;
333 int error;
335 if ((ssize_t)uap->nbyte < 0)
336 error = EINVAL;
338 aiov.iov_base = (void *)(uintptr_t)uap->buf;
339 aiov.iov_len = uap->nbyte;
340 auio.uio_iov = &aiov;
341 auio.uio_iovcnt = 1;
342 auio.uio_offset = -1;
343 auio.uio_resid = uap->nbyte;
344 auio.uio_rw = UIO_WRITE;
345 auio.uio_segflg = UIO_USERSPACE;
346 auio.uio_td = td;
348 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
350 return(error);
354 * Pwrite system call
356 * MPSAFE
359 sys_extpwrite(struct extpwrite_args *uap)
361 struct thread *td = curthread;
362 struct uio auio;
363 struct iovec aiov;
364 int error;
365 int flags;
367 if ((ssize_t)uap->nbyte < 0)
368 error = EINVAL;
370 aiov.iov_base = (void *)(uintptr_t)uap->buf;
371 aiov.iov_len = uap->nbyte;
372 auio.uio_iov = &aiov;
373 auio.uio_iovcnt = 1;
374 auio.uio_offset = uap->offset;
375 auio.uio_resid = uap->nbyte;
376 auio.uio_rw = UIO_WRITE;
377 auio.uio_segflg = UIO_USERSPACE;
378 auio.uio_td = td;
380 flags = uap->flags & O_FMASK;
381 if (uap->offset != (off_t)-1)
382 flags |= O_FOFFSET;
383 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
384 return(error);
388 * MPSAFE
391 sys_writev(struct writev_args *uap)
393 struct thread *td = curthread;
394 struct uio auio;
395 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
396 int error;
398 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
399 &auio.uio_resid);
400 if (error)
401 return (error);
402 auio.uio_iov = iov;
403 auio.uio_iovcnt = uap->iovcnt;
404 auio.uio_offset = -1;
405 auio.uio_rw = UIO_WRITE;
406 auio.uio_segflg = UIO_USERSPACE;
407 auio.uio_td = td;
409 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
411 iovec_free(&iov, aiov);
412 return (error);
417 * Gather positioned write system call
419 * MPSAFE
422 sys_extpwritev(struct extpwritev_args *uap)
424 struct thread *td = curthread;
425 struct uio auio;
426 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
427 int error;
428 int flags;
430 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
431 &auio.uio_resid);
432 if (error)
433 return (error);
434 auio.uio_iov = iov;
435 auio.uio_iovcnt = uap->iovcnt;
436 auio.uio_offset = uap->offset;
437 auio.uio_rw = UIO_WRITE;
438 auio.uio_segflg = UIO_USERSPACE;
439 auio.uio_td = td;
441 flags = uap->flags & O_FMASK;
442 if (uap->offset != (off_t)-1)
443 flags |= O_FOFFSET;
445 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
447 iovec_free(&iov, aiov);
448 return(error);
452 * MPSAFE
455 kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
457 struct thread *td = curthread;
458 struct proc *p = td->td_proc;
459 struct file *fp;
460 int error;
462 KKASSERT(p);
464 fp = holdfp(p->p_fd, fd, FWRITE);
465 if (fp == NULL)
466 return (EBADF);
467 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
468 error = ESPIPE;
469 } else {
470 error = dofilewrite(fd, fp, auio, flags, res);
473 fdrop(fp);
474 return (error);
478 * Common code for writev and pwritev that writes data to
479 * a file using the passed in uio, offset, and flags.
481 * MPALMOSTSAFE - ktrace needs help
483 static int
484 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
486 struct thread *td = curthread;
487 struct lwp *lp = td->td_lwp;
488 int error;
489 size_t len;
490 #ifdef KTRACE
491 struct iovec *ktriov = NULL;
492 struct uio ktruio;
493 #endif
495 #ifdef KTRACE
497 * if tracing, save a copy of iovec and uio
499 if (KTRPOINT(td, KTR_GENIO)) {
500 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
502 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
503 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
504 ktruio = *auio;
506 #endif
507 len = auio->uio_resid;
508 error = fo_write(fp, auio, fp->f_cred, flags);
509 if (error) {
510 if (auio->uio_resid != len && (error == ERESTART ||
511 error == EINTR || error == EWOULDBLOCK))
512 error = 0;
513 /* Socket layer is responsible for issuing SIGPIPE. */
514 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
515 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
517 #ifdef KTRACE
518 if (ktriov != NULL) {
519 if (error == 0) {
520 ktruio.uio_iov = ktriov;
521 ktruio.uio_resid = len - auio->uio_resid;
522 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
524 kfree(ktriov, M_TEMP);
526 #endif
527 if (error == 0)
528 *res = len - auio->uio_resid;
530 return(error);
534 * Ioctl system call
536 * MPSAFE
539 sys_ioctl(struct ioctl_args *uap)
541 int error;
543 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
544 return (error);
547 struct ioctl_map_entry {
548 const char *subsys;
549 struct ioctl_map_range *cmd_ranges;
550 LIST_ENTRY(ioctl_map_entry) entries;
554 * The true heart of all ioctl syscall handlers (native, emulation).
555 * If map != NULL, it will be searched for a matching entry for com,
556 * and appropriate conversions/conversion functions will be utilized.
558 * MPSAFE
561 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
562 struct sysmsg *msg)
564 struct thread *td = curthread;
565 struct proc *p = td->td_proc;
566 struct ucred *cred;
567 struct file *fp;
568 struct ioctl_map_range *iomc = NULL;
569 int error;
570 u_int size;
571 u_long ocom = com;
572 caddr_t data, memp;
573 int tmp;
574 #define STK_PARAMS 128
575 union {
576 char stkbuf[STK_PARAMS];
577 long align;
578 } ubuf;
580 KKASSERT(p);
581 cred = td->td_ucred;
582 memp = NULL;
584 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
585 if (fp == NULL)
586 return(EBADF);
588 if (map != NULL) { /* obey translation map */
589 u_long maskcmd;
590 struct ioctl_map_entry *e;
592 maskcmd = com & map->mask;
594 lwkt_gettoken(&mioctl_token);
595 LIST_FOREACH(e, &map->mapping, entries) {
596 for (iomc = e->cmd_ranges; iomc->start != 0 ||
597 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
598 iomc->mapfunc != NULL;
599 iomc++) {
600 if (maskcmd >= iomc->start &&
601 maskcmd <= iomc->end)
602 break;
605 /* Did we find a match? */
606 if (iomc->start != 0 || iomc->maptocmd != 0 ||
607 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
608 break;
610 lwkt_reltoken(&mioctl_token);
612 if (iomc == NULL ||
613 (iomc->start == 0 && iomc->maptocmd == 0
614 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
615 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
616 map->sys, fd, maskcmd,
617 (int)((maskcmd >> 8) & 0xff),
618 (int)(maskcmd & 0xff));
619 error = EINVAL;
620 goto done;
624 * If it's a non-range one to one mapping, maptocmd should be
625 * correct. If it's a ranged one to one mapping, we pass the
626 * original value of com, and for a range mapped to a different
627 * range, we always need a mapping function to translate the
628 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
630 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
631 com = iomc->maptocmd;
632 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
633 if (iomc->mapfunc != NULL)
634 com = iomc->mapfunc(iomc->start, iomc->end,
635 iomc->start, iomc->end,
636 com, com);
637 } else {
638 if (iomc->mapfunc != NULL) {
639 com = iomc->mapfunc(iomc->start, iomc->end,
640 iomc->maptocmd, iomc->maptoend,
641 com, ocom);
642 } else {
643 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
644 map->sys, fd, maskcmd,
645 (int)((maskcmd >> 8) & 0xff),
646 (int)(maskcmd & 0xff));
647 error = EINVAL;
648 goto done;
653 switch (com) {
654 case FIONCLEX:
655 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
656 goto done;
657 case FIOCLEX:
658 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
659 goto done;
663 * Interpret high order word to find amount of data to be
664 * copied to/from the user's address space.
666 size = IOCPARM_LEN(com);
667 if (size > IOCPARM_MAX) {
668 error = ENOTTY;
669 goto done;
672 if ((com & IOC_VOID) == 0 && size > sizeof(ubuf.stkbuf)) {
673 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
674 data = memp;
675 } else {
676 memp = NULL;
677 data = ubuf.stkbuf;
679 if (com & IOC_VOID) {
680 *(caddr_t *)data = uspc_data;
681 } else if (com & IOC_IN) {
682 if (size != 0) {
683 error = copyin(uspc_data, data, (size_t)size);
684 if (error)
685 goto done;
686 } else {
687 *(caddr_t *)data = uspc_data;
689 } else if ((com & IOC_OUT) != 0 && size) {
691 * Zero the buffer so the user always
692 * gets back something deterministic.
694 bzero(data, (size_t)size);
697 switch (com) {
698 case FIONBIO:
699 if ((tmp = *(int *)data))
700 atomic_set_int(&fp->f_flag, FNONBLOCK);
701 else
702 atomic_clear_int(&fp->f_flag, FNONBLOCK);
703 error = 0;
704 break;
706 case FIOASYNC:
707 if ((tmp = *(int *)data))
708 atomic_set_int(&fp->f_flag, FASYNC);
709 else
710 atomic_clear_int(&fp->f_flag, FASYNC);
711 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
712 break;
714 default:
716 * If there is a override function,
717 * call it instead of directly routing the call
719 if (map != NULL && iomc->wrapfunc != NULL)
720 error = iomc->wrapfunc(fp, com, ocom, data, cred);
721 else
722 error = fo_ioctl(fp, com, data, cred, msg);
724 * Copy any data to user, size was
725 * already set and checked above.
727 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
728 error = copyout(data, uspc_data, (size_t)size);
729 break;
731 done:
732 if (memp != NULL)
733 kfree(memp, M_IOCTLOPS);
734 fdrop(fp);
735 return(error);
739 * MPSAFE
742 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
744 struct ioctl_map_entry *ne;
746 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
747 he->subsys != NULL && *he->subsys != '\0');
749 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP,
750 M_WAITOK | M_ZERO);
752 ne->subsys = he->subsys;
753 ne->cmd_ranges = he->cmd_ranges;
755 lwkt_gettoken(&mioctl_token);
756 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
757 lwkt_reltoken(&mioctl_token);
759 return(0);
763 * MPSAFE
766 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
768 struct ioctl_map_entry *ne;
769 int error = EINVAL;
771 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
773 lwkt_gettoken(&mioctl_token);
774 LIST_FOREACH(ne, &he->map->mapping, entries) {
775 if (ne->cmd_ranges == he->cmd_ranges) {
776 LIST_REMOVE(ne, entries);
777 kfree(ne, M_IOCTLMAP);
778 error = 0;
779 break;
782 lwkt_reltoken(&mioctl_token);
783 return(error);
786 static int nselcoll; /* Select collisions since boot */
787 int selwait;
788 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
789 static int nseldebug;
790 SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
793 * Select system call.
795 * MPSAFE
798 sys_select(struct select_args *uap)
800 struct timeval ktv;
801 struct timespec *ktsp, kts;
802 int error;
805 * Get timeout if any.
807 if (uap->tv != NULL) {
808 error = copyin(uap->tv, &ktv, sizeof (ktv));
809 if (error)
810 return (error);
811 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
812 ktsp = &kts;
813 } else {
814 ktsp = NULL;
818 * Do real work.
820 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
821 &uap->sysmsg_result);
823 return (error);
828 * Pselect system call.
831 sys_pselect(struct pselect_args *uap)
833 struct thread *td = curthread;
834 struct lwp *lp = td->td_lwp;
835 struct timespec *ktsp, kts;
836 sigset_t sigmask;
837 int error;
840 * Get timeout if any.
842 if (uap->ts != NULL) {
843 error = copyin(uap->ts, &kts, sizeof (kts));
844 if (error)
845 return (error);
846 ktsp = &kts;
847 } else {
848 ktsp = NULL;
852 * Install temporary signal mask if any provided.
854 if (uap->sigmask != NULL) {
855 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
856 if (error)
857 return (error);
858 lwkt_gettoken(&lp->lwp_proc->p_token);
859 lp->lwp_oldsigmask = lp->lwp_sigmask;
860 SIG_CANTMASK(sigmask);
861 lp->lwp_sigmask = sigmask;
862 lwkt_reltoken(&lp->lwp_proc->p_token);
866 * Do real job.
868 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
869 &uap->sysmsg_result);
871 if (uap->sigmask != NULL) {
872 lwkt_gettoken(&lp->lwp_proc->p_token);
873 /* doselect() responsible for turning ERESTART into EINTR */
874 KKASSERT(error != ERESTART);
875 if (error == EINTR) {
877 * We can't restore the previous signal mask now
878 * because it could block the signal that interrupted
879 * us. So make a note to restore it after executing
880 * the handler.
882 lp->lwp_flags |= LWP_OLDMASK;
883 } else {
885 * No handler to run. Restore previous mask immediately.
887 lp->lwp_sigmask = lp->lwp_oldsigmask;
889 lwkt_reltoken(&lp->lwp_proc->p_token);
892 return (error);
895 static int
896 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
898 struct select_kevent_copyin_args *skap = NULL;
899 struct kevent *kev;
900 int fd;
901 kfd_set *fdp = NULL;
902 short filter = 0;
903 u_int fflags = 0;
905 skap = (struct select_kevent_copyin_args *)arg;
907 if (*events == maxevents)
908 return (0);
910 while (skap->active_set < COPYIN_DONE) {
911 switch (skap->active_set) {
912 case COPYIN_READ:
914 * Register descriptors for the read filter
916 fdp = skap->read_set;
917 filter = EVFILT_READ;
918 fflags = NOTE_OLDAPI;
919 if (fdp)
920 break;
921 ++skap->active_set;
922 skap->proc_fds = 0;
923 /* fall through */
924 case COPYIN_WRITE:
926 * Register descriptors for the write filter
928 fdp = skap->write_set;
929 filter = EVFILT_WRITE;
930 fflags = NOTE_OLDAPI;
931 if (fdp)
932 break;
933 ++skap->active_set;
934 skap->proc_fds = 0;
935 /* fall through */
936 case COPYIN_EXCEPT:
938 * Register descriptors for the exception filter
940 fdp = skap->except_set;
941 filter = EVFILT_EXCEPT;
942 fflags = NOTE_OLDAPI | NOTE_OOB;
943 if (fdp)
944 break;
945 ++skap->active_set;
946 skap->proc_fds = 0;
947 /* fall through */
948 case COPYIN_DONE:
950 * Nothing left to register
952 return(0);
953 /* NOT REACHED */
956 while (skap->proc_fds < skap->num_fds) {
957 fd = skap->proc_fds;
958 if (FD_ISSET(fd, fdp)) {
959 kev = &kevp[*events];
960 EV_SET(kev, fd, filter,
961 EV_ADD|EV_ENABLE,
962 fflags, 0,
963 (void *)(uintptr_t)
964 skap->lwp->lwp_kqueue_serial);
965 FD_CLR(fd, fdp);
966 ++*events;
968 if (nseldebug)
969 kprintf("select fd %d filter %d serial %d\n",
970 fd, filter, skap->lwp->lwp_kqueue_serial);
972 ++skap->proc_fds;
973 if (*events == maxevents)
974 return (0);
976 skap->active_set++;
977 skap->proc_fds = 0;
980 return (0);
983 static int
984 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
986 struct select_kevent_copyin_args *skap;
987 struct kevent kev;
988 int i = 0;
990 skap = (struct select_kevent_copyin_args *)arg;
992 for (i = 0; i < count; ++i) {
994 * Filter out and delete spurious events
996 if ((u_int)(uintptr_t)kevp[i].udata !=
997 skap->lwp->lwp_kqueue_serial) {
998 kev = kevp[i];
999 kev.flags = EV_DISABLE|EV_DELETE;
1000 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
1001 if (nseldebug)
1002 kprintf("select fd %ju mismatched serial %d\n",
1003 (uintmax_t)kevp[i].ident,
1004 skap->lwp->lwp_kqueue_serial);
1005 continue;
1009 * Handle errors
1011 if (kevp[i].flags & EV_ERROR) {
1012 int error = kevp[i].data;
1014 switch (error) {
1015 case EBADF:
1017 * A bad file descriptor is considered a
1018 * fatal error for select, bail out.
1020 skap->error = error;
1021 *res = -1;
1022 return error;
1024 default:
1026 * Select silently swallows any unknown errors
1027 * for descriptors in the read or write sets.
1029 * ALWAYS filter out EOPNOTSUPP errors from
1030 * filters (at least until all filters support
1031 * EVFILT_EXCEPT)
1033 * We also filter out ENODEV since dev_dkqfilter
1034 * returns ENODEV if EOPNOTSUPP is returned in an
1035 * inner call.
1037 * XXX: fix this
1039 if (kevp[i].filter != EVFILT_READ &&
1040 kevp[i].filter != EVFILT_WRITE &&
1041 error != EOPNOTSUPP &&
1042 error != ENODEV) {
1043 skap->error = error;
1044 *res = -1;
1045 return error;
1047 break;
1049 if (nseldebug)
1050 kprintf("select fd %ju filter %d error %d\n",
1051 (uintmax_t)kevp[i].ident,
1052 kevp[i].filter, error);
1053 continue;
1056 switch (kevp[i].filter) {
1057 case EVFILT_READ:
1058 FD_SET(kevp[i].ident, skap->read_set);
1059 break;
1060 case EVFILT_WRITE:
1061 FD_SET(kevp[i].ident, skap->write_set);
1062 break;
1063 case EVFILT_EXCEPT:
1064 FD_SET(kevp[i].ident, skap->except_set);
1065 break;
1068 ++*res;
1071 return (0);
1075 * Copy select bits in from userland. Allocate kernel memory if the
1076 * set is large.
1078 static int
1079 getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1081 int error;
1083 if (in_set) {
1084 if (bytes < sizeof(*tmp_set))
1085 *out_set = tmp_set;
1086 else
1087 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1088 error = copyin(in_set, *out_set, bytes);
1089 } else {
1090 *out_set = NULL;
1091 error = 0;
1093 return (error);
1097 * Copy returned select bits back out to userland.
1099 static int
1100 putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1102 int error;
1104 if (in_set) {
1105 error = copyout(in_set, out_set, bytes);
1106 } else {
1107 error = 0;
1109 return (error);
1112 static int
1113 dotimeout_only(struct timespec *ts)
1115 return(nanosleep1(ts, NULL));
1119 * Common code for sys_select() and sys_pselect().
1121 * in, out and ex are userland pointers. ts must point to validated
1122 * kernel-side timeout value or NULL for infinite timeout. res must
1123 * point to syscall return value.
1125 static int
1126 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1127 struct timespec *ts, int *res)
1129 struct proc *p = curproc;
1130 struct select_kevent_copyin_args *kap, ka;
1131 int bytes, error;
1132 kfd_set read_tmp;
1133 kfd_set write_tmp;
1134 kfd_set except_tmp;
1136 *res = 0;
1137 if (nd < 0)
1138 return (EINVAL);
1139 if (nd == 0 && ts)
1140 return (dotimeout_only(ts));
1142 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1143 nd = p->p_fd->fd_nfiles;
1145 kap = &ka;
1146 kap->lwp = curthread->td_lwp;
1147 kap->num_fds = nd;
1148 kap->proc_fds = 0;
1149 kap->error = 0;
1150 kap->active_set = COPYIN_READ;
1153 * Calculate bytes based on the number of __fd_mask[] array entries
1154 * multiplied by the size of __fd_mask.
1156 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1158 /* kap->read_set = NULL; not needed */
1159 kap->write_set = NULL;
1160 kap->except_set = NULL;
1162 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1163 if (error == 0)
1164 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1165 if (error == 0)
1166 error = getbits(bytes, except, &kap->except_set, &except_tmp);
1167 if (error)
1168 goto done;
1171 * NOTE: Make sure the max events passed to kern_kevent() is
1172 * effectively unlimited. (nd * 3) accomplishes this.
1174 * (*res) continues to increment as returned events are
1175 * loaded in.
1177 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1178 select_copyin, select_copyout, ts, 0);
1179 if (error == 0)
1180 error = putbits(bytes, kap->read_set, read);
1181 if (error == 0)
1182 error = putbits(bytes, kap->write_set, write);
1183 if (error == 0)
1184 error = putbits(bytes, kap->except_set, except);
1187 * An error from an individual event that should be passed
1188 * back to userland (EBADF)
1190 if (kap->error)
1191 error = kap->error;
1194 * Clean up.
1196 done:
1197 if (kap->read_set && kap->read_set != &read_tmp)
1198 kfree(kap->read_set, M_SELECT);
1199 if (kap->write_set && kap->write_set != &write_tmp)
1200 kfree(kap->write_set, M_SELECT);
1201 if (kap->except_set && kap->except_set != &except_tmp)
1202 kfree(kap->except_set, M_SELECT);
1204 kap->lwp->lwp_kqueue_serial += kap->num_fds;
1206 return (error);
1210 * Poll system call.
1212 * MPSAFE
1215 sys_poll(struct poll_args *uap)
1217 struct timespec ts, *tsp;
1218 int error;
1220 if (uap->timeout != INFTIM) {
1221 if (uap->timeout < 0)
1222 return (EINVAL);
1223 ts.tv_sec = uap->timeout / 1000;
1224 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1225 tsp = &ts;
1226 } else {
1227 tsp = NULL;
1230 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result, 0);
1232 return (error);
1236 * Ppoll system call.
1238 * MPSAFE
1241 sys_ppoll(struct ppoll_args *uap)
1243 struct thread *td = curthread;
1244 struct lwp *lp = td->td_lwp;
1245 struct timespec *ktsp, kts;
1246 sigset_t sigmask;
1247 int error;
1250 * Get timeout if any.
1252 if (uap->ts != NULL) {
1253 error = copyin(uap->ts, &kts, sizeof (kts));
1254 if (error)
1255 return (error);
1256 ktsp = &kts;
1257 } else {
1258 ktsp = NULL;
1262 * Install temporary signal mask if any provided.
1264 if (uap->sigmask != NULL) {
1265 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
1266 if (error)
1267 return (error);
1268 lwkt_gettoken(&lp->lwp_proc->p_token);
1269 lp->lwp_oldsigmask = lp->lwp_sigmask;
1270 SIG_CANTMASK(sigmask);
1271 lp->lwp_sigmask = sigmask;
1272 lwkt_reltoken(&lp->lwp_proc->p_token);
1275 error = dopoll(uap->nfds, uap->fds, ktsp, &uap->sysmsg_result,
1276 ktsp != NULL ? KEVENT_TIMEOUT_PRECISE : 0);
1278 if (uap->sigmask != NULL) {
1279 lwkt_gettoken(&lp->lwp_proc->p_token);
1280 /* dopoll() responsible for turning ERESTART into EINTR */
1281 KKASSERT(error != ERESTART);
1282 if (error == EINTR) {
1284 * We can't restore the previous signal mask now
1285 * because it could block the signal that interrupted
1286 * us. So make a note to restore it after executing
1287 * the handler.
1289 lp->lwp_flags |= LWP_OLDMASK;
1290 } else {
1292 * No handler to run. Restore previous mask immediately.
1294 lp->lwp_sigmask = lp->lwp_oldsigmask;
1296 lwkt_reltoken(&lp->lwp_proc->p_token);
1299 return (error);
1302 static int
1303 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1305 struct poll_kevent_copyin_args *pkap;
1306 struct pollfd *pfd;
1307 struct kevent *kev;
1308 int kev_count;
1310 pkap = (struct poll_kevent_copyin_args *)arg;
1312 while (pkap->pfds < pkap->nfds) {
1313 pfd = &pkap->fds[pkap->pfds];
1315 /* Clear return events */
1316 pfd->revents = 0;
1318 /* Do not check if fd is equal to -1 */
1319 if (pfd->fd == -1) {
1320 ++pkap->pfds;
1321 continue;
1324 kev_count = 0;
1325 if (pfd->events & (POLLIN | POLLRDNORM))
1326 kev_count++;
1327 if (pfd->events & (POLLOUT | POLLWRNORM))
1328 kev_count++;
1329 if (pfd->events & (POLLPRI | POLLRDBAND))
1330 kev_count++;
1332 if (*events + kev_count > maxevents)
1333 return (0);
1336 * NOTE: A combined serial number and poll array index is
1337 * stored in kev->udata.
1339 kev = &kevp[*events];
1340 if (pfd->events & (POLLIN | POLLRDNORM)) {
1341 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1342 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1343 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1345 if (pfd->events & (POLLOUT | POLLWRNORM)) {
1346 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1347 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1348 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1350 if (pfd->events & (POLLPRI | POLLRDBAND)) {
1351 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1352 NOTE_OLDAPI | NOTE_OOB, 0,
1353 (void *)(uintptr_t)
1354 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1357 if (nseldebug) {
1358 kprintf("poll index %d/%d fd %d events %08x serial %d\n",
1359 pkap->pfds, pkap->nfds-1, pfd->fd, pfd->events,
1360 pkap->lwp->lwp_kqueue_serial);
1363 ++pkap->pfds;
1364 (*events) += kev_count;
1367 return (0);
1370 static int
1371 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1373 struct poll_kevent_copyin_args *pkap;
1374 struct pollfd *pfd;
1375 struct kevent kev;
1376 int count_res;
1377 int i;
1378 u_int pi;
1380 pkap = (struct poll_kevent_copyin_args *)arg;
1382 for (i = 0; i < count; ++i) {
1384 * Extract the poll array index and delete spurious events.
1385 * We can easily tell if the serial number is incorrect
1386 * by checking whether the extracted index is out of range.
1388 pi = (u_int)(uintptr_t)kevp[i].udata -
1389 (u_int)pkap->lwp->lwp_kqueue_serial;
1391 if (pi >= pkap->nfds) {
1392 kev = kevp[i];
1393 kev.flags = EV_DISABLE|EV_DELETE;
1394 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1395 if (nseldebug)
1396 kprintf("poll index %d out of range against serial %d\n",
1397 pi, pkap->lwp->lwp_kqueue_serial);
1398 continue;
1400 pfd = &pkap->fds[pi];
1401 if (kevp[i].ident == pfd->fd) {
1403 * A single descriptor may generate an error against
1404 * more than one filter, make sure to set the
1405 * appropriate flags but do not increment (*res)
1406 * more than once.
1408 count_res = (pfd->revents == 0);
1409 if (kevp[i].flags & EV_ERROR) {
1410 switch(kevp[i].data) {
1411 case EBADF:
1412 case POLLNVAL:
1413 /* Bad file descriptor */
1414 if (count_res)
1415 ++*res;
1416 pfd->revents |= POLLNVAL;
1417 break;
1418 default:
1420 * Poll silently swallows any unknown
1421 * errors except in the case of POLLPRI
1422 * (OOB/urgent data).
1424 * ALWAYS filter out EOPNOTSUPP errors
1425 * from filters, common applications
1426 * set POLLPRI|POLLRDBAND and most
1427 * filters do not support EVFILT_EXCEPT.
1429 * We also filter out ENODEV since dev_dkqfilter
1430 * returns ENODEV if EOPNOTSUPP is returned in an
1431 * inner call.
1433 * XXX: fix this
1435 if (kevp[i].filter != EVFILT_READ &&
1436 kevp[i].filter != EVFILT_WRITE &&
1437 kevp[i].data != EOPNOTSUPP &&
1438 kevp[i].data != ENODEV) {
1439 if (count_res == 0)
1440 ++*res;
1441 pfd->revents |= POLLERR;
1443 break;
1445 if (nseldebug) {
1446 kprintf("poll index %d fd %d "
1447 "filter %d error %jd\n",
1448 pi, pfd->fd,
1449 kevp[i].filter,
1450 (intmax_t)kevp[i].data);
1452 continue;
1455 switch (kevp[i].filter) {
1456 case EVFILT_READ:
1457 #if 0
1459 * NODATA on the read side can indicate a
1460 * half-closed situation and not necessarily
1461 * a disconnect, so depend on the user
1462 * issuing a read() and getting 0 bytes back.
1464 if (kevp[i].flags & EV_NODATA)
1465 pfd->revents |= POLLHUP;
1466 #endif
1467 if ((kevp[i].flags & EV_EOF) &&
1468 kevp[i].fflags != 0)
1469 pfd->revents |= POLLERR;
1470 if (pfd->events & POLLIN)
1471 pfd->revents |= POLLIN;
1472 if (pfd->events & POLLRDNORM)
1473 pfd->revents |= POLLRDNORM;
1474 break;
1475 case EVFILT_WRITE:
1477 * As per the OpenGroup POLLHUP is mutually
1478 * exclusive with the writability flags. I
1479 * consider this a bit broken but...
1481 * In this case a disconnect is implied even
1482 * for a half-closed (write side) situation.
1484 if (kevp[i].flags & EV_EOF) {
1485 pfd->revents |= POLLHUP;
1486 if (kevp[i].fflags != 0)
1487 pfd->revents |= POLLERR;
1488 } else {
1489 if (pfd->events & POLLOUT)
1490 pfd->revents |= POLLOUT;
1491 if (pfd->events & POLLWRNORM)
1492 pfd->revents |= POLLWRNORM;
1494 break;
1495 case EVFILT_EXCEPT:
1497 * EV_NODATA should never be tagged for this
1498 * filter.
1500 if (pfd->events & POLLPRI)
1501 pfd->revents |= POLLPRI;
1502 if (pfd->events & POLLRDBAND)
1503 pfd->revents |= POLLRDBAND;
1504 break;
1507 if (nseldebug) {
1508 kprintf("poll index %d/%d fd %d revents %08x\n",
1509 pi, pkap->nfds, pfd->fd, pfd->revents);
1512 if (count_res && pfd->revents)
1513 ++*res;
1514 } else {
1515 if (nseldebug) {
1516 kprintf("poll index %d mismatch %ju/%d\n",
1517 pi, (uintmax_t)kevp[i].ident, pfd->fd);
1522 return (0);
1525 static int
1526 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res, int flags)
1528 struct poll_kevent_copyin_args ka;
1529 struct pollfd sfds[64];
1530 int bytes;
1531 int error;
1533 *res = 0;
1534 if (nfds < 0)
1535 return (EINVAL);
1537 if (nfds == 0 && ts)
1538 return (dotimeout_only(ts));
1541 * This is a bit arbitrary but we need to limit internal kmallocs.
1543 if (nfds > maxfilesperproc * 2)
1544 nfds = maxfilesperproc * 2;
1545 bytes = sizeof(struct pollfd) * nfds;
1547 ka.lwp = curthread->td_lwp;
1548 ka.nfds = nfds;
1549 ka.pfds = 0;
1550 ka.error = 0;
1552 if (ka.nfds < 64)
1553 ka.fds = sfds;
1554 else
1555 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1557 error = copyin(fds, ka.fds, bytes);
1558 if (error == 0)
1559 error = kern_kevent(&ka.lwp->lwp_kqueue, 0x7FFFFFFF, res, &ka,
1560 poll_copyin, poll_copyout, ts, flags);
1562 if (error == 0)
1563 error = copyout(ka.fds, fds, bytes);
1565 if (ka.fds != sfds)
1566 kfree(ka.fds, M_SELECT);
1568 ka.lwp->lwp_kqueue_serial += nfds;
1570 return (error);
1573 static int
1574 socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1576 return (0);
1579 static int
1580 socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1582 ++*res;
1583 return (0);
1586 extern struct fileops socketops;
1589 * NOTE: Callers of socket_wait() must already have a reference on the
1590 * socket.
1593 socket_wait(struct socket *so, struct timespec *ts, int *res)
1595 struct thread *td = curthread;
1596 struct file *fp;
1597 struct kqueue kq;
1598 struct kevent kev;
1599 int error, fd;
1601 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
1602 return (error);
1604 fp->f_type = DTYPE_SOCKET;
1605 fp->f_flag = FREAD | FWRITE;
1606 fp->f_ops = &socketops;
1607 fp->f_data = so;
1608 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
1609 fsetfdflags(td->td_proc->p_fd, fd, UF_EXCLOSE);
1611 bzero(&kq, sizeof(kq));
1612 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1613 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
1614 if ((error = kqueue_register(&kq, &kev)) != 0) {
1615 fdrop(fp);
1616 return (error);
1619 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1620 socket_wait_copyout, ts, 0);
1622 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE|EV_DISABLE, 0, 0, NULL);
1623 kqueue_register(&kq, &kev);
1624 fp->f_ops = &badfileops;
1625 fdrop(fp);
1627 return (error);
1631 * OpenBSD poll system call.
1632 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1634 * MPSAFE
1637 sys_openbsd_poll(struct openbsd_poll_args *uap)
1639 return (sys_poll((struct poll_args *)uap));
1642 /*ARGSUSED*/
1644 seltrue(cdev_t dev, int events)
1646 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));