xref: /freebsd/sys/kern/sys_generic.c (revision 677b542ea243380af64822b30e4ef5f7a6d978ee)
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
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41677b542eSDavid E. O'Brien #include <sys/cdefs.h>
42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
43677b542eSDavid E. O'Brien 
44db6a20e2SGarrett Wollman #include "opt_ktrace.h"
45db6a20e2SGarrett Wollman 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
48d2d3e875SBruce Evans #include <sys/sysproto.h>
49df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
5020982410SBruce Evans #include <sys/filio.h>
513ac4d1efSBruce Evans #include <sys/fcntl.h>
52df8bae1dSRodney W. Grimes #include <sys/file.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
54797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
56df8bae1dSRodney W. Grimes #include <sys/uio.h>
57df8bae1dSRodney W. Grimes #include <sys/kernel.h>
58104a9b7eSAlexander Kabaev #include <sys/limits.h>
59df8bae1dSRodney W. Grimes #include <sys/malloc.h>
6042d11757SPeter Wemm #include <sys/poll.h>
6189b71647SPeter Wemm #include <sys/resourcevar.h>
620a2c3d48SGarrett Wollman #include <sys/selinfo.h>
638f19eb88SIan Dowse #include <sys/syscallsubr.h>
648cb96f20SPeter Wemm #include <sys/sysctl.h>
6542d11757SPeter Wemm #include <sys/sysent.h>
66279d7226SMatthew Dillon #include <sys/bio.h>
67279d7226SMatthew Dillon #include <sys/buf.h>
68265fc98fSSeigo Tanimura #include <sys/condvar.h>
69df8bae1dSRodney W. Grimes #ifdef KTRACE
70df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
71df8bae1dSRodney W. Grimes #endif
72279d7226SMatthew Dillon #include <vm/vm.h>
73279d7226SMatthew Dillon #include <vm/vm_page.h>
74df8bae1dSRodney W. Grimes 
75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
77a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7855166637SPoul-Henning Kamp 
79bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
80bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
81bbbb04ceSAlfred Perlstein static int	dofileread(struct thread *, struct file *, int, void *,
82bbbb04ceSAlfred Perlstein 		    size_t, off_t, int);
83bbbb04ceSAlfred Perlstein static int	dofilewrite(struct thread *, struct file *, int,
84bbbb04ceSAlfred Perlstein 		    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 
107b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) == 0) {
108b40ce416SJulian Elischer 		error = dofileread(td, fp, uap->fd, uap->buf,
109ad2edad9SMatthew Dillon 			    uap->nbyte, (off_t)-1, 0);
110b40ce416SJulian Elischer 		fdrop(fp, td);
111ad2edad9SMatthew Dillon 	}
112279d7226SMatthew Dillon 	return(error);
113df8bae1dSRodney W. Grimes }
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes /*
1168fe387abSDmitrij Tejblum  * Pread system call
1174160ccd9SAlan Cox  */
1184160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1194160ccd9SAlan Cox struct pread_args {
1204160ccd9SAlan Cox 	int	fd;
1214160ccd9SAlan Cox 	void	*buf;
1224160ccd9SAlan Cox 	size_t	nbyte;
1238fe387abSDmitrij Tejblum 	int	pad;
1244160ccd9SAlan Cox 	off_t	offset;
1254160ccd9SAlan Cox };
1264160ccd9SAlan Cox #endif
127ad2edad9SMatthew Dillon /*
128ad2edad9SMatthew Dillon  * MPSAFE
129ad2edad9SMatthew Dillon  */
1304160ccd9SAlan Cox int
131b40ce416SJulian Elischer pread(td, uap)
132b40ce416SJulian Elischer 	struct thread *td;
133b064d43dSMatthew Dillon 	struct pread_args *uap;
1344160ccd9SAlan Cox {
135b064d43dSMatthew Dillon 	struct file *fp;
136279d7226SMatthew Dillon 	int error;
1378fe387abSDmitrij Tejblum 
13897fa4397SAlfred Perlstein 	if ((error = fget_read(td, uap->fd, &fp)) != 0)
13997fa4397SAlfred Perlstein 		return (error);
140426da3bcSAlfred Perlstein 	if (fp->f_type != DTYPE_VNODE) {
141b064d43dSMatthew Dillon 		error = ESPIPE;
142426da3bcSAlfred Perlstein 	} else {
143426da3bcSAlfred Perlstein 		error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte,
144426da3bcSAlfred Perlstein 			    uap->offset, FOF_OFFSET);
145b064d43dSMatthew Dillon 	}
146b40ce416SJulian Elischer 	fdrop(fp, td);
147279d7226SMatthew Dillon 	return(error);
1488fe387abSDmitrij Tejblum }
1498fe387abSDmitrij Tejblum 
1508fe387abSDmitrij Tejblum /*
1518fe387abSDmitrij Tejblum  * Code common for read and pread
1528fe387abSDmitrij Tejblum  */
15337c84183SPoul-Henning Kamp static int
154b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags)
155b40ce416SJulian Elischer 	struct thread *td;
1568fe387abSDmitrij Tejblum 	struct file *fp;
1578fe387abSDmitrij Tejblum 	int fd, flags;
1588fe387abSDmitrij Tejblum 	void *buf;
1598fe387abSDmitrij Tejblum 	size_t nbyte;
1608fe387abSDmitrij Tejblum 	off_t offset;
1618fe387abSDmitrij Tejblum {
1624160ccd9SAlan Cox 	struct uio auio;
1634160ccd9SAlan Cox 	struct iovec aiov;
1644160ccd9SAlan Cox 	long cnt, error = 0;
1654160ccd9SAlan Cox #ifdef KTRACE
1664160ccd9SAlan Cox 	struct iovec ktriov;
16742ebfbf2SBrian Feldman 	struct uio ktruio;
1683c89e357SBrian Feldman 	int didktr = 0;
1694160ccd9SAlan Cox #endif
1704160ccd9SAlan Cox 
1710a3e28cfSAlfred Perlstein 	aiov.iov_base = buf;
1728fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
1734160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1744160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
1758fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
1768fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
1774160ccd9SAlan Cox 		return (EINVAL);
1788fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
1794160ccd9SAlan Cox 	auio.uio_rw = UIO_READ;
1804160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
181b40ce416SJulian Elischer 	auio.uio_td = td;
1824160ccd9SAlan Cox #ifdef KTRACE
1834160ccd9SAlan Cox 	/*
1844160ccd9SAlan Cox 	 * if tracing, save a copy of iovec
1854160ccd9SAlan Cox 	 */
18660a9bb19SJohn Baldwin 	if (KTRPOINT(td, KTR_GENIO)) {
1874160ccd9SAlan Cox 		ktriov = aiov;
18842ebfbf2SBrian Feldman 		ktruio = auio;
1893c89e357SBrian Feldman 		didktr = 1;
19042ebfbf2SBrian Feldman 	}
1914160ccd9SAlan Cox #endif
1928fe387abSDmitrij Tejblum 	cnt = nbyte;
193279d7226SMatthew Dillon 
1949ca43589SRobert Watson 	if ((error = fo_read(fp, &auio, td->td_ucred, flags, td))) {
1954160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
1964160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
1974160ccd9SAlan Cox 			error = 0;
198279d7226SMatthew Dillon 	}
1994160ccd9SAlan Cox 	cnt -= auio.uio_resid;
2004160ccd9SAlan Cox #ifdef KTRACE
2013c89e357SBrian Feldman 	if (didktr && error == 0) {
20242ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
20342ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
20460a9bb19SJohn Baldwin 		ktrgenio(fd, UIO_READ, &ktruio, error);
20542ebfbf2SBrian Feldman 	}
2064160ccd9SAlan Cox #endif
207b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
2084160ccd9SAlan Cox 	return (error);
2094160ccd9SAlan Cox }
2104160ccd9SAlan Cox 
2114160ccd9SAlan Cox /*
212df8bae1dSRodney W. Grimes  * Scatter read system call.
213df8bae1dSRodney W. Grimes  */
214d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
215df8bae1dSRodney W. Grimes struct readv_args {
2167147b19dSBruce Evans 	int	fd;
217df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
218df8bae1dSRodney W. Grimes 	u_int	iovcnt;
219df8bae1dSRodney W. Grimes };
220d2d3e875SBruce Evans #endif
221ad2edad9SMatthew Dillon /*
222ad2edad9SMatthew Dillon  * MPSAFE
223ad2edad9SMatthew Dillon  */
22426f9a767SRodney W. Grimes int
225b40ce416SJulian Elischer readv(td, uap)
226b40ce416SJulian Elischer 	struct thread *td;
227b064d43dSMatthew Dillon 	struct readv_args *uap;
228df8bae1dSRodney W. Grimes {
229b064d43dSMatthew Dillon 	struct file *fp;
230df8bae1dSRodney W. Grimes 	struct uio auio;
231b064d43dSMatthew Dillon 	struct iovec *iov;
232df8bae1dSRodney W. Grimes 	struct iovec *needfree;
233df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
23482641acdSAlan Cox 	long i, cnt;
23582641acdSAlan Cox 	int error;
236df8bae1dSRodney W. Grimes 	u_int iovlen;
237df8bae1dSRodney W. Grimes #ifdef KTRACE
238df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
23942ebfbf2SBrian Feldman 	struct uio ktruio;
240df8bae1dSRodney W. Grimes #endif
241df8bae1dSRodney W. Grimes 
242b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) != 0)
24382641acdSAlan Cox 		return (error);
24482641acdSAlan Cox 	needfree = NULL;
245df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
246df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
247df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
248ad2edad9SMatthew Dillon 		if (uap->iovcnt > UIO_MAXIOV) {
249ad2edad9SMatthew Dillon 			error = EINVAL;
25082641acdSAlan Cox 			goto done;
251ad2edad9SMatthew Dillon 		}
252a163d034SWarner Losh 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
253df8bae1dSRodney W. Grimes 		needfree = iov;
25482641acdSAlan Cox 	} else
255df8bae1dSRodney W. Grimes 		iov = aiov;
256df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
257df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
258df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
259df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
260b40ce416SJulian Elischer 	auio.uio_td = td;
2612c1011f7SJohn Dyson 	auio.uio_offset = -1;
2620a3e28cfSAlfred Perlstein 	if ((error = copyin(uap->iovp, iov, iovlen)))
263df8bae1dSRodney W. Grimes 		goto done;
264df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
265df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
266069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
267df8bae1dSRodney W. Grimes 			error = EINVAL;
268df8bae1dSRodney W. Grimes 			goto done;
269df8bae1dSRodney W. Grimes 		}
270069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
271df8bae1dSRodney W. Grimes 		iov++;
272df8bae1dSRodney W. Grimes 	}
273df8bae1dSRodney W. Grimes #ifdef KTRACE
274df8bae1dSRodney W. Grimes 	/*
275df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
276df8bae1dSRodney W. Grimes 	 */
27760a9bb19SJohn Baldwin 	if (KTRPOINT(td, KTR_GENIO))  {
278a163d034SWarner Losh 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
2790a3e28cfSAlfred Perlstein 		bcopy(auio.uio_iov, ktriov, iovlen);
28042ebfbf2SBrian Feldman 		ktruio = auio;
281df8bae1dSRodney W. Grimes 	}
282df8bae1dSRodney W. Grimes #endif
283df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
2849ca43589SRobert Watson 	if ((error = fo_read(fp, &auio, td->td_ucred, 0, td))) {
285df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
286df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
287df8bae1dSRodney W. Grimes 			error = 0;
288279d7226SMatthew Dillon 	}
289df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
290df8bae1dSRodney W. Grimes #ifdef KTRACE
291df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
29242ebfbf2SBrian Feldman 		if (error == 0) {
29342ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
29442ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
29560a9bb19SJohn Baldwin 			ktrgenio(uap->fd, UIO_READ, &ktruio, error);
29642ebfbf2SBrian Feldman 		}
297df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
298df8bae1dSRodney W. Grimes 	}
299df8bae1dSRodney W. Grimes #endif
300b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
301df8bae1dSRodney W. Grimes done:
302b40ce416SJulian Elischer 	fdrop(fp, td);
303df8bae1dSRodney W. Grimes 	if (needfree)
304df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
305df8bae1dSRodney W. Grimes 	return (error);
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes /*
309df8bae1dSRodney W. Grimes  * Write system call
310df8bae1dSRodney W. Grimes  */
311d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
312df8bae1dSRodney W. Grimes struct write_args {
313df8bae1dSRodney W. Grimes 	int	fd;
314134e06feSBruce Evans 	const void *buf;
315134e06feSBruce Evans 	size_t	nbyte;
316df8bae1dSRodney W. Grimes };
317d2d3e875SBruce Evans #endif
318ad2edad9SMatthew Dillon /*
319ad2edad9SMatthew Dillon  * MPSAFE
320ad2edad9SMatthew Dillon  */
32126f9a767SRodney W. Grimes int
322b40ce416SJulian Elischer write(td, uap)
323b40ce416SJulian Elischer 	struct thread *td;
324b064d43dSMatthew Dillon 	struct write_args *uap;
325df8bae1dSRodney W. Grimes {
326b064d43dSMatthew Dillon 	struct file *fp;
327279d7226SMatthew Dillon 	int error;
328df8bae1dSRodney W. Grimes 
329b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
330b40ce416SJulian Elischer 		error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte,
331ad2edad9SMatthew Dillon 			    (off_t)-1, 0);
332b40ce416SJulian Elischer 		fdrop(fp, td);
333ad2edad9SMatthew Dillon 	} else {
334b064d43dSMatthew Dillon 		error = EBADF;	/* XXX this can't be right */
335ad2edad9SMatthew Dillon 	}
336279d7226SMatthew Dillon 	return(error);
337df8bae1dSRodney W. Grimes }
338df8bae1dSRodney W. Grimes 
339df8bae1dSRodney W. Grimes /*
3408fe387abSDmitrij Tejblum  * Pwrite system call
3414160ccd9SAlan Cox  */
3424160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3434160ccd9SAlan Cox struct pwrite_args {
3444160ccd9SAlan Cox 	int	fd;
3454160ccd9SAlan Cox 	const void *buf;
3464160ccd9SAlan Cox 	size_t	nbyte;
3478fe387abSDmitrij Tejblum 	int	pad;
3484160ccd9SAlan Cox 	off_t	offset;
3494160ccd9SAlan Cox };
3504160ccd9SAlan Cox #endif
351ad2edad9SMatthew Dillon /*
352ad2edad9SMatthew Dillon  * MPSAFE
353ad2edad9SMatthew Dillon  */
3544160ccd9SAlan Cox int
355b40ce416SJulian Elischer pwrite(td, uap)
356b40ce416SJulian Elischer 	struct thread *td;
357b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3584160ccd9SAlan Cox {
359b064d43dSMatthew Dillon 	struct file *fp;
360279d7226SMatthew Dillon 	int error;
3618fe387abSDmitrij Tejblum 
362b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
363aa11a498SAlfred Perlstein 		if (fp->f_type == DTYPE_VNODE) {
364b064d43dSMatthew Dillon 			error = dofilewrite(td, fp, uap->fd, uap->buf,
365b064d43dSMatthew Dillon 				    uap->nbyte, uap->offset, FOF_OFFSET);
366b064d43dSMatthew Dillon 		} else {
367279d7226SMatthew Dillon 			error = ESPIPE;
368b064d43dSMatthew Dillon 		}
369b40ce416SJulian Elischer 		fdrop(fp, td);
370279d7226SMatthew Dillon 	} else {
371b064d43dSMatthew Dillon 		error = EBADF;	/* this can't be right */
372ad2edad9SMatthew Dillon 	}
373279d7226SMatthew Dillon 	return(error);
3748fe387abSDmitrij Tejblum }
3758fe387abSDmitrij Tejblum 
3768fe387abSDmitrij Tejblum static int
377b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags)
378b40ce416SJulian Elischer 	struct thread *td;
3798fe387abSDmitrij Tejblum 	struct file *fp;
3808fe387abSDmitrij Tejblum 	int fd, flags;
3818fe387abSDmitrij Tejblum 	const void *buf;
3828fe387abSDmitrij Tejblum 	size_t nbyte;
3838fe387abSDmitrij Tejblum 	off_t offset;
3848fe387abSDmitrij Tejblum {
3854160ccd9SAlan Cox 	struct uio auio;
3864160ccd9SAlan Cox 	struct iovec aiov;
3874160ccd9SAlan Cox 	long cnt, error = 0;
3884160ccd9SAlan Cox #ifdef KTRACE
3894160ccd9SAlan Cox 	struct iovec ktriov;
39042ebfbf2SBrian Feldman 	struct uio ktruio;
3913c89e357SBrian Feldman 	int didktr = 0;
3924160ccd9SAlan Cox #endif
3934160ccd9SAlan Cox 
394b31ae1adSPeter Wemm 	aiov.iov_base = (void *)(uintptr_t)buf;
3958fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
3964160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3974160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
3988fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
3998fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
4004160ccd9SAlan Cox 		return (EINVAL);
4018fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
4024160ccd9SAlan Cox 	auio.uio_rw = UIO_WRITE;
4034160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
404b40ce416SJulian Elischer 	auio.uio_td = td;
4054160ccd9SAlan Cox #ifdef KTRACE
4064160ccd9SAlan Cox 	/*
40742ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
4084160ccd9SAlan Cox 	 */
40960a9bb19SJohn Baldwin 	if (KTRPOINT(td, KTR_GENIO)) {
4104160ccd9SAlan Cox 		ktriov = aiov;
41142ebfbf2SBrian Feldman 		ktruio = auio;
4123c89e357SBrian Feldman 		didktr = 1;
41342ebfbf2SBrian Feldman 	}
4144160ccd9SAlan Cox #endif
4158fe387abSDmitrij Tejblum 	cnt = nbyte;
416c6ab5768SAlfred Perlstein 	if (fp->f_type == DTYPE_VNODE)
417279d7226SMatthew Dillon 		bwillwrite();
4189ca43589SRobert Watson 	if ((error = fo_write(fp, &auio, td->td_ucred, flags, td))) {
4194160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
4204160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
4214160ccd9SAlan Cox 			error = 0;
422c33c8251SAlfred Perlstein 		/* Socket layer is responsible for issuing SIGPIPE. */
423c33c8251SAlfred Perlstein 		if (error == EPIPE && fp->f_type != DTYPE_SOCKET) {
424b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
425b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
426b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
42719eb87d2SJohn Baldwin 		}
4284160ccd9SAlan Cox 	}
4294160ccd9SAlan Cox 	cnt -= auio.uio_resid;
4304160ccd9SAlan Cox #ifdef KTRACE
4313c89e357SBrian Feldman 	if (didktr && error == 0) {
43242ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
43342ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
43460a9bb19SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, &ktruio, error);
43542ebfbf2SBrian Feldman 	}
4364160ccd9SAlan Cox #endif
437b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
4384160ccd9SAlan Cox 	return (error);
4394160ccd9SAlan Cox }
4404160ccd9SAlan Cox 
4414160ccd9SAlan Cox /*
442df8bae1dSRodney W. Grimes  * Gather write system call
443df8bae1dSRodney W. Grimes  */
444d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
445df8bae1dSRodney W. Grimes struct writev_args {
446df8bae1dSRodney W. Grimes 	int	fd;
447df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
448df8bae1dSRodney W. Grimes 	u_int	iovcnt;
449df8bae1dSRodney W. Grimes };
450d2d3e875SBruce Evans #endif
451ad2edad9SMatthew Dillon /*
452ad2edad9SMatthew Dillon  * MPSAFE
453ad2edad9SMatthew Dillon  */
45426f9a767SRodney W. Grimes int
455b40ce416SJulian Elischer writev(td, uap)
456b40ce416SJulian Elischer 	struct thread *td;
457df8bae1dSRodney W. Grimes 	register struct writev_args *uap;
458df8bae1dSRodney W. Grimes {
459b064d43dSMatthew Dillon 	struct file *fp;
460df8bae1dSRodney W. Grimes 	struct uio auio;
461df8bae1dSRodney W. Grimes 	register struct iovec *iov;
462df8bae1dSRodney W. Grimes 	struct iovec *needfree;
463df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
464df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
465df8bae1dSRodney W. Grimes 	u_int iovlen;
466df8bae1dSRodney W. Grimes #ifdef KTRACE
467df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
46842ebfbf2SBrian Feldman 	struct uio ktruio;
469df8bae1dSRodney W. Grimes #endif
470df8bae1dSRodney W. Grimes 
471ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
472b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) != 0) {
473ad2edad9SMatthew Dillon 		error = EBADF;
474ad2edad9SMatthew Dillon 		goto done2;
475ad2edad9SMatthew Dillon 	}
476df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
477df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
478df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
4791aa3e7ddSBrian Feldman 		if (uap->iovcnt > UIO_MAXIOV) {
4801aa3e7ddSBrian Feldman 			needfree = NULL;
4811aa3e7ddSBrian Feldman 			error = EINVAL;
4821aa3e7ddSBrian Feldman 			goto done;
4831aa3e7ddSBrian Feldman 		}
484a163d034SWarner Losh 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
485df8bae1dSRodney W. Grimes 		needfree = iov;
486df8bae1dSRodney W. Grimes 	} else {
487df8bae1dSRodney W. Grimes 		iov = aiov;
488df8bae1dSRodney W. Grimes 		needfree = NULL;
489df8bae1dSRodney W. Grimes 	}
490df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
491df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
492df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
493df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
494b40ce416SJulian Elischer 	auio.uio_td = td;
4952c1011f7SJohn Dyson 	auio.uio_offset = -1;
4960a3e28cfSAlfred Perlstein 	if ((error = copyin(uap->iovp, iov, iovlen)))
497df8bae1dSRodney W. Grimes 		goto done;
498df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
499df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
500069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
501df8bae1dSRodney W. Grimes 			error = EINVAL;
502df8bae1dSRodney W. Grimes 			goto done;
503df8bae1dSRodney W. Grimes 		}
504069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
505df8bae1dSRodney W. Grimes 		iov++;
506df8bae1dSRodney W. Grimes 	}
507df8bae1dSRodney W. Grimes #ifdef KTRACE
508df8bae1dSRodney W. Grimes 	/*
50942ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
510df8bae1dSRodney W. Grimes 	 */
51160a9bb19SJohn Baldwin 	if (KTRPOINT(td, KTR_GENIO))  {
512a163d034SWarner Losh 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
5130a3e28cfSAlfred Perlstein 		bcopy(auio.uio_iov, ktriov, iovlen);
51442ebfbf2SBrian Feldman 		ktruio = auio;
515df8bae1dSRodney W. Grimes 	}
516df8bae1dSRodney W. Grimes #endif
517df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
518a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5199440653dSMatthew Dillon 		bwillwrite();
5209ca43589SRobert Watson 	if ((error = fo_write(fp, &auio, td->td_ucred, 0, td))) {
521df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
522df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
523df8bae1dSRodney W. Grimes 			error = 0;
52419eb87d2SJohn Baldwin 		if (error == EPIPE) {
525b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
526b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
527b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
52819eb87d2SJohn Baldwin 		}
529df8bae1dSRodney W. Grimes 	}
530df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
531df8bae1dSRodney W. Grimes #ifdef KTRACE
532df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
53342ebfbf2SBrian Feldman 		if (error == 0) {
53442ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
53542ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
53660a9bb19SJohn Baldwin 			ktrgenio(uap->fd, UIO_WRITE, &ktruio, error);
53742ebfbf2SBrian Feldman 		}
538df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
539df8bae1dSRodney W. Grimes 	}
540df8bae1dSRodney W. Grimes #endif
541b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
542df8bae1dSRodney W. Grimes done:
543b40ce416SJulian Elischer 	fdrop(fp, td);
544df8bae1dSRodney W. Grimes 	if (needfree)
545df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
546ad2edad9SMatthew Dillon done2:
547ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
548df8bae1dSRodney W. Grimes 	return (error);
549df8bae1dSRodney W. Grimes }
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes /*
552df8bae1dSRodney W. Grimes  * Ioctl system call
553df8bae1dSRodney W. Grimes  */
554d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
555df8bae1dSRodney W. Grimes struct ioctl_args {
556df8bae1dSRodney W. Grimes 	int	fd;
557069e9bc1SDoug Rabson 	u_long	com;
558df8bae1dSRodney W. Grimes 	caddr_t	data;
559df8bae1dSRodney W. Grimes };
560d2d3e875SBruce Evans #endif
561ad2edad9SMatthew Dillon /*
562ad2edad9SMatthew Dillon  * MPSAFE
563ad2edad9SMatthew Dillon  */
564df8bae1dSRodney W. Grimes /* ARGSUSED */
56526f9a767SRodney W. Grimes int
566b40ce416SJulian Elischer ioctl(td, uap)
567b40ce416SJulian Elischer 	struct thread *td;
568df8bae1dSRodney W. Grimes 	register struct ioctl_args *uap;
569df8bae1dSRodney W. Grimes {
570a4db4953SAlfred Perlstein 	struct file *fp;
571df8bae1dSRodney W. Grimes 	register struct filedesc *fdp;
572831b9ef2SDoug Rabson 	register u_long com;
573ad2edad9SMatthew Dillon 	int error = 0;
574df8bae1dSRodney W. Grimes 	register u_int size;
575df8bae1dSRodney W. Grimes 	caddr_t data, memp;
576df8bae1dSRodney W. Grimes 	int tmp;
577df8bae1dSRodney W. Grimes #define STK_PARAMS	128
578d2ba455cSMatthew Dillon 	union {
579df8bae1dSRodney W. Grimes 	    char stkbuf[STK_PARAMS];
580d2ba455cSMatthew Dillon 	    long align;
581d2ba455cSMatthew Dillon 	} ubuf;
582df8bae1dSRodney W. Grimes 
583a4db4953SAlfred Perlstein 	if ((error = fget(td, uap->fd, &fp)) != 0)
584a4db4953SAlfred Perlstein 		return (error);
585aa11a498SAlfred Perlstein 	mtx_lock(&Giant);
586ad2edad9SMatthew Dillon 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
587426da3bcSAlfred Perlstein 		fdrop(fp, td);
588aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
589426da3bcSAlfred Perlstein 		return (EBADF);
590ad2edad9SMatthew Dillon 	}
591426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
592df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
593df8bae1dSRodney W. Grimes 	case FIONCLEX:
594426da3bcSAlfred Perlstein 		FILEDESC_LOCK(fdp);
595df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
596426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(fdp);
597426da3bcSAlfred Perlstein 		fdrop(fp, td);
598aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
599426da3bcSAlfred Perlstein 		return (0);
600df8bae1dSRodney W. Grimes 	case FIOCLEX:
601426da3bcSAlfred Perlstein 		FILEDESC_LOCK(fdp);
602df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
603426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(fdp);
604426da3bcSAlfred Perlstein 		fdrop(fp, td);
605aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
606426da3bcSAlfred Perlstein 		return (0);
607df8bae1dSRodney W. Grimes 	}
608df8bae1dSRodney W. Grimes 
609df8bae1dSRodney W. Grimes 	/*
610df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
611df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
612df8bae1dSRodney W. Grimes 	 */
613df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
614ad2edad9SMatthew Dillon 	if (size > IOCPARM_MAX) {
615426da3bcSAlfred Perlstein 		fdrop(fp, td);
616aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
617426da3bcSAlfred Perlstein 		return (ENOTTY);
618ad2edad9SMatthew Dillon 	}
619279d7226SMatthew Dillon 
620df8bae1dSRodney W. Grimes 	memp = NULL;
621d2ba455cSMatthew Dillon 	if (size > sizeof (ubuf.stkbuf)) {
622a163d034SWarner Losh 		memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
623df8bae1dSRodney W. Grimes 		data = memp;
624279d7226SMatthew Dillon 	} else {
625d2ba455cSMatthew Dillon 		data = ubuf.stkbuf;
626279d7226SMatthew Dillon 	}
627df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
628df8bae1dSRodney W. Grimes 		if (size) {
629df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
630df8bae1dSRodney W. Grimes 			if (error) {
631df8bae1dSRodney W. Grimes 				if (memp)
632df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
633b40ce416SJulian Elischer 				fdrop(fp, td);
634426da3bcSAlfred Perlstein 				goto done;
635df8bae1dSRodney W. Grimes 			}
636279d7226SMatthew Dillon 		} else {
637df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
638279d7226SMatthew Dillon 		}
639279d7226SMatthew Dillon 	} else if ((com&IOC_OUT) && size) {
640df8bae1dSRodney W. Grimes 		/*
641df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
642df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
643df8bae1dSRodney W. Grimes 		 */
644df8bae1dSRodney W. Grimes 		bzero(data, size);
645279d7226SMatthew Dillon 	} else if (com&IOC_VOID) {
646df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
647279d7226SMatthew Dillon 	}
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes 	switch (com) {
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes 	case FIONBIO:
652426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
653bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
654df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
655df8bae1dSRodney W. Grimes 		else
656df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
657426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
658d49fa1caSRobert Watson 		error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
659df8bae1dSRodney W. Grimes 		break;
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes 	case FIOASYNC:
662426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
663bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
664df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
665df8bae1dSRodney W. Grimes 		else
666df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
667426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
668d49fa1caSRobert Watson 		error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
669df8bae1dSRodney W. Grimes 		break;
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	default:
672d49fa1caSRobert Watson 		error = fo_ioctl(fp, com, data, td->td_ucred, td);
673df8bae1dSRodney W. Grimes 		/*
674df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
675df8bae1dSRodney W. Grimes 		 * already set and checked above.
676df8bae1dSRodney W. Grimes 		 */
677df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
678df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
679df8bae1dSRodney W. Grimes 		break;
680df8bae1dSRodney W. Grimes 	}
681df8bae1dSRodney W. Grimes 	if (memp)
682df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
683b40ce416SJulian Elischer 	fdrop(fp, td);
684426da3bcSAlfred Perlstein done:
685ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
686df8bae1dSRodney W. Grimes 	return (error);
687df8bae1dSRodney W. Grimes }
688df8bae1dSRodney W. Grimes 
68985f190e4SAlfred Perlstein /*
69085f190e4SAlfred Perlstein  * sellock and selwait are initialized in selectinit() via SYSINIT.
69185f190e4SAlfred Perlstein  */
69285f190e4SAlfred Perlstein struct mtx	sellock;
693265fc98fSSeigo Tanimura struct cv	selwait;
6949ae6d334SKelly Yancey u_int		nselcoll;	/* Select collisions since boot */
6959ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes /*
698df8bae1dSRodney W. Grimes  * Select system call.
699df8bae1dSRodney W. Grimes  */
700d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
701df8bae1dSRodney W. Grimes struct select_args {
702b08f7993SSujal Patel 	int	nd;
703df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
704df8bae1dSRodney W. Grimes 	struct	timeval *tv;
705df8bae1dSRodney W. Grimes };
706d2d3e875SBruce Evans #endif
707ad2edad9SMatthew Dillon /*
708ad2edad9SMatthew Dillon  * MPSAFE
709ad2edad9SMatthew Dillon  */
71026f9a767SRodney W. Grimes int
711b40ce416SJulian Elischer select(td, uap)
712b40ce416SJulian Elischer 	register struct thread *td;
713df8bae1dSRodney W. Grimes 	register struct select_args *uap;
714df8bae1dSRodney W. Grimes {
7158f19eb88SIan Dowse 	struct timeval tv, *tvp;
7168f19eb88SIan Dowse 	int error;
7178f19eb88SIan Dowse 
7188f19eb88SIan Dowse 	if (uap->tv != NULL) {
7198f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
7208f19eb88SIan Dowse 		if (error)
7218f19eb88SIan Dowse 			return (error);
7228f19eb88SIan Dowse 		tvp = &tv;
7238f19eb88SIan Dowse 	} else
7248f19eb88SIan Dowse 		tvp = NULL;
7258f19eb88SIan Dowse 
7268f19eb88SIan Dowse 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
7278f19eb88SIan Dowse }
7288f19eb88SIan Dowse 
7298f19eb88SIan Dowse int
7308f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
7318f19eb88SIan Dowse     fd_set *fd_ex, struct timeval *tvp)
7328f19eb88SIan Dowse {
733426da3bcSAlfred Perlstein 	struct filedesc *fdp;
734d5e4d7e1SBruce Evans 	/*
735d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
736d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
737d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
738d5e4d7e1SBruce Evans 	 * of 256.
739d5e4d7e1SBruce Evans 	 */
740d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
741eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
74200af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
7439ae6d334SKelly Yancey 	int error, timo;
7449ae6d334SKelly Yancey 	u_int ncoll, nbufbytes, ncpbytes, nfdbits;
745df8bae1dSRodney W. Grimes 
7468f19eb88SIan Dowse 	if (nd < 0)
747acbfbfeaSSujal Patel 		return (EINVAL);
748426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
749ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
750426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
751ad2edad9SMatthew Dillon 
7528f19eb88SIan Dowse 	if (nd > td->td_proc->p_fd->fd_nfiles)
7538f19eb88SIan Dowse 		nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
754426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
755b08f7993SSujal Patel 
756d5e4d7e1SBruce Evans 	/*
757d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
758d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
759d5e4d7e1SBruce Evans 	 */
7608f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
761d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
762d5e4d7e1SBruce Evans 	nbufbytes = 0;
7638f19eb88SIan Dowse 	if (fd_in != NULL)
764d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7658f19eb88SIan Dowse 	if (fd_ou != NULL)
766d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7678f19eb88SIan Dowse 	if (fd_ex != NULL)
768d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
769d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
770d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
771d5e4d7e1SBruce Evans 	else
772a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
773b08f7993SSujal Patel 
774b08f7993SSujal Patel 	/*
775d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
776d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
777d5e4d7e1SBruce Evans 	 * together.
778b08f7993SSujal Patel 	 */
779d5e4d7e1SBruce Evans 	sbp = selbits;
780df8bae1dSRodney W. Grimes #define	getbits(name, x) \
781d5e4d7e1SBruce Evans 	do {								\
7828f19eb88SIan Dowse 		if (name == NULL)					\
783d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
784d5e4d7e1SBruce Evans 		else {							\
785d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
786d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
787d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
7888f19eb88SIan Dowse 			error = copyin(name, ibits[x], ncpbytes);	\
789265fc98fSSeigo Tanimura 			if (error != 0)					\
79085f190e4SAlfred Perlstein 				goto done_nosellock;			\
791e04ac2feSJohn Baldwin 		}							\
792d5e4d7e1SBruce Evans 	} while (0)
7938f19eb88SIan Dowse 	getbits(fd_in, 0);
7948f19eb88SIan Dowse 	getbits(fd_ou, 1);
7958f19eb88SIan Dowse 	getbits(fd_ex, 2);
796df8bae1dSRodney W. Grimes #undef	getbits
797d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
798d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
799df8bae1dSRodney W. Grimes 
8008f19eb88SIan Dowse 	if (tvp != NULL) {
8018f19eb88SIan Dowse 		atv = *tvp;
802df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
803df8bae1dSRodney W. Grimes 			error = EINVAL;
80485f190e4SAlfred Perlstein 			goto done_nosellock;
805df8bae1dSRodney W. Grimes 		}
806c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
80700af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
8089c386f6bSJohn Baldwin 	} else {
80900af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
8109c386f6bSJohn Baldwin 		atv.tv_usec = 0;
8119c386f6bSJohn Baldwin 	}
81200af9731SPoul-Henning Kamp 	timo = 0;
8132149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
81485f190e4SAlfred Perlstein 	mtx_lock(&sellock);
815df8bae1dSRodney W. Grimes retry:
816df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
817fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
818b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
819fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
82085f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
82185f190e4SAlfred Perlstein 
8228f19eb88SIan Dowse 	error = selscan(td, ibits, obits, nd);
82385f190e4SAlfred Perlstein 	mtx_lock(&sellock);
824b40ce416SJulian Elischer 	if (error || td->td_retval[0])
825df8bae1dSRodney W. Grimes 		goto done;
8264da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
827c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
82885f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
829df8bae1dSRodney W. Grimes 			goto done;
83000af9731SPoul-Henning Kamp 		ttv = atv;
83100af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
83200af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
83300af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
834df8bae1dSRodney W. Grimes 	}
83585f190e4SAlfred Perlstein 
83685f190e4SAlfred Perlstein 	/*
83785f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
83885f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of
83985f190e4SAlfred Perlstein 	 * collisions and rescan the file descriptors if
84085f190e4SAlfred Perlstein 	 * necessary.
84185f190e4SAlfred Perlstein 	 */
842fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
84385f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
84485f190e4SAlfred Perlstein 		mtx_unlock_spin(&sched_lock);
84585f190e4SAlfred Perlstein 		goto retry;
84685f190e4SAlfred Perlstein 	}
847fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
848bfbbc4aaSJason Evans 
849265fc98fSSeigo Tanimura 	if (timo > 0)
85085f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
851265fc98fSSeigo Tanimura 	else
85285f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
853bfbbc4aaSJason Evans 
854df8bae1dSRodney W. Grimes 	if (error == 0)
855df8bae1dSRodney W. Grimes 		goto retry;
856265fc98fSSeigo Tanimura 
857df8bae1dSRodney W. Grimes done:
85885f190e4SAlfred Perlstein 	clear_selinfo_list(td);
859fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
860b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
861fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
86285f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
86385f190e4SAlfred Perlstein 
86485f190e4SAlfred Perlstein done_nosellock:
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) \
8718f19eb88SIan Dowse 	if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
872df8bae1dSRodney W. Grimes 		error = error2;
873df8bae1dSRodney W. Grimes 	if (error == 0) {
874df8bae1dSRodney W. Grimes 		int error2;
875df8bae1dSRodney W. Grimes 
8768f19eb88SIan Dowse 		putbits(fd_in, 0);
8778f19eb88SIan Dowse 		putbits(fd_ou, 1);
8788f19eb88SIan Dowse 		putbits(fd_ex, 2);
879df8bae1dSRodney W. Grimes #undef putbits
880df8bae1dSRodney W. Grimes 	}
881d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
882d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
883ad2edad9SMatthew Dillon 
884ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
885df8bae1dSRodney W. Grimes 	return (error);
886df8bae1dSRodney W. Grimes }
887df8bae1dSRodney W. Grimes 
888265fc98fSSeigo Tanimura static int
889b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
890b40ce416SJulian Elischer 	struct thread *td;
891b08f7993SSujal Patel 	fd_mask **ibits, **obits;
892cb226aaaSPoul-Henning Kamp 	int nfd;
893df8bae1dSRodney W. Grimes {
894f082218cSPeter Wemm 	int msk, i, fd;
895f082218cSPeter Wemm 	fd_mask bits;
896df8bae1dSRodney W. Grimes 	struct file *fp;
897df8bae1dSRodney W. Grimes 	int n = 0;
8982087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
89942d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
900eb209311SAlfred Perlstein 	struct filedesc *fdp = td->td_proc->p_fd;
901df8bae1dSRodney W. Grimes 
902eb209311SAlfred Perlstein 	FILEDESC_LOCK(fdp);
903df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
904d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
905d5e4d7e1SBruce Evans 			continue;
906df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
907b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
908f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
909f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
910f082218cSPeter Wemm 				if (!(bits & 1))
911f082218cSPeter Wemm 					continue;
912eb209311SAlfred Perlstein 				if ((fp = fget_locked(fdp, fd)) == NULL) {
913eb209311SAlfred Perlstein 					FILEDESC_UNLOCK(fdp);
914df8bae1dSRodney W. Grimes 					return (EBADF);
915eb209311SAlfred Perlstein 				}
916ea6027a8SRobert Watson 				if (fo_poll(fp, flag[msk], td->td_ucred,
917ea6027a8SRobert Watson 				    td)) {
918b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
919f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
920df8bae1dSRodney W. Grimes 					n++;
921df8bae1dSRodney W. Grimes 				}
922df8bae1dSRodney W. Grimes 			}
923df8bae1dSRodney W. Grimes 		}
924df8bae1dSRodney W. Grimes 	}
925eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
926b40ce416SJulian Elischer 	td->td_retval[0] = n;
927df8bae1dSRodney W. Grimes 	return (0);
928df8bae1dSRodney W. Grimes }
929df8bae1dSRodney W. Grimes 
93042d11757SPeter Wemm /*
93142d11757SPeter Wemm  * Poll system call.
93242d11757SPeter Wemm  */
93342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
93442d11757SPeter Wemm struct poll_args {
93542d11757SPeter Wemm 	struct pollfd *fds;
93642d11757SPeter Wemm 	u_int	nfds;
93742d11757SPeter Wemm 	int	timeout;
93842d11757SPeter Wemm };
93942d11757SPeter Wemm #endif
940ad2edad9SMatthew Dillon /*
941ad2edad9SMatthew Dillon  * MPSAFE
942ad2edad9SMatthew Dillon  */
94342d11757SPeter Wemm int
944b40ce416SJulian Elischer poll(td, uap)
945b40ce416SJulian Elischer 	struct thread *td;
946ea0237edSJonathan Lemon 	struct poll_args *uap;
94742d11757SPeter Wemm {
94842d11757SPeter Wemm 	caddr_t bits;
94942d11757SPeter Wemm 	char smallbits[32 * sizeof(struct pollfd)];
95000af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
9519ae6d334SKelly Yancey 	int error = 0, timo;
9529ae6d334SKelly Yancey 	u_int ncoll, nfds;
95342d11757SPeter Wemm 	size_t ni;
95442d11757SPeter Wemm 
955d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
956ad2edad9SMatthew Dillon 
957ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
95889b71647SPeter Wemm 	/*
9592bd5ac33SPeter Wemm 	 * This is kinda bogus.  We have fd limits, but that is not
9602bd5ac33SPeter Wemm 	 * really related to the size of the pollfd array.  Make sure
9612bd5ac33SPeter Wemm 	 * we let the process use at least FD_SETSIZE entries and at
9622bd5ac33SPeter Wemm 	 * least enough for the current limits.  We want to be reasonably
9632bd5ac33SPeter Wemm 	 * safe, but not overly restrictive.
96489b71647SPeter Wemm 	 */
965b40ce416SJulian Elischer 	if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) &&
966b40ce416SJulian Elischer 	    (nfds > FD_SETSIZE)) {
967ad2edad9SMatthew Dillon 		error = EINVAL;
968ad2edad9SMatthew Dillon 		goto done2;
969ad2edad9SMatthew Dillon 	}
97089b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
97142d11757SPeter Wemm 	if (ni > sizeof(smallbits))
972a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
97342d11757SPeter Wemm 	else
97442d11757SPeter Wemm 		bits = smallbits;
975d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
97642d11757SPeter Wemm 	if (error)
97785f190e4SAlfred Perlstein 		goto done_nosellock;
978d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
979d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
980d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
98142d11757SPeter Wemm 		if (itimerfix(&atv)) {
98242d11757SPeter Wemm 			error = EINVAL;
98385f190e4SAlfred Perlstein 			goto done_nosellock;
98442d11757SPeter Wemm 		}
985c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
98600af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
9879c386f6bSJohn Baldwin 	} else {
98800af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
9899c386f6bSJohn Baldwin 		atv.tv_usec = 0;
9909c386f6bSJohn Baldwin 	}
99100af9731SPoul-Henning Kamp 	timo = 0;
9922149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
99385f190e4SAlfred Perlstein 	mtx_lock(&sellock);
99442d11757SPeter Wemm retry:
99542d11757SPeter Wemm 	ncoll = nselcoll;
996fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
997b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
998fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
99985f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
100085f190e4SAlfred Perlstein 
1001b40ce416SJulian Elischer 	error = pollscan(td, (struct pollfd *)bits, nfds);
100285f190e4SAlfred Perlstein 	mtx_lock(&sellock);
1003b40ce416SJulian Elischer 	if (error || td->td_retval[0])
100442d11757SPeter Wemm 		goto done;
10054da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
1006c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
100785f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
100842d11757SPeter Wemm 			goto done;
100900af9731SPoul-Henning Kamp 		ttv = atv;
101000af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
101100af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
101200af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
101342d11757SPeter Wemm 	}
101485f190e4SAlfred Perlstein 	/*
101585f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
101685f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of collisions
101785f190e4SAlfred Perlstein 	 * and rescan the file descriptors if necessary.
101885f190e4SAlfred Perlstein 	 */
1019fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
102085f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
1021fea2ab83SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
102285f190e4SAlfred Perlstein 		goto retry;
102385f190e4SAlfred Perlstein 	}
102485f190e4SAlfred Perlstein 	mtx_unlock_spin(&sched_lock);
102585f190e4SAlfred Perlstein 
1026265fc98fSSeigo Tanimura 	if (timo > 0)
102785f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
1028265fc98fSSeigo Tanimura 	else
102985f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
103085f190e4SAlfred Perlstein 
103142d11757SPeter Wemm 	if (error == 0)
103242d11757SPeter Wemm 		goto retry;
1033265fc98fSSeigo Tanimura 
103442d11757SPeter Wemm done:
103585f190e4SAlfred Perlstein 	clear_selinfo_list(td);
1036fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
1037b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
1038fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
103985f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
104085f190e4SAlfred Perlstein 
104185f190e4SAlfred Perlstein done_nosellock:
104242d11757SPeter Wemm 	/* poll is not restarted after signals... */
104342d11757SPeter Wemm 	if (error == ERESTART)
104442d11757SPeter Wemm 		error = EINTR;
104542d11757SPeter Wemm 	if (error == EWOULDBLOCK)
104642d11757SPeter Wemm 		error = 0;
104742d11757SPeter Wemm 	if (error == 0) {
1048d1e405c5SAlfred Perlstein 		error = copyout(bits, uap->fds, ni);
104942d11757SPeter Wemm 		if (error)
105042d11757SPeter Wemm 			goto out;
105142d11757SPeter Wemm 	}
105242d11757SPeter Wemm out:
105342d11757SPeter Wemm 	if (ni > sizeof(smallbits))
105442d11757SPeter Wemm 		free(bits, M_TEMP);
1055ad2edad9SMatthew Dillon done2:
1056ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
105742d11757SPeter Wemm 	return (error);
105842d11757SPeter Wemm }
105942d11757SPeter Wemm 
106042d11757SPeter Wemm static int
1061b40ce416SJulian Elischer pollscan(td, fds, nfd)
1062b40ce416SJulian Elischer 	struct thread *td;
106342d11757SPeter Wemm 	struct pollfd *fds;
1064ea0237edSJonathan Lemon 	u_int nfd;
106542d11757SPeter Wemm {
1066b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
106742d11757SPeter Wemm 	int i;
106842d11757SPeter Wemm 	struct file *fp;
106942d11757SPeter Wemm 	int n = 0;
107042d11757SPeter Wemm 
1071426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
1072eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1073337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
107442d11757SPeter Wemm 			fds->revents = POLLNVAL;
107542d11757SPeter Wemm 			n++;
1076337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1077337c9691SJordan K. Hubbard 			fds->revents = 0;
107842d11757SPeter Wemm 		} else {
107942d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1080279d7226SMatthew Dillon 			if (fp == NULL) {
108142d11757SPeter Wemm 				fds->revents = POLLNVAL;
108242d11757SPeter Wemm 				n++;
108342d11757SPeter Wemm 			} else {
10842087c896SBruce Evans 				/*
10852087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
10862087c896SBruce Evans 				 * POLLERR if appropriate.
10872087c896SBruce Evans 				 */
108813ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1089ea6027a8SRobert Watson 				    td->td_ucred, td);
109042d11757SPeter Wemm 				if (fds->revents != 0)
109142d11757SPeter Wemm 					n++;
109242d11757SPeter Wemm 			}
109342d11757SPeter Wemm 		}
109442d11757SPeter Wemm 	}
1095eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
1096b40ce416SJulian Elischer 	td->td_retval[0] = n;
109742d11757SPeter Wemm 	return (0);
109842d11757SPeter Wemm }
109942d11757SPeter Wemm 
110042d11757SPeter Wemm /*
110142d11757SPeter Wemm  * OpenBSD poll system call.
110242d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
110342d11757SPeter Wemm  */
110442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
110542d11757SPeter Wemm struct openbsd_poll_args {
110642d11757SPeter Wemm 	struct pollfd *fds;
110742d11757SPeter Wemm 	u_int	nfds;
110842d11757SPeter Wemm 	int	timeout;
110942d11757SPeter Wemm };
111042d11757SPeter Wemm #endif
1111ad2edad9SMatthew Dillon /*
1112ad2edad9SMatthew Dillon  * MPSAFE
1113ad2edad9SMatthew Dillon  */
111442d11757SPeter Wemm int
1115b40ce416SJulian Elischer openbsd_poll(td, uap)
1116b40ce416SJulian Elischer 	register struct thread *td;
111742d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
111842d11757SPeter Wemm {
1119b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
112042d11757SPeter Wemm }
112142d11757SPeter Wemm 
112285f190e4SAlfred Perlstein /*
112385f190e4SAlfred Perlstein  * Remove the references to the thread from all of the objects
112485f190e4SAlfred Perlstein  * we were polling.
112585f190e4SAlfred Perlstein  *
112685f190e4SAlfred Perlstein  * This code assumes that the underlying owner of the selinfo
112785f190e4SAlfred Perlstein  * structure will hold sellock before it changes it, and that
112885f190e4SAlfred Perlstein  * it will unlink itself from our list if it goes away.
112985f190e4SAlfred Perlstein  */
113085f190e4SAlfred Perlstein void
113185f190e4SAlfred Perlstein clear_selinfo_list(td)
113285f190e4SAlfred Perlstein 	struct thread *td;
113385f190e4SAlfred Perlstein {
113485f190e4SAlfred Perlstein 	struct selinfo *si;
113585f190e4SAlfred Perlstein 
113685f190e4SAlfred Perlstein 	mtx_assert(&sellock, MA_OWNED);
113785f190e4SAlfred Perlstein 	TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
113885f190e4SAlfred Perlstein 		si->si_thread = NULL;
113985f190e4SAlfred Perlstein 	TAILQ_INIT(&td->td_selq);
114085f190e4SAlfred Perlstein }
114185f190e4SAlfred Perlstein 
1142df8bae1dSRodney W. Grimes /*ARGSUSED*/
114326f9a767SRodney W. Grimes int
1144b40ce416SJulian Elischer seltrue(dev, events, td)
1145df8bae1dSRodney W. Grimes 	dev_t dev;
114642d11757SPeter Wemm 	int events;
1147b40ce416SJulian Elischer 	struct thread *td;
1148df8bae1dSRodney W. Grimes {
1149df8bae1dSRodney W. Grimes 
115042d11757SPeter Wemm 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1151df8bae1dSRodney W. Grimes }
1152df8bae1dSRodney W. Grimes 
1153df8bae1dSRodney W. Grimes /*
1154df8bae1dSRodney W. Grimes  * Record a select request.
1155df8bae1dSRodney W. Grimes  */
1156df8bae1dSRodney W. Grimes void
1157df8bae1dSRodney W. Grimes selrecord(selector, sip)
1158b40ce416SJulian Elischer 	struct thread *selector;
1159df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1160df8bae1dSRodney W. Grimes {
1161df8bae1dSRodney W. Grimes 
116285f190e4SAlfred Perlstein 	mtx_lock(&sellock);
116385f190e4SAlfred Perlstein 	/*
1164b605b54cSAlfred Perlstein 	 * If the selinfo's thread pointer is NULL then take ownership of it.
1165b605b54cSAlfred Perlstein 	 *
1166b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and it points to another
1167b605b54cSAlfred Perlstein 	 * thread, then we have a collision.
1168b605b54cSAlfred Perlstein 	 *
1169b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and points back to us then leave
1170b605b54cSAlfred Perlstein 	 * it alone as we've already added pointed it at us and added it to
1171b605b54cSAlfred Perlstein 	 * our list.
117285f190e4SAlfred Perlstein 	 */
117385f190e4SAlfred Perlstein 	if (sip->si_thread == NULL) {
1174b40ce416SJulian Elischer 		sip->si_thread = selector;
117585f190e4SAlfred Perlstein 		TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
117685f190e4SAlfred Perlstein 	} else if (sip->si_thread != selector) {
117785f190e4SAlfred Perlstein 		sip->si_flags |= SI_COLL;
117885f190e4SAlfred Perlstein 	}
117985f190e4SAlfred Perlstein 
118085f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1181df8bae1dSRodney W. Grimes }
1182df8bae1dSRodney W. Grimes 
1183df8bae1dSRodney W. Grimes /*
1184df8bae1dSRodney W. Grimes  * Do a wakeup when a selectable event occurs.
1185df8bae1dSRodney W. Grimes  */
1186df8bae1dSRodney W. Grimes void
1187df8bae1dSRodney W. Grimes selwakeup(sip)
118885f190e4SAlfred Perlstein 	struct selinfo *sip;
1189df8bae1dSRodney W. Grimes {
1190b40ce416SJulian Elischer 	struct thread *td;
1191df8bae1dSRodney W. Grimes 
119285f190e4SAlfred Perlstein 	mtx_lock(&sellock);
119385f190e4SAlfred Perlstein 	td = sip->si_thread;
119485f190e4SAlfred Perlstein 	if ((sip->si_flags & SI_COLL) != 0) {
1195df8bae1dSRodney W. Grimes 		nselcoll++;
1196df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1197265fc98fSSeigo Tanimura 		cv_broadcast(&selwait);
1198df8bae1dSRodney W. Grimes 	}
119985f190e4SAlfred Perlstein 	if (td == NULL) {
120085f190e4SAlfred Perlstein 		mtx_unlock(&sellock);
1201b40ce416SJulian Elischer 		return;
1202b40ce416SJulian Elischer 	}
120385f190e4SAlfred Perlstein 	TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
120485f190e4SAlfred Perlstein 	sip->si_thread = NULL;
12059ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
12060a3e28cfSAlfred Perlstein 	if (td->td_wchan == &selwait) {
1207b40ce416SJulian Elischer 		cv_waitq_remove(td);
120871fad9fdSJulian Elischer 		TD_CLR_SLEEPING(td);
120971fad9fdSJulian Elischer 		setrunnable(td);
121033a9ed9dSJohn Baldwin 	} else
1211b40ce416SJulian Elischer 		td->td_flags &= ~TDF_SELECT;
121233a9ed9dSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
121385f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1214df8bae1dSRodney W. Grimes }
1215265fc98fSSeigo Tanimura 
12164d77a549SAlfred Perlstein static void selectinit(void *);
1217265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1218265fc98fSSeigo Tanimura 
1219265fc98fSSeigo Tanimura /* ARGSUSED*/
1220265fc98fSSeigo Tanimura static void
1221265fc98fSSeigo Tanimura selectinit(dummy)
1222265fc98fSSeigo Tanimura 	void *dummy;
1223265fc98fSSeigo Tanimura {
1224265fc98fSSeigo Tanimura 	cv_init(&selwait, "select");
12256008862bSJohn Baldwin 	mtx_init(&sellock, "sellck", NULL, MTX_DEF);
1226265fc98fSSeigo Tanimura }
1227