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 int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
112 struct timespec *ts, int *res);
113 static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
115 static int dofileread(int, struct file *, struct uio *, int, size_t *);
116 static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
124 sys_read(struct read_args *uap)
126 struct thread *td = curthread;
131 if ((ssize_t)uap->nbyte < 0)
134 aiov.iov_base = uap->buf;
135 aiov.iov_len = uap->nbyte;
136 auio.uio_iov = &aiov;
138 auio.uio_offset = -1;
139 auio.uio_resid = uap->nbyte;
140 auio.uio_rw = UIO_READ;
141 auio.uio_segflg = UIO_USERSPACE;
144 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
149 * Positioned (Pread) read system call
154 sys_extpread(struct extpread_args *uap)
156 struct thread *td = curthread;
162 if ((ssize_t)uap->nbyte < 0)
165 aiov.iov_base = uap->buf;
166 aiov.iov_len = uap->nbyte;
167 auio.uio_iov = &aiov;
169 auio.uio_offset = uap->offset;
170 auio.uio_resid = uap->nbyte;
171 auio.uio_rw = UIO_READ;
172 auio.uio_segflg = UIO_USERSPACE;
175 flags = uap->flags & O_FMASK;
176 if (uap->offset != (off_t)-1)
179 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
184 * Scatter read system call.
189 sys_readv(struct readv_args *uap)
191 struct thread *td = curthread;
193 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
196 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
201 auio.uio_iovcnt = uap->iovcnt;
202 auio.uio_offset = -1;
203 auio.uio_rw = UIO_READ;
204 auio.uio_segflg = UIO_USERSPACE;
207 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
209 iovec_free(&iov, aiov);
215 * Scatter positioned read system call.
220 sys_extpreadv(struct extpreadv_args *uap)
222 struct thread *td = curthread;
224 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
228 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
233 auio.uio_iovcnt = uap->iovcnt;
234 auio.uio_offset = uap->offset;
235 auio.uio_rw = UIO_READ;
236 auio.uio_segflg = UIO_USERSPACE;
239 flags = uap->flags & O_FMASK;
240 if (uap->offset != (off_t)-1)
243 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
245 iovec_free(&iov, aiov);
253 kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
255 struct thread *td = curthread;
256 struct proc *p = td->td_proc;
262 fp = holdfp(p->p_fd, fd, FREAD);
265 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
268 error = dofileread(fd, fp, auio, flags, res);
275 * Common code for readv and preadv that reads data in
276 * from a file using the passed in uio, offset, and flags.
278 * MPALMOSTSAFE - ktrace needs help
281 dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
286 struct thread *td = curthread;
287 struct iovec *ktriov = NULL;
293 * if tracing, save a copy of iovec
295 if (KTRPOINT(td, KTR_GENIO)) {
296 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
298 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
299 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
303 len = auio->uio_resid;
304 error = fo_read(fp, auio, fp->f_cred, flags);
306 if (auio->uio_resid != len && (error == ERESTART ||
307 error == EINTR || error == EWOULDBLOCK))
311 if (ktriov != NULL) {
313 ktruio.uio_iov = ktriov;
314 ktruio.uio_resid = len - auio->uio_resid;
316 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
319 FREE(ktriov, M_TEMP);
323 *res = len - auio->uio_resid;
334 sys_write(struct write_args *uap)
336 struct thread *td = curthread;
341 if ((ssize_t)uap->nbyte < 0)
344 aiov.iov_base = (void *)(uintptr_t)uap->buf;
345 aiov.iov_len = uap->nbyte;
346 auio.uio_iov = &aiov;
348 auio.uio_offset = -1;
349 auio.uio_resid = uap->nbyte;
350 auio.uio_rw = UIO_WRITE;
351 auio.uio_segflg = UIO_USERSPACE;
354 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
365 sys_extpwrite(struct extpwrite_args *uap)
367 struct thread *td = curthread;
373 if ((ssize_t)uap->nbyte < 0)
376 aiov.iov_base = (void *)(uintptr_t)uap->buf;
377 aiov.iov_len = uap->nbyte;
378 auio.uio_iov = &aiov;
380 auio.uio_offset = uap->offset;
381 auio.uio_resid = uap->nbyte;
382 auio.uio_rw = UIO_WRITE;
383 auio.uio_segflg = UIO_USERSPACE;
386 flags = uap->flags & O_FMASK;
387 if (uap->offset != (off_t)-1)
389 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
397 sys_writev(struct writev_args *uap)
399 struct thread *td = curthread;
401 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
404 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
409 auio.uio_iovcnt = uap->iovcnt;
410 auio.uio_offset = -1;
411 auio.uio_rw = UIO_WRITE;
412 auio.uio_segflg = UIO_USERSPACE;
415 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
417 iovec_free(&iov, aiov);
423 * Gather positioned write system call
428 sys_extpwritev(struct extpwritev_args *uap)
430 struct thread *td = curthread;
432 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
436 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
441 auio.uio_iovcnt = uap->iovcnt;
442 auio.uio_offset = uap->offset;
443 auio.uio_rw = UIO_WRITE;
444 auio.uio_segflg = UIO_USERSPACE;
447 flags = uap->flags & O_FMASK;
448 if (uap->offset != (off_t)-1)
451 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
453 iovec_free(&iov, aiov);
461 kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
463 struct thread *td = curthread;
464 struct proc *p = td->td_proc;
470 fp = holdfp(p->p_fd, fd, FWRITE);
473 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
476 error = dofilewrite(fd, fp, auio, flags, res);
484 * Common code for writev and pwritev that writes data to
485 * a file using the passed in uio, offset, and flags.
487 * MPALMOSTSAFE - ktrace needs help
490 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
492 struct thread *td = curthread;
493 struct lwp *lp = td->td_lwp;
497 struct iovec *ktriov = NULL;
503 * if tracing, save a copy of iovec and uio
505 if (KTRPOINT(td, KTR_GENIO)) {
506 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
508 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
509 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
513 len = auio->uio_resid;
514 error = fo_write(fp, auio, fp->f_cred, flags);
516 if (auio->uio_resid != len && (error == ERESTART ||
517 error == EINTR || error == EWOULDBLOCK))
519 /* Socket layer is responsible for issuing SIGPIPE. */
520 if (error == EPIPE) {
522 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
527 if (ktriov != NULL) {
529 ktruio.uio_iov = ktriov;
530 ktruio.uio_resid = len - auio->uio_resid;
532 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
535 FREE(ktriov, M_TEMP);
539 *res = len - auio->uio_resid;
550 sys_ioctl(struct ioctl_args *uap)
555 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
560 struct ioctl_map_entry {
562 struct ioctl_map_range *cmd_ranges;
563 LIST_ENTRY(ioctl_map_entry) entries;
567 * The true heart of all ioctl syscall handlers (native, emulation).
568 * If map != NULL, it will be searched for a matching entry for com,
569 * and appropriate conversions/conversion functions will be utilized.
572 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
575 struct thread *td = curthread;
576 struct proc *p = td->td_proc;
579 struct ioctl_map_range *iomc = NULL;
585 #define STK_PARAMS 128
587 char stkbuf[STK_PARAMS];
594 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
598 if (map != NULL) { /* obey translation map */
600 struct ioctl_map_entry *e;
602 maskcmd = com & map->mask;
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)
621 (iomc->start == 0 && iomc->maptocmd == 0
622 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
623 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
624 map->sys, fd, maskcmd,
625 (int)((maskcmd >> 8) & 0xff),
626 (int)(maskcmd & 0xff));
632 * If it's a non-range one to one mapping, maptocmd should be
633 * correct. If it's a ranged one to one mapping, we pass the
634 * original value of com, and for a range mapped to a different
635 * range, we always need a mapping function to translate the
636 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
638 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
639 com = iomc->maptocmd;
640 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
641 if (iomc->mapfunc != NULL)
642 com = iomc->mapfunc(iomc->start, iomc->end,
643 iomc->start, iomc->end,
646 if (iomc->mapfunc != NULL) {
647 com = iomc->mapfunc(iomc->start, iomc->end,
648 iomc->maptocmd, iomc->maptoend,
651 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
652 map->sys, fd, maskcmd,
653 (int)((maskcmd >> 8) & 0xff),
654 (int)(maskcmd & 0xff));
663 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
666 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
671 * Interpret high order word to find amount of data to be
672 * copied to/from the user's address space.
674 size = IOCPARM_LEN(com);
675 if (size > IOCPARM_MAX) {
681 if (size > sizeof (ubuf.stkbuf)) {
682 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
687 if ((com & IOC_IN) != 0) {
689 error = copyin(uspc_data, data, (size_t)size);
692 kfree(memp, M_IOCTLOPS);
696 *(caddr_t *)data = uspc_data;
698 } else if ((com & IOC_OUT) != 0 && size) {
700 * Zero the buffer so the user always
701 * gets back something deterministic.
703 bzero(data, (size_t)size);
704 } else if ((com & IOC_VOID) != 0) {
705 *(caddr_t *)data = uspc_data;
710 if ((tmp = *(int *)data))
711 fp->f_flag |= FNONBLOCK;
713 fp->f_flag &= ~FNONBLOCK;
718 if ((tmp = *(int *)data))
719 fp->f_flag |= FASYNC;
721 fp->f_flag &= ~FASYNC;
722 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
727 * If there is a override function,
728 * call it instead of directly routing the call
730 if (map != NULL && iomc->wrapfunc != NULL)
731 error = iomc->wrapfunc(fp, com, ocom, data, cred);
733 error = fo_ioctl(fp, com, data, cred, msg);
735 * Copy any data to user, size was
736 * already set and checked above.
738 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
739 error = copyout(data, uspc_data, (size_t)size);
743 kfree(memp, M_IOCTLOPS);
750 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
752 struct ioctl_map_entry *ne;
754 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
755 he->subsys != NULL && *he->subsys != '\0');
757 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
759 ne->subsys = he->subsys;
760 ne->cmd_ranges = he->cmd_ranges;
762 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
768 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
770 struct ioctl_map_entry *ne;
772 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
774 LIST_FOREACH(ne, &he->map->mapping, entries) {
775 if (ne->cmd_ranges != he->cmd_ranges)
777 LIST_REMOVE(ne, entries);
778 kfree(ne, M_IOCTLMAP);
784 static int nselcoll; /* Select collisions since boot */
786 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
787 static int nseldebug;
788 SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
791 * Select system call.
796 sys_select(struct select_args *uap)
799 struct timespec *ktsp, kts;
803 * Get timeout if any.
805 if (uap->tv != NULL) {
806 error = copyin(uap->tv, &ktv, sizeof (ktv));
809 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
818 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
819 &uap->sysmsg_result);
826 * 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;
840 * Get timeout if any.
842 if (uap->ts != NULL) {
843 error = copyin(uap->ts, &kts, sizeof (kts));
852 * Install temporary signal mask if any provided.
854 if (uap->sigmask != NULL) {
855 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
859 lp->lwp_oldsigmask = lp->lwp_sigmask;
860 SIG_CANTMASK(sigmask);
861 lp->lwp_sigmask = sigmask;
869 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
870 &uap->sysmsg_result);
872 if (uap->sigmask != NULL) {
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
882 lp->lwp_flag |= LWP_OLDMASK;
885 * No handler to run. Restore previous mask immediately.
887 lp->lwp_sigmask = lp->lwp_oldsigmask;
896 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
898 struct select_kevent_copyin_args *skap = NULL;
905 skap = (struct select_kevent_copyin_args *)arg;
907 if (*events == maxevents)
910 while (skap->active_set < COPYIN_DONE) {
911 switch (skap->active_set) {
914 * Register descriptors for the read filter
916 fdp = skap->read_set;
917 filter = EVFILT_READ;
926 * Register descriptors for the write filter
928 fdp = skap->write_set;
929 filter = EVFILT_WRITE;
938 * Register descriptors for the exception filter
940 fdp = skap->except_set;
941 filter = EVFILT_EXCEPT;
950 * Nothing left to register
956 while (skap->proc_fds < skap->num_fds) {
958 if (FD_ISSET(fd, fdp)) {
959 kev = &kevp[*events];
960 EV_SET(kev, fd, filter,
964 skap->lwp->lwp_kqueue_serial);
969 if (*events == maxevents)
980 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
982 struct select_kevent_copyin_args *skap;
986 skap = (struct select_kevent_copyin_args *)arg;
988 if (kevp[0].flags & EV_ERROR) {
989 skap->error = kevp[0].data;
993 for (i = 0; i < count; ++i) {
994 if ((u_int)(uintptr_t)kevp[i].udata !=
995 skap->lwp->lwp_kqueue_serial) {
997 kev.flags = EV_DISABLE|EV_DELETE;
998 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
1002 switch (kevp[i].filter) {
1004 FD_SET(kevp[i].ident, skap->read_set);
1007 FD_SET(kevp[i].ident, skap->write_set);
1010 FD_SET(kevp[i].ident, skap->except_set);
1021 * Copy select bits in from userland. Allocate kernel memory if the
1025 getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1030 if (bytes < sizeof(*tmp_set))
1033 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1034 error = copyin(in_set, *out_set, bytes);
1043 * Copy returned select bits back out to userland.
1046 putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1051 error = copyout(in_set, out_set, bytes);
1059 * Common code for sys_select() and sys_pselect().
1061 * in, out and ex are userland pointers. ts must point to validated
1062 * kernel-side timeout value or NULL for infinite timeout. res must
1063 * point to syscall return value.
1066 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1067 struct timespec *ts, int *res)
1069 struct proc *p = curproc;
1070 struct select_kevent_copyin_args *kap, ka;
1079 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1080 nd = p->p_fd->fd_nfiles;
1083 kap->lwp = curthread->td_lwp;
1087 kap->active_set = COPYIN_READ;
1090 * Calculate bytes based on the number of __fd_mask[] array entries
1091 * multiplied by the size of __fd_mask.
1093 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1095 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1097 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1099 error = getbits(bytes, except, &kap->except_set, &except_tmp);
1104 * NOTE: Make sure the max events passed to kern_kevent() is
1105 * effectively unlimited. (nd * 3) accomplishes this.
1107 * (*res) continues to increment as returned events are
1110 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1111 select_copyin, select_copyout, ts);
1113 error = putbits(bytes, kap->read_set, read);
1115 error = putbits(bytes, kap->write_set, write);
1117 error = putbits(bytes, kap->except_set, except);
1120 * Cumulative error from individual events (EBADFD?)
1129 if (kap->read_set && kap->read_set != &read_tmp)
1130 kfree(kap->read_set, M_SELECT);
1131 if (kap->write_set && kap->write_set != &write_tmp)
1132 kfree(kap->write_set, M_SELECT);
1133 if (kap->except_set && kap->except_set != &except_tmp)
1134 kfree(kap->except_set, M_SELECT);
1136 kap->lwp->lwp_kqueue_serial++;
1147 sys_poll(struct poll_args *uap)
1149 struct timespec ts, *tsp;
1152 if (uap->timeout != INFTIM) {
1153 ts.tv_sec = uap->timeout / 1000;
1154 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1160 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1166 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1168 struct poll_kevent_copyin_args *pkap;
1173 pkap = (struct poll_kevent_copyin_args *)arg;
1175 while (pkap->pfds < pkap->nfds) {
1176 pfd = &pkap->fds[pkap->pfds];
1178 /* Clear return events */
1181 /* Do not check if fd is equal to -1 */
1182 if (pfd->fd == -1) {
1188 if (pfd->events & (POLLIN | POLLRDNORM))
1190 if (pfd->events & (POLLOUT | POLLWRNORM))
1192 if (pfd->events & (POLLPRI | POLLRDBAND))
1195 if (*events + kev_count > maxevents)
1199 * NOTE: A combined serial number and poll array index is
1200 * stored in kev->udata.
1202 kev = &kevp[*events];
1203 if (pfd->events & (POLLIN | POLLRDNORM)) {
1204 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1205 0, 0, (void *)(uintptr_t)
1206 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1208 if (pfd->events & (POLLOUT | POLLWRNORM)) {
1209 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1210 0, 0, (void *)(uintptr_t)
1211 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1213 if (pfd->events & (POLLPRI | POLLRDBAND)) {
1214 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1217 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1221 kprintf("poll index %d fd %d events %08x\n",
1222 pkap->pfds, pfd->fd, pfd->events);
1226 (*events) += kev_count;
1233 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1235 struct poll_kevent_copyin_args *pkap;
1241 pkap = (struct poll_kevent_copyin_args *)arg;
1243 for (i = 0; i < count; ++i) {
1245 * Extract the poll array index and delete spurious events.
1246 * We can easily tell if the serial number is incorrect
1247 * by checking whether the extracted index is out of range.
1249 pi = (u_int)(uintptr_t)kevp[i].udata -
1250 (u_int)pkap->lwp->lwp_kqueue_serial;
1252 if (pi >= pkap->nfds) {
1254 kev.flags = EV_DISABLE|EV_DELETE;
1255 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1257 kprintf("poll index %d out of range\n", pi);
1260 pfd = &pkap->fds[pi];
1261 if (kevp[i].ident == pfd->fd) {
1262 if (kevp[i].flags & EV_ERROR) {
1263 switch(kevp[i].data) {
1266 * Operation not supported. Poll
1267 * does not return an error for
1268 * POLLPRI (OOB/urgent data) when
1269 * it is not supported by the device.
1271 if (kevp[i].filter != EVFILT_EXCEPT) {
1272 pfd->revents |= POLLERR;
1277 /* Bad file descriptor */
1278 pfd->revents |= POLLNVAL;
1282 pfd->revents |= POLLERR;
1287 kprintf("poll index %d fd %d "
1288 "filter %d error %d\n",
1291 (u_int)kevp[i].data);
1296 if (kevp[i].flags & EV_EOF) {
1297 pfd->revents |= POLLHUP;
1302 switch (kevp[i].filter) {
1304 pfd->revents |= (POLLIN | POLLRDNORM);
1307 pfd->revents |= (POLLOUT | POLLWRNORM);
1310 pfd->revents |= (POLLPRI | POLLRDBAND);
1315 kprintf("poll index %d fd %d revents %08x\n",
1316 pi, pfd->fd, pfd->revents);
1323 kprintf("poll index %d mismatch %d/%d\n",
1324 pi, (u_int)kevp[i].ident, pfd->fd);
1333 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1335 struct poll_kevent_copyin_args ka;
1336 struct pollfd sfds[64];
1345 * This is a bit arbitrary but we need to limit internal kmallocs.
1347 if (nfds > maxfilesperproc * 2)
1348 nfds = maxfilesperproc * 2;
1349 bytes = sizeof(struct pollfd) * nfds;
1351 ka.lwp = curthread->td_lwp;
1359 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1361 error = copyin(fds, ka.fds, bytes);
1363 error = kern_kevent(&ka.lwp->lwp_kqueue, ka.nfds, res, &ka,
1364 poll_copyin, poll_copyout, ts);
1367 error = copyout(ka.fds, fds, bytes);
1370 kfree(ka.fds, M_SELECT);
1372 ka.lwp->lwp_kqueue_serial += nfds;
1378 socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1384 socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1390 extern struct fileops socketops;
1392 socket_wait(struct socket *so, struct timespec *ts, int *res)
1394 struct thread *td = curthread;
1400 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
1403 fp->f_type = DTYPE_SOCKET;
1404 fp->f_flag = FREAD | FWRITE;
1405 fp->f_ops = &socketops;
1407 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
1409 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1410 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
1411 if ((error = kqueue_register(&kq, &kev)) != 0) {
1416 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1417 socket_wait_copyout, ts);
1419 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
1420 kqueue_register(&kq, &kev);
1421 fp->f_ops = &badfileops;
1428 * OpenBSD poll system call.
1429 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1434 sys_openbsd_poll(struct openbsd_poll_args *uap)
1436 return (sys_poll((struct poll_args *)uap));
1441 seltrue(cdev_t dev, int events)
1443 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1447 * Do a wakeup when a selectable event occurs.
1450 selwakeup(struct selinfo *sip)
1453 struct lwp *lp = NULL;
1455 if (sip->si_pid == 0)
1457 if (sip->si_flags & SI_COLL) {
1459 sip->si_flags &= ~SI_COLL;
1460 wakeup((caddr_t)&selwait); /* YYY fixable */
1462 p = pfind(sip->si_pid);
1466 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1471 * This is a temporary hack until the code can be rewritten.
1472 * Check LWP_SELECT before assuming we can setrunnable().
1473 * Otherwise we might catch the lwp before it actually goes to
1477 if (lp->lwp_flag & LWP_SELECT) {
1478 lp->lwp_flag &= ~LWP_SELECT;
1479 } else if (lp->lwp_wchan == (caddr_t)&selwait) {
1481 * Flag the process to break the tsleep when
1482 * setrunnable is called, but only call setrunnable
1483 * here if the process is not in a stopped state.
1485 lp->lwp_flag |= LWP_BREAKTSLEEP;
1486 if (p->p_stat != SSTOP)
1491 kqueue_wakeup(&lp->lwp_kqueue);