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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/filedesc.h>
47 #include <sys/filio.h>
48 #include <sys/fcntl.h>
51 #include <sys/signalvar.h>
52 #include <sys/socketvar.h>
54 #include <sys/kernel.h>
56 #include <sys/limits.h>
57 #include <sys/malloc.h>
59 #include <sys/resourcevar.h>
60 #include <sys/selinfo.h>
61 #include <sys/sleepqueue.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/vnode.h>
68 #include <sys/condvar.h>
70 #include <sys/ktrace.h>
73 #include <security/audit/audit.h>
75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
77 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
79 static int pollout(struct thread *, struct pollfd *, struct pollfd *,
81 static int pollscan(struct thread *, struct pollfd *, u_int);
82 static int pollrescan(struct thread *);
83 static int selscan(struct thread *, fd_mask **, fd_mask **, int);
84 static int selrescan(struct thread *, fd_mask **, fd_mask **);
85 static void selfdalloc(struct thread *, void *);
86 static void selfdfree(struct seltd *, struct selfd *);
87 static int dofileread(struct thread *, int, struct file *, struct uio *,
89 static int dofilewrite(struct thread *, int, struct file *, struct uio *,
91 static void doselwakeup(struct selinfo *, int);
92 static void seltdinit(struct thread *);
93 static int seltdwait(struct thread *, int);
94 static void seltdclear(struct thread *);
97 * One seltd per-thread allocated on demand as needed.
99 * t - protected by st_mtx
100 * k - Only accessed by curthread or read-only
103 STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */
104 struct selfd *st_free1; /* (k) free fd for read set. */
105 struct selfd *st_free2; /* (k) free fd for write set. */
106 struct mtx st_mtx; /* Protects struct seltd */
107 struct cv st_wait; /* (t) Wait channel. */
108 int st_flags; /* (t) SELTD_ flags. */
111 #define SELTD_PENDING 0x0001 /* We have pending events. */
112 #define SELTD_RESCAN 0x0002 /* Doing a rescan. */
115 * One selfd allocated per-thread per-file-descriptor.
116 * f - protected by sf_mtx
119 STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */
120 TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */
121 struct selinfo *sf_si; /* (f) selinfo when linked. */
122 struct mtx *sf_mtx; /* Pointer to selinfo mtx. */
123 struct seltd *sf_td; /* (k) owning seltd. */
124 void *sf_cookie; /* (k) fd or pollfd. */
127 static uma_zone_t selfd_zone;
128 static struct mtx_pool *mtxpool_select;
130 #ifndef _SYS_SYSPROTO_H_
140 struct read_args *uap;
146 if (uap->nbyte > INT_MAX)
148 aiov.iov_base = uap->buf;
149 aiov.iov_len = uap->nbyte;
150 auio.uio_iov = &aiov;
152 auio.uio_resid = uap->nbyte;
153 auio.uio_segflg = UIO_USERSPACE;
154 error = kern_readv(td, uap->fd, &auio);
159 * Positioned read system call
161 #ifndef _SYS_SYSPROTO_H_
173 struct pread_args *uap;
179 if (uap->nbyte > INT_MAX)
181 aiov.iov_base = uap->buf;
182 aiov.iov_len = uap->nbyte;
183 auio.uio_iov = &aiov;
185 auio.uio_resid = uap->nbyte;
186 auio.uio_segflg = UIO_USERSPACE;
187 error = kern_preadv(td, uap->fd, &auio, uap->offset);
192 freebsd6_pread(td, uap)
194 struct freebsd6_pread_args *uap;
196 struct pread_args oargs;
199 oargs.buf = uap->buf;
200 oargs.nbyte = uap->nbyte;
201 oargs.offset = uap->offset;
202 return (pread(td, &oargs));
206 * Scatter read system call.
208 #ifndef _SYS_SYSPROTO_H_
216 readv(struct thread *td, struct readv_args *uap)
221 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
224 error = kern_readv(td, uap->fd, auio);
230 kern_readv(struct thread *td, int fd, struct uio *auio)
235 error = fget_read(td, fd, &fp);
238 error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
244 * Scatter positioned read system call.
246 #ifndef _SYS_SYSPROTO_H_
255 preadv(struct thread *td, struct preadv_args *uap)
260 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
263 error = kern_preadv(td, uap->fd, auio, uap->offset);
269 kern_preadv(td, fd, auio, offset)
278 error = fget_read(td, fd, &fp);
281 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
283 else if (offset < 0 && fp->f_vnode->v_type != VCHR)
286 error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
292 * Common code for readv and preadv that reads data in
293 * from a file using the passed in uio, offset, and flags.
296 dofileread(td, fd, fp, auio, offset, flags)
307 struct uio *ktruio = NULL;
310 /* Finish zero length reads right here */
311 if (auio->uio_resid == 0) {
312 td->td_retval[0] = 0;
315 auio->uio_rw = UIO_READ;
316 auio->uio_offset = offset;
319 if (KTRPOINT(td, KTR_GENIO))
320 ktruio = cloneuio(auio);
322 cnt = auio->uio_resid;
323 if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
324 if (auio->uio_resid != cnt && (error == ERESTART ||
325 error == EINTR || error == EWOULDBLOCK))
328 cnt -= auio->uio_resid;
330 if (ktruio != NULL) {
331 ktruio->uio_resid = cnt;
332 ktrgenio(fd, UIO_READ, ktruio, error);
335 td->td_retval[0] = cnt;
339 #ifndef _SYS_SYSPROTO_H_
349 struct write_args *uap;
355 if (uap->nbyte > INT_MAX)
357 aiov.iov_base = (void *)(uintptr_t)uap->buf;
358 aiov.iov_len = uap->nbyte;
359 auio.uio_iov = &aiov;
361 auio.uio_resid = uap->nbyte;
362 auio.uio_segflg = UIO_USERSPACE;
363 error = kern_writev(td, uap->fd, &auio);
368 * Positioned write system call.
370 #ifndef _SYS_SYSPROTO_H_
382 struct pwrite_args *uap;
388 if (uap->nbyte > INT_MAX)
390 aiov.iov_base = (void *)(uintptr_t)uap->buf;
391 aiov.iov_len = uap->nbyte;
392 auio.uio_iov = &aiov;
394 auio.uio_resid = uap->nbyte;
395 auio.uio_segflg = UIO_USERSPACE;
396 error = kern_pwritev(td, uap->fd, &auio, uap->offset);
401 freebsd6_pwrite(td, uap)
403 struct freebsd6_pwrite_args *uap;
405 struct pwrite_args oargs;
408 oargs.buf = uap->buf;
409 oargs.nbyte = uap->nbyte;
410 oargs.offset = uap->offset;
411 return (pwrite(td, &oargs));
415 * Gather write system call.
417 #ifndef _SYS_SYSPROTO_H_
425 writev(struct thread *td, struct writev_args *uap)
430 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
433 error = kern_writev(td, uap->fd, auio);
439 kern_writev(struct thread *td, int fd, struct uio *auio)
444 error = fget_write(td, fd, &fp);
447 error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
453 * Gather positioned write system call.
455 #ifndef _SYS_SYSPROTO_H_
456 struct pwritev_args {
464 pwritev(struct thread *td, struct pwritev_args *uap)
469 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
472 error = kern_pwritev(td, uap->fd, auio, uap->offset);
478 kern_pwritev(td, fd, auio, offset)
487 error = fget_write(td, fd, &fp);
490 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
492 else if (offset < 0 && fp->f_vnode->v_type != VCHR)
495 error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
501 * Common code for writev and pwritev that writes data to
502 * a file using the passed in uio, offset, and flags.
505 dofilewrite(td, fd, fp, auio, offset, flags)
516 struct uio *ktruio = NULL;
519 auio->uio_rw = UIO_WRITE;
521 auio->uio_offset = offset;
523 if (KTRPOINT(td, KTR_GENIO))
524 ktruio = cloneuio(auio);
526 cnt = auio->uio_resid;
527 if (fp->f_type == DTYPE_VNODE)
529 if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
530 if (auio->uio_resid != cnt && (error == ERESTART ||
531 error == EINTR || error == EWOULDBLOCK))
533 /* Socket layer is responsible for issuing SIGPIPE. */
534 if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
535 PROC_LOCK(td->td_proc);
536 tdksignal(td, SIGPIPE, NULL);
537 PROC_UNLOCK(td->td_proc);
540 cnt -= auio->uio_resid;
542 if (ktruio != NULL) {
543 ktruio->uio_resid = cnt;
544 ktrgenio(fd, UIO_WRITE, ktruio, error);
547 td->td_retval[0] = cnt;
552 * Truncate a file given a file descriptor.
554 * Can't use fget_write() here, since must return EINVAL and not EBADF if the
555 * descriptor isn't writable.
558 kern_ftruncate(td, fd, length)
569 error = fget(td, fd, &fp);
572 AUDIT_ARG_FILE(td->td_proc, fp);
573 if (!(fp->f_flag & FWRITE)) {
577 error = fo_truncate(fp, length, td->td_ucred, td);
582 #ifndef _SYS_SYSPROTO_H_
583 struct ftruncate_args {
592 struct ftruncate_args *uap;
595 return (kern_ftruncate(td, uap->fd, uap->length));
598 #if defined(COMPAT_43)
599 #ifndef _SYS_SYSPROTO_H_
600 struct oftruncate_args {
608 struct oftruncate_args *uap;
611 return (kern_ftruncate(td, uap->fd, uap->length));
613 #endif /* COMPAT_43 */
615 #ifndef _SYS_SYSPROTO_H_
624 ioctl(struct thread *td, struct ioctl_args *uap)
631 if (uap->com > 0xffffffff) {
633 "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
634 td->td_proc->p_pid, td->td_name, uap->com);
635 uap->com &= 0xffffffff;
640 * Interpret high order word to find amount of data to be
641 * copied to/from the user's address space.
643 size = IOCPARM_LEN(com);
644 if ((size > IOCPARM_MAX) ||
645 ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) ||
646 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
647 ((com & IOC_OUT) && size == 0) ||
649 ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
651 ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
655 if (com & IOC_VOID) {
656 /* Integer argument. */
657 arg = (intptr_t)uap->data;
661 data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
663 data = (void *)&uap->data;
665 error = copyin(uap->data, data, (u_int)size);
668 free(data, M_IOCTLOPS);
671 } else if (com & IOC_OUT) {
673 * Zero the buffer so the user always
674 * gets back something deterministic.
679 error = kern_ioctl(td, uap->fd, com, data);
681 if (error == 0 && (com & IOC_OUT))
682 error = copyout(data, uap->data, (u_int)size);
685 free(data, M_IOCTLOPS);
690 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
693 struct filedesc *fdp;
699 if ((error = fget(td, fd, &fp)) != 0)
701 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
705 fdp = td->td_proc->p_fd;
709 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
710 FILEDESC_XUNLOCK(fdp);
714 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
715 FILEDESC_XUNLOCK(fdp);
718 if ((tmp = *(int *)data))
719 atomic_set_int(&fp->f_flag, FNONBLOCK);
721 atomic_clear_int(&fp->f_flag, FNONBLOCK);
725 if ((tmp = *(int *)data))
726 atomic_set_int(&fp->f_flag, FASYNC);
728 atomic_clear_int(&fp->f_flag, FASYNC);
733 error = fo_ioctl(fp, com, data, td->td_ucred, td);
740 poll_no_poll(int events)
743 * Return true for read/write. If the user asked for something
744 * special, return POLLNVAL, so that clients have a way of
745 * determining reliably whether or not the extended
746 * functionality is present without hard-coding knowledge
747 * of specific filesystem implementations.
749 if (events & ~POLLSTANDARD)
752 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
756 pselect(struct thread *td, struct pselect_args *uap)
759 struct timeval tv, *tvp;
763 if (uap->ts != NULL) {
764 error = copyin(uap->ts, &ts, sizeof(ts));
767 TIMESPEC_TO_TIMEVAL(&tv, &ts);
771 if (uap->sm != NULL) {
772 error = copyin(uap->sm, &set, sizeof(set));
778 return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
783 kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
784 struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
789 error = kern_sigprocmask(td, SIG_SETMASK, uset,
790 &td->td_oldsigmask, 0);
793 td->td_pflags |= TDP_OLDMASK;
795 * Make sure that ast() is called on return to
796 * usermode and TDP_OLDMASK is cleared, restoring old
800 td->td_flags |= TDF_ASTPENDING;
803 error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
807 #ifndef _SYS_SYSPROTO_H_
810 fd_set *in, *ou, *ex;
815 select(struct thread *td, struct select_args *uap)
817 struct timeval tv, *tvp;
820 if (uap->tv != NULL) {
821 error = copyin(uap->tv, &tv, sizeof(tv));
828 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
833 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
834 fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
836 struct filedesc *fdp;
838 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
839 * infds with the new FD_SETSIZE of 1024, and more than enough for
840 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
843 fd_mask s_selbits[howmany(2048, NFDBITS)];
844 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
845 struct timeval atv, rtv, ttv;
847 u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
851 fdp = td->td_proc->p_fd;
852 if (nd > fdp->fd_lastfile + 1)
853 nd = fdp->fd_lastfile + 1;
856 * Allocate just enough bits for the non-null fd_sets. Use the
857 * preallocated auto buffer if possible.
859 nfdbits = roundup(nd, NFDBITS);
860 ncpbytes = nfdbits / NBBY;
861 ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
864 nbufbytes += 2 * ncpbytes;
866 nbufbytes += 2 * ncpbytes;
868 nbufbytes += 2 * ncpbytes;
869 if (nbufbytes <= sizeof s_selbits)
870 selbits = &s_selbits[0];
872 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
875 * Assign pointers into the bit buffers and fetch the input bits.
876 * Put the output buffers together so that they can be bzeroed
880 #define getbits(name, x) \
882 if (name == NULL) { \
886 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
888 sbp += ncpbytes / sizeof *sbp; \
889 error = copyin(name, ibits[x], ncpubytes); \
892 bzero((char *)ibits[x] + ncpubytes, \
893 ncpbytes - ncpubytes); \
901 #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
903 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
904 * we are running under 32-bit emulation. This should be more
907 #define swizzle_fdset(bits) \
908 if (abi_nfdbits != NFDBITS && bits != NULL) { \
910 for (i = 0; i < ncpbytes / sizeof *sbp; i++) \
911 bits[i] = (bits[i] >> 32) | (bits[i] << 32); \
914 #define swizzle_fdset(bits)
917 /* Make sure the bit order makes it through an ABI transition */
918 swizzle_fdset(ibits[0]);
919 swizzle_fdset(ibits[1]);
920 swizzle_fdset(ibits[2]);
923 bzero(selbits, nbufbytes / 2);
927 if (itimerfix(&atv)) {
931 getmicrouptime(&rtv);
932 timevaladd(&atv, &rtv);
939 /* Iterate until the timeout expires or descriptors become ready. */
941 error = selscan(td, ibits, obits, nd);
942 if (error || td->td_retval[0] != 0)
944 if (atv.tv_sec || atv.tv_usec) {
945 getmicrouptime(&rtv);
946 if (timevalcmp(&rtv, &atv, >=))
949 timevalsub(&ttv, &rtv);
950 timo = ttv.tv_sec > 24 * 60 * 60 ?
951 24 * 60 * 60 * hz : tvtohz(&ttv);
953 error = seltdwait(td, timo);
956 error = selrescan(td, ibits, obits);
957 if (error || td->td_retval[0] != 0)
963 /* select is not restarted after signals... */
964 if (error == ERESTART)
966 if (error == EWOULDBLOCK)
969 /* swizzle bit order back, if necessary */
970 swizzle_fdset(obits[0]);
971 swizzle_fdset(obits[1]);
972 swizzle_fdset(obits[2]);
975 #define putbits(name, x) \
976 if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
986 if (selbits != &s_selbits[0])
987 free(selbits, M_SELECT);
992 * Convert a select bit set to poll flags.
994 * The backend always returns POLLHUP/POLLERR if appropriate and we
995 * return this as a set bit in any set.
997 static int select_flags[3] = {
998 POLLRDNORM | POLLHUP | POLLERR,
999 POLLWRNORM | POLLHUP | POLLERR,
1000 POLLRDBAND | POLLERR
1004 * Compute the fo_poll flags required for a fd given by the index and
1005 * bit position in the fd_mask array.
1008 selflags(fd_mask **ibits, int idx, fd_mask bit)
1014 for (msk = 0; msk < 3; msk++) {
1015 if (ibits[msk] == NULL)
1017 if ((ibits[msk][idx] & bit) == 0)
1019 flags |= select_flags[msk];
1025 * Set the appropriate output bits given a mask of fired events and the
1026 * input bits originally requested.
1029 selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
1035 for (msk = 0; msk < 3; msk++) {
1036 if ((events & select_flags[msk]) == 0)
1038 if (ibits[msk] == NULL)
1040 if ((ibits[msk][idx] & bit) == 0)
1043 * XXX Check for a duplicate set. This can occur because a
1044 * socket calls selrecord() twice for each poll() call
1045 * resulting in two selfds per real fd. selrescan() will
1046 * call selsetbits twice as a result.
1048 if ((obits[msk][idx] & bit) != 0)
1050 obits[msk][idx] |= bit;
1058 * Traverse the list of fds attached to this thread's seltd and check for
1062 selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1064 struct filedesc *fdp;
1073 fdp = td->td_proc->p_fd;
1076 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1077 fd = (int)(uintptr_t)sfp->sf_cookie;
1079 selfdfree(stp, sfp);
1080 /* If the selinfo wasn't cleared the event didn't fire. */
1083 if ((fp = fget_unlocked(fdp, fd)) == NULL)
1086 bit = (fd_mask)1 << (fd % NFDBITS);
1087 ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1090 n += selsetbits(ibits, obits, idx, bit, ev);
1093 td->td_retval[0] = n;
1098 * Perform the initial filedescriptor scan and register ourselves with
1102 selscan(td, ibits, obits, nfd)
1104 fd_mask **ibits, **obits;
1107 struct filedesc *fdp;
1110 int ev, flags, end, fd;
1113 fdp = td->td_proc->p_fd;
1115 for (idx = 0, fd = 0; fd < nfd; idx++) {
1116 end = imin(fd + NFDBITS, nfd);
1117 for (bit = 1; fd < end; bit <<= 1, fd++) {
1118 /* Compute the list of events we're interested in. */
1119 flags = selflags(ibits, idx, bit);
1122 if ((fp = fget_unlocked(fdp, fd)) == NULL)
1124 selfdalloc(td, (void *)(uintptr_t)fd);
1125 ev = fo_poll(fp, flags, td->td_ucred, td);
1128 n += selsetbits(ibits, obits, idx, bit, ev);
1132 td->td_retval[0] = n;
1136 #ifndef _SYS_SYSPROTO_H_
1146 struct poll_args *uap;
1148 struct pollfd *bits;
1149 struct pollfd smallbits[32];
1150 struct timeval atv, rtv, ttv;
1151 int error = 0, timo;
1156 if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1158 ni = nfds * sizeof(struct pollfd);
1159 if (ni > sizeof(smallbits))
1160 bits = malloc(ni, M_TEMP, M_WAITOK);
1163 error = copyin(uap->fds, bits, ni);
1166 if (uap->timeout != INFTIM) {
1167 atv.tv_sec = uap->timeout / 1000;
1168 atv.tv_usec = (uap->timeout % 1000) * 1000;
1169 if (itimerfix(&atv)) {
1173 getmicrouptime(&rtv);
1174 timevaladd(&atv, &rtv);
1181 /* Iterate until the timeout expires or descriptors become ready. */
1183 error = pollscan(td, bits, nfds);
1184 if (error || td->td_retval[0] != 0)
1186 if (atv.tv_sec || atv.tv_usec) {
1187 getmicrouptime(&rtv);
1188 if (timevalcmp(&rtv, &atv, >=))
1191 timevalsub(&ttv, &rtv);
1192 timo = ttv.tv_sec > 24 * 60 * 60 ?
1193 24 * 60 * 60 * hz : tvtohz(&ttv);
1195 error = seltdwait(td, timo);
1198 error = pollrescan(td);
1199 if (error || td->td_retval[0] != 0)
1205 /* poll is not restarted after signals... */
1206 if (error == ERESTART)
1208 if (error == EWOULDBLOCK)
1211 error = pollout(td, bits, uap->fds, nfds);
1216 if (ni > sizeof(smallbits))
1222 pollrescan(struct thread *td)
1228 struct filedesc *fdp;
1234 fdp = td->td_proc->p_fd;
1236 FILEDESC_SLOCK(fdp);
1237 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1238 fd = (struct pollfd *)sfp->sf_cookie;
1240 selfdfree(stp, sfp);
1241 /* If the selinfo wasn't cleared the event didn't fire. */
1244 fp = fdp->fd_ofiles[fd->fd];
1246 fd->revents = POLLNVAL;
1251 * Note: backend also returns POLLHUP and
1252 * POLLERR if appropriate.
1254 fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1255 if (fd->revents != 0)
1258 FILEDESC_SUNLOCK(fdp);
1260 td->td_retval[0] = n;
1266 pollout(td, fds, ufds, nfd)
1269 struct pollfd *ufds;
1276 for (i = 0; i < nfd; i++) {
1277 error = copyout(&fds->revents, &ufds->revents,
1278 sizeof(ufds->revents));
1281 if (fds->revents != 0)
1286 td->td_retval[0] = n;
1291 pollscan(td, fds, nfd)
1296 struct filedesc *fdp = td->td_proc->p_fd;
1301 FILEDESC_SLOCK(fdp);
1302 for (i = 0; i < nfd; i++, fds++) {
1303 if (fds->fd >= fdp->fd_nfiles) {
1304 fds->revents = POLLNVAL;
1306 } else if (fds->fd < 0) {
1309 fp = fdp->fd_ofiles[fds->fd];
1311 fds->revents = POLLNVAL;
1315 * Note: backend also returns POLLHUP and
1316 * POLLERR if appropriate.
1318 selfdalloc(td, fds);
1319 fds->revents = fo_poll(fp, fds->events,
1322 * POSIX requires POLLOUT to be never
1323 * set simultaneously with POLLHUP.
1325 if ((fds->revents & POLLHUP) != 0)
1326 fds->revents &= ~POLLOUT;
1328 if (fds->revents != 0)
1333 FILEDESC_SUNLOCK(fdp);
1334 td->td_retval[0] = n;
1339 * OpenBSD poll system call.
1341 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1343 #ifndef _SYS_SYSPROTO_H_
1344 struct openbsd_poll_args {
1351 openbsd_poll(td, uap)
1352 register struct thread *td;
1353 register struct openbsd_poll_args *uap;
1355 return (poll(td, (struct poll_args *)uap));
1359 * XXX This was created specifically to support netncp and netsmb. This
1360 * allows the caller to specify a socket to wait for events on. It returns
1361 * 0 if any events matched and an error otherwise. There is no way to
1362 * determine which events fired.
1365 selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1367 struct timeval atv, rtv, ttv;
1372 if (itimerfix(&atv))
1374 getmicrouptime(&rtv);
1375 timevaladd(&atv, &rtv);
1384 * Iterate until the timeout expires or the socket becomes ready.
1387 selfdalloc(td, NULL);
1388 error = sopoll(so, events, NULL, td);
1389 /* error here is actually the ready events. */
1392 if (atv.tv_sec || atv.tv_usec) {
1393 getmicrouptime(&rtv);
1394 if (timevalcmp(&rtv, &atv, >=)) {
1396 return (EWOULDBLOCK);
1399 timevalsub(&ttv, &rtv);
1400 timo = ttv.tv_sec > 24 * 60 * 60 ?
1401 24 * 60 * 60 * hz : tvtohz(&ttv);
1403 error = seltdwait(td, timo);
1408 /* XXX Duplicates ncp/smb behavior. */
1409 if (error == ERESTART)
1415 * Preallocate two selfds associated with 'cookie'. Some fo_poll routines
1416 * have two select sets, one for read and another for write.
1419 selfdalloc(struct thread *td, void *cookie)
1424 if (stp->st_free1 == NULL)
1425 stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1426 stp->st_free1->sf_td = stp;
1427 stp->st_free1->sf_cookie = cookie;
1428 if (stp->st_free2 == NULL)
1429 stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1430 stp->st_free2->sf_td = stp;
1431 stp->st_free2->sf_cookie = cookie;
1435 selfdfree(struct seltd *stp, struct selfd *sfp)
1437 STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1438 mtx_lock(sfp->sf_mtx);
1440 TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1441 mtx_unlock(sfp->sf_mtx);
1442 uma_zfree(selfd_zone, sfp);
1446 * Record a select request.
1449 selrecord(selector, sip)
1450 struct thread *selector;
1451 struct selinfo *sip;
1457 stp = selector->td_sel;
1459 * Don't record when doing a rescan.
1461 if (stp->st_flags & SELTD_RESCAN)
1464 * Grab one of the preallocated descriptors.
1467 if ((sfp = stp->st_free1) != NULL)
1468 stp->st_free1 = NULL;
1469 else if ((sfp = stp->st_free2) != NULL)
1470 stp->st_free2 = NULL;
1472 panic("selrecord: No free selfd on selq");
1475 mtxp = mtx_pool_find(mtxpool_select, sip);
1477 * Initialize the sfp and queue it in the thread.
1481 STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1483 * Now that we've locked the sip, check for initialization.
1486 if (sip->si_mtx == NULL) {
1488 TAILQ_INIT(&sip->si_tdlist);
1491 * Add this thread to the list of selfds listening on this selinfo.
1493 TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1494 mtx_unlock(sip->si_mtx);
1497 /* Wake up a selecting thread. */
1500 struct selinfo *sip;
1502 doselwakeup(sip, -1);
1505 /* Wake up a selecting thread, and set its priority. */
1507 selwakeuppri(sip, pri)
1508 struct selinfo *sip;
1511 doselwakeup(sip, pri);
1515 * Do a wakeup when a selectable event occurs.
1518 doselwakeup(sip, pri)
1519 struct selinfo *sip;
1526 /* If it's not initialized there can't be any waiters. */
1527 if (sip->si_mtx == NULL)
1530 * Locking the selinfo locks all selfds associated with it.
1532 mtx_lock(sip->si_mtx);
1533 TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1535 * Once we remove this sfp from the list and clear the
1536 * sf_si seltdclear will know to ignore this si.
1538 TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1541 mtx_lock(&stp->st_mtx);
1542 stp->st_flags |= SELTD_PENDING;
1543 cv_broadcastpri(&stp->st_wait, pri);
1544 mtx_unlock(&stp->st_mtx);
1546 mtx_unlock(sip->si_mtx);
1550 seltdinit(struct thread *td)
1554 if ((stp = td->td_sel) != NULL)
1556 td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1557 mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1558 cv_init(&stp->st_wait, "select");
1561 STAILQ_INIT(&stp->st_selq);
1565 seltdwait(struct thread *td, int timo)
1572 * An event of interest may occur while we do not hold the seltd
1573 * locked so check the pending flag before we sleep.
1575 mtx_lock(&stp->st_mtx);
1577 * Any further calls to selrecord will be a rescan.
1579 stp->st_flags |= SELTD_RESCAN;
1580 if (stp->st_flags & SELTD_PENDING) {
1581 mtx_unlock(&stp->st_mtx);
1585 error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1587 error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1588 mtx_unlock(&stp->st_mtx);
1594 seltdfini(struct thread *td)
1602 uma_zfree(selfd_zone, stp->st_free1);
1604 uma_zfree(selfd_zone, stp->st_free2);
1606 free(stp, M_SELECT);
1610 * Remove the references to the thread from all of the objects we were
1614 seltdclear(struct thread *td)
1621 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1622 selfdfree(stp, sfp);
1626 static void selectinit(void *);
1627 SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1629 selectinit(void *dummy __unused)
1632 selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1633 NULL, NULL, UMA_ALIGN_PTR, 0);
1634 mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);