xref: /freebsd/sys/kern/sys_generic.c (revision 9fc6aa0618a174f436fb1a26867c99cff4125b40)
19454b2d8SWarner Losh /*-
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 
4759fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
4769fc6aa06SPoul-Henning Kamp 		printf(
4779fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
4789fc6aa06SPoul-Henning Kamp 		    td->td_proc->p_pid, td->td_proc->p_comm, uap->com);
4799fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
4809fc6aa06SPoul-Henning Kamp 	}
481a4db4953SAlfred Perlstein 	if ((error = fget(td, uap->fd, &fp)) != 0)
482a4db4953SAlfred Perlstein 		return (error);
483ad2edad9SMatthew Dillon 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
484426da3bcSAlfred Perlstein 		fdrop(fp, td);
485426da3bcSAlfred Perlstein 		return (EBADF);
486ad2edad9SMatthew Dillon 	}
487426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
488df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
489df8bae1dSRodney W. Grimes 	case FIONCLEX:
490124e4c3bSPoul-Henning Kamp 		FILEDESC_LOCK_FAST(fdp);
491df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
492124e4c3bSPoul-Henning Kamp 		FILEDESC_UNLOCK_FAST(fdp);
493426da3bcSAlfred Perlstein 		fdrop(fp, td);
494426da3bcSAlfred Perlstein 		return (0);
495df8bae1dSRodney W. Grimes 	case FIOCLEX:
496124e4c3bSPoul-Henning Kamp 		FILEDESC_LOCK_FAST(fdp);
497df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
498124e4c3bSPoul-Henning Kamp 		FILEDESC_UNLOCK_FAST(fdp);
499426da3bcSAlfred Perlstein 		fdrop(fp, td);
500426da3bcSAlfred Perlstein 		return (0);
501df8bae1dSRodney W. Grimes 	}
502df8bae1dSRodney W. Grimes 
503df8bae1dSRodney W. Grimes 	/*
504df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
505df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
506df8bae1dSRodney W. Grimes 	 */
507df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
508ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
509ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
510ca51b19bSPoul-Henning Kamp 	    ((com & IOC_VOID) && size > 0) ||
511ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_IN | IOC_OUT)) && size == 0)) {
512426da3bcSAlfred Perlstein 		fdrop(fp, td);
513426da3bcSAlfred Perlstein 		return (ENOTTY);
514ad2edad9SMatthew Dillon 	}
515279d7226SMatthew Dillon 
516ca51b19bSPoul-Henning Kamp 	if (size > 0) {
517a163d034SWarner Losh 		memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
518df8bae1dSRodney W. Grimes 		data = memp;
519279d7226SMatthew Dillon 	} else {
520ca51b19bSPoul-Henning Kamp 		memp = NULL;
521ca51b19bSPoul-Henning Kamp 		data = (void *)&uap->data;
522279d7226SMatthew Dillon 	}
523df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
524df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
525df8bae1dSRodney W. Grimes 		if (error) {
526df8bae1dSRodney W. Grimes 			free(memp, M_IOCTLOPS);
527b40ce416SJulian Elischer 			fdrop(fp, td);
5283e15c66fSPoul-Henning Kamp 			return (error);
529df8bae1dSRodney W. Grimes 		}
530ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
531df8bae1dSRodney W. Grimes 		/*
532df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
533df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
534df8bae1dSRodney W. Grimes 		 */
535df8bae1dSRodney W. Grimes 		bzero(data, size);
536279d7226SMatthew Dillon 	}
537df8bae1dSRodney W. Grimes 
5388ccf264fSPoul-Henning Kamp 	if (com == FIONBIO) {
539426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
540bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
541df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
542df8bae1dSRodney W. Grimes 		else
543df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
544426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
5458ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
5468ccf264fSPoul-Henning Kamp 	} else if (com == FIOASYNC) {
547426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
548bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
549df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
550df8bae1dSRodney W. Grimes 		else
551df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
552426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
5538ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
554df8bae1dSRodney W. Grimes 	}
5558ccf264fSPoul-Henning Kamp 
5568ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
5578ccf264fSPoul-Henning Kamp 
5588ccf264fSPoul-Henning Kamp 	if (error == 0 && (com & IOC_OUT))
5598ccf264fSPoul-Henning Kamp 		error = copyout(data, uap->data, (u_int)size);
5608ccf264fSPoul-Henning Kamp 
561ca51b19bSPoul-Henning Kamp 	if (memp != NULL)
562df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
563b40ce416SJulian Elischer 	fdrop(fp, td);
564df8bae1dSRodney W. Grimes 	return (error);
565df8bae1dSRodney W. Grimes }
566df8bae1dSRodney W. Grimes 
56785f190e4SAlfred Perlstein /*
56885f190e4SAlfred Perlstein  * sellock and selwait are initialized in selectinit() via SYSINIT.
56985f190e4SAlfred Perlstein  */
57085f190e4SAlfred Perlstein struct mtx	sellock;
571265fc98fSSeigo Tanimura struct cv	selwait;
5729ae6d334SKelly Yancey u_int		nselcoll;	/* Select collisions since boot */
5739ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes /*
576df8bae1dSRodney W. Grimes  * Select system call.
577df8bae1dSRodney W. Grimes  */
578d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
579df8bae1dSRodney W. Grimes struct select_args {
580b08f7993SSujal Patel 	int	nd;
581df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
582df8bae1dSRodney W. Grimes 	struct	timeval *tv;
583df8bae1dSRodney W. Grimes };
584d2d3e875SBruce Evans #endif
585ad2edad9SMatthew Dillon /*
586ad2edad9SMatthew Dillon  * MPSAFE
587ad2edad9SMatthew Dillon  */
58826f9a767SRodney W. Grimes int
589b40ce416SJulian Elischer select(td, uap)
590b40ce416SJulian Elischer 	register struct thread *td;
591df8bae1dSRodney W. Grimes 	register struct select_args *uap;
592df8bae1dSRodney W. Grimes {
5938f19eb88SIan Dowse 	struct timeval tv, *tvp;
5948f19eb88SIan Dowse 	int error;
5958f19eb88SIan Dowse 
5968f19eb88SIan Dowse 	if (uap->tv != NULL) {
5978f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
5988f19eb88SIan Dowse 		if (error)
5998f19eb88SIan Dowse 			return (error);
6008f19eb88SIan Dowse 		tvp = &tv;
6018f19eb88SIan Dowse 	} else
6028f19eb88SIan Dowse 		tvp = NULL;
6038f19eb88SIan Dowse 
6048f19eb88SIan Dowse 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
6058f19eb88SIan Dowse }
6068f19eb88SIan Dowse 
6078f19eb88SIan Dowse int
6088f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
6098f19eb88SIan Dowse     fd_set *fd_ex, struct timeval *tvp)
6108f19eb88SIan Dowse {
611426da3bcSAlfred Perlstein 	struct filedesc *fdp;
612d5e4d7e1SBruce Evans 	/*
613d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
614d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
615d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
616d5e4d7e1SBruce Evans 	 * of 256.
617d5e4d7e1SBruce Evans 	 */
618d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
619eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
62000af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
6219ae6d334SKelly Yancey 	int error, timo;
6229ae6d334SKelly Yancey 	u_int ncoll, nbufbytes, ncpbytes, nfdbits;
623df8bae1dSRodney W. Grimes 
6248f19eb88SIan Dowse 	if (nd < 0)
625acbfbfeaSSujal Patel 		return (EINVAL);
626426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
627db446e30SPoul-Henning Kamp 
628124e4c3bSPoul-Henning Kamp 	FILEDESC_LOCK_FAST(fdp);
629ad2edad9SMatthew Dillon 
6308f19eb88SIan Dowse 	if (nd > td->td_proc->p_fd->fd_nfiles)
6318f19eb88SIan Dowse 		nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
632124e4c3bSPoul-Henning Kamp 	FILEDESC_UNLOCK_FAST(fdp);
633b08f7993SSujal Patel 
634d5e4d7e1SBruce Evans 	/*
635d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
636d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
637d5e4d7e1SBruce Evans 	 */
6388f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
639d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
640d5e4d7e1SBruce Evans 	nbufbytes = 0;
6418f19eb88SIan Dowse 	if (fd_in != NULL)
642d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
6438f19eb88SIan Dowse 	if (fd_ou != NULL)
644d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
6458f19eb88SIan Dowse 	if (fd_ex != NULL)
646d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
647d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
648d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
649d5e4d7e1SBruce Evans 	else
650a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
651b08f7993SSujal Patel 
652b08f7993SSujal Patel 	/*
653d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
654d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
655d5e4d7e1SBruce Evans 	 * together.
656b08f7993SSujal Patel 	 */
657d5e4d7e1SBruce Evans 	sbp = selbits;
658df8bae1dSRodney W. Grimes #define	getbits(name, x) \
659d5e4d7e1SBruce Evans 	do {								\
6608f19eb88SIan Dowse 		if (name == NULL)					\
661d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
662d5e4d7e1SBruce Evans 		else {							\
663d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
664d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
665d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
6668f19eb88SIan Dowse 			error = copyin(name, ibits[x], ncpbytes);	\
667265fc98fSSeigo Tanimura 			if (error != 0)					\
66885f190e4SAlfred Perlstein 				goto done_nosellock;			\
669e04ac2feSJohn Baldwin 		}							\
670d5e4d7e1SBruce Evans 	} while (0)
6718f19eb88SIan Dowse 	getbits(fd_in, 0);
6728f19eb88SIan Dowse 	getbits(fd_ou, 1);
6738f19eb88SIan Dowse 	getbits(fd_ex, 2);
674df8bae1dSRodney W. Grimes #undef	getbits
675d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
676d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
677df8bae1dSRodney W. Grimes 
6788f19eb88SIan Dowse 	if (tvp != NULL) {
6798f19eb88SIan Dowse 		atv = *tvp;
680df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
681df8bae1dSRodney W. Grimes 			error = EINVAL;
68285f190e4SAlfred Perlstein 			goto done_nosellock;
683df8bae1dSRodney W. Grimes 		}
684c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
68500af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
6869c386f6bSJohn Baldwin 	} else {
68700af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
6889c386f6bSJohn Baldwin 		atv.tv_usec = 0;
6899c386f6bSJohn Baldwin 	}
69000af9731SPoul-Henning Kamp 	timo = 0;
6912149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
69285f190e4SAlfred Perlstein 	mtx_lock(&sellock);
693df8bae1dSRodney W. Grimes retry:
694df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
695fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
696b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
697fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
69885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
69985f190e4SAlfred Perlstein 
7008f19eb88SIan Dowse 	error = selscan(td, ibits, obits, nd);
70185f190e4SAlfred Perlstein 	mtx_lock(&sellock);
702b40ce416SJulian Elischer 	if (error || td->td_retval[0])
703df8bae1dSRodney W. Grimes 		goto done;
7044da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
705c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
70685f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
707df8bae1dSRodney W. Grimes 			goto done;
70800af9731SPoul-Henning Kamp 		ttv = atv;
70900af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
71000af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
71100af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
712df8bae1dSRodney W. Grimes 	}
71385f190e4SAlfred Perlstein 
71485f190e4SAlfred Perlstein 	/*
71585f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
71685f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of
71785f190e4SAlfred Perlstein 	 * collisions and rescan the file descriptors if
71885f190e4SAlfred Perlstein 	 * necessary.
71985f190e4SAlfred Perlstein 	 */
720fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
72185f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
72285f190e4SAlfred Perlstein 		mtx_unlock_spin(&sched_lock);
72385f190e4SAlfred Perlstein 		goto retry;
72485f190e4SAlfred Perlstein 	}
725fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
726bfbbc4aaSJason Evans 
727265fc98fSSeigo Tanimura 	if (timo > 0)
72885f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
729265fc98fSSeigo Tanimura 	else
73085f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
731bfbbc4aaSJason Evans 
732df8bae1dSRodney W. Grimes 	if (error == 0)
733df8bae1dSRodney W. Grimes 		goto retry;
734265fc98fSSeigo Tanimura 
735df8bae1dSRodney W. Grimes done:
73685f190e4SAlfred Perlstein 	clear_selinfo_list(td);
737fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
738b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
739fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
74085f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
74185f190e4SAlfred Perlstein 
74285f190e4SAlfred Perlstein done_nosellock:
743df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
744df8bae1dSRodney W. Grimes 	if (error == ERESTART)
745df8bae1dSRodney W. Grimes 		error = EINTR;
746df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
747df8bae1dSRodney W. Grimes 		error = 0;
748df8bae1dSRodney W. Grimes #define	putbits(name, x) \
7498f19eb88SIan Dowse 	if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
750df8bae1dSRodney W. Grimes 		error = error2;
751df8bae1dSRodney W. Grimes 	if (error == 0) {
752df8bae1dSRodney W. Grimes 		int error2;
753df8bae1dSRodney W. Grimes 
7548f19eb88SIan Dowse 		putbits(fd_in, 0);
7558f19eb88SIan Dowse 		putbits(fd_ou, 1);
7568f19eb88SIan Dowse 		putbits(fd_ex, 2);
757df8bae1dSRodney W. Grimes #undef putbits
758df8bae1dSRodney W. Grimes 	}
759d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
760d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
761ad2edad9SMatthew Dillon 
762df8bae1dSRodney W. Grimes 	return (error);
763df8bae1dSRodney W. Grimes }
764df8bae1dSRodney W. Grimes 
765265fc98fSSeigo Tanimura static int
766b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
767b40ce416SJulian Elischer 	struct thread *td;
768b08f7993SSujal Patel 	fd_mask **ibits, **obits;
769cb226aaaSPoul-Henning Kamp 	int nfd;
770df8bae1dSRodney W. Grimes {
771f082218cSPeter Wemm 	int msk, i, fd;
772f082218cSPeter Wemm 	fd_mask bits;
773df8bae1dSRodney W. Grimes 	struct file *fp;
774df8bae1dSRodney W. Grimes 	int n = 0;
7752087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
77642d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
777eb209311SAlfred Perlstein 	struct filedesc *fdp = td->td_proc->p_fd;
778df8bae1dSRodney W. Grimes 
779eb209311SAlfred Perlstein 	FILEDESC_LOCK(fdp);
780df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
781d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
782d5e4d7e1SBruce Evans 			continue;
783df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
784b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
785f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
786f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
787f082218cSPeter Wemm 				if (!(bits & 1))
788f082218cSPeter Wemm 					continue;
789eb209311SAlfred Perlstein 				if ((fp = fget_locked(fdp, fd)) == NULL) {
790eb209311SAlfred Perlstein 					FILEDESC_UNLOCK(fdp);
791df8bae1dSRodney W. Grimes 					return (EBADF);
792eb209311SAlfred Perlstein 				}
793ea6027a8SRobert Watson 				if (fo_poll(fp, flag[msk], td->td_ucred,
794ea6027a8SRobert Watson 				    td)) {
795b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
796f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
797df8bae1dSRodney W. Grimes 					n++;
798df8bae1dSRodney W. Grimes 				}
799df8bae1dSRodney W. Grimes 			}
800df8bae1dSRodney W. Grimes 		}
801df8bae1dSRodney W. Grimes 	}
802eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
803b40ce416SJulian Elischer 	td->td_retval[0] = n;
804df8bae1dSRodney W. Grimes 	return (0);
805df8bae1dSRodney W. Grimes }
806df8bae1dSRodney W. Grimes 
80742d11757SPeter Wemm /*
80842d11757SPeter Wemm  * Poll system call.
80942d11757SPeter Wemm  */
81042d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
81142d11757SPeter Wemm struct poll_args {
81242d11757SPeter Wemm 	struct pollfd *fds;
81342d11757SPeter Wemm 	u_int	nfds;
81442d11757SPeter Wemm 	int	timeout;
81542d11757SPeter Wemm };
81642d11757SPeter Wemm #endif
817ad2edad9SMatthew Dillon /*
818ad2edad9SMatthew Dillon  * MPSAFE
819ad2edad9SMatthew Dillon  */
82042d11757SPeter Wemm int
821b40ce416SJulian Elischer poll(td, uap)
822b40ce416SJulian Elischer 	struct thread *td;
823ea0237edSJonathan Lemon 	struct poll_args *uap;
82442d11757SPeter Wemm {
8252580f4e5SAndre Oppermann 	struct pollfd *bits;
8262580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
82700af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
8289ae6d334SKelly Yancey 	int error = 0, timo;
8299ae6d334SKelly Yancey 	u_int ncoll, nfds;
83042d11757SPeter Wemm 	size_t ni;
83142d11757SPeter Wemm 
832d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
833ad2edad9SMatthew Dillon 
8345d8dd01dSRobert Watson 	/*
8352bd5ac33SPeter Wemm 	 * This is kinda bogus.  We have fd limits, but that is not
8362bd5ac33SPeter Wemm 	 * really related to the size of the pollfd array.  Make sure
8372bd5ac33SPeter Wemm 	 * we let the process use at least FD_SETSIZE entries and at
8382bd5ac33SPeter Wemm 	 * least enough for the current limits.  We want to be reasonably
8392bd5ac33SPeter Wemm 	 * safe, but not overly restrictive.
84089b71647SPeter Wemm 	 */
84191d5354aSJohn Baldwin 	PROC_LOCK(td->td_proc);
84291d5354aSJohn Baldwin 	if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) &&
843b40ce416SJulian Elischer 	    (nfds > FD_SETSIZE)) {
84491d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
845ad2edad9SMatthew Dillon 		error = EINVAL;
846ad2edad9SMatthew Dillon 		goto done2;
847ad2edad9SMatthew Dillon 	}
84891d5354aSJohn Baldwin 	PROC_UNLOCK(td->td_proc);
84989b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
85042d11757SPeter Wemm 	if (ni > sizeof(smallbits))
851a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
85242d11757SPeter Wemm 	else
85342d11757SPeter Wemm 		bits = smallbits;
854d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
85542d11757SPeter Wemm 	if (error)
85685f190e4SAlfred Perlstein 		goto done_nosellock;
857d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
858d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
859d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
86042d11757SPeter Wemm 		if (itimerfix(&atv)) {
86142d11757SPeter Wemm 			error = EINVAL;
86285f190e4SAlfred Perlstein 			goto done_nosellock;
86342d11757SPeter Wemm 		}
864c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
86500af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
8669c386f6bSJohn Baldwin 	} else {
86700af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
8689c386f6bSJohn Baldwin 		atv.tv_usec = 0;
8699c386f6bSJohn Baldwin 	}
87000af9731SPoul-Henning Kamp 	timo = 0;
8712149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
87285f190e4SAlfred Perlstein 	mtx_lock(&sellock);
87342d11757SPeter Wemm retry:
87442d11757SPeter Wemm 	ncoll = nselcoll;
875fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
876b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
877fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
87885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
87985f190e4SAlfred Perlstein 
8802580f4e5SAndre Oppermann 	error = pollscan(td, bits, nfds);
88185f190e4SAlfred Perlstein 	mtx_lock(&sellock);
882b40ce416SJulian Elischer 	if (error || td->td_retval[0])
88342d11757SPeter Wemm 		goto done;
8844da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
885c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
88685f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
88742d11757SPeter Wemm 			goto done;
88800af9731SPoul-Henning Kamp 		ttv = atv;
88900af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
89000af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
89100af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
89242d11757SPeter Wemm 	}
89385f190e4SAlfred Perlstein 	/*
89485f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
89585f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of collisions
89685f190e4SAlfred Perlstein 	 * and rescan the file descriptors if necessary.
89785f190e4SAlfred Perlstein 	 */
898fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
89985f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
900fea2ab83SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
90185f190e4SAlfred Perlstein 		goto retry;
90285f190e4SAlfred Perlstein 	}
90385f190e4SAlfred Perlstein 	mtx_unlock_spin(&sched_lock);
90485f190e4SAlfred Perlstein 
905265fc98fSSeigo Tanimura 	if (timo > 0)
90685f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
907265fc98fSSeigo Tanimura 	else
90885f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
90985f190e4SAlfred Perlstein 
91042d11757SPeter Wemm 	if (error == 0)
91142d11757SPeter Wemm 		goto retry;
912265fc98fSSeigo Tanimura 
91342d11757SPeter Wemm done:
91485f190e4SAlfred Perlstein 	clear_selinfo_list(td);
915fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
916b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
917fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
91885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
91985f190e4SAlfred Perlstein 
92085f190e4SAlfred Perlstein done_nosellock:
92142d11757SPeter Wemm 	/* poll is not restarted after signals... */
92242d11757SPeter Wemm 	if (error == ERESTART)
92342d11757SPeter Wemm 		error = EINTR;
92442d11757SPeter Wemm 	if (error == EWOULDBLOCK)
92542d11757SPeter Wemm 		error = 0;
92642d11757SPeter Wemm 	if (error == 0) {
927d1e405c5SAlfred Perlstein 		error = copyout(bits, uap->fds, ni);
92842d11757SPeter Wemm 		if (error)
92942d11757SPeter Wemm 			goto out;
93042d11757SPeter Wemm 	}
93142d11757SPeter Wemm out:
93242d11757SPeter Wemm 	if (ni > sizeof(smallbits))
93342d11757SPeter Wemm 		free(bits, M_TEMP);
934ad2edad9SMatthew Dillon done2:
93542d11757SPeter Wemm 	return (error);
93642d11757SPeter Wemm }
93742d11757SPeter Wemm 
93842d11757SPeter Wemm static int
939b40ce416SJulian Elischer pollscan(td, fds, nfd)
940b40ce416SJulian Elischer 	struct thread *td;
94142d11757SPeter Wemm 	struct pollfd *fds;
942ea0237edSJonathan Lemon 	u_int nfd;
94342d11757SPeter Wemm {
944b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
94542d11757SPeter Wemm 	int i;
94642d11757SPeter Wemm 	struct file *fp;
94742d11757SPeter Wemm 	int n = 0;
94842d11757SPeter Wemm 
949426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
950eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
951337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
95242d11757SPeter Wemm 			fds->revents = POLLNVAL;
95342d11757SPeter Wemm 			n++;
954337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
955337c9691SJordan K. Hubbard 			fds->revents = 0;
95642d11757SPeter Wemm 		} else {
95742d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
958279d7226SMatthew Dillon 			if (fp == NULL) {
95942d11757SPeter Wemm 				fds->revents = POLLNVAL;
96042d11757SPeter Wemm 				n++;
96142d11757SPeter Wemm 			} else {
9622087c896SBruce Evans 				/*
9632087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
9642087c896SBruce Evans 				 * POLLERR if appropriate.
9652087c896SBruce Evans 				 */
96613ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
967ea6027a8SRobert Watson 				    td->td_ucred, td);
96842d11757SPeter Wemm 				if (fds->revents != 0)
96942d11757SPeter Wemm 					n++;
97042d11757SPeter Wemm 			}
97142d11757SPeter Wemm 		}
97242d11757SPeter Wemm 	}
973eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
974b40ce416SJulian Elischer 	td->td_retval[0] = n;
97542d11757SPeter Wemm 	return (0);
97642d11757SPeter Wemm }
97742d11757SPeter Wemm 
97842d11757SPeter Wemm /*
97942d11757SPeter Wemm  * OpenBSD poll system call.
98042d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
98142d11757SPeter Wemm  */
98242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
98342d11757SPeter Wemm struct openbsd_poll_args {
98442d11757SPeter Wemm 	struct pollfd *fds;
98542d11757SPeter Wemm 	u_int	nfds;
98642d11757SPeter Wemm 	int	timeout;
98742d11757SPeter Wemm };
98842d11757SPeter Wemm #endif
989ad2edad9SMatthew Dillon /*
990ad2edad9SMatthew Dillon  * MPSAFE
991ad2edad9SMatthew Dillon  */
99242d11757SPeter Wemm int
993b40ce416SJulian Elischer openbsd_poll(td, uap)
994b40ce416SJulian Elischer 	register struct thread *td;
99542d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
99642d11757SPeter Wemm {
997b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
99842d11757SPeter Wemm }
99942d11757SPeter Wemm 
100085f190e4SAlfred Perlstein /*
100185f190e4SAlfred Perlstein  * Remove the references to the thread from all of the objects
100285f190e4SAlfred Perlstein  * we were polling.
100385f190e4SAlfred Perlstein  *
100485f190e4SAlfred Perlstein  * This code assumes that the underlying owner of the selinfo
100585f190e4SAlfred Perlstein  * structure will hold sellock before it changes it, and that
100685f190e4SAlfred Perlstein  * it will unlink itself from our list if it goes away.
100785f190e4SAlfred Perlstein  */
100885f190e4SAlfred Perlstein void
100985f190e4SAlfred Perlstein clear_selinfo_list(td)
101085f190e4SAlfred Perlstein 	struct thread *td;
101185f190e4SAlfred Perlstein {
101285f190e4SAlfred Perlstein 	struct selinfo *si;
101385f190e4SAlfred Perlstein 
101485f190e4SAlfred Perlstein 	mtx_assert(&sellock, MA_OWNED);
101585f190e4SAlfred Perlstein 	TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
101685f190e4SAlfred Perlstein 		si->si_thread = NULL;
101785f190e4SAlfred Perlstein 	TAILQ_INIT(&td->td_selq);
101885f190e4SAlfred Perlstein }
101985f190e4SAlfred Perlstein 
1020df8bae1dSRodney W. Grimes /*
1021df8bae1dSRodney W. Grimes  * Record a select request.
1022df8bae1dSRodney W. Grimes  */
1023df8bae1dSRodney W. Grimes void
1024df8bae1dSRodney W. Grimes selrecord(selector, sip)
1025b40ce416SJulian Elischer 	struct thread *selector;
1026df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1027df8bae1dSRodney W. Grimes {
1028df8bae1dSRodney W. Grimes 
102985f190e4SAlfred Perlstein 	mtx_lock(&sellock);
103085f190e4SAlfred Perlstein 	/*
1031b605b54cSAlfred Perlstein 	 * If the selinfo's thread pointer is NULL then take ownership of it.
1032b605b54cSAlfred Perlstein 	 *
1033b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and it points to another
1034b605b54cSAlfred Perlstein 	 * thread, then we have a collision.
1035b605b54cSAlfred Perlstein 	 *
1036b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and points back to us then leave
1037b605b54cSAlfred Perlstein 	 * it alone as we've already added pointed it at us and added it to
1038b605b54cSAlfred Perlstein 	 * our list.
103985f190e4SAlfred Perlstein 	 */
104085f190e4SAlfred Perlstein 	if (sip->si_thread == NULL) {
1041b40ce416SJulian Elischer 		sip->si_thread = selector;
104285f190e4SAlfred Perlstein 		TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
104385f190e4SAlfred Perlstein 	} else if (sip->si_thread != selector) {
104485f190e4SAlfred Perlstein 		sip->si_flags |= SI_COLL;
104585f190e4SAlfred Perlstein 	}
104685f190e4SAlfred Perlstein 
104785f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1048df8bae1dSRodney W. Grimes }
1049df8bae1dSRodney W. Grimes 
1050512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1051df8bae1dSRodney W. Grimes void
1052df8bae1dSRodney W. Grimes selwakeup(sip)
105385f190e4SAlfred Perlstein 	struct selinfo *sip;
1054df8bae1dSRodney W. Grimes {
1055512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1056512824f8SSeigo Tanimura }
1057512824f8SSeigo Tanimura 
1058512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1059512824f8SSeigo Tanimura void
1060512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1061512824f8SSeigo Tanimura 	struct selinfo *sip;
1062512824f8SSeigo Tanimura 	int pri;
1063512824f8SSeigo Tanimura {
1064512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1065512824f8SSeigo Tanimura }
1066512824f8SSeigo Tanimura 
1067512824f8SSeigo Tanimura /*
1068512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1069512824f8SSeigo Tanimura  */
1070512824f8SSeigo Tanimura static void
1071512824f8SSeigo Tanimura doselwakeup(sip, pri)
1072512824f8SSeigo Tanimura 	struct selinfo *sip;
1073512824f8SSeigo Tanimura 	int pri;
1074512824f8SSeigo Tanimura {
1075b40ce416SJulian Elischer 	struct thread *td;
1076df8bae1dSRodney W. Grimes 
107785f190e4SAlfred Perlstein 	mtx_lock(&sellock);
107885f190e4SAlfred Perlstein 	td = sip->si_thread;
107985f190e4SAlfred Perlstein 	if ((sip->si_flags & SI_COLL) != 0) {
1080df8bae1dSRodney W. Grimes 		nselcoll++;
1081df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1082512824f8SSeigo Tanimura 		cv_broadcastpri(&selwait, pri);
1083df8bae1dSRodney W. Grimes 	}
108485f190e4SAlfred Perlstein 	if (td == NULL) {
108585f190e4SAlfred Perlstein 		mtx_unlock(&sellock);
1086b40ce416SJulian Elischer 		return;
1087b40ce416SJulian Elischer 	}
108885f190e4SAlfred Perlstein 	TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
108985f190e4SAlfred Perlstein 	sip->si_thread = NULL;
10909ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1091b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
109233a9ed9dSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
109344f3b092SJohn Baldwin 	sleepq_remove(td, &selwait);
109485f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1095df8bae1dSRodney W. Grimes }
1096265fc98fSSeigo Tanimura 
10974d77a549SAlfred Perlstein static void selectinit(void *);
1098265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1099265fc98fSSeigo Tanimura 
1100265fc98fSSeigo Tanimura /* ARGSUSED*/
1101265fc98fSSeigo Tanimura static void
1102265fc98fSSeigo Tanimura selectinit(dummy)
1103265fc98fSSeigo Tanimura 	void *dummy;
1104265fc98fSSeigo Tanimura {
1105265fc98fSSeigo Tanimura 	cv_init(&selwait, "select");
11066008862bSJohn Baldwin 	mtx_init(&sellock, "sellck", NULL, MTX_DEF);
1107265fc98fSSeigo Tanimura }
1108