xref: /freebsd/sys/kern/sys_generic.c (revision 431f89061443375688abea4e01c0b01936f0f905)
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 
402de92a38SPeter Wemm #include "opt_compat.h"
41db6a20e2SGarrett Wollman #include "opt_ktrace.h"
42db6a20e2SGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45d2d3e875SBruce Evans #include <sys/sysproto.h>
46df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4720982410SBruce Evans #include <sys/filio.h>
483ac4d1efSBruce Evans #include <sys/fcntl.h>
49df8bae1dSRodney W. Grimes #include <sys/file.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
51797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53df8bae1dSRodney W. Grimes #include <sys/uio.h>
54df8bae1dSRodney W. Grimes #include <sys/kernel.h>
55104a9b7eSAlexander Kabaev #include <sys/limits.h>
56df8bae1dSRodney W. Grimes #include <sys/malloc.h>
5742d11757SPeter Wemm #include <sys/poll.h>
5889b71647SPeter Wemm #include <sys/resourcevar.h>
590a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6044f3b092SJohn Baldwin #include <sys/sleepqueue.h>
618f19eb88SIan Dowse #include <sys/syscallsubr.h>
628cb96f20SPeter Wemm #include <sys/sysctl.h>
6342d11757SPeter Wemm #include <sys/sysent.h>
649bbee259SAndrey A. Chernov #include <sys/vnode.h>
65279d7226SMatthew Dillon #include <sys/bio.h>
66279d7226SMatthew Dillon #include <sys/buf.h>
67265fc98fSSeigo Tanimura #include <sys/condvar.h>
68df8bae1dSRodney W. Grimes #ifdef KTRACE
69df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
70df8bae1dSRodney W. Grimes #endif
71df8bae1dSRodney W. Grimes 
72a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
73a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
74a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7555166637SPoul-Henning Kamp 
76bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
77bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
78bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
79bcd9e0ddSJohn Baldwin 		    off_t, int);
80bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
81bcd9e0ddSJohn Baldwin 		    off_t, int);
82512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
838fe387abSDmitrij Tejblum 
84d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
85df8bae1dSRodney W. Grimes struct read_args {
86df8bae1dSRodney W. Grimes 	int	fd;
87134e06feSBruce Evans 	void	*buf;
88134e06feSBruce Evans 	size_t	nbyte;
89df8bae1dSRodney W. Grimes };
90d2d3e875SBruce Evans #endif
9126f9a767SRodney W. Grimes int
92b40ce416SJulian Elischer read(td, uap)
93b40ce416SJulian Elischer 	struct thread *td;
94b064d43dSMatthew Dillon 	struct read_args *uap;
95df8bae1dSRodney W. Grimes {
96bcd9e0ddSJohn Baldwin 	struct uio auio;
97bcd9e0ddSJohn Baldwin 	struct iovec aiov;
98279d7226SMatthew Dillon 	int error;
99df8bae1dSRodney W. Grimes 
100bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
101bcd9e0ddSJohn Baldwin 		return (EINVAL);
102bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
103bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
104bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
105bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
106bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
107bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
108bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
109279d7226SMatthew Dillon 	return(error);
110df8bae1dSRodney W. Grimes }
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes /*
113bcd9e0ddSJohn Baldwin  * Positioned read system call
1144160ccd9SAlan Cox  */
1154160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1164160ccd9SAlan Cox struct pread_args {
1174160ccd9SAlan Cox 	int	fd;
1184160ccd9SAlan Cox 	void	*buf;
1194160ccd9SAlan Cox 	size_t	nbyte;
1208fe387abSDmitrij Tejblum 	int	pad;
1214160ccd9SAlan Cox 	off_t	offset;
1224160ccd9SAlan Cox };
1234160ccd9SAlan Cox #endif
1244160ccd9SAlan Cox int
125b40ce416SJulian Elischer pread(td, uap)
126b40ce416SJulian Elischer 	struct thread *td;
127b064d43dSMatthew Dillon 	struct pread_args *uap;
1284160ccd9SAlan Cox {
1294160ccd9SAlan Cox 	struct uio auio;
1304160ccd9SAlan Cox 	struct iovec aiov;
131bcd9e0ddSJohn Baldwin 	int error;
1324160ccd9SAlan Cox 
133bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
134bcd9e0ddSJohn Baldwin 		return (EINVAL);
135bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
136bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
1374160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1384160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
139bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
1404160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
141bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
1424160ccd9SAlan Cox 	return(error);
1434160ccd9SAlan Cox }
1444160ccd9SAlan Cox 
145c2815ad5SPeter Wemm int
146c2815ad5SPeter Wemm freebsd6_pread(td, uap)
147c2815ad5SPeter Wemm 	struct thread *td;
148c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
149c2815ad5SPeter Wemm {
150c2815ad5SPeter Wemm 	struct pread_args oargs;
151c2815ad5SPeter Wemm 
152c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
153c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
154c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
155c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
156c2815ad5SPeter Wemm 	return (pread(td, &oargs));
157c2815ad5SPeter Wemm }
158c2815ad5SPeter Wemm 
1594160ccd9SAlan Cox /*
160df8bae1dSRodney W. Grimes  * Scatter read system call.
161df8bae1dSRodney W. Grimes  */
162d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
163df8bae1dSRodney W. Grimes struct readv_args {
1647147b19dSBruce Evans 	int	fd;
165df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
166df8bae1dSRodney W. Grimes 	u_int	iovcnt;
167df8bae1dSRodney W. Grimes };
168d2d3e875SBruce Evans #endif
16926f9a767SRodney W. Grimes int
170552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap)
171df8bae1dSRodney W. Grimes {
172b88ec951SJohn Baldwin 	struct uio *auio;
173b88ec951SJohn Baldwin 	int error;
174b88ec951SJohn Baldwin 
175b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
176b88ec951SJohn Baldwin 	if (error)
177b88ec951SJohn Baldwin 		return (error);
178b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
179b88ec951SJohn Baldwin 	free(auio, M_IOV);
180b88ec951SJohn Baldwin 	return (error);
181b88ec951SJohn Baldwin }
182b88ec951SJohn Baldwin 
183b88ec951SJohn Baldwin int
184b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
185b88ec951SJohn Baldwin {
186b064d43dSMatthew Dillon 	struct file *fp;
187bcd9e0ddSJohn Baldwin 	int error;
188bcd9e0ddSJohn Baldwin 
189bcd9e0ddSJohn Baldwin 	error = fget_read(td, fd, &fp);
190bcd9e0ddSJohn Baldwin 	if (error)
191bcd9e0ddSJohn Baldwin 		return (error);
192bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
193bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
194bcd9e0ddSJohn Baldwin 	return (error);
195bcd9e0ddSJohn Baldwin }
196bcd9e0ddSJohn Baldwin 
197bcd9e0ddSJohn Baldwin /*
198bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
199bcd9e0ddSJohn Baldwin  */
200bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
201bcd9e0ddSJohn Baldwin struct preadv_args {
202bcd9e0ddSJohn Baldwin 	int	fd;
203bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
204bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
205bcd9e0ddSJohn Baldwin 	off_t	offset;
206bcd9e0ddSJohn Baldwin };
207bcd9e0ddSJohn Baldwin #endif
208bcd9e0ddSJohn Baldwin int
209bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap)
210bcd9e0ddSJohn Baldwin {
211bcd9e0ddSJohn Baldwin 	struct uio *auio;
212bcd9e0ddSJohn Baldwin 	int error;
213bcd9e0ddSJohn Baldwin 
214bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
215bcd9e0ddSJohn Baldwin 	if (error)
216bcd9e0ddSJohn Baldwin 		return (error);
217bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
218bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
219bcd9e0ddSJohn Baldwin 	return (error);
220bcd9e0ddSJohn Baldwin }
221bcd9e0ddSJohn Baldwin 
222bcd9e0ddSJohn Baldwin int
223bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
224bcd9e0ddSJohn Baldwin 	struct thread *td;
225bcd9e0ddSJohn Baldwin 	int fd;
226bcd9e0ddSJohn Baldwin 	struct uio *auio;
227bcd9e0ddSJohn Baldwin 	off_t offset;
228bcd9e0ddSJohn Baldwin {
229bcd9e0ddSJohn Baldwin 	struct file *fp;
230bcd9e0ddSJohn Baldwin 	int error;
231bcd9e0ddSJohn Baldwin 
232bcd9e0ddSJohn Baldwin 	error = fget_read(td, fd, &fp);
233bcd9e0ddSJohn Baldwin 	if (error)
234bcd9e0ddSJohn Baldwin 		return (error);
235bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
236bcd9e0ddSJohn Baldwin 		error = ESPIPE;
237bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
238bcd9e0ddSJohn Baldwin 		error = EINVAL;
239bcd9e0ddSJohn Baldwin 	else
240bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
241bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
242bcd9e0ddSJohn Baldwin 	return (error);
243bcd9e0ddSJohn Baldwin }
244bcd9e0ddSJohn Baldwin 
245bcd9e0ddSJohn Baldwin /*
246bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
247bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
248bcd9e0ddSJohn Baldwin  */
249bcd9e0ddSJohn Baldwin static int
250bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
251bcd9e0ddSJohn Baldwin 	struct thread *td;
252bcd9e0ddSJohn Baldwin 	int fd;
253bcd9e0ddSJohn Baldwin 	struct file *fp;
254bcd9e0ddSJohn Baldwin 	struct uio *auio;
255bcd9e0ddSJohn Baldwin 	off_t offset;
256bcd9e0ddSJohn Baldwin 	int flags;
257bcd9e0ddSJohn Baldwin {
258bcd9e0ddSJohn Baldwin 	ssize_t cnt;
25982641acdSAlan Cox 	int error;
260df8bae1dSRodney W. Grimes #ifdef KTRACE
261552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
262df8bae1dSRodney W. Grimes #endif
263df8bae1dSRodney W. Grimes 
2644f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
2654f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
2664f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
2674f8d23d6SPoul-Henning Kamp 		return(0);
2684f8d23d6SPoul-Henning Kamp 	}
269552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
270bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
271552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
272df8bae1dSRodney W. Grimes #ifdef KTRACE
273552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
274552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
275df8bae1dSRodney W. Grimes #endif
276552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
277bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
278552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
279df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
280df8bae1dSRodney W. Grimes 			error = 0;
281279d7226SMatthew Dillon 	}
282552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
283df8bae1dSRodney W. Grimes #ifdef KTRACE
284552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
285552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
286b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
287df8bae1dSRodney W. Grimes 	}
288df8bae1dSRodney W. Grimes #endif
289b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
290df8bae1dSRodney W. Grimes 	return (error);
291df8bae1dSRodney W. Grimes }
292df8bae1dSRodney W. Grimes 
293d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
294df8bae1dSRodney W. Grimes struct write_args {
295df8bae1dSRodney W. Grimes 	int	fd;
296134e06feSBruce Evans 	const void *buf;
297134e06feSBruce Evans 	size_t	nbyte;
298df8bae1dSRodney W. Grimes };
299d2d3e875SBruce Evans #endif
30026f9a767SRodney W. Grimes int
301b40ce416SJulian Elischer write(td, uap)
302b40ce416SJulian Elischer 	struct thread *td;
303b064d43dSMatthew Dillon 	struct write_args *uap;
304df8bae1dSRodney W. Grimes {
305bcd9e0ddSJohn Baldwin 	struct uio auio;
306bcd9e0ddSJohn Baldwin 	struct iovec aiov;
307279d7226SMatthew Dillon 	int error;
308df8bae1dSRodney W. Grimes 
309bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
310bcd9e0ddSJohn Baldwin 		return (EINVAL);
311bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
312bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
313bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
314bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
315bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
316bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
317bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
318279d7226SMatthew Dillon 	return(error);
319df8bae1dSRodney W. Grimes }
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes /*
3220c14ff0eSRobert Watson  * Positioned write system call.
3234160ccd9SAlan Cox  */
3244160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3254160ccd9SAlan Cox struct pwrite_args {
3264160ccd9SAlan Cox 	int	fd;
3274160ccd9SAlan Cox 	const void *buf;
3284160ccd9SAlan Cox 	size_t	nbyte;
3298fe387abSDmitrij Tejblum 	int	pad;
3304160ccd9SAlan Cox 	off_t	offset;
3314160ccd9SAlan Cox };
3324160ccd9SAlan Cox #endif
3334160ccd9SAlan Cox int
334b40ce416SJulian Elischer pwrite(td, uap)
335b40ce416SJulian Elischer 	struct thread *td;
336b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3374160ccd9SAlan Cox {
3384160ccd9SAlan Cox 	struct uio auio;
3394160ccd9SAlan Cox 	struct iovec aiov;
340bcd9e0ddSJohn Baldwin 	int error;
3414160ccd9SAlan Cox 
342bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
343bcd9e0ddSJohn Baldwin 		return (EINVAL);
344bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
345bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
3464160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3474160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
348bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
3494160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
350bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
3514160ccd9SAlan Cox 	return(error);
3524160ccd9SAlan Cox }
3534160ccd9SAlan Cox 
354c2815ad5SPeter Wemm int
355c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
356c2815ad5SPeter Wemm 	struct thread *td;
357c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
358c2815ad5SPeter Wemm {
359c2815ad5SPeter Wemm 	struct pwrite_args oargs;
360c2815ad5SPeter Wemm 
361c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
362c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
363c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
364c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
365c2815ad5SPeter Wemm 	return (pwrite(td, &oargs));
366c2815ad5SPeter Wemm }
367c2815ad5SPeter Wemm 
3684160ccd9SAlan Cox /*
3690c14ff0eSRobert Watson  * Gather write system call.
370df8bae1dSRodney W. Grimes  */
371d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
372df8bae1dSRodney W. Grimes struct writev_args {
373df8bae1dSRodney W. Grimes 	int	fd;
374df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
375df8bae1dSRodney W. Grimes 	u_int	iovcnt;
376df8bae1dSRodney W. Grimes };
377d2d3e875SBruce Evans #endif
37826f9a767SRodney W. Grimes int
379552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap)
380df8bae1dSRodney W. Grimes {
381b88ec951SJohn Baldwin 	struct uio *auio;
382b88ec951SJohn Baldwin 	int error;
383b88ec951SJohn Baldwin 
384b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
385b88ec951SJohn Baldwin 	if (error)
386b88ec951SJohn Baldwin 		return (error);
387b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
388b88ec951SJohn Baldwin 	free(auio, M_IOV);
389b88ec951SJohn Baldwin 	return (error);
390b88ec951SJohn Baldwin }
391b88ec951SJohn Baldwin 
392b88ec951SJohn Baldwin int
393b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
394b88ec951SJohn Baldwin {
395b064d43dSMatthew Dillon 	struct file *fp;
396bcd9e0ddSJohn Baldwin 	int error;
397bcd9e0ddSJohn Baldwin 
398bcd9e0ddSJohn Baldwin 	error = fget_write(td, fd, &fp);
399bcd9e0ddSJohn Baldwin 	if (error)
400af56abaaSJohn Baldwin 		return (error);
401bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
402bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
403bcd9e0ddSJohn Baldwin 	return (error);
404bcd9e0ddSJohn Baldwin }
405bcd9e0ddSJohn Baldwin 
406bcd9e0ddSJohn Baldwin /*
4070c14ff0eSRobert Watson  * Gather positioned write system call.
408bcd9e0ddSJohn Baldwin  */
409bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
410bcd9e0ddSJohn Baldwin struct pwritev_args {
411bcd9e0ddSJohn Baldwin 	int	fd;
412bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
413bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
414bcd9e0ddSJohn Baldwin 	off_t	offset;
415bcd9e0ddSJohn Baldwin };
416bcd9e0ddSJohn Baldwin #endif
417bcd9e0ddSJohn Baldwin int
418bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap)
419bcd9e0ddSJohn Baldwin {
420bcd9e0ddSJohn Baldwin 	struct uio *auio;
421bcd9e0ddSJohn Baldwin 	int error;
422bcd9e0ddSJohn Baldwin 
423bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
424bcd9e0ddSJohn Baldwin 	if (error)
425bcd9e0ddSJohn Baldwin 		return (error);
426bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
427bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
428bcd9e0ddSJohn Baldwin 	return (error);
429bcd9e0ddSJohn Baldwin }
430bcd9e0ddSJohn Baldwin 
431bcd9e0ddSJohn Baldwin int
432bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
433bcd9e0ddSJohn Baldwin 	struct thread *td;
434bcd9e0ddSJohn Baldwin 	struct uio *auio;
435bcd9e0ddSJohn Baldwin 	int fd;
436bcd9e0ddSJohn Baldwin 	off_t offset;
437bcd9e0ddSJohn Baldwin {
438bcd9e0ddSJohn Baldwin 	struct file *fp;
439bcd9e0ddSJohn Baldwin 	int error;
440bcd9e0ddSJohn Baldwin 
441bcd9e0ddSJohn Baldwin 	error = fget_write(td, fd, &fp);
442bcd9e0ddSJohn Baldwin 	if (error)
443af56abaaSJohn Baldwin 		return (error);
444bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
445bcd9e0ddSJohn Baldwin 		error = ESPIPE;
446bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
447bcd9e0ddSJohn Baldwin 		error = EINVAL;
448bcd9e0ddSJohn Baldwin 	else
449bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
450bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
451bcd9e0ddSJohn Baldwin 	return (error);
452bcd9e0ddSJohn Baldwin }
453bcd9e0ddSJohn Baldwin 
454bcd9e0ddSJohn Baldwin /*
455bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
456bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
457bcd9e0ddSJohn Baldwin  */
458bcd9e0ddSJohn Baldwin static int
459bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
460bcd9e0ddSJohn Baldwin 	struct thread *td;
461bcd9e0ddSJohn Baldwin 	int fd;
462bcd9e0ddSJohn Baldwin 	struct file *fp;
463bcd9e0ddSJohn Baldwin 	struct uio *auio;
464bcd9e0ddSJohn Baldwin 	off_t offset;
465bcd9e0ddSJohn Baldwin 	int flags;
466bcd9e0ddSJohn Baldwin {
467bcd9e0ddSJohn Baldwin 	ssize_t cnt;
468552afd9cSPoul-Henning Kamp 	int error;
469df8bae1dSRodney W. Grimes #ifdef KTRACE
470552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
471df8bae1dSRodney W. Grimes #endif
472df8bae1dSRodney W. Grimes 
473552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
474552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
475bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
476df8bae1dSRodney W. Grimes #ifdef KTRACE
477552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
478552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
479df8bae1dSRodney W. Grimes #endif
480552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
481a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
4829440653dSMatthew Dillon 		bwillwrite();
483bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
484552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
485df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
486df8bae1dSRodney W. Grimes 			error = 0;
487bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
4886f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
489b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
490b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
491b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
49219eb87d2SJohn Baldwin 		}
493df8bae1dSRodney W. Grimes 	}
494552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
495df8bae1dSRodney W. Grimes #ifdef KTRACE
496552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
497552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
498b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
499df8bae1dSRodney W. Grimes 	}
500df8bae1dSRodney W. Grimes #endif
501b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
502df8bae1dSRodney W. Grimes 	return (error);
503df8bae1dSRodney W. Grimes }
504df8bae1dSRodney W. Grimes 
505d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
506df8bae1dSRodney W. Grimes struct ioctl_args {
507df8bae1dSRodney W. Grimes 	int	fd;
508069e9bc1SDoug Rabson 	u_long	com;
509df8bae1dSRodney W. Grimes 	caddr_t	data;
510df8bae1dSRodney W. Grimes };
511d2d3e875SBruce Evans #endif
512df8bae1dSRodney W. Grimes /* ARGSUSED */
51326f9a767SRodney W. Grimes int
5143e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap)
515df8bae1dSRodney W. Grimes {
5163e15c66fSPoul-Henning Kamp 	u_long com;
5179fddcc66SRuslan Ermilov 	int arg, error;
5183e15c66fSPoul-Henning Kamp 	u_int size;
5199fddcc66SRuslan Ermilov 	caddr_t data;
520df8bae1dSRodney W. Grimes 
5219fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
5229fc6aa06SPoul-Henning Kamp 		printf(
5239fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
524431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
5259fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
5269fc6aa06SPoul-Henning Kamp 	}
527d9f46233SJohn Baldwin 	com = uap->com;
528df8bae1dSRodney W. Grimes 
529df8bae1dSRodney W. Grimes 	/*
530df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
531df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
532df8bae1dSRodney W. Grimes 	 */
533df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
534ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
535ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
5362de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
5372de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
5382de92a38SPeter Wemm #else
5392de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
5402de92a38SPeter Wemm #endif
5419fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
542426da3bcSAlfred Perlstein 		return (ENOTTY);
543279d7226SMatthew Dillon 
544ca51b19bSPoul-Henning Kamp 	if (size > 0) {
5459fddcc66SRuslan Ermilov 		if (!(com & IOC_VOID))
5469fddcc66SRuslan Ermilov 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
5479fddcc66SRuslan Ermilov 		else {
5489fddcc66SRuslan Ermilov 			/* Integer argument. */
5499fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
5509fddcc66SRuslan Ermilov 			data = (void *)&arg;
5519fddcc66SRuslan Ermilov 			size = 0;
552279d7226SMatthew Dillon 		}
5539fddcc66SRuslan Ermilov 	} else
5549fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
555df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
556df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
557df8bae1dSRodney W. Grimes 		if (error) {
558a1b0a180SRuslan Ermilov 			if (size > 0)
5599fddcc66SRuslan Ermilov 				free(data, M_IOCTLOPS);
5603e15c66fSPoul-Henning Kamp 			return (error);
561df8bae1dSRodney W. Grimes 		}
562ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
563df8bae1dSRodney W. Grimes 		/*
564df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
565df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
566df8bae1dSRodney W. Grimes 		 */
567df8bae1dSRodney W. Grimes 		bzero(data, size);
568279d7226SMatthew Dillon 	}
569df8bae1dSRodney W. Grimes 
570d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
571d9f46233SJohn Baldwin 
572d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
573d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
574d9f46233SJohn Baldwin 
5759fddcc66SRuslan Ermilov 	if (size > 0)
5769fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
577d9f46233SJohn Baldwin 	return (error);
578d9f46233SJohn Baldwin }
579d9f46233SJohn Baldwin 
580d9f46233SJohn Baldwin int
581d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
582d9f46233SJohn Baldwin {
583d9f46233SJohn Baldwin 	struct file *fp;
584d9f46233SJohn Baldwin 	struct filedesc *fdp;
585d9f46233SJohn Baldwin 	int error;
586d9f46233SJohn Baldwin 	int tmp;
587d9f46233SJohn Baldwin 
588d9f46233SJohn Baldwin 	if ((error = fget(td, fd, &fp)) != 0)
589d9f46233SJohn Baldwin 		return (error);
590d9f46233SJohn Baldwin 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
591d9f46233SJohn Baldwin 		fdrop(fp, td);
592d9f46233SJohn Baldwin 		return (EBADF);
593d9f46233SJohn Baldwin 	}
594d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
595d9f46233SJohn Baldwin 	switch (com) {
596d9f46233SJohn Baldwin 	case FIONCLEX:
5975e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
598d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
5995e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
600d9f46233SJohn Baldwin 		goto out;
601d9f46233SJohn Baldwin 	case FIOCLEX:
6025e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
603d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
6045e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
605d9f46233SJohn Baldwin 		goto out;
606d9f46233SJohn Baldwin 	case FIONBIO:
607426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
608bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
609df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
610df8bae1dSRodney W. Grimes 		else
611df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
612426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
6138ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
614d9f46233SJohn Baldwin 		break;
615d9f46233SJohn Baldwin 	case FIOASYNC:
616426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
617bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
618df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
619df8bae1dSRodney W. Grimes 		else
620df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
621426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
6228ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
623d9f46233SJohn Baldwin 		break;
624df8bae1dSRodney W. Grimes 	}
6258ccf264fSPoul-Henning Kamp 
6268ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
627d9f46233SJohn Baldwin out:
628b40ce416SJulian Elischer 	fdrop(fp, td);
629df8bae1dSRodney W. Grimes 	return (error);
630df8bae1dSRodney W. Grimes }
631df8bae1dSRodney W. Grimes 
63285f190e4SAlfred Perlstein /*
63385f190e4SAlfred Perlstein  * sellock and selwait are initialized in selectinit() via SYSINIT.
63485f190e4SAlfred Perlstein  */
63585f190e4SAlfred Perlstein struct mtx	sellock;
636265fc98fSSeigo Tanimura struct cv	selwait;
6379ae6d334SKelly Yancey u_int		nselcoll;	/* Select collisions since boot */
6389ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
639df8bae1dSRodney W. Grimes 
640d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
641df8bae1dSRodney W. Grimes struct select_args {
642b08f7993SSujal Patel 	int	nd;
643df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
644df8bae1dSRodney W. Grimes 	struct	timeval *tv;
645df8bae1dSRodney W. Grimes };
646d2d3e875SBruce Evans #endif
64726f9a767SRodney W. Grimes int
648b40ce416SJulian Elischer select(td, uap)
649b40ce416SJulian Elischer 	register struct thread *td;
650df8bae1dSRodney W. Grimes 	register struct select_args *uap;
651df8bae1dSRodney W. Grimes {
6528f19eb88SIan Dowse 	struct timeval tv, *tvp;
6538f19eb88SIan Dowse 	int error;
6548f19eb88SIan Dowse 
6558f19eb88SIan Dowse 	if (uap->tv != NULL) {
6568f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
6578f19eb88SIan Dowse 		if (error)
6588f19eb88SIan Dowse 			return (error);
6598f19eb88SIan Dowse 		tvp = &tv;
6608f19eb88SIan Dowse 	} else
6618f19eb88SIan Dowse 		tvp = NULL;
6628f19eb88SIan Dowse 
6638f19eb88SIan Dowse 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
6648f19eb88SIan Dowse }
6658f19eb88SIan Dowse 
6668f19eb88SIan Dowse int
6678f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
6688f19eb88SIan Dowse     fd_set *fd_ex, struct timeval *tvp)
6698f19eb88SIan Dowse {
670426da3bcSAlfred Perlstein 	struct filedesc *fdp;
671d5e4d7e1SBruce Evans 	/*
672d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
673d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
674d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
675d5e4d7e1SBruce Evans 	 * of 256.
676d5e4d7e1SBruce Evans 	 */
677d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
678eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
67900af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
6809ae6d334SKelly Yancey 	int error, timo;
6819ae6d334SKelly Yancey 	u_int ncoll, nbufbytes, ncpbytes, nfdbits;
682df8bae1dSRodney W. Grimes 
6838f19eb88SIan Dowse 	if (nd < 0)
684acbfbfeaSSujal Patel 		return (EINVAL);
685426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
686db446e30SPoul-Henning Kamp 
6875e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
6888f19eb88SIan Dowse 	if (nd > td->td_proc->p_fd->fd_nfiles)
6898f19eb88SIan Dowse 		nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
6905e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
691b08f7993SSujal Patel 
692d5e4d7e1SBruce Evans 	/*
693d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
694d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
695d5e4d7e1SBruce Evans 	 */
6968f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
697d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
698d5e4d7e1SBruce Evans 	nbufbytes = 0;
6998f19eb88SIan Dowse 	if (fd_in != NULL)
700d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7018f19eb88SIan Dowse 	if (fd_ou != NULL)
702d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7038f19eb88SIan Dowse 	if (fd_ex != NULL)
704d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
705d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
706d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
707d5e4d7e1SBruce Evans 	else
708a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
709b08f7993SSujal Patel 
710b08f7993SSujal Patel 	/*
711d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
712d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
713d5e4d7e1SBruce Evans 	 * together.
714b08f7993SSujal Patel 	 */
715d5e4d7e1SBruce Evans 	sbp = selbits;
716df8bae1dSRodney W. Grimes #define	getbits(name, x) \
717d5e4d7e1SBruce Evans 	do {								\
7188f19eb88SIan Dowse 		if (name == NULL)					\
719d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
720d5e4d7e1SBruce Evans 		else {							\
721d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
722d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
723d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
7248f19eb88SIan Dowse 			error = copyin(name, ibits[x], ncpbytes);	\
725265fc98fSSeigo Tanimura 			if (error != 0)					\
72685f190e4SAlfred Perlstein 				goto done_nosellock;			\
727e04ac2feSJohn Baldwin 		}							\
728d5e4d7e1SBruce Evans 	} while (0)
7298f19eb88SIan Dowse 	getbits(fd_in, 0);
7308f19eb88SIan Dowse 	getbits(fd_ou, 1);
7318f19eb88SIan Dowse 	getbits(fd_ex, 2);
732df8bae1dSRodney W. Grimes #undef	getbits
733d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
734d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
735df8bae1dSRodney W. Grimes 
7368f19eb88SIan Dowse 	if (tvp != NULL) {
7378f19eb88SIan Dowse 		atv = *tvp;
738df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
739df8bae1dSRodney W. Grimes 			error = EINVAL;
74085f190e4SAlfred Perlstein 			goto done_nosellock;
741df8bae1dSRodney W. Grimes 		}
742c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
74300af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
7449c386f6bSJohn Baldwin 	} else {
74500af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
7469c386f6bSJohn Baldwin 		atv.tv_usec = 0;
7479c386f6bSJohn Baldwin 	}
74800af9731SPoul-Henning Kamp 	timo = 0;
7492149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
75085f190e4SAlfred Perlstein 	mtx_lock(&sellock);
751df8bae1dSRodney W. Grimes retry:
752df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
753982d11f8SJeff Roberson 	thread_lock(td);
754b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
755982d11f8SJeff Roberson 	thread_unlock(td);
75685f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
75785f190e4SAlfred Perlstein 
7588f19eb88SIan Dowse 	error = selscan(td, ibits, obits, nd);
75985f190e4SAlfred Perlstein 	mtx_lock(&sellock);
760b40ce416SJulian Elischer 	if (error || td->td_retval[0])
761df8bae1dSRodney W. Grimes 		goto done;
7624da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
763c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
76485f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
765df8bae1dSRodney W. Grimes 			goto done;
76600af9731SPoul-Henning Kamp 		ttv = atv;
76700af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
76800af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
76900af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
770df8bae1dSRodney W. Grimes 	}
77185f190e4SAlfred Perlstein 
77285f190e4SAlfred Perlstein 	/*
77385f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
77485f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of
77585f190e4SAlfred Perlstein 	 * collisions and rescan the file descriptors if
77685f190e4SAlfred Perlstein 	 * necessary.
77785f190e4SAlfred Perlstein 	 */
778982d11f8SJeff Roberson 	thread_lock(td);
77985f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
780982d11f8SJeff Roberson 		thread_unlock(td);
78185f190e4SAlfred Perlstein 		goto retry;
78285f190e4SAlfred Perlstein 	}
783982d11f8SJeff Roberson 	thread_unlock(td);
784bfbbc4aaSJason Evans 
785265fc98fSSeigo Tanimura 	if (timo > 0)
78685f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
787265fc98fSSeigo Tanimura 	else
78885f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
789bfbbc4aaSJason Evans 
790df8bae1dSRodney W. Grimes 	if (error == 0)
791df8bae1dSRodney W. Grimes 		goto retry;
792265fc98fSSeigo Tanimura 
793df8bae1dSRodney W. Grimes done:
79485f190e4SAlfred Perlstein 	clear_selinfo_list(td);
795982d11f8SJeff Roberson 	thread_lock(td);
796b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
797982d11f8SJeff Roberson 	thread_unlock(td);
79885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
79985f190e4SAlfred Perlstein 
80085f190e4SAlfred Perlstein done_nosellock:
801df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
802df8bae1dSRodney W. Grimes 	if (error == ERESTART)
803df8bae1dSRodney W. Grimes 		error = EINTR;
804df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
805df8bae1dSRodney W. Grimes 		error = 0;
806df8bae1dSRodney W. Grimes #define	putbits(name, x) \
8078f19eb88SIan Dowse 	if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
808df8bae1dSRodney W. Grimes 		error = error2;
809df8bae1dSRodney W. Grimes 	if (error == 0) {
810df8bae1dSRodney W. Grimes 		int error2;
811df8bae1dSRodney W. Grimes 
8128f19eb88SIan Dowse 		putbits(fd_in, 0);
8138f19eb88SIan Dowse 		putbits(fd_ou, 1);
8148f19eb88SIan Dowse 		putbits(fd_ex, 2);
815df8bae1dSRodney W. Grimes #undef putbits
816df8bae1dSRodney W. Grimes 	}
817d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
818d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
819ad2edad9SMatthew Dillon 
820df8bae1dSRodney W. Grimes 	return (error);
821df8bae1dSRodney W. Grimes }
822df8bae1dSRodney W. Grimes 
823265fc98fSSeigo Tanimura static int
824b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
825b40ce416SJulian Elischer 	struct thread *td;
826b08f7993SSujal Patel 	fd_mask **ibits, **obits;
827cb226aaaSPoul-Henning Kamp 	int nfd;
828df8bae1dSRodney W. Grimes {
829f082218cSPeter Wemm 	int msk, i, fd;
830f082218cSPeter Wemm 	fd_mask bits;
831df8bae1dSRodney W. Grimes 	struct file *fp;
832df8bae1dSRodney W. Grimes 	int n = 0;
8332087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
83442d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
835eb209311SAlfred Perlstein 	struct filedesc *fdp = td->td_proc->p_fd;
836df8bae1dSRodney W. Grimes 
8375e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
838df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
839d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
840d5e4d7e1SBruce Evans 			continue;
841df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
842b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
843f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
844f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
845f082218cSPeter Wemm 				if (!(bits & 1))
846f082218cSPeter Wemm 					continue;
847eb209311SAlfred Perlstein 				if ((fp = fget_locked(fdp, fd)) == NULL) {
8485e3f7694SRobert Watson 					FILEDESC_SUNLOCK(fdp);
849df8bae1dSRodney W. Grimes 					return (EBADF);
850eb209311SAlfred Perlstein 				}
851ea6027a8SRobert Watson 				if (fo_poll(fp, flag[msk], td->td_ucred,
852ea6027a8SRobert Watson 				    td)) {
853b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
854f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
855df8bae1dSRodney W. Grimes 					n++;
856df8bae1dSRodney W. Grimes 				}
857df8bae1dSRodney W. Grimes 			}
858df8bae1dSRodney W. Grimes 		}
859df8bae1dSRodney W. Grimes 	}
8605e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
861b40ce416SJulian Elischer 	td->td_retval[0] = n;
862df8bae1dSRodney W. Grimes 	return (0);
863df8bae1dSRodney W. Grimes }
864df8bae1dSRodney W. Grimes 
86542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
86642d11757SPeter Wemm struct poll_args {
86742d11757SPeter Wemm 	struct pollfd *fds;
86842d11757SPeter Wemm 	u_int	nfds;
86942d11757SPeter Wemm 	int	timeout;
87042d11757SPeter Wemm };
87142d11757SPeter Wemm #endif
87242d11757SPeter Wemm int
873b40ce416SJulian Elischer poll(td, uap)
874b40ce416SJulian Elischer 	struct thread *td;
875ea0237edSJonathan Lemon 	struct poll_args *uap;
87642d11757SPeter Wemm {
8772580f4e5SAndre Oppermann 	struct pollfd *bits;
8782580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
87900af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
8809ae6d334SKelly Yancey 	int error = 0, timo;
8819ae6d334SKelly Yancey 	u_int ncoll, nfds;
88242d11757SPeter Wemm 	size_t ni;
88342d11757SPeter Wemm 
884d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
885ad2edad9SMatthew Dillon 
8865d8dd01dSRobert Watson 	/*
8872bd5ac33SPeter Wemm 	 * This is kinda bogus.  We have fd limits, but that is not
8882bd5ac33SPeter Wemm 	 * really related to the size of the pollfd array.  Make sure
8892bd5ac33SPeter Wemm 	 * we let the process use at least FD_SETSIZE entries and at
8902bd5ac33SPeter Wemm 	 * least enough for the current limits.  We want to be reasonably
8912bd5ac33SPeter Wemm 	 * safe, but not overly restrictive.
89289b71647SPeter Wemm 	 */
89391d5354aSJohn Baldwin 	PROC_LOCK(td->td_proc);
89491d5354aSJohn Baldwin 	if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) &&
895b40ce416SJulian Elischer 	    (nfds > FD_SETSIZE)) {
89691d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
897ad2edad9SMatthew Dillon 		error = EINVAL;
898ad2edad9SMatthew Dillon 		goto done2;
899ad2edad9SMatthew Dillon 	}
90091d5354aSJohn Baldwin 	PROC_UNLOCK(td->td_proc);
90189b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
90242d11757SPeter Wemm 	if (ni > sizeof(smallbits))
903a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
90442d11757SPeter Wemm 	else
90542d11757SPeter Wemm 		bits = smallbits;
906d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
90742d11757SPeter Wemm 	if (error)
90885f190e4SAlfred Perlstein 		goto done_nosellock;
909d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
910d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
911d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
91242d11757SPeter Wemm 		if (itimerfix(&atv)) {
91342d11757SPeter Wemm 			error = EINVAL;
91485f190e4SAlfred Perlstein 			goto done_nosellock;
91542d11757SPeter Wemm 		}
916c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
91700af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
9189c386f6bSJohn Baldwin 	} else {
91900af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
9209c386f6bSJohn Baldwin 		atv.tv_usec = 0;
9219c386f6bSJohn Baldwin 	}
92200af9731SPoul-Henning Kamp 	timo = 0;
9232149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
92485f190e4SAlfred Perlstein 	mtx_lock(&sellock);
92542d11757SPeter Wemm retry:
92642d11757SPeter Wemm 	ncoll = nselcoll;
927982d11f8SJeff Roberson 	thread_lock(td);
928b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
929982d11f8SJeff Roberson 	thread_unlock(td);
93085f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
93185f190e4SAlfred Perlstein 
9322580f4e5SAndre Oppermann 	error = pollscan(td, bits, nfds);
93385f190e4SAlfred Perlstein 	mtx_lock(&sellock);
934b40ce416SJulian Elischer 	if (error || td->td_retval[0])
93542d11757SPeter Wemm 		goto done;
9364da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
937c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
93885f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
93942d11757SPeter Wemm 			goto done;
94000af9731SPoul-Henning Kamp 		ttv = atv;
94100af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
94200af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
94300af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
94442d11757SPeter Wemm 	}
94585f190e4SAlfred Perlstein 	/*
94685f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
94785f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of collisions
94885f190e4SAlfred Perlstein 	 * and rescan the file descriptors if necessary.
94985f190e4SAlfred Perlstein 	 */
950982d11f8SJeff Roberson 	thread_lock(td);
95185f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
952982d11f8SJeff Roberson 		thread_unlock(td);
95385f190e4SAlfred Perlstein 		goto retry;
95485f190e4SAlfred Perlstein 	}
955982d11f8SJeff Roberson 	thread_unlock(td);
95685f190e4SAlfred Perlstein 
957265fc98fSSeigo Tanimura 	if (timo > 0)
95885f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
959265fc98fSSeigo Tanimura 	else
96085f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
96185f190e4SAlfred Perlstein 
96242d11757SPeter Wemm 	if (error == 0)
96342d11757SPeter Wemm 		goto retry;
964265fc98fSSeigo Tanimura 
96542d11757SPeter Wemm done:
96685f190e4SAlfred Perlstein 	clear_selinfo_list(td);
967982d11f8SJeff Roberson 	thread_lock(td);
968b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
969982d11f8SJeff Roberson 	thread_unlock(td);
97085f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
97185f190e4SAlfred Perlstein 
97285f190e4SAlfred Perlstein done_nosellock:
97342d11757SPeter Wemm 	/* poll is not restarted after signals... */
97442d11757SPeter Wemm 	if (error == ERESTART)
97542d11757SPeter Wemm 		error = EINTR;
97642d11757SPeter Wemm 	if (error == EWOULDBLOCK)
97742d11757SPeter Wemm 		error = 0;
97842d11757SPeter Wemm 	if (error == 0) {
979d1e405c5SAlfred Perlstein 		error = copyout(bits, uap->fds, ni);
98042d11757SPeter Wemm 		if (error)
98142d11757SPeter Wemm 			goto out;
98242d11757SPeter Wemm 	}
98342d11757SPeter Wemm out:
98442d11757SPeter Wemm 	if (ni > sizeof(smallbits))
98542d11757SPeter Wemm 		free(bits, M_TEMP);
986ad2edad9SMatthew Dillon done2:
98742d11757SPeter Wemm 	return (error);
98842d11757SPeter Wemm }
98942d11757SPeter Wemm 
99042d11757SPeter Wemm static int
991b40ce416SJulian Elischer pollscan(td, fds, nfd)
992b40ce416SJulian Elischer 	struct thread *td;
99342d11757SPeter Wemm 	struct pollfd *fds;
994ea0237edSJonathan Lemon 	u_int nfd;
99542d11757SPeter Wemm {
996b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
99742d11757SPeter Wemm 	int i;
99842d11757SPeter Wemm 	struct file *fp;
99942d11757SPeter Wemm 	int n = 0;
100042d11757SPeter Wemm 
10015e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1002eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1003337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
100442d11757SPeter Wemm 			fds->revents = POLLNVAL;
100542d11757SPeter Wemm 			n++;
1006337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1007337c9691SJordan K. Hubbard 			fds->revents = 0;
100842d11757SPeter Wemm 		} else {
100942d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1010279d7226SMatthew Dillon 			if (fp == NULL) {
101142d11757SPeter Wemm 				fds->revents = POLLNVAL;
101242d11757SPeter Wemm 				n++;
101342d11757SPeter Wemm 			} else {
10142087c896SBruce Evans 				/*
10152087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
10162087c896SBruce Evans 				 * POLLERR if appropriate.
10172087c896SBruce Evans 				 */
101813ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1019ea6027a8SRobert Watson 				    td->td_ucred, td);
102042d11757SPeter Wemm 				if (fds->revents != 0)
102142d11757SPeter Wemm 					n++;
102242d11757SPeter Wemm 			}
102342d11757SPeter Wemm 		}
102442d11757SPeter Wemm 	}
10255e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1026b40ce416SJulian Elischer 	td->td_retval[0] = n;
102742d11757SPeter Wemm 	return (0);
102842d11757SPeter Wemm }
102942d11757SPeter Wemm 
103042d11757SPeter Wemm /*
103142d11757SPeter Wemm  * OpenBSD poll system call.
10320c14ff0eSRobert Watson  *
103342d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
103442d11757SPeter Wemm  */
103542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
103642d11757SPeter Wemm struct openbsd_poll_args {
103742d11757SPeter Wemm 	struct pollfd *fds;
103842d11757SPeter Wemm 	u_int	nfds;
103942d11757SPeter Wemm 	int	timeout;
104042d11757SPeter Wemm };
104142d11757SPeter Wemm #endif
104242d11757SPeter Wemm int
1043b40ce416SJulian Elischer openbsd_poll(td, uap)
1044b40ce416SJulian Elischer 	register struct thread *td;
104542d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
104642d11757SPeter Wemm {
1047b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
104842d11757SPeter Wemm }
104942d11757SPeter Wemm 
105085f190e4SAlfred Perlstein /*
10510c14ff0eSRobert Watson  * Remove the references to the thread from all of the objects we were
10520c14ff0eSRobert Watson  * polling.
105385f190e4SAlfred Perlstein  *
10540c14ff0eSRobert Watson  * This code assumes that the underlying owner of the selinfo structure will
10550c14ff0eSRobert Watson  * hold sellock before it changes it, and that it will unlink itself from our
10560c14ff0eSRobert Watson  * list if it goes away.
105785f190e4SAlfred Perlstein  */
105885f190e4SAlfred Perlstein void
105985f190e4SAlfred Perlstein clear_selinfo_list(td)
106085f190e4SAlfred Perlstein 	struct thread *td;
106185f190e4SAlfred Perlstein {
106285f190e4SAlfred Perlstein 	struct selinfo *si;
106385f190e4SAlfred Perlstein 
106485f190e4SAlfred Perlstein 	mtx_assert(&sellock, MA_OWNED);
106585f190e4SAlfred Perlstein 	TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
106685f190e4SAlfred Perlstein 		si->si_thread = NULL;
106785f190e4SAlfred Perlstein 	TAILQ_INIT(&td->td_selq);
106885f190e4SAlfred Perlstein }
106985f190e4SAlfred Perlstein 
1070df8bae1dSRodney W. Grimes /*
1071df8bae1dSRodney W. Grimes  * Record a select request.
1072df8bae1dSRodney W. Grimes  */
1073df8bae1dSRodney W. Grimes void
1074df8bae1dSRodney W. Grimes selrecord(selector, sip)
1075b40ce416SJulian Elischer 	struct thread *selector;
1076df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1077df8bae1dSRodney W. Grimes {
1078df8bae1dSRodney W. Grimes 
107985f190e4SAlfred Perlstein 	mtx_lock(&sellock);
108085f190e4SAlfred Perlstein 	/*
1081b605b54cSAlfred Perlstein 	 * If the selinfo's thread pointer is NULL then take ownership of it.
1082b605b54cSAlfred Perlstein 	 *
1083b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and it points to another
1084b605b54cSAlfred Perlstein 	 * thread, then we have a collision.
1085b605b54cSAlfred Perlstein 	 *
1086b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and points back to us then leave
1087b605b54cSAlfred Perlstein 	 * it alone as we've already added pointed it at us and added it to
1088b605b54cSAlfred Perlstein 	 * our list.
108985f190e4SAlfred Perlstein 	 */
109085f190e4SAlfred Perlstein 	if (sip->si_thread == NULL) {
1091b40ce416SJulian Elischer 		sip->si_thread = selector;
109285f190e4SAlfred Perlstein 		TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
109385f190e4SAlfred Perlstein 	} else if (sip->si_thread != selector) {
109485f190e4SAlfred Perlstein 		sip->si_flags |= SI_COLL;
109585f190e4SAlfred Perlstein 	}
109685f190e4SAlfred Perlstein 
109785f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1098df8bae1dSRodney W. Grimes }
1099df8bae1dSRodney W. Grimes 
1100512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1101df8bae1dSRodney W. Grimes void
1102df8bae1dSRodney W. Grimes selwakeup(sip)
110385f190e4SAlfred Perlstein 	struct selinfo *sip;
1104df8bae1dSRodney W. Grimes {
1105512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1106512824f8SSeigo Tanimura }
1107512824f8SSeigo Tanimura 
1108512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1109512824f8SSeigo Tanimura void
1110512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1111512824f8SSeigo Tanimura 	struct selinfo *sip;
1112512824f8SSeigo Tanimura 	int pri;
1113512824f8SSeigo Tanimura {
1114512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1115512824f8SSeigo Tanimura }
1116512824f8SSeigo Tanimura 
1117512824f8SSeigo Tanimura /*
1118512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1119512824f8SSeigo Tanimura  */
1120512824f8SSeigo Tanimura static void
1121512824f8SSeigo Tanimura doselwakeup(sip, pri)
1122512824f8SSeigo Tanimura 	struct selinfo *sip;
1123512824f8SSeigo Tanimura 	int pri;
1124512824f8SSeigo Tanimura {
1125b40ce416SJulian Elischer 	struct thread *td;
1126df8bae1dSRodney W. Grimes 
112785f190e4SAlfred Perlstein 	mtx_lock(&sellock);
112885f190e4SAlfred Perlstein 	td = sip->si_thread;
112985f190e4SAlfred Perlstein 	if ((sip->si_flags & SI_COLL) != 0) {
1130df8bae1dSRodney W. Grimes 		nselcoll++;
1131df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1132512824f8SSeigo Tanimura 		cv_broadcastpri(&selwait, pri);
1133df8bae1dSRodney W. Grimes 	}
113485f190e4SAlfred Perlstein 	if (td == NULL) {
113585f190e4SAlfred Perlstein 		mtx_unlock(&sellock);
1136b40ce416SJulian Elischer 		return;
1137b40ce416SJulian Elischer 	}
113885f190e4SAlfred Perlstein 	TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
113985f190e4SAlfred Perlstein 	sip->si_thread = NULL;
1140982d11f8SJeff Roberson 	thread_lock(td);
1141b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
1142982d11f8SJeff Roberson 	thread_unlock(td);
114344f3b092SJohn Baldwin 	sleepq_remove(td, &selwait);
114485f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1145df8bae1dSRodney W. Grimes }
1146265fc98fSSeigo Tanimura 
11474d77a549SAlfred Perlstein static void selectinit(void *);
1148265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1149265fc98fSSeigo Tanimura 
1150265fc98fSSeigo Tanimura /* ARGSUSED*/
1151265fc98fSSeigo Tanimura static void
1152265fc98fSSeigo Tanimura selectinit(dummy)
1153265fc98fSSeigo Tanimura 	void *dummy;
1154265fc98fSSeigo Tanimura {
1155265fc98fSSeigo Tanimura 	cv_init(&selwait, "select");
11566008862bSJohn Baldwin 	mtx_init(&sellock, "sellck", NULL, MTX_DEF);
1157265fc98fSSeigo Tanimura }
1158