xref: /freebsd/sys/kern/sys_generic.c (revision 9bbee25931b0f5b9f37b9bcc21c582e493c41252)
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>
669bbee259SAndrey A. Chernov #include <sys/vnode.h>
67279d7226SMatthew Dillon #include <sys/bio.h>
68279d7226SMatthew Dillon #include <sys/buf.h>
69265fc98fSSeigo Tanimura #include <sys/condvar.h>
70df8bae1dSRodney W. Grimes #ifdef KTRACE
71df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
72df8bae1dSRodney W. Grimes #endif
73279d7226SMatthew Dillon #include <vm/vm.h>
74279d7226SMatthew Dillon #include <vm/vm_page.h>
75df8bae1dSRodney W. Grimes 
76a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
77a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
78a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7955166637SPoul-Henning Kamp 
80bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
81bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
82bbbb04ceSAlfred Perlstein static int	dofileread(struct thread *, struct file *, int, void *,
83bbbb04ceSAlfred Perlstein 		    size_t, off_t, int);
84bbbb04ceSAlfred Perlstein static int	dofilewrite(struct thread *, struct file *, int,
85bbbb04ceSAlfred Perlstein 		    const void *, size_t, off_t, int);
86512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
878fe387abSDmitrij Tejblum 
88df8bae1dSRodney W. Grimes /*
89df8bae1dSRodney W. Grimes  * Read system call.
90df8bae1dSRodney W. Grimes  */
91d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
92df8bae1dSRodney W. Grimes struct read_args {
93df8bae1dSRodney W. Grimes 	int	fd;
94134e06feSBruce Evans 	void	*buf;
95134e06feSBruce Evans 	size_t	nbyte;
96df8bae1dSRodney W. Grimes };
97d2d3e875SBruce Evans #endif
98ad2edad9SMatthew Dillon /*
99ad2edad9SMatthew Dillon  * MPSAFE
100ad2edad9SMatthew Dillon  */
10126f9a767SRodney W. Grimes int
102b40ce416SJulian Elischer read(td, uap)
103b40ce416SJulian Elischer 	struct thread *td;
104b064d43dSMatthew Dillon 	struct read_args *uap;
105df8bae1dSRodney W. Grimes {
106b064d43dSMatthew Dillon 	struct file *fp;
107279d7226SMatthew Dillon 	int error;
108df8bae1dSRodney W. Grimes 
109b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) == 0) {
110b40ce416SJulian Elischer 		error = dofileread(td, fp, uap->fd, uap->buf,
111ad2edad9SMatthew Dillon 			    uap->nbyte, (off_t)-1, 0);
112b40ce416SJulian Elischer 		fdrop(fp, td);
113ad2edad9SMatthew Dillon 	}
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 
14097fa4397SAlfred Perlstein 	if ((error = fget_read(td, uap->fd, &fp)) != 0)
14197fa4397SAlfred Perlstein 		return (error);
1429bbee259SAndrey A. Chernov 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
143b064d43dSMatthew Dillon 		error = ESPIPE;
1449bbee259SAndrey A. Chernov 	else if (uap->offset < 0 && fp->f_vnode->v_type != VCHR)
1459bbee259SAndrey A. Chernov 		error = EINVAL;
1469bbee259SAndrey A. Chernov 	else {
147426da3bcSAlfred Perlstein 		error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte,
148426da3bcSAlfred Perlstein 			    uap->offset, FOF_OFFSET);
149b064d43dSMatthew Dillon 	}
150b40ce416SJulian Elischer 	fdrop(fp, td);
151279d7226SMatthew Dillon 	return(error);
1528fe387abSDmitrij Tejblum }
1538fe387abSDmitrij Tejblum 
1548fe387abSDmitrij Tejblum /*
1558fe387abSDmitrij Tejblum  * Code common for read and pread
1568fe387abSDmitrij Tejblum  */
15737c84183SPoul-Henning Kamp static 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 
1750a3e28cfSAlfred Perlstein 	aiov.iov_base = 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 	 */
19060a9bb19SJohn Baldwin 	if (KTRPOINT(td, 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 
1989ca43589SRobert Watson 	if ((error = fo_read(fp, &auio, td->td_ucred, 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;
20860a9bb19SJohn Baldwin 		ktrgenio(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];
23882641acdSAlan Cox 	long i, cnt;
23982641acdSAlan Cox 	int error;
240df8bae1dSRodney W. Grimes 	u_int iovlen;
241df8bae1dSRodney W. Grimes #ifdef KTRACE
242df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
24342ebfbf2SBrian Feldman 	struct uio ktruio;
244df8bae1dSRodney W. Grimes #endif
245df8bae1dSRodney W. Grimes 
246b064d43dSMatthew Dillon 	if ((error = fget_read(td, uap->fd, &fp)) != 0)
24782641acdSAlan Cox 		return (error);
24882641acdSAlan Cox 	needfree = NULL;
249df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
250df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
251df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
252ad2edad9SMatthew Dillon 		if (uap->iovcnt > UIO_MAXIOV) {
253ad2edad9SMatthew Dillon 			error = EINVAL;
25482641acdSAlan Cox 			goto done;
255ad2edad9SMatthew Dillon 		}
256a163d034SWarner Losh 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
257df8bae1dSRodney W. Grimes 		needfree = iov;
25882641acdSAlan Cox 	} else
259df8bae1dSRodney W. Grimes 		iov = aiov;
260df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
261df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
262df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
263df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
264b40ce416SJulian Elischer 	auio.uio_td = td;
2652c1011f7SJohn Dyson 	auio.uio_offset = -1;
2660a3e28cfSAlfred Perlstein 	if ((error = copyin(uap->iovp, iov, iovlen)))
267df8bae1dSRodney W. Grimes 		goto done;
268df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
269df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
270069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
271df8bae1dSRodney W. Grimes 			error = EINVAL;
272df8bae1dSRodney W. Grimes 			goto done;
273df8bae1dSRodney W. Grimes 		}
274069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
275df8bae1dSRodney W. Grimes 		iov++;
276df8bae1dSRodney W. Grimes 	}
277df8bae1dSRodney W. Grimes #ifdef KTRACE
278df8bae1dSRodney W. Grimes 	/*
279df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
280df8bae1dSRodney W. Grimes 	 */
28160a9bb19SJohn Baldwin 	if (KTRPOINT(td, KTR_GENIO))  {
282a163d034SWarner Losh 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
2830a3e28cfSAlfred Perlstein 		bcopy(auio.uio_iov, ktriov, iovlen);
28442ebfbf2SBrian Feldman 		ktruio = auio;
285df8bae1dSRodney W. Grimes 	}
286df8bae1dSRodney W. Grimes #endif
287df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
2889ca43589SRobert Watson 	if ((error = fo_read(fp, &auio, td->td_ucred, 0, td))) {
289df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
290df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
291df8bae1dSRodney W. Grimes 			error = 0;
292279d7226SMatthew Dillon 	}
293df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
294df8bae1dSRodney W. Grimes #ifdef KTRACE
295df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
29642ebfbf2SBrian Feldman 		if (error == 0) {
29742ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
29842ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
29960a9bb19SJohn Baldwin 			ktrgenio(uap->fd, UIO_READ, &ktruio, error);
30042ebfbf2SBrian Feldman 		}
301df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
302df8bae1dSRodney W. Grimes 	}
303df8bae1dSRodney W. Grimes #endif
304b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
305df8bae1dSRodney W. Grimes done:
306b40ce416SJulian Elischer 	fdrop(fp, td);
307df8bae1dSRodney W. Grimes 	if (needfree)
308df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
309df8bae1dSRodney W. Grimes 	return (error);
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes /*
313df8bae1dSRodney W. Grimes  * Write system call
314df8bae1dSRodney W. Grimes  */
315d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
316df8bae1dSRodney W. Grimes struct write_args {
317df8bae1dSRodney W. Grimes 	int	fd;
318134e06feSBruce Evans 	const void *buf;
319134e06feSBruce Evans 	size_t	nbyte;
320df8bae1dSRodney W. Grimes };
321d2d3e875SBruce Evans #endif
322ad2edad9SMatthew Dillon /*
323ad2edad9SMatthew Dillon  * MPSAFE
324ad2edad9SMatthew Dillon  */
32526f9a767SRodney W. Grimes int
326b40ce416SJulian Elischer write(td, uap)
327b40ce416SJulian Elischer 	struct thread *td;
328b064d43dSMatthew Dillon 	struct write_args *uap;
329df8bae1dSRodney W. Grimes {
330b064d43dSMatthew Dillon 	struct file *fp;
331279d7226SMatthew Dillon 	int error;
332df8bae1dSRodney W. Grimes 
333b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
334b40ce416SJulian Elischer 		error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte,
335ad2edad9SMatthew Dillon 			    (off_t)-1, 0);
336b40ce416SJulian Elischer 		fdrop(fp, td);
337ad2edad9SMatthew Dillon 	} else {
338b064d43dSMatthew Dillon 		error = EBADF;	/* XXX this can't be right */
339ad2edad9SMatthew Dillon 	}
340279d7226SMatthew Dillon 	return(error);
341df8bae1dSRodney W. Grimes }
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes /*
3448fe387abSDmitrij Tejblum  * Pwrite system call
3454160ccd9SAlan Cox  */
3464160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3474160ccd9SAlan Cox struct pwrite_args {
3484160ccd9SAlan Cox 	int	fd;
3494160ccd9SAlan Cox 	const void *buf;
3504160ccd9SAlan Cox 	size_t	nbyte;
3518fe387abSDmitrij Tejblum 	int	pad;
3524160ccd9SAlan Cox 	off_t	offset;
3534160ccd9SAlan Cox };
3544160ccd9SAlan Cox #endif
355ad2edad9SMatthew Dillon /*
356ad2edad9SMatthew Dillon  * MPSAFE
357ad2edad9SMatthew Dillon  */
3584160ccd9SAlan Cox int
359b40ce416SJulian Elischer pwrite(td, uap)
360b40ce416SJulian Elischer 	struct thread *td;
361b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3624160ccd9SAlan Cox {
363b064d43dSMatthew Dillon 	struct file *fp;
364279d7226SMatthew Dillon 	int error;
3658fe387abSDmitrij Tejblum 
366b064d43dSMatthew Dillon 	if ((error = fget_write(td, uap->fd, &fp)) == 0) {
3679bbee259SAndrey A. Chernov 		if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
3682db4b023SPoul-Henning Kamp 			error = ESPIPE;
3699bbee259SAndrey A. Chernov 		else if (uap->offset < 0 && fp->f_vnode->v_type != VCHR)
3709bbee259SAndrey A. Chernov 			error = EINVAL;
3719bbee259SAndrey A. Chernov 		else {
372b064d43dSMatthew Dillon 			error = dofilewrite(td, fp, uap->fd, uap->buf,
373b064d43dSMatthew Dillon 				    uap->nbyte, uap->offset, FOF_OFFSET);
374b064d43dSMatthew Dillon 		}
375b40ce416SJulian Elischer 		fdrop(fp, td);
376279d7226SMatthew Dillon 	} else {
377b064d43dSMatthew Dillon 		error = EBADF;	/* this can't be right */
378ad2edad9SMatthew Dillon 	}
379279d7226SMatthew Dillon 	return(error);
3808fe387abSDmitrij Tejblum }
3818fe387abSDmitrij Tejblum 
3828fe387abSDmitrij Tejblum static int
383b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags)
384b40ce416SJulian Elischer 	struct thread *td;
3858fe387abSDmitrij Tejblum 	struct file *fp;
3868fe387abSDmitrij Tejblum 	int fd, flags;
3878fe387abSDmitrij Tejblum 	const void *buf;
3888fe387abSDmitrij Tejblum 	size_t nbyte;
3898fe387abSDmitrij Tejblum 	off_t offset;
3908fe387abSDmitrij Tejblum {
3914160ccd9SAlan Cox 	struct uio auio;
3924160ccd9SAlan Cox 	struct iovec aiov;
3934160ccd9SAlan Cox 	long cnt, error = 0;
3944160ccd9SAlan Cox #ifdef KTRACE
3954160ccd9SAlan Cox 	struct iovec ktriov;
39642ebfbf2SBrian Feldman 	struct uio ktruio;
3973c89e357SBrian Feldman 	int didktr = 0;
3984160ccd9SAlan Cox #endif
3994160ccd9SAlan Cox 
400b31ae1adSPeter Wemm 	aiov.iov_base = (void *)(uintptr_t)buf;
4018fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
4024160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4034160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
4048fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
4058fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
4064160ccd9SAlan Cox 		return (EINVAL);
4078fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
4084160ccd9SAlan Cox 	auio.uio_rw = UIO_WRITE;
4094160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
410b40ce416SJulian Elischer 	auio.uio_td = td;
4114160ccd9SAlan Cox #ifdef KTRACE
4124160ccd9SAlan Cox 	/*
41342ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
4144160ccd9SAlan Cox 	 */
41560a9bb19SJohn Baldwin 	if (KTRPOINT(td, KTR_GENIO)) {
4164160ccd9SAlan Cox 		ktriov = aiov;
41742ebfbf2SBrian Feldman 		ktruio = auio;
4183c89e357SBrian Feldman 		didktr = 1;
41942ebfbf2SBrian Feldman 	}
4204160ccd9SAlan Cox #endif
4218fe387abSDmitrij Tejblum 	cnt = nbyte;
422c6ab5768SAlfred Perlstein 	if (fp->f_type == DTYPE_VNODE)
423279d7226SMatthew Dillon 		bwillwrite();
4249ca43589SRobert Watson 	if ((error = fo_write(fp, &auio, td->td_ucred, flags, td))) {
4254160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
4264160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
4274160ccd9SAlan Cox 			error = 0;
428c33c8251SAlfred Perlstein 		/* Socket layer is responsible for issuing SIGPIPE. */
429c33c8251SAlfred Perlstein 		if (error == EPIPE && fp->f_type != DTYPE_SOCKET) {
430b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
431b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
432b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
43319eb87d2SJohn Baldwin 		}
4344160ccd9SAlan Cox 	}
4354160ccd9SAlan Cox 	cnt -= auio.uio_resid;
4364160ccd9SAlan Cox #ifdef KTRACE
4373c89e357SBrian Feldman 	if (didktr && error == 0) {
43842ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
43942ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
44060a9bb19SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, &ktruio, error);
44142ebfbf2SBrian Feldman 	}
4424160ccd9SAlan Cox #endif
443b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
4444160ccd9SAlan Cox 	return (error);
4454160ccd9SAlan Cox }
4464160ccd9SAlan Cox 
4474160ccd9SAlan Cox /*
448df8bae1dSRodney W. Grimes  * Gather write system call
449df8bae1dSRodney W. Grimes  */
450d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
451df8bae1dSRodney W. Grimes struct writev_args {
452df8bae1dSRodney W. Grimes 	int	fd;
453df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
454df8bae1dSRodney W. Grimes 	u_int	iovcnt;
455df8bae1dSRodney W. Grimes };
456d2d3e875SBruce Evans #endif
457ad2edad9SMatthew Dillon /*
458ad2edad9SMatthew Dillon  * MPSAFE
459ad2edad9SMatthew Dillon  */
46026f9a767SRodney W. Grimes int
461b40ce416SJulian Elischer writev(td, uap)
462b40ce416SJulian Elischer 	struct thread *td;
463df8bae1dSRodney W. Grimes 	register struct writev_args *uap;
464df8bae1dSRodney W. Grimes {
465b064d43dSMatthew Dillon 	struct file *fp;
466df8bae1dSRodney W. Grimes 	struct uio auio;
467df8bae1dSRodney W. Grimes 	register struct iovec *iov;
468df8bae1dSRodney W. Grimes 	struct iovec *needfree;
469df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
470df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
471df8bae1dSRodney W. Grimes 	u_int iovlen;
472df8bae1dSRodney W. Grimes #ifdef KTRACE
473df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
47442ebfbf2SBrian Feldman 	struct uio ktruio;
475df8bae1dSRodney W. Grimes #endif
476df8bae1dSRodney W. Grimes 
477882d8469SAlan Cox 	if ((error = fget_write(td, uap->fd, &fp)) != 0)
478882d8469SAlan Cox 		return (EBADF);
479882d8469SAlan Cox 	needfree = NULL;
480df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
481df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
482df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
4831aa3e7ddSBrian Feldman 		if (uap->iovcnt > UIO_MAXIOV) {
4841aa3e7ddSBrian Feldman 			error = EINVAL;
4851aa3e7ddSBrian Feldman 			goto done;
4861aa3e7ddSBrian Feldman 		}
487a163d034SWarner Losh 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
488df8bae1dSRodney W. Grimes 		needfree = iov;
489882d8469SAlan Cox 	} else
490df8bae1dSRodney W. Grimes 		iov = aiov;
491df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
492df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
493df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
494df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
495b40ce416SJulian Elischer 	auio.uio_td = td;
4962c1011f7SJohn Dyson 	auio.uio_offset = -1;
4970a3e28cfSAlfred Perlstein 	if ((error = copyin(uap->iovp, iov, iovlen)))
498df8bae1dSRodney W. Grimes 		goto done;
499df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
500df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
501069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
502df8bae1dSRodney W. Grimes 			error = EINVAL;
503df8bae1dSRodney W. Grimes 			goto done;
504df8bae1dSRodney W. Grimes 		}
505069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
506df8bae1dSRodney W. Grimes 		iov++;
507df8bae1dSRodney W. Grimes 	}
508df8bae1dSRodney W. Grimes #ifdef KTRACE
509df8bae1dSRodney W. Grimes 	/*
51042ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
511df8bae1dSRodney W. Grimes 	 */
51260a9bb19SJohn Baldwin 	if (KTRPOINT(td, KTR_GENIO))  {
513a163d034SWarner Losh 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
5140a3e28cfSAlfred Perlstein 		bcopy(auio.uio_iov, ktriov, iovlen);
51542ebfbf2SBrian Feldman 		ktruio = auio;
516df8bae1dSRodney W. Grimes 	}
517df8bae1dSRodney W. Grimes #endif
518df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
519a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5209440653dSMatthew Dillon 		bwillwrite();
5219ca43589SRobert Watson 	if ((error = fo_write(fp, &auio, td->td_ucred, 0, td))) {
522df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
523df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
524df8bae1dSRodney W. Grimes 			error = 0;
52519eb87d2SJohn Baldwin 		if (error == EPIPE) {
526b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
527b40ce416SJulian Elischer 			psignal(td->td_proc, SIGPIPE);
528b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
52919eb87d2SJohn Baldwin 		}
530df8bae1dSRodney W. Grimes 	}
531df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
532df8bae1dSRodney W. Grimes #ifdef KTRACE
533df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
53442ebfbf2SBrian Feldman 		if (error == 0) {
53542ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
53642ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
53760a9bb19SJohn Baldwin 			ktrgenio(uap->fd, UIO_WRITE, &ktruio, error);
53842ebfbf2SBrian Feldman 		}
539df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
540df8bae1dSRodney W. Grimes 	}
541df8bae1dSRodney W. Grimes #endif
542b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
543df8bae1dSRodney W. Grimes done:
544b40ce416SJulian Elischer 	fdrop(fp, td);
545df8bae1dSRodney W. Grimes 	if (needfree)
546df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
547df8bae1dSRodney W. Grimes 	return (error);
548df8bae1dSRodney W. Grimes }
549df8bae1dSRodney W. Grimes 
550df8bae1dSRodney W. Grimes /*
551df8bae1dSRodney W. Grimes  * Ioctl system call
552df8bae1dSRodney W. Grimes  */
553d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
554df8bae1dSRodney W. Grimes struct ioctl_args {
555df8bae1dSRodney W. Grimes 	int	fd;
556069e9bc1SDoug Rabson 	u_long	com;
557df8bae1dSRodney W. Grimes 	caddr_t	data;
558df8bae1dSRodney W. Grimes };
559d2d3e875SBruce Evans #endif
560ad2edad9SMatthew Dillon /*
561ad2edad9SMatthew Dillon  * MPSAFE
562ad2edad9SMatthew Dillon  */
563df8bae1dSRodney W. Grimes /* ARGSUSED */
56426f9a767SRodney W. Grimes int
565b40ce416SJulian Elischer ioctl(td, uap)
566b40ce416SJulian Elischer 	struct thread *td;
567df8bae1dSRodney W. Grimes 	register struct ioctl_args *uap;
568df8bae1dSRodney W. Grimes {
569a4db4953SAlfred Perlstein 	struct file *fp;
570df8bae1dSRodney W. Grimes 	register struct filedesc *fdp;
571831b9ef2SDoug Rabson 	register u_long com;
572ad2edad9SMatthew Dillon 	int error = 0;
573df8bae1dSRodney W. Grimes 	register u_int size;
574df8bae1dSRodney W. Grimes 	caddr_t data, memp;
575df8bae1dSRodney W. Grimes 	int tmp;
576df8bae1dSRodney W. Grimes #define STK_PARAMS	128
577d2ba455cSMatthew Dillon 	union {
578df8bae1dSRodney W. Grimes 	    char stkbuf[STK_PARAMS];
579d2ba455cSMatthew Dillon 	    long align;
580d2ba455cSMatthew Dillon 	} ubuf;
581df8bae1dSRodney W. Grimes 
582a4db4953SAlfred Perlstein 	if ((error = fget(td, uap->fd, &fp)) != 0)
583a4db4953SAlfred Perlstein 		return (error);
584aa11a498SAlfred Perlstein 	mtx_lock(&Giant);
585ad2edad9SMatthew Dillon 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
586426da3bcSAlfred Perlstein 		fdrop(fp, td);
587aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
588426da3bcSAlfred Perlstein 		return (EBADF);
589ad2edad9SMatthew Dillon 	}
590426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
591df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
592df8bae1dSRodney W. Grimes 	case FIONCLEX:
593426da3bcSAlfred Perlstein 		FILEDESC_LOCK(fdp);
594df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
595426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(fdp);
596426da3bcSAlfred Perlstein 		fdrop(fp, td);
597aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
598426da3bcSAlfred Perlstein 		return (0);
599df8bae1dSRodney W. Grimes 	case FIOCLEX:
600426da3bcSAlfred Perlstein 		FILEDESC_LOCK(fdp);
601df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
602426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(fdp);
603426da3bcSAlfred Perlstein 		fdrop(fp, td);
604aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
605426da3bcSAlfred Perlstein 		return (0);
606df8bae1dSRodney W. Grimes 	}
607df8bae1dSRodney W. Grimes 
608df8bae1dSRodney W. Grimes 	/*
609df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
610df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
611df8bae1dSRodney W. Grimes 	 */
612df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
613ad2edad9SMatthew Dillon 	if (size > IOCPARM_MAX) {
614426da3bcSAlfred Perlstein 		fdrop(fp, td);
615aa11a498SAlfred Perlstein 		mtx_unlock(&Giant);
616426da3bcSAlfred Perlstein 		return (ENOTTY);
617ad2edad9SMatthew Dillon 	}
618279d7226SMatthew Dillon 
619df8bae1dSRodney W. Grimes 	memp = NULL;
620d2ba455cSMatthew Dillon 	if (size > sizeof (ubuf.stkbuf)) {
621a163d034SWarner Losh 		memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
622df8bae1dSRodney W. Grimes 		data = memp;
623279d7226SMatthew Dillon 	} else {
624d2ba455cSMatthew Dillon 		data = ubuf.stkbuf;
625279d7226SMatthew Dillon 	}
626df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
627df8bae1dSRodney W. Grimes 		if (size) {
628df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
629df8bae1dSRodney W. Grimes 			if (error) {
630df8bae1dSRodney W. Grimes 				if (memp)
631df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
632b40ce416SJulian Elischer 				fdrop(fp, td);
633426da3bcSAlfred Perlstein 				goto done;
634df8bae1dSRodney W. Grimes 			}
635279d7226SMatthew Dillon 		} else {
636df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
637279d7226SMatthew Dillon 		}
638279d7226SMatthew Dillon 	} else if ((com&IOC_OUT) && size) {
639df8bae1dSRodney W. Grimes 		/*
640df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
641df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
642df8bae1dSRodney W. Grimes 		 */
643df8bae1dSRodney W. Grimes 		bzero(data, size);
644279d7226SMatthew Dillon 	} else if (com&IOC_VOID) {
645df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
646279d7226SMatthew Dillon 	}
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 	switch (com) {
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes 	case FIONBIO:
651426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
652bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
653df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
654df8bae1dSRodney W. Grimes 		else
655df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
656426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
657d49fa1caSRobert Watson 		error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
658df8bae1dSRodney W. Grimes 		break;
659df8bae1dSRodney W. Grimes 
660df8bae1dSRodney W. Grimes 	case FIOASYNC:
661426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
662bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
663df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
664df8bae1dSRodney W. Grimes 		else
665df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
666426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
667d49fa1caSRobert Watson 		error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
668df8bae1dSRodney W. Grimes 		break;
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	default:
671d49fa1caSRobert Watson 		error = fo_ioctl(fp, com, data, td->td_ucred, td);
672df8bae1dSRodney W. Grimes 		/*
673df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
674df8bae1dSRodney W. Grimes 		 * already set and checked above.
675df8bae1dSRodney W. Grimes 		 */
676df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
677df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
678df8bae1dSRodney W. Grimes 		break;
679df8bae1dSRodney W. Grimes 	}
680df8bae1dSRodney W. Grimes 	if (memp)
681df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
682b40ce416SJulian Elischer 	fdrop(fp, td);
683426da3bcSAlfred Perlstein done:
684ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
685df8bae1dSRodney W. Grimes 	return (error);
686df8bae1dSRodney W. Grimes }
687df8bae1dSRodney W. Grimes 
68885f190e4SAlfred Perlstein /*
68985f190e4SAlfred Perlstein  * sellock and selwait are initialized in selectinit() via SYSINIT.
69085f190e4SAlfred Perlstein  */
69185f190e4SAlfred Perlstein struct mtx	sellock;
692265fc98fSSeigo Tanimura struct cv	selwait;
6939ae6d334SKelly Yancey u_int		nselcoll;	/* Select collisions since boot */
6949ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes /*
697df8bae1dSRodney W. Grimes  * Select system call.
698df8bae1dSRodney W. Grimes  */
699d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
700df8bae1dSRodney W. Grimes struct select_args {
701b08f7993SSujal Patel 	int	nd;
702df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
703df8bae1dSRodney W. Grimes 	struct	timeval *tv;
704df8bae1dSRodney W. Grimes };
705d2d3e875SBruce Evans #endif
706ad2edad9SMatthew Dillon /*
707ad2edad9SMatthew Dillon  * MPSAFE
708ad2edad9SMatthew Dillon  */
70926f9a767SRodney W. Grimes int
710b40ce416SJulian Elischer select(td, uap)
711b40ce416SJulian Elischer 	register struct thread *td;
712df8bae1dSRodney W. Grimes 	register struct select_args *uap;
713df8bae1dSRodney W. Grimes {
7148f19eb88SIan Dowse 	struct timeval tv, *tvp;
7158f19eb88SIan Dowse 	int error;
7168f19eb88SIan Dowse 
7178f19eb88SIan Dowse 	if (uap->tv != NULL) {
7188f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
7198f19eb88SIan Dowse 		if (error)
7208f19eb88SIan Dowse 			return (error);
7218f19eb88SIan Dowse 		tvp = &tv;
7228f19eb88SIan Dowse 	} else
7238f19eb88SIan Dowse 		tvp = NULL;
7248f19eb88SIan Dowse 
7258f19eb88SIan Dowse 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
7268f19eb88SIan Dowse }
7278f19eb88SIan Dowse 
7288f19eb88SIan Dowse int
7298f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
7308f19eb88SIan Dowse     fd_set *fd_ex, struct timeval *tvp)
7318f19eb88SIan Dowse {
732426da3bcSAlfred Perlstein 	struct filedesc *fdp;
733d5e4d7e1SBruce Evans 	/*
734d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
735d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
736d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
737d5e4d7e1SBruce Evans 	 * of 256.
738d5e4d7e1SBruce Evans 	 */
739d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
740eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
74100af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
7429ae6d334SKelly Yancey 	int error, timo;
7439ae6d334SKelly Yancey 	u_int ncoll, nbufbytes, ncpbytes, nfdbits;
744df8bae1dSRodney W. Grimes 
7458f19eb88SIan Dowse 	if (nd < 0)
746acbfbfeaSSujal Patel 		return (EINVAL);
747426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
748ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
749426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
750ad2edad9SMatthew Dillon 
7518f19eb88SIan Dowse 	if (nd > td->td_proc->p_fd->fd_nfiles)
7528f19eb88SIan Dowse 		nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
753426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
754b08f7993SSujal Patel 
755d5e4d7e1SBruce Evans 	/*
756d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
757d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
758d5e4d7e1SBruce Evans 	 */
7598f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
760d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
761d5e4d7e1SBruce Evans 	nbufbytes = 0;
7628f19eb88SIan Dowse 	if (fd_in != NULL)
763d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7648f19eb88SIan Dowse 	if (fd_ou != NULL)
765d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
7668f19eb88SIan Dowse 	if (fd_ex != NULL)
767d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
768d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
769d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
770d5e4d7e1SBruce Evans 	else
771a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
772b08f7993SSujal Patel 
773b08f7993SSujal Patel 	/*
774d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
775d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
776d5e4d7e1SBruce Evans 	 * together.
777b08f7993SSujal Patel 	 */
778d5e4d7e1SBruce Evans 	sbp = selbits;
779df8bae1dSRodney W. Grimes #define	getbits(name, x) \
780d5e4d7e1SBruce Evans 	do {								\
7818f19eb88SIan Dowse 		if (name == NULL)					\
782d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
783d5e4d7e1SBruce Evans 		else {							\
784d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
785d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
786d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
7878f19eb88SIan Dowse 			error = copyin(name, ibits[x], ncpbytes);	\
788265fc98fSSeigo Tanimura 			if (error != 0)					\
78985f190e4SAlfred Perlstein 				goto done_nosellock;			\
790e04ac2feSJohn Baldwin 		}							\
791d5e4d7e1SBruce Evans 	} while (0)
7928f19eb88SIan Dowse 	getbits(fd_in, 0);
7938f19eb88SIan Dowse 	getbits(fd_ou, 1);
7948f19eb88SIan Dowse 	getbits(fd_ex, 2);
795df8bae1dSRodney W. Grimes #undef	getbits
796d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
797d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
798df8bae1dSRodney W. Grimes 
7998f19eb88SIan Dowse 	if (tvp != NULL) {
8008f19eb88SIan Dowse 		atv = *tvp;
801df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
802df8bae1dSRodney W. Grimes 			error = EINVAL;
80385f190e4SAlfred Perlstein 			goto done_nosellock;
804df8bae1dSRodney W. Grimes 		}
805c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
80600af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
8079c386f6bSJohn Baldwin 	} else {
80800af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
8099c386f6bSJohn Baldwin 		atv.tv_usec = 0;
8109c386f6bSJohn Baldwin 	}
81100af9731SPoul-Henning Kamp 	timo = 0;
8122149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
81385f190e4SAlfred Perlstein 	mtx_lock(&sellock);
814df8bae1dSRodney W. Grimes retry:
815df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
816fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
817b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
818fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
81985f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
82085f190e4SAlfred Perlstein 
8218f19eb88SIan Dowse 	error = selscan(td, ibits, obits, nd);
82285f190e4SAlfred Perlstein 	mtx_lock(&sellock);
823b40ce416SJulian Elischer 	if (error || td->td_retval[0])
824df8bae1dSRodney W. Grimes 		goto done;
8254da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
826c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
82785f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
828df8bae1dSRodney W. Grimes 			goto done;
82900af9731SPoul-Henning Kamp 		ttv = atv;
83000af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
83100af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
83200af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
833df8bae1dSRodney W. Grimes 	}
83485f190e4SAlfred Perlstein 
83585f190e4SAlfred Perlstein 	/*
83685f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
83785f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of
83885f190e4SAlfred Perlstein 	 * collisions and rescan the file descriptors if
83985f190e4SAlfred Perlstein 	 * necessary.
84085f190e4SAlfred Perlstein 	 */
841fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
84285f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
84385f190e4SAlfred Perlstein 		mtx_unlock_spin(&sched_lock);
84485f190e4SAlfred Perlstein 		goto retry;
84585f190e4SAlfred Perlstein 	}
846fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
847bfbbc4aaSJason Evans 
848265fc98fSSeigo Tanimura 	if (timo > 0)
84985f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
850265fc98fSSeigo Tanimura 	else
85185f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
852bfbbc4aaSJason Evans 
853df8bae1dSRodney W. Grimes 	if (error == 0)
854df8bae1dSRodney W. Grimes 		goto retry;
855265fc98fSSeigo Tanimura 
856df8bae1dSRodney W. Grimes done:
85785f190e4SAlfred Perlstein 	clear_selinfo_list(td);
858fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
859b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
860fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
86185f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
86285f190e4SAlfred Perlstein 
86385f190e4SAlfred Perlstein done_nosellock:
864df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
865df8bae1dSRodney W. Grimes 	if (error == ERESTART)
866df8bae1dSRodney W. Grimes 		error = EINTR;
867df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
868df8bae1dSRodney W. Grimes 		error = 0;
869df8bae1dSRodney W. Grimes #define	putbits(name, x) \
8708f19eb88SIan Dowse 	if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
871df8bae1dSRodney W. Grimes 		error = error2;
872df8bae1dSRodney W. Grimes 	if (error == 0) {
873df8bae1dSRodney W. Grimes 		int error2;
874df8bae1dSRodney W. Grimes 
8758f19eb88SIan Dowse 		putbits(fd_in, 0);
8768f19eb88SIan Dowse 		putbits(fd_ou, 1);
8778f19eb88SIan Dowse 		putbits(fd_ex, 2);
878df8bae1dSRodney W. Grimes #undef putbits
879df8bae1dSRodney W. Grimes 	}
880d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
881d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
882ad2edad9SMatthew Dillon 
883ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
884df8bae1dSRodney W. Grimes 	return (error);
885df8bae1dSRodney W. Grimes }
886df8bae1dSRodney W. Grimes 
887265fc98fSSeigo Tanimura static int
888b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
889b40ce416SJulian Elischer 	struct thread *td;
890b08f7993SSujal Patel 	fd_mask **ibits, **obits;
891cb226aaaSPoul-Henning Kamp 	int nfd;
892df8bae1dSRodney W. Grimes {
893f082218cSPeter Wemm 	int msk, i, fd;
894f082218cSPeter Wemm 	fd_mask bits;
895df8bae1dSRodney W. Grimes 	struct file *fp;
896df8bae1dSRodney W. Grimes 	int n = 0;
8972087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
89842d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
899eb209311SAlfred Perlstein 	struct filedesc *fdp = td->td_proc->p_fd;
900df8bae1dSRodney W. Grimes 
901eb209311SAlfred Perlstein 	FILEDESC_LOCK(fdp);
902df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
903d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
904d5e4d7e1SBruce Evans 			continue;
905df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
906b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
907f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
908f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
909f082218cSPeter Wemm 				if (!(bits & 1))
910f082218cSPeter Wemm 					continue;
911eb209311SAlfred Perlstein 				if ((fp = fget_locked(fdp, fd)) == NULL) {
912eb209311SAlfred Perlstein 					FILEDESC_UNLOCK(fdp);
913df8bae1dSRodney W. Grimes 					return (EBADF);
914eb209311SAlfred Perlstein 				}
915ea6027a8SRobert Watson 				if (fo_poll(fp, flag[msk], td->td_ucred,
916ea6027a8SRobert Watson 				    td)) {
917b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
918f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
919df8bae1dSRodney W. Grimes 					n++;
920df8bae1dSRodney W. Grimes 				}
921df8bae1dSRodney W. Grimes 			}
922df8bae1dSRodney W. Grimes 		}
923df8bae1dSRodney W. Grimes 	}
924eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
925b40ce416SJulian Elischer 	td->td_retval[0] = n;
926df8bae1dSRodney W. Grimes 	return (0);
927df8bae1dSRodney W. Grimes }
928df8bae1dSRodney W. Grimes 
92942d11757SPeter Wemm /*
93042d11757SPeter Wemm  * Poll system call.
93142d11757SPeter Wemm  */
93242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
93342d11757SPeter Wemm struct poll_args {
93442d11757SPeter Wemm 	struct pollfd *fds;
93542d11757SPeter Wemm 	u_int	nfds;
93642d11757SPeter Wemm 	int	timeout;
93742d11757SPeter Wemm };
93842d11757SPeter Wemm #endif
939ad2edad9SMatthew Dillon /*
940ad2edad9SMatthew Dillon  * MPSAFE
941ad2edad9SMatthew Dillon  */
94242d11757SPeter Wemm int
943b40ce416SJulian Elischer poll(td, uap)
944b40ce416SJulian Elischer 	struct thread *td;
945ea0237edSJonathan Lemon 	struct poll_args *uap;
94642d11757SPeter Wemm {
94742d11757SPeter Wemm 	caddr_t bits;
94842d11757SPeter Wemm 	char smallbits[32 * sizeof(struct pollfd)];
94900af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
9509ae6d334SKelly Yancey 	int error = 0, timo;
9519ae6d334SKelly Yancey 	u_int ncoll, nfds;
95242d11757SPeter Wemm 	size_t ni;
95342d11757SPeter Wemm 
954d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
955ad2edad9SMatthew Dillon 
956ad2edad9SMatthew Dillon 	mtx_lock(&Giant);
95789b71647SPeter Wemm 	/*
9582bd5ac33SPeter Wemm 	 * This is kinda bogus.  We have fd limits, but that is not
9592bd5ac33SPeter Wemm 	 * really related to the size of the pollfd array.  Make sure
9602bd5ac33SPeter Wemm 	 * we let the process use at least FD_SETSIZE entries and at
9612bd5ac33SPeter Wemm 	 * least enough for the current limits.  We want to be reasonably
9622bd5ac33SPeter Wemm 	 * safe, but not overly restrictive.
96389b71647SPeter Wemm 	 */
964b40ce416SJulian Elischer 	if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) &&
965b40ce416SJulian Elischer 	    (nfds > FD_SETSIZE)) {
966ad2edad9SMatthew Dillon 		error = EINVAL;
967ad2edad9SMatthew Dillon 		goto done2;
968ad2edad9SMatthew Dillon 	}
96989b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
97042d11757SPeter Wemm 	if (ni > sizeof(smallbits))
971a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
97242d11757SPeter Wemm 	else
97342d11757SPeter Wemm 		bits = smallbits;
974d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
97542d11757SPeter Wemm 	if (error)
97685f190e4SAlfred Perlstein 		goto done_nosellock;
977d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
978d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
979d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
98042d11757SPeter Wemm 		if (itimerfix(&atv)) {
98142d11757SPeter Wemm 			error = EINVAL;
98285f190e4SAlfred Perlstein 			goto done_nosellock;
98342d11757SPeter Wemm 		}
984c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
98500af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
9869c386f6bSJohn Baldwin 	} else {
98700af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
9889c386f6bSJohn Baldwin 		atv.tv_usec = 0;
9899c386f6bSJohn Baldwin 	}
99000af9731SPoul-Henning Kamp 	timo = 0;
9912149c527SPeter Wemm 	TAILQ_INIT(&td->td_selq);
99285f190e4SAlfred Perlstein 	mtx_lock(&sellock);
99342d11757SPeter Wemm retry:
99442d11757SPeter Wemm 	ncoll = nselcoll;
995fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
996b40ce416SJulian Elischer 	td->td_flags |= TDF_SELECT;
997fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
99885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
99985f190e4SAlfred Perlstein 
1000b40ce416SJulian Elischer 	error = pollscan(td, (struct pollfd *)bits, nfds);
100185f190e4SAlfred Perlstein 	mtx_lock(&sellock);
1002b40ce416SJulian Elischer 	if (error || td->td_retval[0])
100342d11757SPeter Wemm 		goto done;
10044da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
1005c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
100685f190e4SAlfred Perlstein 		if (timevalcmp(&rtv, &atv, >=))
100742d11757SPeter Wemm 			goto done;
100800af9731SPoul-Henning Kamp 		ttv = atv;
100900af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
101000af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
101100af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
101242d11757SPeter Wemm 	}
101385f190e4SAlfred Perlstein 	/*
101485f190e4SAlfred Perlstein 	 * An event of interest may occur while we do not hold
101585f190e4SAlfred Perlstein 	 * sellock, so check TDF_SELECT and the number of collisions
101685f190e4SAlfred Perlstein 	 * and rescan the file descriptors if necessary.
101785f190e4SAlfred Perlstein 	 */
1018fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
101985f190e4SAlfred Perlstein 	if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
1020fea2ab83SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
102185f190e4SAlfred Perlstein 		goto retry;
102285f190e4SAlfred Perlstein 	}
102385f190e4SAlfred Perlstein 	mtx_unlock_spin(&sched_lock);
102485f190e4SAlfred Perlstein 
1025265fc98fSSeigo Tanimura 	if (timo > 0)
102685f190e4SAlfred Perlstein 		error = cv_timedwait_sig(&selwait, &sellock, timo);
1027265fc98fSSeigo Tanimura 	else
102885f190e4SAlfred Perlstein 		error = cv_wait_sig(&selwait, &sellock);
102985f190e4SAlfred Perlstein 
103042d11757SPeter Wemm 	if (error == 0)
103142d11757SPeter Wemm 		goto retry;
1032265fc98fSSeigo Tanimura 
103342d11757SPeter Wemm done:
103485f190e4SAlfred Perlstein 	clear_selinfo_list(td);
1035fea2ab83SJohn Baldwin 	mtx_lock_spin(&sched_lock);
1036b40ce416SJulian Elischer 	td->td_flags &= ~TDF_SELECT;
1037fea2ab83SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
103885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
103985f190e4SAlfred Perlstein 
104085f190e4SAlfred Perlstein done_nosellock:
104142d11757SPeter Wemm 	/* poll is not restarted after signals... */
104242d11757SPeter Wemm 	if (error == ERESTART)
104342d11757SPeter Wemm 		error = EINTR;
104442d11757SPeter Wemm 	if (error == EWOULDBLOCK)
104542d11757SPeter Wemm 		error = 0;
104642d11757SPeter Wemm 	if (error == 0) {
1047d1e405c5SAlfred Perlstein 		error = copyout(bits, uap->fds, ni);
104842d11757SPeter Wemm 		if (error)
104942d11757SPeter Wemm 			goto out;
105042d11757SPeter Wemm 	}
105142d11757SPeter Wemm out:
105242d11757SPeter Wemm 	if (ni > sizeof(smallbits))
105342d11757SPeter Wemm 		free(bits, M_TEMP);
1054ad2edad9SMatthew Dillon done2:
1055ad2edad9SMatthew Dillon 	mtx_unlock(&Giant);
105642d11757SPeter Wemm 	return (error);
105742d11757SPeter Wemm }
105842d11757SPeter Wemm 
105942d11757SPeter Wemm static int
1060b40ce416SJulian Elischer pollscan(td, fds, nfd)
1061b40ce416SJulian Elischer 	struct thread *td;
106242d11757SPeter Wemm 	struct pollfd *fds;
1063ea0237edSJonathan Lemon 	u_int nfd;
106442d11757SPeter Wemm {
1065b40ce416SJulian Elischer 	register struct filedesc *fdp = td->td_proc->p_fd;
106642d11757SPeter Wemm 	int i;
106742d11757SPeter Wemm 	struct file *fp;
106842d11757SPeter Wemm 	int n = 0;
106942d11757SPeter Wemm 
1070426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
1071eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1072337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
107342d11757SPeter Wemm 			fds->revents = POLLNVAL;
107442d11757SPeter Wemm 			n++;
1075337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1076337c9691SJordan K. Hubbard 			fds->revents = 0;
107742d11757SPeter Wemm 		} else {
107842d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1079279d7226SMatthew Dillon 			if (fp == NULL) {
108042d11757SPeter Wemm 				fds->revents = POLLNVAL;
108142d11757SPeter Wemm 				n++;
108242d11757SPeter Wemm 			} else {
10832087c896SBruce Evans 				/*
10842087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
10852087c896SBruce Evans 				 * POLLERR if appropriate.
10862087c896SBruce Evans 				 */
108713ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1088ea6027a8SRobert Watson 				    td->td_ucred, td);
108942d11757SPeter Wemm 				if (fds->revents != 0)
109042d11757SPeter Wemm 					n++;
109142d11757SPeter Wemm 			}
109242d11757SPeter Wemm 		}
109342d11757SPeter Wemm 	}
1094eb209311SAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
1095b40ce416SJulian Elischer 	td->td_retval[0] = n;
109642d11757SPeter Wemm 	return (0);
109742d11757SPeter Wemm }
109842d11757SPeter Wemm 
109942d11757SPeter Wemm /*
110042d11757SPeter Wemm  * OpenBSD poll system call.
110142d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
110242d11757SPeter Wemm  */
110342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
110442d11757SPeter Wemm struct openbsd_poll_args {
110542d11757SPeter Wemm 	struct pollfd *fds;
110642d11757SPeter Wemm 	u_int	nfds;
110742d11757SPeter Wemm 	int	timeout;
110842d11757SPeter Wemm };
110942d11757SPeter Wemm #endif
1110ad2edad9SMatthew Dillon /*
1111ad2edad9SMatthew Dillon  * MPSAFE
1112ad2edad9SMatthew Dillon  */
111342d11757SPeter Wemm int
1114b40ce416SJulian Elischer openbsd_poll(td, uap)
1115b40ce416SJulian Elischer 	register struct thread *td;
111642d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
111742d11757SPeter Wemm {
1118b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
111942d11757SPeter Wemm }
112042d11757SPeter Wemm 
112185f190e4SAlfred Perlstein /*
112285f190e4SAlfred Perlstein  * Remove the references to the thread from all of the objects
112385f190e4SAlfred Perlstein  * we were polling.
112485f190e4SAlfred Perlstein  *
112585f190e4SAlfred Perlstein  * This code assumes that the underlying owner of the selinfo
112685f190e4SAlfred Perlstein  * structure will hold sellock before it changes it, and that
112785f190e4SAlfred Perlstein  * it will unlink itself from our list if it goes away.
112885f190e4SAlfred Perlstein  */
112985f190e4SAlfred Perlstein void
113085f190e4SAlfred Perlstein clear_selinfo_list(td)
113185f190e4SAlfred Perlstein 	struct thread *td;
113285f190e4SAlfred Perlstein {
113385f190e4SAlfred Perlstein 	struct selinfo *si;
113485f190e4SAlfred Perlstein 
113585f190e4SAlfred Perlstein 	mtx_assert(&sellock, MA_OWNED);
113685f190e4SAlfred Perlstein 	TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
113785f190e4SAlfred Perlstein 		si->si_thread = NULL;
113885f190e4SAlfred Perlstein 	TAILQ_INIT(&td->td_selq);
113985f190e4SAlfred Perlstein }
114085f190e4SAlfred Perlstein 
1141df8bae1dSRodney W. Grimes /*
1142df8bae1dSRodney W. Grimes  * Record a select request.
1143df8bae1dSRodney W. Grimes  */
1144df8bae1dSRodney W. Grimes void
1145df8bae1dSRodney W. Grimes selrecord(selector, sip)
1146b40ce416SJulian Elischer 	struct thread *selector;
1147df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1148df8bae1dSRodney W. Grimes {
1149df8bae1dSRodney W. Grimes 
115085f190e4SAlfred Perlstein 	mtx_lock(&sellock);
115185f190e4SAlfred Perlstein 	/*
1152b605b54cSAlfred Perlstein 	 * If the selinfo's thread pointer is NULL then take ownership of it.
1153b605b54cSAlfred Perlstein 	 *
1154b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and it points to another
1155b605b54cSAlfred Perlstein 	 * thread, then we have a collision.
1156b605b54cSAlfred Perlstein 	 *
1157b605b54cSAlfred Perlstein 	 * If the thread pointer is not NULL and points back to us then leave
1158b605b54cSAlfred Perlstein 	 * it alone as we've already added pointed it at us and added it to
1159b605b54cSAlfred Perlstein 	 * our list.
116085f190e4SAlfred Perlstein 	 */
116185f190e4SAlfred Perlstein 	if (sip->si_thread == NULL) {
1162b40ce416SJulian Elischer 		sip->si_thread = selector;
116385f190e4SAlfred Perlstein 		TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
116485f190e4SAlfred Perlstein 	} else if (sip->si_thread != selector) {
116585f190e4SAlfred Perlstein 		sip->si_flags |= SI_COLL;
116685f190e4SAlfred Perlstein 	}
116785f190e4SAlfred Perlstein 
116885f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1169df8bae1dSRodney W. Grimes }
1170df8bae1dSRodney W. Grimes 
1171512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1172df8bae1dSRodney W. Grimes void
1173df8bae1dSRodney W. Grimes selwakeup(sip)
117485f190e4SAlfred Perlstein 	struct selinfo *sip;
1175df8bae1dSRodney W. Grimes {
1176512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1177512824f8SSeigo Tanimura }
1178512824f8SSeigo Tanimura 
1179512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1180512824f8SSeigo Tanimura void
1181512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1182512824f8SSeigo Tanimura 	struct selinfo *sip;
1183512824f8SSeigo Tanimura 	int pri;
1184512824f8SSeigo Tanimura {
1185512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1186512824f8SSeigo Tanimura }
1187512824f8SSeigo Tanimura 
1188512824f8SSeigo Tanimura /*
1189512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1190512824f8SSeigo Tanimura  */
1191512824f8SSeigo Tanimura static void
1192512824f8SSeigo Tanimura doselwakeup(sip, pri)
1193512824f8SSeigo Tanimura 	struct selinfo *sip;
1194512824f8SSeigo Tanimura 	int pri;
1195512824f8SSeigo Tanimura {
1196b40ce416SJulian Elischer 	struct thread *td;
1197df8bae1dSRodney W. Grimes 
119885f190e4SAlfred Perlstein 	mtx_lock(&sellock);
119985f190e4SAlfred Perlstein 	td = sip->si_thread;
120085f190e4SAlfred Perlstein 	if ((sip->si_flags & SI_COLL) != 0) {
1201df8bae1dSRodney W. Grimes 		nselcoll++;
1202df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1203512824f8SSeigo Tanimura 		cv_broadcastpri(&selwait, pri);
1204df8bae1dSRodney W. Grimes 	}
120585f190e4SAlfred Perlstein 	if (td == NULL) {
120685f190e4SAlfred Perlstein 		mtx_unlock(&sellock);
1207b40ce416SJulian Elischer 		return;
1208b40ce416SJulian Elischer 	}
120985f190e4SAlfred Perlstein 	TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
121085f190e4SAlfred Perlstein 	sip->si_thread = NULL;
12119ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
12120a3e28cfSAlfred Perlstein 	if (td->td_wchan == &selwait) {
1213b40ce416SJulian Elischer 		cv_waitq_remove(td);
121471fad9fdSJulian Elischer 		TD_CLR_SLEEPING(td);
1215512824f8SSeigo Tanimura 		if (pri >= PRI_MIN && pri <= PRI_MAX && td->td_priority > pri)
1216512824f8SSeigo Tanimura 			td->td_priority = pri;
121771fad9fdSJulian Elischer 		setrunnable(td);
121833a9ed9dSJohn Baldwin 	} else
1219b40ce416SJulian Elischer 		td->td_flags &= ~TDF_SELECT;
122033a9ed9dSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
122185f190e4SAlfred Perlstein 	mtx_unlock(&sellock);
1222df8bae1dSRodney W. Grimes }
1223265fc98fSSeigo Tanimura 
12244d77a549SAlfred Perlstein static void selectinit(void *);
1225265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1226265fc98fSSeigo Tanimura 
1227265fc98fSSeigo Tanimura /* ARGSUSED*/
1228265fc98fSSeigo Tanimura static void
1229265fc98fSSeigo Tanimura selectinit(dummy)
1230265fc98fSSeigo Tanimura 	void *dummy;
1231265fc98fSSeigo Tanimura {
1232265fc98fSSeigo Tanimura 	cv_init(&selwait, "select");
12336008862bSJohn Baldwin 	mtx_init(&sellock, "sellck", NULL, MTX_DEF);
1234265fc98fSSeigo Tanimura }
1235