xref: /freebsd/sys/kern/sys_generic.c (revision 0a2c3d48c6383833e24ee0f978460e00fefa6b28)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46d2d3e875SBruce Evans #include <sys/sysproto.h>
47df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4820982410SBruce Evans #include <sys/filio.h>
493ac4d1efSBruce Evans #include <sys/fcntl.h>
50df8bae1dSRodney W. Grimes #include <sys/file.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
52797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
53df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
54df8bae1dSRodney W. Grimes #include <sys/uio.h>
55df8bae1dSRodney W. Grimes #include <sys/kernel.h>
56df8bae1dSRodney W. Grimes #include <sys/malloc.h>
5742d11757SPeter Wemm #include <sys/poll.h>
580a2c3d48SGarrett Wollman #include <sys/selinfo.h>
598cb96f20SPeter Wemm #include <sys/sysctl.h>
6042d11757SPeter Wemm #include <sys/sysent.h>
61279d7226SMatthew Dillon #include <sys/bio.h>
62279d7226SMatthew Dillon #include <sys/buf.h>
63df8bae1dSRodney W. Grimes #ifdef KTRACE
64df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
65df8bae1dSRodney W. Grimes #endif
66279d7226SMatthew Dillon #include <vm/vm.h>
67279d7226SMatthew Dillon #include <vm/vm_page.h>
68df8bae1dSRodney W. Grimes 
69069e9bc1SDoug Rabson #include <machine/limits.h>
70069e9bc1SDoug Rabson 
71a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
72a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
73a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7455166637SPoul-Henning Kamp 
75cb226aaaSPoul-Henning Kamp static int	pollscan __P((struct proc *, struct pollfd *, int));
762087c896SBruce Evans static int	selscan __P((struct proc *, fd_mask **, fd_mask **, int));
778fe387abSDmitrij Tejblum static int	dofileread __P((struct proc *, struct file *, int, void *,
788fe387abSDmitrij Tejblum 		    size_t, off_t, int));
798fe387abSDmitrij Tejblum static int	dofilewrite __P((struct proc *, struct file *, int,
808fe387abSDmitrij Tejblum 		    const void *, size_t, off_t, int));
818fe387abSDmitrij Tejblum 
828757e5bbSAlfred Perlstein struct file*
83279d7226SMatthew Dillon holdfp(fdp, fd, flag)
848fe387abSDmitrij Tejblum 	struct filedesc* fdp;
858fe387abSDmitrij Tejblum 	int fd, flag;
868fe387abSDmitrij Tejblum {
878fe387abSDmitrij Tejblum 	struct file* fp;
888fe387abSDmitrij Tejblum 
898fe387abSDmitrij Tejblum 	if (((u_int)fd) >= fdp->fd_nfiles ||
908fe387abSDmitrij Tejblum 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
91279d7226SMatthew Dillon 	    (fp->f_flag & flag) == 0) {
928fe387abSDmitrij Tejblum 		return (NULL);
93279d7226SMatthew Dillon 	}
94279d7226SMatthew Dillon 	fhold(fp);
958fe387abSDmitrij Tejblum 	return (fp);
968fe387abSDmitrij Tejblum }
97d93f860cSPoul-Henning Kamp 
98df8bae1dSRodney W. Grimes /*
99df8bae1dSRodney W. Grimes  * Read system call.
100df8bae1dSRodney W. Grimes  */
101d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
102df8bae1dSRodney W. Grimes struct read_args {
103df8bae1dSRodney W. Grimes 	int	fd;
104134e06feSBruce Evans 	void	*buf;
105134e06feSBruce Evans 	size_t	nbyte;
106df8bae1dSRodney W. Grimes };
107d2d3e875SBruce Evans #endif
10826f9a767SRodney W. Grimes int
109cb226aaaSPoul-Henning Kamp read(p, uap)
110df8bae1dSRodney W. Grimes 	struct proc *p;
111df8bae1dSRodney W. Grimes 	register struct read_args *uap;
112df8bae1dSRodney W. Grimes {
113df8bae1dSRodney W. Grimes 	register struct file *fp;
114279d7226SMatthew Dillon 	int error;
115df8bae1dSRodney W. Grimes 
116279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
117df8bae1dSRodney W. Grimes 		return (EBADF);
118279d7226SMatthew Dillon 	error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
119279d7226SMatthew Dillon 	fdrop(fp, p);
120279d7226SMatthew Dillon 	return(error);
121df8bae1dSRodney W. Grimes }
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes /*
1248fe387abSDmitrij Tejblum  * Pread system call
1254160ccd9SAlan Cox  */
1264160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1274160ccd9SAlan Cox struct pread_args {
1284160ccd9SAlan Cox 	int	fd;
1294160ccd9SAlan Cox 	void	*buf;
1304160ccd9SAlan Cox 	size_t	nbyte;
1318fe387abSDmitrij Tejblum 	int	pad;
1324160ccd9SAlan Cox 	off_t	offset;
1334160ccd9SAlan Cox };
1344160ccd9SAlan Cox #endif
1354160ccd9SAlan Cox int
1364160ccd9SAlan Cox pread(p, uap)
1374160ccd9SAlan Cox 	struct proc *p;
1384160ccd9SAlan Cox 	register struct pread_args *uap;
1394160ccd9SAlan Cox {
1404160ccd9SAlan Cox 	register struct file *fp;
141279d7226SMatthew Dillon 	int error;
1428fe387abSDmitrij Tejblum 
143279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
1448fe387abSDmitrij Tejblum 		return (EBADF);
145279d7226SMatthew Dillon 	if (fp->f_type != DTYPE_VNODE) {
146279d7226SMatthew Dillon 		error = ESPIPE;
147279d7226SMatthew Dillon 	} else {
148279d7226SMatthew Dillon 	    error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte,
149279d7226SMatthew Dillon 		uap->offset, FOF_OFFSET);
150279d7226SMatthew Dillon 	}
151279d7226SMatthew Dillon 	fdrop(fp, p);
152279d7226SMatthew Dillon 	return(error);
1538fe387abSDmitrij Tejblum }
1548fe387abSDmitrij Tejblum 
1558fe387abSDmitrij Tejblum /*
1568fe387abSDmitrij Tejblum  * Code common for read and pread
1578fe387abSDmitrij Tejblum  */
1588fe387abSDmitrij Tejblum int
1598fe387abSDmitrij Tejblum dofileread(p, fp, fd, buf, nbyte, offset, flags)
1608fe387abSDmitrij Tejblum 	struct proc *p;
1618fe387abSDmitrij Tejblum 	struct file *fp;
1628fe387abSDmitrij Tejblum 	int fd, flags;
1638fe387abSDmitrij Tejblum 	void *buf;
1648fe387abSDmitrij Tejblum 	size_t nbyte;
1658fe387abSDmitrij Tejblum 	off_t offset;
1668fe387abSDmitrij Tejblum {
1674160ccd9SAlan Cox 	struct uio auio;
1684160ccd9SAlan Cox 	struct iovec aiov;
1694160ccd9SAlan Cox 	long cnt, error = 0;
1704160ccd9SAlan Cox #ifdef KTRACE
1714160ccd9SAlan Cox 	struct iovec ktriov;
17242ebfbf2SBrian Feldman 	struct uio ktruio;
1733c89e357SBrian Feldman 	int didktr = 0;
1744160ccd9SAlan Cox #endif
1754160ccd9SAlan Cox 
1768fe387abSDmitrij Tejblum 	aiov.iov_base = (caddr_t)buf;
1778fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
1784160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1794160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
1808fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
1818fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
1824160ccd9SAlan Cox 		return (EINVAL);
1838fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
1844160ccd9SAlan Cox 	auio.uio_rw = UIO_READ;
1854160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
1864160ccd9SAlan Cox 	auio.uio_procp = p;
1874160ccd9SAlan Cox #ifdef KTRACE
1884160ccd9SAlan Cox 	/*
1894160ccd9SAlan Cox 	 * if tracing, save a copy of iovec
1904160ccd9SAlan Cox 	 */
19142ebfbf2SBrian Feldman 	if (KTRPOINT(p, KTR_GENIO)) {
1924160ccd9SAlan Cox 		ktriov = aiov;
19342ebfbf2SBrian Feldman 		ktruio = auio;
1943c89e357SBrian Feldman 		didktr = 1;
19542ebfbf2SBrian Feldman 	}
1964160ccd9SAlan Cox #endif
1978fe387abSDmitrij Tejblum 	cnt = nbyte;
198279d7226SMatthew Dillon 
199279d7226SMatthew Dillon 	if ((error = fo_read(fp, &auio, fp->f_cred, flags, p))) {
2004160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
2014160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
2024160ccd9SAlan Cox 			error = 0;
203279d7226SMatthew Dillon 	}
2044160ccd9SAlan Cox 	cnt -= auio.uio_resid;
2054160ccd9SAlan Cox #ifdef KTRACE
2063c89e357SBrian Feldman 	if (didktr && error == 0) {
20742ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
20842ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
20942ebfbf2SBrian Feldman 		ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
21042ebfbf2SBrian Feldman 	}
2114160ccd9SAlan Cox #endif
2124160ccd9SAlan Cox 	p->p_retval[0] = cnt;
2134160ccd9SAlan Cox 	return (error);
2144160ccd9SAlan Cox }
2154160ccd9SAlan Cox 
2164160ccd9SAlan Cox /*
217df8bae1dSRodney W. Grimes  * Scatter read system call.
218df8bae1dSRodney W. Grimes  */
219d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
220df8bae1dSRodney W. Grimes struct readv_args {
2217147b19dSBruce Evans 	int	fd;
222df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
223df8bae1dSRodney W. Grimes 	u_int	iovcnt;
224df8bae1dSRodney W. Grimes };
225d2d3e875SBruce Evans #endif
22626f9a767SRodney W. Grimes int
227cb226aaaSPoul-Henning Kamp readv(p, uap)
228df8bae1dSRodney W. Grimes 	struct proc *p;
229df8bae1dSRodney W. Grimes 	register struct readv_args *uap;
230df8bae1dSRodney W. Grimes {
231df8bae1dSRodney W. Grimes 	register struct file *fp;
232df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
233df8bae1dSRodney W. Grimes 	struct uio auio;
234df8bae1dSRodney W. Grimes 	register struct iovec *iov;
235df8bae1dSRodney W. Grimes 	struct iovec *needfree;
236df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
237df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
238df8bae1dSRodney W. Grimes 	u_int iovlen;
239df8bae1dSRodney W. Grimes #ifdef KTRACE
240df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
24142ebfbf2SBrian Feldman 	struct uio ktruio;
242df8bae1dSRodney W. Grimes #endif
243df8bae1dSRodney W. Grimes 
244279d7226SMatthew Dillon 	if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL)
245df8bae1dSRodney W. Grimes 		return (EBADF);
246df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
247df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
248df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
249df8bae1dSRodney W. Grimes 		if (uap->iovcnt > UIO_MAXIOV)
250df8bae1dSRodney W. Grimes 			return (EINVAL);
251df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
252df8bae1dSRodney W. Grimes 		needfree = iov;
253df8bae1dSRodney W. Grimes 	} else {
254df8bae1dSRodney W. Grimes 		iov = aiov;
255df8bae1dSRodney W. Grimes 		needfree = NULL;
256df8bae1dSRodney W. Grimes 	}
257df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
258df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
259df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
260df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
261df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
2622c1011f7SJohn Dyson 	auio.uio_offset = -1;
263bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
264df8bae1dSRodney W. Grimes 		goto done;
265df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
266df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
267069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
268df8bae1dSRodney W. Grimes 			error = EINVAL;
269df8bae1dSRodney W. Grimes 			goto done;
270df8bae1dSRodney W. Grimes 		}
271069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
272df8bae1dSRodney W. Grimes 		iov++;
273df8bae1dSRodney W. Grimes 	}
274df8bae1dSRodney W. Grimes #ifdef KTRACE
275df8bae1dSRodney W. Grimes 	/*
276df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
277df8bae1dSRodney W. Grimes 	 */
278df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
279df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
280df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
28142ebfbf2SBrian Feldman 		ktruio = auio;
282df8bae1dSRodney W. Grimes 	}
283df8bae1dSRodney W. Grimes #endif
284df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
285279d7226SMatthew Dillon 	if ((error = fo_read(fp, &auio, fp->f_cred, 0, p))) {
286df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
287df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
288df8bae1dSRodney W. Grimes 			error = 0;
289279d7226SMatthew Dillon 	}
290df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
291df8bae1dSRodney W. Grimes #ifdef KTRACE
292df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
29342ebfbf2SBrian Feldman 		if (error == 0) {
29442ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
29542ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
29642ebfbf2SBrian Feldman 			ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktruio,
29742ebfbf2SBrian Feldman 			    error);
29842ebfbf2SBrian Feldman 		}
299df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
300df8bae1dSRodney W. Grimes 	}
301df8bae1dSRodney W. Grimes #endif
302cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = cnt;
303df8bae1dSRodney W. Grimes done:
304279d7226SMatthew Dillon 	fdrop(fp, p);
305df8bae1dSRodney W. Grimes 	if (needfree)
306df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
307df8bae1dSRodney W. Grimes 	return (error);
308df8bae1dSRodney W. Grimes }
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes /*
311df8bae1dSRodney W. Grimes  * Write system call
312df8bae1dSRodney W. Grimes  */
313d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
314df8bae1dSRodney W. Grimes struct write_args {
315df8bae1dSRodney W. Grimes 	int	fd;
316134e06feSBruce Evans 	const void *buf;
317134e06feSBruce Evans 	size_t	nbyte;
318df8bae1dSRodney W. Grimes };
319d2d3e875SBruce Evans #endif
32026f9a767SRodney W. Grimes int
321cb226aaaSPoul-Henning Kamp write(p, uap)
322df8bae1dSRodney W. Grimes 	struct proc *p;
323df8bae1dSRodney W. Grimes 	register struct write_args *uap;
324df8bae1dSRodney W. Grimes {
325df8bae1dSRodney W. Grimes 	register struct file *fp;
326279d7226SMatthew Dillon 	int error;
327df8bae1dSRodney W. Grimes 
328279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
329df8bae1dSRodney W. Grimes 		return (EBADF);
330279d7226SMatthew Dillon 	error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
331279d7226SMatthew Dillon 	fdrop(fp, p);
332279d7226SMatthew Dillon 	return(error);
333df8bae1dSRodney W. Grimes }
334df8bae1dSRodney W. Grimes 
335df8bae1dSRodney W. Grimes /*
3368fe387abSDmitrij Tejblum  * Pwrite system call
3374160ccd9SAlan Cox  */
3384160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3394160ccd9SAlan Cox struct pwrite_args {
3404160ccd9SAlan Cox 	int	fd;
3414160ccd9SAlan Cox 	const void *buf;
3424160ccd9SAlan Cox 	size_t	nbyte;
3438fe387abSDmitrij Tejblum 	int	pad;
3444160ccd9SAlan Cox 	off_t	offset;
3454160ccd9SAlan Cox };
3464160ccd9SAlan Cox #endif
3474160ccd9SAlan Cox int
3484160ccd9SAlan Cox pwrite(p, uap)
3494160ccd9SAlan Cox 	struct proc *p;
3504160ccd9SAlan Cox 	register struct pwrite_args *uap;
3514160ccd9SAlan Cox {
3524160ccd9SAlan Cox 	register struct file *fp;
353279d7226SMatthew Dillon 	int error;
3548fe387abSDmitrij Tejblum 
355279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
3568fe387abSDmitrij Tejblum 		return (EBADF);
357279d7226SMatthew Dillon 	if (fp->f_type != DTYPE_VNODE) {
358279d7226SMatthew Dillon 		error = ESPIPE;
359279d7226SMatthew Dillon 	} else {
360279d7226SMatthew Dillon 	    error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte,
361279d7226SMatthew Dillon 		uap->offset, FOF_OFFSET);
362279d7226SMatthew Dillon 	}
363279d7226SMatthew Dillon 	fdrop(fp, p);
364279d7226SMatthew Dillon 	return(error);
3658fe387abSDmitrij Tejblum }
3668fe387abSDmitrij Tejblum 
3678fe387abSDmitrij Tejblum static int
3688fe387abSDmitrij Tejblum dofilewrite(p, fp, fd, buf, nbyte, offset, flags)
3698fe387abSDmitrij Tejblum 	struct proc *p;
3708fe387abSDmitrij Tejblum 	struct file *fp;
3718fe387abSDmitrij Tejblum 	int fd, flags;
3728fe387abSDmitrij Tejblum 	const void *buf;
3738fe387abSDmitrij Tejblum 	size_t nbyte;
3748fe387abSDmitrij Tejblum 	off_t offset;
3758fe387abSDmitrij Tejblum {
3764160ccd9SAlan Cox 	struct uio auio;
3774160ccd9SAlan Cox 	struct iovec aiov;
3784160ccd9SAlan Cox 	long cnt, error = 0;
3794160ccd9SAlan Cox #ifdef KTRACE
3804160ccd9SAlan Cox 	struct iovec ktriov;
38142ebfbf2SBrian Feldman 	struct uio ktruio;
3823c89e357SBrian Feldman 	int didktr = 0;
3834160ccd9SAlan Cox #endif
3844160ccd9SAlan Cox 
385b31ae1adSPeter Wemm 	aiov.iov_base = (void *)(uintptr_t)buf;
3868fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
3874160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3884160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
3898fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
3908fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
3914160ccd9SAlan Cox 		return (EINVAL);
3928fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
3934160ccd9SAlan Cox 	auio.uio_rw = UIO_WRITE;
3944160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
3954160ccd9SAlan Cox 	auio.uio_procp = p;
3964160ccd9SAlan Cox #ifdef KTRACE
3974160ccd9SAlan Cox 	/*
39842ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
3994160ccd9SAlan Cox 	 */
40042ebfbf2SBrian Feldman 	if (KTRPOINT(p, KTR_GENIO)) {
4014160ccd9SAlan Cox 		ktriov = aiov;
40242ebfbf2SBrian Feldman 		ktruio = auio;
4033c89e357SBrian Feldman 		didktr = 1;
40442ebfbf2SBrian Feldman 	}
4054160ccd9SAlan Cox #endif
4068fe387abSDmitrij Tejblum 	cnt = nbyte;
407c6ab5768SAlfred Perlstein 	if (fp->f_type == DTYPE_VNODE)
408279d7226SMatthew Dillon 		bwillwrite();
40913ccadd4SBrian Feldman 	if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
4104160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
4114160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
4124160ccd9SAlan Cox 			error = 0;
4134160ccd9SAlan Cox 		if (error == EPIPE)
4144160ccd9SAlan Cox 			psignal(p, SIGPIPE);
4154160ccd9SAlan Cox 	}
4164160ccd9SAlan Cox 	cnt -= auio.uio_resid;
4174160ccd9SAlan Cox #ifdef KTRACE
4183c89e357SBrian Feldman 	if (didktr && error == 0) {
41942ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
42042ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
42142ebfbf2SBrian Feldman 		ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
42242ebfbf2SBrian Feldman 	}
4234160ccd9SAlan Cox #endif
4244160ccd9SAlan Cox 	p->p_retval[0] = cnt;
4254160ccd9SAlan Cox 	return (error);
4264160ccd9SAlan Cox }
4274160ccd9SAlan Cox 
4284160ccd9SAlan Cox /*
429df8bae1dSRodney W. Grimes  * Gather write system call
430df8bae1dSRodney W. Grimes  */
431d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
432df8bae1dSRodney W. Grimes struct writev_args {
433df8bae1dSRodney W. Grimes 	int	fd;
434df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
435df8bae1dSRodney W. Grimes 	u_int	iovcnt;
436df8bae1dSRodney W. Grimes };
437d2d3e875SBruce Evans #endif
43826f9a767SRodney W. Grimes int
439cb226aaaSPoul-Henning Kamp writev(p, uap)
440df8bae1dSRodney W. Grimes 	struct proc *p;
441df8bae1dSRodney W. Grimes 	register struct writev_args *uap;
442df8bae1dSRodney W. Grimes {
443df8bae1dSRodney W. Grimes 	register struct file *fp;
444df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
445df8bae1dSRodney W. Grimes 	struct uio auio;
446df8bae1dSRodney W. Grimes 	register struct iovec *iov;
447df8bae1dSRodney W. Grimes 	struct iovec *needfree;
448df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
449df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
450df8bae1dSRodney W. Grimes 	u_int iovlen;
451df8bae1dSRodney W. Grimes #ifdef KTRACE
452df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
45342ebfbf2SBrian Feldman 	struct uio ktruio;
454df8bae1dSRodney W. Grimes #endif
455df8bae1dSRodney W. Grimes 
456279d7226SMatthew Dillon 	if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
457df8bae1dSRodney W. Grimes 		return (EBADF);
458df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
459df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
460df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
4611aa3e7ddSBrian Feldman 		if (uap->iovcnt > UIO_MAXIOV) {
4621aa3e7ddSBrian Feldman 			needfree = NULL;
4631aa3e7ddSBrian Feldman 			error = EINVAL;
4641aa3e7ddSBrian Feldman 			goto done;
4651aa3e7ddSBrian Feldman 		}
466df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
467df8bae1dSRodney W. Grimes 		needfree = iov;
468df8bae1dSRodney W. Grimes 	} else {
469df8bae1dSRodney W. Grimes 		iov = aiov;
470df8bae1dSRodney W. Grimes 		needfree = NULL;
471df8bae1dSRodney W. Grimes 	}
472df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
473df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
474df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
475df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
476df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
4772c1011f7SJohn Dyson 	auio.uio_offset = -1;
478bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
479df8bae1dSRodney W. Grimes 		goto done;
480df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
481df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
482069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
483df8bae1dSRodney W. Grimes 			error = EINVAL;
484df8bae1dSRodney W. Grimes 			goto done;
485df8bae1dSRodney W. Grimes 		}
486069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
487df8bae1dSRodney W. Grimes 		iov++;
488df8bae1dSRodney W. Grimes 	}
489df8bae1dSRodney W. Grimes #ifdef KTRACE
490df8bae1dSRodney W. Grimes 	/*
49142ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
492df8bae1dSRodney W. Grimes 	 */
493df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
494df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
495df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
49642ebfbf2SBrian Feldman 		ktruio = auio;
497df8bae1dSRodney W. Grimes 	}
498df8bae1dSRodney W. Grimes #endif
499df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
500a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5019440653dSMatthew Dillon 		bwillwrite();
50213ccadd4SBrian Feldman 	if ((error = fo_write(fp, &auio, fp->f_cred, 0, p))) {
503df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
504df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
505df8bae1dSRodney W. Grimes 			error = 0;
506df8bae1dSRodney W. Grimes 		if (error == EPIPE)
507df8bae1dSRodney W. Grimes 			psignal(p, SIGPIPE);
508df8bae1dSRodney W. Grimes 	}
509df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
510df8bae1dSRodney W. Grimes #ifdef KTRACE
511df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
51242ebfbf2SBrian Feldman 		if (error == 0) {
51342ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
51442ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
51542ebfbf2SBrian Feldman 			ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, &ktruio,
51642ebfbf2SBrian Feldman 			    error);
51742ebfbf2SBrian Feldman 		}
518df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
519df8bae1dSRodney W. Grimes 	}
520df8bae1dSRodney W. Grimes #endif
521cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = cnt;
522df8bae1dSRodney W. Grimes done:
523d8177437SBrian Feldman 	fdrop(fp, p);
524df8bae1dSRodney W. Grimes 	if (needfree)
525df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
526df8bae1dSRodney W. Grimes 	return (error);
527df8bae1dSRodney W. Grimes }
528df8bae1dSRodney W. Grimes 
529df8bae1dSRodney W. Grimes /*
530df8bae1dSRodney W. Grimes  * Ioctl system call
531df8bae1dSRodney W. Grimes  */
532d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
533df8bae1dSRodney W. Grimes struct ioctl_args {
534df8bae1dSRodney W. Grimes 	int	fd;
535069e9bc1SDoug Rabson 	u_long	com;
536df8bae1dSRodney W. Grimes 	caddr_t	data;
537df8bae1dSRodney W. Grimes };
538d2d3e875SBruce Evans #endif
539df8bae1dSRodney W. Grimes /* ARGSUSED */
54026f9a767SRodney W. Grimes int
541cb226aaaSPoul-Henning Kamp ioctl(p, uap)
542df8bae1dSRodney W. Grimes 	struct proc *p;
543df8bae1dSRodney W. Grimes 	register struct ioctl_args *uap;
544df8bae1dSRodney W. Grimes {
545df8bae1dSRodney W. Grimes 	register struct file *fp;
546df8bae1dSRodney W. Grimes 	register struct filedesc *fdp;
547831b9ef2SDoug Rabson 	register u_long com;
548831b9ef2SDoug Rabson 	int error;
549df8bae1dSRodney W. Grimes 	register u_int size;
550df8bae1dSRodney W. Grimes 	caddr_t data, memp;
551df8bae1dSRodney W. Grimes 	int tmp;
552df8bae1dSRodney W. Grimes #define STK_PARAMS	128
553d2ba455cSMatthew Dillon 	union {
554df8bae1dSRodney W. Grimes 	    char stkbuf[STK_PARAMS];
555d2ba455cSMatthew Dillon 	    long align;
556d2ba455cSMatthew Dillon 	} ubuf;
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 	fdp = p->p_fd;
559df8bae1dSRodney W. Grimes 	if ((u_int)uap->fd >= fdp->fd_nfiles ||
560df8bae1dSRodney W. Grimes 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
561df8bae1dSRodney W. Grimes 		return (EBADF);
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
564df8bae1dSRodney W. Grimes 		return (EBADF);
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
567df8bae1dSRodney W. Grimes 	case FIONCLEX:
568df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
569df8bae1dSRodney W. Grimes 		return (0);
570df8bae1dSRodney W. Grimes 	case FIOCLEX:
571df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
572df8bae1dSRodney W. Grimes 		return (0);
573df8bae1dSRodney W. Grimes 	}
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes 	/*
576df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
577df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
578df8bae1dSRodney W. Grimes 	 */
579df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
580df8bae1dSRodney W. Grimes 	if (size > IOCPARM_MAX)
581df8bae1dSRodney W. Grimes 		return (ENOTTY);
582279d7226SMatthew Dillon 
583279d7226SMatthew Dillon 	fhold(fp);
584279d7226SMatthew Dillon 
585df8bae1dSRodney W. Grimes 	memp = NULL;
586d2ba455cSMatthew Dillon 	if (size > sizeof (ubuf.stkbuf)) {
587df8bae1dSRodney W. Grimes 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
588df8bae1dSRodney W. Grimes 		data = memp;
589279d7226SMatthew Dillon 	} else {
590d2ba455cSMatthew Dillon 		data = ubuf.stkbuf;
591279d7226SMatthew Dillon 	}
592df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
593df8bae1dSRodney W. Grimes 		if (size) {
594df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
595df8bae1dSRodney W. Grimes 			if (error) {
596df8bae1dSRodney W. Grimes 				if (memp)
597df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
598279d7226SMatthew Dillon 				fdrop(fp, p);
599df8bae1dSRodney W. Grimes 				return (error);
600df8bae1dSRodney W. Grimes 			}
601279d7226SMatthew Dillon 		} else {
602df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
603279d7226SMatthew Dillon 		}
604279d7226SMatthew Dillon 	} else if ((com&IOC_OUT) && size) {
605df8bae1dSRodney W. Grimes 		/*
606df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
607df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
608df8bae1dSRodney W. Grimes 		 */
609df8bae1dSRodney W. Grimes 		bzero(data, size);
610279d7226SMatthew Dillon 	} else if (com&IOC_VOID) {
611df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
612279d7226SMatthew Dillon 	}
613df8bae1dSRodney W. Grimes 
614df8bae1dSRodney W. Grimes 	switch (com) {
615df8bae1dSRodney W. Grimes 
616df8bae1dSRodney W. Grimes 	case FIONBIO:
617bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
618df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
619df8bae1dSRodney W. Grimes 		else
620df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
62113ccadd4SBrian Feldman 		error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
622df8bae1dSRodney W. Grimes 		break;
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes 	case FIOASYNC:
625bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
626df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
627df8bae1dSRodney W. Grimes 		else
628df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
62913ccadd4SBrian Feldman 		error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
630df8bae1dSRodney W. Grimes 		break;
631df8bae1dSRodney W. Grimes 
632df8bae1dSRodney W. Grimes 	default:
63313ccadd4SBrian Feldman 		error = fo_ioctl(fp, com, data, p);
634df8bae1dSRodney W. Grimes 		/*
635df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
636df8bae1dSRodney W. Grimes 		 * already set and checked above.
637df8bae1dSRodney W. Grimes 		 */
638df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
639df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
640df8bae1dSRodney W. Grimes 		break;
641df8bae1dSRodney W. Grimes 	}
642df8bae1dSRodney W. Grimes 	if (memp)
643df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
644279d7226SMatthew Dillon 	fdrop(fp, p);
645df8bae1dSRodney W. Grimes 	return (error);
646df8bae1dSRodney W. Grimes }
647df8bae1dSRodney W. Grimes 
6488cb96f20SPeter Wemm static int	nselcoll;	/* Select collisions since boot */
64987b6de2bSPoul-Henning Kamp int	selwait;
6508cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
651df8bae1dSRodney W. Grimes 
652df8bae1dSRodney W. Grimes /*
653df8bae1dSRodney W. Grimes  * Select system call.
654df8bae1dSRodney W. Grimes  */
655d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
656df8bae1dSRodney W. Grimes struct select_args {
657b08f7993SSujal Patel 	int	nd;
658df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
659df8bae1dSRodney W. Grimes 	struct	timeval *tv;
660df8bae1dSRodney W. Grimes };
661d2d3e875SBruce Evans #endif
66226f9a767SRodney W. Grimes int
663cb226aaaSPoul-Henning Kamp select(p, uap)
664df8bae1dSRodney W. Grimes 	register struct proc *p;
665df8bae1dSRodney W. Grimes 	register struct select_args *uap;
666df8bae1dSRodney W. Grimes {
667d5e4d7e1SBruce Evans 	/*
668d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
669d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
670d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
671d5e4d7e1SBruce Evans 	 * of 256.
672d5e4d7e1SBruce Evans 	 */
673d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
674d5e4d7e1SBruce Evans 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
67500af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
67600af9731SPoul-Henning Kamp 	int s, ncoll, error, timo;
677d5e4d7e1SBruce Evans 	u_int nbufbytes, ncpbytes, nfdbits;
678df8bae1dSRodney W. Grimes 
679b08f7993SSujal Patel 	if (uap->nd < 0)
680acbfbfeaSSujal Patel 		return (EINVAL);
681df8bae1dSRodney W. Grimes 	if (uap->nd > p->p_fd->fd_nfiles)
682df8bae1dSRodney W. Grimes 		uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
683b08f7993SSujal Patel 
684d5e4d7e1SBruce Evans 	/*
685d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
686d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
687d5e4d7e1SBruce Evans 	 */
688d5e4d7e1SBruce Evans 	nfdbits = roundup(uap->nd, NFDBITS);
689d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
690d5e4d7e1SBruce Evans 	nbufbytes = 0;
691d5e4d7e1SBruce Evans 	if (uap->in != NULL)
692d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
693d5e4d7e1SBruce Evans 	if (uap->ou != NULL)
694d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
695d5e4d7e1SBruce Evans 	if (uap->ex != NULL)
696d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
697d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
698d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
699d5e4d7e1SBruce Evans 	else
700d5e4d7e1SBruce Evans 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
701b08f7993SSujal Patel 
702b08f7993SSujal Patel 	/*
703d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
704d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
705d5e4d7e1SBruce Evans 	 * together.
706b08f7993SSujal Patel 	 */
707d5e4d7e1SBruce Evans 	sbp = selbits;
708df8bae1dSRodney W. Grimes #define	getbits(name, x) \
709d5e4d7e1SBruce Evans 	do {								\
710d5e4d7e1SBruce Evans 		if (uap->name == NULL)					\
711d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
712d5e4d7e1SBruce Evans 		else {							\
713d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
714d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
715d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
716d5e4d7e1SBruce Evans 			error = copyin(uap->name, ibits[x], ncpbytes);	\
717d5e4d7e1SBruce Evans 			if (error != 0)					\
718d5e4d7e1SBruce Evans 				goto done;				\
719d5e4d7e1SBruce Evans 		}							\
720d5e4d7e1SBruce Evans 	} while (0)
721df8bae1dSRodney W. Grimes 	getbits(in, 0);
722df8bae1dSRodney W. Grimes 	getbits(ou, 1);
723df8bae1dSRodney W. Grimes 	getbits(ex, 2);
724df8bae1dSRodney W. Grimes #undef	getbits
725d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
726d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes 	if (uap->tv) {
729df8bae1dSRodney W. Grimes 		error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
730df8bae1dSRodney W. Grimes 			sizeof (atv));
731df8bae1dSRodney W. Grimes 		if (error)
732df8bae1dSRodney W. Grimes 			goto done;
733df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
734df8bae1dSRodney W. Grimes 			error = EINVAL;
735df8bae1dSRodney W. Grimes 			goto done;
736df8bae1dSRodney W. Grimes 		}
737c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
73800af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
7399c386f6bSJohn Baldwin 	} else {
74000af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
7419c386f6bSJohn Baldwin 		atv.tv_usec = 0;
7429c386f6bSJohn Baldwin 	}
74300af9731SPoul-Henning Kamp 	timo = 0;
744df8bae1dSRodney W. Grimes retry:
745df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
746df8bae1dSRodney W. Grimes 	p->p_flag |= P_SELECT;
747cb226aaaSPoul-Henning Kamp 	error = selscan(p, ibits, obits, uap->nd);
748cb226aaaSPoul-Henning Kamp 	if (error || p->p_retval[0])
749df8bae1dSRodney W. Grimes 		goto done;
7504da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
751c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
75200af9731SPoul-Henning Kamp 		if (timevalcmp(&rtv, &atv, >=))
753df8bae1dSRodney W. Grimes 			goto done;
75400af9731SPoul-Henning Kamp 		ttv = atv;
75500af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
75600af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
75700af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
758df8bae1dSRodney W. Grimes 	}
75900af9731SPoul-Henning Kamp 	s = splhigh();
760df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
761df8bae1dSRodney W. Grimes 		splx(s);
762df8bae1dSRodney W. Grimes 		goto retry;
763df8bae1dSRodney W. Grimes 	}
764df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
765bfbbc4aaSJason Evans 
766df8bae1dSRodney W. Grimes 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
767bfbbc4aaSJason Evans 
768df8bae1dSRodney W. Grimes 	splx(s);
769df8bae1dSRodney W. Grimes 	if (error == 0)
770df8bae1dSRodney W. Grimes 		goto retry;
771df8bae1dSRodney W. Grimes done:
772df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
773df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
774df8bae1dSRodney W. Grimes 	if (error == ERESTART)
775df8bae1dSRodney W. Grimes 		error = EINTR;
776df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
777df8bae1dSRodney W. Grimes 		error = 0;
778df8bae1dSRodney W. Grimes #define	putbits(name, x) \
779d5e4d7e1SBruce Evans 	if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
780df8bae1dSRodney W. Grimes 		error = error2;
781df8bae1dSRodney W. Grimes 	if (error == 0) {
782df8bae1dSRodney W. Grimes 		int error2;
783df8bae1dSRodney W. Grimes 
784df8bae1dSRodney W. Grimes 		putbits(in, 0);
785df8bae1dSRodney W. Grimes 		putbits(ou, 1);
786df8bae1dSRodney W. Grimes 		putbits(ex, 2);
787df8bae1dSRodney W. Grimes #undef putbits
788df8bae1dSRodney W. Grimes 	}
789d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
790d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
791df8bae1dSRodney W. Grimes 	return (error);
792df8bae1dSRodney W. Grimes }
793df8bae1dSRodney W. Grimes 
79487b6de2bSPoul-Henning Kamp static int
795cb226aaaSPoul-Henning Kamp selscan(p, ibits, obits, nfd)
796df8bae1dSRodney W. Grimes 	struct proc *p;
797b08f7993SSujal Patel 	fd_mask **ibits, **obits;
798cb226aaaSPoul-Henning Kamp 	int nfd;
799df8bae1dSRodney W. Grimes {
800f082218cSPeter Wemm 	struct filedesc *fdp = p->p_fd;
801f082218cSPeter Wemm 	int msk, i, fd;
802f082218cSPeter Wemm 	fd_mask bits;
803df8bae1dSRodney W. Grimes 	struct file *fp;
804df8bae1dSRodney W. Grimes 	int n = 0;
8052087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
80642d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
809d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
810d5e4d7e1SBruce Evans 			continue;
811df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
812b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
813f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
814f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
815f082218cSPeter Wemm 				if (!(bits & 1))
816f082218cSPeter Wemm 					continue;
817df8bae1dSRodney W. Grimes 				fp = fdp->fd_ofiles[fd];
818df8bae1dSRodney W. Grimes 				if (fp == NULL)
819df8bae1dSRodney W. Grimes 					return (EBADF);
82013ccadd4SBrian Feldman 				if (fo_poll(fp, flag[msk], fp->f_cred, p)) {
821b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
822f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
823df8bae1dSRodney W. Grimes 					n++;
824df8bae1dSRodney W. Grimes 				}
825df8bae1dSRodney W. Grimes 			}
826df8bae1dSRodney W. Grimes 		}
827df8bae1dSRodney W. Grimes 	}
828cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = n;
829df8bae1dSRodney W. Grimes 	return (0);
830df8bae1dSRodney W. Grimes }
831df8bae1dSRodney W. Grimes 
83242d11757SPeter Wemm /*
83342d11757SPeter Wemm  * Poll system call.
83442d11757SPeter Wemm  */
83542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
83642d11757SPeter Wemm struct poll_args {
83742d11757SPeter Wemm 	struct pollfd *fds;
83842d11757SPeter Wemm 	u_int	nfds;
83942d11757SPeter Wemm 	int	timeout;
84042d11757SPeter Wemm };
84142d11757SPeter Wemm #endif
84242d11757SPeter Wemm int
843cb226aaaSPoul-Henning Kamp poll(p, uap)
84442d11757SPeter Wemm 	register struct proc *p;
84542d11757SPeter Wemm 	register struct poll_args *uap;
84642d11757SPeter Wemm {
84742d11757SPeter Wemm 	caddr_t bits;
84842d11757SPeter Wemm 	char smallbits[32 * sizeof(struct pollfd)];
84900af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
85000af9731SPoul-Henning Kamp 	int s, ncoll, error = 0, timo;
85142d11757SPeter Wemm 	size_t ni;
85242d11757SPeter Wemm 
85342d11757SPeter Wemm 	if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
85442d11757SPeter Wemm 		/* forgiving; slightly wrong */
85542d11757SPeter Wemm 		SCARG(uap, nfds) = p->p_fd->fd_nfiles;
85642d11757SPeter Wemm 	}
85742d11757SPeter Wemm 	ni = SCARG(uap, nfds) * sizeof(struct pollfd);
85842d11757SPeter Wemm 	if (ni > sizeof(smallbits))
85942d11757SPeter Wemm 		bits = malloc(ni, M_TEMP, M_WAITOK);
86042d11757SPeter Wemm 	else
86142d11757SPeter Wemm 		bits = smallbits;
86242d11757SPeter Wemm 	error = copyin(SCARG(uap, fds), bits, ni);
86342d11757SPeter Wemm 	if (error)
86442d11757SPeter Wemm 		goto done;
86542d11757SPeter Wemm 	if (SCARG(uap, timeout) != INFTIM) {
86642d11757SPeter Wemm 		atv.tv_sec = SCARG(uap, timeout) / 1000;
86742d11757SPeter Wemm 		atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
86842d11757SPeter Wemm 		if (itimerfix(&atv)) {
86942d11757SPeter Wemm 			error = EINVAL;
87042d11757SPeter Wemm 			goto done;
87142d11757SPeter Wemm 		}
872c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
87300af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
8749c386f6bSJohn Baldwin 	} else {
87500af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
8769c386f6bSJohn Baldwin 		atv.tv_usec = 0;
8779c386f6bSJohn Baldwin 	}
87800af9731SPoul-Henning Kamp 	timo = 0;
87942d11757SPeter Wemm retry:
88042d11757SPeter Wemm 	ncoll = nselcoll;
88142d11757SPeter Wemm 	p->p_flag |= P_SELECT;
882cb226aaaSPoul-Henning Kamp 	error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds));
883cb226aaaSPoul-Henning Kamp 	if (error || p->p_retval[0])
88442d11757SPeter Wemm 		goto done;
8854da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
886c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
88700af9731SPoul-Henning Kamp 		if (timevalcmp(&rtv, &atv, >=))
88842d11757SPeter Wemm 			goto done;
88900af9731SPoul-Henning Kamp 		ttv = atv;
89000af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
89100af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
89200af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
89342d11757SPeter Wemm 	}
89400af9731SPoul-Henning Kamp 	s = splhigh();
89542d11757SPeter Wemm 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
89642d11757SPeter Wemm 		splx(s);
89742d11757SPeter Wemm 		goto retry;
89842d11757SPeter Wemm 	}
89942d11757SPeter Wemm 	p->p_flag &= ~P_SELECT;
90042d11757SPeter Wemm 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
90142d11757SPeter Wemm 	splx(s);
90242d11757SPeter Wemm 	if (error == 0)
90342d11757SPeter Wemm 		goto retry;
90442d11757SPeter Wemm done:
90542d11757SPeter Wemm 	p->p_flag &= ~P_SELECT;
90642d11757SPeter Wemm 	/* poll is not restarted after signals... */
90742d11757SPeter Wemm 	if (error == ERESTART)
90842d11757SPeter Wemm 		error = EINTR;
90942d11757SPeter Wemm 	if (error == EWOULDBLOCK)
91042d11757SPeter Wemm 		error = 0;
91142d11757SPeter Wemm 	if (error == 0) {
91242d11757SPeter Wemm 		error = copyout(bits, SCARG(uap, fds), ni);
91342d11757SPeter Wemm 		if (error)
91442d11757SPeter Wemm 			goto out;
91542d11757SPeter Wemm 	}
91642d11757SPeter Wemm out:
91742d11757SPeter Wemm 	if (ni > sizeof(smallbits))
91842d11757SPeter Wemm 		free(bits, M_TEMP);
91942d11757SPeter Wemm 	return (error);
92042d11757SPeter Wemm }
92142d11757SPeter Wemm 
92242d11757SPeter Wemm static int
923cb226aaaSPoul-Henning Kamp pollscan(p, fds, nfd)
92442d11757SPeter Wemm 	struct proc *p;
92542d11757SPeter Wemm 	struct pollfd *fds;
92642d11757SPeter Wemm 	int nfd;
92742d11757SPeter Wemm {
92842d11757SPeter Wemm 	register struct filedesc *fdp = p->p_fd;
92942d11757SPeter Wemm 	int i;
93042d11757SPeter Wemm 	struct file *fp;
93142d11757SPeter Wemm 	int n = 0;
93242d11757SPeter Wemm 
93342d11757SPeter Wemm 	for (i = 0; i < nfd; i++, fds++) {
934337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
93542d11757SPeter Wemm 			fds->revents = POLLNVAL;
93642d11757SPeter Wemm 			n++;
937337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
938337c9691SJordan K. Hubbard 			fds->revents = 0;
93942d11757SPeter Wemm 		} else {
94042d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
941279d7226SMatthew Dillon 			if (fp == NULL) {
94242d11757SPeter Wemm 				fds->revents = POLLNVAL;
94342d11757SPeter Wemm 				n++;
94442d11757SPeter Wemm 			} else {
9452087c896SBruce Evans 				/*
9462087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
9472087c896SBruce Evans 				 * POLLERR if appropriate.
9482087c896SBruce Evans 				 */
94913ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
95013ccadd4SBrian Feldman 				    fp->f_cred, p);
95142d11757SPeter Wemm 				if (fds->revents != 0)
95242d11757SPeter Wemm 					n++;
95342d11757SPeter Wemm 			}
95442d11757SPeter Wemm 		}
95542d11757SPeter Wemm 	}
956cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = n;
95742d11757SPeter Wemm 	return (0);
95842d11757SPeter Wemm }
95942d11757SPeter Wemm 
96042d11757SPeter Wemm /*
96142d11757SPeter Wemm  * OpenBSD poll system call.
96242d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
96342d11757SPeter Wemm  */
96442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
96542d11757SPeter Wemm struct openbsd_poll_args {
96642d11757SPeter Wemm 	struct pollfd *fds;
96742d11757SPeter Wemm 	u_int	nfds;
96842d11757SPeter Wemm 	int	timeout;
96942d11757SPeter Wemm };
97042d11757SPeter Wemm #endif
97142d11757SPeter Wemm int
972cb226aaaSPoul-Henning Kamp openbsd_poll(p, uap)
97342d11757SPeter Wemm 	register struct proc *p;
97442d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
97542d11757SPeter Wemm {
976cb226aaaSPoul-Henning Kamp 	return (poll(p, (struct poll_args *)uap));
97742d11757SPeter Wemm }
97842d11757SPeter Wemm 
979df8bae1dSRodney W. Grimes /*ARGSUSED*/
98026f9a767SRodney W. Grimes int
98142d11757SPeter Wemm seltrue(dev, events, p)
982df8bae1dSRodney W. Grimes 	dev_t dev;
98342d11757SPeter Wemm 	int events;
984df8bae1dSRodney W. Grimes 	struct proc *p;
985df8bae1dSRodney W. Grimes {
986df8bae1dSRodney W. Grimes 
98742d11757SPeter Wemm 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
988df8bae1dSRodney W. Grimes }
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes /*
991df8bae1dSRodney W. Grimes  * Record a select request.
992df8bae1dSRodney W. Grimes  */
993df8bae1dSRodney W. Grimes void
994df8bae1dSRodney W. Grimes selrecord(selector, sip)
995df8bae1dSRodney W. Grimes 	struct proc *selector;
996df8bae1dSRodney W. Grimes 	struct selinfo *sip;
997df8bae1dSRodney W. Grimes {
998df8bae1dSRodney W. Grimes 	struct proc *p;
999df8bae1dSRodney W. Grimes 	pid_t mypid;
1000df8bae1dSRodney W. Grimes 
1001df8bae1dSRodney W. Grimes 	mypid = selector->p_pid;
1002df8bae1dSRodney W. Grimes 	if (sip->si_pid == mypid)
1003df8bae1dSRodney W. Grimes 		return;
1004df8bae1dSRodney W. Grimes 	if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1005df8bae1dSRodney W. Grimes 	    p->p_wchan == (caddr_t)&selwait)
1006df8bae1dSRodney W. Grimes 		sip->si_flags |= SI_COLL;
1007df8bae1dSRodney W. Grimes 	else
1008df8bae1dSRodney W. Grimes 		sip->si_pid = mypid;
1009df8bae1dSRodney W. Grimes }
1010df8bae1dSRodney W. Grimes 
1011df8bae1dSRodney W. Grimes /*
1012df8bae1dSRodney W. Grimes  * Do a wakeup when a selectable event occurs.
1013df8bae1dSRodney W. Grimes  */
1014df8bae1dSRodney W. Grimes void
1015df8bae1dSRodney W. Grimes selwakeup(sip)
1016df8bae1dSRodney W. Grimes 	register struct selinfo *sip;
1017df8bae1dSRodney W. Grimes {
1018df8bae1dSRodney W. Grimes 	register struct proc *p;
1019df8bae1dSRodney W. Grimes 	int s;
1020df8bae1dSRodney W. Grimes 
1021df8bae1dSRodney W. Grimes 	if (sip->si_pid == 0)
1022df8bae1dSRodney W. Grimes 		return;
1023df8bae1dSRodney W. Grimes 	if (sip->si_flags & SI_COLL) {
1024df8bae1dSRodney W. Grimes 		nselcoll++;
1025df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1026df8bae1dSRodney W. Grimes 		wakeup((caddr_t)&selwait);
1027df8bae1dSRodney W. Grimes 	}
1028df8bae1dSRodney W. Grimes 	p = pfind(sip->si_pid);
1029df8bae1dSRodney W. Grimes 	sip->si_pid = 0;
1030df8bae1dSRodney W. Grimes 	if (p != NULL) {
1031df8bae1dSRodney W. Grimes 		s = splhigh();
10324a476efaSJonathan Lemon 		mtx_enter(&sched_lock, MTX_SPIN);
1033df8bae1dSRodney W. Grimes 		if (p->p_wchan == (caddr_t)&selwait) {
1034df8bae1dSRodney W. Grimes 			if (p->p_stat == SSLEEP)
1035df8bae1dSRodney W. Grimes 				setrunnable(p);
1036df8bae1dSRodney W. Grimes 			else
1037df8bae1dSRodney W. Grimes 				unsleep(p);
1038df8bae1dSRodney W. Grimes 		} else if (p->p_flag & P_SELECT)
1039df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_SELECT;
10404a476efaSJonathan Lemon 		mtx_exit(&sched_lock, MTX_SPIN);
1041df8bae1dSRodney W. Grimes 		splx(s);
1042df8bae1dSRodney W. Grimes 	}
1043df8bae1dSRodney W. Grimes }
1044