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 pollfd *, struct pollfd *, u_int);
80 static int pollscan(struct thread *, struct pollfd *, u_int);
81 static int pollrescan(struct thread *);
82 static int selscan(struct thread *, fd_mask **, fd_mask **, int);
83 static int selrescan(struct thread *, fd_mask **, fd_mask **);
84 static void selfdalloc(struct thread *, void *);
85 static void selfdfree(struct seltd *, struct selfd *);
86 static int dofileread(struct thread *, int, struct file *, struct uio *,
88 static int dofilewrite(struct thread *, int, struct file *, struct uio *,
90 static void doselwakeup(struct selinfo *, int);
91 static void seltdinit(struct thread *);
92 static int seltdwait(struct thread *, int);
93 static void seltdclear(struct thread *);
96 * One seltd per-thread allocated on demand as needed.
98 * t - protected by st_mtx
99 * k - Only accessed by curthread or read-only
102 STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */
103 struct selfd *st_free1; /* (k) free fd for read set. */
104 struct selfd *st_free2; /* (k) free fd for write set. */
105 struct mtx st_mtx; /* Protects struct seltd */
106 struct cv st_wait; /* (t) Wait channel. */
107 int st_flags; /* (t) SELTD_ flags. */
110 #define SELTD_PENDING 0x0001 /* We have pending events. */
111 #define SELTD_RESCAN 0x0002 /* Doing a rescan. */
114 * One selfd allocated per-thread per-file-descriptor.
115 * f - protected by sf_mtx
118 STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */
119 TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */
120 struct selinfo *sf_si; /* (f) selinfo when linked. */
121 struct mtx *sf_mtx; /* Pointer to selinfo mtx. */
122 struct seltd *sf_td; /* (k) owning seltd. */
123 void *sf_cookie; /* (k) fd or pollfd. */
126 static uma_zone_t selfd_zone;
127 static struct mtx_pool *mtxpool_select;
129 #ifndef _SYS_SYSPROTO_H_
139 struct read_args *uap;
145 if (uap->nbyte > INT_MAX)
147 aiov.iov_base = uap->buf;
148 aiov.iov_len = uap->nbyte;
149 auio.uio_iov = &aiov;
151 auio.uio_resid = uap->nbyte;
152 auio.uio_segflg = UIO_USERSPACE;
153 error = kern_readv(td, uap->fd, &auio);
158 * Positioned read system call
160 #ifndef _SYS_SYSPROTO_H_
172 struct pread_args *uap;
178 if (uap->nbyte > INT_MAX)
180 aiov.iov_base = uap->buf;
181 aiov.iov_len = uap->nbyte;
182 auio.uio_iov = &aiov;
184 auio.uio_resid = uap->nbyte;
185 auio.uio_segflg = UIO_USERSPACE;
186 error = kern_preadv(td, uap->fd, &auio, uap->offset);
191 freebsd6_pread(td, uap)
193 struct freebsd6_pread_args *uap;
195 struct pread_args oargs;
198 oargs.buf = uap->buf;
199 oargs.nbyte = uap->nbyte;
200 oargs.offset = uap->offset;
201 return (pread(td, &oargs));
205 * Scatter read system call.
207 #ifndef _SYS_SYSPROTO_H_
215 readv(struct thread *td, struct readv_args *uap)
220 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
223 error = kern_readv(td, uap->fd, auio);
229 kern_readv(struct thread *td, int fd, struct uio *auio)
234 error = fget_read(td, fd, &fp);
237 error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
243 * Scatter positioned read system call.
245 #ifndef _SYS_SYSPROTO_H_
254 preadv(struct thread *td, struct preadv_args *uap)
259 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
262 error = kern_preadv(td, uap->fd, auio, uap->offset);
268 kern_preadv(td, fd, auio, offset)
277 error = fget_read(td, fd, &fp);
280 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
282 else if (offset < 0 && fp->f_vnode->v_type != VCHR)
285 error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
291 * Common code for readv and preadv that reads data in
292 * from a file using the passed in uio, offset, and flags.
295 dofileread(td, fd, fp, auio, offset, flags)
306 struct uio *ktruio = NULL;
309 /* Finish zero length reads right here */
310 if (auio->uio_resid == 0) {
311 td->td_retval[0] = 0;
314 auio->uio_rw = UIO_READ;
315 auio->uio_offset = offset;
318 if (KTRPOINT(td, KTR_GENIO))
319 ktruio = cloneuio(auio);
321 cnt = auio->uio_resid;
322 if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
323 if (auio->uio_resid != cnt && (error == ERESTART ||
324 error == EINTR || error == EWOULDBLOCK))
327 cnt -= auio->uio_resid;
329 if (ktruio != NULL) {
330 ktruio->uio_resid = cnt;
331 ktrgenio(fd, UIO_READ, ktruio, error);
334 td->td_retval[0] = cnt;
338 #ifndef _SYS_SYSPROTO_H_
348 struct write_args *uap;
354 if (uap->nbyte > INT_MAX)
356 aiov.iov_base = (void *)(uintptr_t)uap->buf;
357 aiov.iov_len = uap->nbyte;
358 auio.uio_iov = &aiov;
360 auio.uio_resid = uap->nbyte;
361 auio.uio_segflg = UIO_USERSPACE;
362 error = kern_writev(td, uap->fd, &auio);
367 * Positioned write system call.
369 #ifndef _SYS_SYSPROTO_H_
381 struct pwrite_args *uap;
387 if (uap->nbyte > INT_MAX)
389 aiov.iov_base = (void *)(uintptr_t)uap->buf;
390 aiov.iov_len = uap->nbyte;
391 auio.uio_iov = &aiov;
393 auio.uio_resid = uap->nbyte;
394 auio.uio_segflg = UIO_USERSPACE;
395 error = kern_pwritev(td, uap->fd, &auio, uap->offset);
400 freebsd6_pwrite(td, uap)
402 struct freebsd6_pwrite_args *uap;
404 struct pwrite_args oargs;
407 oargs.buf = uap->buf;
408 oargs.nbyte = uap->nbyte;
409 oargs.offset = uap->offset;
410 return (pwrite(td, &oargs));
414 * Gather write system call.
416 #ifndef _SYS_SYSPROTO_H_
424 writev(struct thread *td, struct writev_args *uap)
429 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
432 error = kern_writev(td, uap->fd, auio);
438 kern_writev(struct thread *td, int fd, struct uio *auio)
443 error = fget_write(td, fd, &fp);
446 error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
452 * Gather positioned write system call.
454 #ifndef _SYS_SYSPROTO_H_
455 struct pwritev_args {
463 pwritev(struct thread *td, struct pwritev_args *uap)
468 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
471 error = kern_pwritev(td, uap->fd, auio, uap->offset);
477 kern_pwritev(td, fd, auio, offset)
486 error = fget_write(td, fd, &fp);
489 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
491 else if (offset < 0 && fp->f_vnode->v_type != VCHR)
494 error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
500 * Common code for writev and pwritev that writes data to
501 * a file using the passed in uio, offset, and flags.
504 dofilewrite(td, fd, fp, auio, offset, flags)
515 struct uio *ktruio = NULL;
518 auio->uio_rw = UIO_WRITE;
520 auio->uio_offset = offset;
522 if (KTRPOINT(td, KTR_GENIO))
523 ktruio = cloneuio(auio);
525 cnt = auio->uio_resid;
526 if (fp->f_type == DTYPE_VNODE)
528 if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
529 if (auio->uio_resid != cnt && (error == ERESTART ||
530 error == EINTR || error == EWOULDBLOCK))
532 /* Socket layer is responsible for issuing SIGPIPE. */
533 if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
534 PROC_LOCK(td->td_proc);
535 psignal(td->td_proc, SIGPIPE);
536 PROC_UNLOCK(td->td_proc);
539 cnt -= auio->uio_resid;
541 if (ktruio != NULL) {
542 ktruio->uio_resid = cnt;
543 ktrgenio(fd, UIO_WRITE, ktruio, error);
546 td->td_retval[0] = cnt;
551 * Truncate a file given a file descriptor.
553 * Can't use fget_write() here, since must return EINVAL and not EBADF if the
554 * descriptor isn't writable.
557 kern_ftruncate(td, fd, length)
568 error = fget(td, fd, &fp);
571 AUDIT_ARG_FILE(td->td_proc, fp);
572 if (!(fp->f_flag & FWRITE)) {
576 error = fo_truncate(fp, length, td->td_ucred, td);
581 #ifndef _SYS_SYSPROTO_H_
582 struct ftruncate_args {
591 struct ftruncate_args *uap;
594 return (kern_ftruncate(td, uap->fd, uap->length));
597 #if defined(COMPAT_43)
598 #ifndef _SYS_SYSPROTO_H_
599 struct oftruncate_args {
607 struct oftruncate_args *uap;
610 return (kern_ftruncate(td, uap->fd, uap->length));
612 #endif /* COMPAT_43 */
614 #ifndef _SYS_SYSPROTO_H_
623 ioctl(struct thread *td, struct ioctl_args *uap)
630 if (uap->com > 0xffffffff) {
632 "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
633 td->td_proc->p_pid, td->td_name, uap->com);
634 uap->com &= 0xffffffff;
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) ||
644 ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) ||
645 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
646 ((com & IOC_OUT) && size == 0) ||
648 ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
650 ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
654 if (com & IOC_VOID) {
655 /* Integer argument. */
656 arg = (intptr_t)uap->data;
660 data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
662 data = (void *)&uap->data;
664 error = copyin(uap->data, data, (u_int)size);
667 free(data, M_IOCTLOPS);
670 } else if (com & IOC_OUT) {
672 * Zero the buffer so the user always
673 * gets back something deterministic.
678 error = kern_ioctl(td, uap->fd, com, data);
680 if (error == 0 && (com & IOC_OUT))
681 error = copyout(data, uap->data, (u_int)size);
684 free(data, M_IOCTLOPS);
689 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
692 struct filedesc *fdp;
698 if ((error = fget(td, fd, &fp)) != 0)
700 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
704 fdp = td->td_proc->p_fd;
708 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
709 FILEDESC_XUNLOCK(fdp);
713 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
714 FILEDESC_XUNLOCK(fdp);
717 if ((tmp = *(int *)data))
718 atomic_set_int(&fp->f_flag, FNONBLOCK);
720 atomic_clear_int(&fp->f_flag, FNONBLOCK);
724 if ((tmp = *(int *)data))
725 atomic_set_int(&fp->f_flag, FASYNC);
727 atomic_clear_int(&fp->f_flag, FASYNC);
732 error = fo_ioctl(fp, com, data, td->td_ucred, td);
739 poll_no_poll(int events)
742 * Return true for read/write. If the user asked for something
743 * special, return POLLNVAL, so that clients have a way of
744 * determining reliably whether or not the extended
745 * functionality is present without hard-coding knowledge
746 * of specific filesystem implementations.
748 if (events & ~POLLSTANDARD)
751 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
755 pselect(struct thread *td, struct pselect_args *uap)
758 struct timeval tv, *tvp;
762 if (uap->ts != NULL) {
763 error = copyin(uap->ts, &ts, sizeof(ts));
766 TIMESPEC_TO_TIMEVAL(&tv, &ts);
770 if (uap->sm != NULL) {
771 error = copyin(uap->sm, &set, sizeof(set));
777 return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
782 kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
783 struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
788 error = kern_sigprocmask(td, SIG_SETMASK, uset,
789 &td->td_oldsigmask, 0);
792 td->td_pflags |= TDP_OLDMASK;
794 * Make sure that ast() is called on return to
795 * usermode and TDP_OLDMASK is cleared, restoring old
799 td->td_flags |= TDF_ASTPENDING;
802 error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
806 #ifndef _SYS_SYSPROTO_H_
809 fd_set *in, *ou, *ex;
814 select(struct thread *td, struct select_args *uap)
816 struct timeval tv, *tvp;
819 if (uap->tv != NULL) {
820 error = copyin(uap->tv, &tv, sizeof(tv));
827 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
832 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
833 fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
835 struct filedesc *fdp;
837 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
838 * infds with the new FD_SETSIZE of 1024, and more than enough for
839 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
842 fd_mask s_selbits[howmany(2048, NFDBITS)];
843 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
844 struct timeval atv, rtv, ttv;
846 u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
850 fdp = td->td_proc->p_fd;
851 if (nd > fdp->fd_lastfile + 1)
852 nd = fdp->fd_lastfile + 1;
855 * Allocate just enough bits for the non-null fd_sets. Use the
856 * preallocated auto buffer if possible.
858 nfdbits = roundup(nd, NFDBITS);
859 ncpbytes = nfdbits / NBBY;
860 ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
863 nbufbytes += 2 * ncpbytes;
865 nbufbytes += 2 * ncpbytes;
867 nbufbytes += 2 * ncpbytes;
868 if (nbufbytes <= sizeof s_selbits)
869 selbits = &s_selbits[0];
871 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
874 * Assign pointers into the bit buffers and fetch the input bits.
875 * Put the output buffers together so that they can be bzeroed
879 #define getbits(name, x) \
881 if (name == NULL) { \
885 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
887 sbp += ncpbytes / sizeof *sbp; \
888 error = copyin(name, ibits[x], ncpubytes); \
891 bzero((char *)ibits[x] + ncpubytes, \
892 ncpbytes - ncpubytes); \
900 #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
902 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
903 * we are running under 32-bit emulation. This should be more
906 #define swizzle_fdset(bits) \
907 if (abi_nfdbits != NFDBITS && bits != NULL) { \
909 for (i = 0; i < ncpbytes / sizeof *sbp; i++) \
910 bits[i] = (bits[i] >> 32) | (bits[i] << 32); \
913 #define swizzle_fdset(bits)
916 /* Make sure the bit order makes it through an ABI transition */
917 swizzle_fdset(ibits[0]);
918 swizzle_fdset(ibits[1]);
919 swizzle_fdset(ibits[2]);
922 bzero(selbits, nbufbytes / 2);
926 if (itimerfix(&atv)) {
930 getmicrouptime(&rtv);
931 timevaladd(&atv, &rtv);
938 /* Iterate until the timeout expires or descriptors become ready. */
940 error = selscan(td, ibits, obits, nd);
941 if (error || td->td_retval[0] != 0)
943 if (atv.tv_sec || atv.tv_usec) {
944 getmicrouptime(&rtv);
945 if (timevalcmp(&rtv, &atv, >=))
948 timevalsub(&ttv, &rtv);
949 timo = ttv.tv_sec > 24 * 60 * 60 ?
950 24 * 60 * 60 * hz : tvtohz(&ttv);
952 error = seltdwait(td, timo);
955 error = selrescan(td, ibits, obits);
956 if (error || td->td_retval[0] != 0)
962 /* select is not restarted after signals... */
963 if (error == ERESTART)
965 if (error == EWOULDBLOCK)
968 /* swizzle bit order back, if necessary */
969 swizzle_fdset(obits[0]);
970 swizzle_fdset(obits[1]);
971 swizzle_fdset(obits[2]);
974 #define putbits(name, x) \
975 if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
985 if (selbits != &s_selbits[0])
986 free(selbits, M_SELECT);
991 * Convert a select bit set to poll flags.
993 * The backend always returns POLLHUP/POLLERR if appropriate and we
994 * return this as a set bit in any set.
996 static int select_flags[3] = {
997 POLLRDNORM | POLLHUP | POLLERR,
998 POLLWRNORM | POLLHUP | POLLERR,
1003 * Compute the fo_poll flags required for a fd given by the index and
1004 * bit position in the fd_mask array.
1007 selflags(fd_mask **ibits, int idx, fd_mask bit)
1013 for (msk = 0; msk < 3; msk++) {
1014 if (ibits[msk] == NULL)
1016 if ((ibits[msk][idx] & bit) == 0)
1018 flags |= select_flags[msk];
1024 * Set the appropriate output bits given a mask of fired events and the
1025 * input bits originally requested.
1028 selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
1034 for (msk = 0; msk < 3; msk++) {
1035 if ((events & select_flags[msk]) == 0)
1037 if (ibits[msk] == NULL)
1039 if ((ibits[msk][idx] & bit) == 0)
1042 * XXX Check for a duplicate set. This can occur because a
1043 * socket calls selrecord() twice for each poll() call
1044 * resulting in two selfds per real fd. selrescan() will
1045 * call selsetbits twice as a result.
1047 if ((obits[msk][idx] & bit) != 0)
1049 obits[msk][idx] |= bit;
1057 * Traverse the list of fds attached to this thread's seltd and check for
1061 selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1063 struct filedesc *fdp;
1072 fdp = td->td_proc->p_fd;
1075 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1076 fd = (int)(uintptr_t)sfp->sf_cookie;
1078 selfdfree(stp, sfp);
1079 /* If the selinfo wasn't cleared the event didn't fire. */
1082 if ((fp = fget_unlocked(fdp, fd)) == NULL)
1085 bit = (fd_mask)1 << (fd % NFDBITS);
1086 ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1089 n += selsetbits(ibits, obits, idx, bit, ev);
1092 td->td_retval[0] = n;
1097 * Perform the initial filedescriptor scan and register ourselves with
1101 selscan(td, ibits, obits, nfd)
1103 fd_mask **ibits, **obits;
1106 struct filedesc *fdp;
1109 int ev, flags, end, fd;
1112 fdp = td->td_proc->p_fd;
1114 for (idx = 0, fd = 0; fd < nfd; idx++) {
1115 end = imin(fd + NFDBITS, nfd);
1116 for (bit = 1; fd < end; bit <<= 1, fd++) {
1117 /* Compute the list of events we're interested in. */
1118 flags = selflags(ibits, idx, bit);
1121 if ((fp = fget_unlocked(fdp, fd)) == NULL)
1123 selfdalloc(td, (void *)(uintptr_t)fd);
1124 ev = fo_poll(fp, flags, td->td_ucred, td);
1127 n += selsetbits(ibits, obits, idx, bit, ev);
1131 td->td_retval[0] = n;
1135 #ifndef _SYS_SYSPROTO_H_
1145 struct poll_args *uap;
1147 struct pollfd *bits;
1148 struct pollfd smallbits[32];
1149 struct timeval atv, rtv, ttv;
1150 int error = 0, timo;
1155 if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1157 ni = nfds * sizeof(struct pollfd);
1158 if (ni > sizeof(smallbits))
1159 bits = malloc(ni, M_TEMP, M_WAITOK);
1162 error = copyin(uap->fds, bits, ni);
1165 if (uap->timeout != INFTIM) {
1166 atv.tv_sec = uap->timeout / 1000;
1167 atv.tv_usec = (uap->timeout % 1000) * 1000;
1168 if (itimerfix(&atv)) {
1172 getmicrouptime(&rtv);
1173 timevaladd(&atv, &rtv);
1180 /* Iterate until the timeout expires or descriptors become ready. */
1182 error = pollscan(td, bits, nfds);
1183 if (error || td->td_retval[0] != 0)
1185 if (atv.tv_sec || atv.tv_usec) {
1186 getmicrouptime(&rtv);
1187 if (timevalcmp(&rtv, &atv, >=))
1190 timevalsub(&ttv, &rtv);
1191 timo = ttv.tv_sec > 24 * 60 * 60 ?
1192 24 * 60 * 60 * hz : tvtohz(&ttv);
1194 error = seltdwait(td, timo);
1197 error = pollrescan(td);
1198 if (error || td->td_retval[0] != 0)
1204 /* poll is not restarted after signals... */
1205 if (error == ERESTART)
1207 if (error == EWOULDBLOCK)
1210 error = pollout(bits, uap->fds, nfds);
1215 if (ni > sizeof(smallbits))
1221 pollrescan(struct thread *td)
1227 struct filedesc *fdp;
1233 fdp = td->td_proc->p_fd;
1235 FILEDESC_SLOCK(fdp);
1236 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1237 fd = (struct pollfd *)sfp->sf_cookie;
1239 selfdfree(stp, sfp);
1240 /* If the selinfo wasn't cleared the event didn't fire. */
1243 fp = fdp->fd_ofiles[fd->fd];
1245 fd->revents = POLLNVAL;
1250 * Note: backend also returns POLLHUP and
1251 * POLLERR if appropriate.
1253 fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1254 if (fd->revents != 0)
1257 FILEDESC_SUNLOCK(fdp);
1259 td->td_retval[0] = n;
1265 pollout(fds, ufds, nfd)
1267 struct pollfd *ufds;
1273 for (i = 0; i < nfd; i++) {
1274 error = copyout(&fds->revents, &ufds->revents,
1275 sizeof(ufds->revents));
1285 pollscan(td, fds, nfd)
1290 struct filedesc *fdp = td->td_proc->p_fd;
1295 FILEDESC_SLOCK(fdp);
1296 for (i = 0; i < nfd; i++, fds++) {
1297 if (fds->fd >= fdp->fd_nfiles) {
1298 fds->revents = POLLNVAL;
1300 } else if (fds->fd < 0) {
1303 fp = fdp->fd_ofiles[fds->fd];
1305 fds->revents = POLLNVAL;
1309 * Note: backend also returns POLLHUP and
1310 * POLLERR if appropriate.
1312 selfdalloc(td, fds);
1313 fds->revents = fo_poll(fp, fds->events,
1316 * POSIX requires POLLOUT to be never
1317 * set simultaneously with POLLHUP.
1319 if ((fds->revents & POLLHUP) != 0)
1320 fds->revents &= ~POLLOUT;
1322 if (fds->revents != 0)
1327 FILEDESC_SUNLOCK(fdp);
1328 td->td_retval[0] = n;
1333 * OpenBSD poll system call.
1335 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1337 #ifndef _SYS_SYSPROTO_H_
1338 struct openbsd_poll_args {
1345 openbsd_poll(td, uap)
1346 register struct thread *td;
1347 register struct openbsd_poll_args *uap;
1349 return (poll(td, (struct poll_args *)uap));
1353 * XXX This was created specifically to support netncp and netsmb. This
1354 * allows the caller to specify a socket to wait for events on. It returns
1355 * 0 if any events matched and an error otherwise. There is no way to
1356 * determine which events fired.
1359 selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1361 struct timeval atv, rtv, ttv;
1366 if (itimerfix(&atv))
1368 getmicrouptime(&rtv);
1369 timevaladd(&atv, &rtv);
1378 * Iterate until the timeout expires or the socket becomes ready.
1381 selfdalloc(td, NULL);
1382 error = sopoll(so, events, NULL, td);
1383 /* error here is actually the ready events. */
1386 if (atv.tv_sec || atv.tv_usec) {
1387 getmicrouptime(&rtv);
1388 if (timevalcmp(&rtv, &atv, >=)) {
1390 return (EWOULDBLOCK);
1393 timevalsub(&ttv, &rtv);
1394 timo = ttv.tv_sec > 24 * 60 * 60 ?
1395 24 * 60 * 60 * hz : tvtohz(&ttv);
1397 error = seltdwait(td, timo);
1402 /* XXX Duplicates ncp/smb behavior. */
1403 if (error == ERESTART)
1409 * Preallocate two selfds associated with 'cookie'. Some fo_poll routines
1410 * have two select sets, one for read and another for write.
1413 selfdalloc(struct thread *td, void *cookie)
1418 if (stp->st_free1 == NULL)
1419 stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1420 stp->st_free1->sf_td = stp;
1421 stp->st_free1->sf_cookie = cookie;
1422 if (stp->st_free2 == NULL)
1423 stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1424 stp->st_free2->sf_td = stp;
1425 stp->st_free2->sf_cookie = cookie;
1429 selfdfree(struct seltd *stp, struct selfd *sfp)
1431 STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1432 mtx_lock(sfp->sf_mtx);
1434 TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1435 mtx_unlock(sfp->sf_mtx);
1436 uma_zfree(selfd_zone, sfp);
1440 * Record a select request.
1443 selrecord(selector, sip)
1444 struct thread *selector;
1445 struct selinfo *sip;
1451 stp = selector->td_sel;
1453 * Don't record when doing a rescan.
1455 if (stp->st_flags & SELTD_RESCAN)
1458 * Grab one of the preallocated descriptors.
1461 if ((sfp = stp->st_free1) != NULL)
1462 stp->st_free1 = NULL;
1463 else if ((sfp = stp->st_free2) != NULL)
1464 stp->st_free2 = NULL;
1466 panic("selrecord: No free selfd on selq");
1469 mtxp = mtx_pool_find(mtxpool_select, sip);
1471 * Initialize the sfp and queue it in the thread.
1475 STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1477 * Now that we've locked the sip, check for initialization.
1480 if (sip->si_mtx == NULL) {
1482 TAILQ_INIT(&sip->si_tdlist);
1485 * Add this thread to the list of selfds listening on this selinfo.
1487 TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1488 mtx_unlock(sip->si_mtx);
1491 /* Wake up a selecting thread. */
1494 struct selinfo *sip;
1496 doselwakeup(sip, -1);
1499 /* Wake up a selecting thread, and set its priority. */
1501 selwakeuppri(sip, pri)
1502 struct selinfo *sip;
1505 doselwakeup(sip, pri);
1509 * Do a wakeup when a selectable event occurs.
1512 doselwakeup(sip, pri)
1513 struct selinfo *sip;
1520 /* If it's not initialized there can't be any waiters. */
1521 if (sip->si_mtx == NULL)
1524 * Locking the selinfo locks all selfds associated with it.
1526 mtx_lock(sip->si_mtx);
1527 TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1529 * Once we remove this sfp from the list and clear the
1530 * sf_si seltdclear will know to ignore this si.
1532 TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1535 mtx_lock(&stp->st_mtx);
1536 stp->st_flags |= SELTD_PENDING;
1537 cv_broadcastpri(&stp->st_wait, pri);
1538 mtx_unlock(&stp->st_mtx);
1540 mtx_unlock(sip->si_mtx);
1544 seltdinit(struct thread *td)
1548 if ((stp = td->td_sel) != NULL)
1550 td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1551 mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1552 cv_init(&stp->st_wait, "select");
1555 STAILQ_INIT(&stp->st_selq);
1559 seltdwait(struct thread *td, int timo)
1566 * An event of interest may occur while we do not hold the seltd
1567 * locked so check the pending flag before we sleep.
1569 mtx_lock(&stp->st_mtx);
1571 * Any further calls to selrecord will be a rescan.
1573 stp->st_flags |= SELTD_RESCAN;
1574 if (stp->st_flags & SELTD_PENDING) {
1575 mtx_unlock(&stp->st_mtx);
1579 error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1581 error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1582 mtx_unlock(&stp->st_mtx);
1588 seltdfini(struct thread *td)
1596 uma_zfree(selfd_zone, stp->st_free1);
1598 uma_zfree(selfd_zone, stp->st_free2);
1600 free(stp, M_SELECT);
1604 * Remove the references to the thread from all of the objects we were
1608 seltdclear(struct thread *td)
1615 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1616 selfdfree(stp, sfp);
1620 static void selectinit(void *);
1621 SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1623 selectinit(void *dummy __unused)
1626 selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1627 NULL, NULL, UMA_ALIGN_PTR, 0);
1628 mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);