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.33 2006/06/13 08:12:03 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 *);
92 sys_read(struct read_args *uap)
94 struct thread *td = curthread;
99 aiov.iov_base = uap->buf;
100 aiov.iov_len = uap->nbyte;
101 auio.uio_iov = &aiov;
103 auio.uio_offset = -1;
104 auio.uio_resid = uap->nbyte;
105 auio.uio_rw = UIO_READ;
106 auio.uio_segflg = UIO_USERSPACE;
109 if (auio.uio_resid < 0)
112 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
117 * Positioned (Pread) read system call
122 sys___pread(struct __pread_args *uap)
124 struct thread *td = curthread;
130 aiov.iov_base = uap->buf;
131 aiov.iov_len = uap->nbyte;
132 auio.uio_iov = &aiov;
134 auio.uio_offset = uap->offset;
135 auio.uio_resid = uap->nbyte;
136 auio.uio_rw = UIO_READ;
137 auio.uio_segflg = UIO_USERSPACE;
140 flags = uap->flags & O_FMASK;
141 if (uap->offset != (off_t)-1)
144 if (auio.uio_resid < 0)
147 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_result);
152 * Scatter read system call.
157 sys_readv(struct readv_args *uap)
159 struct thread *td = curthread;
161 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
164 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
169 auio.uio_iovcnt = uap->iovcnt;
170 auio.uio_offset = -1;
171 auio.uio_rw = UIO_READ;
172 auio.uio_segflg = UIO_USERSPACE;
175 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
177 iovec_free(&iov, aiov);
183 * Scatter positioned read system call.
188 sys___preadv(struct __preadv_args *uap)
190 struct thread *td = curthread;
192 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
196 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
201 auio.uio_iovcnt = uap->iovcnt;
202 auio.uio_offset = uap->offset;
203 auio.uio_rw = UIO_READ;
204 auio.uio_segflg = UIO_USERSPACE;
207 flags = uap->flags & O_FMASK;
208 if (uap->offset != (off_t)-1)
211 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_result);
213 iovec_free(&iov, aiov);
221 kern_preadv(int fd, struct uio *auio, int flags, int *res)
223 struct thread *td = curthread;
224 struct proc *p = td->td_proc;
230 fp = holdfp(p->p_fd, fd, FREAD);
233 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
235 } else if (auio->uio_resid < 0) {
238 error = dofileread(fd, fp, auio, flags, res);
245 * Common code for readv and preadv that reads data in
246 * from a file using the passed in uio, offset, and flags.
248 * MPALMOSTSAFE - ktrace needs help
251 dofileread(int fd, struct file *fp, struct uio *auio, int flags, int *res)
253 struct thread *td = curthread;
254 struct proc *p = td->td_proc;
258 struct iovec *ktriov = NULL;
264 * if tracing, save a copy of iovec
266 if (KTRPOINT(td, KTR_GENIO)) {
267 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
269 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
270 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
274 len = auio->uio_resid;
275 error = fo_read(fp, auio, fp->f_cred, flags);
277 if (auio->uio_resid != len && (error == ERESTART ||
278 error == EINTR || error == EWOULDBLOCK))
282 if (ktriov != NULL) {
284 ktruio.uio_iov = ktriov;
285 ktruio.uio_resid = len - auio->uio_resid;
287 ktrgenio(p, fd, UIO_READ, &ktruio, error);
290 FREE(ktriov, M_TEMP);
294 *res = len - auio->uio_resid;
305 sys_write(struct write_args *uap)
307 struct thread *td = curthread;
312 aiov.iov_base = (void *)(uintptr_t)uap->buf;
313 aiov.iov_len = uap->nbyte;
314 auio.uio_iov = &aiov;
316 auio.uio_offset = -1;
317 auio.uio_resid = uap->nbyte;
318 auio.uio_rw = UIO_WRITE;
319 auio.uio_segflg = UIO_USERSPACE;
322 if (auio.uio_resid < 0)
325 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
336 sys___pwrite(struct __pwrite_args *uap)
338 struct thread *td = curthread;
344 aiov.iov_base = (void *)(uintptr_t)uap->buf;
345 aiov.iov_len = uap->nbyte;
346 auio.uio_iov = &aiov;
348 auio.uio_offset = uap->offset;
349 auio.uio_resid = uap->nbyte;
350 auio.uio_rw = UIO_WRITE;
351 auio.uio_segflg = UIO_USERSPACE;
354 flags = uap->flags & O_FMASK;
355 if (uap->offset != (off_t)-1)
358 if (auio.uio_resid < 0)
361 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_result);
370 sys_writev(struct writev_args *uap)
372 struct thread *td = curthread;
374 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
377 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
382 auio.uio_iovcnt = uap->iovcnt;
383 auio.uio_offset = -1;
384 auio.uio_rw = UIO_WRITE;
385 auio.uio_segflg = UIO_USERSPACE;
388 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
390 iovec_free(&iov, aiov);
396 * Gather positioned write system call
401 sys___pwritev(struct __pwritev_args *uap)
403 struct thread *td = curthread;
405 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
409 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
414 auio.uio_iovcnt = uap->iovcnt;
415 auio.uio_offset = uap->offset;
416 auio.uio_rw = UIO_WRITE;
417 auio.uio_segflg = UIO_USERSPACE;
420 flags = uap->flags & O_FMASK;
421 if (uap->offset != (off_t)-1)
424 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_result);
426 iovec_free(&iov, aiov);
434 kern_pwritev(int fd, struct uio *auio, int flags, int *res)
436 struct thread *td = curthread;
437 struct proc *p = td->td_proc;
443 fp = holdfp(p->p_fd, fd, FWRITE);
446 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
449 error = dofilewrite(fd, fp, auio, flags, res);
457 * Common code for writev and pwritev that writes data to
458 * a file using the passed in uio, offset, and flags.
460 * MPALMOSTSAFE - ktrace needs help
463 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, int *res)
465 struct thread *td = curthread;
466 struct proc *p = td->td_proc;
470 struct iovec *ktriov = NULL;
476 * if tracing, save a copy of iovec and uio
478 if (KTRPOINT(td, KTR_GENIO)) {
479 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
481 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
482 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
486 len = auio->uio_resid;
487 if (fp->f_type == DTYPE_VNODE)
489 error = fo_write(fp, auio, fp->f_cred, flags);
491 if (auio->uio_resid != len && (error == ERESTART ||
492 error == EINTR || error == EWOULDBLOCK))
494 /* Socket layer is responsible for issuing SIGPIPE. */
495 if (error == EPIPE) {
502 if (ktriov != NULL) {
504 ktruio.uio_iov = ktriov;
505 ktruio.uio_resid = len - auio->uio_resid;
507 ktrgenio(p, fd, UIO_WRITE, &ktruio, error);
510 FREE(ktriov, M_TEMP);
514 *res = len - auio->uio_resid;
524 sys_ioctl(struct ioctl_args *uap)
526 return(mapped_ioctl(uap->fd, uap->com, uap->data, NULL));
529 struct ioctl_map_entry {
531 struct ioctl_map_range *cmd_ranges;
532 LIST_ENTRY(ioctl_map_entry) entries;
536 * The true heart of all ioctl syscall handlers (native, emulation).
537 * If map != NULL, it will be searched for a matching entry for com,
538 * and appropriate conversions/conversion functions will be utilized.
541 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map)
543 struct thread *td = curthread;
544 struct proc *p = td->td_proc;
547 struct ioctl_map_range *iomc = NULL;
553 #define STK_PARAMS 128
555 char stkbuf[STK_PARAMS];
562 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
566 if (map != NULL) { /* obey translation map */
568 struct ioctl_map_entry *e;
570 maskcmd = com & map->mask;
572 LIST_FOREACH(e, &map->mapping, entries) {
573 for (iomc = e->cmd_ranges; iomc->start != 0 ||
574 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
575 iomc->mapfunc != NULL;
577 if (maskcmd >= iomc->start &&
578 maskcmd <= iomc->end)
582 /* Did we find a match? */
583 if (iomc->start != 0 || iomc->maptocmd != 0 ||
584 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
589 (iomc->start == 0 && iomc->maptocmd == 0
590 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
591 printf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
592 map->sys, fd, maskcmd,
593 (int)((maskcmd >> 8) & 0xff),
594 (int)(maskcmd & 0xff));
600 * If it's a non-range one to one mapping, maptocmd should be
601 * correct. If it's a ranged one to one mapping, we pass the
602 * original value of com, and for a range mapped to a different
603 * range, we always need a mapping function to translate the
604 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
606 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
607 com = iomc->maptocmd;
608 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
609 if (iomc->mapfunc != NULL)
610 com = iomc->mapfunc(iomc->start, iomc->end,
611 iomc->start, iomc->end,
614 if (iomc->mapfunc != NULL) {
615 com = iomc->mapfunc(iomc->start, iomc->end,
616 iomc->maptocmd, iomc->maptoend,
619 printf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
620 map->sys, fd, maskcmd,
621 (int)((maskcmd >> 8) & 0xff),
622 (int)(maskcmd & 0xff));
631 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
634 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
639 * Interpret high order word to find amount of data to be
640 * copied to/from the user's address space.
642 size = IOCPARM_LEN(com);
643 if (size > IOCPARM_MAX) {
649 if (size > sizeof (ubuf.stkbuf)) {
650 memp = malloc(size, M_IOCTLOPS, M_WAITOK);
655 if ((com & IOC_IN) != 0) {
657 error = copyin(uspc_data, data, (u_int)size);
660 free(memp, M_IOCTLOPS);
664 *(caddr_t *)data = uspc_data;
666 } else if ((com & IOC_OUT) != 0 && size) {
668 * Zero the buffer so the user always
669 * gets back something deterministic.
672 } else if ((com & IOC_VOID) != 0) {
673 *(caddr_t *)data = uspc_data;
678 if ((tmp = *(int *)data))
679 fp->f_flag |= FNONBLOCK;
681 fp->f_flag &= ~FNONBLOCK;
686 if ((tmp = *(int *)data))
687 fp->f_flag |= FASYNC;
689 fp->f_flag &= ~FASYNC;
690 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred);
695 * If there is a override function,
696 * call it instead of directly routing the call
698 if (map != NULL && iomc->wrapfunc != NULL)
699 error = iomc->wrapfunc(fp, com, ocom, data, cred);
701 error = fo_ioctl(fp, com, data, cred);
703 * Copy any data to user, size was
704 * already set and checked above.
706 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
707 error = copyout(data, uspc_data, (u_int)size);
711 free(memp, M_IOCTLOPS);
718 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
720 struct ioctl_map_entry *ne;
722 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
723 he->subsys != NULL && *he->subsys != '\0');
725 ne = malloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
727 ne->subsys = he->subsys;
728 ne->cmd_ranges = he->cmd_ranges;
730 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
736 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
738 struct ioctl_map_entry *ne;
740 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
742 LIST_FOREACH(ne, &he->map->mapping, entries) {
743 if (ne->cmd_ranges != he->cmd_ranges)
745 LIST_REMOVE(ne, entries);
746 free(ne, M_IOCTLMAP);
752 static int nselcoll; /* Select collisions since boot */
754 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
757 * Select system call.
760 sys_select(struct select_args *uap)
762 struct proc *p = curproc;
765 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
766 * infds with the new FD_SETSIZE of 1024, and more than enough for
767 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
770 fd_mask s_selbits[howmany(2048, NFDBITS)];
771 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
772 struct timeval atv, rtv, ttv;
773 int ncoll, error, timo;
774 u_int nbufbytes, ncpbytes, nfdbits;
778 if (uap->nd > p->p_fd->fd_nfiles)
779 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
782 * Allocate just enough bits for the non-null fd_sets. Use the
783 * preallocated auto buffer if possible.
785 nfdbits = roundup(uap->nd, NFDBITS);
786 ncpbytes = nfdbits / NBBY;
789 nbufbytes += 2 * ncpbytes;
791 nbufbytes += 2 * ncpbytes;
793 nbufbytes += 2 * ncpbytes;
794 if (nbufbytes <= sizeof s_selbits)
795 selbits = &s_selbits[0];
797 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
800 * Assign pointers into the bit buffers and fetch the input bits.
801 * Put the output buffers together so that they can be bzeroed
805 #define getbits(name, x) \
807 if (uap->name == NULL) \
810 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
812 sbp += ncpbytes / sizeof *sbp; \
813 error = copyin(uap->name, ibits[x], ncpbytes); \
823 bzero(selbits, nbufbytes / 2);
826 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
830 if (itimerfix(&atv)) {
834 getmicrouptime(&rtv);
835 timevaladd(&atv, &rtv);
843 p->p_flag |= P_SELECT;
844 error = selscan(p, ibits, obits, uap->nd, &uap->sysmsg_result);
845 if (error || uap->sysmsg_result)
847 if (atv.tv_sec || atv.tv_usec) {
848 getmicrouptime(&rtv);
849 if (timevalcmp(&rtv, &atv, >=))
852 timevalsub(&ttv, &rtv);
853 timo = ttv.tv_sec > 24 * 60 * 60 ?
854 24 * 60 * 60 * hz : tvtohz_high(&ttv);
857 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
861 p->p_flag &= ~P_SELECT;
863 error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
869 p->p_flag &= ~P_SELECT;
870 /* select is not restarted after signals... */
871 if (error == ERESTART)
873 if (error == EWOULDBLOCK)
875 #define putbits(name, x) \
876 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
886 if (selbits != &s_selbits[0])
887 free(selbits, M_SELECT);
892 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd, int *res)
898 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
899 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
901 for (msk = 0; msk < 3; msk++) {
902 if (ibits[msk] == NULL)
904 for (i = 0; i < nfd; i += NFDBITS) {
905 bits = ibits[msk][i/NFDBITS];
906 /* ffs(int mask) not portable, fd_mask is long */
907 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
910 fp = holdfp(p->p_fd, fd, -1);
913 if (fo_poll(fp, flag[msk], fp->f_cred)) {
914 obits[msk][(fd)/NFDBITS] |=
915 ((fd_mask)1 << ((fd) % NFDBITS));
930 sys_poll(struct poll_args *uap)
933 struct pollfd smallbits[32];
934 struct timeval atv, rtv, ttv;
935 int ncoll, error = 0, timo;
938 struct proc *p = curproc;
942 * This is kinda bogus. We have fd limits, but that is not
943 * really related to the size of the pollfd array. Make sure
944 * we let the process use at least FD_SETSIZE entries and at
945 * least enough for the current limits. We want to be reasonably
946 * safe, but not overly restrictive.
948 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
950 ni = nfds * sizeof(struct pollfd);
951 if (ni > sizeof(smallbits))
952 bits = malloc(ni, M_TEMP, M_WAITOK);
955 error = copyin(uap->fds, bits, ni);
958 if (uap->timeout != INFTIM) {
959 atv.tv_sec = uap->timeout / 1000;
960 atv.tv_usec = (uap->timeout % 1000) * 1000;
961 if (itimerfix(&atv)) {
965 getmicrouptime(&rtv);
966 timevaladd(&atv, &rtv);
974 p->p_flag |= P_SELECT;
975 error = pollscan(p, bits, nfds, &uap->sysmsg_result);
976 if (error || uap->sysmsg_result)
978 if (atv.tv_sec || atv.tv_usec) {
979 getmicrouptime(&rtv);
980 if (timevalcmp(&rtv, &atv, >=))
983 timevalsub(&ttv, &rtv);
984 timo = ttv.tv_sec > 24 * 60 * 60 ?
985 24 * 60 * 60 * hz : tvtohz_high(&ttv);
988 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
992 p->p_flag &= ~P_SELECT;
993 error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
998 p->p_flag &= ~P_SELECT;
999 /* poll is not restarted after signals... */
1000 if (error == ERESTART)
1002 if (error == EWOULDBLOCK)
1005 error = copyout(bits, uap->fds, ni);
1010 if (ni > sizeof(smallbits))
1016 pollscan(struct proc *p, struct pollfd *fds, u_int nfd, int *res)
1022 for (i = 0; i < nfd; i++, fds++) {
1023 if (fds->fd >= p->p_fd->fd_nfiles) {
1024 fds->revents = POLLNVAL;
1026 } else if (fds->fd < 0) {
1029 fp = holdfp(p->p_fd, fds->fd, -1);
1031 fds->revents = POLLNVAL;
1035 * Note: backend also returns POLLHUP and
1036 * POLLERR if appropriate.
1038 fds->revents = fo_poll(fp, fds->events,
1040 if (fds->revents != 0)
1051 * OpenBSD poll system call.
1052 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1055 sys_openbsd_poll(struct openbsd_poll_args *uap)
1057 return (sys_poll((struct poll_args *)uap));
1062 seltrue(dev_t dev, int events, struct thread *td)
1064 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1068 * Record a select request. A global wait must be used since a process/thread
1069 * might go away after recording its request.
1072 selrecord(struct thread *selector, struct selinfo *sip)
1077 if ((p = selector->td_proc) == NULL)
1078 panic("selrecord: thread needs a process");
1081 if (sip->si_pid == mypid)
1083 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1084 p->p_wchan == (caddr_t)&selwait) {
1085 sip->si_flags |= SI_COLL;
1087 sip->si_pid = mypid;
1092 * Do a wakeup when a selectable event occurs.
1095 selwakeup(struct selinfo *sip)
1099 if (sip->si_pid == 0)
1101 if (sip->si_flags & SI_COLL) {
1103 sip->si_flags &= ~SI_COLL;
1104 wakeup((caddr_t)&selwait); /* YYY fixable */
1106 p = pfind(sip->si_pid);
1110 if (p->p_wchan == (caddr_t)&selwait) {
1112 * Flag the process to break the tsleep when
1113 * setrunnable is called, but only call setrunnable
1114 * here if the process is not in a stopped state.
1116 p->p_flag |= P_BREAKTSLEEP;
1117 if ((p->p_flag & P_STOPPED) == 0)
1119 } else if (p->p_flag & P_SELECT) {
1120 p->p_flag &= ~P_SELECT;