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