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
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/filio.h>
49 #include <sys/fcntl.h>
52 #include <sys/signalvar.h>
53 #include <sys/socketvar.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/selinfo.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
64 #include <sys/condvar.h>
66 #include <sys/ktrace.h>
69 #include <vm/vm_page.h>
71 #include <machine/limits.h>
73 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
74 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
75 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
77 static int pollscan(struct thread *, struct pollfd *, u_int);
78 static int selscan(struct thread *, fd_mask **, fd_mask **, int);
79 static int dofileread(struct thread *, struct file *, int, void *,
81 static int dofilewrite(struct thread *, struct file *, int,
82 const void *, size_t, off_t, int);
87 #ifndef _SYS_SYSPROTO_H_
100 struct read_args *uap;
106 if ((error = fget_read(td, uap->fd, &fp)) == 0) {
107 error = dofileread(td, fp, uap->fd, uap->buf,
108 uap->nbyte, (off_t)-1, 0);
118 #ifndef _SYS_SYSPROTO_H_
133 struct pread_args *uap;
138 if ((error = fget_read(td, uap->fd, &fp)) != 0)
141 if (fp->f_type != DTYPE_VNODE) {
144 error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte,
145 uap->offset, FOF_OFFSET);
153 * Code common for read and pread
156 dofileread(td, fp, fd, buf, nbyte, offset, flags)
173 aiov.iov_base = (caddr_t)buf;
174 aiov.iov_len = nbyte;
175 auio.uio_iov = &aiov;
177 auio.uio_offset = offset;
180 auio.uio_resid = nbyte;
181 auio.uio_rw = UIO_READ;
182 auio.uio_segflg = UIO_USERSPACE;
186 * if tracing, save a copy of iovec
188 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
196 if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) {
197 if (auio.uio_resid != cnt && (error == ERESTART ||
198 error == EINTR || error == EWOULDBLOCK))
201 cnt -= auio.uio_resid;
203 if (didktr && error == 0) {
204 ktruio.uio_iov = &ktriov;
205 ktruio.uio_resid = cnt;
206 ktrgenio(td->td_proc->p_tracep, fd, UIO_READ, &ktruio, error);
209 td->td_retval[0] = cnt;
214 * Scatter read system call.
216 #ifndef _SYS_SYSPROTO_H_
229 struct readv_args *uap;
234 struct iovec *needfree;
235 struct iovec aiov[UIO_SMALLIOV];
236 long i, cnt, error = 0;
239 struct iovec *ktriov = NULL;
244 if ((error = fget_read(td, uap->fd, &fp)) != 0)
246 /* note: can't use iovlen until iovcnt is validated */
247 iovlen = uap->iovcnt * sizeof (struct iovec);
248 if (uap->iovcnt > UIO_SMALLIOV) {
249 if (uap->iovcnt > UIO_MAXIOV) {
253 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
260 auio.uio_iovcnt = uap->iovcnt;
261 auio.uio_rw = UIO_READ;
262 auio.uio_segflg = UIO_USERSPACE;
264 auio.uio_offset = -1;
265 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
268 for (i = 0; i < uap->iovcnt; i++) {
269 if (iov->iov_len > INT_MAX - auio.uio_resid) {
273 auio.uio_resid += iov->iov_len;
278 * if tracing, save a copy of iovec
280 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
281 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
282 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
286 cnt = auio.uio_resid;
287 if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) {
288 if (auio.uio_resid != cnt && (error == ERESTART ||
289 error == EINTR || error == EWOULDBLOCK))
292 cnt -= auio.uio_resid;
294 if (ktriov != NULL) {
296 ktruio.uio_iov = ktriov;
297 ktruio.uio_resid = cnt;
298 ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio,
301 FREE(ktriov, M_TEMP);
304 td->td_retval[0] = cnt;
308 FREE(needfree, M_IOV);
317 #ifndef _SYS_SYSPROTO_H_
330 struct write_args *uap;
336 if ((error = fget_write(td, uap->fd, &fp)) == 0) {
337 error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte,
341 error = EBADF; /* XXX this can't be right */
350 #ifndef _SYS_SYSPROTO_H_
365 struct pwrite_args *uap;
370 if ((error = fget_write(td, uap->fd, &fp)) == 0) {
372 if (fp->f_type == DTYPE_VNODE) {
373 error = dofilewrite(td, fp, uap->fd, uap->buf,
374 uap->nbyte, uap->offset, FOF_OFFSET);
381 error = EBADF; /* this can't be right */
387 dofilewrite(td, fp, fd, buf, nbyte, offset, flags)
404 aiov.iov_base = (void *)(uintptr_t)buf;
405 aiov.iov_len = nbyte;
406 auio.uio_iov = &aiov;
408 auio.uio_offset = offset;
411 auio.uio_resid = nbyte;
412 auio.uio_rw = UIO_WRITE;
413 auio.uio_segflg = UIO_USERSPACE;
417 * if tracing, save a copy of iovec and uio
419 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
426 if (fp->f_type == DTYPE_VNODE)
428 if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) {
429 if (auio.uio_resid != cnt && (error == ERESTART ||
430 error == EINTR || error == EWOULDBLOCK))
432 if (error == EPIPE) {
433 PROC_LOCK(td->td_proc);
434 psignal(td->td_proc, SIGPIPE);
435 PROC_UNLOCK(td->td_proc);
438 cnt -= auio.uio_resid;
440 if (didktr && error == 0) {
441 ktruio.uio_iov = &ktriov;
442 ktruio.uio_resid = cnt;
443 ktrgenio(td->td_proc->p_tracep, fd, UIO_WRITE, &ktruio, error);
446 td->td_retval[0] = cnt;
451 * Gather write system call
453 #ifndef _SYS_SYSPROTO_H_
466 register struct writev_args *uap;
470 register struct iovec *iov;
471 struct iovec *needfree;
472 struct iovec aiov[UIO_SMALLIOV];
473 long i, cnt, error = 0;
476 struct iovec *ktriov = NULL;
481 if ((error = fget_write(td, uap->fd, &fp)) != 0) {
485 /* note: can't use iovlen until iovcnt is validated */
486 iovlen = uap->iovcnt * sizeof (struct iovec);
487 if (uap->iovcnt > UIO_SMALLIOV) {
488 if (uap->iovcnt > UIO_MAXIOV) {
493 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
500 auio.uio_iovcnt = uap->iovcnt;
501 auio.uio_rw = UIO_WRITE;
502 auio.uio_segflg = UIO_USERSPACE;
504 auio.uio_offset = -1;
505 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
508 for (i = 0; i < uap->iovcnt; i++) {
509 if (iov->iov_len > INT_MAX - auio.uio_resid) {
513 auio.uio_resid += iov->iov_len;
518 * if tracing, save a copy of iovec and uio
520 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
521 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
522 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
526 cnt = auio.uio_resid;
527 if (fp->f_type == DTYPE_VNODE)
529 if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) {
530 if (auio.uio_resid != cnt && (error == ERESTART ||
531 error == EINTR || error == EWOULDBLOCK))
533 if (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 (ktriov != NULL) {
543 ktruio.uio_iov = ktriov;
544 ktruio.uio_resid = cnt;
545 ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_WRITE, &ktruio,
548 FREE(ktriov, M_TEMP);
551 td->td_retval[0] = cnt;
555 FREE(needfree, M_IOV);
564 #ifndef _SYS_SYSPROTO_H_
578 register struct ioctl_args *uap;
581 register struct filedesc *fdp;
587 #define STK_PARAMS 128
589 char stkbuf[STK_PARAMS];
593 if ((error = fget(td, uap->fd, &fp)) != 0)
596 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
601 fdp = td->td_proc->p_fd;
602 switch (com = uap->com) {
605 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
606 FILEDESC_UNLOCK(fdp);
612 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
613 FILEDESC_UNLOCK(fdp);
620 * Interpret high order word to find amount of data to be
621 * copied to/from the user's address space.
623 size = IOCPARM_LEN(com);
624 if (size > IOCPARM_MAX) {
631 if (size > sizeof (ubuf.stkbuf)) {
632 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
639 error = copyin(uap->data, data, (u_int)size);
642 free(memp, M_IOCTLOPS);
647 *(caddr_t *)data = uap->data;
649 } else if ((com&IOC_OUT) && size) {
651 * Zero the buffer so the user always
652 * gets back something deterministic.
655 } else if (com&IOC_VOID) {
656 *(caddr_t *)data = uap->data;
663 if ((tmp = *(int *)data))
664 fp->f_flag |= FNONBLOCK;
666 fp->f_flag &= ~FNONBLOCK;
668 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
673 if ((tmp = *(int *)data))
674 fp->f_flag |= FASYNC;
676 fp->f_flag &= ~FASYNC;
678 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
682 error = fo_ioctl(fp, com, data, td);
684 * Copy any data to user, size was
685 * already set and checked above.
687 if (error == 0 && (com&IOC_OUT) && size)
688 error = copyout(data, uap->data, (u_int)size);
692 free(memp, M_IOCTLOPS);
700 * sellock and selwait are initialized in selectinit() via SYSINIT.
704 int nselcoll; /* Select collisions since boot */
705 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
708 * Select system call.
710 #ifndef _SYS_SYSPROTO_H_
713 fd_set *in, *ou, *ex;
722 register struct thread *td;
723 register struct select_args *uap;
725 struct filedesc *fdp;
727 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
728 * infds with the new FD_SETSIZE of 1024, and more than enough for
729 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
732 fd_mask s_selbits[howmany(2048, NFDBITS)];
733 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
734 struct timeval atv, rtv, ttv;
735 int ncoll, error, timo;
736 u_int nbufbytes, ncpbytes, nfdbits;
740 fdp = td->td_proc->p_fd;
744 if (uap->nd > td->td_proc->p_fd->fd_nfiles)
745 uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */
746 FILEDESC_UNLOCK(fdp);
749 * Allocate just enough bits for the non-null fd_sets. Use the
750 * preallocated auto buffer if possible.
752 nfdbits = roundup(uap->nd, NFDBITS);
753 ncpbytes = nfdbits / NBBY;
756 nbufbytes += 2 * ncpbytes;
758 nbufbytes += 2 * ncpbytes;
760 nbufbytes += 2 * ncpbytes;
761 if (nbufbytes <= sizeof s_selbits)
762 selbits = &s_selbits[0];
764 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
767 * Assign pointers into the bit buffers and fetch the input bits.
768 * Put the output buffers together so that they can be bzeroed
772 #define getbits(name, x) \
774 if (uap->name == NULL) \
777 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
779 sbp += ncpbytes / sizeof *sbp; \
780 error = copyin(uap->name, ibits[x], ncpbytes); \
782 goto done_nosellock; \
790 bzero(selbits, nbufbytes / 2);
793 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
797 if (itimerfix(&atv)) {
801 getmicrouptime(&rtv);
802 timevaladd(&atv, &rtv);
811 mtx_lock_spin(&sched_lock);
812 td->td_flags |= TDF_SELECT;
813 mtx_unlock_spin(&sched_lock);
814 mtx_unlock(&sellock);
816 /* XXX Is there a better place for this? */
817 TAILQ_INIT(&td->td_selq);
818 error = selscan(td, ibits, obits, uap->nd);
820 if (error || td->td_retval[0])
822 if (atv.tv_sec || atv.tv_usec) {
823 getmicrouptime(&rtv);
824 if (timevalcmp(&rtv, &atv, >=))
827 timevalsub(&ttv, &rtv);
828 timo = ttv.tv_sec > 24 * 60 * 60 ?
829 24 * 60 * 60 * hz : tvtohz(&ttv);
833 * An event of interest may occur while we do not hold
834 * sellock, so check TDF_SELECT and the number of
835 * collisions and rescan the file descriptors if
838 mtx_lock_spin(&sched_lock);
839 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
840 mtx_unlock_spin(&sched_lock);
843 mtx_unlock_spin(&sched_lock);
846 error = cv_timedwait_sig(&selwait, &sellock, timo);
848 error = cv_wait_sig(&selwait, &sellock);
854 clear_selinfo_list(td);
855 mtx_lock_spin(&sched_lock);
856 td->td_flags &= ~TDF_SELECT;
857 mtx_unlock_spin(&sched_lock);
858 mtx_unlock(&sellock);
861 /* select is not restarted after signals... */
862 if (error == ERESTART)
864 if (error == EWOULDBLOCK)
866 #define putbits(name, x) \
867 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
877 if (selbits != &s_selbits[0])
878 free(selbits, M_SELECT);
885 selscan(td, ibits, obits, nfd)
887 fd_mask **ibits, **obits;
894 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
895 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
896 struct filedesc *fdp = td->td_proc->p_fd;
899 for (msk = 0; msk < 3; msk++) {
900 if (ibits[msk] == NULL)
902 for (i = 0; i < nfd; i += NFDBITS) {
903 bits = ibits[msk][i/NFDBITS];
904 /* ffs(int mask) not portable, fd_mask is long */
905 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
908 if ((fp = fget_locked(fdp, fd)) == NULL) {
909 FILEDESC_UNLOCK(fdp);
912 if (fo_poll(fp, flag[msk], fp->f_cred, td)) {
913 obits[msk][(fd)/NFDBITS] |=
914 ((fd_mask)1 << ((fd) % NFDBITS));
920 FILEDESC_UNLOCK(fdp);
921 td->td_retval[0] = n;
928 #ifndef _SYS_SYSPROTO_H_
941 struct poll_args *uap;
944 char smallbits[32 * sizeof(struct pollfd)];
945 struct timeval atv, rtv, ttv;
946 int ncoll, error = 0, timo;
950 nfds = SCARG(uap, nfds);
954 * This is kinda bogus. We have fd limits, but that is not
955 * really related to the size of the pollfd array. Make sure
956 * we let the process use at least FD_SETSIZE entries and at
957 * least enough for the current limits. We want to be reasonably
958 * safe, but not overly restrictive.
960 if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) &&
961 (nfds > FD_SETSIZE)) {
965 ni = nfds * sizeof(struct pollfd);
966 if (ni > sizeof(smallbits))
967 bits = malloc(ni, M_TEMP, M_WAITOK);
970 error = copyin(SCARG(uap, fds), bits, ni);
973 if (SCARG(uap, timeout) != INFTIM) {
974 atv.tv_sec = SCARG(uap, timeout) / 1000;
975 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
976 if (itimerfix(&atv)) {
980 getmicrouptime(&rtv);
981 timevaladd(&atv, &rtv);
990 mtx_lock_spin(&sched_lock);
991 td->td_flags |= TDF_SELECT;
992 mtx_unlock_spin(&sched_lock);
993 mtx_unlock(&sellock);
995 /* XXX Is there a better place for this? */
996 TAILQ_INIT(&td->td_selq);
997 error = pollscan(td, (struct pollfd *)bits, nfds);
999 if (error || td->td_retval[0])
1001 if (atv.tv_sec || atv.tv_usec) {
1002 getmicrouptime(&rtv);
1003 if (timevalcmp(&rtv, &atv, >=))
1006 timevalsub(&ttv, &rtv);
1007 timo = ttv.tv_sec > 24 * 60 * 60 ?
1008 24 * 60 * 60 * hz : tvtohz(&ttv);
1011 * An event of interest may occur while we do not hold
1012 * sellock, so check TDF_SELECT and the number of collisions
1013 * and rescan the file descriptors if necessary.
1015 mtx_lock_spin(&sched_lock);
1016 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
1017 mtx_unlock_spin(&sched_lock);
1020 mtx_unlock_spin(&sched_lock);
1023 error = cv_timedwait_sig(&selwait, &sellock, timo);
1025 error = cv_wait_sig(&selwait, &sellock);
1031 clear_selinfo_list(td);
1032 mtx_lock_spin(&sched_lock);
1033 td->td_flags &= ~TDF_SELECT;
1034 mtx_unlock_spin(&sched_lock);
1035 mtx_unlock(&sellock);
1038 /* poll is not restarted after signals... */
1039 if (error == ERESTART)
1041 if (error == EWOULDBLOCK)
1044 error = copyout(bits, SCARG(uap, fds), ni);
1049 if (ni > sizeof(smallbits))
1057 pollscan(td, fds, nfd)
1062 register struct filedesc *fdp = td->td_proc->p_fd;
1068 for (i = 0; i < nfd; i++, fds++) {
1069 if (fds->fd >= fdp->fd_nfiles) {
1070 fds->revents = POLLNVAL;
1072 } else if (fds->fd < 0) {
1075 fp = fdp->fd_ofiles[fds->fd];
1077 fds->revents = POLLNVAL;
1081 * Note: backend also returns POLLHUP and
1082 * POLLERR if appropriate.
1084 fds->revents = fo_poll(fp, fds->events,
1086 if (fds->revents != 0)
1091 FILEDESC_UNLOCK(fdp);
1092 td->td_retval[0] = n;
1097 * OpenBSD poll system call.
1098 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1100 #ifndef _SYS_SYSPROTO_H_
1101 struct openbsd_poll_args {
1111 openbsd_poll(td, uap)
1112 register struct thread *td;
1113 register struct openbsd_poll_args *uap;
1115 return (poll(td, (struct poll_args *)uap));
1119 * Remove the references to the thread from all of the objects
1122 * This code assumes that the underlying owner of the selinfo
1123 * structure will hold sellock before it changes it, and that
1124 * it will unlink itself from our list if it goes away.
1127 clear_selinfo_list(td)
1132 mtx_assert(&sellock, MA_OWNED);
1133 TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
1134 si->si_thread = NULL;
1135 TAILQ_INIT(&td->td_selq);
1140 seltrue(dev, events, td)
1146 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1150 * Record a select request.
1153 selrecord(selector, sip)
1154 struct thread *selector;
1155 struct selinfo *sip;
1160 * If the thread is NULL then take ownership of selinfo
1161 * however if the thread is not NULL and the thread points to
1162 * someone else, then we have a collision, otherwise leave it alone
1163 * as we've owned it in a previous selrecord on this selinfo.
1165 if (sip->si_thread == NULL) {
1166 sip->si_thread = selector;
1167 TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
1168 } else if (sip->si_thread != selector) {
1169 sip->si_flags |= SI_COLL;
1172 mtx_unlock(&sellock);
1176 * Do a wakeup when a selectable event occurs.
1180 struct selinfo *sip;
1185 td = sip->si_thread;
1186 if ((sip->si_flags & SI_COLL) != 0) {
1188 sip->si_flags &= ~SI_COLL;
1189 cv_broadcast(&selwait);
1192 mtx_unlock(&sellock);
1195 TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
1196 sip->si_thread = NULL;
1197 mtx_lock_spin(&sched_lock);
1198 if (td->td_wchan == (caddr_t)&selwait) {
1199 if (td->td_proc->p_stat == SSLEEP)
1202 cv_waitq_remove(td);
1204 td->td_flags &= ~TDF_SELECT;
1205 mtx_unlock_spin(&sched_lock);
1206 mtx_unlock(&sellock);
1209 static void selectinit __P((void *));
1210 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1217 cv_init(&selwait, "select");
1218 mtx_init(&sellock, "sellck", MTX_DEF);