xref: /freebsd/sys/kern/sys_generic.c (revision 9fddcc6661d2cf7bb8e704308437d2b1674b50be)
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
71279d7226SMatthew Dillon #include <vm/vm.h>
72279d7226SMatthew Dillon #include <vm/vm_page.h>
73df8bae1dSRodney W. Grimes 
74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
76a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7755166637SPoul-Henning Kamp 
78bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
79bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
80bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
81bcd9e0ddSJohn Baldwin 		    off_t, int);
82bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
83bcd9e0ddSJohn Baldwin 		    off_t, int);
84512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
858fe387abSDmitrij Tejblum 
86df8bae1dSRodney W. Grimes /*
87df8bae1dSRodney W. Grimes  * Read system call.
88df8bae1dSRodney W. Grimes  */
89d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
90df8bae1dSRodney W. Grimes struct read_args {
91df8bae1dSRodney W. Grimes 	int	fd;
92134e06feSBruce Evans 	void	*buf;
93134e06feSBruce Evans 	size_t	nbyte;
94df8bae1dSRodney W. Grimes };
95d2d3e875SBruce Evans #endif
96ad2edad9SMatthew Dillon /*
97ad2edad9SMatthew Dillon  * MPSAFE
98ad2edad9SMatthew Dillon  */
9926f9a767SRodney W. Grimes int
100b40ce416SJulian Elischer read(td, uap)
101b40ce416SJulian Elischer 	struct thread *td;
102b064d43dSMatthew Dillon 	struct read_args *uap;
103df8bae1dSRodney W. Grimes {
104bcd9e0ddSJohn Baldwin 	struct uio auio;
105bcd9e0ddSJohn Baldwin 	struct iovec aiov;
106279d7226SMatthew Dillon 	int error;
107df8bae1dSRodney W. Grimes 
108bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
109bcd9e0ddSJohn Baldwin 		return (EINVAL);
110bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
111bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
112bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
113bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
114bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
115bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
116bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
117279d7226SMatthew Dillon 	return(error);
118df8bae1dSRodney W. Grimes }
119df8bae1dSRodney W. Grimes 
120df8bae1dSRodney W. Grimes /*
121bcd9e0ddSJohn Baldwin  * Positioned read system call
1224160ccd9SAlan Cox  */
1234160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1244160ccd9SAlan Cox struct pread_args {
1254160ccd9SAlan Cox 	int	fd;
1264160ccd9SAlan Cox 	void	*buf;
1274160ccd9SAlan Cox 	size_t	nbyte;
1288fe387abSDmitrij Tejblum 	int	pad;
1294160ccd9SAlan Cox 	off_t	offset;
1304160ccd9SAlan Cox };
1314160ccd9SAlan Cox #endif
132ad2edad9SMatthew Dillon /*
133ad2edad9SMatthew Dillon  * MPSAFE
134ad2edad9SMatthew Dillon  */
1354160ccd9SAlan Cox int
136b40ce416SJulian Elischer pread(td, uap)
137b40ce416SJulian Elischer 	struct thread *td;
138b064d43dSMatthew Dillon 	struct pread_args *uap;
1394160ccd9SAlan Cox {
1404160ccd9SAlan Cox 	struct uio auio;
1414160ccd9SAlan Cox 	struct iovec aiov;
142bcd9e0ddSJohn Baldwin 	int error;
1434160ccd9SAlan Cox 
144bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
145bcd9e0ddSJohn Baldwin 		return (EINVAL);
146bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
147bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
1484160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1494160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
150bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
1514160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
152bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
1534160ccd9SAlan Cox 	return(error);
1544160ccd9SAlan Cox }
1554160ccd9SAlan Cox 
1564160ccd9SAlan Cox /*
157df8bae1dSRodney W. Grimes  * Scatter read system call.
158df8bae1dSRodney W. Grimes  */
159d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
160df8bae1dSRodney W. Grimes struct readv_args {
1617147b19dSBruce Evans 	int	fd;
162df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
163df8bae1dSRodney W. Grimes 	u_int	iovcnt;
164df8bae1dSRodney W. Grimes };
165d2d3e875SBruce Evans #endif
166ad2edad9SMatthew Dillon /*
167ad2edad9SMatthew Dillon  * MPSAFE
168ad2edad9SMatthew Dillon  */
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 /*
209bcd9e0ddSJohn Baldwin  * MPSAFE
210bcd9e0ddSJohn Baldwin  */
211bcd9e0ddSJohn Baldwin int
212bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap)
213bcd9e0ddSJohn Baldwin {
214bcd9e0ddSJohn Baldwin 	struct uio *auio;
215bcd9e0ddSJohn Baldwin 	int error;
216bcd9e0ddSJohn Baldwin 
217bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
218bcd9e0ddSJohn Baldwin 	if (error)
219bcd9e0ddSJohn Baldwin 		return (error);
220bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
221bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
222bcd9e0ddSJohn Baldwin 	return (error);
223bcd9e0ddSJohn Baldwin }
224bcd9e0ddSJohn Baldwin 
225bcd9e0ddSJohn Baldwin int
226bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
227bcd9e0ddSJohn Baldwin 	struct thread *td;
228bcd9e0ddSJohn Baldwin 	int fd;
229bcd9e0ddSJohn Baldwin 	struct uio *auio;
230bcd9e0ddSJohn Baldwin 	off_t offset;
231bcd9e0ddSJohn Baldwin {
232bcd9e0ddSJohn Baldwin 	struct file *fp;
233bcd9e0ddSJohn Baldwin 	int error;
234bcd9e0ddSJohn Baldwin 
235bcd9e0ddSJohn Baldwin 	error = fget_read(td, fd, &fp);
236bcd9e0ddSJohn Baldwin 	if (error)
237bcd9e0ddSJohn Baldwin 		return (error);
238bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
239bcd9e0ddSJohn Baldwin 		error = ESPIPE;
240bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
241bcd9e0ddSJohn Baldwin 		error = EINVAL;
242bcd9e0ddSJohn Baldwin 	else
243bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
244bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
245bcd9e0ddSJohn Baldwin 	return (error);
246bcd9e0ddSJohn Baldwin }
247bcd9e0ddSJohn Baldwin 
248bcd9e0ddSJohn Baldwin /*
249bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
250bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
251bcd9e0ddSJohn Baldwin  */
252bcd9e0ddSJohn Baldwin static int
253bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
254bcd9e0ddSJohn Baldwin 	struct thread *td;
255bcd9e0ddSJohn Baldwin 	int fd;
256bcd9e0ddSJohn Baldwin 	struct file *fp;
257bcd9e0ddSJohn Baldwin 	struct uio *auio;
258bcd9e0ddSJohn Baldwin 	off_t offset;
259bcd9e0ddSJohn Baldwin 	int flags;
260bcd9e0ddSJohn Baldwin {
261bcd9e0ddSJohn Baldwin 	ssize_t cnt;
26282641acdSAlan Cox 	int error;
263df8bae1dSRodney W. Grimes #ifdef KTRACE
264552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
265df8bae1dSRodney W. Grimes #endif
266df8bae1dSRodney W. Grimes 
2674f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
2684f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
2694f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
2704f8d23d6SPoul-Henning Kamp 		return(0);
2714f8d23d6SPoul-Henning Kamp 	}
272552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
273bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
274552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
275df8bae1dSRodney W. Grimes #ifdef KTRACE
276552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
277552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
278df8bae1dSRodney W. Grimes #endif
279552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
280bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
281552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
282df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
283df8bae1dSRodney W. Grimes 			error = 0;
284279d7226SMatthew Dillon 	}
285552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
286df8bae1dSRodney W. Grimes #ifdef KTRACE
287552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
288552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
289b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
290df8bae1dSRodney W. Grimes 	}
291df8bae1dSRodney W. Grimes #endif
292b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
293df8bae1dSRodney W. Grimes 	return (error);
294df8bae1dSRodney W. Grimes }
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes /*
297df8bae1dSRodney W. Grimes  * Write system call
298df8bae1dSRodney W. Grimes  */
299d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
300df8bae1dSRodney W. Grimes struct write_args {
301df8bae1dSRodney W. Grimes 	int	fd;
302134e06feSBruce Evans 	const void *buf;
303134e06feSBruce Evans 	size_t	nbyte;
304df8bae1dSRodney W. Grimes };
305d2d3e875SBruce Evans #endif
306ad2edad9SMatthew Dillon /*
307ad2edad9SMatthew Dillon  * MPSAFE
308ad2edad9SMatthew Dillon  */
30926f9a767SRodney W. Grimes int
310b40ce416SJulian Elischer write(td, uap)
311b40ce416SJulian Elischer 	struct thread *td;
312b064d43dSMatthew Dillon 	struct write_args *uap;
313df8bae1dSRodney W. Grimes {
314bcd9e0ddSJohn Baldwin 	struct uio auio;
315bcd9e0ddSJohn Baldwin 	struct iovec aiov;
316279d7226SMatthew Dillon 	int error;
317df8bae1dSRodney W. Grimes 
318bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
319bcd9e0ddSJohn Baldwin 		return (EINVAL);
320bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
321bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
322bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
323bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
324bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
325bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
326bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
327279d7226SMatthew Dillon 	return(error);
328df8bae1dSRodney W. Grimes }
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes /*
331bcd9e0ddSJohn Baldwin  * Positioned write system call
3324160ccd9SAlan Cox  */
3334160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3344160ccd9SAlan Cox struct pwrite_args {
3354160ccd9SAlan Cox 	int	fd;
3364160ccd9SAlan Cox 	const void *buf;
3374160ccd9SAlan Cox 	size_t	nbyte;
3388fe387abSDmitrij Tejblum 	int	pad;
3394160ccd9SAlan Cox 	off_t	offset;
3404160ccd9SAlan Cox };
3414160ccd9SAlan Cox #endif
342ad2edad9SMatthew Dillon /*
343ad2edad9SMatthew Dillon  * MPSAFE
344ad2edad9SMatthew Dillon  */
3454160ccd9SAlan Cox int
346b40ce416SJulian Elischer pwrite(td, uap)
347b40ce416SJulian Elischer 	struct thread *td;
348b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3494160ccd9SAlan Cox {
3504160ccd9SAlan Cox 	struct uio auio;
3514160ccd9SAlan Cox 	struct iovec aiov;
352bcd9e0ddSJohn Baldwin 	int error;
3534160ccd9SAlan Cox 
354bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
355bcd9e0ddSJohn Baldwin 		return (EINVAL);
356bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
357bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
3584160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3594160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
360bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
3614160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
362bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
3634160ccd9SAlan Cox 	return(error);
3644160ccd9SAlan Cox }
3654160ccd9SAlan Cox 
3664160ccd9SAlan Cox /*
367df8bae1dSRodney W. Grimes  * Gather write system call
368df8bae1dSRodney W. Grimes  */
369d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
370df8bae1dSRodney W. Grimes struct writev_args {
371df8bae1dSRodney W. Grimes 	int	fd;
372df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
373df8bae1dSRodney W. Grimes 	u_int	iovcnt;
374df8bae1dSRodney W. Grimes };
375d2d3e875SBruce Evans #endif
376ad2edad9SMatthew Dillon /*
377ad2edad9SMatthew Dillon  * MPSAFE
378ad2edad9SMatthew Dillon  */
37926f9a767SRodney W. Grimes int
380552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap)
381df8bae1dSRodney W. Grimes {
382b88ec951SJohn Baldwin 	struct uio *auio;
383b88ec951SJohn Baldwin 	int error;
384b88ec951SJohn Baldwin 
385b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
386b88ec951SJohn Baldwin 	if (error)
387b88ec951SJohn Baldwin 		return (error);
388b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
389b88ec951SJohn Baldwin 	free(auio, M_IOV);
390b88ec951SJohn Baldwin 	return (error);
391b88ec951SJohn Baldwin }
392b88ec951SJohn Baldwin 
393b88ec951SJohn Baldwin int
394b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
395b88ec951SJohn Baldwin {
396b064d43dSMatthew Dillon 	struct file *fp;
397bcd9e0ddSJohn Baldwin 	int error;
398bcd9e0ddSJohn Baldwin 
399bcd9e0ddSJohn Baldwin 	error = fget_write(td, fd, &fp);
400bcd9e0ddSJohn Baldwin 	if (error)
401af56abaaSJohn Baldwin 		return (error);
402bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
403bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
404bcd9e0ddSJohn Baldwin 	return (error);
405bcd9e0ddSJohn Baldwin }
406bcd9e0ddSJohn Baldwin 
407bcd9e0ddSJohn Baldwin /*
408bcd9e0ddSJohn Baldwin  * Gather positioned write system call
409bcd9e0ddSJohn Baldwin  */
410bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
411bcd9e0ddSJohn Baldwin struct pwritev_args {
412bcd9e0ddSJohn Baldwin 	int	fd;
413bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
414bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
415bcd9e0ddSJohn Baldwin 	off_t	offset;
416bcd9e0ddSJohn Baldwin };
417bcd9e0ddSJohn Baldwin #endif
418bcd9e0ddSJohn Baldwin /*
419bcd9e0ddSJohn Baldwin  * MPSAFE
420bcd9e0ddSJohn Baldwin  */
421bcd9e0ddSJohn Baldwin int
422bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap)
423bcd9e0ddSJohn Baldwin {
424bcd9e0ddSJohn Baldwin 	struct uio *auio;
425bcd9e0ddSJohn Baldwin 	int error;
426bcd9e0ddSJohn Baldwin 
427bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
428bcd9e0ddSJohn Baldwin 	if (error)
429bcd9e0ddSJohn Baldwin 		return (error);
430bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
431bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
432bcd9e0ddSJohn Baldwin 	return (error);
433bcd9e0ddSJohn Baldwin }
434bcd9e0ddSJohn Baldwin 
435bcd9e0ddSJohn Baldwin int
436bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
437bcd9e0ddSJohn Baldwin 	struct thread *td;
438bcd9e0ddSJohn Baldwin 	struct uio *auio;
439bcd9e0ddSJohn Baldwin 	int fd;
440bcd9e0ddSJohn Baldwin 	off_t offset;
441bcd9e0ddSJohn Baldwin {
442bcd9e0ddSJohn Baldwin 	struct file *fp;
443bcd9e0ddSJohn Baldwin 	int error;
444bcd9e0ddSJohn Baldwin 
445bcd9e0ddSJohn Baldwin 	error = fget_write(td, fd, &fp);
446bcd9e0ddSJohn Baldwin 	if (error)
447af56abaaSJohn Baldwin 		return (error);
448bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
449bcd9e0ddSJohn Baldwin 		error = ESPIPE;
450bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
451bcd9e0ddSJohn Baldwin 		error = EINVAL;
452bcd9e0ddSJohn Baldwin 	else
453bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
454bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
455bcd9e0ddSJohn Baldwin 	return (error);
456bcd9e0ddSJohn Baldwin }
457bcd9e0ddSJohn Baldwin 
458bcd9e0ddSJohn Baldwin /*
459bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
460bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
461bcd9e0ddSJohn Baldwin  */
462bcd9e0ddSJohn Baldwin static int
463bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
464bcd9e0ddSJohn Baldwin 	struct thread *td;
465bcd9e0ddSJohn Baldwin 	int fd;
466bcd9e0ddSJohn Baldwin 	struct file *fp;
467bcd9e0ddSJohn Baldwin 	struct uio *auio;
468bcd9e0ddSJohn Baldwin 	off_t offset;
469bcd9e0ddSJohn Baldwin 	int flags;
470bcd9e0ddSJohn Baldwin {
471bcd9e0ddSJohn Baldwin 	ssize_t cnt;
472552afd9cSPoul-Henning Kamp 	int error;
473df8bae1dSRodney W. Grimes #ifdef KTRACE
474552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
475df8bae1dSRodney W. Grimes #endif
476df8bae1dSRodney W. Grimes 
477552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
478552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
479bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
480df8bae1dSRodney W. Grimes #ifdef KTRACE
481552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
482552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
483df8bae1dSRodney W. Grimes #endif
484552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
485a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
4869440653dSMatthew Dillon 		bwillwrite();
487bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
488552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
489df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
490df8bae1dSRodney W. Grimes 			error = 0;
491bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
49219eb87d2SJohn Baldwin 		if (error == EPIPE) {
493b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
494b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
495b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
49619eb87d2SJohn Baldwin 		}
497df8bae1dSRodney W. Grimes 	}
498552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
499df8bae1dSRodney W. Grimes #ifdef KTRACE
500552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
501552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
502b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
503df8bae1dSRodney W. Grimes 	}
504df8bae1dSRodney W. Grimes #endif
505b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
506df8bae1dSRodney W. Grimes 	return (error);
507df8bae1dSRodney W. Grimes }
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes /*
510df8bae1dSRodney W. Grimes  * Ioctl system call
511df8bae1dSRodney W. Grimes  */
512d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
513df8bae1dSRodney W. Grimes struct ioctl_args {
514df8bae1dSRodney W. Grimes 	int	fd;
515069e9bc1SDoug Rabson 	u_long	com;
516df8bae1dSRodney W. Grimes 	caddr_t	data;
517df8bae1dSRodney W. Grimes };
518d2d3e875SBruce Evans #endif
519ad2edad9SMatthew Dillon /*
520ad2edad9SMatthew Dillon  * MPSAFE
521ad2edad9SMatthew Dillon  */
522df8bae1dSRodney W. Grimes /* ARGSUSED */
52326f9a767SRodney W. Grimes int
5243e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap)
525df8bae1dSRodney W. Grimes {
5263e15c66fSPoul-Henning Kamp 	u_long com;
5279fddcc66SRuslan Ermilov 	int arg, error;
5283e15c66fSPoul-Henning Kamp 	u_int size;
5299fddcc66SRuslan Ermilov 	caddr_t data;
530df8bae1dSRodney W. Grimes 
5319fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
5329fc6aa06SPoul-Henning Kamp 		printf(
5339fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
5349fc6aa06SPoul-Henning Kamp 		    td->td_proc->p_pid, td->td_proc->p_comm, uap->com);
5359fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
5369fc6aa06SPoul-Henning Kamp 	}
537d9f46233SJohn Baldwin 	com = uap->com;
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes 	/*
540df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
541df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
542df8bae1dSRodney W. Grimes 	 */
543df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
544ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
545ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
5462de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
5472de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
5482de92a38SPeter Wemm #else
5492de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
5502de92a38SPeter Wemm #endif
5519fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
552426da3bcSAlfred Perlstein 		return (ENOTTY);
553279d7226SMatthew Dillon 
554ca51b19bSPoul-Henning Kamp 	if (size > 0) {
5559fddcc66SRuslan Ermilov 		if (!(com & IOC_VOID))
5569fddcc66SRuslan Ermilov 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
5579fddcc66SRuslan Ermilov 		else {
5589fddcc66SRuslan Ermilov 			/* Integer argument. */
5599fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
5609fddcc66SRuslan Ermilov 			data = (void *)&arg;
5619fddcc66SRuslan Ermilov 			size = 0;
562279d7226SMatthew Dillon 		}
5639fddcc66SRuslan Ermilov 	} else
5649fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
565df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
566df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
567df8bae1dSRodney W. Grimes 		if (error) {
5689fddcc66SRuslan Ermilov 			free(data, M_IOCTLOPS);
5693e15c66fSPoul-Henning Kamp 			return (error);
570df8bae1dSRodney W. Grimes 		}
571ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
572df8bae1dSRodney W. Grimes 		/*
573df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
574df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
575df8bae1dSRodney W. Grimes 		 */
576df8bae1dSRodney W. Grimes 		bzero(data, size);
577279d7226SMatthew Dillon 	}
578df8bae1dSRodney W. Grimes 
579d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
580d9f46233SJohn Baldwin 
581d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
582d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
583d9f46233SJohn Baldwin 
5849fddcc66SRuslan Ermilov 	if (size > 0)
5859fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
586d9f46233SJohn Baldwin 	return (error);
587d9f46233SJohn Baldwin }
588d9f46233SJohn Baldwin 
589d9f46233SJohn Baldwin int
590d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
591d9f46233SJohn Baldwin {
592d9f46233SJohn Baldwin 	struct file *fp;
593d9f46233SJohn Baldwin 	struct filedesc *fdp;
594d9f46233SJohn Baldwin 	int error;
595d9f46233SJohn Baldwin 	int tmp;
596d9f46233SJohn Baldwin 
597d9f46233SJohn Baldwin 	if ((error = fget(td, fd, &fp)) != 0)
598d9f46233SJohn Baldwin 		return (error);
599d9f46233SJohn Baldwin 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
600d9f46233SJohn Baldwin 		fdrop(fp, td);
601d9f46233SJohn Baldwin 		return (EBADF);
602d9f46233SJohn Baldwin 	}
603d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
604d9f46233SJohn Baldwin 	switch (com) {
605d9f46233SJohn Baldwin 	case FIONCLEX:
606d9f46233SJohn Baldwin 		FILEDESC_LOCK_FAST(fdp);
607d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
608d9f46233SJohn Baldwin 		FILEDESC_UNLOCK_FAST(fdp);
609d9f46233SJohn Baldwin 		goto out;
610d9f46233SJohn Baldwin 	case FIOCLEX:
611d9f46233SJohn Baldwin 		FILEDESC_LOCK_FAST(fdp);
612d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
613d9f46233SJohn Baldwin 		FILEDESC_UNLOCK_FAST(fdp);
614d9f46233SJohn Baldwin 		goto out;
615d9f46233SJohn Baldwin 	case FIONBIO:
616426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
617bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
618df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
619df8bae1dSRodney W. Grimes 		else
620df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
621426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
6228ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
623d9f46233SJohn Baldwin 		break;
624d9f46233SJohn Baldwin 	case FIOASYNC:
625426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
626bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
627df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
628df8bae1dSRodney W. Grimes 		else
629df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
630426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
6318ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
632d9f46233SJohn Baldwin 		break;
633df8bae1dSRodney W. Grimes 	}
6348ccf264fSPoul-Henning Kamp 
6358ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
636d9f46233SJohn Baldwin out:
637b40ce416SJulian Elischer 	fdrop(fp, td);
638df8bae1dSRodney W. Grimes 	return (error);
639df8bae1dSRodney W. Grimes }
640df8bae1dSRodney W. Grimes 
64185f190e4SAlfred Perlstein /*
64285f190e4SAlfred Perlstein  * sellock and selwait are initialized in selectinit() via SYSINIT.
64385f190e4SAlfred Perlstein  */
64485f190e4SAlfred Perlstein struct mtx	sellock;
645265fc98fSSeigo Tanimura struct cv	selwait;
6469ae6d334SKelly Yancey u_int		nselcoll;	/* Select collisions since boot */
6479ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes /*
650df8bae1dSRodney W. Grimes  * Select system call.
651df8bae1dSRodney W. Grimes  */
652d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
653df8bae1dSRodney W. Grimes struct select_args {
654b08f7993SSujal Patel 	int	nd;
655df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
656df8bae1dSRodney W. Grimes 	struct	timeval *tv;
657df8bae1dSRodney W. Grimes };
658d2d3e875SBruce Evans #endif
659ad2edad9SMatthew Dillon /*
660ad2edad9SMatthew Dillon  * MPSAFE
661ad2edad9SMatthew Dillon  */
66226f9a767SRodney W. Grimes int
663b40ce416SJulian Elischer select(td, uap)
664b40ce416SJulian Elischer 	register struct thread *td;
665df8bae1dSRodney W. Grimes 	register struct select_args *uap;
666df8bae1dSRodney W. Grimes {
6678f19eb88SIan Dowse 	struct timeval tv, *tvp;
6688f19eb88SIan Dowse 	int error;
6698f19eb88SIan Dowse 
6708f19eb88SIan Dowse 	if (uap->tv != NULL) {
6718f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
6728f19eb88SIan Dowse 		if (error)
6738f19eb88SIan Dowse 			return (error);
6748f19eb88SIan Dowse 		tvp = &tv;
6758f19eb88SIan Dowse 	} else
6768f19eb88SIan Dowse 		tvp = NULL;
6778f19eb88SIan Dowse 
6788f19eb88SIan Dowse 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
6798f19eb88SIan Dowse }
6808f19eb88SIan Dowse 
6818f19eb88SIan Dowse int
6828f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
6838f19eb88SIan Dowse     fd_set *fd_ex, struct timeval *tvp)
6848f19eb88SIan Dowse {
685426da3bcSAlfred Perlstein 	struct filedesc *fdp;
686d5e4d7e1SBruce Evans 	/*
687d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
688d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
689d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
690d5e4d7e1SBruce Evans 	 * of 256.
691d5e4d7e1SBruce Evans 	 */
692d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
693eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
69400af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
6959ae6d334SKelly Yancey 	int error, timo;
6969ae6d334SKelly Yancey 	u_int ncoll, nbufbytes, ncpbytes, nfdbits;
697df8bae1dSRodney W. Grimes 
6988f19eb88SIan Dowse 	if (nd < 0)
699acbfbfeaSSujal Patel 		return (EINVAL);
700426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
701db446e30SPoul-Henning Kamp 
702124e4c3bSPoul-Henning Kamp 	FILEDESC_LOCK_FAST(fdp);
703ad2edad9SMatthew Dillon 
7048f19eb88SIan Dowse 	if (nd > td->td_proc->p_fd->fd_nfiles)
7058f19eb88SIan Dowse 		nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
706124e4c3bSPoul-Henning Kamp 	FILEDESC_UNLOCK_FAST(fdp);
707b08f7993SSujal Patel 
708d5e4d7e1SBruce Evans 	/*
709d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
710d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
711d5e4d7e1SBruce Evans 	 */
7128f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
713d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
714d5e4d7e1SBruce Evans 	nbufbytes = 0;
7158f19eb88SIan Dowse 	if (fd_in != NULL)
716d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7178f19eb88SIan Dowse 	if (fd_ou != NULL)
718d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7198f19eb88SIan Dowse 	if (fd_ex != NULL)
720d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
721d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
722d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
723d5e4d7e1SBruce Evans 	else
724a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
725b08f7993SSujal Patel 
726b08f7993SSujal Patel 	/*
727d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
728d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
729d5e4d7e1SBruce Evans 	 * together.
730b08f7993SSujal Patel 	 */
731d5e4d7e1SBruce Evans 	sbp = selbits;
732df8bae1dSRodney W. Grimes #define	getbits(name, x) \
733d5e4d7e1SBruce Evans 	do {								\
7348f19eb88SIan Dowse 		if (name == NULL)					\
735d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
736d5e4d7e1SBruce Evans 		else {							\
737d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
738d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
739d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
7408f19eb88SIan Dowse 			error = copyin(name, ibits[x], ncpbytes);	\
741265fc98fSSeigo Tanimura 			if (error != 0)					\
74285f190e4SAlfred Perlstein 				goto done_nosellock;			\
743e04ac2feSJohn Baldwin 		}							\
744d5e4d7e1SBruce Evans 	} while (0)
7458f19eb88SIan Dowse 	getbits(fd_in, 0);
7468f19eb88SIan Dowse 	getbits(fd_ou, 1);
7478f19eb88SIan Dowse 	getbits(fd_ex, 2);
748df8bae1dSRodney W. Grimes #undef	getbits
749d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
750d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
751df8bae1dSRodney W. Grimes 
7528f19eb88SIan Dowse 	if (tvp != NULL) {
7538f19eb88SIan Dowse 		atv = *tvp;
754df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
755df8bae1dSRodney W. Grimes 			error = EINVAL;
75685f190e4SAlfred Perlstein 			goto done_nosellock;
757df8bae1dSRodney W. Grimes 		}
758c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
75900af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
7609c386f6bSJohn Baldwin 	} else {
76100af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
7629c386f6bSJohn Baldwin 		atv.tv_usec = 0;
7639c386f6bSJohn Baldwin 	}
76400af9731SPoul-Henning Kamp 	timo = 0;
7652149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
76685f190e4SAlfred Perlstein 	mtx_lock(&sellock);
767df8bae1dSRodney W. Grimes retry:
768df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
769fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
770b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
771fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
77285f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
77385f190e4SAlfred Perlstein 
7748f19eb88SIan Dowse 	error = selscan(td, ibits, obits, nd);
77585f190e4SAlfred Perlstein 	mtx_lock(&sellock);
776b40ce416SJulian Elischer 	if (error || td->td_retval[0])
777df8bae1dSRodney W. Grimes 		goto done;
7784da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
779c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
78085f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
781df8bae1dSRodney W. Grimes 			goto done;
78200af9731SPoul-Henning Kamp 		ttv = atv;
78300af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
78400af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
78500af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
786df8bae1dSRodney W. Grimes 	}
78785f190e4SAlfred Perlstein 
78885f190e4SAlfred Perlstein 	/*
78985f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
79085f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of
79185f190e4SAlfred Perlstein 	 * collisions and rescan the file descriptors if
79285f190e4SAlfred Perlstein 	 * necessary.
79385f190e4SAlfred Perlstein 	 */
794fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
79585f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
79685f190e4SAlfred Perlstein 		mtx_unlock_spin(&sched_lock);
79785f190e4SAlfred Perlstein 		goto retry;
79885f190e4SAlfred Perlstein 	}
799fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
800bfbbc4aaSJason Evans 
801265fc98fSSeigo Tanimura 	if (timo > 0)
80285f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
803265fc98fSSeigo Tanimura 	else
80485f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
805bfbbc4aaSJason Evans 
806df8bae1dSRodney W. Grimes 	if (error == 0)
807df8bae1dSRodney W. Grimes 		goto retry;
808265fc98fSSeigo Tanimura 
809df8bae1dSRodney W. Grimes done:
81085f190e4SAlfred Perlstein 	clear_selinfo_list(td);
811fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
812b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
813fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
81485f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
81585f190e4SAlfred Perlstein 
81685f190e4SAlfred Perlstein done_nosellock:
817df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
818df8bae1dSRodney W. Grimes 	if (error == ERESTART)
819df8bae1dSRodney W. Grimes 		error = EINTR;
820df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
821df8bae1dSRodney W. Grimes 		error = 0;
822df8bae1dSRodney W. Grimes #define	putbits(name, x) \
8238f19eb88SIan Dowse 	if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
824df8bae1dSRodney W. Grimes 		error = error2;
825df8bae1dSRodney W. Grimes 	if (error == 0) {
826df8bae1dSRodney W. Grimes 		int error2;
827df8bae1dSRodney W. Grimes 
8288f19eb88SIan Dowse 		putbits(fd_in, 0);
8298f19eb88SIan Dowse 		putbits(fd_ou, 1);
8308f19eb88SIan Dowse 		putbits(fd_ex, 2);
831df8bae1dSRodney W. Grimes #undef putbits
832df8bae1dSRodney W. Grimes 	}
833d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
834d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
835ad2edad9SMatthew Dillon 
836df8bae1dSRodney W. Grimes 	return (error);
837df8bae1dSRodney W. Grimes }
838df8bae1dSRodney W. Grimes 
839265fc98fSSeigo Tanimura static int
840b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
841b40ce416SJulian Elischer 	struct thread *td;
842b08f7993SSujal Patel 	fd_mask **ibits, **obits;
843cb226aaaSPoul-Henning Kamp 	int nfd;
844df8bae1dSRodney W. Grimes {
845f082218cSPeter Wemm 	int msk, i, fd;
846f082218cSPeter Wemm 	fd_mask bits;
847df8bae1dSRodney W. Grimes 	struct file *fp;
848df8bae1dSRodney W. Grimes 	int n = 0;
8492087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
85042d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
851eb209311SAlfred Perlstein 	struct filedesc *fdp = td->td_proc->p_fd;
852df8bae1dSRodney W. Grimes 
853eb209311SAlfred Perlstein 	FILEDESC_LOCK(fdp);
854df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
855d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
856d5e4d7e1SBruce Evans 			continue;
857df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
858b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
859f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
860f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
861f082218cSPeter Wemm 				if (!(bits & 1))
862f082218cSPeter Wemm 					continue;
863eb209311SAlfred Perlstein 				if ((fp = fget_locked(fdp, fd)) == NULL) {
864eb209311SAlfred Perlstein 					FILEDESC_UNLOCK(fdp);
865df8bae1dSRodney W. Grimes 					return (EBADF);
866eb209311SAlfred Perlstein 				}
867ea6027a8SRobert Watson 				if (fo_poll(fp, flag[msk], td->td_ucred,
868ea6027a8SRobert Watson 				    td)) {
869b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
870f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
871df8bae1dSRodney W. Grimes 					n++;
872df8bae1dSRodney W. Grimes 				}
873df8bae1dSRodney W. Grimes 			}
874df8bae1dSRodney W. Grimes 		}
875df8bae1dSRodney W. Grimes 	}
876eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
877b40ce416SJulian Elischer 	td->td_retval[0] = n;
878df8bae1dSRodney W. Grimes 	return (0);
879df8bae1dSRodney W. Grimes }
880df8bae1dSRodney W. Grimes 
88142d11757SPeter Wemm /*
88242d11757SPeter Wemm  * Poll system call.
88342d11757SPeter Wemm  */
88442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
88542d11757SPeter Wemm struct poll_args {
88642d11757SPeter Wemm 	struct pollfd *fds;
88742d11757SPeter Wemm 	u_int	nfds;
88842d11757SPeter Wemm 	int	timeout;
88942d11757SPeter Wemm };
89042d11757SPeter Wemm #endif
891ad2edad9SMatthew Dillon /*
892ad2edad9SMatthew Dillon  * MPSAFE
893ad2edad9SMatthew Dillon  */
89442d11757SPeter Wemm int
895b40ce416SJulian Elischer poll(td, uap)
896b40ce416SJulian Elischer 	struct thread *td;
897ea0237edSJonathan Lemon 	struct poll_args *uap;
89842d11757SPeter Wemm {
8992580f4e5SAndre Oppermann 	struct pollfd *bits;
9002580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
90100af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
9029ae6d334SKelly Yancey 	int error = 0, timo;
9039ae6d334SKelly Yancey 	u_int ncoll, nfds;
90442d11757SPeter Wemm 	size_t ni;
90542d11757SPeter Wemm 
906d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
907ad2edad9SMatthew Dillon 
9085d8dd01dSRobert Watson 	/*
9092bd5ac33SPeter Wemm 	 * This is kinda bogus.  We have fd limits, but that is not
9102bd5ac33SPeter Wemm 	 * really related to the size of the pollfd array.  Make sure
9112bd5ac33SPeter Wemm 	 * we let the process use at least FD_SETSIZE entries and at
9122bd5ac33SPeter Wemm 	 * least enough for the current limits.  We want to be reasonably
9132bd5ac33SPeter Wemm 	 * safe, but not overly restrictive.
91489b71647SPeter Wemm 	 */
91591d5354aSJohn Baldwin 	PROC_LOCK(td->td_proc);
91691d5354aSJohn Baldwin 	if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) &&
917b40ce416SJulian Elischer 	    (nfds > FD_SETSIZE)) {
91891d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
919ad2edad9SMatthew Dillon 		error = EINVAL;
920ad2edad9SMatthew Dillon 		goto done2;
921ad2edad9SMatthew Dillon 	}
92291d5354aSJohn Baldwin 	PROC_UNLOCK(td->td_proc);
92389b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
92442d11757SPeter Wemm 	if (ni > sizeof(smallbits))
925a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
92642d11757SPeter Wemm 	else
92742d11757SPeter Wemm 		bits = smallbits;
928d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
92942d11757SPeter Wemm 	if (error)
93085f190e4SAlfred Perlstein 		goto done_nosellock;
931d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
932d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
933d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
93442d11757SPeter Wemm 		if (itimerfix(&atv)) {
93542d11757SPeter Wemm 			error = EINVAL;
93685f190e4SAlfred Perlstein 			goto done_nosellock;
93742d11757SPeter Wemm 		}
938c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
93900af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
9409c386f6bSJohn Baldwin 	} else {
94100af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
9429c386f6bSJohn Baldwin 		atv.tv_usec = 0;
9439c386f6bSJohn Baldwin 	}
94400af9731SPoul-Henning Kamp 	timo = 0;
9452149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
94685f190e4SAlfred Perlstein 	mtx_lock(&sellock);
94742d11757SPeter Wemm retry:
94842d11757SPeter Wemm 	ncoll = nselcoll;
949fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
950b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
951fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
95285f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
95385f190e4SAlfred Perlstein 
9542580f4e5SAndre Oppermann 	error = pollscan(td, bits, nfds);
95585f190e4SAlfred Perlstein 	mtx_lock(&sellock);
956b40ce416SJulian Elischer 	if (error || td->td_retval[0])
95742d11757SPeter Wemm 		goto done;
9584da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
959c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
96085f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
96142d11757SPeter Wemm 			goto done;
96200af9731SPoul-Henning Kamp 		ttv = atv;
96300af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
96400af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
96500af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
96642d11757SPeter Wemm 	}
96785f190e4SAlfred Perlstein 	/*
96885f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
96985f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of collisions
97085f190e4SAlfred Perlstein 	 * and rescan the file descriptors if necessary.
97185f190e4SAlfred Perlstein 	 */
972fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
97385f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
974fea2ab83SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
97585f190e4SAlfred Perlstein 		goto retry;
97685f190e4SAlfred Perlstein 	}
97785f190e4SAlfred Perlstein 	mtx_unlock_spin(&sched_lock);
97885f190e4SAlfred Perlstein 
979265fc98fSSeigo Tanimura 	if (timo > 0)
98085f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
981265fc98fSSeigo Tanimura 	else
98285f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
98385f190e4SAlfred Perlstein 
98442d11757SPeter Wemm 	if (error == 0)
98542d11757SPeter Wemm 		goto retry;
986265fc98fSSeigo Tanimura 
98742d11757SPeter Wemm done:
98885f190e4SAlfred Perlstein 	clear_selinfo_list(td);
989fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
990b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
991fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
99285f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
99385f190e4SAlfred Perlstein 
99485f190e4SAlfred Perlstein done_nosellock:
99542d11757SPeter Wemm 	/* poll is not restarted after signals... */
99642d11757SPeter Wemm 	if (error == ERESTART)
99742d11757SPeter Wemm 		error = EINTR;
99842d11757SPeter Wemm 	if (error == EWOULDBLOCK)
99942d11757SPeter Wemm 		error = 0;
100042d11757SPeter Wemm 	if (error == 0) {
1001d1e405c5SAlfred Perlstein 		error = copyout(bits, uap->fds, ni);
100242d11757SPeter Wemm 		if (error)
100342d11757SPeter Wemm 			goto out;
100442d11757SPeter Wemm 	}
100542d11757SPeter Wemm out:
100642d11757SPeter Wemm 	if (ni > sizeof(smallbits))
100742d11757SPeter Wemm 		free(bits, M_TEMP);
1008ad2edad9SMatthew Dillon done2:
100942d11757SPeter Wemm 	return (error);
101042d11757SPeter Wemm }
101142d11757SPeter Wemm 
101242d11757SPeter Wemm static int
1013b40ce416SJulian Elischer pollscan(td, fds, nfd)
1014b40ce416SJulian Elischer 	struct thread *td;
101542d11757SPeter Wemm 	struct pollfd *fds;
1016ea0237edSJonathan Lemon 	u_int nfd;
101742d11757SPeter Wemm {
1018b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
101942d11757SPeter Wemm 	int i;
102042d11757SPeter Wemm 	struct file *fp;
102142d11757SPeter Wemm 	int n = 0;
102242d11757SPeter Wemm 
1023426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
1024eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1025337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
102642d11757SPeter Wemm 			fds->revents = POLLNVAL;
102742d11757SPeter Wemm 			n++;
1028337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1029337c9691SJordan K. Hubbard 			fds->revents = 0;
103042d11757SPeter Wemm 		} else {
103142d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1032279d7226SMatthew Dillon 			if (fp == NULL) {
103342d11757SPeter Wemm 				fds->revents = POLLNVAL;
103442d11757SPeter Wemm 				n++;
103542d11757SPeter Wemm 			} else {
10362087c896SBruce Evans 				/*
10372087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
10382087c896SBruce Evans 				 * POLLERR if appropriate.
10392087c896SBruce Evans 				 */
104013ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1041ea6027a8SRobert Watson 				    td->td_ucred, td);
104242d11757SPeter Wemm 				if (fds->revents != 0)
104342d11757SPeter Wemm 					n++;
104442d11757SPeter Wemm 			}
104542d11757SPeter Wemm 		}
104642d11757SPeter Wemm 	}
1047eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
1048b40ce416SJulian Elischer 	td->td_retval[0] = n;
104942d11757SPeter Wemm 	return (0);
105042d11757SPeter Wemm }
105142d11757SPeter Wemm 
105242d11757SPeter Wemm /*
105342d11757SPeter Wemm  * OpenBSD poll system call.
105442d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
105542d11757SPeter Wemm  */
105642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
105742d11757SPeter Wemm struct openbsd_poll_args {
105842d11757SPeter Wemm 	struct pollfd *fds;
105942d11757SPeter Wemm 	u_int	nfds;
106042d11757SPeter Wemm 	int	timeout;
106142d11757SPeter Wemm };
106242d11757SPeter Wemm #endif
1063ad2edad9SMatthew Dillon /*
1064ad2edad9SMatthew Dillon  * MPSAFE
1065ad2edad9SMatthew Dillon  */
106642d11757SPeter Wemm int
1067b40ce416SJulian Elischer openbsd_poll(td, uap)
1068b40ce416SJulian Elischer 	register struct thread *td;
106942d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
107042d11757SPeter Wemm {
1071b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
107242d11757SPeter Wemm }
107342d11757SPeter Wemm 
107485f190e4SAlfred Perlstein /*
107585f190e4SAlfred Perlstein  * Remove the references to the thread from all of the objects
107685f190e4SAlfred Perlstein  * we were polling.
107785f190e4SAlfred Perlstein  *
107885f190e4SAlfred Perlstein  * This code assumes that the underlying owner of the selinfo
107985f190e4SAlfred Perlstein  * structure will hold sellock before it changes it, and that
108085f190e4SAlfred Perlstein  * it will unlink itself from our list if it goes away.
108185f190e4SAlfred Perlstein  */
108285f190e4SAlfred Perlstein void
108385f190e4SAlfred Perlstein clear_selinfo_list(td)
108485f190e4SAlfred Perlstein 	struct thread *td;
108585f190e4SAlfred Perlstein {
108685f190e4SAlfred Perlstein 	struct selinfo *si;
108785f190e4SAlfred Perlstein 
108885f190e4SAlfred Perlstein 	mtx_assert(&sellock, MA_OWNED);
108985f190e4SAlfred Perlstein 	TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
109085f190e4SAlfred Perlstein 		si->si_thread = NULL;
109185f190e4SAlfred Perlstein 	TAILQ_INIT(&td->td_selq);
109285f190e4SAlfred Perlstein }
109385f190e4SAlfred Perlstein 
1094df8bae1dSRodney W. Grimes /*
1095df8bae1dSRodney W. Grimes  * Record a select request.
1096df8bae1dSRodney W. Grimes  */
1097df8bae1dSRodney W. Grimes void
1098df8bae1dSRodney W. Grimes selrecord(selector, sip)
1099b40ce416SJulian Elischer 	struct thread *selector;
1100df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1101df8bae1dSRodney W. Grimes {
1102df8bae1dSRodney W. Grimes 
110385f190e4SAlfred Perlstein 	mtx_lock(&sellock);
110485f190e4SAlfred Perlstein 	/*
1105b605b54cSAlfred Perlstein 	 * If the selinfo's thread pointer is NULL then take ownership of it.
1106b605b54cSAlfred Perlstein 	 *
1107b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and it points to another
1108b605b54cSAlfred Perlstein 	 * thread, then we have a collision.
1109b605b54cSAlfred Perlstein 	 *
1110b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and points back to us then leave
1111b605b54cSAlfred Perlstein 	 * it alone as we've already added pointed it at us and added it to
1112b605b54cSAlfred Perlstein 	 * our list.
111385f190e4SAlfred Perlstein 	 */
111485f190e4SAlfred Perlstein 	if (sip->si_thread == NULL) {
1115b40ce416SJulian Elischer 		sip->si_thread = selector;
111685f190e4SAlfred Perlstein 		TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
111785f190e4SAlfred Perlstein 	} else if (sip->si_thread != selector) {
111885f190e4SAlfred Perlstein 		sip->si_flags |= SI_COLL;
111985f190e4SAlfred Perlstein 	}
112085f190e4SAlfred Perlstein 
112185f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1122df8bae1dSRodney W. Grimes }
1123df8bae1dSRodney W. Grimes 
1124512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1125df8bae1dSRodney W. Grimes void
1126df8bae1dSRodney W. Grimes selwakeup(sip)
112785f190e4SAlfred Perlstein 	struct selinfo *sip;
1128df8bae1dSRodney W. Grimes {
1129512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1130512824f8SSeigo Tanimura }
1131512824f8SSeigo Tanimura 
1132512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1133512824f8SSeigo Tanimura void
1134512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1135512824f8SSeigo Tanimura 	struct selinfo *sip;
1136512824f8SSeigo Tanimura 	int pri;
1137512824f8SSeigo Tanimura {
1138512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1139512824f8SSeigo Tanimura }
1140512824f8SSeigo Tanimura 
1141512824f8SSeigo Tanimura /*
1142512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1143512824f8SSeigo Tanimura  */
1144512824f8SSeigo Tanimura static void
1145512824f8SSeigo Tanimura doselwakeup(sip, pri)
1146512824f8SSeigo Tanimura 	struct selinfo *sip;
1147512824f8SSeigo Tanimura 	int pri;
1148512824f8SSeigo Tanimura {
1149b40ce416SJulian Elischer 	struct thread *td;
1150df8bae1dSRodney W. Grimes 
115185f190e4SAlfred Perlstein 	mtx_lock(&sellock);
115285f190e4SAlfred Perlstein 	td = sip->si_thread;
115385f190e4SAlfred Perlstein 	if ((sip->si_flags & SI_COLL) != 0) {
1154df8bae1dSRodney W. Grimes 		nselcoll++;
1155df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1156512824f8SSeigo Tanimura 		cv_broadcastpri(&selwait, pri);
1157df8bae1dSRodney W. Grimes 	}
115885f190e4SAlfred Perlstein 	if (td == NULL) {
115985f190e4SAlfred Perlstein 		mtx_unlock(&sellock);
1160b40ce416SJulian Elischer 		return;
1161b40ce416SJulian Elischer 	}
116285f190e4SAlfred Perlstein 	TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
116385f190e4SAlfred Perlstein 	sip->si_thread = NULL;
11649ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1165b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
116633a9ed9dSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
116744f3b092SJohn Baldwin 	sleepq_remove(td, &selwait);
116885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1169df8bae1dSRodney W. Grimes }
1170265fc98fSSeigo Tanimura 
11714d77a549SAlfred Perlstein static void selectinit(void *);
1172265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1173265fc98fSSeigo Tanimura 
1174265fc98fSSeigo Tanimura /* ARGSUSED*/
1175265fc98fSSeigo Tanimura static void
1176265fc98fSSeigo Tanimura selectinit(dummy)
1177265fc98fSSeigo Tanimura 	void *dummy;
1178265fc98fSSeigo Tanimura {
1179265fc98fSSeigo Tanimura 	cv_init(&selwait, "select");
11806008862bSJohn Baldwin 	mtx_init(&sellock, "sellck", NULL, MTX_DEF);
1181265fc98fSSeigo Tanimura }
1182