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
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
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>
49 #include <sys/signalvar.h>
50 #include <sys/socketvar.h>
52 #include <sys/kernel.h>
53 #include <sys/kern_syscall.h>
54 #include <sys/malloc.h>
55 #include <sys/mapped_ioctl.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>
64 #include <sys/ktrace.h>
67 #include <vm/vm_page.h>
69 #include <sys/file2.h>
70 #include <sys/mplock2.h>
71 #include <sys/spinlock2.h>
73 #include <machine/limits.h>
75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
77 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
78 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
80 typedef struct kfd_set {
84 enum select_copyin_states {
85 COPYIN_READ, COPYIN_WRITE, COPYIN_EXCEPT, COPYIN_DONE };
87 struct select_kevent_copyin_args {
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 */
98 struct poll_kevent_copyin_args {
106 static struct lwkt_token mioctl_token = LWKT_TOKEN_INITIALIZER(mioctl_token);
108 static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
109 struct timespec *ts, int *res);
110 static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
112 static int dofileread(int, struct file *, struct uio *, int, size_t *);
113 static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
121 sys_read(struct read_args *uap)
123 struct thread *td = curthread;
128 if ((ssize_t)uap->nbyte < 0)
131 aiov.iov_base = uap->buf;
132 aiov.iov_len = uap->nbyte;
133 auio.uio_iov = &aiov;
135 auio.uio_offset = -1;
136 auio.uio_resid = uap->nbyte;
137 auio.uio_rw = UIO_READ;
138 auio.uio_segflg = UIO_USERSPACE;
141 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
146 * Positioned (Pread) read system call
151 sys_extpread(struct extpread_args *uap)
153 struct thread *td = curthread;
159 if ((ssize_t)uap->nbyte < 0)
162 aiov.iov_base = uap->buf;
163 aiov.iov_len = uap->nbyte;
164 auio.uio_iov = &aiov;
166 auio.uio_offset = uap->offset;
167 auio.uio_resid = uap->nbyte;
168 auio.uio_rw = UIO_READ;
169 auio.uio_segflg = UIO_USERSPACE;
172 flags = uap->flags & O_FMASK;
173 if (uap->offset != (off_t)-1)
176 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
181 * Scatter read system call.
186 sys_readv(struct readv_args *uap)
188 struct thread *td = curthread;
190 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
193 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
198 auio.uio_iovcnt = uap->iovcnt;
199 auio.uio_offset = -1;
200 auio.uio_rw = UIO_READ;
201 auio.uio_segflg = UIO_USERSPACE;
204 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
206 iovec_free(&iov, aiov);
212 * Scatter positioned read system call.
217 sys_extpreadv(struct extpreadv_args *uap)
219 struct thread *td = curthread;
221 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
225 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
230 auio.uio_iovcnt = uap->iovcnt;
231 auio.uio_offset = uap->offset;
232 auio.uio_rw = UIO_READ;
233 auio.uio_segflg = UIO_USERSPACE;
236 flags = uap->flags & O_FMASK;
237 if (uap->offset != (off_t)-1)
240 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
242 iovec_free(&iov, aiov);
250 kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
252 struct thread *td = curthread;
253 struct proc *p = td->td_proc;
259 fp = holdfp(p->p_fd, fd, FREAD);
262 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
265 error = dofileread(fd, fp, auio, flags, res);
272 * Common code for readv and preadv that reads data in
273 * from a file using the passed in uio, offset, and flags.
275 * MPALMOSTSAFE - ktrace needs help
278 dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
283 struct thread *td = curthread;
284 struct iovec *ktriov = NULL;
290 * if tracing, save a copy of iovec
292 if (KTRPOINT(td, KTR_GENIO)) {
293 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
295 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
296 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
300 len = auio->uio_resid;
301 error = fo_read(fp, auio, fp->f_cred, flags);
303 if (auio->uio_resid != len && (error == ERESTART ||
304 error == EINTR || error == EWOULDBLOCK))
308 if (ktriov != NULL) {
310 ktruio.uio_iov = ktriov;
311 ktruio.uio_resid = len - auio->uio_resid;
313 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
316 kfree(ktriov, M_TEMP);
320 *res = len - auio->uio_resid;
331 sys_write(struct write_args *uap)
333 struct thread *td = curthread;
338 if ((ssize_t)uap->nbyte < 0)
341 aiov.iov_base = (void *)(uintptr_t)uap->buf;
342 aiov.iov_len = uap->nbyte;
343 auio.uio_iov = &aiov;
345 auio.uio_offset = -1;
346 auio.uio_resid = uap->nbyte;
347 auio.uio_rw = UIO_WRITE;
348 auio.uio_segflg = UIO_USERSPACE;
351 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
362 sys_extpwrite(struct extpwrite_args *uap)
364 struct thread *td = curthread;
370 if ((ssize_t)uap->nbyte < 0)
373 aiov.iov_base = (void *)(uintptr_t)uap->buf;
374 aiov.iov_len = uap->nbyte;
375 auio.uio_iov = &aiov;
377 auio.uio_offset = uap->offset;
378 auio.uio_resid = uap->nbyte;
379 auio.uio_rw = UIO_WRITE;
380 auio.uio_segflg = UIO_USERSPACE;
383 flags = uap->flags & O_FMASK;
384 if (uap->offset != (off_t)-1)
386 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
394 sys_writev(struct writev_args *uap)
396 struct thread *td = curthread;
398 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
401 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
406 auio.uio_iovcnt = uap->iovcnt;
407 auio.uio_offset = -1;
408 auio.uio_rw = UIO_WRITE;
409 auio.uio_segflg = UIO_USERSPACE;
412 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
414 iovec_free(&iov, aiov);
420 * Gather positioned write system call
425 sys_extpwritev(struct extpwritev_args *uap)
427 struct thread *td = curthread;
429 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
433 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
438 auio.uio_iovcnt = uap->iovcnt;
439 auio.uio_offset = uap->offset;
440 auio.uio_rw = UIO_WRITE;
441 auio.uio_segflg = UIO_USERSPACE;
444 flags = uap->flags & O_FMASK;
445 if (uap->offset != (off_t)-1)
448 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
450 iovec_free(&iov, aiov);
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;
467 fp = holdfp(p->p_fd, fd, FWRITE);
470 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
473 error = dofilewrite(fd, fp, auio, flags, res);
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
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;
494 struct iovec *ktriov = NULL;
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);
510 len = auio->uio_resid;
511 error = fo_write(fp, auio, fp->f_cred, flags);
513 if (auio->uio_resid != len && (error == ERESTART ||
514 error == EINTR || error == EWOULDBLOCK))
516 /* Socket layer is responsible for issuing SIGPIPE. */
517 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
518 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
521 if (ktriov != NULL) {
523 ktruio.uio_iov = ktriov;
524 ktruio.uio_resid = len - auio->uio_resid;
526 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
529 kfree(ktriov, M_TEMP);
533 *res = len - auio->uio_resid;
544 sys_ioctl(struct ioctl_args *uap)
548 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
552 struct ioctl_map_entry {
554 struct ioctl_map_range *cmd_ranges;
555 LIST_ENTRY(ioctl_map_entry) entries;
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.
566 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
569 struct thread *td = curthread;
570 struct proc *p = td->td_proc;
573 struct ioctl_map_range *iomc = NULL;
579 #define STK_PARAMS 128
581 char stkbuf[STK_PARAMS];
589 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
593 if (map != NULL) { /* obey translation map */
595 struct ioctl_map_entry *e;
597 maskcmd = com & map->mask;
599 lwkt_gettoken(&mioctl_token);
600 LIST_FOREACH(e, &map->mapping, entries) {
601 for (iomc = e->cmd_ranges; iomc->start != 0 ||
602 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
603 iomc->mapfunc != NULL;
605 if (maskcmd >= iomc->start &&
606 maskcmd <= iomc->end)
610 /* Did we find a match? */
611 if (iomc->start != 0 || iomc->maptocmd != 0 ||
612 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
615 lwkt_reltoken(&mioctl_token);
618 (iomc->start == 0 && iomc->maptocmd == 0
619 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
620 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
621 map->sys, fd, maskcmd,
622 (int)((maskcmd >> 8) & 0xff),
623 (int)(maskcmd & 0xff));
629 * If it's a non-range one to one mapping, maptocmd should be
630 * correct. If it's a ranged one to one mapping, we pass the
631 * original value of com, and for a range mapped to a different
632 * range, we always need a mapping function to translate the
633 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
635 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
636 com = iomc->maptocmd;
637 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
638 if (iomc->mapfunc != NULL)
639 com = iomc->mapfunc(iomc->start, iomc->end,
640 iomc->start, iomc->end,
643 if (iomc->mapfunc != NULL) {
644 com = iomc->mapfunc(iomc->start, iomc->end,
645 iomc->maptocmd, iomc->maptoend,
648 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
649 map->sys, fd, maskcmd,
650 (int)((maskcmd >> 8) & 0xff),
651 (int)(maskcmd & 0xff));
660 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
663 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
668 * Interpret high order word to find amount of data to be
669 * copied to/from the user's address space.
671 size = IOCPARM_LEN(com);
672 if (size > IOCPARM_MAX) {
677 if ((com & IOC_VOID) == 0 && size > sizeof(ubuf.stkbuf)) {
678 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
684 if (com & IOC_VOID) {
685 *(caddr_t *)data = uspc_data;
686 } else if (com & IOC_IN) {
688 error = copyin(uspc_data, data, (size_t)size);
692 *(caddr_t *)data = uspc_data;
694 } else if ((com & IOC_OUT) != 0 && size) {
696 * Zero the buffer so the user always
697 * gets back something deterministic.
699 bzero(data, (size_t)size);
704 if ((tmp = *(int *)data))
705 atomic_set_int(&fp->f_flag, FNONBLOCK);
707 atomic_clear_int(&fp->f_flag, FNONBLOCK);
712 if ((tmp = *(int *)data))
713 atomic_set_int(&fp->f_flag, FASYNC);
715 atomic_clear_int(&fp->f_flag, FASYNC);
716 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
721 * If there is a override function,
722 * call it instead of directly routing the call
724 if (map != NULL && iomc->wrapfunc != NULL)
725 error = iomc->wrapfunc(fp, com, ocom, data, cred);
727 error = fo_ioctl(fp, com, data, cred, msg);
729 * Copy any data to user, size was
730 * already set and checked above.
732 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
733 error = copyout(data, uspc_data, (size_t)size);
738 kfree(memp, M_IOCTLOPS);
747 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
749 struct ioctl_map_entry *ne;
751 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
752 he->subsys != NULL && *he->subsys != '\0');
754 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP,
757 ne->subsys = he->subsys;
758 ne->cmd_ranges = he->cmd_ranges;
760 lwkt_gettoken(&mioctl_token);
761 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
762 lwkt_reltoken(&mioctl_token);
771 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
773 struct ioctl_map_entry *ne;
776 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
778 lwkt_gettoken(&mioctl_token);
779 LIST_FOREACH(ne, &he->map->mapping, entries) {
780 if (ne->cmd_ranges == he->cmd_ranges) {
781 LIST_REMOVE(ne, entries);
782 kfree(ne, M_IOCTLMAP);
787 lwkt_reltoken(&mioctl_token);
791 static int nselcoll; /* Select collisions since boot */
793 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
794 static int nseldebug;
795 SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
798 * Select system call.
803 sys_select(struct select_args *uap)
806 struct timespec *ktsp, kts;
810 * Get timeout if any.
812 if (uap->tv != NULL) {
813 error = copyin(uap->tv, &ktv, sizeof (ktv));
816 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
825 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
826 &uap->sysmsg_result);
833 * Pselect system call.
836 sys_pselect(struct pselect_args *uap)
838 struct thread *td = curthread;
839 struct lwp *lp = td->td_lwp;
840 struct timespec *ktsp, kts;
845 * Get timeout if any.
847 if (uap->ts != NULL) {
848 error = copyin(uap->ts, &kts, sizeof (kts));
857 * Install temporary signal mask if any provided.
859 if (uap->sigmask != NULL) {
860 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
863 lwkt_gettoken(&lp->lwp_proc->p_token);
864 lp->lwp_oldsigmask = lp->lwp_sigmask;
865 SIG_CANTMASK(sigmask);
866 lp->lwp_sigmask = sigmask;
867 lwkt_reltoken(&lp->lwp_proc->p_token);
873 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
874 &uap->sysmsg_result);
876 if (uap->sigmask != NULL) {
877 lwkt_gettoken(&lp->lwp_proc->p_token);
878 /* doselect() responsible for turning ERESTART into EINTR */
879 KKASSERT(error != ERESTART);
880 if (error == EINTR) {
882 * We can't restore the previous signal mask now
883 * because it could block the signal that interrupted
884 * us. So make a note to restore it after executing
887 lp->lwp_flags |= LWP_OLDMASK;
890 * No handler to run. Restore previous mask immediately.
892 lp->lwp_sigmask = lp->lwp_oldsigmask;
894 lwkt_reltoken(&lp->lwp_proc->p_token);
901 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
903 struct select_kevent_copyin_args *skap = NULL;
910 skap = (struct select_kevent_copyin_args *)arg;
912 if (*events == maxevents)
915 while (skap->active_set < COPYIN_DONE) {
916 switch (skap->active_set) {
919 * Register descriptors for the read filter
921 fdp = skap->read_set;
922 filter = EVFILT_READ;
923 fflags = NOTE_OLDAPI;
931 * Register descriptors for the write filter
933 fdp = skap->write_set;
934 filter = EVFILT_WRITE;
935 fflags = NOTE_OLDAPI;
943 * Register descriptors for the exception filter
945 fdp = skap->except_set;
946 filter = EVFILT_EXCEPT;
947 fflags = NOTE_OLDAPI | NOTE_OOB;
955 * Nothing left to register
961 while (skap->proc_fds < skap->num_fds) {
963 if (FD_ISSET(fd, fdp)) {
964 kev = &kevp[*events];
965 EV_SET(kev, fd, filter,
969 skap->lwp->lwp_kqueue_serial);
974 kprintf("select fd %d filter %d serial %d\n",
975 fd, filter, skap->lwp->lwp_kqueue_serial);
978 if (*events == maxevents)
989 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
991 struct select_kevent_copyin_args *skap;
995 skap = (struct select_kevent_copyin_args *)arg;
997 for (i = 0; i < count; ++i) {
999 * Filter out and delete spurious events
1001 if ((u_int)(uintptr_t)kevp[i].udata !=
1002 skap->lwp->lwp_kqueue_serial) {
1004 kev.flags = EV_DISABLE|EV_DELETE;
1005 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
1007 kprintf("select fd %ju mismatched serial %d\n",
1008 (uintmax_t)kevp[i].ident,
1009 skap->lwp->lwp_kqueue_serial);
1016 if (kevp[i].flags & EV_ERROR) {
1017 int error = kevp[i].data;
1022 * A bad file descriptor is considered a
1023 * fatal error for select, bail out.
1025 skap->error = error;
1031 * Select silently swallows any unknown errors
1032 * for descriptors in the read or write sets.
1034 * ALWAYS filter out EOPNOTSUPP errors from
1035 * filters (at least until all filters support
1038 * We also filter out ENODEV since dev_dkqfilter
1039 * returns ENODEV if EOPNOTSUPP is returned in an
1044 if (kevp[i].filter != EVFILT_READ &&
1045 kevp[i].filter != EVFILT_WRITE &&
1046 error != EOPNOTSUPP &&
1048 skap->error = error;
1055 kprintf("select fd %ju filter %d error %d\n",
1056 (uintmax_t)kevp[i].ident,
1057 kevp[i].filter, error);
1061 switch (kevp[i].filter) {
1063 FD_SET(kevp[i].ident, skap->read_set);
1066 FD_SET(kevp[i].ident, skap->write_set);
1069 FD_SET(kevp[i].ident, skap->except_set);
1080 * Copy select bits in from userland. Allocate kernel memory if the
1084 getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1089 if (bytes < sizeof(*tmp_set))
1092 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1093 error = copyin(in_set, *out_set, bytes);
1102 * Copy returned select bits back out to userland.
1105 putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1110 error = copyout(in_set, out_set, bytes);
1118 dotimeout_only(struct timespec *ts)
1120 return(nanosleep1(ts, NULL));
1124 * Common code for sys_select() and sys_pselect().
1126 * in, out and ex are userland pointers. ts must point to validated
1127 * kernel-side timeout value or NULL for infinite timeout. res must
1128 * point to syscall return value.
1131 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1132 struct timespec *ts, int *res)
1134 struct proc *p = curproc;
1135 struct select_kevent_copyin_args *kap, ka;
1145 return (dotimeout_only(ts));
1147 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1148 nd = p->p_fd->fd_nfiles;
1151 kap->lwp = curthread->td_lwp;
1155 kap->active_set = COPYIN_READ;
1158 * Calculate bytes based on the number of __fd_mask[] array entries
1159 * multiplied by the size of __fd_mask.
1161 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1163 /* kap->read_set = NULL; not needed */
1164 kap->write_set = NULL;
1165 kap->except_set = NULL;
1167 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1169 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1171 error = getbits(bytes, except, &kap->except_set, &except_tmp);
1176 * NOTE: Make sure the max events passed to kern_kevent() is
1177 * effectively unlimited. (nd * 3) accomplishes this.
1179 * (*res) continues to increment as returned events are
1182 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1183 select_copyin, select_copyout, ts);
1185 error = putbits(bytes, kap->read_set, read);
1187 error = putbits(bytes, kap->write_set, write);
1189 error = putbits(bytes, kap->except_set, except);
1192 * An error from an individual event that should be passed
1193 * back to userland (EBADF)
1202 if (kap->read_set && kap->read_set != &read_tmp)
1203 kfree(kap->read_set, M_SELECT);
1204 if (kap->write_set && kap->write_set != &write_tmp)
1205 kfree(kap->write_set, M_SELECT);
1206 if (kap->except_set && kap->except_set != &except_tmp)
1207 kfree(kap->except_set, M_SELECT);
1209 kap->lwp->lwp_kqueue_serial += kap->num_fds;
1220 sys_poll(struct poll_args *uap)
1222 struct timespec ts, *tsp;
1225 if (uap->timeout != INFTIM) {
1226 ts.tv_sec = uap->timeout / 1000;
1227 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1233 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1239 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1241 struct poll_kevent_copyin_args *pkap;
1246 pkap = (struct poll_kevent_copyin_args *)arg;
1248 while (pkap->pfds < pkap->nfds) {
1249 pfd = &pkap->fds[pkap->pfds];
1251 /* Clear return events */
1254 /* Do not check if fd is equal to -1 */
1255 if (pfd->fd == -1) {
1261 if (pfd->events & (POLLIN | POLLRDNORM))
1263 if (pfd->events & (POLLOUT | POLLWRNORM))
1265 if (pfd->events & (POLLPRI | POLLRDBAND))
1268 if (*events + kev_count > maxevents)
1272 * NOTE: A combined serial number and poll array index is
1273 * stored in kev->udata.
1275 kev = &kevp[*events];
1276 if (pfd->events & (POLLIN | POLLRDNORM)) {
1277 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1278 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1279 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1281 if (pfd->events & (POLLOUT | POLLWRNORM)) {
1282 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1283 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1284 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1286 if (pfd->events & (POLLPRI | POLLRDBAND)) {
1287 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1288 NOTE_OLDAPI | NOTE_OOB, 0,
1290 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1294 kprintf("poll index %d/%d fd %d events %08x serial %d\n",
1295 pkap->pfds, pkap->nfds-1, pfd->fd, pfd->events,
1296 pkap->lwp->lwp_kqueue_serial);
1300 (*events) += kev_count;
1307 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1309 struct poll_kevent_copyin_args *pkap;
1316 pkap = (struct poll_kevent_copyin_args *)arg;
1318 for (i = 0; i < count; ++i) {
1320 * Extract the poll array index and delete spurious events.
1321 * We can easily tell if the serial number is incorrect
1322 * by checking whether the extracted index is out of range.
1324 pi = (u_int)(uintptr_t)kevp[i].udata -
1325 (u_int)pkap->lwp->lwp_kqueue_serial;
1327 if (pi >= pkap->nfds) {
1329 kev.flags = EV_DISABLE|EV_DELETE;
1330 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1332 kprintf("poll index %d out of range against serial %d\n",
1333 pi, pkap->lwp->lwp_kqueue_serial);
1336 pfd = &pkap->fds[pi];
1337 if (kevp[i].ident == pfd->fd) {
1339 * A single descriptor may generate an error against
1340 * more than one filter, make sure to set the
1341 * appropriate flags but do not increment (*res)
1344 count_res = (pfd->revents == 0);
1345 if (kevp[i].flags & EV_ERROR) {
1346 switch(kevp[i].data) {
1349 /* Bad file descriptor */
1352 pfd->revents |= POLLNVAL;
1356 * Poll silently swallows any unknown
1357 * errors except in the case of POLLPRI
1358 * (OOB/urgent data).
1360 * ALWAYS filter out EOPNOTSUPP errors
1361 * from filters, common applications
1362 * set POLLPRI|POLLRDBAND and most
1363 * filters do not support EVFILT_EXCEPT.
1365 * We also filter out ENODEV since dev_dkqfilter
1366 * returns ENODEV if EOPNOTSUPP is returned in an
1371 if (kevp[i].filter != EVFILT_READ &&
1372 kevp[i].filter != EVFILT_WRITE &&
1373 kevp[i].data != EOPNOTSUPP &&
1374 kevp[i].data != ENODEV) {
1377 pfd->revents |= POLLERR;
1382 kprintf("poll index %d fd %d "
1383 "filter %d error %jd\n",
1386 (intmax_t)kevp[i].data);
1391 switch (kevp[i].filter) {
1395 * NODATA on the read side can indicate a
1396 * half-closed situation and not necessarily
1397 * a disconnect, so depend on the user
1398 * issuing a read() and getting 0 bytes back.
1400 if (kevp[i].flags & EV_NODATA)
1401 pfd->revents |= POLLHUP;
1403 if ((kevp[i].flags & EV_EOF) &&
1404 kevp[i].fflags != 0)
1405 pfd->revents |= POLLERR;
1406 if (pfd->events & POLLIN)
1407 pfd->revents |= POLLIN;
1408 if (pfd->events & POLLRDNORM)
1409 pfd->revents |= POLLRDNORM;
1413 * As per the OpenGroup POLLHUP is mutually
1414 * exclusive with the writability flags. I
1415 * consider this a bit broken but...
1417 * In this case a disconnect is implied even
1418 * for a half-closed (write side) situation.
1420 if (kevp[i].flags & EV_EOF) {
1421 pfd->revents |= POLLHUP;
1422 if (kevp[i].fflags != 0)
1423 pfd->revents |= POLLERR;
1425 if (pfd->events & POLLOUT)
1426 pfd->revents |= POLLOUT;
1427 if (pfd->events & POLLWRNORM)
1428 pfd->revents |= POLLWRNORM;
1433 * EV_NODATA should never be tagged for this
1436 if (pfd->events & POLLPRI)
1437 pfd->revents |= POLLPRI;
1438 if (pfd->events & POLLRDBAND)
1439 pfd->revents |= POLLRDBAND;
1444 kprintf("poll index %d/%d fd %d revents %08x\n",
1445 pi, pkap->nfds, pfd->fd, pfd->revents);
1448 if (count_res && pfd->revents)
1452 kprintf("poll index %d mismatch %ju/%d\n",
1453 pi, (uintmax_t)kevp[i].ident, pfd->fd);
1462 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1464 struct poll_kevent_copyin_args ka;
1465 struct pollfd sfds[64];
1473 if (nfds == 0 && ts)
1474 return (dotimeout_only(ts));
1477 * This is a bit arbitrary but we need to limit internal kmallocs.
1479 if (nfds > maxfilesperproc * 2)
1480 nfds = maxfilesperproc * 2;
1481 bytes = sizeof(struct pollfd) * nfds;
1483 ka.lwp = curthread->td_lwp;
1491 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1493 error = copyin(fds, ka.fds, bytes);
1495 error = kern_kevent(&ka.lwp->lwp_kqueue, 0x7FFFFFFF, res, &ka,
1496 poll_copyin, poll_copyout, ts);
1499 error = copyout(ka.fds, fds, bytes);
1502 kfree(ka.fds, M_SELECT);
1504 ka.lwp->lwp_kqueue_serial += nfds;
1510 socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1516 socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1522 extern struct fileops socketops;
1525 * NOTE: Callers of socket_wait() must already have a reference on the
1529 socket_wait(struct socket *so, struct timespec *ts, int *res)
1531 struct thread *td = curthread;
1537 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
1540 fp->f_type = DTYPE_SOCKET;
1541 fp->f_flag = FREAD | FWRITE;
1542 fp->f_ops = &socketops;
1544 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
1546 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1547 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
1548 if ((error = kqueue_register(&kq, &kev)) != 0) {
1553 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1554 socket_wait_copyout, ts);
1556 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
1557 kqueue_register(&kq, &kev);
1558 fp->f_ops = &badfileops;
1565 * OpenBSD poll system call.
1566 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1571 sys_openbsd_poll(struct openbsd_poll_args *uap)
1573 return (sys_poll((struct poll_args *)uap));
1578 seltrue(cdev_t dev, int events)
1580 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));