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.47 2008/01/10 22:30:27 nth Exp $
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
50 #include <sys/fcntl.h>
53 #include <sys/signalvar.h>
54 #include <sys/socketvar.h>
56 #include <sys/kernel.h>
57 #include <sys/kern_syscall.h>
58 #include <sys/malloc.h>
59 #include <sys/mapped_ioctl.h>
61 #include <sys/queue.h>
62 #include <sys/resourcevar.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
67 #include <sys/ktrace.h>
70 #include <vm/vm_page.h>
71 #include <sys/file2.h>
73 #include <machine/limits.h>
75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
77 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
78 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
80 static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
81 struct timeval *tv, int *res);
82 static int pollscan (struct proc *, struct pollfd *, u_int, int *);
83 static int selscan (struct proc *, fd_mask **, fd_mask **,
85 static int dofileread(int, struct file *, struct uio *, int, int *);
86 static int dofilewrite(int, struct file *, struct uio *, int, int *);
94 sys_read(struct read_args *uap)
96 struct thread *td = curthread;
101 aiov.iov_base = uap->buf;
102 aiov.iov_len = uap->nbyte;
103 auio.uio_iov = &aiov;
105 auio.uio_offset = -1;
106 auio.uio_resid = uap->nbyte;
107 auio.uio_rw = UIO_READ;
108 auio.uio_segflg = UIO_USERSPACE;
111 if (auio.uio_resid < 0)
114 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
119 * Positioned (Pread) read system call
124 sys_extpread(struct extpread_args *uap)
126 struct thread *td = curthread;
132 aiov.iov_base = uap->buf;
133 aiov.iov_len = uap->nbyte;
134 auio.uio_iov = &aiov;
136 auio.uio_offset = uap->offset;
137 auio.uio_resid = uap->nbyte;
138 auio.uio_rw = UIO_READ;
139 auio.uio_segflg = UIO_USERSPACE;
142 flags = uap->flags & O_FMASK;
143 if (uap->offset != (off_t)-1)
146 if (auio.uio_resid < 0)
149 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_result);
154 * Scatter read system call.
159 sys_readv(struct readv_args *uap)
161 struct thread *td = curthread;
163 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
166 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
171 auio.uio_iovcnt = uap->iovcnt;
172 auio.uio_offset = -1;
173 auio.uio_rw = UIO_READ;
174 auio.uio_segflg = UIO_USERSPACE;
177 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
179 iovec_free(&iov, aiov);
185 * Scatter positioned read system call.
190 sys_extpreadv(struct extpreadv_args *uap)
192 struct thread *td = curthread;
194 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 = uap->offset;
205 auio.uio_rw = UIO_READ;
206 auio.uio_segflg = UIO_USERSPACE;
209 flags = uap->flags & O_FMASK;
210 if (uap->offset != (off_t)-1)
213 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_result);
215 iovec_free(&iov, aiov);
223 kern_preadv(int fd, struct uio *auio, int flags, int *res)
225 struct thread *td = curthread;
226 struct proc *p = td->td_proc;
232 fp = holdfp(p->p_fd, fd, FREAD);
235 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
237 } else if (auio->uio_resid < 0) {
240 error = dofileread(fd, fp, auio, flags, res);
247 * Common code for readv and preadv that reads data in
248 * from a file using the passed in uio, offset, and flags.
250 * MPALMOSTSAFE - ktrace needs help
253 dofileread(int fd, struct file *fp, struct uio *auio, int flags, int *res)
255 struct thread *td = curthread;
256 struct proc *p = td->td_proc;
260 struct iovec *ktriov = NULL;
266 * if tracing, save a copy of iovec
268 if (KTRPOINT(td, KTR_GENIO)) {
269 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
271 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
272 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
276 len = auio->uio_resid;
277 error = fo_read(fp, auio, fp->f_cred, flags);
279 if (auio->uio_resid != len && (error == ERESTART ||
280 error == EINTR || error == EWOULDBLOCK))
284 if (ktriov != NULL) {
286 ktruio.uio_iov = ktriov;
287 ktruio.uio_resid = len - auio->uio_resid;
289 ktrgenio(p, fd, UIO_READ, &ktruio, error);
292 FREE(ktriov, M_TEMP);
296 *res = len - auio->uio_resid;
307 sys_write(struct write_args *uap)
309 struct thread *td = curthread;
314 aiov.iov_base = (void *)(uintptr_t)uap->buf;
315 aiov.iov_len = uap->nbyte;
316 auio.uio_iov = &aiov;
318 auio.uio_offset = -1;
319 auio.uio_resid = uap->nbyte;
320 auio.uio_rw = UIO_WRITE;
321 auio.uio_segflg = UIO_USERSPACE;
324 if (auio.uio_resid < 0)
327 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
338 sys_extpwrite(struct extpwrite_args *uap)
340 struct thread *td = curthread;
346 aiov.iov_base = (void *)(uintptr_t)uap->buf;
347 aiov.iov_len = uap->nbyte;
348 auio.uio_iov = &aiov;
350 auio.uio_offset = uap->offset;
351 auio.uio_resid = uap->nbyte;
352 auio.uio_rw = UIO_WRITE;
353 auio.uio_segflg = UIO_USERSPACE;
356 flags = uap->flags & O_FMASK;
357 if (uap->offset != (off_t)-1)
360 if (auio.uio_resid < 0)
363 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_result);
372 sys_writev(struct writev_args *uap)
374 struct thread *td = curthread;
376 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
379 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
384 auio.uio_iovcnt = uap->iovcnt;
385 auio.uio_offset = -1;
386 auio.uio_rw = UIO_WRITE;
387 auio.uio_segflg = UIO_USERSPACE;
390 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
392 iovec_free(&iov, aiov);
398 * Gather positioned write system call
403 sys_extpwritev(struct extpwritev_args *uap)
405 struct thread *td = curthread;
407 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
411 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
416 auio.uio_iovcnt = uap->iovcnt;
417 auio.uio_offset = uap->offset;
418 auio.uio_rw = UIO_WRITE;
419 auio.uio_segflg = UIO_USERSPACE;
422 flags = uap->flags & O_FMASK;
423 if (uap->offset != (off_t)-1)
426 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_result);
428 iovec_free(&iov, aiov);
436 kern_pwritev(int fd, struct uio *auio, int flags, int *res)
438 struct thread *td = curthread;
439 struct proc *p = td->td_proc;
445 fp = holdfp(p->p_fd, fd, FWRITE);
448 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
451 error = dofilewrite(fd, fp, auio, flags, res);
459 * Common code for writev and pwritev that writes data to
460 * a file using the passed in uio, offset, and flags.
462 * MPALMOSTSAFE - ktrace needs help
465 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, int *res)
467 struct thread *td = curthread;
468 struct lwp *lp = td->td_lwp;
469 struct proc *p = td->td_proc;
473 struct iovec *ktriov = NULL;
479 * if tracing, save a copy of iovec and uio
481 if (KTRPOINT(td, KTR_GENIO)) {
482 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
484 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
485 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
489 len = auio->uio_resid;
490 if (fp->f_type == DTYPE_VNODE)
492 error = fo_write(fp, auio, fp->f_cred, flags);
494 if (auio->uio_resid != len && (error == ERESTART ||
495 error == EINTR || error == EWOULDBLOCK))
497 /* Socket layer is responsible for issuing SIGPIPE. */
498 if (error == EPIPE) {
500 lwpsignal(p, lp, SIGPIPE);
505 if (ktriov != NULL) {
507 ktruio.uio_iov = ktriov;
508 ktruio.uio_resid = len - auio->uio_resid;
510 ktrgenio(p, fd, UIO_WRITE, &ktruio, error);
513 FREE(ktriov, M_TEMP);
517 *res = len - auio->uio_resid;
527 sys_ioctl(struct ioctl_args *uap)
529 return(mapped_ioctl(uap->fd, uap->com, uap->data, NULL));
532 struct ioctl_map_entry {
534 struct ioctl_map_range *cmd_ranges;
535 LIST_ENTRY(ioctl_map_entry) entries;
539 * The true heart of all ioctl syscall handlers (native, emulation).
540 * If map != NULL, it will be searched for a matching entry for com,
541 * and appropriate conversions/conversion functions will be utilized.
544 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map)
546 struct thread *td = curthread;
547 struct proc *p = td->td_proc;
550 struct ioctl_map_range *iomc = NULL;
556 #define STK_PARAMS 128
558 char stkbuf[STK_PARAMS];
565 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
569 if (map != NULL) { /* obey translation map */
571 struct ioctl_map_entry *e;
573 maskcmd = com & map->mask;
575 LIST_FOREACH(e, &map->mapping, entries) {
576 for (iomc = e->cmd_ranges; iomc->start != 0 ||
577 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
578 iomc->mapfunc != NULL;
580 if (maskcmd >= iomc->start &&
581 maskcmd <= iomc->end)
585 /* Did we find a match? */
586 if (iomc->start != 0 || iomc->maptocmd != 0 ||
587 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
592 (iomc->start == 0 && iomc->maptocmd == 0
593 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
594 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
595 map->sys, fd, maskcmd,
596 (int)((maskcmd >> 8) & 0xff),
597 (int)(maskcmd & 0xff));
603 * If it's a non-range one to one mapping, maptocmd should be
604 * correct. If it's a ranged one to one mapping, we pass the
605 * original value of com, and for a range mapped to a different
606 * range, we always need a mapping function to translate the
607 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
609 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
610 com = iomc->maptocmd;
611 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
612 if (iomc->mapfunc != NULL)
613 com = iomc->mapfunc(iomc->start, iomc->end,
614 iomc->start, iomc->end,
617 if (iomc->mapfunc != NULL) {
618 com = iomc->mapfunc(iomc->start, iomc->end,
619 iomc->maptocmd, iomc->maptoend,
622 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
623 map->sys, fd, maskcmd,
624 (int)((maskcmd >> 8) & 0xff),
625 (int)(maskcmd & 0xff));
634 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
637 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
642 * Interpret high order word to find amount of data to be
643 * copied to/from the user's address space.
645 size = IOCPARM_LEN(com);
646 if (size > IOCPARM_MAX) {
652 if (size > sizeof (ubuf.stkbuf)) {
653 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
658 if ((com & IOC_IN) != 0) {
660 error = copyin(uspc_data, data, (u_int)size);
663 kfree(memp, M_IOCTLOPS);
667 *(caddr_t *)data = uspc_data;
669 } else if ((com & IOC_OUT) != 0 && size) {
671 * Zero the buffer so the user always
672 * gets back something deterministic.
675 } else if ((com & IOC_VOID) != 0) {
676 *(caddr_t *)data = uspc_data;
681 if ((tmp = *(int *)data))
682 fp->f_flag |= FNONBLOCK;
684 fp->f_flag &= ~FNONBLOCK;
689 if ((tmp = *(int *)data))
690 fp->f_flag |= FASYNC;
692 fp->f_flag &= ~FASYNC;
693 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred);
698 * If there is a override function,
699 * call it instead of directly routing the call
701 if (map != NULL && iomc->wrapfunc != NULL)
702 error = iomc->wrapfunc(fp, com, ocom, data, cred);
704 error = fo_ioctl(fp, com, data, cred);
706 * Copy any data to user, size was
707 * already set and checked above.
709 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
710 error = copyout(data, uspc_data, (u_int)size);
714 kfree(memp, M_IOCTLOPS);
721 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
723 struct ioctl_map_entry *ne;
725 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
726 he->subsys != NULL && *he->subsys != '\0');
728 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
730 ne->subsys = he->subsys;
731 ne->cmd_ranges = he->cmd_ranges;
733 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
739 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
741 struct ioctl_map_entry *ne;
743 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
745 LIST_FOREACH(ne, &he->map->mapping, entries) {
746 if (ne->cmd_ranges != he->cmd_ranges)
748 LIST_REMOVE(ne, entries);
749 kfree(ne, M_IOCTLMAP);
755 static int nselcoll; /* Select collisions since boot */
757 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
760 * Select system call.
763 sys_select(struct select_args *uap)
766 struct timeval *ktvp;
770 * Get timeout if any.
772 if (uap->tv != NULL) {
773 error = copyin(uap->tv, &ktv, sizeof (ktv));
776 error = itimerfix(&ktv);
787 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktvp,
788 &uap->sysmsg_result);
795 * Pselect system call.
798 sys_pselect(struct pselect_args *uap)
800 struct thread *td = curthread;
801 struct lwp *lp = td->td_lwp;
804 struct timeval *ktvp;
809 * Get timeout if any and convert it.
810 * Round up during conversion to avoid timeout going off early.
812 if (uap->ts != NULL) {
813 error = copyin(uap->ts, &kts, sizeof (kts));
816 ktv.tv_sec = kts.tv_sec;
817 ktv.tv_usec = (kts.tv_nsec + 999) / 1000;
818 error = itimerfix(&ktv);
827 * Install temporary signal mask if any provided.
829 if (uap->sigmask != NULL) {
830 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
833 lp->lwp_oldsigmask = lp->lwp_sigmask;
834 SIG_CANTMASK(sigmask);
835 lp->lwp_sigmask = sigmask;
841 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktvp,
842 &uap->sysmsg_result);
844 if (uap->sigmask != NULL) {
845 /* doselect() responsible for turning ERESTART into EINTR */
846 KKASSERT(error != ERESTART);
847 if (error == EINTR) {
849 * We can't restore the previous signal mask now
850 * because it could block the signal that interrupted
851 * us. So make a note to restore it after executing
854 lp->lwp_flag |= LWP_OLDMASK;
857 * No handler to run. Restore previous mask immediately.
859 lp->lwp_sigmask = lp->lwp_oldsigmask;
867 * Common code for sys_select() and sys_pselect().
869 * in, out and ex are userland pointers. tv must point to validated
870 * kernel-side timeout value or NULL for infinite timeout. res must
871 * point to syscall return value.
874 doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex, struct timeval *tv,
877 struct lwp *lp = curthread->td_lwp;
878 struct proc *p = curproc;
881 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
882 * infds with the new FD_SETSIZE of 1024, and more than enough for
883 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
886 fd_mask s_selbits[howmany(2048, NFDBITS)];
887 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
888 struct timeval atv, rtv, ttv;
889 int ncoll, error, timo;
890 u_int nbufbytes, ncpbytes, nfdbits;
894 if (nd > p->p_fd->fd_nfiles)
895 nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
898 * Allocate just enough bits for the non-null fd_sets. Use the
899 * preallocated auto buffer if possible.
901 nfdbits = roundup(nd, NFDBITS);
902 ncpbytes = nfdbits / NBBY;
905 nbufbytes += 2 * ncpbytes;
907 nbufbytes += 2 * ncpbytes;
909 nbufbytes += 2 * ncpbytes;
910 if (nbufbytes <= sizeof s_selbits)
911 selbits = &s_selbits[0];
913 selbits = kmalloc(nbufbytes, M_SELECT, M_WAITOK);
916 * Assign pointers into the bit buffers and fetch the input bits.
917 * Put the output buffers together so that they can be bzeroed
921 #define getbits(name, x) \
926 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
928 sbp += ncpbytes / sizeof *sbp; \
929 error = copyin(name, ibits[x], ncpbytes); \
939 bzero(selbits, nbufbytes / 2);
943 getmicrouptime(&rtv);
944 timevaladd(&atv, &rtv);
952 lp->lwp_flag |= LWP_SELECT;
953 error = selscan(p, ibits, obits, nd, res);
956 if (atv.tv_sec || atv.tv_usec) {
957 getmicrouptime(&rtv);
958 if (timevalcmp(&rtv, &atv, >=))
961 timevalsub(&ttv, &rtv);
962 timo = ttv.tv_sec > 24 * 60 * 60 ?
963 24 * 60 * 60 * hz : tvtohz_high(&ttv);
966 if ((lp->lwp_flag & LWP_SELECT) == 0 || nselcoll != ncoll) {
970 lp->lwp_flag &= ~LWP_SELECT;
972 error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
978 lp->lwp_flag &= ~LWP_SELECT;
979 /* select is not restarted after signals... */
980 if (error == ERESTART)
982 if (error == EWOULDBLOCK)
984 #define putbits(name, x) \
985 if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
995 if (selbits != &s_selbits[0])
996 kfree(selbits, M_SELECT);
1001 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd, int *res)
1007 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
1008 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
1010 for (msk = 0; msk < 3; msk++) {
1011 if (ibits[msk] == NULL)
1013 for (i = 0; i < nfd; i += NFDBITS) {
1014 bits = ibits[msk][i/NFDBITS];
1015 /* ffs(int mask) not portable, fd_mask is long */
1016 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
1019 fp = holdfp(p->p_fd, fd, -1);
1022 if (fo_poll(fp, flag[msk], fp->f_cred)) {
1023 obits[msk][(fd)/NFDBITS] |=
1024 ((fd_mask)1 << ((fd) % NFDBITS));
1039 sys_poll(struct poll_args *uap)
1041 struct pollfd *bits;
1042 struct pollfd smallbits[32];
1043 struct timeval atv, rtv, ttv;
1044 int ncoll, error = 0, timo;
1047 struct lwp *lp = curthread->td_lwp;
1048 struct proc *p = curproc;
1052 * This is kinda bogus. We have fd limits, but that is not
1053 * really related to the size of the pollfd array. Make sure
1054 * we let the process use at least FD_SETSIZE entries and at
1055 * least enough for the current limits. We want to be reasonably
1056 * safe, but not overly restrictive.
1058 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
1060 ni = nfds * sizeof(struct pollfd);
1061 if (ni > sizeof(smallbits))
1062 bits = kmalloc(ni, M_TEMP, M_WAITOK);
1065 error = copyin(uap->fds, bits, ni);
1068 if (uap->timeout != INFTIM) {
1069 atv.tv_sec = uap->timeout / 1000;
1070 atv.tv_usec = (uap->timeout % 1000) * 1000;
1071 if (itimerfix(&atv)) {
1075 getmicrouptime(&rtv);
1076 timevaladd(&atv, &rtv);
1084 lp->lwp_flag |= LWP_SELECT;
1085 error = pollscan(p, bits, nfds, &uap->sysmsg_result);
1086 if (error || uap->sysmsg_result)
1088 if (atv.tv_sec || atv.tv_usec) {
1089 getmicrouptime(&rtv);
1090 if (timevalcmp(&rtv, &atv, >=))
1093 timevalsub(&ttv, &rtv);
1094 timo = ttv.tv_sec > 24 * 60 * 60 ?
1095 24 * 60 * 60 * hz : tvtohz_high(&ttv);
1098 if ((lp->lwp_flag & LWP_SELECT) == 0 || nselcoll != ncoll) {
1102 lp->lwp_flag &= ~LWP_SELECT;
1103 error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
1108 lp->lwp_flag &= ~LWP_SELECT;
1109 /* poll is not restarted after signals... */
1110 if (error == ERESTART)
1112 if (error == EWOULDBLOCK)
1115 error = copyout(bits, uap->fds, ni);
1120 if (ni > sizeof(smallbits))
1121 kfree(bits, M_TEMP);
1126 pollscan(struct proc *p, struct pollfd *fds, u_int nfd, int *res)
1132 for (i = 0; i < nfd; i++, fds++) {
1133 if (fds->fd >= p->p_fd->fd_nfiles) {
1134 fds->revents = POLLNVAL;
1136 } else if (fds->fd < 0) {
1139 fp = holdfp(p->p_fd, fds->fd, -1);
1141 fds->revents = POLLNVAL;
1145 * Note: backend also returns POLLHUP and
1146 * POLLERR if appropriate.
1148 fds->revents = fo_poll(fp, fds->events,
1150 if (fds->revents != 0)
1161 * OpenBSD poll system call.
1162 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1165 sys_openbsd_poll(struct openbsd_poll_args *uap)
1167 return (sys_poll((struct poll_args *)uap));
1172 seltrue(cdev_t dev, int events)
1174 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1178 * Record a select request. A global wait must be used since a process/thread
1179 * might go away after recording its request.
1182 selrecord(struct thread *selector, struct selinfo *sip)
1185 struct lwp *lp = NULL;
1187 if (selector->td_lwp == NULL)
1188 panic("selrecord: thread needs a process");
1190 if (sip->si_pid == selector->td_proc->p_pid &&
1191 sip->si_tid == selector->td_lwp->lwp_tid)
1193 if (sip->si_pid && (p = pfind(sip->si_pid)))
1194 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1195 if (lp != NULL && lp->lwp_wchan == (caddr_t)&selwait) {
1196 sip->si_flags |= SI_COLL;
1198 sip->si_pid = selector->td_proc->p_pid;
1199 sip->si_tid = selector->td_lwp->lwp_tid;
1204 * Do a wakeup when a selectable event occurs.
1207 selwakeup(struct selinfo *sip)
1210 struct lwp *lp = NULL;
1212 if (sip->si_pid == 0)
1214 if (sip->si_flags & SI_COLL) {
1216 sip->si_flags &= ~SI_COLL;
1217 wakeup((caddr_t)&selwait); /* YYY fixable */
1219 p = pfind(sip->si_pid);
1223 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1228 if (lp->lwp_wchan == (caddr_t)&selwait) {
1230 * Flag the process to break the tsleep when
1231 * setrunnable is called, but only call setrunnable
1232 * here if the process is not in a stopped state.
1234 lp->lwp_flag |= LWP_BREAKTSLEEP;
1235 if (p->p_stat != SSTOP)
1237 } else if (lp->lwp_flag & LWP_SELECT) {
1238 lp->lwp_flag &= ~LWP_SELECT;