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.27 2006/05/06 06:38:38 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/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 pollscan (struct proc *, struct pollfd *, u_int, int *);
81 static int selscan (struct proc *, fd_mask **, fd_mask **,
83 static int dofileread(int, struct file *, struct uio *, int, int *);
84 static int dofilewrite(int, struct file *, struct uio *, int, int *);
94 if (((u_int)fd) >= fdp->fd_nfiles ||
95 (fp = fdp->fd_files[fd].fp) == NULL ||
96 (fp->f_flag & flag) == 0) {
107 read(struct read_args *uap)
109 struct thread *td = curthread;
114 aiov.iov_base = uap->buf;
115 aiov.iov_len = uap->nbyte;
116 auio.uio_iov = &aiov;
118 auio.uio_offset = -1;
119 auio.uio_resid = uap->nbyte;
120 auio.uio_rw = UIO_READ;
121 auio.uio_segflg = UIO_USERSPACE;
124 if (auio.uio_resid < 0)
127 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
132 * Positioned (Pread) read system call
135 pread(struct pread_args *uap)
137 struct thread *td = curthread;
142 aiov.iov_base = uap->buf;
143 aiov.iov_len = uap->nbyte;
144 auio.uio_iov = &aiov;
146 auio.uio_offset = uap->offset;
147 auio.uio_resid = uap->nbyte;
148 auio.uio_rw = UIO_READ;
149 auio.uio_segflg = UIO_USERSPACE;
152 if (auio.uio_resid < 0)
155 error = kern_preadv(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
160 * Scatter read system call.
163 readv(struct readv_args *uap)
165 struct thread *td = curthread;
167 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
170 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
175 auio.uio_iovcnt = uap->iovcnt;
176 auio.uio_offset = -1;
177 auio.uio_rw = UIO_READ;
178 auio.uio_segflg = UIO_USERSPACE;
181 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
183 iovec_free(&iov, aiov);
189 * Scatter positioned read system call.
192 preadv(struct preadv_args *uap)
194 struct thread *td = curthread;
196 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
199 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
204 auio.uio_iovcnt = uap->iovcnt;
205 auio.uio_offset = uap->offset;
206 auio.uio_rw = UIO_READ;
207 auio.uio_segflg = UIO_USERSPACE;
210 error = kern_preadv(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
212 iovec_free(&iov, aiov);
217 kern_preadv(int fd, struct uio *auio, int flags, int *res)
219 struct thread *td = curthread;
220 struct proc *p = td->td_proc;
222 struct filedesc *fdp = p->p_fd;
227 fp = holdfp(fdp, fd, FREAD);
230 if (flags & FOF_OFFSET && fp->f_type != DTYPE_VNODE) {
232 } else if (auio->uio_resid < 0) {
235 error = dofileread(fd, fp, auio, flags, res);
242 * Common code for readv and preadv that reads data in
243 * from a file using the passed in uio, offset, and flags.
246 dofileread(int fd, struct file *fp, struct uio *auio, int flags, int *res)
248 struct thread *td = curthread;
249 struct proc *p = td->td_proc;
253 struct iovec *ktriov = NULL;
259 * if tracing, save a copy of iovec
261 if (KTRPOINT(td, KTR_GENIO)) {
262 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
264 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
265 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
269 len = auio->uio_resid;
270 error = fo_read(fp, auio, fp->f_cred, flags);
272 if (auio->uio_resid != len && (error == ERESTART ||
273 error == EINTR || error == EWOULDBLOCK))
277 if (ktriov != NULL) {
279 ktruio.uio_iov = ktriov;
280 ktruio.uio_resid = len - auio->uio_resid;
281 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
283 FREE(ktriov, M_TEMP);
287 *res = len - auio->uio_resid;
296 write(struct write_args *uap)
298 struct thread *td = curthread;
303 aiov.iov_base = (void *)(uintptr_t)uap->buf;
304 aiov.iov_len = uap->nbyte;
305 auio.uio_iov = &aiov;
307 auio.uio_offset = -1;
308 auio.uio_resid = uap->nbyte;
309 auio.uio_rw = UIO_WRITE;
310 auio.uio_segflg = UIO_USERSPACE;
313 if (auio.uio_resid < 0)
316 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
325 pwrite(struct pwrite_args *uap)
327 struct thread *td = curthread;
332 aiov.iov_base = (void *)(uintptr_t)uap->buf;
333 aiov.iov_len = uap->nbyte;
334 auio.uio_iov = &aiov;
336 auio.uio_offset = uap->offset;
337 auio.uio_resid = uap->nbyte;
338 auio.uio_rw = UIO_WRITE;
339 auio.uio_segflg = UIO_USERSPACE;
342 if (auio.uio_resid < 0)
345 error = kern_pwritev(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
351 writev(struct writev_args *uap)
353 struct thread *td = curthread;
355 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
358 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
363 auio.uio_iovcnt = uap->iovcnt;
364 auio.uio_offset = -1;
365 auio.uio_rw = UIO_WRITE;
366 auio.uio_segflg = UIO_USERSPACE;
369 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
371 iovec_free(&iov, aiov);
377 * Gather positioned write system call
380 pwritev(struct pwritev_args *uap)
382 struct thread *td = curthread;
384 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
387 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
392 auio.uio_iovcnt = uap->iovcnt;
393 auio.uio_offset = uap->offset;
394 auio.uio_rw = UIO_WRITE;
395 auio.uio_segflg = UIO_USERSPACE;
398 error = kern_pwritev(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
400 iovec_free(&iov, aiov);
405 kern_pwritev(int fd, struct uio *auio, int flags, int *res)
407 struct thread *td = curthread;
408 struct proc *p = td->td_proc;
410 struct filedesc *fdp = p->p_fd;
415 fp = holdfp(fdp, fd, FWRITE);
418 else if ((flags & FOF_OFFSET) && fp->f_type != DTYPE_VNODE) {
421 error = dofilewrite(fd, fp, auio, flags, res);
429 * Common code for writev and pwritev that writes data to
430 * a file using the passed in uio, offset, and flags.
433 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, int *res)
435 struct thread *td = curthread;
436 struct proc *p = td->td_proc;
440 struct iovec *ktriov = NULL;
446 * if tracing, save a copy of iovec and uio
448 if (KTRPOINT(td, KTR_GENIO)) {
449 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
451 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
452 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
456 len = auio->uio_resid;
457 if (fp->f_type == DTYPE_VNODE)
459 error = fo_write(fp, auio, fp->f_cred, flags);
461 if (auio->uio_resid != len && (error == ERESTART ||
462 error == EINTR || error == EWOULDBLOCK))
464 /* Socket layer is responsible for issuing SIGPIPE. */
469 if (ktriov != NULL) {
471 ktruio.uio_iov = ktriov;
472 ktruio.uio_resid = len - auio->uio_resid;
473 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
475 FREE(ktriov, M_TEMP);
479 *res = len - auio->uio_resid;
489 ioctl(struct ioctl_args *uap)
491 return(mapped_ioctl(uap->fd, uap->com, uap->data, NULL));
494 struct ioctl_map_entry {
496 struct ioctl_map_range *cmd_ranges;
497 LIST_ENTRY(ioctl_map_entry) entries;
501 * The true heart of all ioctl syscall handlers (native, emulation).
502 * If map != NULL, it will be searched for a matching entry for com,
503 * and appropriate conversions/conversion functions will be utilized.
506 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map)
508 struct thread *td = curthread;
509 struct proc *p = td->td_proc;
512 struct filedesc *fdp;
513 struct ioctl_map_range *iomc = NULL;
519 #define STK_PARAMS 128
521 char stkbuf[STK_PARAMS];
528 if ((u_int)fd >= fdp->fd_nfiles ||
529 (fp = fdp->fd_files[fd].fp) == NULL)
532 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
535 if (map != NULL) { /* obey translation map */
537 struct ioctl_map_entry *e;
539 maskcmd = com & map->mask;
541 LIST_FOREACH(e, &map->mapping, entries) {
542 for (iomc = e->cmd_ranges; iomc->start != 0 ||
543 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
544 iomc->mapfunc != NULL;
546 if (maskcmd >= iomc->start &&
547 maskcmd <= iomc->end)
551 /* Did we find a match? */
552 if (iomc->start != 0 || iomc->maptocmd != 0 ||
553 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
558 (iomc->start == 0 && iomc->maptocmd == 0
559 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
560 printf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
561 map->sys, fd, maskcmd,
562 (int)((maskcmd >> 8) & 0xff),
563 (int)(maskcmd & 0xff));
568 * If it's a non-range one to one mapping, maptocmd should be
569 * correct. If it's a ranged one to one mapping, we pass the
570 * original value of com, and for a range mapped to a different
571 * range, we always need a mapping function to translate the
572 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
574 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
575 com = iomc->maptocmd;
576 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
577 if (iomc->mapfunc != NULL)
578 com = iomc->mapfunc(iomc->start, iomc->end,
579 iomc->start, iomc->end,
582 if (iomc->mapfunc != NULL) {
583 com = iomc->mapfunc(iomc->start, iomc->end,
584 iomc->maptocmd, iomc->maptoend,
587 printf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
588 map->sys, fd, maskcmd,
589 (int)((maskcmd >> 8) & 0xff),
590 (int)(maskcmd & 0xff));
598 fdp->fd_files[fd].fileflags &= ~UF_EXCLOSE;
601 fdp->fd_files[fd].fileflags |= UF_EXCLOSE;
606 * Interpret high order word to find amount of data to be
607 * copied to/from the user's address space.
609 size = IOCPARM_LEN(com);
610 if (size > IOCPARM_MAX)
616 if (size > sizeof (ubuf.stkbuf)) {
617 memp = malloc(size, M_IOCTLOPS, M_WAITOK);
622 if ((com & IOC_IN) != 0) {
624 error = copyin(uspc_data, data, (u_int)size);
627 free(memp, M_IOCTLOPS);
632 *(caddr_t *)data = uspc_data;
634 } else if ((com & IOC_OUT) != 0 && size) {
636 * Zero the buffer so the user always
637 * gets back something deterministic.
640 } else if ((com & IOC_VOID) != 0) {
641 *(caddr_t *)data = uspc_data;
647 if ((tmp = *(int *)data))
648 fp->f_flag |= FNONBLOCK;
650 fp->f_flag &= ~FNONBLOCK;
651 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, cred);
655 if ((tmp = *(int *)data))
656 fp->f_flag |= FASYNC;
658 fp->f_flag &= ~FASYNC;
659 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred);
664 * If there is a override function,
665 * call it instead of directly routing the call
667 if (map != NULL && iomc->wrapfunc != NULL)
668 error = iomc->wrapfunc(fp, com, ocom, data, cred);
670 error = fo_ioctl(fp, com, data, cred);
672 * Copy any data to user, size was
673 * already set and checked above.
675 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
676 error = copyout(data, uspc_data, (u_int)size);
680 free(memp, M_IOCTLOPS);
686 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
688 struct ioctl_map_entry *ne;
690 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
691 he->subsys != NULL && *he->subsys != '\0');
693 ne = malloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
695 ne->subsys = he->subsys;
696 ne->cmd_ranges = he->cmd_ranges;
698 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
704 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
706 struct ioctl_map_entry *ne;
708 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
710 LIST_FOREACH(ne, &he->map->mapping, entries) {
711 if (ne->cmd_ranges != he->cmd_ranges)
713 LIST_REMOVE(ne, entries);
714 free(ne, M_IOCTLMAP);
720 static int nselcoll; /* Select collisions since boot */
722 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
725 * Select system call.
728 select(struct select_args *uap)
730 struct proc *p = curproc;
733 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
734 * infds with the new FD_SETSIZE of 1024, and more than enough for
735 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
738 fd_mask s_selbits[howmany(2048, NFDBITS)];
739 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
740 struct timeval atv, rtv, ttv;
741 int ncoll, error, timo;
742 u_int nbufbytes, ncpbytes, nfdbits;
746 if (uap->nd > p->p_fd->fd_nfiles)
747 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
750 * Allocate just enough bits for the non-null fd_sets. Use the
751 * preallocated auto buffer if possible.
753 nfdbits = roundup(uap->nd, NFDBITS);
754 ncpbytes = nfdbits / NBBY;
757 nbufbytes += 2 * ncpbytes;
759 nbufbytes += 2 * ncpbytes;
761 nbufbytes += 2 * ncpbytes;
762 if (nbufbytes <= sizeof s_selbits)
763 selbits = &s_selbits[0];
765 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
768 * Assign pointers into the bit buffers and fetch the input bits.
769 * Put the output buffers together so that they can be bzeroed
773 #define getbits(name, x) \
775 if (uap->name == NULL) \
778 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
780 sbp += ncpbytes / sizeof *sbp; \
781 error = copyin(uap->name, ibits[x], ncpbytes); \
791 bzero(selbits, nbufbytes / 2);
794 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
798 if (itimerfix(&atv)) {
802 getmicrouptime(&rtv);
803 timevaladd(&atv, &rtv);
811 p->p_flag |= P_SELECT;
812 error = selscan(p, ibits, obits, uap->nd, &uap->sysmsg_result);
813 if (error || uap->sysmsg_result)
815 if (atv.tv_sec || atv.tv_usec) {
816 getmicrouptime(&rtv);
817 if (timevalcmp(&rtv, &atv, >=))
820 timevalsub(&ttv, &rtv);
821 timo = ttv.tv_sec > 24 * 60 * 60 ?
822 24 * 60 * 60 * hz : tvtohz_high(&ttv);
825 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
829 p->p_flag &= ~P_SELECT;
831 error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
837 p->p_flag &= ~P_SELECT;
838 /* select is not restarted after signals... */
839 if (error == ERESTART)
841 if (error == EWOULDBLOCK)
843 #define putbits(name, x) \
844 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
854 if (selbits != &s_selbits[0])
855 free(selbits, M_SELECT);
860 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd, int *res)
862 struct filedesc *fdp = p->p_fd;
867 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
868 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
870 for (msk = 0; msk < 3; msk++) {
871 if (ibits[msk] == NULL)
873 for (i = 0; i < nfd; i += NFDBITS) {
874 bits = ibits[msk][i/NFDBITS];
875 /* ffs(int mask) not portable, fd_mask is long */
876 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
879 fp = fdp->fd_files[fd].fp;
882 if (fo_poll(fp, flag[msk], fp->f_cred)) {
883 obits[msk][(fd)/NFDBITS] |=
884 ((fd_mask)1 << ((fd) % NFDBITS));
898 poll(struct poll_args *uap)
901 struct pollfd smallbits[32];
902 struct timeval atv, rtv, ttv;
903 int ncoll, error = 0, timo;
906 struct proc *p = curproc;
910 * This is kinda bogus. We have fd limits, but that is not
911 * really related to the size of the pollfd array. Make sure
912 * we let the process use at least FD_SETSIZE entries and at
913 * least enough for the current limits. We want to be reasonably
914 * safe, but not overly restrictive.
916 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
918 ni = nfds * sizeof(struct pollfd);
919 if (ni > sizeof(smallbits))
920 bits = malloc(ni, M_TEMP, M_WAITOK);
923 error = copyin(uap->fds, bits, ni);
926 if (uap->timeout != INFTIM) {
927 atv.tv_sec = uap->timeout / 1000;
928 atv.tv_usec = (uap->timeout % 1000) * 1000;
929 if (itimerfix(&atv)) {
933 getmicrouptime(&rtv);
934 timevaladd(&atv, &rtv);
942 p->p_flag |= P_SELECT;
943 error = pollscan(p, bits, nfds, &uap->sysmsg_result);
944 if (error || uap->sysmsg_result)
946 if (atv.tv_sec || atv.tv_usec) {
947 getmicrouptime(&rtv);
948 if (timevalcmp(&rtv, &atv, >=))
951 timevalsub(&ttv, &rtv);
952 timo = ttv.tv_sec > 24 * 60 * 60 ?
953 24 * 60 * 60 * hz : tvtohz_high(&ttv);
956 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
960 p->p_flag &= ~P_SELECT;
961 error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
966 p->p_flag &= ~P_SELECT;
967 /* poll is not restarted after signals... */
968 if (error == ERESTART)
970 if (error == EWOULDBLOCK)
973 error = copyout(bits, uap->fds, ni);
978 if (ni > sizeof(smallbits))
984 pollscan(struct proc *p, struct pollfd *fds, u_int nfd, int *res)
986 struct filedesc *fdp = p->p_fd;
991 for (i = 0; i < nfd; i++, fds++) {
992 if (fds->fd >= fdp->fd_nfiles) {
993 fds->revents = POLLNVAL;
995 } else if (fds->fd < 0) {
998 fp = fdp->fd_files[fds->fd].fp;
1000 fds->revents = POLLNVAL;
1004 * Note: backend also returns POLLHUP and
1005 * POLLERR if appropriate.
1007 fds->revents = fo_poll(fp, fds->events,
1009 if (fds->revents != 0)
1019 * OpenBSD poll system call.
1020 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1023 openbsd_poll(struct openbsd_poll_args *uap)
1025 return (poll((struct poll_args *)uap));
1030 seltrue(dev_t dev, int events, struct thread *td)
1032 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1036 * Record a select request. A global wait must be used since a process/thread
1037 * might go away after recording its request.
1040 selrecord(struct thread *selector, struct selinfo *sip)
1045 if ((p = selector->td_proc) == NULL)
1046 panic("selrecord: thread needs a process");
1049 if (sip->si_pid == mypid)
1051 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1052 p->p_wchan == (caddr_t)&selwait) {
1053 sip->si_flags |= SI_COLL;
1055 sip->si_pid = mypid;
1060 * Do a wakeup when a selectable event occurs.
1063 selwakeup(struct selinfo *sip)
1067 if (sip->si_pid == 0)
1069 if (sip->si_flags & SI_COLL) {
1071 sip->si_flags &= ~SI_COLL;
1072 wakeup((caddr_t)&selwait); /* YYY fixable */
1074 p = pfind(sip->si_pid);
1078 if (p->p_wchan == (caddr_t)&selwait) {
1080 * Flag the process to break the tsleep when
1081 * setrunnable is called, but only call setrunnable
1082 * here if the process is not in a stopped state.
1084 p->p_flag |= P_BREAKTSLEEP;
1085 if ((p->p_flag & P_STOPPED) == 0)
1087 } else if (p->p_flag & P_SELECT) {
1088 p->p_flag &= ~P_SELECT;