xref: /freebsd/sys/kern/sys_generic.c (revision 279d7226048c39b48b788e5ea5607c0dbc042acd)
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>
588cb96f20SPeter Wemm #include <sys/sysctl.h>
5942d11757SPeter Wemm #include <sys/sysent.h>
60279d7226SMatthew Dillon #include <sys/bio.h>
61279d7226SMatthew Dillon #include <sys/buf.h>
62df8bae1dSRodney W. Grimes #ifdef KTRACE
63df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
64df8bae1dSRodney W. Grimes #endif
65279d7226SMatthew Dillon #include <vm/vm.h>
66279d7226SMatthew Dillon #include <vm/vm_page.h>
67df8bae1dSRodney W. Grimes 
68069e9bc1SDoug Rabson #include <machine/limits.h>
69069e9bc1SDoug Rabson 
70a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
71a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
72a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7355166637SPoul-Henning Kamp 
74cb226aaaSPoul-Henning Kamp static int	pollscan __P((struct proc *, struct pollfd *, int));
752087c896SBruce Evans static int	selscan __P((struct proc *, fd_mask **, fd_mask **, int));
768fe387abSDmitrij Tejblum static int	dofileread __P((struct proc *, struct file *, int, void *,
778fe387abSDmitrij Tejblum 		    size_t, off_t, int));
788fe387abSDmitrij Tejblum static int	dofilewrite __P((struct proc *, struct file *, int,
798fe387abSDmitrij Tejblum 		    const void *, size_t, off_t, int));
808fe387abSDmitrij Tejblum 
818757e5bbSAlfred Perlstein struct file*
82279d7226SMatthew Dillon holdfp(fdp, fd, flag)
838fe387abSDmitrij Tejblum 	struct filedesc* fdp;
848fe387abSDmitrij Tejblum 	int fd, flag;
858fe387abSDmitrij Tejblum {
868fe387abSDmitrij Tejblum 	struct file* fp;
878fe387abSDmitrij Tejblum 
888fe387abSDmitrij Tejblum 	if (((u_int)fd) >= fdp->fd_nfiles ||
898fe387abSDmitrij Tejblum 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
90279d7226SMatthew Dillon 	    (fp->f_flag & flag) == 0) {
918fe387abSDmitrij Tejblum 		return (NULL);
92279d7226SMatthew Dillon 	}
93279d7226SMatthew Dillon 	fhold(fp);
948fe387abSDmitrij Tejblum 	return (fp);
958fe387abSDmitrij Tejblum }
96d93f860cSPoul-Henning Kamp 
97df8bae1dSRodney W. Grimes /*
98df8bae1dSRodney W. Grimes  * Read system call.
99df8bae1dSRodney W. Grimes  */
100d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
101df8bae1dSRodney W. Grimes struct read_args {
102df8bae1dSRodney W. Grimes 	int	fd;
103134e06feSBruce Evans 	void	*buf;
104134e06feSBruce Evans 	size_t	nbyte;
105df8bae1dSRodney W. Grimes };
106d2d3e875SBruce Evans #endif
10726f9a767SRodney W. Grimes int
108cb226aaaSPoul-Henning Kamp read(p, uap)
109df8bae1dSRodney W. Grimes 	struct proc *p;
110df8bae1dSRodney W. Grimes 	register struct read_args *uap;
111df8bae1dSRodney W. Grimes {
112df8bae1dSRodney W. Grimes 	register struct file *fp;
113279d7226SMatthew Dillon 	int error;
114df8bae1dSRodney W. Grimes 
115279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
116df8bae1dSRodney W. Grimes 		return (EBADF);
117279d7226SMatthew Dillon 	error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
118279d7226SMatthew Dillon 	fdrop(fp, p);
119279d7226SMatthew Dillon 	return(error);
120df8bae1dSRodney W. Grimes }
121df8bae1dSRodney W. Grimes 
122df8bae1dSRodney W. Grimes /*
1238fe387abSDmitrij Tejblum  * Pread system call
1244160ccd9SAlan Cox  */
1254160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1264160ccd9SAlan Cox struct pread_args {
1274160ccd9SAlan Cox 	int	fd;
1284160ccd9SAlan Cox 	void	*buf;
1294160ccd9SAlan Cox 	size_t	nbyte;
1308fe387abSDmitrij Tejblum 	int	pad;
1314160ccd9SAlan Cox 	off_t	offset;
1324160ccd9SAlan Cox };
1334160ccd9SAlan Cox #endif
1344160ccd9SAlan Cox int
1354160ccd9SAlan Cox pread(p, uap)
1364160ccd9SAlan Cox 	struct proc *p;
1374160ccd9SAlan Cox 	register struct pread_args *uap;
1384160ccd9SAlan Cox {
1394160ccd9SAlan Cox 	register struct file *fp;
140279d7226SMatthew Dillon 	int error;
1418fe387abSDmitrij Tejblum 
142279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
1438fe387abSDmitrij Tejblum 		return (EBADF);
144279d7226SMatthew Dillon 	if (fp->f_type != DTYPE_VNODE) {
145279d7226SMatthew Dillon 		error = ESPIPE;
146279d7226SMatthew Dillon 	} else {
147279d7226SMatthew Dillon 	    error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte,
148279d7226SMatthew Dillon 		uap->offset, FOF_OFFSET);
149279d7226SMatthew Dillon 	}
150279d7226SMatthew Dillon 	fdrop(fp, p);
151279d7226SMatthew Dillon 	return(error);
1528fe387abSDmitrij Tejblum }
1538fe387abSDmitrij Tejblum 
1548fe387abSDmitrij Tejblum /*
1558fe387abSDmitrij Tejblum  * Code common for read and pread
1568fe387abSDmitrij Tejblum  */
1578fe387abSDmitrij Tejblum int
1588fe387abSDmitrij Tejblum dofileread(p, fp, fd, buf, nbyte, offset, flags)
1598fe387abSDmitrij Tejblum 	struct proc *p;
1608fe387abSDmitrij Tejblum 	struct file *fp;
1618fe387abSDmitrij Tejblum 	int fd, flags;
1628fe387abSDmitrij Tejblum 	void *buf;
1638fe387abSDmitrij Tejblum 	size_t nbyte;
1648fe387abSDmitrij Tejblum 	off_t offset;
1658fe387abSDmitrij Tejblum {
1664160ccd9SAlan Cox 	struct uio auio;
1674160ccd9SAlan Cox 	struct iovec aiov;
1684160ccd9SAlan Cox 	long cnt, error = 0;
1694160ccd9SAlan Cox #ifdef KTRACE
1704160ccd9SAlan Cox 	struct iovec ktriov;
17142ebfbf2SBrian Feldman 	struct uio ktruio;
1723c89e357SBrian Feldman 	int didktr = 0;
1734160ccd9SAlan Cox #endif
1744160ccd9SAlan Cox 
1758fe387abSDmitrij Tejblum 	aiov.iov_base = (caddr_t)buf;
1768fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
1774160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1784160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
1798fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
1808fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
1814160ccd9SAlan Cox 		return (EINVAL);
1828fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
1834160ccd9SAlan Cox 	auio.uio_rw = UIO_READ;
1844160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
1854160ccd9SAlan Cox 	auio.uio_procp = p;
1864160ccd9SAlan Cox #ifdef KTRACE
1874160ccd9SAlan Cox 	/*
1884160ccd9SAlan Cox 	 * if tracing, save a copy of iovec
1894160ccd9SAlan Cox 	 */
19042ebfbf2SBrian Feldman 	if (KTRPOINT(p, 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 
198279d7226SMatthew Dillon 	if ((error = fo_read(fp, &auio, fp->f_cred, flags, p))) {
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;
20842ebfbf2SBrian Feldman 		ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
20942ebfbf2SBrian Feldman 	}
2104160ccd9SAlan Cox #endif
2114160ccd9SAlan Cox 	p->p_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
22526f9a767SRodney W. Grimes int
226cb226aaaSPoul-Henning Kamp readv(p, uap)
227df8bae1dSRodney W. Grimes 	struct proc *p;
228df8bae1dSRodney W. Grimes 	register struct readv_args *uap;
229df8bae1dSRodney W. Grimes {
230df8bae1dSRodney W. Grimes 	register struct file *fp;
231df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
232df8bae1dSRodney W. Grimes 	struct uio auio;
233df8bae1dSRodney W. Grimes 	register struct iovec *iov;
234df8bae1dSRodney W. Grimes 	struct iovec *needfree;
235df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
236df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
237df8bae1dSRodney W. Grimes 	u_int iovlen;
238df8bae1dSRodney W. Grimes #ifdef KTRACE
239df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
24042ebfbf2SBrian Feldman 	struct uio ktruio;
241df8bae1dSRodney W. Grimes #endif
242df8bae1dSRodney W. Grimes 
243279d7226SMatthew Dillon 	if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL)
244df8bae1dSRodney W. Grimes 		return (EBADF);
245df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
246df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
247df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
248df8bae1dSRodney W. Grimes 		if (uap->iovcnt > UIO_MAXIOV)
249df8bae1dSRodney W. Grimes 			return (EINVAL);
250df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
251df8bae1dSRodney W. Grimes 		needfree = iov;
252df8bae1dSRodney W. Grimes 	} else {
253df8bae1dSRodney W. Grimes 		iov = aiov;
254df8bae1dSRodney W. Grimes 		needfree = NULL;
255df8bae1dSRodney W. Grimes 	}
256df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
257df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
258df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
259df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
260df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
2612c1011f7SJohn Dyson 	auio.uio_offset = -1;
262bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
263df8bae1dSRodney W. Grimes 		goto done;
264df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
265df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
266069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
267df8bae1dSRodney W. Grimes 			error = EINVAL;
268df8bae1dSRodney W. Grimes 			goto done;
269df8bae1dSRodney W. Grimes 		}
270069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
271df8bae1dSRodney W. Grimes 		iov++;
272df8bae1dSRodney W. Grimes 	}
273df8bae1dSRodney W. Grimes #ifdef KTRACE
274df8bae1dSRodney W. Grimes 	/*
275df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
276df8bae1dSRodney W. Grimes 	 */
277df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
278df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
279df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
28042ebfbf2SBrian Feldman 		ktruio = auio;
281df8bae1dSRodney W. Grimes 	}
282df8bae1dSRodney W. Grimes #endif
283df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
284279d7226SMatthew Dillon 	if ((error = fo_read(fp, &auio, fp->f_cred, 0, p))) {
285df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
286df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
287df8bae1dSRodney W. Grimes 			error = 0;
288279d7226SMatthew Dillon 	}
289df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
290df8bae1dSRodney W. Grimes #ifdef KTRACE
291df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
29242ebfbf2SBrian Feldman 		if (error == 0) {
29342ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
29442ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
29542ebfbf2SBrian Feldman 			ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktruio,
29642ebfbf2SBrian Feldman 			    error);
29742ebfbf2SBrian Feldman 		}
298df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
299df8bae1dSRodney W. Grimes 	}
300df8bae1dSRodney W. Grimes #endif
301cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = cnt;
302df8bae1dSRodney W. Grimes done:
303279d7226SMatthew Dillon 	fdrop(fp, p);
304df8bae1dSRodney W. Grimes 	if (needfree)
305df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
306df8bae1dSRodney W. Grimes 	return (error);
307df8bae1dSRodney W. Grimes }
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes /*
310df8bae1dSRodney W. Grimes  * Write system call
311df8bae1dSRodney W. Grimes  */
312d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
313df8bae1dSRodney W. Grimes struct write_args {
314df8bae1dSRodney W. Grimes 	int	fd;
315134e06feSBruce Evans 	const void *buf;
316134e06feSBruce Evans 	size_t	nbyte;
317df8bae1dSRodney W. Grimes };
318d2d3e875SBruce Evans #endif
31926f9a767SRodney W. Grimes int
320cb226aaaSPoul-Henning Kamp write(p, uap)
321df8bae1dSRodney W. Grimes 	struct proc *p;
322df8bae1dSRodney W. Grimes 	register struct write_args *uap;
323df8bae1dSRodney W. Grimes {
324df8bae1dSRodney W. Grimes 	register struct file *fp;
325279d7226SMatthew Dillon 	int error;
326df8bae1dSRodney W. Grimes 
327279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
328df8bae1dSRodney W. Grimes 		return (EBADF);
329279d7226SMatthew Dillon 	error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
330279d7226SMatthew Dillon 	fdrop(fp, p);
331279d7226SMatthew Dillon 	return(error);
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes /*
3358fe387abSDmitrij Tejblum  * Pwrite system call
3364160ccd9SAlan Cox  */
3374160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3384160ccd9SAlan Cox struct pwrite_args {
3394160ccd9SAlan Cox 	int	fd;
3404160ccd9SAlan Cox 	const void *buf;
3414160ccd9SAlan Cox 	size_t	nbyte;
3428fe387abSDmitrij Tejblum 	int	pad;
3434160ccd9SAlan Cox 	off_t	offset;
3444160ccd9SAlan Cox };
3454160ccd9SAlan Cox #endif
3464160ccd9SAlan Cox int
3474160ccd9SAlan Cox pwrite(p, uap)
3484160ccd9SAlan Cox 	struct proc *p;
3494160ccd9SAlan Cox 	register struct pwrite_args *uap;
3504160ccd9SAlan Cox {
3514160ccd9SAlan Cox 	register struct file *fp;
352279d7226SMatthew Dillon 	int error;
3538fe387abSDmitrij Tejblum 
354279d7226SMatthew Dillon 	if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
3558fe387abSDmitrij Tejblum 		return (EBADF);
356279d7226SMatthew Dillon 	if (fp->f_type != DTYPE_VNODE) {
357279d7226SMatthew Dillon 		error = ESPIPE;
358279d7226SMatthew Dillon 	} else {
359279d7226SMatthew Dillon 	    error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte,
360279d7226SMatthew Dillon 		uap->offset, FOF_OFFSET);
361279d7226SMatthew Dillon 	}
362279d7226SMatthew Dillon 	fdrop(fp, p);
363279d7226SMatthew Dillon 	return(error);
3648fe387abSDmitrij Tejblum }
3658fe387abSDmitrij Tejblum 
3668fe387abSDmitrij Tejblum static int
3678fe387abSDmitrij Tejblum dofilewrite(p, fp, fd, buf, nbyte, offset, flags)
3688fe387abSDmitrij Tejblum 	struct proc *p;
3698fe387abSDmitrij Tejblum 	struct file *fp;
3708fe387abSDmitrij Tejblum 	int fd, flags;
3718fe387abSDmitrij Tejblum 	const void *buf;
3728fe387abSDmitrij Tejblum 	size_t nbyte;
3738fe387abSDmitrij Tejblum 	off_t offset;
3748fe387abSDmitrij Tejblum {
3754160ccd9SAlan Cox 	struct uio auio;
3764160ccd9SAlan Cox 	struct iovec aiov;
3774160ccd9SAlan Cox 	long cnt, error = 0;
3784160ccd9SAlan Cox #ifdef KTRACE
3794160ccd9SAlan Cox 	struct iovec ktriov;
38042ebfbf2SBrian Feldman 	struct uio ktruio;
3813c89e357SBrian Feldman 	int didktr = 0;
3824160ccd9SAlan Cox #endif
3834160ccd9SAlan Cox 
384b31ae1adSPeter Wemm 	aiov.iov_base = (void *)(uintptr_t)buf;
3858fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
3864160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3874160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
3888fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
3898fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
3904160ccd9SAlan Cox 		return (EINVAL);
3918fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
3924160ccd9SAlan Cox 	auio.uio_rw = UIO_WRITE;
3934160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
3944160ccd9SAlan Cox 	auio.uio_procp = p;
3954160ccd9SAlan Cox #ifdef KTRACE
3964160ccd9SAlan Cox 	/*
39742ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
3984160ccd9SAlan Cox 	 */
39942ebfbf2SBrian Feldman 	if (KTRPOINT(p, KTR_GENIO)) {
4004160ccd9SAlan Cox 		ktriov = aiov;
40142ebfbf2SBrian Feldman 		ktruio = auio;
4023c89e357SBrian Feldman 		didktr = 1;
40342ebfbf2SBrian Feldman 	}
4044160ccd9SAlan Cox #endif
4058fe387abSDmitrij Tejblum 	cnt = nbyte;
406279d7226SMatthew Dillon 	bwillwrite();
40713ccadd4SBrian Feldman 	if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
4084160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
4094160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
4104160ccd9SAlan Cox 			error = 0;
4114160ccd9SAlan Cox 		if (error == EPIPE)
4124160ccd9SAlan Cox 			psignal(p, SIGPIPE);
4134160ccd9SAlan Cox 	}
4144160ccd9SAlan Cox 	cnt -= auio.uio_resid;
4154160ccd9SAlan Cox #ifdef KTRACE
4163c89e357SBrian Feldman 	if (didktr && error == 0) {
41742ebfbf2SBrian Feldman 		ktruio.uio_iov = &ktriov;
41842ebfbf2SBrian Feldman 		ktruio.uio_resid = cnt;
41942ebfbf2SBrian Feldman 		ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
42042ebfbf2SBrian Feldman 	}
4214160ccd9SAlan Cox #endif
4224160ccd9SAlan Cox 	p->p_retval[0] = cnt;
4234160ccd9SAlan Cox 	return (error);
4244160ccd9SAlan Cox }
4254160ccd9SAlan Cox 
4264160ccd9SAlan Cox /*
427df8bae1dSRodney W. Grimes  * Gather write system call
428df8bae1dSRodney W. Grimes  */
429d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
430df8bae1dSRodney W. Grimes struct writev_args {
431df8bae1dSRodney W. Grimes 	int	fd;
432df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
433df8bae1dSRodney W. Grimes 	u_int	iovcnt;
434df8bae1dSRodney W. Grimes };
435d2d3e875SBruce Evans #endif
43626f9a767SRodney W. Grimes int
437cb226aaaSPoul-Henning Kamp writev(p, uap)
438df8bae1dSRodney W. Grimes 	struct proc *p;
439df8bae1dSRodney W. Grimes 	register struct writev_args *uap;
440df8bae1dSRodney W. Grimes {
441df8bae1dSRodney W. Grimes 	register struct file *fp;
442df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
443df8bae1dSRodney W. Grimes 	struct uio auio;
444df8bae1dSRodney W. Grimes 	register struct iovec *iov;
445df8bae1dSRodney W. Grimes 	struct iovec *needfree;
446df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
447df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
448df8bae1dSRodney W. Grimes 	u_int iovlen;
449df8bae1dSRodney W. Grimes #ifdef KTRACE
450df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
45142ebfbf2SBrian Feldman 	struct uio ktruio;
452df8bae1dSRodney W. Grimes #endif
453df8bae1dSRodney W. Grimes 
454279d7226SMatthew Dillon 	if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
455df8bae1dSRodney W. Grimes 		return (EBADF);
456df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
457df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
458df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
4591aa3e7ddSBrian Feldman 		if (uap->iovcnt > UIO_MAXIOV) {
4601aa3e7ddSBrian Feldman 			needfree = NULL;
4611aa3e7ddSBrian Feldman 			error = EINVAL;
4621aa3e7ddSBrian Feldman 			goto done;
4631aa3e7ddSBrian Feldman 		}
464df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
465df8bae1dSRodney W. Grimes 		needfree = iov;
466df8bae1dSRodney W. Grimes 	} else {
467df8bae1dSRodney W. Grimes 		iov = aiov;
468df8bae1dSRodney W. Grimes 		needfree = NULL;
469df8bae1dSRodney W. Grimes 	}
470df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
471df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
472df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
473df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
474df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
4752c1011f7SJohn Dyson 	auio.uio_offset = -1;
476bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
477df8bae1dSRodney W. Grimes 		goto done;
478df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
479df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
480069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
481df8bae1dSRodney W. Grimes 			error = EINVAL;
482df8bae1dSRodney W. Grimes 			goto done;
483df8bae1dSRodney W. Grimes 		}
484069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
485df8bae1dSRodney W. Grimes 		iov++;
486df8bae1dSRodney W. Grimes 	}
487df8bae1dSRodney W. Grimes #ifdef KTRACE
488df8bae1dSRodney W. Grimes 	/*
48942ebfbf2SBrian Feldman 	 * if tracing, save a copy of iovec and uio
490df8bae1dSRodney W. Grimes 	 */
491df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
492df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
493df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
49442ebfbf2SBrian Feldman 		ktruio = auio;
495df8bae1dSRodney W. Grimes 	}
496df8bae1dSRodney W. Grimes #endif
497df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
49813ccadd4SBrian Feldman 	if ((error = fo_write(fp, &auio, fp->f_cred, 0, p))) {
499df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
500df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
501df8bae1dSRodney W. Grimes 			error = 0;
502df8bae1dSRodney W. Grimes 		if (error == EPIPE)
503df8bae1dSRodney W. Grimes 			psignal(p, SIGPIPE);
504df8bae1dSRodney W. Grimes 	}
505df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
506df8bae1dSRodney W. Grimes #ifdef KTRACE
507df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
50842ebfbf2SBrian Feldman 		if (error == 0) {
50942ebfbf2SBrian Feldman 			ktruio.uio_iov = ktriov;
51042ebfbf2SBrian Feldman 			ktruio.uio_resid = cnt;
51142ebfbf2SBrian Feldman 			ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, &ktruio,
51242ebfbf2SBrian Feldman 			    error);
51342ebfbf2SBrian Feldman 		}
514df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
515df8bae1dSRodney W. Grimes 	}
516df8bae1dSRodney W. Grimes #endif
517cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = cnt;
518df8bae1dSRodney W. Grimes done:
519d8177437SBrian Feldman 	fdrop(fp, p);
520df8bae1dSRodney W. Grimes 	if (needfree)
521df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
522df8bae1dSRodney W. Grimes 	return (error);
523df8bae1dSRodney W. Grimes }
524df8bae1dSRodney W. Grimes 
525df8bae1dSRodney W. Grimes /*
526df8bae1dSRodney W. Grimes  * Ioctl system call
527df8bae1dSRodney W. Grimes  */
528d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
529df8bae1dSRodney W. Grimes struct ioctl_args {
530df8bae1dSRodney W. Grimes 	int	fd;
531069e9bc1SDoug Rabson 	u_long	com;
532df8bae1dSRodney W. Grimes 	caddr_t	data;
533df8bae1dSRodney W. Grimes };
534d2d3e875SBruce Evans #endif
535df8bae1dSRodney W. Grimes /* ARGSUSED */
53626f9a767SRodney W. Grimes int
537cb226aaaSPoul-Henning Kamp ioctl(p, uap)
538df8bae1dSRodney W. Grimes 	struct proc *p;
539df8bae1dSRodney W. Grimes 	register struct ioctl_args *uap;
540df8bae1dSRodney W. Grimes {
541df8bae1dSRodney W. Grimes 	register struct file *fp;
542df8bae1dSRodney W. Grimes 	register struct filedesc *fdp;
543831b9ef2SDoug Rabson 	register u_long com;
544831b9ef2SDoug Rabson 	int error;
545df8bae1dSRodney W. Grimes 	register u_int size;
546df8bae1dSRodney W. Grimes 	caddr_t data, memp;
547df8bae1dSRodney W. Grimes 	int tmp;
548df8bae1dSRodney W. Grimes #define STK_PARAMS	128
549d2ba455cSMatthew Dillon 	union {
550df8bae1dSRodney W. Grimes 	    char stkbuf[STK_PARAMS];
551d2ba455cSMatthew Dillon 	    long align;
552d2ba455cSMatthew Dillon 	} ubuf;
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes 	fdp = p->p_fd;
555df8bae1dSRodney W. Grimes 	if ((u_int)uap->fd >= fdp->fd_nfiles ||
556df8bae1dSRodney W. Grimes 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
557df8bae1dSRodney W. Grimes 		return (EBADF);
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
560df8bae1dSRodney W. Grimes 		return (EBADF);
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
563df8bae1dSRodney W. Grimes 	case FIONCLEX:
564df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
565df8bae1dSRodney W. Grimes 		return (0);
566df8bae1dSRodney W. Grimes 	case FIOCLEX:
567df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
568df8bae1dSRodney W. Grimes 		return (0);
569df8bae1dSRodney W. Grimes 	}
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes 	/*
572df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
573df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
574df8bae1dSRodney W. Grimes 	 */
575df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
576df8bae1dSRodney W. Grimes 	if (size > IOCPARM_MAX)
577df8bae1dSRodney W. Grimes 		return (ENOTTY);
578279d7226SMatthew Dillon 
579279d7226SMatthew Dillon 	fhold(fp);
580279d7226SMatthew Dillon 
581df8bae1dSRodney W. Grimes 	memp = NULL;
582d2ba455cSMatthew Dillon 	if (size > sizeof (ubuf.stkbuf)) {
583df8bae1dSRodney W. Grimes 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
584df8bae1dSRodney W. Grimes 		data = memp;
585279d7226SMatthew Dillon 	} else {
586d2ba455cSMatthew Dillon 		data = ubuf.stkbuf;
587279d7226SMatthew Dillon 	}
588df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
589df8bae1dSRodney W. Grimes 		if (size) {
590df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
591df8bae1dSRodney W. Grimes 			if (error) {
592df8bae1dSRodney W. Grimes 				if (memp)
593df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
594279d7226SMatthew Dillon 				fdrop(fp, p);
595df8bae1dSRodney W. Grimes 				return (error);
596df8bae1dSRodney W. Grimes 			}
597279d7226SMatthew Dillon 		} else {
598df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
599279d7226SMatthew Dillon 		}
600279d7226SMatthew Dillon 	} else if ((com&IOC_OUT) && size) {
601df8bae1dSRodney W. Grimes 		/*
602df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
603df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
604df8bae1dSRodney W. Grimes 		 */
605df8bae1dSRodney W. Grimes 		bzero(data, size);
606279d7226SMatthew Dillon 	} else if (com&IOC_VOID) {
607df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
608279d7226SMatthew Dillon 	}
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	switch (com) {
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 	case FIONBIO:
613bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
614df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
615df8bae1dSRodney W. Grimes 		else
616df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
61713ccadd4SBrian Feldman 		error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
618df8bae1dSRodney W. Grimes 		break;
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes 	case FIOASYNC:
621bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
622df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
623df8bae1dSRodney W. Grimes 		else
624df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
62513ccadd4SBrian Feldman 		error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
626df8bae1dSRodney W. Grimes 		break;
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes 	default:
62913ccadd4SBrian Feldman 		error = fo_ioctl(fp, com, data, p);
630df8bae1dSRodney W. Grimes 		/*
631df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
632df8bae1dSRodney W. Grimes 		 * already set and checked above.
633df8bae1dSRodney W. Grimes 		 */
634df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
635df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
636df8bae1dSRodney W. Grimes 		break;
637df8bae1dSRodney W. Grimes 	}
638df8bae1dSRodney W. Grimes 	if (memp)
639df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
640279d7226SMatthew Dillon 	fdrop(fp, p);
641df8bae1dSRodney W. Grimes 	return (error);
642df8bae1dSRodney W. Grimes }
643df8bae1dSRodney W. Grimes 
6448cb96f20SPeter Wemm static int	nselcoll;	/* Select collisions since boot */
64587b6de2bSPoul-Henning Kamp int	selwait;
6468cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes /*
649df8bae1dSRodney W. Grimes  * Select system call.
650df8bae1dSRodney W. Grimes  */
651d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
652df8bae1dSRodney W. Grimes struct select_args {
653b08f7993SSujal Patel 	int	nd;
654df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
655df8bae1dSRodney W. Grimes 	struct	timeval *tv;
656df8bae1dSRodney W. Grimes };
657d2d3e875SBruce Evans #endif
65826f9a767SRodney W. Grimes int
659cb226aaaSPoul-Henning Kamp select(p, uap)
660df8bae1dSRodney W. Grimes 	register struct proc *p;
661df8bae1dSRodney W. Grimes 	register struct select_args *uap;
662df8bae1dSRodney W. Grimes {
663d5e4d7e1SBruce Evans 	/*
664d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
665d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
666d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
667d5e4d7e1SBruce Evans 	 * of 256.
668d5e4d7e1SBruce Evans 	 */
669d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
670d5e4d7e1SBruce Evans 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
67100af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
67200af9731SPoul-Henning Kamp 	int s, ncoll, error, timo;
673d5e4d7e1SBruce Evans 	u_int nbufbytes, ncpbytes, nfdbits;
674df8bae1dSRodney W. Grimes 
675b08f7993SSujal Patel 	if (uap->nd < 0)
676acbfbfeaSSujal Patel 		return (EINVAL);
677df8bae1dSRodney W. Grimes 	if (uap->nd > p->p_fd->fd_nfiles)
678df8bae1dSRodney W. Grimes 		uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
679b08f7993SSujal Patel 
680d5e4d7e1SBruce Evans 	/*
681d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
682d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
683d5e4d7e1SBruce Evans 	 */
684d5e4d7e1SBruce Evans 	nfdbits = roundup(uap->nd, NFDBITS);
685d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
686d5e4d7e1SBruce Evans 	nbufbytes = 0;
687d5e4d7e1SBruce Evans 	if (uap->in != NULL)
688d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
689d5e4d7e1SBruce Evans 	if (uap->ou != NULL)
690d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
691d5e4d7e1SBruce Evans 	if (uap->ex != NULL)
692d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
693d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
694d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
695d5e4d7e1SBruce Evans 	else
696d5e4d7e1SBruce Evans 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
697b08f7993SSujal Patel 
698b08f7993SSujal Patel 	/*
699d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
700d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
701d5e4d7e1SBruce Evans 	 * together.
702b08f7993SSujal Patel 	 */
703d5e4d7e1SBruce Evans 	sbp = selbits;
704df8bae1dSRodney W. Grimes #define	getbits(name, x) \
705d5e4d7e1SBruce Evans 	do {								\
706d5e4d7e1SBruce Evans 		if (uap->name == NULL)					\
707d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
708d5e4d7e1SBruce Evans 		else {							\
709d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
710d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
711d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
712d5e4d7e1SBruce Evans 			error = copyin(uap->name, ibits[x], ncpbytes);	\
713d5e4d7e1SBruce Evans 			if (error != 0)					\
714d5e4d7e1SBruce Evans 				goto done;				\
715d5e4d7e1SBruce Evans 		}							\
716d5e4d7e1SBruce Evans 	} while (0)
717df8bae1dSRodney W. Grimes 	getbits(in, 0);
718df8bae1dSRodney W. Grimes 	getbits(ou, 1);
719df8bae1dSRodney W. Grimes 	getbits(ex, 2);
720df8bae1dSRodney W. Grimes #undef	getbits
721d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
722d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
723df8bae1dSRodney W. Grimes 
724df8bae1dSRodney W. Grimes 	if (uap->tv) {
725df8bae1dSRodney W. Grimes 		error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
726df8bae1dSRodney W. Grimes 			sizeof (atv));
727df8bae1dSRodney W. Grimes 		if (error)
728df8bae1dSRodney W. Grimes 			goto done;
729df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
730df8bae1dSRodney W. Grimes 			error = EINVAL;
731df8bae1dSRodney W. Grimes 			goto done;
732df8bae1dSRodney W. Grimes 		}
733c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
73400af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
7359c386f6bSJohn Baldwin 	} else {
73600af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
7379c386f6bSJohn Baldwin 		atv.tv_usec = 0;
7389c386f6bSJohn Baldwin 	}
73900af9731SPoul-Henning Kamp 	timo = 0;
740df8bae1dSRodney W. Grimes retry:
741df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
742df8bae1dSRodney W. Grimes 	p->p_flag |= P_SELECT;
743cb226aaaSPoul-Henning Kamp 	error = selscan(p, ibits, obits, uap->nd);
744cb226aaaSPoul-Henning Kamp 	if (error || p->p_retval[0])
745df8bae1dSRodney W. Grimes 		goto done;
7464da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
747c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
74800af9731SPoul-Henning Kamp 		if (timevalcmp(&rtv, &atv, >=))
749df8bae1dSRodney W. Grimes 			goto done;
75000af9731SPoul-Henning Kamp 		ttv = atv;
75100af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
75200af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
75300af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
754df8bae1dSRodney W. Grimes 	}
75500af9731SPoul-Henning Kamp 	s = splhigh();
756df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
757df8bae1dSRodney W. Grimes 		splx(s);
758df8bae1dSRodney W. Grimes 		goto retry;
759df8bae1dSRodney W. Grimes 	}
760df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
761bfbbc4aaSJason Evans 
762df8bae1dSRodney W. Grimes 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
763bfbbc4aaSJason Evans 
764df8bae1dSRodney W. Grimes 	splx(s);
765df8bae1dSRodney W. Grimes 	if (error == 0)
766df8bae1dSRodney W. Grimes 		goto retry;
767df8bae1dSRodney W. Grimes done:
768df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
769df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
770df8bae1dSRodney W. Grimes 	if (error == ERESTART)
771df8bae1dSRodney W. Grimes 		error = EINTR;
772df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
773df8bae1dSRodney W. Grimes 		error = 0;
774df8bae1dSRodney W. Grimes #define	putbits(name, x) \
775d5e4d7e1SBruce Evans 	if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
776df8bae1dSRodney W. Grimes 		error = error2;
777df8bae1dSRodney W. Grimes 	if (error == 0) {
778df8bae1dSRodney W. Grimes 		int error2;
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes 		putbits(in, 0);
781df8bae1dSRodney W. Grimes 		putbits(ou, 1);
782df8bae1dSRodney W. Grimes 		putbits(ex, 2);
783df8bae1dSRodney W. Grimes #undef putbits
784df8bae1dSRodney W. Grimes 	}
785d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
786d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
787df8bae1dSRodney W. Grimes 	return (error);
788df8bae1dSRodney W. Grimes }
789df8bae1dSRodney W. Grimes 
79087b6de2bSPoul-Henning Kamp static int
791cb226aaaSPoul-Henning Kamp selscan(p, ibits, obits, nfd)
792df8bae1dSRodney W. Grimes 	struct proc *p;
793b08f7993SSujal Patel 	fd_mask **ibits, **obits;
794cb226aaaSPoul-Henning Kamp 	int nfd;
795df8bae1dSRodney W. Grimes {
796f082218cSPeter Wemm 	struct filedesc *fdp = p->p_fd;
797f082218cSPeter Wemm 	int msk, i, fd;
798f082218cSPeter Wemm 	fd_mask bits;
799df8bae1dSRodney W. Grimes 	struct file *fp;
800df8bae1dSRodney W. Grimes 	int n = 0;
8012087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
80242d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
805d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
806d5e4d7e1SBruce Evans 			continue;
807df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
808b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
809f082218cSPeter Wemm 			/* ffs(int mask) not portable, fd_mask is long */
810f082218cSPeter Wemm 			for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
811f082218cSPeter Wemm 				if (!(bits & 1))
812f082218cSPeter Wemm 					continue;
813df8bae1dSRodney W. Grimes 				fp = fdp->fd_ofiles[fd];
814df8bae1dSRodney W. Grimes 				if (fp == NULL)
815df8bae1dSRodney W. Grimes 					return (EBADF);
81613ccadd4SBrian Feldman 				if (fo_poll(fp, flag[msk], fp->f_cred, p)) {
817b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
818f082218cSPeter Wemm 					    ((fd_mask)1 << ((fd) % NFDBITS));
819df8bae1dSRodney W. Grimes 					n++;
820df8bae1dSRodney W. Grimes 				}
821df8bae1dSRodney W. Grimes 			}
822df8bae1dSRodney W. Grimes 		}
823df8bae1dSRodney W. Grimes 	}
824cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = n;
825df8bae1dSRodney W. Grimes 	return (0);
826df8bae1dSRodney W. Grimes }
827df8bae1dSRodney W. Grimes 
82842d11757SPeter Wemm /*
82942d11757SPeter Wemm  * Poll system call.
83042d11757SPeter Wemm  */
83142d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
83242d11757SPeter Wemm struct poll_args {
83342d11757SPeter Wemm 	struct pollfd *fds;
83442d11757SPeter Wemm 	u_int	nfds;
83542d11757SPeter Wemm 	int	timeout;
83642d11757SPeter Wemm };
83742d11757SPeter Wemm #endif
83842d11757SPeter Wemm int
839cb226aaaSPoul-Henning Kamp poll(p, uap)
84042d11757SPeter Wemm 	register struct proc *p;
84142d11757SPeter Wemm 	register struct poll_args *uap;
84242d11757SPeter Wemm {
84342d11757SPeter Wemm 	caddr_t bits;
84442d11757SPeter Wemm 	char smallbits[32 * sizeof(struct pollfd)];
84500af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
84600af9731SPoul-Henning Kamp 	int s, ncoll, error = 0, timo;
84742d11757SPeter Wemm 	size_t ni;
84842d11757SPeter Wemm 
84942d11757SPeter Wemm 	if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
85042d11757SPeter Wemm 		/* forgiving; slightly wrong */
85142d11757SPeter Wemm 		SCARG(uap, nfds) = p->p_fd->fd_nfiles;
85242d11757SPeter Wemm 	}
85342d11757SPeter Wemm 	ni = SCARG(uap, nfds) * sizeof(struct pollfd);
85442d11757SPeter Wemm 	if (ni > sizeof(smallbits))
85542d11757SPeter Wemm 		bits = malloc(ni, M_TEMP, M_WAITOK);
85642d11757SPeter Wemm 	else
85742d11757SPeter Wemm 		bits = smallbits;
85842d11757SPeter Wemm 	error = copyin(SCARG(uap, fds), bits, ni);
85942d11757SPeter Wemm 	if (error)
86042d11757SPeter Wemm 		goto done;
86142d11757SPeter Wemm 	if (SCARG(uap, timeout) != INFTIM) {
86242d11757SPeter Wemm 		atv.tv_sec = SCARG(uap, timeout) / 1000;
86342d11757SPeter Wemm 		atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
86442d11757SPeter Wemm 		if (itimerfix(&atv)) {
86542d11757SPeter Wemm 			error = EINVAL;
86642d11757SPeter Wemm 			goto done;
86742d11757SPeter Wemm 		}
868c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
86900af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
8709c386f6bSJohn Baldwin 	} else {
87100af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
8729c386f6bSJohn Baldwin 		atv.tv_usec = 0;
8739c386f6bSJohn Baldwin 	}
87400af9731SPoul-Henning Kamp 	timo = 0;
87542d11757SPeter Wemm retry:
87642d11757SPeter Wemm 	ncoll = nselcoll;
87742d11757SPeter Wemm 	p->p_flag |= P_SELECT;
878cb226aaaSPoul-Henning Kamp 	error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds));
879cb226aaaSPoul-Henning Kamp 	if (error || p->p_retval[0])
88042d11757SPeter Wemm 		goto done;
8814da144c0SJohn Baldwin 	if (atv.tv_sec || atv.tv_usec) {
882c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
88300af9731SPoul-Henning Kamp 		if (timevalcmp(&rtv, &atv, >=))
88442d11757SPeter Wemm 			goto done;
88500af9731SPoul-Henning Kamp 		ttv = atv;
88600af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
88700af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
88800af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
88942d11757SPeter Wemm 	}
89000af9731SPoul-Henning Kamp 	s = splhigh();
89142d11757SPeter Wemm 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
89242d11757SPeter Wemm 		splx(s);
89342d11757SPeter Wemm 		goto retry;
89442d11757SPeter Wemm 	}
89542d11757SPeter Wemm 	p->p_flag &= ~P_SELECT;
89642d11757SPeter Wemm 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
89742d11757SPeter Wemm 	splx(s);
89842d11757SPeter Wemm 	if (error == 0)
89942d11757SPeter Wemm 		goto retry;
90042d11757SPeter Wemm done:
90142d11757SPeter Wemm 	p->p_flag &= ~P_SELECT;
90242d11757SPeter Wemm 	/* poll is not restarted after signals... */
90342d11757SPeter Wemm 	if (error == ERESTART)
90442d11757SPeter Wemm 		error = EINTR;
90542d11757SPeter Wemm 	if (error == EWOULDBLOCK)
90642d11757SPeter Wemm 		error = 0;
90742d11757SPeter Wemm 	if (error == 0) {
90842d11757SPeter Wemm 		error = copyout(bits, SCARG(uap, fds), ni);
90942d11757SPeter Wemm 		if (error)
91042d11757SPeter Wemm 			goto out;
91142d11757SPeter Wemm 	}
91242d11757SPeter Wemm out:
91342d11757SPeter Wemm 	if (ni > sizeof(smallbits))
91442d11757SPeter Wemm 		free(bits, M_TEMP);
91542d11757SPeter Wemm 	return (error);
91642d11757SPeter Wemm }
91742d11757SPeter Wemm 
91842d11757SPeter Wemm static int
919cb226aaaSPoul-Henning Kamp pollscan(p, fds, nfd)
92042d11757SPeter Wemm 	struct proc *p;
92142d11757SPeter Wemm 	struct pollfd *fds;
92242d11757SPeter Wemm 	int nfd;
92342d11757SPeter Wemm {
92442d11757SPeter Wemm 	register struct filedesc *fdp = p->p_fd;
92542d11757SPeter Wemm 	int i;
92642d11757SPeter Wemm 	struct file *fp;
92742d11757SPeter Wemm 	int n = 0;
92842d11757SPeter Wemm 
92942d11757SPeter Wemm 	for (i = 0; i < nfd; i++, fds++) {
930337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
93142d11757SPeter Wemm 			fds->revents = POLLNVAL;
93242d11757SPeter Wemm 			n++;
933337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
934337c9691SJordan K. Hubbard 			fds->revents = 0;
93542d11757SPeter Wemm 		} else {
93642d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
937279d7226SMatthew Dillon 			if (fp == NULL) {
93842d11757SPeter Wemm 				fds->revents = POLLNVAL;
93942d11757SPeter Wemm 				n++;
94042d11757SPeter Wemm 			} else {
9412087c896SBruce Evans 				/*
9422087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
9432087c896SBruce Evans 				 * POLLERR if appropriate.
9442087c896SBruce Evans 				 */
94513ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
94613ccadd4SBrian Feldman 				    fp->f_cred, p);
94742d11757SPeter Wemm 				if (fds->revents != 0)
94842d11757SPeter Wemm 					n++;
94942d11757SPeter Wemm 			}
95042d11757SPeter Wemm 		}
95142d11757SPeter Wemm 	}
952cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = n;
95342d11757SPeter Wemm 	return (0);
95442d11757SPeter Wemm }
95542d11757SPeter Wemm 
95642d11757SPeter Wemm /*
95742d11757SPeter Wemm  * OpenBSD poll system call.
95842d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
95942d11757SPeter Wemm  */
96042d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
96142d11757SPeter Wemm struct openbsd_poll_args {
96242d11757SPeter Wemm 	struct pollfd *fds;
96342d11757SPeter Wemm 	u_int	nfds;
96442d11757SPeter Wemm 	int	timeout;
96542d11757SPeter Wemm };
96642d11757SPeter Wemm #endif
96742d11757SPeter Wemm int
968cb226aaaSPoul-Henning Kamp openbsd_poll(p, uap)
96942d11757SPeter Wemm 	register struct proc *p;
97042d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
97142d11757SPeter Wemm {
972cb226aaaSPoul-Henning Kamp 	return (poll(p, (struct poll_args *)uap));
97342d11757SPeter Wemm }
97442d11757SPeter Wemm 
975df8bae1dSRodney W. Grimes /*ARGSUSED*/
97626f9a767SRodney W. Grimes int
97742d11757SPeter Wemm seltrue(dev, events, p)
978df8bae1dSRodney W. Grimes 	dev_t dev;
97942d11757SPeter Wemm 	int events;
980df8bae1dSRodney W. Grimes 	struct proc *p;
981df8bae1dSRodney W. Grimes {
982df8bae1dSRodney W. Grimes 
98342d11757SPeter Wemm 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
984df8bae1dSRodney W. Grimes }
985df8bae1dSRodney W. Grimes 
986df8bae1dSRodney W. Grimes /*
987df8bae1dSRodney W. Grimes  * Record a select request.
988df8bae1dSRodney W. Grimes  */
989df8bae1dSRodney W. Grimes void
990df8bae1dSRodney W. Grimes selrecord(selector, sip)
991df8bae1dSRodney W. Grimes 	struct proc *selector;
992df8bae1dSRodney W. Grimes 	struct selinfo *sip;
993df8bae1dSRodney W. Grimes {
994df8bae1dSRodney W. Grimes 	struct proc *p;
995df8bae1dSRodney W. Grimes 	pid_t mypid;
996df8bae1dSRodney W. Grimes 
997df8bae1dSRodney W. Grimes 	mypid = selector->p_pid;
998df8bae1dSRodney W. Grimes 	if (sip->si_pid == mypid)
999df8bae1dSRodney W. Grimes 		return;
1000df8bae1dSRodney W. Grimes 	if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1001df8bae1dSRodney W. Grimes 	    p->p_wchan == (caddr_t)&selwait)
1002df8bae1dSRodney W. Grimes 		sip->si_flags |= SI_COLL;
1003df8bae1dSRodney W. Grimes 	else
1004df8bae1dSRodney W. Grimes 		sip->si_pid = mypid;
1005df8bae1dSRodney W. Grimes }
1006df8bae1dSRodney W. Grimes 
1007df8bae1dSRodney W. Grimes /*
1008df8bae1dSRodney W. Grimes  * Do a wakeup when a selectable event occurs.
1009df8bae1dSRodney W. Grimes  */
1010df8bae1dSRodney W. Grimes void
1011df8bae1dSRodney W. Grimes selwakeup(sip)
1012df8bae1dSRodney W. Grimes 	register struct selinfo *sip;
1013df8bae1dSRodney W. Grimes {
1014df8bae1dSRodney W. Grimes 	register struct proc *p;
1015df8bae1dSRodney W. Grimes 	int s;
1016df8bae1dSRodney W. Grimes 
1017df8bae1dSRodney W. Grimes 	if (sip->si_pid == 0)
1018df8bae1dSRodney W. Grimes 		return;
1019df8bae1dSRodney W. Grimes 	if (sip->si_flags & SI_COLL) {
1020df8bae1dSRodney W. Grimes 		nselcoll++;
1021df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
1022df8bae1dSRodney W. Grimes 		wakeup((caddr_t)&selwait);
1023df8bae1dSRodney W. Grimes 	}
1024df8bae1dSRodney W. Grimes 	p = pfind(sip->si_pid);
1025df8bae1dSRodney W. Grimes 	sip->si_pid = 0;
1026df8bae1dSRodney W. Grimes 	if (p != NULL) {
1027df8bae1dSRodney W. Grimes 		s = splhigh();
1028df8bae1dSRodney W. Grimes 		if (p->p_wchan == (caddr_t)&selwait) {
1029df8bae1dSRodney W. Grimes 			if (p->p_stat == SSLEEP)
1030df8bae1dSRodney W. Grimes 				setrunnable(p);
1031df8bae1dSRodney W. Grimes 			else
1032df8bae1dSRodney W. Grimes 				unsleep(p);
1033df8bae1dSRodney W. Grimes 		} else if (p->p_flag & P_SELECT)
1034df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_SELECT;
1035df8bae1dSRodney W. Grimes 		splx(s);
1036df8bae1dSRodney W. Grimes 	}
1037df8bae1dSRodney W. Grimes }
1038