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;
105 if ((error = fget_read(td, uap->fd, &fp)) == 0) {
106 error = dofileread(td, fp, uap->fd, uap->buf,
107 uap->nbyte, (off_t)-1, 0);
116 #ifndef _SYS_SYSPROTO_H_
131 struct pread_args *uap;
136 if ((error = fget_read(td, uap->fd, &fp)) != 0)
138 if (fp->f_type != DTYPE_VNODE) {
141 error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte,
142 uap->offset, FOF_OFFSET);
149 * Code common for read and pread
152 dofileread(td, fp, fd, buf, nbyte, offset, flags)
169 aiov.iov_base = (caddr_t)buf;
170 aiov.iov_len = nbyte;
171 auio.uio_iov = &aiov;
173 auio.uio_offset = offset;
176 auio.uio_resid = nbyte;
177 auio.uio_rw = UIO_READ;
178 auio.uio_segflg = UIO_USERSPACE;
182 * if tracing, save a copy of iovec
184 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
192 if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) {
193 if (auio.uio_resid != cnt && (error == ERESTART ||
194 error == EINTR || error == EWOULDBLOCK))
197 cnt -= auio.uio_resid;
199 if (didktr && error == 0) {
200 ktruio.uio_iov = &ktriov;
201 ktruio.uio_resid = cnt;
202 ktrgenio(td->td_proc->p_tracep, fd, UIO_READ, &ktruio, error);
205 td->td_retval[0] = cnt;
210 * Scatter read system call.
212 #ifndef _SYS_SYSPROTO_H_
225 struct readv_args *uap;
230 struct iovec *needfree;
231 struct iovec aiov[UIO_SMALLIOV];
232 long i, cnt, error = 0;
235 struct iovec *ktriov = NULL;
240 if ((error = fget_read(td, uap->fd, &fp)) != 0)
242 /* note: can't use iovlen until iovcnt is validated */
243 iovlen = uap->iovcnt * sizeof (struct iovec);
244 if (uap->iovcnt > UIO_SMALLIOV) {
245 if (uap->iovcnt > UIO_MAXIOV) {
249 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
256 auio.uio_iovcnt = uap->iovcnt;
257 auio.uio_rw = UIO_READ;
258 auio.uio_segflg = UIO_USERSPACE;
260 auio.uio_offset = -1;
261 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
264 for (i = 0; i < uap->iovcnt; i++) {
265 if (iov->iov_len > INT_MAX - auio.uio_resid) {
269 auio.uio_resid += iov->iov_len;
274 * if tracing, save a copy of iovec
276 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
277 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
278 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
282 cnt = auio.uio_resid;
283 if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) {
284 if (auio.uio_resid != cnt && (error == ERESTART ||
285 error == EINTR || error == EWOULDBLOCK))
288 cnt -= auio.uio_resid;
290 if (ktriov != NULL) {
292 ktruio.uio_iov = ktriov;
293 ktruio.uio_resid = cnt;
294 ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio,
297 FREE(ktriov, M_TEMP);
300 td->td_retval[0] = cnt;
304 FREE(needfree, M_IOV);
313 #ifndef _SYS_SYSPROTO_H_
326 struct write_args *uap;
331 if ((error = fget_write(td, uap->fd, &fp)) == 0) {
332 error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte,
336 error = EBADF; /* XXX this can't be right */
344 #ifndef _SYS_SYSPROTO_H_
359 struct pwrite_args *uap;
364 if ((error = fget_write(td, uap->fd, &fp)) == 0) {
365 if (fp->f_type == DTYPE_VNODE) {
366 error = dofilewrite(td, fp, uap->fd, uap->buf,
367 uap->nbyte, uap->offset, FOF_OFFSET);
373 error = EBADF; /* this can't be right */
379 dofilewrite(td, fp, fd, buf, nbyte, offset, flags)
396 aiov.iov_base = (void *)(uintptr_t)buf;
397 aiov.iov_len = nbyte;
398 auio.uio_iov = &aiov;
400 auio.uio_offset = offset;
403 auio.uio_resid = nbyte;
404 auio.uio_rw = UIO_WRITE;
405 auio.uio_segflg = UIO_USERSPACE;
409 * if tracing, save a copy of iovec and uio
411 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
418 if (fp->f_type == DTYPE_VNODE)
420 if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) {
421 if (auio.uio_resid != cnt && (error == ERESTART ||
422 error == EINTR || error == EWOULDBLOCK))
424 if (error == EPIPE) {
425 PROC_LOCK(td->td_proc);
426 psignal(td->td_proc, SIGPIPE);
427 PROC_UNLOCK(td->td_proc);
430 cnt -= auio.uio_resid;
432 if (didktr && error == 0) {
433 ktruio.uio_iov = &ktriov;
434 ktruio.uio_resid = cnt;
435 ktrgenio(td->td_proc->p_tracep, fd, UIO_WRITE, &ktruio, error);
438 td->td_retval[0] = cnt;
443 * Gather write system call
445 #ifndef _SYS_SYSPROTO_H_
458 register struct writev_args *uap;
462 register struct iovec *iov;
463 struct iovec *needfree;
464 struct iovec aiov[UIO_SMALLIOV];
465 long i, cnt, error = 0;
468 struct iovec *ktriov = NULL;
473 if ((error = fget_write(td, uap->fd, &fp)) != 0) {
477 /* note: can't use iovlen until iovcnt is validated */
478 iovlen = uap->iovcnt * sizeof (struct iovec);
479 if (uap->iovcnt > UIO_SMALLIOV) {
480 if (uap->iovcnt > UIO_MAXIOV) {
485 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
492 auio.uio_iovcnt = uap->iovcnt;
493 auio.uio_rw = UIO_WRITE;
494 auio.uio_segflg = UIO_USERSPACE;
496 auio.uio_offset = -1;
497 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
500 for (i = 0; i < uap->iovcnt; i++) {
501 if (iov->iov_len > INT_MAX - auio.uio_resid) {
505 auio.uio_resid += iov->iov_len;
510 * if tracing, save a copy of iovec and uio
512 if (KTRPOINT(td->td_proc, KTR_GENIO)) {
513 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
514 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
518 cnt = auio.uio_resid;
519 if (fp->f_type == DTYPE_VNODE)
521 if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) {
522 if (auio.uio_resid != cnt && (error == ERESTART ||
523 error == EINTR || error == EWOULDBLOCK))
525 if (error == EPIPE) {
526 PROC_LOCK(td->td_proc);
527 psignal(td->td_proc, SIGPIPE);
528 PROC_UNLOCK(td->td_proc);
531 cnt -= auio.uio_resid;
533 if (ktriov != NULL) {
535 ktruio.uio_iov = ktriov;
536 ktruio.uio_resid = cnt;
537 ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_WRITE, &ktruio,
540 FREE(ktriov, M_TEMP);
543 td->td_retval[0] = cnt;
547 FREE(needfree, M_IOV);
556 #ifndef _SYS_SYSPROTO_H_
570 register struct ioctl_args *uap;
573 register struct filedesc *fdp;
579 #define STK_PARAMS 128
581 char stkbuf[STK_PARAMS];
585 if ((error = fget(td, uap->fd, &fp)) != 0)
588 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
593 fdp = td->td_proc->p_fd;
594 switch (com = uap->com) {
597 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
598 FILEDESC_UNLOCK(fdp);
604 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
605 FILEDESC_UNLOCK(fdp);
612 * Interpret high order word to find amount of data to be
613 * copied to/from the user's address space.
615 size = IOCPARM_LEN(com);
616 if (size > IOCPARM_MAX) {
623 if (size > sizeof (ubuf.stkbuf)) {
624 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
631 error = copyin(uap->data, data, (u_int)size);
634 free(memp, M_IOCTLOPS);
639 *(caddr_t *)data = uap->data;
641 } else if ((com&IOC_OUT) && size) {
643 * Zero the buffer so the user always
644 * gets back something deterministic.
647 } else if (com&IOC_VOID) {
648 *(caddr_t *)data = uap->data;
655 if ((tmp = *(int *)data))
656 fp->f_flag |= FNONBLOCK;
658 fp->f_flag &= ~FNONBLOCK;
660 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
665 if ((tmp = *(int *)data))
666 fp->f_flag |= FASYNC;
668 fp->f_flag &= ~FASYNC;
670 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
674 error = fo_ioctl(fp, com, data, td);
676 * Copy any data to user, size was
677 * already set and checked above.
679 if (error == 0 && (com&IOC_OUT) && size)
680 error = copyout(data, uap->data, (u_int)size);
684 free(memp, M_IOCTLOPS);
692 * sellock and selwait are initialized in selectinit() via SYSINIT.
696 int nselcoll; /* Select collisions since boot */
697 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
700 * Select system call.
702 #ifndef _SYS_SYSPROTO_H_
705 fd_set *in, *ou, *ex;
714 register struct thread *td;
715 register struct select_args *uap;
717 struct filedesc *fdp;
719 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
720 * infds with the new FD_SETSIZE of 1024, and more than enough for
721 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
724 fd_mask s_selbits[howmany(2048, NFDBITS)];
725 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
726 struct timeval atv, rtv, ttv;
727 int ncoll, error, timo;
728 u_int nbufbytes, ncpbytes, nfdbits;
732 fdp = td->td_proc->p_fd;
736 if (uap->nd > td->td_proc->p_fd->fd_nfiles)
737 uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */
738 FILEDESC_UNLOCK(fdp);
741 * Allocate just enough bits for the non-null fd_sets. Use the
742 * preallocated auto buffer if possible.
744 nfdbits = roundup(uap->nd, NFDBITS);
745 ncpbytes = nfdbits / NBBY;
748 nbufbytes += 2 * ncpbytes;
750 nbufbytes += 2 * ncpbytes;
752 nbufbytes += 2 * ncpbytes;
753 if (nbufbytes <= sizeof s_selbits)
754 selbits = &s_selbits[0];
756 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
759 * Assign pointers into the bit buffers and fetch the input bits.
760 * Put the output buffers together so that they can be bzeroed
764 #define getbits(name, x) \
766 if (uap->name == NULL) \
769 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
771 sbp += ncpbytes / sizeof *sbp; \
772 error = copyin(uap->name, ibits[x], ncpbytes); \
774 goto done_nosellock; \
782 bzero(selbits, nbufbytes / 2);
785 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
789 if (itimerfix(&atv)) {
793 getmicrouptime(&rtv);
794 timevaladd(&atv, &rtv);
803 mtx_lock_spin(&sched_lock);
804 td->td_flags |= TDF_SELECT;
805 mtx_unlock_spin(&sched_lock);
806 mtx_unlock(&sellock);
808 /* XXX Is there a better place for this? */
809 TAILQ_INIT(&td->td_selq);
810 error = selscan(td, ibits, obits, uap->nd);
812 if (error || td->td_retval[0])
814 if (atv.tv_sec || atv.tv_usec) {
815 getmicrouptime(&rtv);
816 if (timevalcmp(&rtv, &atv, >=))
819 timevalsub(&ttv, &rtv);
820 timo = ttv.tv_sec > 24 * 60 * 60 ?
821 24 * 60 * 60 * hz : tvtohz(&ttv);
825 * An event of interest may occur while we do not hold
826 * sellock, so check TDF_SELECT and the number of
827 * collisions and rescan the file descriptors if
830 mtx_lock_spin(&sched_lock);
831 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
832 mtx_unlock_spin(&sched_lock);
835 mtx_unlock_spin(&sched_lock);
838 error = cv_timedwait_sig(&selwait, &sellock, timo);
840 error = cv_wait_sig(&selwait, &sellock);
846 clear_selinfo_list(td);
847 mtx_lock_spin(&sched_lock);
848 td->td_flags &= ~TDF_SELECT;
849 mtx_unlock_spin(&sched_lock);
850 mtx_unlock(&sellock);
853 /* select is not restarted after signals... */
854 if (error == ERESTART)
856 if (error == EWOULDBLOCK)
858 #define putbits(name, x) \
859 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
869 if (selbits != &s_selbits[0])
870 free(selbits, M_SELECT);
877 selscan(td, ibits, obits, nfd)
879 fd_mask **ibits, **obits;
886 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
887 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
888 struct filedesc *fdp = td->td_proc->p_fd;
891 for (msk = 0; msk < 3; msk++) {
892 if (ibits[msk] == NULL)
894 for (i = 0; i < nfd; i += NFDBITS) {
895 bits = ibits[msk][i/NFDBITS];
896 /* ffs(int mask) not portable, fd_mask is long */
897 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
900 if ((fp = fget_locked(fdp, fd)) == NULL) {
901 FILEDESC_UNLOCK(fdp);
904 if (fo_poll(fp, flag[msk], fp->f_cred, td)) {
905 obits[msk][(fd)/NFDBITS] |=
906 ((fd_mask)1 << ((fd) % NFDBITS));
912 FILEDESC_UNLOCK(fdp);
913 td->td_retval[0] = n;
920 #ifndef _SYS_SYSPROTO_H_
933 struct poll_args *uap;
936 char smallbits[32 * sizeof(struct pollfd)];
937 struct timeval atv, rtv, ttv;
938 int ncoll, error = 0, timo;
942 nfds = SCARG(uap, nfds);
946 * This is kinda bogus. We have fd limits, but that is not
947 * really related to the size of the pollfd array. Make sure
948 * we let the process use at least FD_SETSIZE entries and at
949 * least enough for the current limits. We want to be reasonably
950 * safe, but not overly restrictive.
952 if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) &&
953 (nfds > FD_SETSIZE)) {
957 ni = nfds * sizeof(struct pollfd);
958 if (ni > sizeof(smallbits))
959 bits = malloc(ni, M_TEMP, M_WAITOK);
962 error = copyin(SCARG(uap, fds), bits, ni);
965 if (SCARG(uap, timeout) != INFTIM) {
966 atv.tv_sec = SCARG(uap, timeout) / 1000;
967 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
968 if (itimerfix(&atv)) {
972 getmicrouptime(&rtv);
973 timevaladd(&atv, &rtv);
982 mtx_lock_spin(&sched_lock);
983 td->td_flags |= TDF_SELECT;
984 mtx_unlock_spin(&sched_lock);
985 mtx_unlock(&sellock);
987 /* XXX Is there a better place for this? */
988 TAILQ_INIT(&td->td_selq);
989 error = pollscan(td, (struct pollfd *)bits, nfds);
991 if (error || td->td_retval[0])
993 if (atv.tv_sec || atv.tv_usec) {
994 getmicrouptime(&rtv);
995 if (timevalcmp(&rtv, &atv, >=))
998 timevalsub(&ttv, &rtv);
999 timo = ttv.tv_sec > 24 * 60 * 60 ?
1000 24 * 60 * 60 * hz : tvtohz(&ttv);
1003 * An event of interest may occur while we do not hold
1004 * sellock, so check TDF_SELECT and the number of collisions
1005 * and rescan the file descriptors if necessary.
1007 mtx_lock_spin(&sched_lock);
1008 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
1009 mtx_unlock_spin(&sched_lock);
1012 mtx_unlock_spin(&sched_lock);
1015 error = cv_timedwait_sig(&selwait, &sellock, timo);
1017 error = cv_wait_sig(&selwait, &sellock);
1023 clear_selinfo_list(td);
1024 mtx_lock_spin(&sched_lock);
1025 td->td_flags &= ~TDF_SELECT;
1026 mtx_unlock_spin(&sched_lock);
1027 mtx_unlock(&sellock);
1030 /* poll is not restarted after signals... */
1031 if (error == ERESTART)
1033 if (error == EWOULDBLOCK)
1036 error = copyout(bits, SCARG(uap, fds), ni);
1041 if (ni > sizeof(smallbits))
1049 pollscan(td, fds, nfd)
1054 register struct filedesc *fdp = td->td_proc->p_fd;
1060 for (i = 0; i < nfd; i++, fds++) {
1061 if (fds->fd >= fdp->fd_nfiles) {
1062 fds->revents = POLLNVAL;
1064 } else if (fds->fd < 0) {
1067 fp = fdp->fd_ofiles[fds->fd];
1069 fds->revents = POLLNVAL;
1073 * Note: backend also returns POLLHUP and
1074 * POLLERR if appropriate.
1076 fds->revents = fo_poll(fp, fds->events,
1078 if (fds->revents != 0)
1083 FILEDESC_UNLOCK(fdp);
1084 td->td_retval[0] = n;
1089 * OpenBSD poll system call.
1090 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1092 #ifndef _SYS_SYSPROTO_H_
1093 struct openbsd_poll_args {
1103 openbsd_poll(td, uap)
1104 register struct thread *td;
1105 register struct openbsd_poll_args *uap;
1107 return (poll(td, (struct poll_args *)uap));
1111 * Remove the references to the thread from all of the objects
1114 * This code assumes that the underlying owner of the selinfo
1115 * structure will hold sellock before it changes it, and that
1116 * it will unlink itself from our list if it goes away.
1119 clear_selinfo_list(td)
1124 mtx_assert(&sellock, MA_OWNED);
1125 TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
1126 si->si_thread = NULL;
1127 TAILQ_INIT(&td->td_selq);
1132 seltrue(dev, events, td)
1138 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1142 * Record a select request.
1145 selrecord(selector, sip)
1146 struct thread *selector;
1147 struct selinfo *sip;
1152 * If the thread is NULL then take ownership of selinfo
1153 * however if the thread is not NULL and the thread points to
1154 * someone else, then we have a collision, otherwise leave it alone
1155 * as we've owned it in a previous selrecord on this selinfo.
1157 if (sip->si_thread == NULL) {
1158 sip->si_thread = selector;
1159 TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
1160 } else if (sip->si_thread != selector) {
1161 sip->si_flags |= SI_COLL;
1164 mtx_unlock(&sellock);
1168 * Do a wakeup when a selectable event occurs.
1172 struct selinfo *sip;
1177 td = sip->si_thread;
1178 if ((sip->si_flags & SI_COLL) != 0) {
1180 sip->si_flags &= ~SI_COLL;
1181 cv_broadcast(&selwait);
1184 mtx_unlock(&sellock);
1187 TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
1188 sip->si_thread = NULL;
1189 mtx_lock_spin(&sched_lock);
1190 if (td->td_wchan == (caddr_t)&selwait) {
1191 if (td->td_proc->p_stat == SSLEEP)
1194 cv_waitq_remove(td);
1196 td->td_flags &= ~TDF_SELECT;
1197 mtx_unlock_spin(&sched_lock);
1198 mtx_unlock(&sellock);
1201 static void selectinit __P((void *));
1202 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1209 cv_init(&selwait, "select");
1210 mtx_init(&sellock, "sellck", MTX_DEF);