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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/sys_generic.c,v 1.55.2.10 2001/03/17 10:39:32 peter Exp $
40 * $DragonFly: src/sys/kern/sys_generic.c,v 1.49 2008/05/05 22:09:44 dillon Exp $
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/event.h>
49 #include <sys/filedesc.h>
50 #include <sys/filio.h>
51 #include <sys/fcntl.h>
54 #include <sys/signalvar.h>
55 #include <sys/socketvar.h>
57 #include <sys/kernel.h>
58 #include <sys/kern_syscall.h>
59 #include <sys/malloc.h>
60 #include <sys/mapped_ioctl.h>
62 #include <sys/queue.h>
63 #include <sys/resourcevar.h>
64 #include <sys/socketops.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
69 #include <sys/ktrace.h>
72 #include <vm/vm_page.h>
74 #include <sys/file2.h>
75 #include <sys/mplock2.h>
76 #include <sys/spinlock2.h>
78 #include <machine/limits.h>
80 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
81 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
82 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
83 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
85 typedef struct kfd_set {
89 enum select_copyin_states {
90 COPYIN_READ, COPYIN_WRITE, COPYIN_EXCEPT, COPYIN_DONE };
92 struct select_kevent_copyin_args {
96 int active_set; /* One of select_copyin_states */
97 struct lwp *lwp; /* Pointer to our lwp */
98 int num_fds; /* Number of file descriptors (syscall arg) */
99 int proc_fds; /* Processed fd's (wraps) */
100 int error; /* Returned to userland */
103 struct poll_kevent_copyin_args {
111 static struct lwkt_token mioctl_token = LWKT_TOKEN_INITIALIZER(mioctl_token);
113 static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
114 struct timespec *ts, int *res);
115 static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
117 static int dofileread(int, struct file *, struct uio *, int, size_t *);
118 static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
126 sys_read(struct read_args *uap)
128 struct thread *td = curthread;
133 if ((ssize_t)uap->nbyte < 0)
136 aiov.iov_base = uap->buf;
137 aiov.iov_len = uap->nbyte;
138 auio.uio_iov = &aiov;
140 auio.uio_offset = -1;
141 auio.uio_resid = uap->nbyte;
142 auio.uio_rw = UIO_READ;
143 auio.uio_segflg = UIO_USERSPACE;
146 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
151 * Positioned (Pread) read system call
156 sys_extpread(struct extpread_args *uap)
158 struct thread *td = curthread;
164 if ((ssize_t)uap->nbyte < 0)
167 aiov.iov_base = uap->buf;
168 aiov.iov_len = uap->nbyte;
169 auio.uio_iov = &aiov;
171 auio.uio_offset = uap->offset;
172 auio.uio_resid = uap->nbyte;
173 auio.uio_rw = UIO_READ;
174 auio.uio_segflg = UIO_USERSPACE;
177 flags = uap->flags & O_FMASK;
178 if (uap->offset != (off_t)-1)
181 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
186 * Scatter read system call.
191 sys_readv(struct readv_args *uap)
193 struct thread *td = curthread;
195 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
198 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
203 auio.uio_iovcnt = uap->iovcnt;
204 auio.uio_offset = -1;
205 auio.uio_rw = UIO_READ;
206 auio.uio_segflg = UIO_USERSPACE;
209 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
211 iovec_free(&iov, aiov);
217 * Scatter positioned read system call.
222 sys_extpreadv(struct extpreadv_args *uap)
224 struct thread *td = curthread;
226 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
230 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
235 auio.uio_iovcnt = uap->iovcnt;
236 auio.uio_offset = uap->offset;
237 auio.uio_rw = UIO_READ;
238 auio.uio_segflg = UIO_USERSPACE;
241 flags = uap->flags & O_FMASK;
242 if (uap->offset != (off_t)-1)
245 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
247 iovec_free(&iov, aiov);
255 kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
257 struct thread *td = curthread;
258 struct proc *p = td->td_proc;
264 fp = holdfp(p->p_fd, fd, FREAD);
267 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
270 error = dofileread(fd, fp, auio, flags, res);
277 * Common code for readv and preadv that reads data in
278 * from a file using the passed in uio, offset, and flags.
280 * MPALMOSTSAFE - ktrace needs help
283 dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
288 struct thread *td = curthread;
289 struct iovec *ktriov = NULL;
295 * if tracing, save a copy of iovec
297 if (KTRPOINT(td, KTR_GENIO)) {
298 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
300 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
301 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
305 len = auio->uio_resid;
306 error = fo_read(fp, auio, fp->f_cred, flags);
308 if (auio->uio_resid != len && (error == ERESTART ||
309 error == EINTR || error == EWOULDBLOCK))
313 if (ktriov != NULL) {
315 ktruio.uio_iov = ktriov;
316 ktruio.uio_resid = len - auio->uio_resid;
318 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
321 FREE(ktriov, M_TEMP);
325 *res = len - auio->uio_resid;
336 sys_write(struct write_args *uap)
338 struct thread *td = curthread;
343 if ((ssize_t)uap->nbyte < 0)
346 aiov.iov_base = (void *)(uintptr_t)uap->buf;
347 aiov.iov_len = uap->nbyte;
348 auio.uio_iov = &aiov;
350 auio.uio_offset = -1;
351 auio.uio_resid = uap->nbyte;
352 auio.uio_rw = UIO_WRITE;
353 auio.uio_segflg = UIO_USERSPACE;
356 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
367 sys_extpwrite(struct extpwrite_args *uap)
369 struct thread *td = curthread;
375 if ((ssize_t)uap->nbyte < 0)
378 aiov.iov_base = (void *)(uintptr_t)uap->buf;
379 aiov.iov_len = uap->nbyte;
380 auio.uio_iov = &aiov;
382 auio.uio_offset = uap->offset;
383 auio.uio_resid = uap->nbyte;
384 auio.uio_rw = UIO_WRITE;
385 auio.uio_segflg = UIO_USERSPACE;
388 flags = uap->flags & O_FMASK;
389 if (uap->offset != (off_t)-1)
391 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
399 sys_writev(struct writev_args *uap)
401 struct thread *td = curthread;
403 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
406 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
411 auio.uio_iovcnt = uap->iovcnt;
412 auio.uio_offset = -1;
413 auio.uio_rw = UIO_WRITE;
414 auio.uio_segflg = UIO_USERSPACE;
417 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
419 iovec_free(&iov, aiov);
425 * Gather positioned write system call
430 sys_extpwritev(struct extpwritev_args *uap)
432 struct thread *td = curthread;
434 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
438 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
443 auio.uio_iovcnt = uap->iovcnt;
444 auio.uio_offset = uap->offset;
445 auio.uio_rw = UIO_WRITE;
446 auio.uio_segflg = UIO_USERSPACE;
449 flags = uap->flags & O_FMASK;
450 if (uap->offset != (off_t)-1)
453 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
455 iovec_free(&iov, aiov);
463 kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
465 struct thread *td = curthread;
466 struct proc *p = td->td_proc;
472 fp = holdfp(p->p_fd, fd, FWRITE);
475 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
478 error = dofilewrite(fd, fp, auio, flags, res);
486 * Common code for writev and pwritev that writes data to
487 * a file using the passed in uio, offset, and flags.
489 * MPALMOSTSAFE - ktrace needs help
492 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
494 struct thread *td = curthread;
495 struct lwp *lp = td->td_lwp;
499 struct iovec *ktriov = NULL;
505 * if tracing, save a copy of iovec and uio
507 if (KTRPOINT(td, KTR_GENIO)) {
508 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
510 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
511 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
515 len = auio->uio_resid;
516 error = fo_write(fp, auio, fp->f_cred, flags);
518 if (auio->uio_resid != len && (error == ERESTART ||
519 error == EINTR || error == EWOULDBLOCK))
521 /* Socket layer is responsible for issuing SIGPIPE. */
523 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
526 if (ktriov != NULL) {
528 ktruio.uio_iov = ktriov;
529 ktruio.uio_resid = len - auio->uio_resid;
531 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
534 FREE(ktriov, M_TEMP);
538 *res = len - auio->uio_resid;
549 sys_ioctl(struct ioctl_args *uap)
553 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
557 struct ioctl_map_entry {
559 struct ioctl_map_range *cmd_ranges;
560 LIST_ENTRY(ioctl_map_entry) entries;
564 * The true heart of all ioctl syscall handlers (native, emulation).
565 * If map != NULL, it will be searched for a matching entry for com,
566 * and appropriate conversions/conversion functions will be utilized.
571 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
574 struct thread *td = curthread;
575 struct proc *p = td->td_proc;
578 struct ioctl_map_range *iomc = NULL;
584 #define STK_PARAMS 128
586 char stkbuf[STK_PARAMS];
593 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
597 if (map != NULL) { /* obey translation map */
599 struct ioctl_map_entry *e;
601 maskcmd = com & map->mask;
603 lwkt_gettoken(&mioctl_token);
604 LIST_FOREACH(e, &map->mapping, entries) {
605 for (iomc = e->cmd_ranges; iomc->start != 0 ||
606 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
607 iomc->mapfunc != NULL;
609 if (maskcmd >= iomc->start &&
610 maskcmd <= iomc->end)
614 /* Did we find a match? */
615 if (iomc->start != 0 || iomc->maptocmd != 0 ||
616 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
619 lwkt_reltoken(&mioctl_token);
622 (iomc->start == 0 && iomc->maptocmd == 0
623 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
624 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
625 map->sys, fd, maskcmd,
626 (int)((maskcmd >> 8) & 0xff),
627 (int)(maskcmd & 0xff));
633 * If it's a non-range one to one mapping, maptocmd should be
634 * correct. If it's a ranged one to one mapping, we pass the
635 * original value of com, and for a range mapped to a different
636 * range, we always need a mapping function to translate the
637 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
639 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
640 com = iomc->maptocmd;
641 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
642 if (iomc->mapfunc != NULL)
643 com = iomc->mapfunc(iomc->start, iomc->end,
644 iomc->start, iomc->end,
647 if (iomc->mapfunc != NULL) {
648 com = iomc->mapfunc(iomc->start, iomc->end,
649 iomc->maptocmd, iomc->maptoend,
652 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
653 map->sys, fd, maskcmd,
654 (int)((maskcmd >> 8) & 0xff),
655 (int)(maskcmd & 0xff));
664 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
667 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
672 * Interpret high order word to find amount of data to be
673 * copied to/from the user's address space.
675 size = IOCPARM_LEN(com);
676 if (size > IOCPARM_MAX) {
681 if (size > sizeof (ubuf.stkbuf)) {
682 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
688 if ((com & IOC_IN) != 0) {
690 error = copyin(uspc_data, data, (size_t)size);
693 kfree(memp, M_IOCTLOPS);
697 *(caddr_t *)data = uspc_data;
699 } else if ((com & IOC_OUT) != 0 && size) {
701 * Zero the buffer so the user always
702 * gets back something deterministic.
704 bzero(data, (size_t)size);
705 } else if ((com & IOC_VOID) != 0) {
706 *(caddr_t *)data = uspc_data;
711 if ((tmp = *(int *)data))
712 atomic_set_int(&fp->f_flag, FNONBLOCK);
714 atomic_clear_int(&fp->f_flag, FNONBLOCK);
719 if ((tmp = *(int *)data))
720 atomic_set_int(&fp->f_flag, FASYNC);
722 atomic_clear_int(&fp->f_flag, FASYNC);
723 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
728 * If there is a override function,
729 * call it instead of directly routing the call
731 if (map != NULL && iomc->wrapfunc != NULL)
732 error = iomc->wrapfunc(fp, com, ocom, data, cred);
734 error = fo_ioctl(fp, com, data, cred, msg);
736 * Copy any data to user, size was
737 * already set and checked above.
739 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
740 error = copyout(data, uspc_data, (size_t)size);
744 kfree(memp, M_IOCTLOPS);
754 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
756 struct ioctl_map_entry *ne;
758 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
759 he->subsys != NULL && *he->subsys != '\0');
761 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP,
764 ne->subsys = he->subsys;
765 ne->cmd_ranges = he->cmd_ranges;
767 lwkt_gettoken(&mioctl_token);
768 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
769 lwkt_reltoken(&mioctl_token);
778 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
780 struct ioctl_map_entry *ne;
783 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
785 lwkt_gettoken(&mioctl_token);
786 LIST_FOREACH(ne, &he->map->mapping, entries) {
787 if (ne->cmd_ranges == he->cmd_ranges) {
788 LIST_REMOVE(ne, entries);
789 kfree(ne, M_IOCTLMAP);
794 lwkt_reltoken(&mioctl_token);
798 static int nselcoll; /* Select collisions since boot */
800 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
801 static int nseldebug;
802 SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
805 * Select system call.
810 sys_select(struct select_args *uap)
813 struct timespec *ktsp, kts;
817 * Get timeout if any.
819 if (uap->tv != NULL) {
820 error = copyin(uap->tv, &ktv, sizeof (ktv));
823 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
832 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
833 &uap->sysmsg_result);
840 * Pselect system call.
843 sys_pselect(struct pselect_args *uap)
845 struct thread *td = curthread;
846 struct lwp *lp = td->td_lwp;
847 struct timespec *ktsp, kts;
852 * Get timeout if any.
854 if (uap->ts != NULL) {
855 error = copyin(uap->ts, &kts, sizeof (kts));
864 * Install temporary signal mask if any provided.
866 if (uap->sigmask != NULL) {
867 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
870 lwkt_gettoken(&lp->lwp_proc->p_token);
871 lp->lwp_oldsigmask = lp->lwp_sigmask;
872 SIG_CANTMASK(sigmask);
873 lp->lwp_sigmask = sigmask;
874 lwkt_reltoken(&lp->lwp_proc->p_token);
880 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
881 &uap->sysmsg_result);
883 if (uap->sigmask != NULL) {
884 lwkt_gettoken(&lp->lwp_proc->p_token);
885 /* doselect() responsible for turning ERESTART into EINTR */
886 KKASSERT(error != ERESTART);
887 if (error == EINTR) {
889 * We can't restore the previous signal mask now
890 * because it could block the signal that interrupted
891 * us. So make a note to restore it after executing
894 lp->lwp_flag |= LWP_OLDMASK;
897 * No handler to run. Restore previous mask immediately.
899 lp->lwp_sigmask = lp->lwp_oldsigmask;
901 lwkt_reltoken(&lp->lwp_proc->p_token);
908 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
910 struct select_kevent_copyin_args *skap = NULL;
917 skap = (struct select_kevent_copyin_args *)arg;
919 if (*events == maxevents)
922 while (skap->active_set < COPYIN_DONE) {
923 switch (skap->active_set) {
926 * Register descriptors for the read filter
928 fdp = skap->read_set;
929 filter = EVFILT_READ;
930 fflags = NOTE_OLDAPI;
938 * Register descriptors for the write filter
940 fdp = skap->write_set;
941 filter = EVFILT_WRITE;
942 fflags = NOTE_OLDAPI;
950 * Register descriptors for the exception filter
952 fdp = skap->except_set;
953 filter = EVFILT_EXCEPT;
954 fflags = NOTE_OLDAPI | NOTE_OOB;
962 * Nothing left to register
968 while (skap->proc_fds < skap->num_fds) {
970 if (FD_ISSET(fd, fdp)) {
971 kev = &kevp[*events];
972 EV_SET(kev, fd, filter,
976 skap->lwp->lwp_kqueue_serial);
981 kprintf("select fd %d filter %d serial %d\n",
982 fd, filter, skap->lwp->lwp_kqueue_serial);
985 if (*events == maxevents)
996 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
998 struct select_kevent_copyin_args *skap;
1002 skap = (struct select_kevent_copyin_args *)arg;
1004 for (i = 0; i < count; ++i) {
1006 * Filter out and delete spurious events
1008 if ((u_int)(uintptr_t)kevp[i].udata !=
1009 skap->lwp->lwp_kqueue_serial) {
1011 kev.flags = EV_DISABLE|EV_DELETE;
1012 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
1014 kprintf("select fd %ju mismatched serial %d\n",
1015 (uintmax_t)kevp[i].ident,
1016 skap->lwp->lwp_kqueue_serial);
1023 if (kevp[i].flags & EV_ERROR) {
1024 int error = kevp[i].data;
1029 * A bad file descriptor is considered a
1030 * fatal error for select, bail out.
1032 skap->error = error;
1038 * Select silently swallows any unknown errors
1039 * for descriptors in the read or write sets.
1041 * ALWAYS filter out EOPNOTSUPP errors from
1042 * filters (at least until all filters support
1045 if (kevp[i].filter != EVFILT_READ &&
1046 kevp[i].filter != EVFILT_WRITE &&
1047 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 * Common code for sys_select() and sys_pselect().
1120 * in, out and ex are userland pointers. ts must point to validated
1121 * kernel-side timeout value or NULL for infinite timeout. res must
1122 * point to syscall return value.
1125 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1126 struct timespec *ts, int *res)
1128 struct proc *p = curproc;
1129 struct select_kevent_copyin_args *kap, ka;
1138 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1139 nd = p->p_fd->fd_nfiles;
1142 kap->lwp = curthread->td_lwp;
1146 kap->active_set = COPYIN_READ;
1149 * Calculate bytes based on the number of __fd_mask[] array entries
1150 * multiplied by the size of __fd_mask.
1152 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1154 /* kap->read_set = NULL; not needed */
1155 kap->write_set = NULL;
1156 kap->except_set = NULL;
1158 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1160 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1162 error = getbits(bytes, except, &kap->except_set, &except_tmp);
1167 * NOTE: Make sure the max events passed to kern_kevent() is
1168 * effectively unlimited. (nd * 3) accomplishes this.
1170 * (*res) continues to increment as returned events are
1173 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1174 select_copyin, select_copyout, ts);
1176 error = putbits(bytes, kap->read_set, read);
1178 error = putbits(bytes, kap->write_set, write);
1180 error = putbits(bytes, kap->except_set, except);
1183 * An error from an individual event that should be passed
1184 * back to userland (EBADF)
1193 if (kap->read_set && kap->read_set != &read_tmp)
1194 kfree(kap->read_set, M_SELECT);
1195 if (kap->write_set && kap->write_set != &write_tmp)
1196 kfree(kap->write_set, M_SELECT);
1197 if (kap->except_set && kap->except_set != &except_tmp)
1198 kfree(kap->except_set, M_SELECT);
1200 kap->lwp->lwp_kqueue_serial += kap->num_fds;
1211 sys_poll(struct poll_args *uap)
1213 struct timespec ts, *tsp;
1216 if (uap->timeout != INFTIM) {
1217 ts.tv_sec = uap->timeout / 1000;
1218 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1224 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1230 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1232 struct poll_kevent_copyin_args *pkap;
1237 pkap = (struct poll_kevent_copyin_args *)arg;
1239 while (pkap->pfds < pkap->nfds) {
1240 pfd = &pkap->fds[pkap->pfds];
1242 /* Clear return events */
1245 /* Do not check if fd is equal to -1 */
1246 if (pfd->fd == -1) {
1252 if (pfd->events & (POLLIN | POLLRDNORM))
1254 if (pfd->events & (POLLOUT | POLLWRNORM))
1256 if (pfd->events & (POLLPRI | POLLRDBAND))
1259 if (*events + kev_count > maxevents)
1263 * NOTE: A combined serial number and poll array index is
1264 * stored in kev->udata.
1266 kev = &kevp[*events];
1267 if (pfd->events & (POLLIN | POLLRDNORM)) {
1268 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1269 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1270 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1272 if (pfd->events & (POLLOUT | POLLWRNORM)) {
1273 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1274 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1275 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1277 if (pfd->events & (POLLPRI | POLLRDBAND)) {
1278 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1279 NOTE_OLDAPI | NOTE_OOB, 0,
1281 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1285 kprintf("poll index %d/%d fd %d events %08x serial %d\n",
1286 pkap->pfds, pkap->nfds-1, pfd->fd, pfd->events,
1287 pkap->lwp->lwp_kqueue_serial);
1291 (*events) += kev_count;
1298 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1300 struct poll_kevent_copyin_args *pkap;
1307 pkap = (struct poll_kevent_copyin_args *)arg;
1309 for (i = 0; i < count; ++i) {
1311 * Extract the poll array index and delete spurious events.
1312 * We can easily tell if the serial number is incorrect
1313 * by checking whether the extracted index is out of range.
1315 pi = (u_int)(uintptr_t)kevp[i].udata -
1316 (u_int)pkap->lwp->lwp_kqueue_serial;
1318 if (pi >= pkap->nfds) {
1320 kev.flags = EV_DISABLE|EV_DELETE;
1321 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1323 kprintf("poll index %d out of range against serial %d\n",
1324 pi, pkap->lwp->lwp_kqueue_serial);
1327 pfd = &pkap->fds[pi];
1328 if (kevp[i].ident == pfd->fd) {
1330 * A single descriptor may generate an error against
1331 * more than one filter, make sure to set the
1332 * appropriate flags but do not increment (*res)
1335 count_res = (pfd->revents == 0);
1336 if (kevp[i].flags & EV_ERROR) {
1337 switch(kevp[i].data) {
1339 /* Bad file descriptor */
1342 pfd->revents |= POLLNVAL;
1346 * Poll silently swallows any unknown
1347 * errors except in the case of POLLPRI
1348 * (OOB/urgent data).
1350 * ALWAYS filter out EOPNOTSUPP errors
1351 * from filters, common applications
1352 * set POLLPRI|POLLRDBAND and most
1353 * filters do not support EVFILT_EXCEPT.
1355 if (kevp[i].filter != EVFILT_READ &&
1356 kevp[i].filter != EVFILT_WRITE &&
1357 kevp[i].data != EOPNOTSUPP) {
1360 pfd->revents |= POLLERR;
1365 kprintf("poll index %d fd %d "
1366 "filter %d error %jd\n",
1369 (intmax_t)kevp[i].data);
1374 switch (kevp[i].filter) {
1378 * NODATA on the read side can indicate a
1379 * half-closed situation and not necessarily
1380 * a disconnect, so depend on the user
1381 * issuing a read() and getting 0 bytes back.
1383 if (kevp[i].flags & EV_NODATA)
1384 pfd->revents |= POLLHUP;
1386 if ((kevp[i].flags & EV_EOF) &&
1387 kevp[i].fflags != 0)
1388 pfd->revents |= POLLERR;
1389 if (pfd->events & POLLIN)
1390 pfd->revents |= POLLIN;
1391 if (pfd->events & POLLRDNORM)
1392 pfd->revents |= POLLRDNORM;
1396 * As per the OpenGroup POLLHUP is mutually
1397 * exclusive with the writability flags. I
1398 * consider this a bit broken but...
1400 * In this case a disconnect is implied even
1401 * for a half-closed (write side) situation.
1403 if (kevp[i].flags & EV_EOF) {
1404 pfd->revents |= POLLHUP;
1405 if (kevp[i].fflags != 0)
1406 pfd->revents |= POLLERR;
1408 if (pfd->events & POLLOUT)
1409 pfd->revents |= POLLOUT;
1410 if (pfd->events & POLLWRNORM)
1411 pfd->revents |= POLLWRNORM;
1416 * EV_NODATA should never be tagged for this
1419 if (pfd->events & POLLPRI)
1420 pfd->revents |= POLLPRI;
1421 if (pfd->events & POLLRDBAND)
1422 pfd->revents |= POLLRDBAND;
1427 kprintf("poll index %d/%d fd %d revents %08x\n",
1428 pi, pkap->nfds, pfd->fd, pfd->revents);
1431 if (count_res && pfd->revents)
1435 kprintf("poll index %d mismatch %ju/%d\n",
1436 pi, (uintmax_t)kevp[i].ident, pfd->fd);
1445 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1447 struct poll_kevent_copyin_args ka;
1448 struct pollfd sfds[64];
1457 * This is a bit arbitrary but we need to limit internal kmallocs.
1459 if (nfds > maxfilesperproc * 2)
1460 nfds = maxfilesperproc * 2;
1461 bytes = sizeof(struct pollfd) * nfds;
1463 ka.lwp = curthread->td_lwp;
1471 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1473 error = copyin(fds, ka.fds, bytes);
1475 error = kern_kevent(&ka.lwp->lwp_kqueue, 0x7FFFFFFF, res, &ka,
1476 poll_copyin, poll_copyout, ts);
1479 error = copyout(ka.fds, fds, bytes);
1482 kfree(ka.fds, M_SELECT);
1484 ka.lwp->lwp_kqueue_serial += nfds;
1490 socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1496 socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1502 extern struct fileops socketops;
1505 * NOTE: Callers of socket_wait() must already have a reference on the
1509 socket_wait(struct socket *so, struct timespec *ts, int *res)
1511 struct thread *td = curthread;
1517 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
1520 fp->f_type = DTYPE_SOCKET;
1521 fp->f_flag = FREAD | FWRITE;
1522 fp->f_ops = &socketops;
1524 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
1526 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1527 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
1528 if ((error = kqueue_register(&kq, &kev)) != 0) {
1533 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1534 socket_wait_copyout, ts);
1536 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
1537 kqueue_register(&kq, &kev);
1538 fp->f_ops = &badfileops;
1545 * OpenBSD poll system call.
1546 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1551 sys_openbsd_poll(struct openbsd_poll_args *uap)
1553 return (sys_poll((struct poll_args *)uap));
1558 seltrue(cdev_t dev, int events)
1560 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));