xref: /freebsd/sys/kern/sys_generic.c (revision b064d43d8f8e1ff9b7497ff1b1ab5332d80ab037)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46d2d3e875SBruce Evans #include <sys/sysproto.h>
47df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4820982410SBruce Evans #include <sys/filio.h>
493ac4d1efSBruce Evans #include <sys/fcntl.h>
50df8bae1dSRodney W. Grimes #include <sys/file.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
52797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
53df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
54df8bae1dSRodney W. Grimes #include <sys/uio.h>
55df8bae1dSRodney W. Grimes #include <sys/kernel.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>
608cb96f20SPeter Wemm #include <sys/sysctl.h>
6142d11757SPeter Wemm #include <sys/sysent.h>
62279d7226SMatthew Dillon #include <sys/bio.h>
63279d7226SMatthew Dillon #include <sys/buf.h>
64265fc98fSSeigo Tanimura #include <sys/condvar.h>
65df8bae1dSRodney W. Grimes #ifdef KTRACE
66df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
67df8bae1dSRodney W. Grimes #endif
68279d7226SMatthew Dillon #include <vm/vm.h>
69279d7226SMatthew Dillon #include <vm/vm_page.h>
70df8bae1dSRodney W. Grimes 
71069e9bc1SDoug Rabson #include <machine/limits.h>
72069e9bc1SDoug Rabson 
73a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
75a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7655166637SPoul-Henning Kamp 
77b40ce416SJulian Elischer static int	pollscan __P((struct thread *, struct pollfd *, u_int));
78b40ce416SJulian Elischer static int	pollholddrop __P((struct thread *, struct pollfd *, u_int, int));
79b40ce416SJulian Elischer static int	selscan __P((struct thread *, fd_mask **, fd_mask **, int));
80b40ce416SJulian Elischer static int	selholddrop __P((struct thread *, fd_mask *, fd_mask *, int, int));
81b40ce416SJulian Elischer static int	dofileread __P((struct thread *, struct file *, int, void *,
828fe387abSDmitrij Tejblum 		    size_t, off_t, int));
83b40ce416SJulian Elischer static int	dofilewrite __P((struct thread *, struct file *, int,
848fe387abSDmitrij Tejblum 		    const void *, size_t, off_t, 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 {
104b064d43dSMatthew Dillon 	struct file *fp;
105279d7226SMatthew Dillon 	int error;
106df8bae1dSRodney W. Grimes 
107ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
108b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) == 0) {
109b40ce416SJulian Elischer 		error = dofileread(td, fp, uap->fd, uap->buf,
110ad2edad9SMatthew Dillon 			    uap->nbyte, (off_t)-1, 0);
111b40ce416SJulian Elischer 		fdrop(fp, td);
112ad2edad9SMatthew Dillon 	}
113ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
114279d7226SMatthew Dillon 	return(error);
115df8bae1dSRodney W. Grimes }
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes /*
1188fe387abSDmitrij Tejblum  * Pread system call
1194160ccd9SAlan Cox  */
1204160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1214160ccd9SAlan Cox struct pread_args {
1224160ccd9SAlan Cox 	int	fd;
1234160ccd9SAlan Cox 	void	*buf;
1244160ccd9SAlan Cox 	size_t	nbyte;
1258fe387abSDmitrij Tejblum 	int	pad;
1264160ccd9SAlan Cox 	off_t	offset;
1274160ccd9SAlan Cox };
1284160ccd9SAlan Cox #endif
129ad2edad9SMatthew Dillon /*
130ad2edad9SMatthew Dillon  * MPSAFE
131ad2edad9SMatthew Dillon  */
1324160ccd9SAlan Cox int
133b40ce416SJulian Elischer pread(td, uap)
134b40ce416SJulian Elischer 	struct thread *td;
135b064d43dSMatthew Dillon 	struct pread_args *uap;
1364160ccd9SAlan Cox {
137b064d43dSMatthew Dillon 	struct file *fp;
138279d7226SMatthew Dillon 	int error;
1398fe387abSDmitrij Tejblum 
140ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
141b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) == 0) {
142b064d43dSMatthew Dillon 		if (fp->f_type == DTYPE_VNODE) {
143b064d43dSMatthew Dillon 			error = dofileread(td, fp, uap->fd, uap->buf,
144b064d43dSMatthew Dillon 				    uap->nbyte, uap->offset, FOF_OFFSET);
145279d7226SMatthew Dillon 		} else {
146b064d43dSMatthew Dillon 			error = ESPIPE;
147b064d43dSMatthew Dillon 		}
148b40ce416SJulian Elischer 		fdrop(fp, td);
149ad2edad9SMatthew Dillon 	}
150ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
151279d7226SMatthew Dillon 	return(error);
1528fe387abSDmitrij Tejblum }
1538fe387abSDmitrij Tejblum 
1548fe387abSDmitrij Tejblum /*
1558fe387abSDmitrij Tejblum  * Code common for read and pread
1568fe387abSDmitrij Tejblum  */
1578fe387abSDmitrij Tejblum int
158b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags)
159b40ce416SJulian Elischer 	struct thread *td;
1608fe387abSDmitrij Tejblum 	struct file *fp;
1618fe387abSDmitrij Tejblum 	int fd, flags;
1628fe387abSDmitrij Tejblum 	void *buf;
1638fe387abSDmitrij Tejblum 	size_t nbyte;
1648fe387abSDmitrij Tejblum 	off_t offset;
1658fe387abSDmitrij Tejblum {
1664160ccd9SAlan Cox 	struct uio auio;
1674160ccd9SAlan Cox 	struct iovec aiov;
1684160ccd9SAlan Cox 	long cnt, error = 0;
1694160ccd9SAlan Cox #ifdef KTRACE
1704160ccd9SAlan Cox 	struct iovec ktriov;
17142ebfbf2SBrian Feldman 	struct uio ktruio;
1723c89e357SBrian Feldman 	int didktr = 0;
1734160ccd9SAlan Cox #endif
1744160ccd9SAlan Cox 
1758fe387abSDmitrij Tejblum 	aiov.iov_base = (caddr_t)buf;
1768fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
1774160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1784160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
1798fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
1808fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
1814160ccd9SAlan Cox 		return (EINVAL);
1828fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
1834160ccd9SAlan Cox 	auio.uio_rw = UIO_READ;
1844160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
185b40ce416SJulian Elischer 	auio.uio_td = td;
1864160ccd9SAlan Cox #ifdef KTRACE
1874160ccd9SAlan Cox 	/*
1884160ccd9SAlan Cox 	 * if tracing, save a copy of iovec
1894160ccd9SAlan Cox 	 */
190b40ce416SJulian Elischer 	if (KTRPOINT(td->td_proc, KTR_GENIO)) {
1914160ccd9SAlan Cox 		ktriov = aiov;
19242ebfbf2SBrian Feldman 		ktruio = auio;
1933c89e357SBrian Feldman 		didktr = 1;
19442ebfbf2SBrian Feldman 	}
1954160ccd9SAlan Cox #endif
1968fe387abSDmitrij Tejblum 	cnt = nbyte;
197279d7226SMatthew Dillon 
198b40ce416SJulian Elischer 	if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) {
1994160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
2004160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
2014160ccd9SAlan Cox 			error = 0;
202279d7226SMatthew Dillon 	}
2034160ccd9SAlan Cox 	cnt -= auio.uio_resid;
2044160ccd9SAlan Cox #ifdef KTRACE
2053c89e357SBrian Feldman 	if (didktr && error == 0) {
20642ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
20742ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
208b40ce416SJulian Elischer 		ktrgenio(td->td_proc->p_tracep, fd, UIO_READ, &ktruio, error);
20942ebfbf2SBrian Feldman 	}
2104160ccd9SAlan Cox #endif
211b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
2124160ccd9SAlan Cox 	return (error);
2134160ccd9SAlan Cox }
2144160ccd9SAlan Cox 
2154160ccd9SAlan Cox /*
216df8bae1dSRodney W. Grimes  * Scatter read system call.
217df8bae1dSRodney W. Grimes  */
218d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
219df8bae1dSRodney W. Grimes struct readv_args {
2207147b19dSBruce Evans 	int	fd;
221df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
222df8bae1dSRodney W. Grimes 	u_int	iovcnt;
223df8bae1dSRodney W. Grimes };
224d2d3e875SBruce Evans #endif
225ad2edad9SMatthew Dillon /*
226ad2edad9SMatthew Dillon  * MPSAFE
227ad2edad9SMatthew Dillon  */
22826f9a767SRodney W. Grimes int
229b40ce416SJulian Elischer readv(td, uap)
230b40ce416SJulian Elischer 	struct thread *td;
231b064d43dSMatthew Dillon 	struct readv_args *uap;
232df8bae1dSRodney W. Grimes {
233b064d43dSMatthew Dillon 	struct file *fp;
234df8bae1dSRodney W. Grimes 	struct uio auio;
235b064d43dSMatthew Dillon 	struct iovec *iov;
236df8bae1dSRodney W. Grimes 	struct iovec *needfree;
237df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
238df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
239df8bae1dSRodney W. Grimes 	u_int iovlen;
240df8bae1dSRodney W. Grimes #ifdef KTRACE
241df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
24242ebfbf2SBrian Feldman 	struct uio ktruio;
243df8bae1dSRodney W. Grimes #endif
244ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
245df8bae1dSRodney W. Grimes 
246b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) != 0)
247ad2edad9SMatthew Dillon 		goto done2;
248df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
249df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
250df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
251ad2edad9SMatthew Dillon 		if (uap->iovcnt > UIO_MAXIOV) {
252ad2edad9SMatthew Dillon 			error = EINVAL;
253ad2edad9SMatthew Dillon 			goto done2;
254ad2edad9SMatthew Dillon 		}
255df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
256df8bae1dSRodney W. Grimes 		needfree = iov;
257df8bae1dSRodney W. Grimes 	} else {
258df8bae1dSRodney W. Grimes 		iov = aiov;
259df8bae1dSRodney W. Grimes 		needfree = NULL;
260df8bae1dSRodney W. Grimes 	}
261df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
262df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
263df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
264df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
265b40ce416SJulian Elischer 	auio.uio_td = td;
2662c1011f7SJohn Dyson 	auio.uio_offset = -1;
267bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
268df8bae1dSRodney W. Grimes 		goto done;
269df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
270df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
271069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
272df8bae1dSRodney W. Grimes 			error = EINVAL;
273df8bae1dSRodney W. Grimes 			goto done;
274df8bae1dSRodney W. Grimes 		}
275069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
276df8bae1dSRodney W. Grimes 		iov++;
277df8bae1dSRodney W. Grimes 	}
278df8bae1dSRodney W. Grimes #ifdef KTRACE
279df8bae1dSRodney W. Grimes 	/*
280df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
281df8bae1dSRodney W. Grimes 	 */
282b40ce416SJulian Elischer 	if (KTRPOINT(td->td_proc, KTR_GENIO))  {
283df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
284df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
28542ebfbf2SBrian Feldman 		ktruio = auio;
286df8bae1dSRodney W. Grimes 	}
287df8bae1dSRodney W. Grimes #endif
288df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
289b40ce416SJulian Elischer 	if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) {
290df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
291df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
292df8bae1dSRodney W. Grimes 			error = 0;
293279d7226SMatthew Dillon 	}
294df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
295df8bae1dSRodney W. Grimes #ifdef KTRACE
296df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
29742ebfbf2SBrian Feldman 		if (error == 0) {
29842ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
29942ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
300b40ce416SJulian Elischer 			ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio,
30142ebfbf2SBrian Feldman 			    error);
30242ebfbf2SBrian Feldman 		}
303df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
304df8bae1dSRodney W. Grimes 	}
305df8bae1dSRodney W. Grimes #endif
306b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
307df8bae1dSRodney W. Grimes done:
308b40ce416SJulian Elischer 	fdrop(fp, td);
309df8bae1dSRodney W. Grimes 	if (needfree)
310df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
311ad2edad9SMatthew Dillon done2:
312ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
313df8bae1dSRodney W. Grimes 	return (error);
314df8bae1dSRodney W. Grimes }
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes /*
317df8bae1dSRodney W. Grimes  * Write system call
318df8bae1dSRodney W. Grimes  */
319d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
320df8bae1dSRodney W. Grimes struct write_args {
321df8bae1dSRodney W. Grimes 	int	fd;
322134e06feSBruce Evans 	const void *buf;
323134e06feSBruce Evans 	size_t	nbyte;
324df8bae1dSRodney W. Grimes };
325d2d3e875SBruce Evans #endif
326ad2edad9SMatthew Dillon /*
327ad2edad9SMatthew Dillon  * MPSAFE
328ad2edad9SMatthew Dillon  */
32926f9a767SRodney W. Grimes int
330b40ce416SJulian Elischer write(td, uap)
331b40ce416SJulian Elischer 	struct thread *td;
332b064d43dSMatthew Dillon 	struct write_args *uap;
333df8bae1dSRodney W. Grimes {
334b064d43dSMatthew Dillon 	struct file *fp;
335279d7226SMatthew Dillon 	int error;
336df8bae1dSRodney W. Grimes 
337ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
338b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
339b40ce416SJulian Elischer 		error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte,
340ad2edad9SMatthew Dillon 			    (off_t)-1, 0);
341b40ce416SJulian Elischer 		fdrop(fp, td);
342ad2edad9SMatthew Dillon 	} else {
343b064d43dSMatthew Dillon 		error = EBADF;	/* XXX this can't be right */
344ad2edad9SMatthew Dillon 	}
345ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
346279d7226SMatthew Dillon 	return(error);
347df8bae1dSRodney W. Grimes }
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes /*
3508fe387abSDmitrij Tejblum  * Pwrite system call
3514160ccd9SAlan Cox  */
3524160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3534160ccd9SAlan Cox struct pwrite_args {
3544160ccd9SAlan Cox 	int	fd;
3554160ccd9SAlan Cox 	const void *buf;
3564160ccd9SAlan Cox 	size_t	nbyte;
3578fe387abSDmitrij Tejblum 	int	pad;
3584160ccd9SAlan Cox 	off_t	offset;
3594160ccd9SAlan Cox };
3604160ccd9SAlan Cox #endif
361ad2edad9SMatthew Dillon /*
362ad2edad9SMatthew Dillon  * MPSAFE
363ad2edad9SMatthew Dillon  */
3644160ccd9SAlan Cox int
365b40ce416SJulian Elischer pwrite(td, uap)
366b40ce416SJulian Elischer 	struct thread *td;
367b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3684160ccd9SAlan Cox {
369b064d43dSMatthew Dillon 	struct file *fp;
370279d7226SMatthew Dillon 	int error;
3718fe387abSDmitrij Tejblum 
372ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
373b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
374b064d43dSMatthew Dillon 		if (fp->f_type == DTYPE_VNODE) {
375b064d43dSMatthew Dillon 			error = dofilewrite(td, fp, uap->fd, uap->buf,
376b064d43dSMatthew Dillon 				    uap->nbyte, uap->offset, FOF_OFFSET);
377b064d43dSMatthew Dillon 		} else {
378279d7226SMatthew Dillon 			error = ESPIPE;
379b064d43dSMatthew Dillon 		}
380b40ce416SJulian Elischer 		fdrop(fp, td);
381279d7226SMatthew Dillon 	} else {
382b064d43dSMatthew Dillon 		error = EBADF;	/* this can't be right */
383ad2edad9SMatthew Dillon 	}
384ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
385279d7226SMatthew Dillon 	return(error);
3868fe387abSDmitrij Tejblum }
3878fe387abSDmitrij Tejblum 
3888fe387abSDmitrij Tejblum static int
389b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags)
390b40ce416SJulian Elischer 	struct thread *td;
3918fe387abSDmitrij Tejblum 	struct file *fp;
3928fe387abSDmitrij Tejblum 	int fd, flags;
3938fe387abSDmitrij Tejblum 	const void *buf;
3948fe387abSDmitrij Tejblum 	size_t nbyte;
3958fe387abSDmitrij Tejblum 	off_t offset;
3968fe387abSDmitrij Tejblum {
3974160ccd9SAlan Cox 	struct uio auio;
3984160ccd9SAlan Cox 	struct iovec aiov;
3994160ccd9SAlan Cox 	long cnt, error = 0;
4004160ccd9SAlan Cox #ifdef KTRACE
4014160ccd9SAlan Cox 	struct iovec ktriov;
40242ebfbf2SBrian Feldman 	struct uio ktruio;
4033c89e357SBrian Feldman 	int didktr = 0;
4044160ccd9SAlan Cox #endif
4054160ccd9SAlan Cox 
406b31ae1adSPeter Wemm 	aiov.iov_base = (void *)(uintptr_t)buf;
4078fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
4084160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4094160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
4108fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
4118fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
4124160ccd9SAlan Cox 		return (EINVAL);
4138fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
4144160ccd9SAlan Cox 	auio.uio_rw = UIO_WRITE;
4154160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
416b40ce416SJulian Elischer 	auio.uio_td = td;
4174160ccd9SAlan Cox #ifdef KTRACE
4184160ccd9SAlan Cox 	/*
41942ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
4204160ccd9SAlan Cox 	 */
421b40ce416SJulian Elischer 	if (KTRPOINT(td->td_proc, KTR_GENIO)) {
4224160ccd9SAlan Cox 		ktriov = aiov;
42342ebfbf2SBrian Feldman 		ktruio = auio;
4243c89e357SBrian Feldman 		didktr = 1;
42542ebfbf2SBrian Feldman 	}
4264160ccd9SAlan Cox #endif
4278fe387abSDmitrij Tejblum 	cnt = nbyte;
428c6ab5768SAlfred Perlstein 	if (fp->f_type == DTYPE_VNODE)
429279d7226SMatthew Dillon 		bwillwrite();
430b40ce416SJulian Elischer 	if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) {
4314160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
4324160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
4334160ccd9SAlan Cox 			error = 0;
43419eb87d2SJohn Baldwin 		if (error == EPIPE) {
435b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
436b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
437b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
43819eb87d2SJohn Baldwin 		}
4394160ccd9SAlan Cox 	}
4404160ccd9SAlan Cox 	cnt -= auio.uio_resid;
4414160ccd9SAlan Cox #ifdef KTRACE
4423c89e357SBrian Feldman 	if (didktr && error == 0) {
44342ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
44442ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
445b40ce416SJulian Elischer 		ktrgenio(td->td_proc->p_tracep, fd, UIO_WRITE, &ktruio, error);
44642ebfbf2SBrian Feldman 	}
4474160ccd9SAlan Cox #endif
448b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
4494160ccd9SAlan Cox 	return (error);
4504160ccd9SAlan Cox }
4514160ccd9SAlan Cox 
4524160ccd9SAlan Cox /*
453df8bae1dSRodney W. Grimes  * Gather write system call
454df8bae1dSRodney W. Grimes  */
455d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
456df8bae1dSRodney W. Grimes struct writev_args {
457df8bae1dSRodney W. Grimes 	int	fd;
458df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
459df8bae1dSRodney W. Grimes 	u_int	iovcnt;
460df8bae1dSRodney W. Grimes };
461d2d3e875SBruce Evans #endif
462ad2edad9SMatthew Dillon /*
463ad2edad9SMatthew Dillon  * MPSAFE
464ad2edad9SMatthew Dillon  */
46526f9a767SRodney W. Grimes int
466b40ce416SJulian Elischer writev(td, uap)
467b40ce416SJulian Elischer 	struct thread *td;
468df8bae1dSRodney W. Grimes 	register struct writev_args *uap;
469df8bae1dSRodney W. Grimes {
470b064d43dSMatthew Dillon 	struct file *fp;
471df8bae1dSRodney W. Grimes 	struct uio auio;
472df8bae1dSRodney W. Grimes 	register struct iovec *iov;
473df8bae1dSRodney W. Grimes 	struct iovec *needfree;
474df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
475df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
476df8bae1dSRodney W. Grimes 	u_int iovlen;
477df8bae1dSRodney W. Grimes #ifdef KTRACE
478df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
47942ebfbf2SBrian Feldman 	struct uio ktruio;
480df8bae1dSRodney W. Grimes #endif
481df8bae1dSRodney W. Grimes 
482ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
483b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) != 0) {
484ad2edad9SMatthew Dillon 		error = EBADF;
485ad2edad9SMatthew Dillon 		goto done2;
486ad2edad9SMatthew Dillon 	}
487df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
488df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
489df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
4901aa3e7ddSBrian Feldman 		if (uap->iovcnt > UIO_MAXIOV) {
4911aa3e7ddSBrian Feldman 			needfree = NULL;
4921aa3e7ddSBrian Feldman 			error = EINVAL;
4931aa3e7ddSBrian Feldman 			goto done;
4941aa3e7ddSBrian Feldman 		}
495df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
496df8bae1dSRodney W. Grimes 		needfree = iov;
497df8bae1dSRodney W. Grimes 	} else {
498df8bae1dSRodney W. Grimes 		iov = aiov;
499df8bae1dSRodney W. Grimes 		needfree = NULL;
500df8bae1dSRodney W. Grimes 	}
501df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
502df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
503df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
504df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
505b40ce416SJulian Elischer 	auio.uio_td = td;
5062c1011f7SJohn Dyson 	auio.uio_offset = -1;
507bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
508df8bae1dSRodney W. Grimes 		goto done;
509df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
510df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
511069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
512df8bae1dSRodney W. Grimes 			error = EINVAL;
513df8bae1dSRodney W. Grimes 			goto done;
514df8bae1dSRodney W. Grimes 		}
515069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
516df8bae1dSRodney W. Grimes 		iov++;
517df8bae1dSRodney W. Grimes 	}
518df8bae1dSRodney W. Grimes #ifdef KTRACE
519df8bae1dSRodney W. Grimes 	/*
52042ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
521df8bae1dSRodney W. Grimes 	 */
522b40ce416SJulian Elischer 	if (KTRPOINT(td->td_proc, KTR_GENIO))  {
523df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
524df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
52542ebfbf2SBrian Feldman 		ktruio = auio;
526df8bae1dSRodney W. Grimes 	}
527df8bae1dSRodney W. Grimes #endif
528df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
529a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5309440653dSMatthew Dillon 		bwillwrite();
531b40ce416SJulian Elischer 	if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) {
532df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
533df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
534df8bae1dSRodney W. Grimes 			error = 0;
53519eb87d2SJohn Baldwin 		if (error == EPIPE) {
536b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
537b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
538b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
53919eb87d2SJohn Baldwin 		}
540df8bae1dSRodney W. Grimes 	}
541df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
542df8bae1dSRodney W. Grimes #ifdef KTRACE
543df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
54442ebfbf2SBrian Feldman 		if (error == 0) {
54542ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
54642ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
547b40ce416SJulian Elischer 			ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_WRITE, &ktruio,
54842ebfbf2SBrian Feldman 			    error);
54942ebfbf2SBrian Feldman 		}
550df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
551df8bae1dSRodney W. Grimes 	}
552df8bae1dSRodney W. Grimes #endif
553b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
554df8bae1dSRodney W. Grimes done:
555b40ce416SJulian Elischer 	fdrop(fp, td);
556df8bae1dSRodney W. Grimes 	if (needfree)
557df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
558ad2edad9SMatthew Dillon done2:
559ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
560df8bae1dSRodney W. Grimes 	return (error);
561df8bae1dSRodney W. Grimes }
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes /*
564df8bae1dSRodney W. Grimes  * Ioctl system call
565df8bae1dSRodney W. Grimes  */
566d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
567df8bae1dSRodney W. Grimes struct ioctl_args {
568df8bae1dSRodney W. Grimes 	int	fd;
569069e9bc1SDoug Rabson 	u_long	com;
570df8bae1dSRodney W. Grimes 	caddr_t	data;
571df8bae1dSRodney W. Grimes };
572d2d3e875SBruce Evans #endif
573ad2edad9SMatthew Dillon /*
574ad2edad9SMatthew Dillon  * MPSAFE
575ad2edad9SMatthew Dillon  */
576df8bae1dSRodney W. Grimes /* ARGSUSED */
57726f9a767SRodney W. Grimes int
578b40ce416SJulian Elischer ioctl(td, uap)
579b40ce416SJulian Elischer 	struct thread *td;
580df8bae1dSRodney W. Grimes 	register struct ioctl_args *uap;
581df8bae1dSRodney W. Grimes {
582df8bae1dSRodney W. Grimes 	register struct file *fp;
583df8bae1dSRodney W. Grimes 	register struct filedesc *fdp;
584831b9ef2SDoug Rabson 	register u_long com;
585ad2edad9SMatthew Dillon 	int error = 0;
586df8bae1dSRodney W. Grimes 	register u_int size;
587df8bae1dSRodney W. Grimes 	caddr_t data, memp;
588df8bae1dSRodney W. Grimes 	int tmp;
589df8bae1dSRodney W. Grimes #define STK_PARAMS	128
590d2ba455cSMatthew Dillon 	union {
591df8bae1dSRodney W. Grimes 	    char stkbuf[STK_PARAMS];
592d2ba455cSMatthew Dillon 	    long align;
593d2ba455cSMatthew Dillon 	} ubuf;
594df8bae1dSRodney W. Grimes 
595ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
596b40ce416SJulian Elischer 	fdp = td->td_proc->p_fd;
597df8bae1dSRodney W. Grimes 	if ((u_int)uap->fd >= fdp->fd_nfiles ||
598ad2edad9SMatthew Dillon 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
599ad2edad9SMatthew Dillon 		error = EBADF;
600ad2edad9SMatthew Dillon 		goto done2;
601ad2edad9SMatthew Dillon 	}
602df8bae1dSRodney W. Grimes 
603ad2edad9SMatthew Dillon 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
604ad2edad9SMatthew Dillon 		error = EBADF;
605ad2edad9SMatthew Dillon 		goto done2;
606ad2edad9SMatthew Dillon 	}
607df8bae1dSRodney W. Grimes 
608df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
609df8bae1dSRodney W. Grimes 	case FIONCLEX:
610df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
611ad2edad9SMatthew Dillon 		goto done2;
612df8bae1dSRodney W. Grimes 	case FIOCLEX:
613df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
614ad2edad9SMatthew Dillon 		goto done2;
615df8bae1dSRodney W. Grimes 	}
616df8bae1dSRodney W. Grimes 
617df8bae1dSRodney W. Grimes 	/*
618df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
619df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
620df8bae1dSRodney W. Grimes 	 */
621df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
622ad2edad9SMatthew Dillon 	if (size > IOCPARM_MAX) {
623ad2edad9SMatthew Dillon 		error = ENOTTY;
624ad2edad9SMatthew Dillon 		goto done2;
625ad2edad9SMatthew Dillon 	}
626279d7226SMatthew Dillon 
627279d7226SMatthew Dillon 	fhold(fp);
628279d7226SMatthew Dillon 
629df8bae1dSRodney W. Grimes 	memp = NULL;
630d2ba455cSMatthew Dillon 	if (size > sizeof (ubuf.stkbuf)) {
631df8bae1dSRodney W. Grimes 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
632df8bae1dSRodney W. Grimes 		data = memp;
633279d7226SMatthew Dillon 	} else {
634d2ba455cSMatthew Dillon 		data = ubuf.stkbuf;
635279d7226SMatthew Dillon 	}
636df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
637df8bae1dSRodney W. Grimes 		if (size) {
638df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
639df8bae1dSRodney W. Grimes 			if (error) {
640df8bae1dSRodney W. Grimes 				if (memp)
641df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
642b40ce416SJulian Elischer 				fdrop(fp, td);
643ad2edad9SMatthew Dillon 				goto done2;
644df8bae1dSRodney W. Grimes 			}
645279d7226SMatthew Dillon 		} else {
646df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
647279d7226SMatthew Dillon 		}
648279d7226SMatthew Dillon 	} else if ((com&IOC_OUT) && size) {
649df8bae1dSRodney W. Grimes 		/*
650df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
651df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
652df8bae1dSRodney W. Grimes 		 */
653df8bae1dSRodney W. Grimes 		bzero(data, size);
654279d7226SMatthew Dillon 	} else if (com&IOC_VOID) {
655df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
656279d7226SMatthew Dillon 	}
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 	switch (com) {
659df8bae1dSRodney W. Grimes 
660df8bae1dSRodney W. Grimes 	case FIONBIO:
661bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
662df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
663df8bae1dSRodney W. Grimes 		else
664df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
665b40ce416SJulian Elischer 		error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
666df8bae1dSRodney W. Grimes 		break;
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes 	case FIOASYNC:
669bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
670df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
671df8bae1dSRodney W. Grimes 		else
672df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
673b40ce416SJulian Elischer 		error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
674df8bae1dSRodney W. Grimes 		break;
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	default:
677b40ce416SJulian Elischer 		error = fo_ioctl(fp, com, data, td);
678df8bae1dSRodney W. Grimes 		/*
679df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
680df8bae1dSRodney W. Grimes 		 * already set and checked above.
681df8bae1dSRodney W. Grimes 		 */
682df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
683df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
684df8bae1dSRodney W. Grimes 		break;
685df8bae1dSRodney W. Grimes 	}
686df8bae1dSRodney W. Grimes 	if (memp)
687df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
688b40ce416SJulian Elischer 	fdrop(fp, td);
689ad2edad9SMatthew Dillon done2:
690ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
691df8bae1dSRodney W. Grimes 	return (error);
692df8bae1dSRodney W. Grimes }
693df8bae1dSRodney W. Grimes 
6948cb96f20SPeter Wemm static int	nselcoll;	/* Select collisions since boot */
695265fc98fSSeigo Tanimura struct cv	selwait;
6968cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
697df8bae1dSRodney W. Grimes 
698df8bae1dSRodney W. Grimes /*
699df8bae1dSRodney W. Grimes  * Select system call.
700df8bae1dSRodney W. Grimes  */
701d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
702df8bae1dSRodney W. Grimes struct select_args {
703b08f7993SSujal Patel 	int	nd;
704df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
705df8bae1dSRodney W. Grimes 	struct	timeval *tv;
706df8bae1dSRodney W. Grimes };
707d2d3e875SBruce Evans #endif
708ad2edad9SMatthew Dillon /*
709ad2edad9SMatthew Dillon  * MPSAFE
710ad2edad9SMatthew Dillon  */
71126f9a767SRodney W. Grimes int
712b40ce416SJulian Elischer select(td, uap)
713b40ce416SJulian Elischer 	register struct thread *td;
714df8bae1dSRodney W. Grimes 	register struct select_args *uap;
715df8bae1dSRodney W. Grimes {
716d5e4d7e1SBruce Evans 	/*
717d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
718d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
719d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
720d5e4d7e1SBruce Evans 	 * of 256.
721d5e4d7e1SBruce Evans 	 */
722d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
723265fc98fSSeigo Tanimura 	fd_mask s_heldbits[howmany(2048, NFDBITS)];
724265fc98fSSeigo Tanimura 	fd_mask *ibits[3], *obits[3], *selbits, *sbp, *heldbits, *hibits, *hobits;
72500af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
726265fc98fSSeigo Tanimura 	int ncoll, error, timo, i;
727d5e4d7e1SBruce Evans 	u_int nbufbytes, ncpbytes, nfdbits;
728df8bae1dSRodney W. Grimes 
729b08f7993SSujal Patel 	if (uap->nd < 0)
730acbfbfeaSSujal Patel 		return (EINVAL);
731ad2edad9SMatthew Dillon 
732ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
733ad2edad9SMatthew Dillon 
734b40ce416SJulian Elischer 	if (uap->nd > td->td_proc->p_fd->fd_nfiles)
735b40ce416SJulian Elischer 		uap->nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
736b08f7993SSujal Patel 
737d5e4d7e1SBruce Evans 	/*
738d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
739d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
740d5e4d7e1SBruce Evans 	 */
741d5e4d7e1SBruce Evans 	nfdbits = roundup(uap->nd, NFDBITS);
742d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
743d5e4d7e1SBruce Evans 	nbufbytes = 0;
744d5e4d7e1SBruce Evans 	if (uap->in != NULL)
745d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
746d5e4d7e1SBruce Evans 	if (uap->ou != NULL)
747d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
748d5e4d7e1SBruce Evans 	if (uap->ex != NULL)
749d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
750d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
751d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
752d5e4d7e1SBruce Evans 	else
753d5e4d7e1SBruce Evans 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
754265fc98fSSeigo Tanimura 	if (2 * ncpbytes <= sizeof s_heldbits) {
755265fc98fSSeigo Tanimura 		bzero(s_heldbits, sizeof(s_heldbits));
756265fc98fSSeigo Tanimura 		heldbits = &s_heldbits[0];
757265fc98fSSeigo Tanimura 	} else
758265fc98fSSeigo Tanimura 		heldbits = malloc(2 * ncpbytes, M_SELECT, M_WAITOK | M_ZERO);
759b08f7993SSujal Patel 
760b08f7993SSujal Patel 	/*
761d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
762d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
763d5e4d7e1SBruce Evans 	 * together.
764b08f7993SSujal Patel 	 */
765d5e4d7e1SBruce Evans 	sbp = selbits;
766265fc98fSSeigo Tanimura 	hibits = heldbits + ncpbytes / sizeof *heldbits;
767265fc98fSSeigo Tanimura 	hobits = heldbits;
768df8bae1dSRodney W. Grimes #define	getbits(name, x) \
769d5e4d7e1SBruce Evans 	do {								\
770d5e4d7e1SBruce Evans 		if (uap->name == NULL)					\
771d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
772d5e4d7e1SBruce Evans 		else {							\
773d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
774d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
775d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
776d5e4d7e1SBruce Evans 			error = copyin(uap->name, ibits[x], ncpbytes);	\
777265fc98fSSeigo Tanimura 			if (error != 0)					\
778265fc98fSSeigo Tanimura 				goto done_noproclock;			\
779265fc98fSSeigo Tanimura 			for (i = 0;					\
780265fc98fSSeigo Tanimura 			     i < ncpbytes / sizeof ibits[i][0];		\
781265fc98fSSeigo Tanimura 			     i++)					\
782265fc98fSSeigo Tanimura 				hibits[i] |= ibits[x][i];		\
783e04ac2feSJohn Baldwin 		}							\
784d5e4d7e1SBruce Evans 	} while (0)
785df8bae1dSRodney W. Grimes 	getbits(in, 0);
786df8bae1dSRodney W. Grimes 	getbits(ou, 1);
787df8bae1dSRodney W. Grimes 	getbits(ex, 2);
788df8bae1dSRodney W. Grimes #undef	getbits
789d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
790d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	if (uap->tv) {
793df8bae1dSRodney W. Grimes 		error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
794df8bae1dSRodney W. Grimes 			sizeof (atv));
795265fc98fSSeigo Tanimura 		if (error)
796265fc98fSSeigo Tanimura 			goto done_noproclock;
797df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
798df8bae1dSRodney W. Grimes 			error = EINVAL;
799265fc98fSSeigo Tanimura 			goto done_noproclock;
800df8bae1dSRodney W. Grimes 		}
801c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
80200af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
8039c386f6bSJohn Baldwin 	} else {
80400af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
8059c386f6bSJohn Baldwin 		atv.tv_usec = 0;
8069c386f6bSJohn Baldwin 	}
807b40ce416SJulian Elischer 	selholddrop(td, hibits, hobits, uap->nd, 1);
80800af9731SPoul-Henning Kamp 	timo = 0;
809b40ce416SJulian Elischer 	PROC_LOCK(td->td_proc);
810df8bae1dSRodney W. Grimes retry:
811df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
812fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
813b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
814fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
815b40ce416SJulian Elischer 	PROC_UNLOCK(td->td_proc);
816b40ce416SJulian Elischer 	error = selscan(td, ibits, obits, uap->nd);
817b40ce416SJulian Elischer 	PROC_LOCK(td->td_proc);
818b40ce416SJulian Elischer 	if (error || td->td_retval[0])
819df8bae1dSRodney W. Grimes 		goto done;
8204da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
821c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
8221b369704SSeigo Tanimura 		if (timevalcmp(&rtv, &atv, >=)) {
8231b369704SSeigo Tanimura 			/*
8241b369704SSeigo Tanimura 			 * An event of our interest may occur during locking a process.
8251b369704SSeigo Tanimura 			 * In order to avoid missing the event that occured during locking
826b40ce416SJulian Elischer 			 * the process, test TDF_SELECT and rescan file descriptors if
8271b369704SSeigo Tanimura 			 * necessary.
8281b369704SSeigo Tanimura 			 */
829fea2ab83SJohn Baldwin 			mtx_lock_spin(&sched_lock);
830b40ce416SJulian Elischer 			if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
8311b369704SSeigo Tanimura 				ncoll = nselcoll;
832b40ce416SJulian Elischer 				td->td_flags |= TDF_SELECT;
833fea2ab83SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
834b40ce416SJulian Elischer 				PROC_UNLOCK(td->td_proc);
835b40ce416SJulian Elischer 				error = selscan(td, ibits, obits, uap->nd);
836b40ce416SJulian Elischer 				PROC_LOCK(td->td_proc);
837fea2ab83SJohn Baldwin 			} else
838fea2ab83SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
839df8bae1dSRodney W. Grimes 			goto done;
8401b369704SSeigo Tanimura 		}
84100af9731SPoul-Henning Kamp 		ttv = atv;
84200af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
84300af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
84400af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
845df8bae1dSRodney W. Grimes 	}
846fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
847b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
848fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
849bfbbc4aaSJason Evans 
850265fc98fSSeigo Tanimura 	if (timo > 0)
851b40ce416SJulian Elischer 		error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo);
852265fc98fSSeigo Tanimura 	else
853b40ce416SJulian Elischer 		error = cv_wait_sig(&selwait, &td->td_proc->p_mtx);
854bfbbc4aaSJason Evans 
855df8bae1dSRodney W. Grimes 	if (error == 0)
856df8bae1dSRodney W. Grimes 		goto retry;
857265fc98fSSeigo Tanimura 
858df8bae1dSRodney W. Grimes done:
859fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
860b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
861fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
862b40ce416SJulian Elischer 	PROC_UNLOCK(td->td_proc);
863b40ce416SJulian Elischer 	selholddrop(td, hibits, hobits, uap->nd, 0);
864265fc98fSSeigo Tanimura done_noproclock:
865df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
866df8bae1dSRodney W. Grimes 	if (error == ERESTART)
867df8bae1dSRodney W. Grimes 		error = EINTR;
868df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
869df8bae1dSRodney W. Grimes 		error = 0;
870df8bae1dSRodney W. Grimes #define	putbits(name, x) \
871d5e4d7e1SBruce Evans 	if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
872df8bae1dSRodney W. Grimes 		error = error2;
873df8bae1dSRodney W. Grimes 	if (error == 0) {
874df8bae1dSRodney W. Grimes 		int error2;
875df8bae1dSRodney W. Grimes 
876df8bae1dSRodney W. Grimes 		putbits(in, 0);
877df8bae1dSRodney W. Grimes 		putbits(ou, 1);
878df8bae1dSRodney W. Grimes 		putbits(ex, 2);
879df8bae1dSRodney W. Grimes #undef putbits
880df8bae1dSRodney W. Grimes 	}
881d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
882d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
883265fc98fSSeigo Tanimura 	if (heldbits != &s_heldbits[0])
884265fc98fSSeigo Tanimura 		free(heldbits, M_SELECT);
885ad2edad9SMatthew Dillon 
886ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
887df8bae1dSRodney W. Grimes 	return (error);
888df8bae1dSRodney W. Grimes }
889df8bae1dSRodney W. Grimes 
89087b6de2bSPoul-Henning Kamp static int
891b40ce416SJulian Elischer selholddrop(td, ibits, obits, nfd, hold)
892b40ce416SJulian Elischer 	struct thread *td;
893265fc98fSSeigo Tanimura 	fd_mask *ibits, *obits;
894265fc98fSSeigo Tanimura 	int nfd, hold;
895265fc98fSSeigo Tanimura {
896b40ce416SJulian Elischer 	struct filedesc *fdp = td->td_proc->p_fd;
897265fc98fSSeigo Tanimura 	int i, fd;
898265fc98fSSeigo Tanimura 	fd_mask bits;
899265fc98fSSeigo Tanimura 	struct file *fp;
900265fc98fSSeigo Tanimura 
901265fc98fSSeigo Tanimura 	for (i = 0; i < nfd; i += NFDBITS) {
902265fc98fSSeigo Tanimura 		if (hold)
903265fc98fSSeigo Tanimura 			bits = ibits[i/NFDBITS];
904265fc98fSSeigo Tanimura 		else
905265fc98fSSeigo Tanimura 			bits = obits[i/NFDBITS];
906265fc98fSSeigo Tanimura 		/* ffs(int mask) not portable, fd_mask is long */
907265fc98fSSeigo Tanimura 		for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
908265fc98fSSeigo Tanimura 			if (!(bits & 1))
909265fc98fSSeigo Tanimura 				continue;
910265fc98fSSeigo Tanimura 			fp = fdp->fd_ofiles[fd];
911265fc98fSSeigo Tanimura 			if (fp == NULL)
912265fc98fSSeigo Tanimura 				return (EBADF);
913265fc98fSSeigo Tanimura 			if (hold) {
914265fc98fSSeigo Tanimura 				fhold(fp);
915265fc98fSSeigo Tanimura 				obits[(fd)/NFDBITS] |=
916265fc98fSSeigo Tanimura 				    ((fd_mask)1 << ((fd) % NFDBITS));
917265fc98fSSeigo Tanimura 			} else
918b40ce416SJulian Elischer 				fdrop(fp, td);
919265fc98fSSeigo Tanimura 		}
920265fc98fSSeigo Tanimura 	}
921265fc98fSSeigo Tanimura 	return (0);
922265fc98fSSeigo Tanimura }
923265fc98fSSeigo Tanimura 
924265fc98fSSeigo Tanimura static int
925b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
926b40ce416SJulian Elischer 	struct thread *td;
927b08f7993SSujal Patel 	fd_mask **ibits, **obits;
928cb226aaaSPoul-Henning Kamp 	int nfd;
929df8bae1dSRodney W. Grimes {
930b40ce416SJulian Elischer 	struct filedesc *fdp = td->td_proc->p_fd;
931f082218cSPeter Wemm 	int msk, i, fd;
932f082218cSPeter Wemm 	fd_mask bits;
933df8bae1dSRodney W. Grimes 	struct file *fp;
934df8bae1dSRodney W. Grimes 	int n = 0;
9352087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
93642d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
937df8bae1dSRodney W. Grimes 
938df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
939d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
940d5e4d7e1SBruce Evans 			continue;
941df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
942b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
943f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
944f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
945f082218cSPeter Wemm 				if (!(bits & 1))
946f082218cSPeter Wemm 					continue;
947df8bae1dSRodney W. Grimes 				fp = fdp->fd_ofiles[fd];
948df8bae1dSRodney W. Grimes 				if (fp == NULL)
949df8bae1dSRodney W. Grimes 					return (EBADF);
950b40ce416SJulian Elischer 				if (fo_poll(fp, flag[msk], fp->f_cred, td)) {
951b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
952f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
953df8bae1dSRodney W. Grimes 					n++;
954df8bae1dSRodney W. Grimes 				}
955df8bae1dSRodney W. Grimes 			}
956df8bae1dSRodney W. Grimes 		}
957df8bae1dSRodney W. Grimes 	}
958b40ce416SJulian Elischer 	td->td_retval[0] = n;
959df8bae1dSRodney W. Grimes 	return (0);
960df8bae1dSRodney W. Grimes }
961df8bae1dSRodney W. Grimes 
96242d11757SPeter Wemm /*
96342d11757SPeter Wemm  * Poll system call.
96442d11757SPeter Wemm  */
96542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
96642d11757SPeter Wemm struct poll_args {
96742d11757SPeter Wemm 	struct pollfd *fds;
96842d11757SPeter Wemm 	u_int	nfds;
96942d11757SPeter Wemm 	int	timeout;
97042d11757SPeter Wemm };
97142d11757SPeter Wemm #endif
972ad2edad9SMatthew Dillon /*
973ad2edad9SMatthew Dillon  * MPSAFE
974ad2edad9SMatthew Dillon  */
97542d11757SPeter Wemm int
976b40ce416SJulian Elischer poll(td, uap)
977b40ce416SJulian Elischer 	struct thread *td;
978ea0237edSJonathan Lemon 	struct poll_args *uap;
97942d11757SPeter Wemm {
98042d11757SPeter Wemm 	caddr_t bits;
98142d11757SPeter Wemm 	char smallbits[32 * sizeof(struct pollfd)];
98200af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
983265fc98fSSeigo Tanimura 	int ncoll, error = 0, timo;
984ea0237edSJonathan Lemon 	u_int nfds;
98542d11757SPeter Wemm 	size_t ni;
986265fc98fSSeigo Tanimura 	struct pollfd p_heldbits[32];
987265fc98fSSeigo Tanimura 	struct pollfd *heldbits;
98842d11757SPeter Wemm 
98989b71647SPeter Wemm 	nfds = SCARG(uap, nfds);
990ad2edad9SMatthew Dillon 
991ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
99289b71647SPeter Wemm 	/*
9932bd5ac33SPeter Wemm 	 * This is kinda bogus.  We have fd limits, but that is not
9942bd5ac33SPeter Wemm 	 * really related to the size of the pollfd array.  Make sure
9952bd5ac33SPeter Wemm 	 * we let the process use at least FD_SETSIZE entries and at
9962bd5ac33SPeter Wemm 	 * least enough for the current limits.  We want to be reasonably
9972bd5ac33SPeter Wemm 	 * safe, but not overly restrictive.
99889b71647SPeter Wemm 	 */
999b40ce416SJulian Elischer 	if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) &&
1000b40ce416SJulian Elischer 	    (nfds > FD_SETSIZE)) {
1001ad2edad9SMatthew Dillon 		error = EINVAL;
1002ad2edad9SMatthew Dillon 		goto done2;
1003ad2edad9SMatthew Dillon 	}
100489b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
100542d11757SPeter Wemm 	if (ni > sizeof(smallbits))
100642d11757SPeter Wemm 		bits = malloc(ni, M_TEMP, M_WAITOK);
100742d11757SPeter Wemm 	else
100842d11757SPeter Wemm 		bits = smallbits;
1009265fc98fSSeigo Tanimura 	if (ni > sizeof(p_heldbits))
1010265fc98fSSeigo Tanimura 		heldbits = malloc(ni, M_TEMP, M_WAITOK);
1011265fc98fSSeigo Tanimura 	else {
1012265fc98fSSeigo Tanimura 		bzero(p_heldbits, sizeof(p_heldbits));
1013265fc98fSSeigo Tanimura 		heldbits = p_heldbits;
1014265fc98fSSeigo Tanimura 	}
101542d11757SPeter Wemm 	error = copyin(SCARG(uap, fds), bits, ni);
101642d11757SPeter Wemm 	if (error)
1017265fc98fSSeigo Tanimura 		goto done_noproclock;
1018265fc98fSSeigo Tanimura 	bcopy(bits, heldbits, ni);
101942d11757SPeter Wemm 	if (SCARG(uap, timeout) != INFTIM) {
102042d11757SPeter Wemm 		atv.tv_sec = SCARG(uap, timeout) / 1000;
102142d11757SPeter Wemm 		atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
102242d11757SPeter Wemm 		if (itimerfix(&atv)) {
102342d11757SPeter Wemm 			error = EINVAL;
1024265fc98fSSeigo Tanimura 			goto done_noproclock;
102542d11757SPeter Wemm 		}
1026c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
102700af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
10289c386f6bSJohn Baldwin 	} else {
102900af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
10309c386f6bSJohn Baldwin 		atv.tv_usec = 0;
10319c386f6bSJohn Baldwin 	}
1032b40ce416SJulian Elischer 	pollholddrop(td, heldbits, nfds, 1);
103300af9731SPoul-Henning Kamp 	timo = 0;
1034b40ce416SJulian Elischer 	PROC_LOCK(td->td_proc);
103542d11757SPeter Wemm retry:
103642d11757SPeter Wemm 	ncoll = nselcoll;
1037fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
1038b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
1039fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
1040b40ce416SJulian Elischer 	PROC_UNLOCK(td->td_proc);
1041b40ce416SJulian Elischer 	error = pollscan(td, (struct pollfd *)bits, nfds);
1042b40ce416SJulian Elischer 	PROC_LOCK(td->td_proc);
1043b40ce416SJulian Elischer 	if (error || td->td_retval[0])
104442d11757SPeter Wemm 		goto done;
10454da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
1046c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
10471b369704SSeigo Tanimura 		if (timevalcmp(&rtv, &atv, >=)) {
10481b369704SSeigo Tanimura 			/*
10491b369704SSeigo Tanimura 			 * An event of our interest may occur during locking a process.
10501b369704SSeigo Tanimura 			 * In order to avoid missing the event that occured during locking
1051b40ce416SJulian Elischer 			 * the process, test TDF_SELECT and rescan file descriptors if
10521b369704SSeigo Tanimura 			 * necessary.
10531b369704SSeigo Tanimura 			 */
1054fea2ab83SJohn Baldwin 			mtx_lock_spin(&sched_lock);
1055b40ce416SJulian Elischer 			if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
10561b369704SSeigo Tanimura 				ncoll = nselcoll;
1057b40ce416SJulian Elischer 				td->td_flags |= TDF_SELECT;
1058fea2ab83SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
1059b40ce416SJulian Elischer 				PROC_UNLOCK(td->td_proc);
1060b40ce416SJulian Elischer 				error = pollscan(td, (struct pollfd *)bits, nfds);
1061b40ce416SJulian Elischer 				PROC_LOCK(td->td_proc);
1062fea2ab83SJohn Baldwin 			} else
1063fea2ab83SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
106442d11757SPeter Wemm 			goto done;
10651b369704SSeigo Tanimura 		}
106600af9731SPoul-Henning Kamp 		ttv = atv;
106700af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
106800af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
106900af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
107042d11757SPeter Wemm 	}
1071fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
1072b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
1073fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
1074265fc98fSSeigo Tanimura 	if (timo > 0)
1075b40ce416SJulian Elischer 		error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo);
1076265fc98fSSeigo Tanimura 	else
1077b40ce416SJulian Elischer 		error = cv_wait_sig(&selwait, &td->td_proc->p_mtx);
107842d11757SPeter Wemm 	if (error == 0)
107942d11757SPeter Wemm 		goto retry;
1080265fc98fSSeigo Tanimura 
108142d11757SPeter Wemm done:
1082fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
1083b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
1084fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
1085b40ce416SJulian Elischer 	PROC_UNLOCK(td->td_proc);
1086b40ce416SJulian Elischer 	pollholddrop(td, heldbits, nfds, 0);
1087265fc98fSSeigo Tanimura done_noproclock:
108842d11757SPeter Wemm 	/* poll is not restarted after signals... */
108942d11757SPeter Wemm 	if (error == ERESTART)
109042d11757SPeter Wemm 		error = EINTR;
109142d11757SPeter Wemm 	if (error == EWOULDBLOCK)
109242d11757SPeter Wemm 		error = 0;
109342d11757SPeter Wemm 	if (error == 0) {
109442d11757SPeter Wemm 		error = copyout(bits, SCARG(uap, fds), ni);
109542d11757SPeter Wemm 		if (error)
109642d11757SPeter Wemm 			goto out;
109742d11757SPeter Wemm 	}
109842d11757SPeter Wemm out:
109942d11757SPeter Wemm 	if (ni > sizeof(smallbits))
110042d11757SPeter Wemm 		free(bits, M_TEMP);
1101265fc98fSSeigo Tanimura 	if (ni > sizeof(p_heldbits))
1102265fc98fSSeigo Tanimura 		free(heldbits, M_TEMP);
1103ad2edad9SMatthew Dillon done2:
1104ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
110542d11757SPeter Wemm 	return (error);
110642d11757SPeter Wemm }
110742d11757SPeter Wemm 
110842d11757SPeter Wemm static int
1109b40ce416SJulian Elischer pollholddrop(td, fds, nfd, hold)
1110b40ce416SJulian Elischer 	struct thread *td;
1111265fc98fSSeigo Tanimura 	struct pollfd *fds;
1112265fc98fSSeigo Tanimura 	u_int nfd;
1113265fc98fSSeigo Tanimura 	int hold;
1114265fc98fSSeigo Tanimura {
1115b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
1116265fc98fSSeigo Tanimura 	int i;
1117265fc98fSSeigo Tanimura 	struct file *fp;
1118265fc98fSSeigo Tanimura 
1119265fc98fSSeigo Tanimura 	for (i = 0; i < nfd; i++, fds++) {
1120265fc98fSSeigo Tanimura 		if (0 <= fds->fd && fds->fd < fdp->fd_nfiles) {
1121265fc98fSSeigo Tanimura 			fp = fdp->fd_ofiles[fds->fd];
1122265fc98fSSeigo Tanimura 			if (hold) {
1123265fc98fSSeigo Tanimura 				if (fp != NULL) {
1124265fc98fSSeigo Tanimura 					fhold(fp);
1125265fc98fSSeigo Tanimura 					fds->revents = 1;
1126265fc98fSSeigo Tanimura 				} else
1127265fc98fSSeigo Tanimura 					fds->revents = 0;
1128265fc98fSSeigo Tanimura 			} else if(fp != NULL && fds->revents)
1129b40ce416SJulian Elischer 				fdrop(fp, td);
1130265fc98fSSeigo Tanimura 		}
1131265fc98fSSeigo Tanimura 	}
1132265fc98fSSeigo Tanimura 	return (0);
1133265fc98fSSeigo Tanimura }
1134265fc98fSSeigo Tanimura 
1135265fc98fSSeigo Tanimura static int
1136b40ce416SJulian Elischer pollscan(td, fds, nfd)
1137b40ce416SJulian Elischer 	struct thread *td;
113842d11757SPeter Wemm 	struct pollfd *fds;
1139ea0237edSJonathan Lemon 	u_int nfd;
114042d11757SPeter Wemm {
1141b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
114242d11757SPeter Wemm 	int i;
114342d11757SPeter Wemm 	struct file *fp;
114442d11757SPeter Wemm 	int n = 0;
114542d11757SPeter Wemm 
114642d11757SPeter Wemm 	for (i = 0; i < nfd; i++, fds++) {
1147337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
114842d11757SPeter Wemm 			fds->revents = POLLNVAL;
114942d11757SPeter Wemm 			n++;
1150337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1151337c9691SJordan K. Hubbard 			fds->revents = 0;
115242d11757SPeter Wemm 		} else {
115342d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1154279d7226SMatthew Dillon 			if (fp == NULL) {
115542d11757SPeter Wemm 				fds->revents = POLLNVAL;
115642d11757SPeter Wemm 				n++;
115742d11757SPeter Wemm 			} else {
11582087c896SBruce Evans 				/*
11592087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
11602087c896SBruce Evans 				 * POLLERR if appropriate.
11612087c896SBruce Evans 				 */
116213ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1163b40ce416SJulian Elischer 				    fp->f_cred, td);
116442d11757SPeter Wemm 				if (fds->revents != 0)
116542d11757SPeter Wemm 					n++;
116642d11757SPeter Wemm 			}
116742d11757SPeter Wemm 		}
116842d11757SPeter Wemm 	}
1169b40ce416SJulian Elischer 	td->td_retval[0] = n;
117042d11757SPeter Wemm 	return (0);
117142d11757SPeter Wemm }
117242d11757SPeter Wemm 
117342d11757SPeter Wemm /*
117442d11757SPeter Wemm  * OpenBSD poll system call.
117542d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
117642d11757SPeter Wemm  */
117742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
117842d11757SPeter Wemm struct openbsd_poll_args {
117942d11757SPeter Wemm 	struct pollfd *fds;
118042d11757SPeter Wemm 	u_int	nfds;
118142d11757SPeter Wemm 	int	timeout;
118242d11757SPeter Wemm };
118342d11757SPeter Wemm #endif
1184ad2edad9SMatthew Dillon /*
1185ad2edad9SMatthew Dillon  * MPSAFE
1186ad2edad9SMatthew Dillon  */
118742d11757SPeter Wemm int
1188b40ce416SJulian Elischer openbsd_poll(td, uap)
1189b40ce416SJulian Elischer 	register struct thread *td;
119042d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
119142d11757SPeter Wemm {
1192b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
119342d11757SPeter Wemm }
119442d11757SPeter Wemm 
1195df8bae1dSRodney W. Grimes /*ARGSUSED*/
119626f9a767SRodney W. Grimes int
1197b40ce416SJulian Elischer seltrue(dev, events, td)
1198df8bae1dSRodney W. Grimes 	dev_t dev;
119942d11757SPeter Wemm 	int events;
1200b40ce416SJulian Elischer 	struct thread *td;
1201df8bae1dSRodney W. Grimes {
1202df8bae1dSRodney W. Grimes 
120342d11757SPeter Wemm 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1204df8bae1dSRodney W. Grimes }
1205df8bae1dSRodney W. Grimes 
1206b40ce416SJulian Elischer static int
1207b40ce416SJulian Elischer find_thread_in_proc(struct proc *p, struct thread *td)
1208b40ce416SJulian Elischer {
1209b40ce416SJulian Elischer 	struct thread *td2;
1210b40ce416SJulian Elischer 	FOREACH_THREAD_IN_PROC(p, td2) {
1211b40ce416SJulian Elischer 		if (td2 == td) {
1212b40ce416SJulian Elischer 			return (1);
1213b40ce416SJulian Elischer 		}
1214b40ce416SJulian Elischer 	}
1215b40ce416SJulian Elischer 	return (0);
1216b40ce416SJulian Elischer }
1217b40ce416SJulian Elischer 
1218df8bae1dSRodney W. Grimes /*
1219df8bae1dSRodney W. Grimes  * Record a select request.
1220df8bae1dSRodney W. Grimes  */
1221df8bae1dSRodney W. Grimes void
1222df8bae1dSRodney W. Grimes selrecord(selector, sip)
1223b40ce416SJulian Elischer 	struct thread *selector;
1224df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1225df8bae1dSRodney W. Grimes {
1226df8bae1dSRodney W. Grimes 	struct proc *p;
1227df8bae1dSRodney W. Grimes 	pid_t mypid;
1228df8bae1dSRodney W. Grimes 
1229b40ce416SJulian Elischer 	mypid = selector->td_proc->p_pid;
1230b40ce416SJulian Elischer 	if ((sip->si_pid == mypid) &&
1231b40ce416SJulian Elischer 	    (sip->si_thread == selector)) { /* XXXKSE should be an ID? */
1232df8bae1dSRodney W. Grimes 		return;
1233b40ce416SJulian Elischer 	}
1234b40ce416SJulian Elischer 	if (sip->si_pid &&
1235b40ce416SJulian Elischer 	    (p = pfind(sip->si_pid)) &&
1236b40ce416SJulian Elischer 	    (find_thread_in_proc(p, sip->si_thread))) {
12379ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
1238b40ce416SJulian Elischer 	    	if (sip->si_thread->td_wchan == (caddr_t)&selwait) {
12399ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
124033a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
1241df8bae1dSRodney W. Grimes 			sip->si_flags |= SI_COLL;
1242e04ac2feSJohn Baldwin 			return;
1243e04ac2feSJohn Baldwin 		}
12449ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
124533a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
1246e04ac2feSJohn Baldwin 	}
1247df8bae1dSRodney W. Grimes 	sip->si_pid = mypid;
1248b40ce416SJulian Elischer 	sip->si_thread = selector;
1249df8bae1dSRodney W. Grimes }
1250df8bae1dSRodney W. Grimes 
1251df8bae1dSRodney W. Grimes /*
1252df8bae1dSRodney W. Grimes  * Do a wakeup when a selectable event occurs.
1253df8bae1dSRodney W. Grimes  */
1254df8bae1dSRodney W. Grimes void
1255df8bae1dSRodney W. Grimes selwakeup(sip)
1256df8bae1dSRodney W. Grimes 	register struct selinfo *sip;
1257df8bae1dSRodney W. Grimes {
1258b40ce416SJulian Elischer 	struct thread *td;
1259df8bae1dSRodney W. Grimes 	register struct proc *p;
1260df8bae1dSRodney W. Grimes 
1261df8bae1dSRodney W. Grimes 	if (sip->si_pid == 0)
1262df8bae1dSRodney W. Grimes 		return;
1263df8bae1dSRodney W. Grimes 	if (sip->si_flags & SI_COLL) {
1264df8bae1dSRodney W. Grimes 		nselcoll++;
1265df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1266265fc98fSSeigo Tanimura 		cv_broadcast(&selwait);
1267df8bae1dSRodney W. Grimes 	}
1268df8bae1dSRodney W. Grimes 	p = pfind(sip->si_pid);
1269df8bae1dSRodney W. Grimes 	sip->si_pid = 0;
1270b40ce416SJulian Elischer 	td = sip->si_thread;
1271df8bae1dSRodney W. Grimes 	if (p != NULL) {
1272b40ce416SJulian Elischer 		if (!find_thread_in_proc(p, td)) {
1273b40ce416SJulian Elischer 			PROC_UNLOCK(p); /* lock is in pfind() */;
1274b40ce416SJulian Elischer 			return;
1275b40ce416SJulian Elischer 		}
12769ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
1277b40ce416SJulian Elischer 		if (td->td_wchan == (caddr_t)&selwait) {
1278b40ce416SJulian Elischer 			if (td->td_proc->p_stat == SSLEEP)
1279b40ce416SJulian Elischer 				setrunnable(td);
1280df8bae1dSRodney W. Grimes 			else
1281b40ce416SJulian Elischer 				cv_waitq_remove(td);
128233a9ed9dSJohn Baldwin 		} else
1283b40ce416SJulian Elischer 			td->td_flags &= ~TDF_SELECT;
128433a9ed9dSJohn Baldwin 		mtx_unlock_spin(&sched_lock);
1285b40ce416SJulian Elischer 		PROC_UNLOCK(p); /* Lock is in pfind() */
1286e04ac2feSJohn Baldwin 	}
1287df8bae1dSRodney W. Grimes }
1288265fc98fSSeigo Tanimura 
1289265fc98fSSeigo Tanimura static void selectinit __P((void *));
1290265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1291265fc98fSSeigo Tanimura 
1292265fc98fSSeigo Tanimura /* ARGSUSED*/
1293265fc98fSSeigo Tanimura static void
1294265fc98fSSeigo Tanimura selectinit(dummy)
1295265fc98fSSeigo Tanimura 	void *dummy;
1296265fc98fSSeigo Tanimura {
1297265fc98fSSeigo Tanimura 	cv_init(&selwait, "select");
1298265fc98fSSeigo Tanimura }
1299