xref: /freebsd/sys/kern/sys_generic.c (revision 13ccadd4b00cd6944ff3e60bee50aa629a769016)
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>
4920982410SBruce Evans #include <sys/ttycom.h>
503ac4d1efSBruce Evans #include <sys/fcntl.h>
51df8bae1dSRodney W. Grimes #include <sys/file.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
53797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55df8bae1dSRodney W. Grimes #include <sys/uio.h>
56df8bae1dSRodney W. Grimes #include <sys/kernel.h>
57df8bae1dSRodney W. Grimes #include <sys/malloc.h>
5842d11757SPeter Wemm #include <sys/poll.h>
5942d11757SPeter Wemm #include <sys/sysent.h>
60df8bae1dSRodney W. Grimes #ifdef KTRACE
61df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
62df8bae1dSRodney W. Grimes #endif
63df8bae1dSRodney W. Grimes 
64069e9bc1SDoug Rabson #include <machine/limits.h>
65069e9bc1SDoug Rabson 
66a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
67a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
68a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
6955166637SPoul-Henning Kamp 
70cb226aaaSPoul-Henning Kamp static int	pollscan __P((struct proc *, struct pollfd *, int));
712087c896SBruce Evans static int	selscan __P((struct proc *, fd_mask **, fd_mask **, int));
728fe387abSDmitrij Tejblum static struct file* getfp __P((struct filedesc *, int, int));
738fe387abSDmitrij Tejblum static int	dofileread __P((struct proc *, struct file *, int, void *,
748fe387abSDmitrij Tejblum 		    size_t, off_t, int));
758fe387abSDmitrij Tejblum static int	dofilewrite __P((struct proc *, struct file *, int,
768fe387abSDmitrij Tejblum 		    const void *, size_t, off_t, int));
778fe387abSDmitrij Tejblum 
788fe387abSDmitrij Tejblum static struct file*
798fe387abSDmitrij Tejblum getfp(fdp, fd, flag)
808fe387abSDmitrij Tejblum 	struct filedesc* fdp;
818fe387abSDmitrij Tejblum 	int fd, flag;
828fe387abSDmitrij Tejblum {
838fe387abSDmitrij Tejblum 	struct file* fp;
848fe387abSDmitrij Tejblum 
858fe387abSDmitrij Tejblum 	if (((u_int)fd) >= fdp->fd_nfiles ||
868fe387abSDmitrij Tejblum 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
878fe387abSDmitrij Tejblum 	    (fp->f_flag & flag) == 0)
888fe387abSDmitrij Tejblum 		return (NULL);
898fe387abSDmitrij Tejblum 	return (fp);
908fe387abSDmitrij Tejblum }
91d93f860cSPoul-Henning Kamp 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * Read system call.
94df8bae1dSRodney W. Grimes  */
95d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
96df8bae1dSRodney W. Grimes struct read_args {
97df8bae1dSRodney W. Grimes 	int	fd;
98134e06feSBruce Evans 	void	*buf;
99134e06feSBruce Evans 	size_t	nbyte;
100df8bae1dSRodney W. Grimes };
101d2d3e875SBruce Evans #endif
10226f9a767SRodney W. Grimes int
103cb226aaaSPoul-Henning Kamp read(p, uap)
104df8bae1dSRodney W. Grimes 	struct proc *p;
105df8bae1dSRodney W. Grimes 	register struct read_args *uap;
106df8bae1dSRodney W. Grimes {
107df8bae1dSRodney W. Grimes 	register struct file *fp;
108df8bae1dSRodney W. Grimes 
1098fe387abSDmitrij Tejblum 	if ((fp = getfp(p->p_fd, uap->fd, FREAD)) == NULL)
110df8bae1dSRodney W. Grimes 		return (EBADF);
1118fe387abSDmitrij Tejblum 	return (dofileread(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0));
112df8bae1dSRodney W. Grimes }
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes /*
1158fe387abSDmitrij Tejblum  * Pread system call
1164160ccd9SAlan Cox  */
1174160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1184160ccd9SAlan Cox struct pread_args {
1194160ccd9SAlan Cox 	int	fd;
1204160ccd9SAlan Cox 	void	*buf;
1214160ccd9SAlan Cox 	size_t	nbyte;
1228fe387abSDmitrij Tejblum 	int	pad;
1234160ccd9SAlan Cox 	off_t	offset;
1244160ccd9SAlan Cox };
1254160ccd9SAlan Cox #endif
1264160ccd9SAlan Cox int
1274160ccd9SAlan Cox pread(p, uap)
1284160ccd9SAlan Cox 	struct proc *p;
1294160ccd9SAlan Cox 	register struct pread_args *uap;
1304160ccd9SAlan Cox {
1314160ccd9SAlan Cox 	register struct file *fp;
1328fe387abSDmitrij Tejblum 
1338fe387abSDmitrij Tejblum 	if ((fp = getfp(p->p_fd, uap->fd, FREAD)) == NULL)
1348fe387abSDmitrij Tejblum 		return (EBADF);
1358fe387abSDmitrij Tejblum 	if (fp->f_type != DTYPE_VNODE)
1368fe387abSDmitrij Tejblum 		return (ESPIPE);
1378fe387abSDmitrij Tejblum 	return (dofileread(p, fp, uap->fd, uap->buf, uap->nbyte, uap->offset,
1388fe387abSDmitrij Tejblum 	    FOF_OFFSET));
1398fe387abSDmitrij Tejblum }
1408fe387abSDmitrij Tejblum 
1418fe387abSDmitrij Tejblum /*
1428fe387abSDmitrij Tejblum  * Code common for read and pread
1438fe387abSDmitrij Tejblum  */
1448fe387abSDmitrij Tejblum int
1458fe387abSDmitrij Tejblum dofileread(p, fp, fd, buf, nbyte, offset, flags)
1468fe387abSDmitrij Tejblum 	struct proc *p;
1478fe387abSDmitrij Tejblum 	struct file *fp;
1488fe387abSDmitrij Tejblum 	int fd, flags;
1498fe387abSDmitrij Tejblum 	void *buf;
1508fe387abSDmitrij Tejblum 	size_t nbyte;
1518fe387abSDmitrij Tejblum 	off_t offset;
1528fe387abSDmitrij Tejblum {
1534160ccd9SAlan Cox 	struct uio auio;
1544160ccd9SAlan Cox 	struct iovec aiov;
1554160ccd9SAlan Cox 	long cnt, error = 0;
1564160ccd9SAlan Cox #ifdef KTRACE
1574160ccd9SAlan Cox 	struct iovec ktriov;
1584160ccd9SAlan Cox #endif
1594160ccd9SAlan Cox 
1608fe387abSDmitrij Tejblum 	aiov.iov_base = (caddr_t)buf;
1618fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
1624160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1634160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
1648fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
1658fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
1664160ccd9SAlan Cox 		return (EINVAL);
1678fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
1684160ccd9SAlan Cox 	auio.uio_rw = UIO_READ;
1694160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
1704160ccd9SAlan Cox 	auio.uio_procp = p;
1714160ccd9SAlan Cox #ifdef KTRACE
1724160ccd9SAlan Cox 	/*
1734160ccd9SAlan Cox 	 * if tracing, save a copy of iovec
1744160ccd9SAlan Cox 	 */
1754160ccd9SAlan Cox 	if (KTRPOINT(p, KTR_GENIO))
1764160ccd9SAlan Cox 		ktriov = aiov;
1774160ccd9SAlan Cox #endif
1788fe387abSDmitrij Tejblum 	cnt = nbyte;
17913ccadd4SBrian Feldman 	if ((error = fo_read(fp, &auio, fp->f_cred, flags, p)))
1804160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
1814160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
1824160ccd9SAlan Cox 			error = 0;
1834160ccd9SAlan Cox 	cnt -= auio.uio_resid;
1844160ccd9SAlan Cox #ifdef KTRACE
1854160ccd9SAlan Cox 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
1868fe387abSDmitrij Tejblum 		ktrgenio(p->p_tracep, fd, UIO_READ, &ktriov, cnt, error);
1874160ccd9SAlan Cox #endif
1884160ccd9SAlan Cox 	p->p_retval[0] = cnt;
1894160ccd9SAlan Cox 	return (error);
1904160ccd9SAlan Cox }
1914160ccd9SAlan Cox 
1924160ccd9SAlan Cox /*
193df8bae1dSRodney W. Grimes  * Scatter read system call.
194df8bae1dSRodney W. Grimes  */
195d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
196df8bae1dSRodney W. Grimes struct readv_args {
1977147b19dSBruce Evans 	int	fd;
198df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
199df8bae1dSRodney W. Grimes 	u_int	iovcnt;
200df8bae1dSRodney W. Grimes };
201d2d3e875SBruce Evans #endif
20226f9a767SRodney W. Grimes int
203cb226aaaSPoul-Henning Kamp readv(p, uap)
204df8bae1dSRodney W. Grimes 	struct proc *p;
205df8bae1dSRodney W. Grimes 	register struct readv_args *uap;
206df8bae1dSRodney W. Grimes {
207df8bae1dSRodney W. Grimes 	register struct file *fp;
208df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
209df8bae1dSRodney W. Grimes 	struct uio auio;
210df8bae1dSRodney W. Grimes 	register struct iovec *iov;
211df8bae1dSRodney W. Grimes 	struct iovec *needfree;
212df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
213df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
214df8bae1dSRodney W. Grimes 	u_int iovlen;
215df8bae1dSRodney W. Grimes #ifdef KTRACE
216df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
217df8bae1dSRodney W. Grimes #endif
218df8bae1dSRodney W. Grimes 
2198fe387abSDmitrij Tejblum 	if ((fp = getfp(fdp, uap->fd, FREAD)) == NULL)
220df8bae1dSRodney W. Grimes 		return (EBADF);
221df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
222df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
223df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
224df8bae1dSRodney W. Grimes 		if (uap->iovcnt > UIO_MAXIOV)
225df8bae1dSRodney W. Grimes 			return (EINVAL);
226df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
227df8bae1dSRodney W. Grimes 		needfree = iov;
228df8bae1dSRodney W. Grimes 	} else {
229df8bae1dSRodney W. Grimes 		iov = aiov;
230df8bae1dSRodney W. Grimes 		needfree = NULL;
231df8bae1dSRodney W. Grimes 	}
232df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
233df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
234df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
235df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
236df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
2372c1011f7SJohn Dyson 	auio.uio_offset = -1;
238bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
239df8bae1dSRodney W. Grimes 		goto done;
240df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
241df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
242069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
243df8bae1dSRodney W. Grimes 			error = EINVAL;
244df8bae1dSRodney W. Grimes 			goto done;
245df8bae1dSRodney W. Grimes 		}
246069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
247df8bae1dSRodney W. Grimes 		iov++;
248df8bae1dSRodney W. Grimes 	}
249df8bae1dSRodney W. Grimes #ifdef KTRACE
250df8bae1dSRodney W. Grimes 	/*
251df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
252df8bae1dSRodney W. Grimes 	 */
253df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
254df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
255df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
256df8bae1dSRodney W. Grimes 	}
257df8bae1dSRodney W. Grimes #endif
258df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
25913ccadd4SBrian Feldman 	if ((error = fo_read(fp, &auio, fp->f_cred, 0, p)))
260df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
261df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
262df8bae1dSRodney W. Grimes 			error = 0;
263df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
264df8bae1dSRodney W. Grimes #ifdef KTRACE
265df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
266df8bae1dSRodney W. Grimes 		if (error == 0)
2677147b19dSBruce Evans 			ktrgenio(p->p_tracep, uap->fd, UIO_READ, ktriov,
268df8bae1dSRodney W. Grimes 			    cnt, error);
269df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
270df8bae1dSRodney W. Grimes 	}
271df8bae1dSRodney W. Grimes #endif
272cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = cnt;
273df8bae1dSRodney W. Grimes done:
274df8bae1dSRodney W. Grimes 	if (needfree)
275df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
276df8bae1dSRodney W. Grimes 	return (error);
277df8bae1dSRodney W. Grimes }
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes /*
280df8bae1dSRodney W. Grimes  * Write system call
281df8bae1dSRodney W. Grimes  */
282d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
283df8bae1dSRodney W. Grimes struct write_args {
284df8bae1dSRodney W. Grimes 	int	fd;
285134e06feSBruce Evans 	const void *buf;
286134e06feSBruce Evans 	size_t	nbyte;
287df8bae1dSRodney W. Grimes };
288d2d3e875SBruce Evans #endif
28926f9a767SRodney W. Grimes int
290cb226aaaSPoul-Henning Kamp write(p, uap)
291df8bae1dSRodney W. Grimes 	struct proc *p;
292df8bae1dSRodney W. Grimes 	register struct write_args *uap;
293df8bae1dSRodney W. Grimes {
294df8bae1dSRodney W. Grimes 	register struct file *fp;
295df8bae1dSRodney W. Grimes 
2968fe387abSDmitrij Tejblum 	if ((fp = getfp(p->p_fd, uap->fd, FWRITE)) == NULL)
297df8bae1dSRodney W. Grimes 		return (EBADF);
2988fe387abSDmitrij Tejblum 	return (dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0));
299df8bae1dSRodney W. Grimes }
300df8bae1dSRodney W. Grimes 
301df8bae1dSRodney W. Grimes /*
3028fe387abSDmitrij Tejblum  * Pwrite system call
3034160ccd9SAlan Cox  */
3044160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3054160ccd9SAlan Cox struct pwrite_args {
3064160ccd9SAlan Cox 	int	fd;
3074160ccd9SAlan Cox 	const void *buf;
3084160ccd9SAlan Cox 	size_t	nbyte;
3098fe387abSDmitrij Tejblum 	int	pad;
3104160ccd9SAlan Cox 	off_t	offset;
3114160ccd9SAlan Cox };
3124160ccd9SAlan Cox #endif
3134160ccd9SAlan Cox int
3144160ccd9SAlan Cox pwrite(p, uap)
3154160ccd9SAlan Cox 	struct proc *p;
3164160ccd9SAlan Cox 	register struct pwrite_args *uap;
3174160ccd9SAlan Cox {
3184160ccd9SAlan Cox 	register struct file *fp;
3198fe387abSDmitrij Tejblum 
3208fe387abSDmitrij Tejblum 	if ((fp = getfp(p->p_fd, uap->fd, FWRITE)) == NULL)
3218fe387abSDmitrij Tejblum 		return (EBADF);
3228fe387abSDmitrij Tejblum 	if (fp->f_type != DTYPE_VNODE)
3238fe387abSDmitrij Tejblum 		return (ESPIPE);
3248fe387abSDmitrij Tejblum 	return (dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, uap->offset,
3258fe387abSDmitrij Tejblum 	    FOF_OFFSET));
3268fe387abSDmitrij Tejblum }
3278fe387abSDmitrij Tejblum 
3288fe387abSDmitrij Tejblum static int
3298fe387abSDmitrij Tejblum dofilewrite(p, fp, fd, buf, nbyte, offset, flags)
3308fe387abSDmitrij Tejblum 	struct proc *p;
3318fe387abSDmitrij Tejblum 	struct file *fp;
3328fe387abSDmitrij Tejblum 	int fd, flags;
3338fe387abSDmitrij Tejblum 	const void *buf;
3348fe387abSDmitrij Tejblum 	size_t nbyte;
3358fe387abSDmitrij Tejblum 	off_t offset;
3368fe387abSDmitrij Tejblum {
3374160ccd9SAlan Cox 	struct uio auio;
3384160ccd9SAlan Cox 	struct iovec aiov;
3394160ccd9SAlan Cox 	long cnt, error = 0;
3404160ccd9SAlan Cox #ifdef KTRACE
3414160ccd9SAlan Cox 	struct iovec ktriov;
3424160ccd9SAlan Cox #endif
3434160ccd9SAlan Cox 
3448fe387abSDmitrij Tejblum 	aiov.iov_base = (void *)buf;
3458fe387abSDmitrij Tejblum 	aiov.iov_len = nbyte;
3464160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3474160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
3488fe387abSDmitrij Tejblum 	auio.uio_offset = offset;
3498fe387abSDmitrij Tejblum 	if (nbyte > INT_MAX)
3504160ccd9SAlan Cox 		return (EINVAL);
3518fe387abSDmitrij Tejblum 	auio.uio_resid = nbyte;
3524160ccd9SAlan Cox 	auio.uio_rw = UIO_WRITE;
3534160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
3544160ccd9SAlan Cox 	auio.uio_procp = p;
3554160ccd9SAlan Cox #ifdef KTRACE
3564160ccd9SAlan Cox 	/*
3574160ccd9SAlan Cox 	 * if tracing, save a copy of iovec
3584160ccd9SAlan Cox 	 */
3594160ccd9SAlan Cox 	if (KTRPOINT(p, KTR_GENIO))
3604160ccd9SAlan Cox 		ktriov = aiov;
3614160ccd9SAlan Cox #endif
3628fe387abSDmitrij Tejblum 	cnt = nbyte;
36313ccadd4SBrian Feldman 	if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
3644160ccd9SAlan Cox 		if (auio.uio_resid != cnt && (error == ERESTART ||
3654160ccd9SAlan Cox 		    error == EINTR || error == EWOULDBLOCK))
3664160ccd9SAlan Cox 			error = 0;
3674160ccd9SAlan Cox 		if (error == EPIPE)
3684160ccd9SAlan Cox 			psignal(p, SIGPIPE);
3694160ccd9SAlan Cox 	}
3704160ccd9SAlan Cox 	cnt -= auio.uio_resid;
3714160ccd9SAlan Cox #ifdef KTRACE
3724160ccd9SAlan Cox 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
3738fe387abSDmitrij Tejblum 		ktrgenio(p->p_tracep, fd, UIO_WRITE,
3744160ccd9SAlan Cox 		    &ktriov, cnt, error);
3754160ccd9SAlan Cox #endif
3764160ccd9SAlan Cox 	p->p_retval[0] = cnt;
3774160ccd9SAlan Cox 	return (error);
3784160ccd9SAlan Cox }
3794160ccd9SAlan Cox 
3804160ccd9SAlan Cox /*
381df8bae1dSRodney W. Grimes  * Gather write system call
382df8bae1dSRodney W. Grimes  */
383d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
384df8bae1dSRodney W. Grimes struct writev_args {
385df8bae1dSRodney W. Grimes 	int	fd;
386df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
387df8bae1dSRodney W. Grimes 	u_int	iovcnt;
388df8bae1dSRodney W. Grimes };
389d2d3e875SBruce Evans #endif
39026f9a767SRodney W. Grimes int
391cb226aaaSPoul-Henning Kamp writev(p, uap)
392df8bae1dSRodney W. Grimes 	struct proc *p;
393df8bae1dSRodney W. Grimes 	register struct writev_args *uap;
394df8bae1dSRodney W. Grimes {
395df8bae1dSRodney W. Grimes 	register struct file *fp;
396df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
397df8bae1dSRodney W. Grimes 	struct uio auio;
398df8bae1dSRodney W. Grimes 	register struct iovec *iov;
399df8bae1dSRodney W. Grimes 	struct iovec *needfree;
400df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
401df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
402df8bae1dSRodney W. Grimes 	u_int iovlen;
403df8bae1dSRodney W. Grimes #ifdef KTRACE
404df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
405df8bae1dSRodney W. Grimes #endif
406df8bae1dSRodney W. Grimes 
4078fe387abSDmitrij Tejblum 	if ((fp = getfp(fdp, uap->fd, FWRITE)) == NULL)
408df8bae1dSRodney W. Grimes 		return (EBADF);
409df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
410df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
411df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
412df8bae1dSRodney W. Grimes 		if (uap->iovcnt > UIO_MAXIOV)
413df8bae1dSRodney W. Grimes 			return (EINVAL);
414df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
415df8bae1dSRodney W. Grimes 		needfree = iov;
416df8bae1dSRodney W. Grimes 	} else {
417df8bae1dSRodney W. Grimes 		iov = aiov;
418df8bae1dSRodney W. Grimes 		needfree = NULL;
419df8bae1dSRodney W. Grimes 	}
420df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
421df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
422df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
423df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
424df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
4252c1011f7SJohn Dyson 	auio.uio_offset = -1;
426bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
427df8bae1dSRodney W. Grimes 		goto done;
428df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
429df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
430069e9bc1SDoug Rabson 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
431df8bae1dSRodney W. Grimes 			error = EINVAL;
432df8bae1dSRodney W. Grimes 			goto done;
433df8bae1dSRodney W. Grimes 		}
434069e9bc1SDoug Rabson 		auio.uio_resid += iov->iov_len;
435df8bae1dSRodney W. Grimes 		iov++;
436df8bae1dSRodney W. Grimes 	}
437df8bae1dSRodney W. Grimes #ifdef KTRACE
438df8bae1dSRodney W. Grimes 	/*
439df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
440df8bae1dSRodney W. Grimes 	 */
441df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
442df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
443df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
444df8bae1dSRodney W. Grimes 	}
445df8bae1dSRodney W. Grimes #endif
446df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
44713ccadd4SBrian Feldman 	if ((error = fo_write(fp, &auio, fp->f_cred, 0, p))) {
448df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
449df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
450df8bae1dSRodney W. Grimes 			error = 0;
451df8bae1dSRodney W. Grimes 		if (error == EPIPE)
452df8bae1dSRodney W. Grimes 			psignal(p, SIGPIPE);
453df8bae1dSRodney W. Grimes 	}
454df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
455df8bae1dSRodney W. Grimes #ifdef KTRACE
456df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
457df8bae1dSRodney W. Grimes 		if (error == 0)
458df8bae1dSRodney W. Grimes 			ktrgenio(p->p_tracep, uap->fd, UIO_WRITE,
459df8bae1dSRodney W. Grimes 				ktriov, cnt, error);
460df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
461df8bae1dSRodney W. Grimes 	}
462df8bae1dSRodney W. Grimes #endif
463cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = cnt;
464df8bae1dSRodney W. Grimes done:
465df8bae1dSRodney W. Grimes 	if (needfree)
466df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
467df8bae1dSRodney W. Grimes 	return (error);
468df8bae1dSRodney W. Grimes }
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes /*
471df8bae1dSRodney W. Grimes  * Ioctl system call
472df8bae1dSRodney W. Grimes  */
473d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
474df8bae1dSRodney W. Grimes struct ioctl_args {
475df8bae1dSRodney W. Grimes 	int	fd;
476069e9bc1SDoug Rabson 	u_long	com;
477df8bae1dSRodney W. Grimes 	caddr_t	data;
478df8bae1dSRodney W. Grimes };
479d2d3e875SBruce Evans #endif
480df8bae1dSRodney W. Grimes /* ARGSUSED */
48126f9a767SRodney W. Grimes int
482cb226aaaSPoul-Henning Kamp ioctl(p, uap)
483df8bae1dSRodney W. Grimes 	struct proc *p;
484df8bae1dSRodney W. Grimes 	register struct ioctl_args *uap;
485df8bae1dSRodney W. Grimes {
486df8bae1dSRodney W. Grimes 	register struct file *fp;
487df8bae1dSRodney W. Grimes 	register struct filedesc *fdp;
488831b9ef2SDoug Rabson 	register u_long com;
489831b9ef2SDoug Rabson 	int error;
490df8bae1dSRodney W. Grimes 	register u_int size;
491df8bae1dSRodney W. Grimes 	caddr_t data, memp;
492df8bae1dSRodney W. Grimes 	int tmp;
493df8bae1dSRodney W. Grimes #define STK_PARAMS	128
494df8bae1dSRodney W. Grimes 	char stkbuf[STK_PARAMS];
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes 	fdp = p->p_fd;
497df8bae1dSRodney W. Grimes 	if ((u_int)uap->fd >= fdp->fd_nfiles ||
498df8bae1dSRodney W. Grimes 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
499df8bae1dSRodney W. Grimes 		return (EBADF);
500df8bae1dSRodney W. Grimes 
501df8bae1dSRodney W. Grimes 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
502df8bae1dSRodney W. Grimes 		return (EBADF);
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
505df8bae1dSRodney W. Grimes 	case FIONCLEX:
506df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
507df8bae1dSRodney W. Grimes 		return (0);
508df8bae1dSRodney W. Grimes 	case FIOCLEX:
509df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
510df8bae1dSRodney W. Grimes 		return (0);
511df8bae1dSRodney W. Grimes 	}
512df8bae1dSRodney W. Grimes 
513df8bae1dSRodney W. Grimes 	/*
514df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
515df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
516df8bae1dSRodney W. Grimes 	 */
517df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
518df8bae1dSRodney W. Grimes 	if (size > IOCPARM_MAX)
519df8bae1dSRodney W. Grimes 		return (ENOTTY);
520df8bae1dSRodney W. Grimes 	memp = NULL;
521df8bae1dSRodney W. Grimes 	if (size > sizeof (stkbuf)) {
522df8bae1dSRodney W. Grimes 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
523df8bae1dSRodney W. Grimes 		data = memp;
524df8bae1dSRodney W. Grimes 	} else
525df8bae1dSRodney W. Grimes 		data = stkbuf;
526df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
527df8bae1dSRodney W. Grimes 		if (size) {
528df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
529df8bae1dSRodney W. Grimes 			if (error) {
530df8bae1dSRodney W. Grimes 				if (memp)
531df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
532df8bae1dSRodney W. Grimes 				return (error);
533df8bae1dSRodney W. Grimes 			}
534df8bae1dSRodney W. Grimes 		} else
535df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
536df8bae1dSRodney W. Grimes 	} else if ((com&IOC_OUT) && size)
537df8bae1dSRodney W. Grimes 		/*
538df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
539df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
540df8bae1dSRodney W. Grimes 		 */
541df8bae1dSRodney W. Grimes 		bzero(data, size);
542df8bae1dSRodney W. Grimes 	else if (com&IOC_VOID)
543df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
544df8bae1dSRodney W. Grimes 
545df8bae1dSRodney W. Grimes 	switch (com) {
546df8bae1dSRodney W. Grimes 
547df8bae1dSRodney W. Grimes 	case FIONBIO:
548bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
549df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
550df8bae1dSRodney W. Grimes 		else
551df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
55213ccadd4SBrian Feldman 		error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
553df8bae1dSRodney W. Grimes 		break;
554df8bae1dSRodney W. Grimes 
555df8bae1dSRodney W. Grimes 	case FIOASYNC:
556bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
557df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
558df8bae1dSRodney W. Grimes 		else
559df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
56013ccadd4SBrian Feldman 		error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
561df8bae1dSRodney W. Grimes 		break;
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 	default:
56413ccadd4SBrian Feldman 		error = fo_ioctl(fp, com, data, p);
565df8bae1dSRodney W. Grimes 		/*
566df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
567df8bae1dSRodney W. Grimes 		 * already set and checked above.
568df8bae1dSRodney W. Grimes 		 */
569df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
570df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
571df8bae1dSRodney W. Grimes 		break;
572df8bae1dSRodney W. Grimes 	}
573df8bae1dSRodney W. Grimes 	if (memp)
574df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
575df8bae1dSRodney W. Grimes 	return (error);
576df8bae1dSRodney W. Grimes }
577df8bae1dSRodney W. Grimes 
57887b6de2bSPoul-Henning Kamp static int	nselcoll;
57987b6de2bSPoul-Henning Kamp int	selwait;
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes /*
582df8bae1dSRodney W. Grimes  * Select system call.
583df8bae1dSRodney W. Grimes  */
584d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
585df8bae1dSRodney W. Grimes struct select_args {
586b08f7993SSujal Patel 	int	nd;
587df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
588df8bae1dSRodney W. Grimes 	struct	timeval *tv;
589df8bae1dSRodney W. Grimes };
590d2d3e875SBruce Evans #endif
59126f9a767SRodney W. Grimes int
592cb226aaaSPoul-Henning Kamp select(p, uap)
593df8bae1dSRodney W. Grimes 	register struct proc *p;
594df8bae1dSRodney W. Grimes 	register struct select_args *uap;
595df8bae1dSRodney W. Grimes {
596d5e4d7e1SBruce Evans 	/*
597d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
598d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
599d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
600d5e4d7e1SBruce Evans 	 * of 256.
601d5e4d7e1SBruce Evans 	 */
602d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
603d5e4d7e1SBruce Evans 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
60400af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
60500af9731SPoul-Henning Kamp 	int s, ncoll, error, timo;
606d5e4d7e1SBruce Evans 	u_int nbufbytes, ncpbytes, nfdbits;
607df8bae1dSRodney W. Grimes 
608b08f7993SSujal Patel 	if (uap->nd < 0)
609acbfbfeaSSujal Patel 		return (EINVAL);
610df8bae1dSRodney W. Grimes 	if (uap->nd > p->p_fd->fd_nfiles)
611df8bae1dSRodney W. Grimes 		uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
612b08f7993SSujal Patel 
613d5e4d7e1SBruce Evans 	/*
614d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
615d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
616d5e4d7e1SBruce Evans 	 */
617d5e4d7e1SBruce Evans 	nfdbits = roundup(uap->nd, NFDBITS);
618d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
619d5e4d7e1SBruce Evans 	nbufbytes = 0;
620d5e4d7e1SBruce Evans 	if (uap->in != NULL)
621d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
622d5e4d7e1SBruce Evans 	if (uap->ou != NULL)
623d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
624d5e4d7e1SBruce Evans 	if (uap->ex != NULL)
625d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
626d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
627d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
628d5e4d7e1SBruce Evans 	else
629d5e4d7e1SBruce Evans 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
630b08f7993SSujal Patel 
631b08f7993SSujal Patel 	/*
632d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
633d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
634d5e4d7e1SBruce Evans 	 * together.
635b08f7993SSujal Patel 	 */
636d5e4d7e1SBruce Evans 	sbp = selbits;
637df8bae1dSRodney W. Grimes #define	getbits(name, x) \
638d5e4d7e1SBruce Evans 	do {								\
639d5e4d7e1SBruce Evans 		if (uap->name == NULL)					\
640d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
641d5e4d7e1SBruce Evans 		else {							\
642d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
643d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
644d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
645d5e4d7e1SBruce Evans 			error = copyin(uap->name, ibits[x], ncpbytes);	\
646d5e4d7e1SBruce Evans 			if (error != 0)					\
647d5e4d7e1SBruce Evans 				goto done;				\
648d5e4d7e1SBruce Evans 		}							\
649d5e4d7e1SBruce Evans 	} while (0)
650df8bae1dSRodney W. Grimes 	getbits(in, 0);
651df8bae1dSRodney W. Grimes 	getbits(ou, 1);
652df8bae1dSRodney W. Grimes 	getbits(ex, 2);
653df8bae1dSRodney W. Grimes #undef	getbits
654d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
655d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	if (uap->tv) {
658df8bae1dSRodney W. Grimes 		error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
659df8bae1dSRodney W. Grimes 			sizeof (atv));
660df8bae1dSRodney W. Grimes 		if (error)
661df8bae1dSRodney W. Grimes 			goto done;
662df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
663df8bae1dSRodney W. Grimes 			error = EINVAL;
664df8bae1dSRodney W. Grimes 			goto done;
665df8bae1dSRodney W. Grimes 		}
666c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
66700af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
66880a39463SAndrey A. Chernov 	} else
66900af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
67000af9731SPoul-Henning Kamp 	timo = 0;
671df8bae1dSRodney W. Grimes retry:
672df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
673df8bae1dSRodney W. Grimes 	p->p_flag |= P_SELECT;
674cb226aaaSPoul-Henning Kamp 	error = selscan(p, ibits, obits, uap->nd);
675cb226aaaSPoul-Henning Kamp 	if (error || p->p_retval[0])
676df8bae1dSRodney W. Grimes 		goto done;
67700af9731SPoul-Henning Kamp 	if (atv.tv_sec) {
678c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
67900af9731SPoul-Henning Kamp 		if (timevalcmp(&rtv, &atv, >=))
680df8bae1dSRodney W. Grimes 			goto done;
68100af9731SPoul-Henning Kamp 		ttv = atv;
68200af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
68300af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
68400af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
685df8bae1dSRodney W. Grimes 	}
68600af9731SPoul-Henning Kamp 	s = splhigh();
687df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
688df8bae1dSRodney W. Grimes 		splx(s);
689df8bae1dSRodney W. Grimes 		goto retry;
690df8bae1dSRodney W. Grimes 	}
691df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
692df8bae1dSRodney W. Grimes 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
693df8bae1dSRodney W. Grimes 	splx(s);
694df8bae1dSRodney W. Grimes 	if (error == 0)
695df8bae1dSRodney W. Grimes 		goto retry;
696df8bae1dSRodney W. Grimes done:
697df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
698df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
699df8bae1dSRodney W. Grimes 	if (error == ERESTART)
700df8bae1dSRodney W. Grimes 		error = EINTR;
701df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
702df8bae1dSRodney W. Grimes 		error = 0;
703df8bae1dSRodney W. Grimes #define	putbits(name, x) \
704d5e4d7e1SBruce Evans 	if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
705df8bae1dSRodney W. Grimes 		error = error2;
706df8bae1dSRodney W. Grimes 	if (error == 0) {
707df8bae1dSRodney W. Grimes 		int error2;
708df8bae1dSRodney W. Grimes 
709df8bae1dSRodney W. Grimes 		putbits(in, 0);
710df8bae1dSRodney W. Grimes 		putbits(ou, 1);
711df8bae1dSRodney W. Grimes 		putbits(ex, 2);
712df8bae1dSRodney W. Grimes #undef putbits
713df8bae1dSRodney W. Grimes 	}
714d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
715d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
716df8bae1dSRodney W. Grimes 	return (error);
717df8bae1dSRodney W. Grimes }
718df8bae1dSRodney W. Grimes 
71987b6de2bSPoul-Henning Kamp static int
720cb226aaaSPoul-Henning Kamp selscan(p, ibits, obits, nfd)
721df8bae1dSRodney W. Grimes 	struct proc *p;
722b08f7993SSujal Patel 	fd_mask **ibits, **obits;
723cb226aaaSPoul-Henning Kamp 	int nfd;
724df8bae1dSRodney W. Grimes {
725df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
726df8bae1dSRodney W. Grimes 	register int msk, i, j, fd;
727df8bae1dSRodney W. Grimes 	register fd_mask bits;
728df8bae1dSRodney W. Grimes 	struct file *fp;
729df8bae1dSRodney W. Grimes 	int n = 0;
7302087c896SBruce Evans 	/* Note: backend also returns POLLHUP/POLLERR if appropriate. */
73142d11757SPeter Wemm 	static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
732df8bae1dSRodney W. Grimes 
733df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
734d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
735d5e4d7e1SBruce Evans 			continue;
736df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
737b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
738df8bae1dSRodney W. Grimes 			while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
739df8bae1dSRodney W. Grimes 				bits &= ~(1 << j);
740df8bae1dSRodney W. Grimes 				fp = fdp->fd_ofiles[fd];
741df8bae1dSRodney W. Grimes 				if (fp == NULL)
742df8bae1dSRodney W. Grimes 					return (EBADF);
74313ccadd4SBrian Feldman 				if (fo_poll(fp, flag[msk], fp->f_cred, p)) {
744b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
745b08f7993SSujal Patel 						(1 << ((fd) % NFDBITS));
746df8bae1dSRodney W. Grimes 					n++;
747df8bae1dSRodney W. Grimes 				}
748df8bae1dSRodney W. Grimes 			}
749df8bae1dSRodney W. Grimes 		}
750df8bae1dSRodney W. Grimes 	}
751cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = n;
752df8bae1dSRodney W. Grimes 	return (0);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
75542d11757SPeter Wemm /*
75642d11757SPeter Wemm  * Poll system call.
75742d11757SPeter Wemm  */
75842d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
75942d11757SPeter Wemm struct poll_args {
76042d11757SPeter Wemm 	struct pollfd *fds;
76142d11757SPeter Wemm 	u_int	nfds;
76242d11757SPeter Wemm 	int	timeout;
76342d11757SPeter Wemm };
76442d11757SPeter Wemm #endif
76542d11757SPeter Wemm int
766cb226aaaSPoul-Henning Kamp poll(p, uap)
76742d11757SPeter Wemm 	register struct proc *p;
76842d11757SPeter Wemm 	register struct poll_args *uap;
76942d11757SPeter Wemm {
77042d11757SPeter Wemm 	caddr_t bits;
77142d11757SPeter Wemm 	char smallbits[32 * sizeof(struct pollfd)];
77200af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
77300af9731SPoul-Henning Kamp 	int s, ncoll, error = 0, timo;
77442d11757SPeter Wemm 	size_t ni;
77542d11757SPeter Wemm 
77642d11757SPeter Wemm 	if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
77742d11757SPeter Wemm 		/* forgiving; slightly wrong */
77842d11757SPeter Wemm 		SCARG(uap, nfds) = p->p_fd->fd_nfiles;
77942d11757SPeter Wemm 	}
78042d11757SPeter Wemm 	ni = SCARG(uap, nfds) * sizeof(struct pollfd);
78142d11757SPeter Wemm 	if (ni > sizeof(smallbits))
78242d11757SPeter Wemm 		bits = malloc(ni, M_TEMP, M_WAITOK);
78342d11757SPeter Wemm 	else
78442d11757SPeter Wemm 		bits = smallbits;
78542d11757SPeter Wemm 	error = copyin(SCARG(uap, fds), bits, ni);
78642d11757SPeter Wemm 	if (error)
78742d11757SPeter Wemm 		goto done;
78842d11757SPeter Wemm 	if (SCARG(uap, timeout) != INFTIM) {
78942d11757SPeter Wemm 		atv.tv_sec = SCARG(uap, timeout) / 1000;
79042d11757SPeter Wemm 		atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
79142d11757SPeter Wemm 		if (itimerfix(&atv)) {
79242d11757SPeter Wemm 			error = EINVAL;
79342d11757SPeter Wemm 			goto done;
79442d11757SPeter Wemm 		}
795c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
79600af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
79780a39463SAndrey A. Chernov 	} else
79800af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
79900af9731SPoul-Henning Kamp 	timo = 0;
80042d11757SPeter Wemm retry:
80142d11757SPeter Wemm 	ncoll = nselcoll;
80242d11757SPeter Wemm 	p->p_flag |= P_SELECT;
803cb226aaaSPoul-Henning Kamp 	error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds));
804cb226aaaSPoul-Henning Kamp 	if (error || p->p_retval[0])
80542d11757SPeter Wemm 		goto done;
80600af9731SPoul-Henning Kamp 	if (atv.tv_sec) {
807c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
80800af9731SPoul-Henning Kamp 		if (timevalcmp(&rtv, &atv, >=))
80942d11757SPeter Wemm 			goto done;
81000af9731SPoul-Henning Kamp 		ttv = atv;
81100af9731SPoul-Henning Kamp 		timevalsub(&ttv, &rtv);
81200af9731SPoul-Henning Kamp 		timo = ttv.tv_sec > 24 * 60 * 60 ?
81300af9731SPoul-Henning Kamp 		    24 * 60 * 60 * hz : tvtohz(&ttv);
81442d11757SPeter Wemm 	}
81500af9731SPoul-Henning Kamp 	s = splhigh();
81642d11757SPeter Wemm 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
81742d11757SPeter Wemm 		splx(s);
81842d11757SPeter Wemm 		goto retry;
81942d11757SPeter Wemm 	}
82042d11757SPeter Wemm 	p->p_flag &= ~P_SELECT;
82142d11757SPeter Wemm 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
82242d11757SPeter Wemm 	splx(s);
82342d11757SPeter Wemm 	if (error == 0)
82442d11757SPeter Wemm 		goto retry;
82542d11757SPeter Wemm done:
82642d11757SPeter Wemm 	p->p_flag &= ~P_SELECT;
82742d11757SPeter Wemm 	/* poll is not restarted after signals... */
82842d11757SPeter Wemm 	if (error == ERESTART)
82942d11757SPeter Wemm 		error = EINTR;
83042d11757SPeter Wemm 	if (error == EWOULDBLOCK)
83142d11757SPeter Wemm 		error = 0;
83242d11757SPeter Wemm 	if (error == 0) {
83342d11757SPeter Wemm 		error = copyout(bits, SCARG(uap, fds), ni);
83442d11757SPeter Wemm 		if (error)
83542d11757SPeter Wemm 			goto out;
83642d11757SPeter Wemm 	}
83742d11757SPeter Wemm out:
83842d11757SPeter Wemm 	if (ni > sizeof(smallbits))
83942d11757SPeter Wemm 		free(bits, M_TEMP);
84042d11757SPeter Wemm 	return (error);
84142d11757SPeter Wemm }
84242d11757SPeter Wemm 
84342d11757SPeter Wemm static int
844cb226aaaSPoul-Henning Kamp pollscan(p, fds, nfd)
84542d11757SPeter Wemm 	struct proc *p;
84642d11757SPeter Wemm 	struct pollfd *fds;
84742d11757SPeter Wemm 	int nfd;
84842d11757SPeter Wemm {
84942d11757SPeter Wemm 	register struct filedesc *fdp = p->p_fd;
85042d11757SPeter Wemm 	int i;
85142d11757SPeter Wemm 	struct file *fp;
85242d11757SPeter Wemm 	int n = 0;
85342d11757SPeter Wemm 
85442d11757SPeter Wemm 	for (i = 0; i < nfd; i++, fds++) {
855337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
85642d11757SPeter Wemm 			fds->revents = POLLNVAL;
85742d11757SPeter Wemm 			n++;
858337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
859337c9691SJordan K. Hubbard 			fds->revents = 0;
86042d11757SPeter Wemm 		} else {
86142d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
86242d11757SPeter Wemm 			if (fp == 0) {
86342d11757SPeter Wemm 				fds->revents = POLLNVAL;
86442d11757SPeter Wemm 				n++;
86542d11757SPeter Wemm 			} else {
8662087c896SBruce Evans 				/*
8672087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
8682087c896SBruce Evans 				 * POLLERR if appropriate.
8692087c896SBruce Evans 				 */
87013ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
87113ccadd4SBrian Feldman 				    fp->f_cred, p);
87242d11757SPeter Wemm 				if (fds->revents != 0)
87342d11757SPeter Wemm 					n++;
87442d11757SPeter Wemm 			}
87542d11757SPeter Wemm 		}
87642d11757SPeter Wemm 	}
877cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = n;
87842d11757SPeter Wemm 	return (0);
87942d11757SPeter Wemm }
88042d11757SPeter Wemm 
88142d11757SPeter Wemm /*
88242d11757SPeter Wemm  * OpenBSD poll system call.
88342d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
88442d11757SPeter Wemm  */
88542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
88642d11757SPeter Wemm struct openbsd_poll_args {
88742d11757SPeter Wemm 	struct pollfd *fds;
88842d11757SPeter Wemm 	u_int	nfds;
88942d11757SPeter Wemm 	int	timeout;
89042d11757SPeter Wemm };
89142d11757SPeter Wemm #endif
89242d11757SPeter Wemm int
893cb226aaaSPoul-Henning Kamp openbsd_poll(p, uap)
89442d11757SPeter Wemm 	register struct proc *p;
89542d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
89642d11757SPeter Wemm {
897cb226aaaSPoul-Henning Kamp 	return (poll(p, (struct poll_args *)uap));
89842d11757SPeter Wemm }
89942d11757SPeter Wemm 
900df8bae1dSRodney W. Grimes /*ARGSUSED*/
90126f9a767SRodney W. Grimes int
90242d11757SPeter Wemm seltrue(dev, events, p)
903df8bae1dSRodney W. Grimes 	dev_t dev;
90442d11757SPeter Wemm 	int events;
905df8bae1dSRodney W. Grimes 	struct proc *p;
906df8bae1dSRodney W. Grimes {
907df8bae1dSRodney W. Grimes 
90842d11757SPeter Wemm 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
909df8bae1dSRodney W. Grimes }
910df8bae1dSRodney W. Grimes 
911df8bae1dSRodney W. Grimes /*
912df8bae1dSRodney W. Grimes  * Record a select request.
913df8bae1dSRodney W. Grimes  */
914df8bae1dSRodney W. Grimes void
915df8bae1dSRodney W. Grimes selrecord(selector, sip)
916df8bae1dSRodney W. Grimes 	struct proc *selector;
917df8bae1dSRodney W. Grimes 	struct selinfo *sip;
918df8bae1dSRodney W. Grimes {
919df8bae1dSRodney W. Grimes 	struct proc *p;
920df8bae1dSRodney W. Grimes 	pid_t mypid;
921df8bae1dSRodney W. Grimes 
922df8bae1dSRodney W. Grimes 	mypid = selector->p_pid;
923df8bae1dSRodney W. Grimes 	if (sip->si_pid == mypid)
924df8bae1dSRodney W. Grimes 		return;
925df8bae1dSRodney W. Grimes 	if (sip->si_pid && (p = pfind(sip->si_pid)) &&
926df8bae1dSRodney W. Grimes 	    p->p_wchan == (caddr_t)&selwait)
927df8bae1dSRodney W. Grimes 		sip->si_flags |= SI_COLL;
928df8bae1dSRodney W. Grimes 	else
929df8bae1dSRodney W. Grimes 		sip->si_pid = mypid;
930df8bae1dSRodney W. Grimes }
931df8bae1dSRodney W. Grimes 
932df8bae1dSRodney W. Grimes /*
933df8bae1dSRodney W. Grimes  * Do a wakeup when a selectable event occurs.
934df8bae1dSRodney W. Grimes  */
935df8bae1dSRodney W. Grimes void
936df8bae1dSRodney W. Grimes selwakeup(sip)
937df8bae1dSRodney W. Grimes 	register struct selinfo *sip;
938df8bae1dSRodney W. Grimes {
939df8bae1dSRodney W. Grimes 	register struct proc *p;
940df8bae1dSRodney W. Grimes 	int s;
941df8bae1dSRodney W. Grimes 
942df8bae1dSRodney W. Grimes 	if (sip->si_pid == 0)
943df8bae1dSRodney W. Grimes 		return;
944df8bae1dSRodney W. Grimes 	if (sip->si_flags & SI_COLL) {
945df8bae1dSRodney W. Grimes 		nselcoll++;
946df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
947df8bae1dSRodney W. Grimes 		wakeup((caddr_t)&selwait);
948df8bae1dSRodney W. Grimes 	}
949df8bae1dSRodney W. Grimes 	p = pfind(sip->si_pid);
950df8bae1dSRodney W. Grimes 	sip->si_pid = 0;
951df8bae1dSRodney W. Grimes 	if (p != NULL) {
952df8bae1dSRodney W. Grimes 		s = splhigh();
953df8bae1dSRodney W. Grimes 		if (p->p_wchan == (caddr_t)&selwait) {
954df8bae1dSRodney W. Grimes 			if (p->p_stat == SSLEEP)
955df8bae1dSRodney W. Grimes 				setrunnable(p);
956df8bae1dSRodney W. Grimes 			else
957df8bae1dSRodney W. Grimes 				unsleep(p);
958df8bae1dSRodney W. Grimes 		} else if (p->p_flag & P_SELECT)
959df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_SELECT;
960df8bae1dSRodney W. Grimes 		splx(s);
961df8bae1dSRodney W. Grimes 	}
962df8bae1dSRodney W. Grimes }
963