xref: /freebsd/sys/kern/sys_generic.c (revision 3e15c66f2aeedb3c02fd2043f52d1c78580b1756)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40db6a20e2SGarrett Wollman #include "opt_ktrace.h"
41db6a20e2SGarrett Wollman 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44d2d3e875SBruce Evans #include <sys/sysproto.h>
45df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4620982410SBruce Evans #include <sys/filio.h>
473ac4d1efSBruce Evans #include <sys/fcntl.h>
48df8bae1dSRodney W. Grimes #include <sys/file.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
50797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52df8bae1dSRodney W. Grimes #include <sys/uio.h>
53df8bae1dSRodney W. Grimes #include <sys/kernel.h>
54104a9b7eSAlexander Kabaev #include <sys/limits.h>
55df8bae1dSRodney W. Grimes #include <sys/malloc.h>
5642d11757SPeter Wemm #include <sys/poll.h>
5789b71647SPeter Wemm #include <sys/resourcevar.h>
580a2c3d48SGarrett Wollman #include <sys/selinfo.h>
5944f3b092SJohn Baldwin #include <sys/sleepqueue.h>
608f19eb88SIan Dowse #include <sys/syscallsubr.h>
618cb96f20SPeter Wemm #include <sys/sysctl.h>
6242d11757SPeter Wemm #include <sys/sysent.h>
639bbee259SAndrey A. Chernov #include <sys/vnode.h>
64279d7226SMatthew Dillon #include <sys/bio.h>
65279d7226SMatthew Dillon #include <sys/buf.h>
66265fc98fSSeigo Tanimura #include <sys/condvar.h>
67df8bae1dSRodney W. Grimes #ifdef KTRACE
68df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
69df8bae1dSRodney W. Grimes #endif
70279d7226SMatthew Dillon #include <vm/vm.h>
71279d7226SMatthew Dillon #include <vm/vm_page.h>
72df8bae1dSRodney W. Grimes 
73a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
75a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7655166637SPoul-Henning Kamp 
77bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
78bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
79bbbb04ceSAlfred Perlstein static int	dofileread(struct thread *, struct file *, int, void *,
80bbbb04ceSAlfred Perlstein 		    size_t, off_t, int);
81bbbb04ceSAlfred Perlstein static int	dofilewrite(struct thread *, struct file *, int,
82bbbb04ceSAlfred Perlstein 		    const void *, size_t, off_t, int);
83512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
848fe387abSDmitrij Tejblum 
85df8bae1dSRodney W. Grimes /*
86df8bae1dSRodney W. Grimes  * Read system call.
87df8bae1dSRodney W. Grimes  */
88d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
89df8bae1dSRodney W. Grimes struct read_args {
90df8bae1dSRodney W. Grimes 	int	fd;
91134e06feSBruce Evans 	void	*buf;
92134e06feSBruce Evans 	size_t	nbyte;
93df8bae1dSRodney W. Grimes };
94d2d3e875SBruce Evans #endif
95ad2edad9SMatthew Dillon /*
96ad2edad9SMatthew Dillon  * MPSAFE
97ad2edad9SMatthew Dillon  */
9826f9a767SRodney W. Grimes int
99b40ce416SJulian Elischer read(td, uap)
100b40ce416SJulian Elischer 	struct thread *td;
101b064d43dSMatthew Dillon 	struct read_args *uap;
102df8bae1dSRodney W. Grimes {
103b064d43dSMatthew Dillon 	struct file *fp;
104279d7226SMatthew Dillon 	int error;
105df8bae1dSRodney W. Grimes 
106b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) == 0) {
107b40ce416SJulian Elischer 		error = dofileread(td, fp, uap->fd, uap->buf,
108ad2edad9SMatthew Dillon 			    uap->nbyte, (off_t)-1, 0);
109b40ce416SJulian Elischer 		fdrop(fp, td);
110ad2edad9SMatthew Dillon 	}
111279d7226SMatthew Dillon 	return(error);
112df8bae1dSRodney W. Grimes }
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes /*
1158fe387abSDmitrij Tejblum  * Pread system call
1164160ccd9SAlan Cox  */
1174160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1184160ccd9SAlan Cox struct pread_args {
1194160ccd9SAlan Cox 	int	fd;
1204160ccd9SAlan Cox 	void	*buf;
1214160ccd9SAlan Cox 	size_t	nbyte;
1228fe387abSDmitrij Tejblum 	int	pad;
1234160ccd9SAlan Cox 	off_t	offset;
1244160ccd9SAlan Cox };
1254160ccd9SAlan Cox #endif
126ad2edad9SMatthew Dillon /*
127ad2edad9SMatthew Dillon  * MPSAFE
128ad2edad9SMatthew Dillon  */
1294160ccd9SAlan Cox int
130b40ce416SJulian Elischer pread(td, uap)
131b40ce416SJulian Elischer 	struct thread *td;
132b064d43dSMatthew Dillon 	struct pread_args *uap;
1334160ccd9SAlan Cox {
134b064d43dSMatthew Dillon 	struct file *fp;
135279d7226SMatthew Dillon 	int error;
1368fe387abSDmitrij Tejblum 
13797fa4397SAlfred Perlstein 	if ((error = fget_read(td, uap->fd, &fp)) != 0)
13897fa4397SAlfred Perlstein 		return (error);
1399bbee259SAndrey A. Chernov 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
140b064d43dSMatthew Dillon 		error = ESPIPE;
1419bbee259SAndrey A. Chernov 	else if (uap->offset < 0 && fp->f_vnode->v_type != VCHR)
1429bbee259SAndrey A. Chernov 		error = EINVAL;
1439bbee259SAndrey A. Chernov 	else {
144426da3bcSAlfred Perlstein 		error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte,
145426da3bcSAlfred Perlstein 			    uap->offset, FOF_OFFSET);
146b064d43dSMatthew Dillon 	}
147b40ce416SJulian Elischer 	fdrop(fp, td);
148279d7226SMatthew Dillon 	return(error);
1498fe387abSDmitrij Tejblum }
1508fe387abSDmitrij Tejblum 
1518fe387abSDmitrij Tejblum /*
1528fe387abSDmitrij Tejblum  * Code common for read and pread
1538fe387abSDmitrij Tejblum  */
15437c84183SPoul-Henning Kamp static int
155b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags)
156b40ce416SJulian Elischer 	struct thread *td;
1578fe387abSDmitrij Tejblum 	struct file *fp;
1588fe387abSDmitrij Tejblum 	int fd, flags;
1598fe387abSDmitrij Tejblum 	void *buf;
1608fe387abSDmitrij Tejblum 	size_t nbyte;
1618fe387abSDmitrij Tejblum 	off_t offset;
1628fe387abSDmitrij Tejblum {
1634160ccd9SAlan Cox 	struct uio auio;
1644160ccd9SAlan Cox 	struct iovec aiov;
1654160ccd9SAlan Cox 	long cnt, error = 0;
1664160ccd9SAlan Cox #ifdef KTRACE
167552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
1684160ccd9SAlan Cox #endif
1694160ccd9SAlan Cox 
1700a3e28cfSAlfred Perlstein 	aiov.iov_base = buf;
1718fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
1724160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1734160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
1748fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
1758fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
1764160ccd9SAlan Cox 		return (EINVAL);
1778fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
1784160ccd9SAlan Cox 	auio.uio_rw = UIO_READ;
1794160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
180b40ce416SJulian Elischer 	auio.uio_td = td;
1814160ccd9SAlan Cox #ifdef KTRACE
182552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
183552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(&auio);
1844160ccd9SAlan Cox #endif
1858fe387abSDmitrij Tejblum 	cnt = nbyte;
186279d7226SMatthew Dillon 
1879ca43589SRobert Watson 	if ((error = fo_read(fp, &auio, td->td_ucred, flags, td))) {
1884160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
1894160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
1904160ccd9SAlan Cox 			error = 0;
191279d7226SMatthew Dillon 	}
1924160ccd9SAlan Cox 	cnt -= auio.uio_resid;
1934160ccd9SAlan Cox #ifdef KTRACE
194552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
195552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
196552afd9cSPoul-Henning Kamp 		ktrgenio(fd, UIO_READ, ktruio, error);
19742ebfbf2SBrian Feldman 	}
1984160ccd9SAlan Cox #endif
199b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
2004160ccd9SAlan Cox 	return (error);
2014160ccd9SAlan Cox }
2024160ccd9SAlan Cox 
2034160ccd9SAlan Cox /*
204df8bae1dSRodney W. Grimes  * Scatter read system call.
205df8bae1dSRodney W. Grimes  */
206d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
207df8bae1dSRodney W. Grimes struct readv_args {
2087147b19dSBruce Evans 	int	fd;
209df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
210df8bae1dSRodney W. Grimes 	u_int	iovcnt;
211df8bae1dSRodney W. Grimes };
212d2d3e875SBruce Evans #endif
213ad2edad9SMatthew Dillon /*
214ad2edad9SMatthew Dillon  * MPSAFE
215ad2edad9SMatthew Dillon  */
21626f9a767SRodney W. Grimes int
217552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap)
218df8bae1dSRodney W. Grimes {
219b064d43dSMatthew Dillon 	struct file *fp;
220552afd9cSPoul-Henning Kamp 	struct uio *auio = NULL;
221552afd9cSPoul-Henning Kamp 	long cnt;
22282641acdSAlan Cox 	int error;
223df8bae1dSRodney W. Grimes #ifdef KTRACE
224552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
225df8bae1dSRodney W. Grimes #endif
226df8bae1dSRodney W. Grimes 
227552afd9cSPoul-Henning Kamp 	error = fget_read(td, uap->fd, &fp);
228552afd9cSPoul-Henning Kamp 	if (error)
22982641acdSAlan Cox 		return (error);
230552afd9cSPoul-Henning Kamp 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
231552afd9cSPoul-Henning Kamp 	if (error) {
232552afd9cSPoul-Henning Kamp 		fdrop(fp, td);
233552afd9cSPoul-Henning Kamp 		return (error);
234ad2edad9SMatthew Dillon 	}
235552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
236552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
237df8bae1dSRodney W. Grimes #ifdef KTRACE
238552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
239552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
240df8bae1dSRodney W. Grimes #endif
241552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
242552afd9cSPoul-Henning Kamp 	if ((error = fo_read(fp, auio, td->td_ucred, 0, td))) {
243552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
244df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
245df8bae1dSRodney W. Grimes 			error = 0;
246279d7226SMatthew Dillon 	}
247552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
248df8bae1dSRodney W. Grimes #ifdef KTRACE
249552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
250552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
251552afd9cSPoul-Henning Kamp 		ktrgenio(uap->fd, UIO_READ, ktruio, error);
252df8bae1dSRodney W. Grimes 	}
253df8bae1dSRodney W. Grimes #endif
254b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
255552afd9cSPoul-Henning Kamp 	free(auio, M_IOV);
256b40ce416SJulian Elischer 	fdrop(fp, td);
257df8bae1dSRodney W. Grimes 	return (error);
258df8bae1dSRodney W. Grimes }
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes /*
261df8bae1dSRodney W. Grimes  * Write system call
262df8bae1dSRodney W. Grimes  */
263d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
264df8bae1dSRodney W. Grimes struct write_args {
265df8bae1dSRodney W. Grimes 	int	fd;
266134e06feSBruce Evans 	const void *buf;
267134e06feSBruce Evans 	size_t	nbyte;
268df8bae1dSRodney W. Grimes };
269d2d3e875SBruce Evans #endif
270ad2edad9SMatthew Dillon /*
271ad2edad9SMatthew Dillon  * MPSAFE
272ad2edad9SMatthew Dillon  */
27326f9a767SRodney W. Grimes int
274b40ce416SJulian Elischer write(td, uap)
275b40ce416SJulian Elischer 	struct thread *td;
276b064d43dSMatthew Dillon 	struct write_args *uap;
277df8bae1dSRodney W. Grimes {
278b064d43dSMatthew Dillon 	struct file *fp;
279279d7226SMatthew Dillon 	int error;
280df8bae1dSRodney W. Grimes 
281b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
282b40ce416SJulian Elischer 		error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte,
283ad2edad9SMatthew Dillon 			    (off_t)-1, 0);
284b40ce416SJulian Elischer 		fdrop(fp, td);
285ad2edad9SMatthew Dillon 	} else {
286b064d43dSMatthew Dillon 		error = EBADF;	/* XXX this can't be right */
287ad2edad9SMatthew Dillon 	}
288279d7226SMatthew Dillon 	return(error);
289df8bae1dSRodney W. Grimes }
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes /*
2928fe387abSDmitrij Tejblum  * Pwrite system call
2934160ccd9SAlan Cox  */
2944160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
2954160ccd9SAlan Cox struct pwrite_args {
2964160ccd9SAlan Cox 	int	fd;
2974160ccd9SAlan Cox 	const void *buf;
2984160ccd9SAlan Cox 	size_t	nbyte;
2998fe387abSDmitrij Tejblum 	int	pad;
3004160ccd9SAlan Cox 	off_t	offset;
3014160ccd9SAlan Cox };
3024160ccd9SAlan Cox #endif
303ad2edad9SMatthew Dillon /*
304ad2edad9SMatthew Dillon  * MPSAFE
305ad2edad9SMatthew Dillon  */
3064160ccd9SAlan Cox int
307b40ce416SJulian Elischer pwrite(td, uap)
308b40ce416SJulian Elischer 	struct thread *td;
309b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3104160ccd9SAlan Cox {
311b064d43dSMatthew Dillon 	struct file *fp;
312279d7226SMatthew Dillon 	int error;
3138fe387abSDmitrij Tejblum 
314b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
3159bbee259SAndrey A. Chernov 		if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
3162db4b023SPoul-Henning Kamp 			error = ESPIPE;
3179bbee259SAndrey A. Chernov 		else if (uap->offset < 0 && fp->f_vnode->v_type != VCHR)
3189bbee259SAndrey A. Chernov 			error = EINVAL;
3199bbee259SAndrey A. Chernov 		else {
320b064d43dSMatthew Dillon 			error = dofilewrite(td, fp, uap->fd, uap->buf,
321b064d43dSMatthew Dillon 				    uap->nbyte, uap->offset, FOF_OFFSET);
322b064d43dSMatthew Dillon 		}
323b40ce416SJulian Elischer 		fdrop(fp, td);
324279d7226SMatthew Dillon 	} else {
325b064d43dSMatthew Dillon 		error = EBADF;	/* this can't be right */
326ad2edad9SMatthew Dillon 	}
327279d7226SMatthew Dillon 	return(error);
3288fe387abSDmitrij Tejblum }
3298fe387abSDmitrij Tejblum 
3308fe387abSDmitrij Tejblum static int
331b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags)
332b40ce416SJulian Elischer 	struct thread *td;
3338fe387abSDmitrij Tejblum 	struct file *fp;
3348fe387abSDmitrij Tejblum 	int fd, flags;
3358fe387abSDmitrij Tejblum 	const void *buf;
3368fe387abSDmitrij Tejblum 	size_t nbyte;
3378fe387abSDmitrij Tejblum 	off_t offset;
3388fe387abSDmitrij Tejblum {
3394160ccd9SAlan Cox 	struct uio auio;
3404160ccd9SAlan Cox 	struct iovec aiov;
3414160ccd9SAlan Cox 	long cnt, error = 0;
3424160ccd9SAlan Cox #ifdef KTRACE
343552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
3444160ccd9SAlan Cox #endif
3454160ccd9SAlan Cox 
346b31ae1adSPeter Wemm 	aiov.iov_base = (void *)(uintptr_t)buf;
3478fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
3484160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3494160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
3508fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
3518fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
3524160ccd9SAlan Cox 		return (EINVAL);
3538fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
3544160ccd9SAlan Cox 	auio.uio_rw = UIO_WRITE;
3554160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
356b40ce416SJulian Elischer 	auio.uio_td = td;
3574160ccd9SAlan Cox #ifdef KTRACE
358552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
359552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(&auio);
3604160ccd9SAlan Cox #endif
3618fe387abSDmitrij Tejblum 	cnt = nbyte;
362c6ab5768SAlfred Perlstein 	if (fp->f_type == DTYPE_VNODE)
363279d7226SMatthew Dillon 		bwillwrite();
3649ca43589SRobert Watson 	if ((error = fo_write(fp, &auio, td->td_ucred, flags, td))) {
3654160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
3664160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
3674160ccd9SAlan Cox 			error = 0;
368c33c8251SAlfred Perlstein 		/* Socket layer is responsible for issuing SIGPIPE. */
369c33c8251SAlfred Perlstein 		if (error == EPIPE && fp->f_type != DTYPE_SOCKET) {
370b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
371b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
372b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
37319eb87d2SJohn Baldwin 		}
3744160ccd9SAlan Cox 	}
3754160ccd9SAlan Cox 	cnt -= auio.uio_resid;
3764160ccd9SAlan Cox #ifdef KTRACE
377552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
378552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
379552afd9cSPoul-Henning Kamp 		ktrgenio(fd, UIO_WRITE, ktruio, error);
38042ebfbf2SBrian Feldman 	}
3814160ccd9SAlan Cox #endif
382b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
3834160ccd9SAlan Cox 	return (error);
3844160ccd9SAlan Cox }
3854160ccd9SAlan Cox 
3864160ccd9SAlan Cox /*
387df8bae1dSRodney W. Grimes  * Gather write system call
388df8bae1dSRodney W. Grimes  */
389d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
390df8bae1dSRodney W. Grimes struct writev_args {
391df8bae1dSRodney W. Grimes 	int	fd;
392df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
393df8bae1dSRodney W. Grimes 	u_int	iovcnt;
394df8bae1dSRodney W. Grimes };
395d2d3e875SBruce Evans #endif
396ad2edad9SMatthew Dillon /*
397ad2edad9SMatthew Dillon  * MPSAFE
398ad2edad9SMatthew Dillon  */
39926f9a767SRodney W. Grimes int
400552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap)
401df8bae1dSRodney W. Grimes {
402b064d43dSMatthew Dillon 	struct file *fp;
403552afd9cSPoul-Henning Kamp 	struct uio *auio = NULL;
404552afd9cSPoul-Henning Kamp 	long cnt;
405552afd9cSPoul-Henning Kamp 	int error;
406df8bae1dSRodney W. Grimes #ifdef KTRACE
407552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
408df8bae1dSRodney W. Grimes #endif
409df8bae1dSRodney W. Grimes 
410552afd9cSPoul-Henning Kamp 	error = fget_write(td, uap->fd, &fp);
411552afd9cSPoul-Henning Kamp 	if (error)
412882d8469SAlan Cox 		return (EBADF);
413552afd9cSPoul-Henning Kamp 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
414552afd9cSPoul-Henning Kamp 	if (error) {
415552afd9cSPoul-Henning Kamp 		fdrop(fp, td);
416552afd9cSPoul-Henning Kamp 		return (error);
4171aa3e7ddSBrian Feldman 	}
418552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
419552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
420df8bae1dSRodney W. Grimes #ifdef KTRACE
421552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
422552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
423df8bae1dSRodney W. Grimes #endif
424552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
425a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
4269440653dSMatthew Dillon 		bwillwrite();
427552afd9cSPoul-Henning Kamp 	if ((error = fo_write(fp, auio, td->td_ucred, 0, td))) {
428552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
429df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
430df8bae1dSRodney W. Grimes 			error = 0;
43119eb87d2SJohn Baldwin 		if (error == EPIPE) {
432b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
433b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
434b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
43519eb87d2SJohn Baldwin 		}
436df8bae1dSRodney W. Grimes 	}
437552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
438df8bae1dSRodney W. Grimes #ifdef KTRACE
439552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
440552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
441552afd9cSPoul-Henning Kamp 		ktrgenio(uap->fd, UIO_WRITE, ktruio, error);
442df8bae1dSRodney W. Grimes 	}
443df8bae1dSRodney W. Grimes #endif
444b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
445b40ce416SJulian Elischer 	fdrop(fp, td);
446552afd9cSPoul-Henning Kamp 	free(auio, M_IOV);
447df8bae1dSRodney W. Grimes 	return (error);
448df8bae1dSRodney W. Grimes }
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes /*
451df8bae1dSRodney W. Grimes  * Ioctl system call
452df8bae1dSRodney W. Grimes  */
453d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
454df8bae1dSRodney W. Grimes struct ioctl_args {
455df8bae1dSRodney W. Grimes 	int	fd;
456069e9bc1SDoug Rabson 	u_long	com;
457df8bae1dSRodney W. Grimes 	caddr_t	data;
458df8bae1dSRodney W. Grimes };
459d2d3e875SBruce Evans #endif
460ad2edad9SMatthew Dillon /*
461ad2edad9SMatthew Dillon  * MPSAFE
462ad2edad9SMatthew Dillon  */
463df8bae1dSRodney W. Grimes /* ARGSUSED */
46426f9a767SRodney W. Grimes int
4653e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap)
466df8bae1dSRodney W. Grimes {
467a4db4953SAlfred Perlstein 	struct file *fp;
4683e15c66fSPoul-Henning Kamp 	struct filedesc *fdp;
4693e15c66fSPoul-Henning Kamp 	u_long com;
470ad2edad9SMatthew Dillon 	int error = 0;
4713e15c66fSPoul-Henning Kamp 	u_int size;
472df8bae1dSRodney W. Grimes 	caddr_t data, memp;
473df8bae1dSRodney W. Grimes 	int tmp;
474df8bae1dSRodney W. Grimes #define STK_PARAMS	128
475d2ba455cSMatthew Dillon 	union {
476df8bae1dSRodney W. Grimes 	    char stkbuf[STK_PARAMS];
477d2ba455cSMatthew Dillon 	    long align;
478d2ba455cSMatthew Dillon 	} ubuf;
479df8bae1dSRodney W. Grimes 
480a4db4953SAlfred Perlstein 	if ((error = fget(td, uap->fd, &fp)) != 0)
481a4db4953SAlfred Perlstein 		return (error);
482aa11a498SAlfred Perlstein 	mtx_lock(&Giant);
483ad2edad9SMatthew Dillon 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
484426da3bcSAlfred Perlstein 		fdrop(fp, td);
485aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
486426da3bcSAlfred Perlstein 		return (EBADF);
487ad2edad9SMatthew Dillon 	}
488426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
489df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
490df8bae1dSRodney W. Grimes 	case FIONCLEX:
491124e4c3bSPoul-Henning Kamp 		FILEDESC_LOCK_FAST(fdp);
492df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
493124e4c3bSPoul-Henning Kamp 		FILEDESC_UNLOCK_FAST(fdp);
494426da3bcSAlfred Perlstein 		fdrop(fp, td);
495aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
496426da3bcSAlfred Perlstein 		return (0);
497df8bae1dSRodney W. Grimes 	case FIOCLEX:
498124e4c3bSPoul-Henning Kamp 		FILEDESC_LOCK_FAST(fdp);
499df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
500124e4c3bSPoul-Henning Kamp 		FILEDESC_UNLOCK_FAST(fdp);
501426da3bcSAlfred Perlstein 		fdrop(fp, td);
502aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
503426da3bcSAlfred Perlstein 		return (0);
504df8bae1dSRodney W. Grimes 	}
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 	/*
507df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
508df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
509df8bae1dSRodney W. Grimes 	 */
510df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
511ad2edad9SMatthew Dillon 	if (size > IOCPARM_MAX) {
512426da3bcSAlfred Perlstein 		fdrop(fp, td);
513aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
514426da3bcSAlfred Perlstein 		return (ENOTTY);
515ad2edad9SMatthew Dillon 	}
516279d7226SMatthew Dillon 
517df8bae1dSRodney W. Grimes 	memp = NULL;
518d2ba455cSMatthew Dillon 	if (size > sizeof (ubuf.stkbuf)) {
519a163d034SWarner Losh 		memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
520df8bae1dSRodney W. Grimes 		data = memp;
521279d7226SMatthew Dillon 	} else {
522d2ba455cSMatthew Dillon 		data = ubuf.stkbuf;
523279d7226SMatthew Dillon 	}
524df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
525df8bae1dSRodney W. Grimes 		if (size) {
526df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
527df8bae1dSRodney W. Grimes 			if (error) {
528df8bae1dSRodney W. Grimes 				if (memp)
529df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
530b40ce416SJulian Elischer 				fdrop(fp, td);
5313e15c66fSPoul-Henning Kamp 				mtx_unlock(&Giant);
5323e15c66fSPoul-Henning Kamp 				return (error);
533df8bae1dSRodney W. Grimes 			}
534279d7226SMatthew Dillon 		} else {
535df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
536279d7226SMatthew Dillon 		}
537279d7226SMatthew Dillon 	} else if ((com&IOC_OUT) && size) {
538df8bae1dSRodney W. Grimes 		/*
539df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
540df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
541df8bae1dSRodney W. Grimes 		 */
542df8bae1dSRodney W. Grimes 		bzero(data, size);
543279d7226SMatthew Dillon 	} else if (com&IOC_VOID) {
544df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
545279d7226SMatthew Dillon 	}
546df8bae1dSRodney W. Grimes 
547df8bae1dSRodney W. Grimes 	switch (com) {
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes 	case FIONBIO:
550426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
551bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
552df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
553df8bae1dSRodney W. Grimes 		else
554df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
555426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
556d49fa1caSRobert Watson 		error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
557df8bae1dSRodney W. Grimes 		break;
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 	case FIOASYNC:
560426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
561bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
562df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
563df8bae1dSRodney W. Grimes 		else
564df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
565426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
566d49fa1caSRobert Watson 		error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
567df8bae1dSRodney W. Grimes 		break;
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 	default:
570d49fa1caSRobert Watson 		error = fo_ioctl(fp, com, data, td->td_ucred, td);
571df8bae1dSRodney W. Grimes 		/*
572df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
573df8bae1dSRodney W. Grimes 		 * already set and checked above.
574df8bae1dSRodney W. Grimes 		 */
575df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
576df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
577df8bae1dSRodney W. Grimes 		break;
578df8bae1dSRodney W. Grimes 	}
579df8bae1dSRodney W. Grimes 	if (memp)
580df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
581b40ce416SJulian Elischer 	fdrop(fp, td);
582ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
583df8bae1dSRodney W. Grimes 	return (error);
584df8bae1dSRodney W. Grimes }
585df8bae1dSRodney W. Grimes 
58685f190e4SAlfred Perlstein /*
58785f190e4SAlfred Perlstein  * sellock and selwait are initialized in selectinit() via SYSINIT.
58885f190e4SAlfred Perlstein  */
58985f190e4SAlfred Perlstein struct mtx	sellock;
590265fc98fSSeigo Tanimura struct cv	selwait;
5919ae6d334SKelly Yancey u_int		nselcoll;	/* Select collisions since boot */
5929ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes /*
595df8bae1dSRodney W. Grimes  * Select system call.
596df8bae1dSRodney W. Grimes  */
597d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
598df8bae1dSRodney W. Grimes struct select_args {
599b08f7993SSujal Patel 	int	nd;
600df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
601df8bae1dSRodney W. Grimes 	struct	timeval *tv;
602df8bae1dSRodney W. Grimes };
603d2d3e875SBruce Evans #endif
604ad2edad9SMatthew Dillon /*
605ad2edad9SMatthew Dillon  * MPSAFE
606ad2edad9SMatthew Dillon  */
60726f9a767SRodney W. Grimes int
608b40ce416SJulian Elischer select(td, uap)
609b40ce416SJulian Elischer 	register struct thread *td;
610df8bae1dSRodney W. Grimes 	register struct select_args *uap;
611df8bae1dSRodney W. Grimes {
6128f19eb88SIan Dowse 	struct timeval tv, *tvp;
6138f19eb88SIan Dowse 	int error;
6148f19eb88SIan Dowse 
6158f19eb88SIan Dowse 	if (uap->tv != NULL) {
6168f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
6178f19eb88SIan Dowse 		if (error)
6188f19eb88SIan Dowse 			return (error);
6198f19eb88SIan Dowse 		tvp = &tv;
6208f19eb88SIan Dowse 	} else
6218f19eb88SIan Dowse 		tvp = NULL;
6228f19eb88SIan Dowse 
6238f19eb88SIan Dowse 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
6248f19eb88SIan Dowse }
6258f19eb88SIan Dowse 
6268f19eb88SIan Dowse int
6278f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
6288f19eb88SIan Dowse     fd_set *fd_ex, struct timeval *tvp)
6298f19eb88SIan Dowse {
630426da3bcSAlfred Perlstein 	struct filedesc *fdp;
631d5e4d7e1SBruce Evans 	/*
632d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
633d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
634d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
635d5e4d7e1SBruce Evans 	 * of 256.
636d5e4d7e1SBruce Evans 	 */
637d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
638eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
63900af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
6409ae6d334SKelly Yancey 	int error, timo;
6419ae6d334SKelly Yancey 	u_int ncoll, nbufbytes, ncpbytes, nfdbits;
642df8bae1dSRodney W. Grimes 
6438f19eb88SIan Dowse 	if (nd < 0)
644acbfbfeaSSujal Patel 		return (EINVAL);
645426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
6465d8dd01dSRobert Watson 	/*
6475d8dd01dSRobert Watson 	 * XXX: kern_select() currently requires that we acquire Giant
6485d8dd01dSRobert Watson 	 * even if none of the file descriptors we poll requires Giant.
6495d8dd01dSRobert Watson 	 */
650ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
651124e4c3bSPoul-Henning Kamp 	FILEDESC_LOCK_FAST(fdp);
652ad2edad9SMatthew Dillon 
6538f19eb88SIan Dowse 	if (nd > td->td_proc->p_fd->fd_nfiles)
6548f19eb88SIan Dowse 		nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
655124e4c3bSPoul-Henning Kamp 	FILEDESC_UNLOCK_FAST(fdp);
656b08f7993SSujal Patel 
657d5e4d7e1SBruce Evans 	/*
658d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
659d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
660d5e4d7e1SBruce Evans 	 */
6618f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
662d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
663d5e4d7e1SBruce Evans 	nbufbytes = 0;
6648f19eb88SIan Dowse 	if (fd_in != NULL)
665d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
6668f19eb88SIan Dowse 	if (fd_ou != NULL)
667d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
6688f19eb88SIan Dowse 	if (fd_ex != NULL)
669d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
670d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
671d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
672d5e4d7e1SBruce Evans 	else
673a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
674b08f7993SSujal Patel 
675b08f7993SSujal Patel 	/*
676d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
677d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
678d5e4d7e1SBruce Evans 	 * together.
679b08f7993SSujal Patel 	 */
680d5e4d7e1SBruce Evans 	sbp = selbits;
681df8bae1dSRodney W. Grimes #define	getbits(name, x) \
682d5e4d7e1SBruce Evans 	do {								\
6838f19eb88SIan Dowse 		if (name == NULL)					\
684d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
685d5e4d7e1SBruce Evans 		else {							\
686d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
687d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
688d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
6898f19eb88SIan Dowse 			error = copyin(name, ibits[x], ncpbytes);	\
690265fc98fSSeigo Tanimura 			if (error != 0)					\
69185f190e4SAlfred Perlstein 				goto done_nosellock;			\
692e04ac2feSJohn Baldwin 		}							\
693d5e4d7e1SBruce Evans 	} while (0)
6948f19eb88SIan Dowse 	getbits(fd_in, 0);
6958f19eb88SIan Dowse 	getbits(fd_ou, 1);
6968f19eb88SIan Dowse 	getbits(fd_ex, 2);
697df8bae1dSRodney W. Grimes #undef	getbits
698d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
699d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
700df8bae1dSRodney W. Grimes 
7018f19eb88SIan Dowse 	if (tvp != NULL) {
7028f19eb88SIan Dowse 		atv = *tvp;
703df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
704df8bae1dSRodney W. Grimes 			error = EINVAL;
70585f190e4SAlfred Perlstein 			goto done_nosellock;
706df8bae1dSRodney W. Grimes 		}
707c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
70800af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
7099c386f6bSJohn Baldwin 	} else {
71000af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
7119c386f6bSJohn Baldwin 		atv.tv_usec = 0;
7129c386f6bSJohn Baldwin 	}
71300af9731SPoul-Henning Kamp 	timo = 0;
7142149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
71585f190e4SAlfred Perlstein 	mtx_lock(&sellock);
716df8bae1dSRodney W. Grimes retry:
717df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
718fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
719b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
720fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
72185f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
72285f190e4SAlfred Perlstein 
7238f19eb88SIan Dowse 	error = selscan(td, ibits, obits, nd);
72485f190e4SAlfred Perlstein 	mtx_lock(&sellock);
725b40ce416SJulian Elischer 	if (error || td->td_retval[0])
726df8bae1dSRodney W. Grimes 		goto done;
7274da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
728c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
72985f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
730df8bae1dSRodney W. Grimes 			goto done;
73100af9731SPoul-Henning Kamp 		ttv = atv;
73200af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
73300af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
73400af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
735df8bae1dSRodney W. Grimes 	}
73685f190e4SAlfred Perlstein 
73785f190e4SAlfred Perlstein 	/*
73885f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
73985f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of
74085f190e4SAlfred Perlstein 	 * collisions and rescan the file descriptors if
74185f190e4SAlfred Perlstein 	 * necessary.
74285f190e4SAlfred Perlstein 	 */
743fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
74485f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
74585f190e4SAlfred Perlstein 		mtx_unlock_spin(&sched_lock);
74685f190e4SAlfred Perlstein 		goto retry;
74785f190e4SAlfred Perlstein 	}
748fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
749bfbbc4aaSJason Evans 
750265fc98fSSeigo Tanimura 	if (timo > 0)
75185f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
752265fc98fSSeigo Tanimura 	else
75385f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
754bfbbc4aaSJason Evans 
755df8bae1dSRodney W. Grimes 	if (error == 0)
756df8bae1dSRodney W. Grimes 		goto retry;
757265fc98fSSeigo Tanimura 
758df8bae1dSRodney W. Grimes done:
75985f190e4SAlfred Perlstein 	clear_selinfo_list(td);
760fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
761b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
762fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
76385f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
76485f190e4SAlfred Perlstein 
76585f190e4SAlfred Perlstein done_nosellock:
766df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
767df8bae1dSRodney W. Grimes 	if (error == ERESTART)
768df8bae1dSRodney W. Grimes 		error = EINTR;
769df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
770df8bae1dSRodney W. Grimes 		error = 0;
771df8bae1dSRodney W. Grimes #define	putbits(name, x) \
7728f19eb88SIan Dowse 	if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
773df8bae1dSRodney W. Grimes 		error = error2;
774df8bae1dSRodney W. Grimes 	if (error == 0) {
775df8bae1dSRodney W. Grimes 		int error2;
776df8bae1dSRodney W. Grimes 
7778f19eb88SIan Dowse 		putbits(fd_in, 0);
7788f19eb88SIan Dowse 		putbits(fd_ou, 1);
7798f19eb88SIan Dowse 		putbits(fd_ex, 2);
780df8bae1dSRodney W. Grimes #undef putbits
781df8bae1dSRodney W. Grimes 	}
782d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
783d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
784ad2edad9SMatthew Dillon 
785ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
786df8bae1dSRodney W. Grimes 	return (error);
787df8bae1dSRodney W. Grimes }
788df8bae1dSRodney W. Grimes 
789265fc98fSSeigo Tanimura static int
790b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
791b40ce416SJulian Elischer 	struct thread *td;
792b08f7993SSujal Patel 	fd_mask **ibits, **obits;
793cb226aaaSPoul-Henning Kamp 	int nfd;
794df8bae1dSRodney W. Grimes {
795f082218cSPeter Wemm 	int msk, i, fd;
796f082218cSPeter Wemm 	fd_mask bits;
797df8bae1dSRodney W. Grimes 	struct file *fp;
798df8bae1dSRodney W. Grimes 	int n = 0;
7992087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
80042d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
801eb209311SAlfred Perlstein 	struct filedesc *fdp = td->td_proc->p_fd;
802df8bae1dSRodney W. Grimes 
803eb209311SAlfred Perlstein 	FILEDESC_LOCK(fdp);
804df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
805d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
806d5e4d7e1SBruce Evans 			continue;
807df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
808b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
809f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
810f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
811f082218cSPeter Wemm 				if (!(bits & 1))
812f082218cSPeter Wemm 					continue;
813eb209311SAlfred Perlstein 				if ((fp = fget_locked(fdp, fd)) == NULL) {
814eb209311SAlfred Perlstein 					FILEDESC_UNLOCK(fdp);
815df8bae1dSRodney W. Grimes 					return (EBADF);
816eb209311SAlfred Perlstein 				}
817ea6027a8SRobert Watson 				if (fo_poll(fp, flag[msk], td->td_ucred,
818ea6027a8SRobert Watson 				    td)) {
819b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
820f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
821df8bae1dSRodney W. Grimes 					n++;
822df8bae1dSRodney W. Grimes 				}
823df8bae1dSRodney W. Grimes 			}
824df8bae1dSRodney W. Grimes 		}
825df8bae1dSRodney W. Grimes 	}
826eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
827b40ce416SJulian Elischer 	td->td_retval[0] = n;
828df8bae1dSRodney W. Grimes 	return (0);
829df8bae1dSRodney W. Grimes }
830df8bae1dSRodney W. Grimes 
83142d11757SPeter Wemm /*
83242d11757SPeter Wemm  * Poll system call.
83342d11757SPeter Wemm  */
83442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
83542d11757SPeter Wemm struct poll_args {
83642d11757SPeter Wemm 	struct pollfd *fds;
83742d11757SPeter Wemm 	u_int	nfds;
83842d11757SPeter Wemm 	int	timeout;
83942d11757SPeter Wemm };
84042d11757SPeter Wemm #endif
841ad2edad9SMatthew Dillon /*
842ad2edad9SMatthew Dillon  * MPSAFE
843ad2edad9SMatthew Dillon  */
84442d11757SPeter Wemm int
845b40ce416SJulian Elischer poll(td, uap)
846b40ce416SJulian Elischer 	struct thread *td;
847ea0237edSJonathan Lemon 	struct poll_args *uap;
84842d11757SPeter Wemm {
8492580f4e5SAndre Oppermann 	struct pollfd *bits;
8502580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
85100af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
8529ae6d334SKelly Yancey 	int error = 0, timo;
8539ae6d334SKelly Yancey 	u_int ncoll, nfds;
85442d11757SPeter Wemm 	size_t ni;
85542d11757SPeter Wemm 
856d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
857ad2edad9SMatthew Dillon 
8585d8dd01dSRobert Watson 	/*
8595d8dd01dSRobert Watson 	 * XXX: poll() currently requires that we acquire Giant even if
8605d8dd01dSRobert Watson 	 * none of the file descriptors we poll requires Giant.
8615d8dd01dSRobert Watson 	 */
862ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
86389b71647SPeter Wemm 	/*
8642bd5ac33SPeter Wemm 	 * This is kinda bogus.  We have fd limits, but that is not
8652bd5ac33SPeter Wemm 	 * really related to the size of the pollfd array.  Make sure
8662bd5ac33SPeter Wemm 	 * we let the process use at least FD_SETSIZE entries and at
8672bd5ac33SPeter Wemm 	 * least enough for the current limits.  We want to be reasonably
8682bd5ac33SPeter Wemm 	 * safe, but not overly restrictive.
86989b71647SPeter Wemm 	 */
87091d5354aSJohn Baldwin 	PROC_LOCK(td->td_proc);
87191d5354aSJohn Baldwin 	if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) &&
872b40ce416SJulian Elischer 	    (nfds > FD_SETSIZE)) {
87391d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
874ad2edad9SMatthew Dillon 		error = EINVAL;
875ad2edad9SMatthew Dillon 		goto done2;
876ad2edad9SMatthew Dillon 	}
87791d5354aSJohn Baldwin 	PROC_UNLOCK(td->td_proc);
87889b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
87942d11757SPeter Wemm 	if (ni > sizeof(smallbits))
880a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
88142d11757SPeter Wemm 	else
88242d11757SPeter Wemm 		bits = smallbits;
883d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
88442d11757SPeter Wemm 	if (error)
88585f190e4SAlfred Perlstein 		goto done_nosellock;
886d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
887d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
888d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
88942d11757SPeter Wemm 		if (itimerfix(&atv)) {
89042d11757SPeter Wemm 			error = EINVAL;
89185f190e4SAlfred Perlstein 			goto done_nosellock;
89242d11757SPeter Wemm 		}
893c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
89400af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
8959c386f6bSJohn Baldwin 	} else {
89600af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
8979c386f6bSJohn Baldwin 		atv.tv_usec = 0;
8989c386f6bSJohn Baldwin 	}
89900af9731SPoul-Henning Kamp 	timo = 0;
9002149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
90185f190e4SAlfred Perlstein 	mtx_lock(&sellock);
90242d11757SPeter Wemm retry:
90342d11757SPeter Wemm 	ncoll = nselcoll;
904fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
905b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
906fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
90785f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
90885f190e4SAlfred Perlstein 
9092580f4e5SAndre Oppermann 	error = pollscan(td, bits, nfds);
91085f190e4SAlfred Perlstein 	mtx_lock(&sellock);
911b40ce416SJulian Elischer 	if (error || td->td_retval[0])
91242d11757SPeter Wemm 		goto done;
9134da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
914c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
91585f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
91642d11757SPeter Wemm 			goto done;
91700af9731SPoul-Henning Kamp 		ttv = atv;
91800af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
91900af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
92000af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
92142d11757SPeter Wemm 	}
92285f190e4SAlfred Perlstein 	/*
92385f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
92485f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of collisions
92585f190e4SAlfred Perlstein 	 * and rescan the file descriptors if necessary.
92685f190e4SAlfred Perlstein 	 */
927fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
92885f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
929fea2ab83SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
93085f190e4SAlfred Perlstein 		goto retry;
93185f190e4SAlfred Perlstein 	}
93285f190e4SAlfred Perlstein 	mtx_unlock_spin(&sched_lock);
93385f190e4SAlfred Perlstein 
934265fc98fSSeigo Tanimura 	if (timo > 0)
93585f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
936265fc98fSSeigo Tanimura 	else
93785f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
93885f190e4SAlfred Perlstein 
93942d11757SPeter Wemm 	if (error == 0)
94042d11757SPeter Wemm 		goto retry;
941265fc98fSSeigo Tanimura 
94242d11757SPeter Wemm done:
94385f190e4SAlfred Perlstein 	clear_selinfo_list(td);
944fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
945b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
946fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
94785f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
94885f190e4SAlfred Perlstein 
94985f190e4SAlfred Perlstein done_nosellock:
95042d11757SPeter Wemm 	/* poll is not restarted after signals... */
95142d11757SPeter Wemm 	if (error == ERESTART)
95242d11757SPeter Wemm 		error = EINTR;
95342d11757SPeter Wemm 	if (error == EWOULDBLOCK)
95442d11757SPeter Wemm 		error = 0;
95542d11757SPeter Wemm 	if (error == 0) {
956d1e405c5SAlfred Perlstein 		error = copyout(bits, uap->fds, ni);
95742d11757SPeter Wemm 		if (error)
95842d11757SPeter Wemm 			goto out;
95942d11757SPeter Wemm 	}
96042d11757SPeter Wemm out:
96142d11757SPeter Wemm 	if (ni > sizeof(smallbits))
96242d11757SPeter Wemm 		free(bits, M_TEMP);
963ad2edad9SMatthew Dillon done2:
964ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
96542d11757SPeter Wemm 	return (error);
96642d11757SPeter Wemm }
96742d11757SPeter Wemm 
96842d11757SPeter Wemm static int
969b40ce416SJulian Elischer pollscan(td, fds, nfd)
970b40ce416SJulian Elischer 	struct thread *td;
97142d11757SPeter Wemm 	struct pollfd *fds;
972ea0237edSJonathan Lemon 	u_int nfd;
97342d11757SPeter Wemm {
974b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
97542d11757SPeter Wemm 	int i;
97642d11757SPeter Wemm 	struct file *fp;
97742d11757SPeter Wemm 	int n = 0;
97842d11757SPeter Wemm 
979426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
980eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
981337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
98242d11757SPeter Wemm 			fds->revents = POLLNVAL;
98342d11757SPeter Wemm 			n++;
984337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
985337c9691SJordan K. Hubbard 			fds->revents = 0;
98642d11757SPeter Wemm 		} else {
98742d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
988279d7226SMatthew Dillon 			if (fp == NULL) {
98942d11757SPeter Wemm 				fds->revents = POLLNVAL;
99042d11757SPeter Wemm 				n++;
99142d11757SPeter Wemm 			} else {
9922087c896SBruce Evans 				/*
9932087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
9942087c896SBruce Evans 				 * POLLERR if appropriate.
9952087c896SBruce Evans 				 */
99613ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
997ea6027a8SRobert Watson 				    td->td_ucred, td);
99842d11757SPeter Wemm 				if (fds->revents != 0)
99942d11757SPeter Wemm 					n++;
100042d11757SPeter Wemm 			}
100142d11757SPeter Wemm 		}
100242d11757SPeter Wemm 	}
1003eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
1004b40ce416SJulian Elischer 	td->td_retval[0] = n;
100542d11757SPeter Wemm 	return (0);
100642d11757SPeter Wemm }
100742d11757SPeter Wemm 
100842d11757SPeter Wemm /*
100942d11757SPeter Wemm  * OpenBSD poll system call.
101042d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
101142d11757SPeter Wemm  */
101242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
101342d11757SPeter Wemm struct openbsd_poll_args {
101442d11757SPeter Wemm 	struct pollfd *fds;
101542d11757SPeter Wemm 	u_int	nfds;
101642d11757SPeter Wemm 	int	timeout;
101742d11757SPeter Wemm };
101842d11757SPeter Wemm #endif
1019ad2edad9SMatthew Dillon /*
1020ad2edad9SMatthew Dillon  * MPSAFE
1021ad2edad9SMatthew Dillon  */
102242d11757SPeter Wemm int
1023b40ce416SJulian Elischer openbsd_poll(td, uap)
1024b40ce416SJulian Elischer 	register struct thread *td;
102542d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
102642d11757SPeter Wemm {
1027b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
102842d11757SPeter Wemm }
102942d11757SPeter Wemm 
103085f190e4SAlfred Perlstein /*
103185f190e4SAlfred Perlstein  * Remove the references to the thread from all of the objects
103285f190e4SAlfred Perlstein  * we were polling.
103385f190e4SAlfred Perlstein  *
103485f190e4SAlfred Perlstein  * This code assumes that the underlying owner of the selinfo
103585f190e4SAlfred Perlstein  * structure will hold sellock before it changes it, and that
103685f190e4SAlfred Perlstein  * it will unlink itself from our list if it goes away.
103785f190e4SAlfred Perlstein  */
103885f190e4SAlfred Perlstein void
103985f190e4SAlfred Perlstein clear_selinfo_list(td)
104085f190e4SAlfred Perlstein 	struct thread *td;
104185f190e4SAlfred Perlstein {
104285f190e4SAlfred Perlstein 	struct selinfo *si;
104385f190e4SAlfred Perlstein 
104485f190e4SAlfred Perlstein 	mtx_assert(&sellock, MA_OWNED);
104585f190e4SAlfred Perlstein 	TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
104685f190e4SAlfred Perlstein 		si->si_thread = NULL;
104785f190e4SAlfred Perlstein 	TAILQ_INIT(&td->td_selq);
104885f190e4SAlfred Perlstein }
104985f190e4SAlfred Perlstein 
1050df8bae1dSRodney W. Grimes /*
1051df8bae1dSRodney W. Grimes  * Record a select request.
1052df8bae1dSRodney W. Grimes  */
1053df8bae1dSRodney W. Grimes void
1054df8bae1dSRodney W. Grimes selrecord(selector, sip)
1055b40ce416SJulian Elischer 	struct thread *selector;
1056df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1057df8bae1dSRodney W. Grimes {
1058df8bae1dSRodney W. Grimes 
105985f190e4SAlfred Perlstein 	mtx_lock(&sellock);
106085f190e4SAlfred Perlstein 	/*
1061b605b54cSAlfred Perlstein 	 * If the selinfo's thread pointer is NULL then take ownership of it.
1062b605b54cSAlfred Perlstein 	 *
1063b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and it points to another
1064b605b54cSAlfred Perlstein 	 * thread, then we have a collision.
1065b605b54cSAlfred Perlstein 	 *
1066b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and points back to us then leave
1067b605b54cSAlfred Perlstein 	 * it alone as we've already added pointed it at us and added it to
1068b605b54cSAlfred Perlstein 	 * our list.
106985f190e4SAlfred Perlstein 	 */
107085f190e4SAlfred Perlstein 	if (sip->si_thread == NULL) {
1071b40ce416SJulian Elischer 		sip->si_thread = selector;
107285f190e4SAlfred Perlstein 		TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
107385f190e4SAlfred Perlstein 	} else if (sip->si_thread != selector) {
107485f190e4SAlfred Perlstein 		sip->si_flags |= SI_COLL;
107585f190e4SAlfred Perlstein 	}
107685f190e4SAlfred Perlstein 
107785f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1078df8bae1dSRodney W. Grimes }
1079df8bae1dSRodney W. Grimes 
1080512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1081df8bae1dSRodney W. Grimes void
1082df8bae1dSRodney W. Grimes selwakeup(sip)
108385f190e4SAlfred Perlstein 	struct selinfo *sip;
1084df8bae1dSRodney W. Grimes {
1085512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1086512824f8SSeigo Tanimura }
1087512824f8SSeigo Tanimura 
1088512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1089512824f8SSeigo Tanimura void
1090512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1091512824f8SSeigo Tanimura 	struct selinfo *sip;
1092512824f8SSeigo Tanimura 	int pri;
1093512824f8SSeigo Tanimura {
1094512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1095512824f8SSeigo Tanimura }
1096512824f8SSeigo Tanimura 
1097512824f8SSeigo Tanimura /*
1098512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1099512824f8SSeigo Tanimura  */
1100512824f8SSeigo Tanimura static void
1101512824f8SSeigo Tanimura doselwakeup(sip, pri)
1102512824f8SSeigo Tanimura 	struct selinfo *sip;
1103512824f8SSeigo Tanimura 	int pri;
1104512824f8SSeigo Tanimura {
1105b40ce416SJulian Elischer 	struct thread *td;
1106df8bae1dSRodney W. Grimes 
110785f190e4SAlfred Perlstein 	mtx_lock(&sellock);
110885f190e4SAlfred Perlstein 	td = sip->si_thread;
110985f190e4SAlfred Perlstein 	if ((sip->si_flags & SI_COLL) != 0) {
1110df8bae1dSRodney W. Grimes 		nselcoll++;
1111df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1112512824f8SSeigo Tanimura 		cv_broadcastpri(&selwait, pri);
1113df8bae1dSRodney W. Grimes 	}
111485f190e4SAlfred Perlstein 	if (td == NULL) {
111585f190e4SAlfred Perlstein 		mtx_unlock(&sellock);
1116b40ce416SJulian Elischer 		return;
1117b40ce416SJulian Elischer 	}
111885f190e4SAlfred Perlstein 	TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
111985f190e4SAlfred Perlstein 	sip->si_thread = NULL;
11209ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1121b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
112233a9ed9dSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
112344f3b092SJohn Baldwin 	sleepq_remove(td, &selwait);
112485f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1125df8bae1dSRodney W. Grimes }
1126265fc98fSSeigo Tanimura 
11274d77a549SAlfred Perlstein static void selectinit(void *);
1128265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1129265fc98fSSeigo Tanimura 
1130265fc98fSSeigo Tanimura /* ARGSUSED*/
1131265fc98fSSeigo Tanimura static void
1132265fc98fSSeigo Tanimura selectinit(dummy)
1133265fc98fSSeigo Tanimura 	void *dummy;
1134265fc98fSSeigo Tanimura {
1135265fc98fSSeigo Tanimura 	cv_init(&selwait, "select");
11366008862bSJohn Baldwin 	mtx_init(&sellock, "sellck", NULL, MTX_DEF);
1137265fc98fSSeigo Tanimura }
1138