xref: /freebsd/sys/kern/sys_generic.c (revision 20982410542a3d817a38ea6175574a5dddabc969)
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
3920982410SBruce Evans  * $Id: sys_generic.c,v 1.25 1997/03/23 03:36:23 bde Exp $
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/stat.h>
54797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
55797f2d22SPoul-Henning Kamp #include <sys/socket.h>
56df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
57df8bae1dSRodney W. Grimes #include <sys/uio.h>
58df8bae1dSRodney W. Grimes #include <sys/kernel.h>
59df8bae1dSRodney W. Grimes #include <sys/stat.h>
60df8bae1dSRodney W. Grimes #include <sys/malloc.h>
61df8bae1dSRodney W. Grimes #ifdef KTRACE
62df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
63df8bae1dSRodney W. Grimes #endif
6490324b07SDavid Greenman #include <vm/vm.h>
65df8bae1dSRodney W. Grimes 
66b08f7993SSujal Patel static int	selscan __P((struct proc *, fd_mask **, fd_mask **, int, int *));
67d93f860cSPoul-Henning Kamp 
68df8bae1dSRodney W. Grimes /*
69df8bae1dSRodney W. Grimes  * Read system call.
70df8bae1dSRodney W. Grimes  */
71d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
72df8bae1dSRodney W. Grimes struct read_args {
73df8bae1dSRodney W. Grimes 	int	fd;
74df8bae1dSRodney W. Grimes 	char	*buf;
75df8bae1dSRodney W. Grimes 	u_int	nbyte;
76df8bae1dSRodney W. Grimes };
77d2d3e875SBruce Evans #endif
78df8bae1dSRodney W. Grimes /* ARGSUSED */
7926f9a767SRodney W. Grimes int
80df8bae1dSRodney W. Grimes read(p, uap, retval)
81df8bae1dSRodney W. Grimes 	struct proc *p;
82df8bae1dSRodney W. Grimes 	register struct read_args *uap;
83df8bae1dSRodney W. Grimes 	int *retval;
84df8bae1dSRodney W. Grimes {
85df8bae1dSRodney W. Grimes 	register struct file *fp;
86df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
87df8bae1dSRodney W. Grimes 	struct uio auio;
88df8bae1dSRodney W. Grimes 	struct iovec aiov;
89df8bae1dSRodney W. Grimes 	long cnt, error = 0;
90df8bae1dSRodney W. Grimes #ifdef KTRACE
91df8bae1dSRodney W. Grimes 	struct iovec ktriov;
92df8bae1dSRodney W. Grimes #endif
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes 	if (((u_int)uap->fd) >= fdp->fd_nfiles ||
95df8bae1dSRodney W. Grimes 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
96df8bae1dSRodney W. Grimes 	    (fp->f_flag & FREAD) == 0)
97df8bae1dSRodney W. Grimes 		return (EBADF);
98df8bae1dSRodney W. Grimes 	aiov.iov_base = (caddr_t)uap->buf;
99df8bae1dSRodney W. Grimes 	aiov.iov_len = uap->nbyte;
100df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov;
101df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
102dd968eaeSDavid Greenman 
103df8bae1dSRodney W. Grimes 	auio.uio_resid = uap->nbyte;
104dd968eaeSDavid Greenman 	if (auio.uio_resid < 0)
105dd968eaeSDavid Greenman 		return (EINVAL);
106dd968eaeSDavid Greenman 
107df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
108df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
109df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
110df8bae1dSRodney W. Grimes #ifdef KTRACE
111df8bae1dSRodney W. Grimes 	/*
112df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
113df8bae1dSRodney W. Grimes 	 */
114df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))
115df8bae1dSRodney W. Grimes 		ktriov = aiov;
116df8bae1dSRodney W. Grimes #endif
117df8bae1dSRodney W. Grimes 	cnt = uap->nbyte;
118bb56ec4aSPoul-Henning Kamp 	if ((error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred)))
119df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
120df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
121df8bae1dSRodney W. Grimes 			error = 0;
122df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
123df8bae1dSRodney W. Grimes #ifdef KTRACE
124df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
125df8bae1dSRodney W. Grimes 		ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktriov, cnt, error);
126df8bae1dSRodney W. Grimes #endif
127df8bae1dSRodney W. Grimes 	*retval = cnt;
128df8bae1dSRodney W. Grimes 	return (error);
129df8bae1dSRodney W. Grimes }
130df8bae1dSRodney W. Grimes 
131df8bae1dSRodney W. Grimes /*
132df8bae1dSRodney W. Grimes  * Scatter read system call.
133df8bae1dSRodney W. Grimes  */
134d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
135df8bae1dSRodney W. Grimes struct readv_args {
1367147b19dSBruce Evans 	int	fd;
137df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
138df8bae1dSRodney W. Grimes 	u_int	iovcnt;
139df8bae1dSRodney W. Grimes };
140d2d3e875SBruce Evans #endif
14126f9a767SRodney W. Grimes int
142df8bae1dSRodney W. Grimes readv(p, uap, retval)
143df8bae1dSRodney W. Grimes 	struct proc *p;
144df8bae1dSRodney W. Grimes 	register struct readv_args *uap;
145df8bae1dSRodney W. Grimes 	int *retval;
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 	register struct file *fp;
148df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
149df8bae1dSRodney W. Grimes 	struct uio auio;
150df8bae1dSRodney W. Grimes 	register struct iovec *iov;
151df8bae1dSRodney W. Grimes 	struct iovec *needfree;
152df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
153df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
154df8bae1dSRodney W. Grimes 	u_int iovlen;
155df8bae1dSRodney W. Grimes #ifdef KTRACE
156df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
157df8bae1dSRodney W. Grimes #endif
158df8bae1dSRodney W. Grimes 
1597147b19dSBruce Evans 	if (((u_int)uap->fd) >= fdp->fd_nfiles ||
1607147b19dSBruce Evans 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
161df8bae1dSRodney W. Grimes 	    (fp->f_flag & FREAD) == 0)
162df8bae1dSRodney W. Grimes 		return (EBADF);
163df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
164df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
165df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
166df8bae1dSRodney W. Grimes 		if (uap->iovcnt > UIO_MAXIOV)
167df8bae1dSRodney W. Grimes 			return (EINVAL);
168df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
169df8bae1dSRodney W. Grimes 		needfree = iov;
170df8bae1dSRodney W. Grimes 	} else {
171df8bae1dSRodney W. Grimes 		iov = aiov;
172df8bae1dSRodney W. Grimes 		needfree = NULL;
173df8bae1dSRodney W. Grimes 	}
174df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
175df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
176df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_READ;
177df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
178df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
179bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
180df8bae1dSRodney W. Grimes 		goto done;
181df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
182df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
183df8bae1dSRodney W. Grimes 		auio.uio_resid += iov->iov_len;
184df8bae1dSRodney W. Grimes 		if (auio.uio_resid < 0) {
185df8bae1dSRodney W. Grimes 			error = EINVAL;
186df8bae1dSRodney W. Grimes 			goto done;
187df8bae1dSRodney W. Grimes 		}
188df8bae1dSRodney W. Grimes 		iov++;
189df8bae1dSRodney W. Grimes 	}
190df8bae1dSRodney W. Grimes #ifdef KTRACE
191df8bae1dSRodney W. Grimes 	/*
192df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
193df8bae1dSRodney W. Grimes 	 */
194df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
195df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
196df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
197df8bae1dSRodney W. Grimes 	}
198df8bae1dSRodney W. Grimes #endif
199df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
200bb56ec4aSPoul-Henning Kamp 	if ((error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred)))
201df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
202df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
203df8bae1dSRodney W. Grimes 			error = 0;
204df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
205df8bae1dSRodney W. Grimes #ifdef KTRACE
206df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
207df8bae1dSRodney W. Grimes 		if (error == 0)
2087147b19dSBruce Evans 			ktrgenio(p->p_tracep, uap->fd, UIO_READ, ktriov,
209df8bae1dSRodney W. Grimes 			    cnt, error);
210df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
211df8bae1dSRodney W. Grimes 	}
212df8bae1dSRodney W. Grimes #endif
213df8bae1dSRodney W. Grimes 	*retval = cnt;
214df8bae1dSRodney W. Grimes done:
215df8bae1dSRodney W. Grimes 	if (needfree)
216df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
217df8bae1dSRodney W. Grimes 	return (error);
218df8bae1dSRodney W. Grimes }
219df8bae1dSRodney W. Grimes 
220df8bae1dSRodney W. Grimes /*
221df8bae1dSRodney W. Grimes  * Write system call
222df8bae1dSRodney W. Grimes  */
223d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
224df8bae1dSRodney W. Grimes struct write_args {
225df8bae1dSRodney W. Grimes 	int	fd;
226df8bae1dSRodney W. Grimes 	char	*buf;
227df8bae1dSRodney W. Grimes 	u_int	nbyte;
228df8bae1dSRodney W. Grimes };
229d2d3e875SBruce Evans #endif
23026f9a767SRodney W. Grimes int
231df8bae1dSRodney W. Grimes write(p, uap, retval)
232df8bae1dSRodney W. Grimes 	struct proc *p;
233df8bae1dSRodney W. Grimes 	register struct write_args *uap;
234df8bae1dSRodney W. Grimes 	int *retval;
235df8bae1dSRodney W. Grimes {
236df8bae1dSRodney W. Grimes 	register struct file *fp;
237df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
238df8bae1dSRodney W. Grimes 	struct uio auio;
239df8bae1dSRodney W. Grimes 	struct iovec aiov;
240df8bae1dSRodney W. Grimes 	long cnt, error = 0;
241df8bae1dSRodney W. Grimes #ifdef KTRACE
242df8bae1dSRodney W. Grimes 	struct iovec ktriov;
243df8bae1dSRodney W. Grimes #endif
244df8bae1dSRodney W. Grimes 
245df8bae1dSRodney W. Grimes 	if (((u_int)uap->fd) >= fdp->fd_nfiles ||
246df8bae1dSRodney W. Grimes 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
247df8bae1dSRodney W. Grimes 	    (fp->f_flag & FWRITE) == 0)
248df8bae1dSRodney W. Grimes 		return (EBADF);
249df8bae1dSRodney W. Grimes 	aiov.iov_base = (caddr_t)uap->buf;
250df8bae1dSRodney W. Grimes 	aiov.iov_len = uap->nbyte;
251df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov;
252df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
253df8bae1dSRodney W. Grimes 	auio.uio_resid = uap->nbyte;
254df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
255df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
256df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
257df8bae1dSRodney W. Grimes #ifdef KTRACE
258df8bae1dSRodney W. Grimes 	/*
259df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
260df8bae1dSRodney W. Grimes 	 */
261df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))
262df8bae1dSRodney W. Grimes 		ktriov = aiov;
263df8bae1dSRodney W. Grimes #endif
264df8bae1dSRodney W. Grimes 	cnt = uap->nbyte;
265bb56ec4aSPoul-Henning Kamp 	if ((error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred))) {
266df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
267df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
268df8bae1dSRodney W. Grimes 			error = 0;
269df8bae1dSRodney W. Grimes 		if (error == EPIPE)
270df8bae1dSRodney W. Grimes 			psignal(p, SIGPIPE);
271df8bae1dSRodney W. Grimes 	}
272df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
273df8bae1dSRodney W. Grimes #ifdef KTRACE
274df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
275df8bae1dSRodney W. Grimes 		ktrgenio(p->p_tracep, uap->fd, UIO_WRITE,
276df8bae1dSRodney W. Grimes 		    &ktriov, cnt, error);
277df8bae1dSRodney W. Grimes #endif
278df8bae1dSRodney W. Grimes 	*retval = cnt;
279df8bae1dSRodney W. Grimes 	return (error);
280df8bae1dSRodney W. Grimes }
281df8bae1dSRodney W. Grimes 
282df8bae1dSRodney W. Grimes /*
283df8bae1dSRodney W. Grimes  * Gather write system call
284df8bae1dSRodney W. Grimes  */
285d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
286df8bae1dSRodney W. Grimes struct writev_args {
287df8bae1dSRodney W. Grimes 	int	fd;
288df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
289df8bae1dSRodney W. Grimes 	u_int	iovcnt;
290df8bae1dSRodney W. Grimes };
291d2d3e875SBruce Evans #endif
29226f9a767SRodney W. Grimes int
293df8bae1dSRodney W. Grimes writev(p, uap, retval)
294df8bae1dSRodney W. Grimes 	struct proc *p;
295df8bae1dSRodney W. Grimes 	register struct writev_args *uap;
296df8bae1dSRodney W. Grimes 	int *retval;
297df8bae1dSRodney W. Grimes {
298df8bae1dSRodney W. Grimes 	register struct file *fp;
299df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
300df8bae1dSRodney W. Grimes 	struct uio auio;
301df8bae1dSRodney W. Grimes 	register struct iovec *iov;
302df8bae1dSRodney W. Grimes 	struct iovec *needfree;
303df8bae1dSRodney W. Grimes 	struct iovec aiov[UIO_SMALLIOV];
304df8bae1dSRodney W. Grimes 	long i, cnt, error = 0;
305df8bae1dSRodney W. Grimes 	u_int iovlen;
306df8bae1dSRodney W. Grimes #ifdef KTRACE
307df8bae1dSRodney W. Grimes 	struct iovec *ktriov = NULL;
308df8bae1dSRodney W. Grimes #endif
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes 	if (((u_int)uap->fd) >= fdp->fd_nfiles ||
311df8bae1dSRodney W. Grimes 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
312df8bae1dSRodney W. Grimes 	    (fp->f_flag & FWRITE) == 0)
313df8bae1dSRodney W. Grimes 		return (EBADF);
314df8bae1dSRodney W. Grimes 	/* note: can't use iovlen until iovcnt is validated */
315df8bae1dSRodney W. Grimes 	iovlen = uap->iovcnt * sizeof (struct iovec);
316df8bae1dSRodney W. Grimes 	if (uap->iovcnt > UIO_SMALLIOV) {
317df8bae1dSRodney W. Grimes 		if (uap->iovcnt > UIO_MAXIOV)
318df8bae1dSRodney W. Grimes 			return (EINVAL);
319df8bae1dSRodney W. Grimes 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
320df8bae1dSRodney W. Grimes 		needfree = iov;
321df8bae1dSRodney W. Grimes 	} else {
322df8bae1dSRodney W. Grimes 		iov = aiov;
323df8bae1dSRodney W. Grimes 		needfree = NULL;
324df8bae1dSRodney W. Grimes 	}
325df8bae1dSRodney W. Grimes 	auio.uio_iov = iov;
326df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = uap->iovcnt;
327df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
328df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_USERSPACE;
329df8bae1dSRodney W. Grimes 	auio.uio_procp = p;
330bb56ec4aSPoul-Henning Kamp 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
331df8bae1dSRodney W. Grimes 		goto done;
332df8bae1dSRodney W. Grimes 	auio.uio_resid = 0;
333df8bae1dSRodney W. Grimes 	for (i = 0; i < uap->iovcnt; i++) {
334df8bae1dSRodney W. Grimes 		auio.uio_resid += iov->iov_len;
335df8bae1dSRodney W. Grimes 		if (auio.uio_resid < 0) {
336df8bae1dSRodney W. Grimes 			error = EINVAL;
337df8bae1dSRodney W. Grimes 			goto done;
338df8bae1dSRodney W. Grimes 		}
339df8bae1dSRodney W. Grimes 		iov++;
340df8bae1dSRodney W. Grimes 	}
341df8bae1dSRodney W. Grimes #ifdef KTRACE
342df8bae1dSRodney W. Grimes 	/*
343df8bae1dSRodney W. Grimes 	 * if tracing, save a copy of iovec
344df8bae1dSRodney W. Grimes 	 */
345df8bae1dSRodney W. Grimes 	if (KTRPOINT(p, KTR_GENIO))  {
346df8bae1dSRodney W. Grimes 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
347df8bae1dSRodney W. Grimes 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
348df8bae1dSRodney W. Grimes 	}
349df8bae1dSRodney W. Grimes #endif
350df8bae1dSRodney W. Grimes 	cnt = auio.uio_resid;
351bb56ec4aSPoul-Henning Kamp 	if ((error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred))) {
352df8bae1dSRodney W. Grimes 		if (auio.uio_resid != cnt && (error == ERESTART ||
353df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
354df8bae1dSRodney W. Grimes 			error = 0;
355df8bae1dSRodney W. Grimes 		if (error == EPIPE)
356df8bae1dSRodney W. Grimes 			psignal(p, SIGPIPE);
357df8bae1dSRodney W. Grimes 	}
358df8bae1dSRodney W. Grimes 	cnt -= auio.uio_resid;
359df8bae1dSRodney W. Grimes #ifdef KTRACE
360df8bae1dSRodney W. Grimes 	if (ktriov != NULL) {
361df8bae1dSRodney W. Grimes 		if (error == 0)
362df8bae1dSRodney W. Grimes 			ktrgenio(p->p_tracep, uap->fd, UIO_WRITE,
363df8bae1dSRodney W. Grimes 				ktriov, cnt, error);
364df8bae1dSRodney W. Grimes 		FREE(ktriov, M_TEMP);
365df8bae1dSRodney W. Grimes 	}
366df8bae1dSRodney W. Grimes #endif
367df8bae1dSRodney W. Grimes 	*retval = cnt;
368df8bae1dSRodney W. Grimes done:
369df8bae1dSRodney W. Grimes 	if (needfree)
370df8bae1dSRodney W. Grimes 		FREE(needfree, M_IOV);
371df8bae1dSRodney W. Grimes 	return (error);
372df8bae1dSRodney W. Grimes }
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes /*
375df8bae1dSRodney W. Grimes  * Ioctl system call
376df8bae1dSRodney W. Grimes  */
377d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
378df8bae1dSRodney W. Grimes struct ioctl_args {
379df8bae1dSRodney W. Grimes 	int	fd;
380df8bae1dSRodney W. Grimes 	int	com;
381df8bae1dSRodney W. Grimes 	caddr_t	data;
382df8bae1dSRodney W. Grimes };
383d2d3e875SBruce Evans #endif
384df8bae1dSRodney W. Grimes /* ARGSUSED */
38526f9a767SRodney W. Grimes int
386df8bae1dSRodney W. Grimes ioctl(p, uap, retval)
387df8bae1dSRodney W. Grimes 	struct proc *p;
388df8bae1dSRodney W. Grimes 	register struct ioctl_args *uap;
389df8bae1dSRodney W. Grimes 	int *retval;
390df8bae1dSRodney W. Grimes {
391df8bae1dSRodney W. Grimes 	register struct file *fp;
392df8bae1dSRodney W. Grimes 	register struct filedesc *fdp;
393df8bae1dSRodney W. Grimes 	register int com, error;
394df8bae1dSRodney W. Grimes 	register u_int size;
395df8bae1dSRodney W. Grimes 	caddr_t data, memp;
396df8bae1dSRodney W. Grimes 	int tmp;
397df8bae1dSRodney W. Grimes #define STK_PARAMS	128
398df8bae1dSRodney W. Grimes 	char stkbuf[STK_PARAMS];
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes 	fdp = p->p_fd;
401df8bae1dSRodney W. Grimes 	if ((u_int)uap->fd >= fdp->fd_nfiles ||
402df8bae1dSRodney W. Grimes 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
403df8bae1dSRodney W. Grimes 		return (EBADF);
404df8bae1dSRodney W. Grimes 
405df8bae1dSRodney W. Grimes 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
406df8bae1dSRodney W. Grimes 		return (EBADF);
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	switch (com = uap->com) {
409df8bae1dSRodney W. Grimes 	case FIONCLEX:
410df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
411df8bae1dSRodney W. Grimes 		return (0);
412df8bae1dSRodney W. Grimes 	case FIOCLEX:
413df8bae1dSRodney W. Grimes 		fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
414df8bae1dSRodney W. Grimes 		return (0);
415df8bae1dSRodney W. Grimes 	}
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
419df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
420df8bae1dSRodney W. Grimes 	 */
421df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
422df8bae1dSRodney W. Grimes 	if (size > IOCPARM_MAX)
423df8bae1dSRodney W. Grimes 		return (ENOTTY);
424df8bae1dSRodney W. Grimes 	memp = NULL;
425df8bae1dSRodney W. Grimes 	if (size > sizeof (stkbuf)) {
426df8bae1dSRodney W. Grimes 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
427df8bae1dSRodney W. Grimes 		data = memp;
428df8bae1dSRodney W. Grimes 	} else
429df8bae1dSRodney W. Grimes 		data = stkbuf;
430df8bae1dSRodney W. Grimes 	if (com&IOC_IN) {
431df8bae1dSRodney W. Grimes 		if (size) {
432df8bae1dSRodney W. Grimes 			error = copyin(uap->data, data, (u_int)size);
433df8bae1dSRodney W. Grimes 			if (error) {
434df8bae1dSRodney W. Grimes 				if (memp)
435df8bae1dSRodney W. Grimes 					free(memp, M_IOCTLOPS);
436df8bae1dSRodney W. Grimes 				return (error);
437df8bae1dSRodney W. Grimes 			}
438df8bae1dSRodney W. Grimes 		} else
439df8bae1dSRodney W. Grimes 			*(caddr_t *)data = uap->data;
440df8bae1dSRodney W. Grimes 	} else if ((com&IOC_OUT) && size)
441df8bae1dSRodney W. Grimes 		/*
442df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
443df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
444df8bae1dSRodney W. Grimes 		 */
445df8bae1dSRodney W. Grimes 		bzero(data, size);
446df8bae1dSRodney W. Grimes 	else if (com&IOC_VOID)
447df8bae1dSRodney W. Grimes 		*(caddr_t *)data = uap->data;
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	switch (com) {
450df8bae1dSRodney W. Grimes 
451df8bae1dSRodney W. Grimes 	case FIONBIO:
452bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
453df8bae1dSRodney W. Grimes 			fp->f_flag |= FNONBLOCK;
454df8bae1dSRodney W. Grimes 		else
455df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FNONBLOCK;
456df8bae1dSRodney W. Grimes 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
457df8bae1dSRodney W. Grimes 		break;
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes 	case FIOASYNC:
460bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
461df8bae1dSRodney W. Grimes 			fp->f_flag |= FASYNC;
462df8bae1dSRodney W. Grimes 		else
463df8bae1dSRodney W. Grimes 			fp->f_flag &= ~FASYNC;
464df8bae1dSRodney W. Grimes 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
465df8bae1dSRodney W. Grimes 		break;
466df8bae1dSRodney W. Grimes 
467df8bae1dSRodney W. Grimes 	case FIOSETOWN:
468df8bae1dSRodney W. Grimes 		tmp = *(int *)data;
469df8bae1dSRodney W. Grimes 		if (fp->f_type == DTYPE_SOCKET) {
470df8bae1dSRodney W. Grimes 			((struct socket *)fp->f_data)->so_pgid = tmp;
471df8bae1dSRodney W. Grimes 			error = 0;
472df8bae1dSRodney W. Grimes 			break;
473df8bae1dSRodney W. Grimes 		}
474df8bae1dSRodney W. Grimes 		if (tmp <= 0) {
475df8bae1dSRodney W. Grimes 			tmp = -tmp;
476df8bae1dSRodney W. Grimes 		} else {
477df8bae1dSRodney W. Grimes 			struct proc *p1 = pfind(tmp);
478df8bae1dSRodney W. Grimes 			if (p1 == 0) {
479df8bae1dSRodney W. Grimes 				error = ESRCH;
480df8bae1dSRodney W. Grimes 				break;
481df8bae1dSRodney W. Grimes 			}
482df8bae1dSRodney W. Grimes 			tmp = p1->p_pgrp->pg_id;
483df8bae1dSRodney W. Grimes 		}
484df8bae1dSRodney W. Grimes 		error = (*fp->f_ops->fo_ioctl)
485df8bae1dSRodney W. Grimes 			(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
486df8bae1dSRodney W. Grimes 		break;
487df8bae1dSRodney W. Grimes 
488df8bae1dSRodney W. Grimes 	case FIOGETOWN:
489df8bae1dSRodney W. Grimes 		if (fp->f_type == DTYPE_SOCKET) {
490df8bae1dSRodney W. Grimes 			error = 0;
491df8bae1dSRodney W. Grimes 			*(int *)data = ((struct socket *)fp->f_data)->so_pgid;
492df8bae1dSRodney W. Grimes 			break;
493df8bae1dSRodney W. Grimes 		}
494df8bae1dSRodney W. Grimes 		error = (*fp->f_ops->fo_ioctl)(fp, (int)TIOCGPGRP, data, p);
495df8bae1dSRodney W. Grimes 		*(int *)data = -*(int *)data;
496df8bae1dSRodney W. Grimes 		break;
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 	default:
499df8bae1dSRodney W. Grimes 		error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
500df8bae1dSRodney W. Grimes 		/*
501df8bae1dSRodney W. Grimes 		 * Copy any data to user, size was
502df8bae1dSRodney W. Grimes 		 * already set and checked above.
503df8bae1dSRodney W. Grimes 		 */
504df8bae1dSRodney W. Grimes 		if (error == 0 && (com&IOC_OUT) && size)
505df8bae1dSRodney W. Grimes 			error = copyout(data, uap->data, (u_int)size);
506df8bae1dSRodney W. Grimes 		break;
507df8bae1dSRodney W. Grimes 	}
508df8bae1dSRodney W. Grimes 	if (memp)
509df8bae1dSRodney W. Grimes 		free(memp, M_IOCTLOPS);
510df8bae1dSRodney W. Grimes 	return (error);
511df8bae1dSRodney W. Grimes }
512df8bae1dSRodney W. Grimes 
51387b6de2bSPoul-Henning Kamp static int	nselcoll;
51487b6de2bSPoul-Henning Kamp int	selwait;
515df8bae1dSRodney W. Grimes 
516df8bae1dSRodney W. Grimes /*
517df8bae1dSRodney W. Grimes  * Select system call.
518df8bae1dSRodney W. Grimes  */
519d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
520df8bae1dSRodney W. Grimes struct select_args {
521b08f7993SSujal Patel 	int	nd;
522df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
523df8bae1dSRodney W. Grimes 	struct	timeval *tv;
524df8bae1dSRodney W. Grimes };
525d2d3e875SBruce Evans #endif
52626f9a767SRodney W. Grimes int
527df8bae1dSRodney W. Grimes select(p, uap, retval)
528df8bae1dSRodney W. Grimes 	register struct proc *p;
529df8bae1dSRodney W. Grimes 	register struct select_args *uap;
530df8bae1dSRodney W. Grimes 	int *retval;
531df8bae1dSRodney W. Grimes {
532d5e4d7e1SBruce Evans 	/*
533d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
534d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
535d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
536d5e4d7e1SBruce Evans 	 * of 256.
537d5e4d7e1SBruce Evans 	 */
538d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
539d5e4d7e1SBruce Evans 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
540df8bae1dSRodney W. Grimes 	struct timeval atv;
541d5e4d7e1SBruce Evans 	int s, ncoll, error, timo;
542d5e4d7e1SBruce Evans 	u_int nbufbytes, ncpbytes, nfdbits;
543df8bae1dSRodney W. Grimes 
544b08f7993SSujal Patel 	if (uap->nd < 0)
545acbfbfeaSSujal Patel 		return (EINVAL);
546df8bae1dSRodney W. Grimes 	if (uap->nd > p->p_fd->fd_nfiles)
547df8bae1dSRodney W. Grimes 		uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
548b08f7993SSujal Patel 
549d5e4d7e1SBruce Evans 	/*
550d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
551d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
552d5e4d7e1SBruce Evans 	 */
553d5e4d7e1SBruce Evans 	nfdbits = roundup(uap->nd, NFDBITS);
554d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
555d5e4d7e1SBruce Evans 	nbufbytes = 0;
556d5e4d7e1SBruce Evans 	if (uap->in != NULL)
557d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
558d5e4d7e1SBruce Evans 	if (uap->ou != NULL)
559d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
560d5e4d7e1SBruce Evans 	if (uap->ex != NULL)
561d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
562d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
563d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
564d5e4d7e1SBruce Evans 	else
565d5e4d7e1SBruce Evans 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
566b08f7993SSujal Patel 
567b08f7993SSujal Patel 	/*
568d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
569d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
570d5e4d7e1SBruce Evans 	 * together.
571b08f7993SSujal Patel 	 */
572d5e4d7e1SBruce Evans 	sbp = selbits;
573df8bae1dSRodney W. Grimes #define	getbits(name, x) \
574d5e4d7e1SBruce Evans 	do {								\
575d5e4d7e1SBruce Evans 		if (uap->name == NULL)					\
576d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
577d5e4d7e1SBruce Evans 		else {							\
578d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
579d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
580d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
581d5e4d7e1SBruce Evans 			error = copyin(uap->name, ibits[x], ncpbytes);	\
582d5e4d7e1SBruce Evans 			if (error != 0)					\
583d5e4d7e1SBruce Evans 				goto done;				\
584d5e4d7e1SBruce Evans 		}							\
585d5e4d7e1SBruce Evans 	} while (0)
586df8bae1dSRodney W. Grimes 	getbits(in, 0);
587df8bae1dSRodney W. Grimes 	getbits(ou, 1);
588df8bae1dSRodney W. Grimes 	getbits(ex, 2);
589df8bae1dSRodney W. Grimes #undef	getbits
590d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
591d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes 	if (uap->tv) {
594df8bae1dSRodney W. Grimes 		error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
595df8bae1dSRodney W. Grimes 			sizeof (atv));
596df8bae1dSRodney W. Grimes 		if (error)
597df8bae1dSRodney W. Grimes 			goto done;
598df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
599df8bae1dSRodney W. Grimes 			error = EINVAL;
600df8bae1dSRodney W. Grimes 			goto done;
601df8bae1dSRodney W. Grimes 		}
602df8bae1dSRodney W. Grimes 		s = splclock();
603774fce94SBruce Evans 		timevaladd(&atv, &time);
604df8bae1dSRodney W. Grimes 		timo = hzto(&atv);
605df8bae1dSRodney W. Grimes 		/*
606df8bae1dSRodney W. Grimes 		 * Avoid inadvertently sleeping forever.
607df8bae1dSRodney W. Grimes 		 */
608df8bae1dSRodney W. Grimes 		if (timo == 0)
609df8bae1dSRodney W. Grimes 			timo = 1;
610df8bae1dSRodney W. Grimes 		splx(s);
611df8bae1dSRodney W. Grimes 	} else
612df8bae1dSRodney W. Grimes 		timo = 0;
613df8bae1dSRodney W. Grimes retry:
614df8bae1dSRodney W. Grimes 	ncoll = nselcoll;
615df8bae1dSRodney W. Grimes 	p->p_flag |= P_SELECT;
616df8bae1dSRodney W. Grimes 	error = selscan(p, ibits, obits, uap->nd, retval);
617df8bae1dSRodney W. Grimes 	if (error || *retval)
618df8bae1dSRodney W. Grimes 		goto done;
619df8bae1dSRodney W. Grimes 	s = splhigh();
620df8bae1dSRodney W. Grimes 	/* this should be timercmp(&time, &atv, >=) */
621df8bae1dSRodney W. Grimes 	if (uap->tv && (time.tv_sec > atv.tv_sec ||
622bb56ec4aSPoul-Henning Kamp 	    (time.tv_sec == atv.tv_sec && time.tv_usec >= atv.tv_usec))) {
623df8bae1dSRodney W. Grimes 		splx(s);
624df8bae1dSRodney W. Grimes 		goto done;
625df8bae1dSRodney W. Grimes 	}
626df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
627df8bae1dSRodney W. Grimes 		splx(s);
628df8bae1dSRodney W. Grimes 		goto retry;
629df8bae1dSRodney W. Grimes 	}
630df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
631df8bae1dSRodney W. Grimes 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
632df8bae1dSRodney W. Grimes 	splx(s);
633df8bae1dSRodney W. Grimes 	if (error == 0)
634df8bae1dSRodney W. Grimes 		goto retry;
635df8bae1dSRodney W. Grimes done:
636df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_SELECT;
637df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
638df8bae1dSRodney W. Grimes 	if (error == ERESTART)
639df8bae1dSRodney W. Grimes 		error = EINTR;
640df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
641df8bae1dSRodney W. Grimes 		error = 0;
642df8bae1dSRodney W. Grimes #define	putbits(name, x) \
643d5e4d7e1SBruce Evans 	if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
644df8bae1dSRodney W. Grimes 		error = error2;
645df8bae1dSRodney W. Grimes 	if (error == 0) {
646df8bae1dSRodney W. Grimes 		int error2;
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 		putbits(in, 0);
649df8bae1dSRodney W. Grimes 		putbits(ou, 1);
650df8bae1dSRodney W. Grimes 		putbits(ex, 2);
651df8bae1dSRodney W. Grimes #undef putbits
652df8bae1dSRodney W. Grimes 	}
653d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
654d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
655df8bae1dSRodney W. Grimes 	return (error);
656df8bae1dSRodney W. Grimes }
657df8bae1dSRodney W. Grimes 
65887b6de2bSPoul-Henning Kamp static int
659df8bae1dSRodney W. Grimes selscan(p, ibits, obits, nfd, retval)
660df8bae1dSRodney W. Grimes 	struct proc *p;
661b08f7993SSujal Patel 	fd_mask **ibits, **obits;
662df8bae1dSRodney W. Grimes 	int nfd, *retval;
663df8bae1dSRodney W. Grimes {
664df8bae1dSRodney W. Grimes 	register struct filedesc *fdp = p->p_fd;
665df8bae1dSRodney W. Grimes 	register int msk, i, j, fd;
666df8bae1dSRodney W. Grimes 	register fd_mask bits;
667df8bae1dSRodney W. Grimes 	struct file *fp;
668df8bae1dSRodney W. Grimes 	int n = 0;
669df8bae1dSRodney W. Grimes 	static int flag[3] = { FREAD, FWRITE, 0 };
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	for (msk = 0; msk < 3; msk++) {
672d5e4d7e1SBruce Evans 		if (ibits[msk] == NULL)
673d5e4d7e1SBruce Evans 			continue;
674df8bae1dSRodney W. Grimes 		for (i = 0; i < nfd; i += NFDBITS) {
675b08f7993SSujal Patel 			bits = ibits[msk][i/NFDBITS];
676df8bae1dSRodney W. Grimes 			while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
677df8bae1dSRodney W. Grimes 				bits &= ~(1 << j);
678df8bae1dSRodney W. Grimes 				fp = fdp->fd_ofiles[fd];
679df8bae1dSRodney W. Grimes 				if (fp == NULL)
680df8bae1dSRodney W. Grimes 					return (EBADF);
681df8bae1dSRodney W. Grimes 				if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
682b08f7993SSujal Patel 					obits[msk][(fd)/NFDBITS] |=
683b08f7993SSujal Patel 						(1 << ((fd) % NFDBITS));
684df8bae1dSRodney W. Grimes 					n++;
685df8bae1dSRodney W. Grimes 				}
686df8bae1dSRodney W. Grimes 			}
687df8bae1dSRodney W. Grimes 		}
688df8bae1dSRodney W. Grimes 	}
689df8bae1dSRodney W. Grimes 	*retval = n;
690df8bae1dSRodney W. Grimes 	return (0);
691df8bae1dSRodney W. Grimes }
692df8bae1dSRodney W. Grimes 
693df8bae1dSRodney W. Grimes /*ARGSUSED*/
69426f9a767SRodney W. Grimes int
695df8bae1dSRodney W. Grimes seltrue(dev, flag, p)
696df8bae1dSRodney W. Grimes 	dev_t dev;
697df8bae1dSRodney W. Grimes 	int flag;
698df8bae1dSRodney W. Grimes 	struct proc *p;
699df8bae1dSRodney W. Grimes {
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes 	return (1);
702df8bae1dSRodney W. Grimes }
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes /*
705df8bae1dSRodney W. Grimes  * Record a select request.
706df8bae1dSRodney W. Grimes  */
707df8bae1dSRodney W. Grimes void
708df8bae1dSRodney W. Grimes selrecord(selector, sip)
709df8bae1dSRodney W. Grimes 	struct proc *selector;
710df8bae1dSRodney W. Grimes 	struct selinfo *sip;
711df8bae1dSRodney W. Grimes {
712df8bae1dSRodney W. Grimes 	struct proc *p;
713df8bae1dSRodney W. Grimes 	pid_t mypid;
714df8bae1dSRodney W. Grimes 
715df8bae1dSRodney W. Grimes 	mypid = selector->p_pid;
716df8bae1dSRodney W. Grimes 	if (sip->si_pid == mypid)
717df8bae1dSRodney W. Grimes 		return;
718df8bae1dSRodney W. Grimes 	if (sip->si_pid && (p = pfind(sip->si_pid)) &&
719df8bae1dSRodney W. Grimes 	    p->p_wchan == (caddr_t)&selwait)
720df8bae1dSRodney W. Grimes 		sip->si_flags |= SI_COLL;
721df8bae1dSRodney W. Grimes 	else
722df8bae1dSRodney W. Grimes 		sip->si_pid = mypid;
723df8bae1dSRodney W. Grimes }
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes /*
726df8bae1dSRodney W. Grimes  * Do a wakeup when a selectable event occurs.
727df8bae1dSRodney W. Grimes  */
728df8bae1dSRodney W. Grimes void
729df8bae1dSRodney W. Grimes selwakeup(sip)
730df8bae1dSRodney W. Grimes 	register struct selinfo *sip;
731df8bae1dSRodney W. Grimes {
732df8bae1dSRodney W. Grimes 	register struct proc *p;
733df8bae1dSRodney W. Grimes 	int s;
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes 	if (sip->si_pid == 0)
736df8bae1dSRodney W. Grimes 		return;
737df8bae1dSRodney W. Grimes 	if (sip->si_flags & SI_COLL) {
738df8bae1dSRodney W. Grimes 		nselcoll++;
739df8bae1dSRodney W. Grimes 		sip->si_flags &= ~SI_COLL;
740df8bae1dSRodney W. Grimes 		wakeup((caddr_t)&selwait);
741df8bae1dSRodney W. Grimes 	}
742df8bae1dSRodney W. Grimes 	p = pfind(sip->si_pid);
743df8bae1dSRodney W. Grimes 	sip->si_pid = 0;
744df8bae1dSRodney W. Grimes 	if (p != NULL) {
745df8bae1dSRodney W. Grimes 		s = splhigh();
746df8bae1dSRodney W. Grimes 		if (p->p_wchan == (caddr_t)&selwait) {
747df8bae1dSRodney W. Grimes 			if (p->p_stat == SSLEEP)
748df8bae1dSRodney W. Grimes 				setrunnable(p);
749df8bae1dSRodney W. Grimes 			else
750df8bae1dSRodney W. Grimes 				unsleep(p);
751df8bae1dSRodney W. Grimes 		} else if (p->p_flag & P_SELECT)
752df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_SELECT;
753df8bae1dSRodney W. Grimes 		splx(s);
754df8bae1dSRodney W. Grimes 	}
755df8bae1dSRodney W. Grimes }
756