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/sysctl.h>
65 #include <sys/sysent.h>
68 #include <sys/ktrace.h>
71 #include <vm/vm_page.h>
73 #include <sys/file2.h>
74 #include <sys/mplock2.h>
76 #include <machine/limits.h>
78 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
79 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
80 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
81 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
83 typedef struct kfd_set {
87 enum select_copyin_states {
88 COPYIN_READ, COPYIN_WRITE, COPYIN_EXCEPT, COPYIN_DONE };
90 struct select_kevent_copyin_args {
94 int active_set; /* One of select_copyin_states */
95 struct lwp *lwp; /* Pointer to our lwp */
96 int num_fds; /* Number of file descriptors (syscall arg) */
97 int proc_fds; /* Processed fd's (wraps) */
98 int error; /* Returned to userland */
101 struct poll_kevent_copyin_args {
109 static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
110 struct timespec *ts, int *res);
111 static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
113 static int dofileread(int, struct file *, struct uio *, int, size_t *);
114 static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
122 sys_read(struct read_args *uap)
124 struct thread *td = curthread;
129 if ((ssize_t)uap->nbyte < 0)
132 aiov.iov_base = uap->buf;
133 aiov.iov_len = uap->nbyte;
134 auio.uio_iov = &aiov;
136 auio.uio_offset = -1;
137 auio.uio_resid = uap->nbyte;
138 auio.uio_rw = UIO_READ;
139 auio.uio_segflg = UIO_USERSPACE;
142 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
147 * Positioned (Pread) read system call
152 sys_extpread(struct extpread_args *uap)
154 struct thread *td = curthread;
160 if ((ssize_t)uap->nbyte < 0)
163 aiov.iov_base = uap->buf;
164 aiov.iov_len = uap->nbyte;
165 auio.uio_iov = &aiov;
167 auio.uio_offset = uap->offset;
168 auio.uio_resid = uap->nbyte;
169 auio.uio_rw = UIO_READ;
170 auio.uio_segflg = UIO_USERSPACE;
173 flags = uap->flags & O_FMASK;
174 if (uap->offset != (off_t)-1)
177 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
182 * Scatter read system call.
187 sys_readv(struct readv_args *uap)
189 struct thread *td = curthread;
191 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
194 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
199 auio.uio_iovcnt = uap->iovcnt;
200 auio.uio_offset = -1;
201 auio.uio_rw = UIO_READ;
202 auio.uio_segflg = UIO_USERSPACE;
205 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
207 iovec_free(&iov, aiov);
213 * Scatter positioned read system call.
218 sys_extpreadv(struct extpreadv_args *uap)
220 struct thread *td = curthread;
222 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
226 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
231 auio.uio_iovcnt = uap->iovcnt;
232 auio.uio_offset = uap->offset;
233 auio.uio_rw = UIO_READ;
234 auio.uio_segflg = UIO_USERSPACE;
237 flags = uap->flags & O_FMASK;
238 if (uap->offset != (off_t)-1)
241 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
243 iovec_free(&iov, aiov);
251 kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
253 struct thread *td = curthread;
254 struct proc *p = td->td_proc;
260 fp = holdfp(p->p_fd, fd, FREAD);
263 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
266 error = dofileread(fd, fp, auio, flags, res);
273 * Common code for readv and preadv that reads data in
274 * from a file using the passed in uio, offset, and flags.
276 * MPALMOSTSAFE - ktrace needs help
279 dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
284 struct thread *td = curthread;
285 struct iovec *ktriov = NULL;
291 * if tracing, save a copy of iovec
293 if (KTRPOINT(td, KTR_GENIO)) {
294 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
296 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
297 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
301 len = auio->uio_resid;
302 error = fo_read(fp, auio, fp->f_cred, flags);
304 if (auio->uio_resid != len && (error == ERESTART ||
305 error == EINTR || error == EWOULDBLOCK))
309 if (ktriov != NULL) {
311 ktruio.uio_iov = ktriov;
312 ktruio.uio_resid = len - auio->uio_resid;
314 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
317 FREE(ktriov, M_TEMP);
321 *res = len - auio->uio_resid;
332 sys_write(struct write_args *uap)
334 struct thread *td = curthread;
339 if ((ssize_t)uap->nbyte < 0)
342 aiov.iov_base = (void *)(uintptr_t)uap->buf;
343 aiov.iov_len = uap->nbyte;
344 auio.uio_iov = &aiov;
346 auio.uio_offset = -1;
347 auio.uio_resid = uap->nbyte;
348 auio.uio_rw = UIO_WRITE;
349 auio.uio_segflg = UIO_USERSPACE;
352 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
363 sys_extpwrite(struct extpwrite_args *uap)
365 struct thread *td = curthread;
371 if ((ssize_t)uap->nbyte < 0)
374 aiov.iov_base = (void *)(uintptr_t)uap->buf;
375 aiov.iov_len = uap->nbyte;
376 auio.uio_iov = &aiov;
378 auio.uio_offset = uap->offset;
379 auio.uio_resid = uap->nbyte;
380 auio.uio_rw = UIO_WRITE;
381 auio.uio_segflg = UIO_USERSPACE;
384 flags = uap->flags & O_FMASK;
385 if (uap->offset != (off_t)-1)
387 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
395 sys_writev(struct writev_args *uap)
397 struct thread *td = curthread;
399 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
402 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
407 auio.uio_iovcnt = uap->iovcnt;
408 auio.uio_offset = -1;
409 auio.uio_rw = UIO_WRITE;
410 auio.uio_segflg = UIO_USERSPACE;
413 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
415 iovec_free(&iov, aiov);
421 * Gather positioned write system call
426 sys_extpwritev(struct extpwritev_args *uap)
428 struct thread *td = curthread;
430 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
434 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
439 auio.uio_iovcnt = uap->iovcnt;
440 auio.uio_offset = uap->offset;
441 auio.uio_rw = UIO_WRITE;
442 auio.uio_segflg = UIO_USERSPACE;
445 flags = uap->flags & O_FMASK;
446 if (uap->offset != (off_t)-1)
449 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
451 iovec_free(&iov, aiov);
459 kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
461 struct thread *td = curthread;
462 struct proc *p = td->td_proc;
468 fp = holdfp(p->p_fd, fd, FWRITE);
471 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
474 error = dofilewrite(fd, fp, auio, flags, res);
482 * Common code for writev and pwritev that writes data to
483 * a file using the passed in uio, offset, and flags.
485 * MPALMOSTSAFE - ktrace needs help
488 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
490 struct thread *td = curthread;
491 struct lwp *lp = td->td_lwp;
495 struct iovec *ktriov = NULL;
501 * if tracing, save a copy of iovec and uio
503 if (KTRPOINT(td, KTR_GENIO)) {
504 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
506 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
507 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
511 len = auio->uio_resid;
512 error = fo_write(fp, auio, fp->f_cred, flags);
514 if (auio->uio_resid != len && (error == ERESTART ||
515 error == EINTR || error == EWOULDBLOCK))
517 /* Socket layer is responsible for issuing SIGPIPE. */
518 if (error == EPIPE) {
520 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
525 if (ktriov != NULL) {
527 ktruio.uio_iov = ktriov;
528 ktruio.uio_resid = len - auio->uio_resid;
530 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
533 FREE(ktriov, M_TEMP);
537 *res = len - auio->uio_resid;
548 sys_ioctl(struct ioctl_args *uap)
553 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
558 struct ioctl_map_entry {
560 struct ioctl_map_range *cmd_ranges;
561 LIST_ENTRY(ioctl_map_entry) entries;
565 * The true heart of all ioctl syscall handlers (native, emulation).
566 * If map != NULL, it will be searched for a matching entry for com,
567 * and appropriate conversions/conversion functions will be utilized.
570 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
573 struct thread *td = curthread;
574 struct proc *p = td->td_proc;
577 struct ioctl_map_range *iomc = NULL;
583 #define STK_PARAMS 128
585 char stkbuf[STK_PARAMS];
592 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
596 if (map != NULL) { /* obey translation map */
598 struct ioctl_map_entry *e;
600 maskcmd = com & map->mask;
602 LIST_FOREACH(e, &map->mapping, entries) {
603 for (iomc = e->cmd_ranges; iomc->start != 0 ||
604 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
605 iomc->mapfunc != NULL;
607 if (maskcmd >= iomc->start &&
608 maskcmd <= iomc->end)
612 /* Did we find a match? */
613 if (iomc->start != 0 || iomc->maptocmd != 0 ||
614 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
619 (iomc->start == 0 && iomc->maptocmd == 0
620 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
621 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
622 map->sys, fd, maskcmd,
623 (int)((maskcmd >> 8) & 0xff),
624 (int)(maskcmd & 0xff));
630 * If it's a non-range one to one mapping, maptocmd should be
631 * correct. If it's a ranged one to one mapping, we pass the
632 * original value of com, and for a range mapped to a different
633 * range, we always need a mapping function to translate the
634 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
636 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
637 com = iomc->maptocmd;
638 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
639 if (iomc->mapfunc != NULL)
640 com = iomc->mapfunc(iomc->start, iomc->end,
641 iomc->start, iomc->end,
644 if (iomc->mapfunc != NULL) {
645 com = iomc->mapfunc(iomc->start, iomc->end,
646 iomc->maptocmd, iomc->maptoend,
649 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
650 map->sys, fd, maskcmd,
651 (int)((maskcmd >> 8) & 0xff),
652 (int)(maskcmd & 0xff));
661 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
664 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
669 * Interpret high order word to find amount of data to be
670 * copied to/from the user's address space.
672 size = IOCPARM_LEN(com);
673 if (size > IOCPARM_MAX) {
679 if (size > sizeof (ubuf.stkbuf)) {
680 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
685 if ((com & IOC_IN) != 0) {
687 error = copyin(uspc_data, data, (size_t)size);
690 kfree(memp, M_IOCTLOPS);
694 *(caddr_t *)data = uspc_data;
696 } else if ((com & IOC_OUT) != 0 && size) {
698 * Zero the buffer so the user always
699 * gets back something deterministic.
701 bzero(data, (size_t)size);
702 } else if ((com & IOC_VOID) != 0) {
703 *(caddr_t *)data = uspc_data;
708 if ((tmp = *(int *)data))
709 fp->f_flag |= FNONBLOCK;
711 fp->f_flag &= ~FNONBLOCK;
716 if ((tmp = *(int *)data))
717 fp->f_flag |= FASYNC;
719 fp->f_flag &= ~FASYNC;
720 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
725 * If there is a override function,
726 * call it instead of directly routing the call
728 if (map != NULL && iomc->wrapfunc != NULL)
729 error = iomc->wrapfunc(fp, com, ocom, data, cred);
731 error = fo_ioctl(fp, com, data, cred, msg);
733 * Copy any data to user, size was
734 * already set and checked above.
736 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
737 error = copyout(data, uspc_data, (size_t)size);
741 kfree(memp, M_IOCTLOPS);
748 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
750 struct ioctl_map_entry *ne;
752 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
753 he->subsys != NULL && *he->subsys != '\0');
755 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
757 ne->subsys = he->subsys;
758 ne->cmd_ranges = he->cmd_ranges;
760 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
766 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
768 struct ioctl_map_entry *ne;
770 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
772 LIST_FOREACH(ne, &he->map->mapping, entries) {
773 if (ne->cmd_ranges != he->cmd_ranges)
775 LIST_REMOVE(ne, entries);
776 kfree(ne, M_IOCTLMAP);
782 static int nselcoll; /* Select collisions since boot */
784 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
785 static int nseldebug;
786 SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
789 * Select system call.
794 sys_select(struct select_args *uap)
797 struct timespec *ktsp, kts;
801 * Get timeout if any.
803 if (uap->tv != NULL) {
804 error = copyin(uap->tv, &ktv, sizeof (ktv));
807 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
816 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
817 &uap->sysmsg_result);
824 * Pselect system call.
829 sys_pselect(struct pselect_args *uap)
831 struct thread *td = curthread;
832 struct lwp *lp = td->td_lwp;
833 struct timespec *ktsp, kts;
838 * Get timeout if any.
840 if (uap->ts != NULL) {
841 error = copyin(uap->ts, &kts, sizeof (kts));
850 * Install temporary signal mask if any provided.
852 if (uap->sigmask != NULL) {
853 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
857 lp->lwp_oldsigmask = lp->lwp_sigmask;
858 SIG_CANTMASK(sigmask);
859 lp->lwp_sigmask = sigmask;
867 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
868 &uap->sysmsg_result);
870 if (uap->sigmask != NULL) {
871 /* doselect() responsible for turning ERESTART into EINTR */
872 KKASSERT(error != ERESTART);
873 if (error == EINTR) {
875 * We can't restore the previous signal mask now
876 * because it could block the signal that interrupted
877 * us. So make a note to restore it after executing
880 lp->lwp_flag |= LWP_OLDMASK;
883 * No handler to run. Restore previous mask immediately.
885 lp->lwp_sigmask = lp->lwp_oldsigmask;
894 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
896 struct select_kevent_copyin_args *skap = NULL;
903 skap = (struct select_kevent_copyin_args *)arg;
905 if (*events == maxevents)
908 while (skap->active_set < COPYIN_DONE) {
909 switch (skap->active_set) {
912 * Register descriptors for the read filter
914 fdp = skap->read_set;
915 filter = EVFILT_READ;
924 * Register descriptors for the write filter
926 fdp = skap->write_set;
927 filter = EVFILT_WRITE;
936 * Register descriptors for the exception filter
938 fdp = skap->except_set;
939 filter = EVFILT_EXCEPT;
948 * Nothing left to register
954 while (skap->proc_fds < skap->num_fds) {
956 if (FD_ISSET(fd, fdp)) {
957 kev = &kevp[*events];
958 EV_SET(kev, fd, filter,
961 (void *)skap->lwp->lwp_kqueue_serial);
966 if (*events == maxevents)
977 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
979 struct select_kevent_copyin_args *skap;
983 skap = (struct select_kevent_copyin_args *)arg;
985 if (kevp[0].flags & EV_ERROR) {
986 skap->error = kevp[0].data;
990 for (i = 0; i < count; ++i) {
991 if ((u_int)kevp[i].udata != skap->lwp->lwp_kqueue_serial) {
993 kev.flags = EV_DISABLE|EV_DELETE;
994 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
998 switch (kevp[i].filter) {
1000 FD_SET(kevp[i].ident, skap->read_set);
1003 FD_SET(kevp[i].ident, skap->write_set);
1006 FD_SET(kevp[i].ident, skap->except_set);
1017 * Copy select bits in from userland. Allocate kernel memory if the
1021 getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1026 if (bytes < sizeof(*tmp_set))
1029 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1030 error = copyin(in_set, *out_set, bytes);
1039 * Copy returned select bits back out to userland.
1042 putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1047 error = copyout(in_set, out_set, bytes);
1055 * Common code for sys_select() and sys_pselect().
1057 * in, out and ex are userland pointers. ts must point to validated
1058 * kernel-side timeout value or NULL for infinite timeout. res must
1059 * point to syscall return value.
1062 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1063 struct timespec *ts, int *res)
1065 struct proc *p = curproc;
1066 struct select_kevent_copyin_args *kap, ka;
1075 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1076 nd = p->p_fd->fd_nfiles;
1079 kap->lwp = curthread->td_lwp;
1083 kap->active_set = COPYIN_READ;
1086 * Calculate bytes based on the number of __fd_mask[] array entries
1087 * multiplied by the size of __fd_mask.
1089 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1091 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1093 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1095 error = getbits(bytes, except, &kap->except_set, &except_tmp);
1100 * NOTE: Make sure the max events passed to kern_kevent() is
1101 * effectively unlimited. (nd * 3) accomplishes this.
1103 * (*res) continues to increment as returned events are
1106 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1107 select_copyin, select_copyout, ts);
1109 error = putbits(bytes, kap->read_set, read);
1111 error = putbits(bytes, kap->write_set, write);
1113 error = putbits(bytes, kap->except_set, except);
1116 * Cumulative error from individual events (EBADFD?)
1125 if (kap->read_set && kap->read_set != &read_tmp)
1126 kfree(kap->read_set, M_SELECT);
1127 if (kap->write_set && kap->write_set != &write_tmp)
1128 kfree(kap->write_set, M_SELECT);
1129 if (kap->except_set && kap->except_set != &except_tmp)
1130 kfree(kap->except_set, M_SELECT);
1132 kap->lwp->lwp_kqueue_serial++;
1143 sys_poll(struct poll_args *uap)
1145 struct timespec ts, *tsp;
1148 if (uap->timeout != INFTIM) {
1149 ts.tv_sec = uap->timeout / 1000;
1150 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1156 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1162 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1164 struct poll_kevent_copyin_args *pkap;
1169 pkap = (struct poll_kevent_copyin_args *)arg;
1171 while (pkap->pfds < pkap->nfds) {
1172 pfd = &pkap->fds[pkap->pfds];
1174 /* Clear return events */
1177 /* Do not check if fd is equal to -1 */
1178 if (pfd->fd == -1) {
1184 if (pfd->events & (POLLIN | POLLRDNORM))
1186 if (pfd->events & (POLLOUT | POLLWRNORM))
1188 if (pfd->events & (POLLPRI | POLLRDBAND))
1191 if (*events + kev_count > maxevents)
1195 * NOTE: A combined serial number and poll array index is
1196 * stored in kev->udata.
1198 kev = &kevp[*events];
1199 if (pfd->events & (POLLIN | POLLRDNORM)) {
1200 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1201 0, 0, (void *)(pkap->lwp->lwp_kqueue_serial +
1204 if (pfd->events & (POLLOUT | POLLWRNORM)) {
1205 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1206 0, 0, (void *)(pkap->lwp->lwp_kqueue_serial +
1209 if (pfd->events & (POLLPRI | POLLRDBAND)) {
1210 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1212 (void *)(pkap->lwp->lwp_kqueue_serial +
1217 kprintf("poll index %d fd %d events %08x\n",
1218 pkap->pfds, pfd->fd, pfd->events);
1222 (*events) += kev_count;
1229 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1231 struct poll_kevent_copyin_args *pkap;
1237 pkap = (struct poll_kevent_copyin_args *)arg;
1239 for (i = 0; i < count; ++i) {
1241 * Extract the poll array index and delete spurious events.
1242 * We can easily tell if the serial number is incorrect
1243 * by checking whether the extracted index is out of range.
1245 pi = (u_int)kevp[i].udata - (u_int)pkap->lwp->lwp_kqueue_serial;
1247 if (pi >= pkap->nfds) {
1249 kev.flags = EV_DISABLE|EV_DELETE;
1250 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1252 kprintf("poll index %d out of range\n", pi);
1255 pfd = &pkap->fds[pi];
1256 if (kevp[i].ident == pfd->fd) {
1257 if (kevp[i].flags & EV_ERROR) {
1258 switch(kevp[i].data) {
1261 * Operation not supported. Poll
1262 * does not return an error for
1263 * POLLPRI (OOB/urgent data) when
1264 * it is not supported by the device.
1266 if (kevp[i].filter != EVFILT_EXCEPT) {
1267 pfd->revents |= POLLERR;
1272 /* Bad file descriptor */
1273 pfd->revents |= POLLNVAL;
1277 pfd->revents |= POLLERR;
1282 kprintf("poll index %d fd %d filter %d error %d\n",
1284 kevp[i].filter, kevp[i].data);
1288 if (kevp[i].flags & EV_EOF) {
1289 pfd->revents |= POLLHUP;
1294 switch (kevp[i].filter) {
1296 pfd->revents |= (POLLIN | POLLRDNORM);
1299 pfd->revents |= (POLLOUT | POLLWRNORM);
1302 pfd->revents |= (POLLPRI | POLLRDBAND);
1307 kprintf("poll index %d fd %d revents %08x\n",
1308 pi, pfd->fd, pfd->revents);
1315 kprintf("poll index %d mismatch %d/%d\n",
1316 pi, kevp[i].ident, pfd->fd);
1324 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1326 struct poll_kevent_copyin_args ka;
1327 struct pollfd sfds[64];
1336 * This is a bit arbitrary but we need to limit internal kmallocs.
1338 if (nfds > maxfilesperproc * 2)
1339 nfds = maxfilesperproc * 2;
1340 bytes = sizeof(struct pollfd) * nfds;
1342 ka.lwp = curthread->td_lwp;
1350 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1352 error = copyin(fds, ka.fds, bytes);
1354 error = kern_kevent(&ka.lwp->lwp_kqueue, ka.nfds, res, &ka,
1355 poll_copyin, poll_copyout, ts);
1358 error = copyout(ka.fds, fds, bytes);
1361 kfree(ka.fds, M_SELECT);
1363 ka.lwp->lwp_kqueue_serial += nfds;
1369 * OpenBSD poll system call.
1370 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1375 sys_openbsd_poll(struct openbsd_poll_args *uap)
1377 return (sys_poll((struct poll_args *)uap));
1382 seltrue(cdev_t dev, int events)
1384 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1388 * Record a select request. A global wait must be used since a process/thread
1389 * might go away after recording its request.
1392 selrecord(struct thread *selector, struct selinfo *sip)
1395 struct lwp *lp = NULL;
1397 if (selector->td_lwp == NULL)
1398 panic("selrecord: thread needs a process");
1400 if (sip->si_pid == selector->td_proc->p_pid &&
1401 sip->si_tid == selector->td_lwp->lwp_tid)
1403 if (sip->si_pid && (p = pfind(sip->si_pid)))
1404 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1405 if (lp != NULL && lp->lwp_wchan == (caddr_t)&selwait) {
1406 sip->si_flags |= SI_COLL;
1408 sip->si_pid = selector->td_proc->p_pid;
1409 sip->si_tid = selector->td_lwp->lwp_tid;
1414 * Do a wakeup when a selectable event occurs.
1417 selwakeup(struct selinfo *sip)
1420 struct lwp *lp = NULL;
1422 if (sip->si_pid == 0)
1424 if (sip->si_flags & SI_COLL) {
1426 sip->si_flags &= ~SI_COLL;
1427 wakeup((caddr_t)&selwait); /* YYY fixable */
1429 p = pfind(sip->si_pid);
1433 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1438 * This is a temporary hack until the code can be rewritten.
1439 * Check LWP_SELECT before assuming we can setrunnable().
1440 * Otherwise we might catch the lwp before it actually goes to
1444 if (lp->lwp_flag & LWP_SELECT) {
1445 lp->lwp_flag &= ~LWP_SELECT;
1446 } else if (lp->lwp_wchan == (caddr_t)&selwait) {
1448 * Flag the process to break the tsleep when
1449 * setrunnable is called, but only call setrunnable
1450 * here if the process is not in a stopped state.
1452 lp->lwp_flag |= LWP_BREAKTSLEEP;
1453 if (p->p_stat != SSTOP)
1458 kqueue_wakeup(&lp->lwp_kqueue);