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, "");
787 * Select system call.
792 sys_select(struct select_args *uap)
795 struct timespec *ktsp, kts;
799 * Get timeout if any.
801 if (uap->tv != NULL) {
802 error = copyin(uap->tv, &ktv, sizeof (ktv));
805 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
814 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
815 &uap->sysmsg_result);
822 * Pselect system call.
827 sys_pselect(struct pselect_args *uap)
829 struct thread *td = curthread;
830 struct lwp *lp = td->td_lwp;
831 struct timespec *ktsp, kts;
836 * Get timeout if any.
838 if (uap->ts != NULL) {
839 error = copyin(uap->ts, &kts, sizeof (kts));
848 * Install temporary signal mask if any provided.
850 if (uap->sigmask != NULL) {
851 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
855 lp->lwp_oldsigmask = lp->lwp_sigmask;
856 SIG_CANTMASK(sigmask);
857 lp->lwp_sigmask = sigmask;
865 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
866 &uap->sysmsg_result);
868 if (uap->sigmask != NULL) {
869 /* doselect() responsible for turning ERESTART into EINTR */
870 KKASSERT(error != ERESTART);
871 if (error == EINTR) {
873 * We can't restore the previous signal mask now
874 * because it could block the signal that interrupted
875 * us. So make a note to restore it after executing
878 lp->lwp_flag |= LWP_OLDMASK;
881 * No handler to run. Restore previous mask immediately.
883 lp->lwp_sigmask = lp->lwp_oldsigmask;
892 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
894 struct select_kevent_copyin_args *skap = NULL;
901 skap = (struct select_kevent_copyin_args *)arg;
903 if (*events == maxevents)
906 while (skap->active_set < COPYIN_DONE) {
907 switch (skap->active_set) {
910 * Register descriptors for the read filter
912 fdp = skap->read_set;
913 filter = EVFILT_READ;
922 * Register descriptors for the write filter
924 fdp = skap->write_set;
925 filter = EVFILT_WRITE;
934 * Register descriptors for the exception filter
936 fdp = skap->except_set;
937 filter = EVFILT_EXCEPT;
946 * Nothing left to register
952 while (skap->proc_fds < skap->num_fds) {
954 if (FD_ISSET(fd, fdp)) {
955 kev = &kevp[*events];
956 EV_SET(kev, fd, filter,
959 (void *)skap->lwp->lwp_kqueue_serial);
964 if (*events == maxevents)
975 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
977 struct select_kevent_copyin_args *skap;
981 skap = (struct select_kevent_copyin_args *)arg;
983 if (kevp[0].flags & EV_ERROR) {
984 skap->error = kevp[0].data;
988 for (i = 0; i < count; ++i) {
989 if ((u_int)kevp[i].udata != skap->lwp->lwp_kqueue_serial) {
991 kev.flags = EV_DISABLE|EV_DELETE;
992 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
996 switch (kevp[i].filter) {
998 FD_SET(kevp[i].ident, skap->read_set);
1001 FD_SET(kevp[i].ident, skap->write_set);
1004 FD_SET(kevp[i].ident, skap->except_set);
1015 * Copy select bits in from userland. Allocate kernel memory if the
1019 getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1024 if (bytes < sizeof(*tmp_set))
1027 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1028 error = copyin(in_set, *out_set, bytes);
1037 * Copy returned select bits back out to userland.
1040 putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1045 error = copyout(in_set, out_set, bytes);
1053 * Common code for sys_select() and sys_pselect().
1055 * in, out and ex are userland pointers. ts must point to validated
1056 * kernel-side timeout value or NULL for infinite timeout. res must
1057 * point to syscall return value.
1060 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1061 struct timespec *ts, int *res)
1063 struct proc *p = curproc;
1064 struct select_kevent_copyin_args *kap, ka;
1073 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1074 nd = p->p_fd->fd_nfiles;
1077 kap->lwp = curthread->td_lwp;
1081 kap->active_set = COPYIN_READ;
1084 * Calculate bytes based on the number of __fd_mask[] array entries
1085 * multiplied by the size of __fd_mask.
1087 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1089 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1091 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1093 error = getbits(bytes, except, &kap->except_set, &except_tmp);
1098 * NOTE: Make sure the max events passed to kern_kevent() is
1099 * effectively unlimited. (nd * 3) accomplishes this.
1101 * (*res) continues to increment as returned events are
1104 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1105 select_copyin, select_copyout, ts);
1107 error = putbits(bytes, kap->read_set, read);
1109 error = putbits(bytes, kap->write_set, write);
1111 error = putbits(bytes, kap->except_set, except);
1114 * Cumulative error from individual events (EBADFD?)
1123 if (kap->read_set && kap->read_set != &read_tmp)
1124 kfree(kap->read_set, M_SELECT);
1125 if (kap->write_set && kap->write_set != &write_tmp)
1126 kfree(kap->write_set, M_SELECT);
1127 if (kap->except_set && kap->except_set != &except_tmp)
1128 kfree(kap->except_set, M_SELECT);
1130 kap->lwp->lwp_kqueue_serial++;
1141 sys_poll(struct poll_args *uap)
1143 struct timespec ts, *tsp;
1146 if (uap->timeout != INFTIM) {
1147 ts.tv_sec = uap->timeout / 1000;
1148 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1154 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1160 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1162 struct poll_kevent_copyin_args *pkap;
1167 pkap = (struct poll_kevent_copyin_args *)arg;
1169 while (pkap->pfds < pkap->nfds) {
1170 pfd = &pkap->fds[pkap->pfds];
1172 /* Clear return events */
1175 /* Do not check if fd is equal to -1 */
1176 if (pfd->fd == -1) {
1182 if (pfd->events & (POLLIN | POLLRDNORM))
1184 if (pfd->events & (POLLOUT | POLLWRNORM))
1186 if (pfd->events & (POLLPRI | POLLRDBAND))
1189 if (*events + kev_count > maxevents)
1192 kev = &kevp[*events];
1193 if (pfd->events & (POLLIN | POLLRDNORM))
1194 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1195 0, 0, (void *)pkap->pfds);
1196 if (pfd->events & (POLLOUT | POLLWRNORM))
1197 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1198 0, 0, (void *)pkap->pfds);
1199 if (pfd->events & (POLLPRI | POLLRDBAND))
1200 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1201 NOTE_OOB, 0, (void *)pkap->pfds);
1204 (*events) += kev_count;
1211 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1213 struct poll_kevent_copyin_args *pkap;
1218 pkap = (struct poll_kevent_copyin_args *)arg;
1220 for (i = 0; i < count; ++i) {
1221 if ((int)kevp[i].udata < pkap->nfds) {
1222 pfd = &pkap->fds[(int)kevp[i].udata];
1223 if (kevp[i].ident == pfd->fd) {
1224 if (kevp[i].flags & EV_ERROR) {
1225 /* Bad file descriptor */
1226 if (kevp[i].data == EBADF)
1227 pfd->revents |= POLLNVAL;
1229 pfd->revents |= POLLERR;
1235 if (kevp[i].flags & EV_EOF) {
1236 pfd->revents |= POLLHUP;
1241 switch (kevp[i].filter) {
1243 pfd->revents |= (POLLIN | POLLRDNORM);
1246 pfd->revents |= (POLLOUT | POLLWRNORM);
1249 pfd->revents |= (POLLPRI | POLLRDBAND);
1258 /* Remove descriptor not in pollfd set from kq */
1260 kev.flags = EV_DISABLE|EV_DELETE;
1261 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1268 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1270 struct poll_kevent_copyin_args ka;
1271 struct pollfd sfds[64];
1280 * This is a bit arbitrary but we need to limit internal kmallocs.
1282 if (nfds > maxfilesperproc * 2)
1283 nfds = maxfilesperproc * 2;
1284 bytes = sizeof(struct pollfd) * nfds;
1286 ka.lwp = curthread->td_lwp;
1294 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1296 error = copyin(fds, ka.fds, bytes);
1298 error = kern_kevent(&ka.lwp->lwp_kqueue, ka.nfds, res, &ka,
1299 poll_copyin, poll_copyout, ts);
1302 error = copyout(ka.fds, fds, bytes);
1305 kfree(ka.fds, M_SELECT);
1311 * OpenBSD poll system call.
1312 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1317 sys_openbsd_poll(struct openbsd_poll_args *uap)
1319 return (sys_poll((struct poll_args *)uap));
1324 seltrue(cdev_t dev, int events)
1326 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1330 * Record a select request. A global wait must be used since a process/thread
1331 * might go away after recording its request.
1334 selrecord(struct thread *selector, struct selinfo *sip)
1337 struct lwp *lp = NULL;
1339 if (selector->td_lwp == NULL)
1340 panic("selrecord: thread needs a process");
1342 if (sip->si_pid == selector->td_proc->p_pid &&
1343 sip->si_tid == selector->td_lwp->lwp_tid)
1345 if (sip->si_pid && (p = pfind(sip->si_pid)))
1346 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1347 if (lp != NULL && lp->lwp_wchan == (caddr_t)&selwait) {
1348 sip->si_flags |= SI_COLL;
1350 sip->si_pid = selector->td_proc->p_pid;
1351 sip->si_tid = selector->td_lwp->lwp_tid;
1356 * Do a wakeup when a selectable event occurs.
1359 selwakeup(struct selinfo *sip)
1362 struct lwp *lp = NULL;
1364 if (sip->si_pid == 0)
1366 if (sip->si_flags & SI_COLL) {
1368 sip->si_flags &= ~SI_COLL;
1369 wakeup((caddr_t)&selwait); /* YYY fixable */
1371 p = pfind(sip->si_pid);
1375 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1380 * This is a temporary hack until the code can be rewritten.
1381 * Check LWP_SELECT before assuming we can setrunnable().
1382 * Otherwise we might catch the lwp before it actually goes to
1386 if (lp->lwp_flag & LWP_SELECT) {
1387 lp->lwp_flag &= ~LWP_SELECT;
1388 } else if (lp->lwp_wchan == (caddr_t)&selwait) {
1390 * Flag the process to break the tsleep when
1391 * setrunnable is called, but only call setrunnable
1392 * here if the process is not in a stopped state.
1394 lp->lwp_flag |= LWP_BREAKTSLEEP;
1395 if (p->p_stat != SSTOP)
1400 kqueue_wakeup(&lp->lwp_kqueue);