sys_generic.c revision 1.46 1 1.46 darrenr /* $NetBSD: sys_generic.c,v 1.46 2000/03/17 00:01:48 darrenr Exp $ */
2 1.15 cgd
3 1.15 cgd /*
4 1.15 cgd * Copyright (c) 1982, 1986, 1989, 1993
5 1.15 cgd * The Regents of the University of California. All rights reserved.
6 1.15 cgd * (c) UNIX System Laboratories, Inc.
7 1.15 cgd * All or some portions of this file are derived from material licensed
8 1.15 cgd * to the University of California by American Telephone and Telegraph
9 1.15 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.15 cgd * the permission of UNIX System Laboratories, Inc.
11 1.15 cgd *
12 1.15 cgd * Redistribution and use in source and binary forms, with or without
13 1.15 cgd * modification, are permitted provided that the following conditions
14 1.15 cgd * are met:
15 1.15 cgd * 1. Redistributions of source code must retain the above copyright
16 1.15 cgd * notice, this list of conditions and the following disclaimer.
17 1.15 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.15 cgd * notice, this list of conditions and the following disclaimer in the
19 1.15 cgd * documentation and/or other materials provided with the distribution.
20 1.15 cgd * 3. All advertising materials mentioning features or use of this software
21 1.15 cgd * must display the following acknowledgement:
22 1.15 cgd * This product includes software developed by the University of
23 1.15 cgd * California, Berkeley and its contributors.
24 1.15 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.15 cgd * may be used to endorse or promote products derived from this software
26 1.15 cgd * without specific prior written permission.
27 1.15 cgd *
28 1.15 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.15 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.15 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.15 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.15 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.15 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.15 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.15 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.15 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.15 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.15 cgd * SUCH DAMAGE.
39 1.15 cgd *
40 1.36 fvdl * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
41 1.15 cgd */
42 1.37 thorpej
43 1.37 thorpej #include "opt_ktrace.h"
44 1.15 cgd
45 1.15 cgd #include <sys/param.h>
46 1.15 cgd #include <sys/systm.h>
47 1.15 cgd #include <sys/filedesc.h>
48 1.15 cgd #include <sys/ioctl.h>
49 1.15 cgd #include <sys/file.h>
50 1.15 cgd #include <sys/proc.h>
51 1.15 cgd #include <sys/socketvar.h>
52 1.22 christos #include <sys/signalvar.h>
53 1.15 cgd #include <sys/uio.h>
54 1.15 cgd #include <sys/kernel.h>
55 1.15 cgd #include <sys/stat.h>
56 1.15 cgd #include <sys/malloc.h>
57 1.28 mycroft #include <sys/poll.h>
58 1.15 cgd #ifdef KTRACE
59 1.15 cgd #include <sys/ktrace.h>
60 1.15 cgd #endif
61 1.15 cgd
62 1.16 cgd #include <sys/mount.h>
63 1.16 cgd #include <sys/syscallargs.h>
64 1.22 christos
65 1.25 mycroft int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
66 1.28 mycroft int pollscan __P((struct proc *, struct pollfd *, int, register_t *));
67 1.22 christos
68 1.15 cgd /*
69 1.15 cgd * Read system call.
70 1.15 cgd */
71 1.15 cgd /* ARGSUSED */
72 1.22 christos int
73 1.21 mycroft sys_read(p, v, retval)
74 1.15 cgd struct proc *p;
75 1.20 thorpej void *v;
76 1.20 thorpej register_t *retval;
77 1.20 thorpej {
78 1.21 mycroft register struct sys_read_args /* {
79 1.16 cgd syscallarg(int) fd;
80 1.31 cgd syscallarg(void *) buf;
81 1.31 cgd syscallarg(size_t) nbyte;
82 1.20 thorpej } */ *uap = v;
83 1.34 mycroft int fd = SCARG(uap, fd);
84 1.15 cgd register struct file *fp;
85 1.15 cgd register struct filedesc *fdp = p->p_fd;
86 1.39 thorpej
87 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
88 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
89 1.45 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
90 1.39 thorpej (fp->f_flag & FREAD) == 0)
91 1.39 thorpej return (EBADF);
92 1.39 thorpej
93 1.45 thorpej FILE_USE(fp);
94 1.45 thorpej
95 1.45 thorpej /* dofileread() will unuse the descriptor for us */
96 1.39 thorpej return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
97 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
98 1.39 thorpej }
99 1.39 thorpej
100 1.39 thorpej int
101 1.39 thorpej dofileread(p, fd, fp, buf, nbyte, offset, flags, retval)
102 1.39 thorpej struct proc *p;
103 1.39 thorpej int fd;
104 1.39 thorpej struct file *fp;
105 1.39 thorpej void *buf;
106 1.39 thorpej size_t nbyte;
107 1.39 thorpej off_t *offset;
108 1.39 thorpej int flags;
109 1.39 thorpej register_t *retval;
110 1.39 thorpej {
111 1.15 cgd struct uio auio;
112 1.15 cgd struct iovec aiov;
113 1.15 cgd long cnt, error = 0;
114 1.15 cgd #ifdef KTRACE
115 1.15 cgd struct iovec ktriov;
116 1.15 cgd #endif
117 1.15 cgd
118 1.39 thorpej aiov.iov_base = (caddr_t)buf;
119 1.39 thorpej aiov.iov_len = nbyte;
120 1.15 cgd auio.uio_iov = &aiov;
121 1.15 cgd auio.uio_iovcnt = 1;
122 1.39 thorpej auio.uio_resid = nbyte;
123 1.15 cgd auio.uio_rw = UIO_READ;
124 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
125 1.15 cgd auio.uio_procp = p;
126 1.40 thorpej
127 1.40 thorpej /*
128 1.40 thorpej * Reads return ssize_t because -1 is returned on error. Therefore
129 1.40 thorpej * we must restrict the length to SSIZE_MAX to avoid garbage return
130 1.40 thorpej * values.
131 1.40 thorpej */
132 1.45 thorpej if (auio.uio_resid > SSIZE_MAX) {
133 1.45 thorpej error = EINVAL;
134 1.45 thorpej goto out;
135 1.45 thorpej }
136 1.40 thorpej
137 1.15 cgd #ifdef KTRACE
138 1.15 cgd /*
139 1.15 cgd * if tracing, save a copy of iovec
140 1.15 cgd */
141 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
142 1.15 cgd ktriov = aiov;
143 1.15 cgd #endif
144 1.38 thorpej cnt = auio.uio_resid;
145 1.39 thorpej error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
146 1.22 christos if (error)
147 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
148 1.15 cgd error == EINTR || error == EWOULDBLOCK))
149 1.15 cgd error = 0;
150 1.15 cgd cnt -= auio.uio_resid;
151 1.15 cgd #ifdef KTRACE
152 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
153 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_READ, &ktriov, cnt, error);
154 1.15 cgd #endif
155 1.15 cgd *retval = cnt;
156 1.45 thorpej out:
157 1.45 thorpej FILE_UNUSE(fp, p);
158 1.15 cgd return (error);
159 1.15 cgd }
160 1.15 cgd
161 1.15 cgd /*
162 1.15 cgd * Scatter read system call.
163 1.15 cgd */
164 1.22 christos int
165 1.21 mycroft sys_readv(p, v, retval)
166 1.15 cgd struct proc *p;
167 1.20 thorpej void *v;
168 1.20 thorpej register_t *retval;
169 1.20 thorpej {
170 1.21 mycroft register struct sys_readv_args /* {
171 1.16 cgd syscallarg(int) fd;
172 1.31 cgd syscallarg(const struct iovec *) iovp;
173 1.34 mycroft syscallarg(int) iovcnt;
174 1.20 thorpej } */ *uap = v;
175 1.34 mycroft int fd = SCARG(uap, fd);
176 1.15 cgd register struct file *fp;
177 1.15 cgd register struct filedesc *fdp = p->p_fd;
178 1.39 thorpej
179 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
180 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
181 1.45 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
182 1.39 thorpej (fp->f_flag & FREAD) == 0)
183 1.39 thorpej return (EBADF);
184 1.39 thorpej
185 1.45 thorpej FILE_USE(fp);
186 1.45 thorpej
187 1.45 thorpej /* dofilereadv() will unuse the descriptor for us */
188 1.39 thorpej return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
189 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
190 1.39 thorpej }
191 1.39 thorpej
192 1.39 thorpej int
193 1.39 thorpej dofilereadv(p, fd, fp, iovp, iovcnt, offset, flags, retval)
194 1.39 thorpej struct proc *p;
195 1.39 thorpej int fd;
196 1.39 thorpej struct file *fp;
197 1.39 thorpej const struct iovec *iovp;
198 1.39 thorpej int iovcnt;
199 1.39 thorpej off_t *offset;
200 1.39 thorpej int flags;
201 1.39 thorpej register_t *retval;
202 1.39 thorpej {
203 1.15 cgd struct uio auio;
204 1.15 cgd register struct iovec *iov;
205 1.15 cgd struct iovec *needfree;
206 1.15 cgd struct iovec aiov[UIO_SMALLIOV];
207 1.15 cgd long i, cnt, error = 0;
208 1.15 cgd u_int iovlen;
209 1.15 cgd #ifdef KTRACE
210 1.15 cgd struct iovec *ktriov = NULL;
211 1.15 cgd #endif
212 1.15 cgd
213 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
214 1.42 perry iovlen = iovcnt * sizeof(struct iovec);
215 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
216 1.45 thorpej if ((u_int)iovcnt > IOV_MAX) {
217 1.45 thorpej error = EINVAL;
218 1.45 thorpej goto out;
219 1.45 thorpej }
220 1.15 cgd MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
221 1.15 cgd needfree = iov;
222 1.41 kleink } else if ((u_int)iovcnt > 0) {
223 1.15 cgd iov = aiov;
224 1.15 cgd needfree = NULL;
225 1.45 thorpej } else {
226 1.45 thorpej error = EINVAL;
227 1.45 thorpej goto out;
228 1.45 thorpej }
229 1.41 kleink
230 1.15 cgd auio.uio_iov = iov;
231 1.34 mycroft auio.uio_iovcnt = iovcnt;
232 1.15 cgd auio.uio_rw = UIO_READ;
233 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
234 1.15 cgd auio.uio_procp = p;
235 1.39 thorpej error = copyin(iovp, iov, iovlen);
236 1.22 christos if (error)
237 1.15 cgd goto done;
238 1.15 cgd auio.uio_resid = 0;
239 1.34 mycroft for (i = 0; i < iovcnt; i++) {
240 1.15 cgd auio.uio_resid += iov->iov_len;
241 1.40 thorpej /*
242 1.40 thorpej * Reads return ssize_t because -1 is returned on error.
243 1.40 thorpej * Therefore we must restrict the length to SSIZE_MAX to
244 1.40 thorpej * avoid garbage return values.
245 1.40 thorpej */
246 1.40 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
247 1.15 cgd error = EINVAL;
248 1.15 cgd goto done;
249 1.15 cgd }
250 1.15 cgd iov++;
251 1.15 cgd }
252 1.15 cgd #ifdef KTRACE
253 1.15 cgd /*
254 1.15 cgd * if tracing, save a copy of iovec
255 1.15 cgd */
256 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
257 1.15 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
258 1.44 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
259 1.15 cgd }
260 1.15 cgd #endif
261 1.15 cgd cnt = auio.uio_resid;
262 1.39 thorpej error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
263 1.22 christos if (error)
264 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
265 1.15 cgd error == EINTR || error == EWOULDBLOCK))
266 1.15 cgd error = 0;
267 1.15 cgd cnt -= auio.uio_resid;
268 1.15 cgd #ifdef KTRACE
269 1.34 mycroft if (KTRPOINT(p, KTR_GENIO))
270 1.34 mycroft if (error == 0) {
271 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_READ, ktriov, cnt,
272 1.34 mycroft error);
273 1.15 cgd FREE(ktriov, M_TEMP);
274 1.15 cgd }
275 1.15 cgd #endif
276 1.15 cgd *retval = cnt;
277 1.45 thorpej done:
278 1.15 cgd if (needfree)
279 1.15 cgd FREE(needfree, M_IOV);
280 1.45 thorpej out:
281 1.45 thorpej FILE_UNUSE(fp, p);
282 1.15 cgd return (error);
283 1.15 cgd }
284 1.15 cgd
285 1.15 cgd /*
286 1.15 cgd * Write system call
287 1.15 cgd */
288 1.22 christos int
289 1.21 mycroft sys_write(p, v, retval)
290 1.15 cgd struct proc *p;
291 1.20 thorpej void *v;
292 1.20 thorpej register_t *retval;
293 1.20 thorpej {
294 1.21 mycroft register struct sys_write_args /* {
295 1.16 cgd syscallarg(int) fd;
296 1.31 cgd syscallarg(const void *) buf;
297 1.31 cgd syscallarg(size_t) nbyte;
298 1.20 thorpej } */ *uap = v;
299 1.34 mycroft int fd = SCARG(uap, fd);
300 1.15 cgd register struct file *fp;
301 1.15 cgd register struct filedesc *fdp = p->p_fd;
302 1.39 thorpej
303 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
304 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
305 1.45 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
306 1.39 thorpej (fp->f_flag & FWRITE) == 0)
307 1.39 thorpej return (EBADF);
308 1.39 thorpej
309 1.45 thorpej FILE_USE(fp);
310 1.45 thorpej
311 1.45 thorpej /* dofilewrite() will unuse the descriptor for us */
312 1.39 thorpej return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
313 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
314 1.39 thorpej }
315 1.39 thorpej
316 1.39 thorpej int
317 1.39 thorpej dofilewrite(p, fd, fp, buf, nbyte, offset, flags, retval)
318 1.39 thorpej struct proc *p;
319 1.39 thorpej int fd;
320 1.39 thorpej struct file *fp;
321 1.39 thorpej const void *buf;
322 1.39 thorpej size_t nbyte;
323 1.39 thorpej off_t *offset;
324 1.39 thorpej int flags;
325 1.39 thorpej register_t *retval;
326 1.39 thorpej {
327 1.15 cgd struct uio auio;
328 1.15 cgd struct iovec aiov;
329 1.15 cgd long cnt, error = 0;
330 1.15 cgd #ifdef KTRACE
331 1.15 cgd struct iovec ktriov;
332 1.15 cgd #endif
333 1.15 cgd
334 1.39 thorpej aiov.iov_base = (caddr_t)buf; /* XXX kills const */
335 1.39 thorpej aiov.iov_len = nbyte;
336 1.15 cgd auio.uio_iov = &aiov;
337 1.15 cgd auio.uio_iovcnt = 1;
338 1.39 thorpej auio.uio_resid = nbyte;
339 1.15 cgd auio.uio_rw = UIO_WRITE;
340 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
341 1.15 cgd auio.uio_procp = p;
342 1.40 thorpej
343 1.40 thorpej /*
344 1.40 thorpej * Writes return ssize_t because -1 is returned on error. Therefore
345 1.40 thorpej * we must restrict the length to SSIZE_MAX to avoid garbage return
346 1.40 thorpej * values.
347 1.40 thorpej */
348 1.45 thorpej if (auio.uio_resid > SSIZE_MAX) {
349 1.45 thorpej error = EINVAL;
350 1.45 thorpej goto out;
351 1.45 thorpej }
352 1.40 thorpej
353 1.15 cgd #ifdef KTRACE
354 1.15 cgd /*
355 1.15 cgd * if tracing, save a copy of iovec
356 1.15 cgd */
357 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
358 1.15 cgd ktriov = aiov;
359 1.15 cgd #endif
360 1.38 thorpej cnt = auio.uio_resid;
361 1.39 thorpej error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
362 1.22 christos if (error) {
363 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
364 1.15 cgd error == EINTR || error == EWOULDBLOCK))
365 1.15 cgd error = 0;
366 1.15 cgd if (error == EPIPE)
367 1.15 cgd psignal(p, SIGPIPE);
368 1.15 cgd }
369 1.15 cgd cnt -= auio.uio_resid;
370 1.15 cgd #ifdef KTRACE
371 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
372 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktriov, cnt, error);
373 1.15 cgd #endif
374 1.15 cgd *retval = cnt;
375 1.45 thorpej out:
376 1.45 thorpej FILE_UNUSE(fp, p);
377 1.15 cgd return (error);
378 1.15 cgd }
379 1.15 cgd
380 1.15 cgd /*
381 1.15 cgd * Gather write system call
382 1.15 cgd */
383 1.22 christos int
384 1.21 mycroft sys_writev(p, v, retval)
385 1.15 cgd struct proc *p;
386 1.20 thorpej void *v;
387 1.20 thorpej register_t *retval;
388 1.20 thorpej {
389 1.21 mycroft register struct sys_writev_args /* {
390 1.16 cgd syscallarg(int) fd;
391 1.31 cgd syscallarg(const struct iovec *) iovp;
392 1.39 thorpej syscallarg(int) iovcnt;
393 1.20 thorpej } */ *uap = v;
394 1.34 mycroft int fd = SCARG(uap, fd);
395 1.15 cgd register struct file *fp;
396 1.15 cgd register struct filedesc *fdp = p->p_fd;
397 1.39 thorpej
398 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
399 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
400 1.45 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
401 1.39 thorpej (fp->f_flag & FWRITE) == 0)
402 1.39 thorpej return (EBADF);
403 1.39 thorpej
404 1.45 thorpej FILE_USE(fp);
405 1.45 thorpej
406 1.45 thorpej /* dofilewritev() will unuse the descriptor for us */
407 1.39 thorpej return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
408 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
409 1.39 thorpej }
410 1.39 thorpej
411 1.39 thorpej int
412 1.39 thorpej dofilewritev(p, fd, fp, iovp, iovcnt, offset, flags, retval)
413 1.39 thorpej struct proc *p;
414 1.39 thorpej int fd;
415 1.39 thorpej struct file *fp;
416 1.39 thorpej const struct iovec *iovp;
417 1.39 thorpej int iovcnt;
418 1.39 thorpej off_t *offset;
419 1.39 thorpej int flags;
420 1.39 thorpej register_t *retval;
421 1.39 thorpej {
422 1.15 cgd struct uio auio;
423 1.15 cgd register struct iovec *iov;
424 1.15 cgd struct iovec *needfree;
425 1.15 cgd struct iovec aiov[UIO_SMALLIOV];
426 1.15 cgd long i, cnt, error = 0;
427 1.15 cgd u_int iovlen;
428 1.15 cgd #ifdef KTRACE
429 1.15 cgd struct iovec *ktriov = NULL;
430 1.15 cgd #endif
431 1.15 cgd
432 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
433 1.42 perry iovlen = iovcnt * sizeof(struct iovec);
434 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
435 1.43 kleink if ((u_int)iovcnt > IOV_MAX)
436 1.15 cgd return (EINVAL);
437 1.15 cgd MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
438 1.15 cgd needfree = iov;
439 1.41 kleink } else if ((u_int)iovcnt > 0) {
440 1.15 cgd iov = aiov;
441 1.15 cgd needfree = NULL;
442 1.45 thorpej } else {
443 1.45 thorpej error = EINVAL;
444 1.45 thorpej goto out;
445 1.45 thorpej }
446 1.41 kleink
447 1.15 cgd auio.uio_iov = iov;
448 1.34 mycroft auio.uio_iovcnt = iovcnt;
449 1.15 cgd auio.uio_rw = UIO_WRITE;
450 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
451 1.15 cgd auio.uio_procp = p;
452 1.39 thorpej error = copyin(iovp, iov, iovlen);
453 1.22 christos if (error)
454 1.15 cgd goto done;
455 1.15 cgd auio.uio_resid = 0;
456 1.34 mycroft for (i = 0; i < iovcnt; i++) {
457 1.15 cgd auio.uio_resid += iov->iov_len;
458 1.40 thorpej /*
459 1.40 thorpej * Writes return ssize_t because -1 is returned on error.
460 1.40 thorpej * Therefore we must restrict the length to SSIZE_MAX to
461 1.40 thorpej * avoid garbage return values.
462 1.40 thorpej */
463 1.40 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
464 1.15 cgd error = EINVAL;
465 1.15 cgd goto done;
466 1.15 cgd }
467 1.15 cgd iov++;
468 1.15 cgd }
469 1.15 cgd #ifdef KTRACE
470 1.15 cgd /*
471 1.15 cgd * if tracing, save a copy of iovec
472 1.15 cgd */
473 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
474 1.15 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
475 1.44 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
476 1.15 cgd }
477 1.15 cgd #endif
478 1.15 cgd cnt = auio.uio_resid;
479 1.39 thorpej error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
480 1.22 christos if (error) {
481 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
482 1.15 cgd error == EINTR || error == EWOULDBLOCK))
483 1.15 cgd error = 0;
484 1.15 cgd if (error == EPIPE)
485 1.15 cgd psignal(p, SIGPIPE);
486 1.15 cgd }
487 1.15 cgd cnt -= auio.uio_resid;
488 1.15 cgd #ifdef KTRACE
489 1.34 mycroft if (KTRPOINT(p, KTR_GENIO))
490 1.34 mycroft if (error == 0) {
491 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_WRITE, ktriov, cnt,
492 1.34 mycroft error);
493 1.15 cgd FREE(ktriov, M_TEMP);
494 1.15 cgd }
495 1.15 cgd #endif
496 1.15 cgd *retval = cnt;
497 1.45 thorpej done:
498 1.15 cgd if (needfree)
499 1.15 cgd FREE(needfree, M_IOV);
500 1.45 thorpej out:
501 1.45 thorpej FILE_UNUSE(fp, p);
502 1.15 cgd return (error);
503 1.15 cgd }
504 1.15 cgd
505 1.15 cgd /*
506 1.15 cgd * Ioctl system call
507 1.15 cgd */
508 1.15 cgd /* ARGSUSED */
509 1.22 christos int
510 1.21 mycroft sys_ioctl(p, v, retval)
511 1.15 cgd struct proc *p;
512 1.20 thorpej void *v;
513 1.20 thorpej register_t *retval;
514 1.20 thorpej {
515 1.21 mycroft register struct sys_ioctl_args /* {
516 1.16 cgd syscallarg(int) fd;
517 1.17 cgd syscallarg(u_long) com;
518 1.16 cgd syscallarg(caddr_t) data;
519 1.20 thorpej } */ *uap = v;
520 1.15 cgd register struct file *fp;
521 1.15 cgd register struct filedesc *fdp;
522 1.17 cgd register u_long com;
523 1.45 thorpej register int error = 0;
524 1.15 cgd register u_int size;
525 1.15 cgd caddr_t data, memp;
526 1.15 cgd int tmp;
527 1.15 cgd #define STK_PARAMS 128
528 1.46 darrenr u_long stkbuf[STK_PARAMS/sizeof(u_long)];
529 1.15 cgd
530 1.15 cgd fdp = p->p_fd;
531 1.16 cgd if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
532 1.45 thorpej (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
533 1.45 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0)
534 1.15 cgd return (EBADF);
535 1.15 cgd
536 1.45 thorpej FILE_USE(fp);
537 1.45 thorpej
538 1.45 thorpej if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
539 1.45 thorpej error = EBADF;
540 1.45 thorpej goto out;
541 1.45 thorpej }
542 1.15 cgd
543 1.16 cgd switch (com = SCARG(uap, com)) {
544 1.15 cgd case FIONCLEX:
545 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
546 1.45 thorpej goto out;
547 1.45 thorpej
548 1.15 cgd case FIOCLEX:
549 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
550 1.45 thorpej goto out;
551 1.15 cgd }
552 1.15 cgd
553 1.15 cgd /*
554 1.15 cgd * Interpret high order word to find amount of data to be
555 1.15 cgd * copied to/from the user's address space.
556 1.15 cgd */
557 1.15 cgd size = IOCPARM_LEN(com);
558 1.45 thorpej if (size > IOCPARM_MAX) {
559 1.45 thorpej error = ENOTTY;
560 1.45 thorpej goto out;
561 1.45 thorpej }
562 1.15 cgd memp = NULL;
563 1.42 perry if (size > sizeof(stkbuf)) {
564 1.15 cgd memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
565 1.15 cgd data = memp;
566 1.15 cgd } else
567 1.46 darrenr data = (caddr_t)stkbuf;
568 1.15 cgd if (com&IOC_IN) {
569 1.15 cgd if (size) {
570 1.31 cgd error = copyin(SCARG(uap, data), data, size);
571 1.15 cgd if (error) {
572 1.15 cgd if (memp)
573 1.15 cgd free(memp, M_IOCTLOPS);
574 1.45 thorpej goto out;
575 1.15 cgd }
576 1.15 cgd } else
577 1.16 cgd *(caddr_t *)data = SCARG(uap, data);
578 1.15 cgd } else if ((com&IOC_OUT) && size)
579 1.15 cgd /*
580 1.15 cgd * Zero the buffer so the user always
581 1.15 cgd * gets back something deterministic.
582 1.15 cgd */
583 1.44 perry memset(data, 0, size);
584 1.15 cgd else if (com&IOC_VOID)
585 1.16 cgd *(caddr_t *)data = SCARG(uap, data);
586 1.15 cgd
587 1.15 cgd switch (com) {
588 1.15 cgd
589 1.15 cgd case FIONBIO:
590 1.22 christos if ((tmp = *(int *)data) != 0)
591 1.15 cgd fp->f_flag |= FNONBLOCK;
592 1.15 cgd else
593 1.15 cgd fp->f_flag &= ~FNONBLOCK;
594 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
595 1.15 cgd break;
596 1.15 cgd
597 1.15 cgd case FIOASYNC:
598 1.22 christos if ((tmp = *(int *)data) != 0)
599 1.15 cgd fp->f_flag |= FASYNC;
600 1.15 cgd else
601 1.15 cgd fp->f_flag &= ~FASYNC;
602 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
603 1.15 cgd break;
604 1.15 cgd
605 1.15 cgd case FIOSETOWN:
606 1.15 cgd tmp = *(int *)data;
607 1.15 cgd if (fp->f_type == DTYPE_SOCKET) {
608 1.15 cgd ((struct socket *)fp->f_data)->so_pgid = tmp;
609 1.15 cgd error = 0;
610 1.15 cgd break;
611 1.15 cgd }
612 1.15 cgd if (tmp <= 0) {
613 1.15 cgd tmp = -tmp;
614 1.15 cgd } else {
615 1.15 cgd struct proc *p1 = pfind(tmp);
616 1.15 cgd if (p1 == 0) {
617 1.15 cgd error = ESRCH;
618 1.15 cgd break;
619 1.15 cgd }
620 1.15 cgd tmp = p1->p_pgrp->pg_id;
621 1.15 cgd }
622 1.15 cgd error = (*fp->f_ops->fo_ioctl)
623 1.24 cgd (fp, TIOCSPGRP, (caddr_t)&tmp, p);
624 1.15 cgd break;
625 1.15 cgd
626 1.15 cgd case FIOGETOWN:
627 1.15 cgd if (fp->f_type == DTYPE_SOCKET) {
628 1.15 cgd error = 0;
629 1.15 cgd *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
630 1.15 cgd break;
631 1.15 cgd }
632 1.17 cgd error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
633 1.15 cgd *(int *)data = -*(int *)data;
634 1.15 cgd break;
635 1.15 cgd
636 1.15 cgd default:
637 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
638 1.15 cgd /*
639 1.15 cgd * Copy any data to user, size was
640 1.15 cgd * already set and checked above.
641 1.15 cgd */
642 1.15 cgd if (error == 0 && (com&IOC_OUT) && size)
643 1.31 cgd error = copyout(data, SCARG(uap, data), size);
644 1.15 cgd break;
645 1.15 cgd }
646 1.15 cgd if (memp)
647 1.15 cgd free(memp, M_IOCTLOPS);
648 1.45 thorpej out:
649 1.45 thorpej FILE_UNUSE(fp, p);
650 1.15 cgd return (error);
651 1.15 cgd }
652 1.15 cgd
653 1.15 cgd int selwait, nselcoll;
654 1.15 cgd
655 1.15 cgd /*
656 1.15 cgd * Select system call.
657 1.15 cgd */
658 1.22 christos int
659 1.21 mycroft sys_select(p, v, retval)
660 1.15 cgd register struct proc *p;
661 1.20 thorpej void *v;
662 1.20 thorpej register_t *retval;
663 1.20 thorpej {
664 1.21 mycroft register struct sys_select_args /* {
665 1.35 thorpej syscallarg(int) nd;
666 1.16 cgd syscallarg(fd_set *) in;
667 1.16 cgd syscallarg(fd_set *) ou;
668 1.16 cgd syscallarg(fd_set *) ex;
669 1.16 cgd syscallarg(struct timeval *) tv;
670 1.20 thorpej } */ *uap = v;
671 1.25 mycroft caddr_t bits;
672 1.26 cgd char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
673 1.15 cgd struct timeval atv;
674 1.15 cgd int s, ncoll, error = 0, timo;
675 1.27 mycroft size_t ni;
676 1.15 cgd
677 1.35 thorpej if (SCARG(uap, nd) < 0)
678 1.35 thorpej return (EINVAL);
679 1.16 cgd if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
680 1.16 cgd /* forgiving; slightly wrong */
681 1.16 cgd SCARG(uap, nd) = p->p_fd->fd_nfiles;
682 1.16 cgd }
683 1.16 cgd ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
684 1.27 mycroft if (ni * 6 > sizeof(smallbits))
685 1.25 mycroft bits = malloc(ni * 6, M_TEMP, M_WAITOK);
686 1.25 mycroft else
687 1.26 cgd bits = smallbits;
688 1.15 cgd
689 1.15 cgd #define getbits(name, x) \
690 1.25 mycroft if (SCARG(uap, name)) { \
691 1.31 cgd error = copyin(SCARG(uap, name), bits + ni * x, ni); \
692 1.25 mycroft if (error) \
693 1.25 mycroft goto done; \
694 1.25 mycroft } else \
695 1.44 perry memset(bits + ni * x, 0, ni);
696 1.15 cgd getbits(in, 0);
697 1.15 cgd getbits(ou, 1);
698 1.15 cgd getbits(ex, 2);
699 1.15 cgd #undef getbits
700 1.15 cgd
701 1.16 cgd if (SCARG(uap, tv)) {
702 1.31 cgd error = copyin(SCARG(uap, tv), (caddr_t)&atv,
703 1.42 perry sizeof(atv));
704 1.15 cgd if (error)
705 1.15 cgd goto done;
706 1.15 cgd if (itimerfix(&atv)) {
707 1.15 cgd error = EINVAL;
708 1.15 cgd goto done;
709 1.15 cgd }
710 1.15 cgd s = splclock();
711 1.19 mycroft timeradd(&atv, &time, &atv);
712 1.15 cgd timo = hzto(&atv);
713 1.15 cgd /*
714 1.15 cgd * Avoid inadvertently sleeping forever.
715 1.15 cgd */
716 1.15 cgd if (timo == 0)
717 1.15 cgd timo = 1;
718 1.15 cgd splx(s);
719 1.15 cgd } else
720 1.15 cgd timo = 0;
721 1.15 cgd retry:
722 1.15 cgd ncoll = nselcoll;
723 1.15 cgd p->p_flag |= P_SELECT;
724 1.25 mycroft error = selscan(p, (fd_mask *)(bits + ni * 0),
725 1.25 mycroft (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
726 1.15 cgd if (error || *retval)
727 1.15 cgd goto done;
728 1.15 cgd s = splhigh();
729 1.27 mycroft if (timo && timercmp(&time, &atv, >=)) {
730 1.15 cgd splx(s);
731 1.15 cgd goto done;
732 1.15 cgd }
733 1.15 cgd if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
734 1.15 cgd splx(s);
735 1.15 cgd goto retry;
736 1.15 cgd }
737 1.15 cgd p->p_flag &= ~P_SELECT;
738 1.15 cgd error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
739 1.15 cgd splx(s);
740 1.15 cgd if (error == 0)
741 1.15 cgd goto retry;
742 1.15 cgd done:
743 1.15 cgd p->p_flag &= ~P_SELECT;
744 1.15 cgd /* select is not restarted after signals... */
745 1.15 cgd if (error == ERESTART)
746 1.15 cgd error = EINTR;
747 1.15 cgd if (error == EWOULDBLOCK)
748 1.15 cgd error = 0;
749 1.27 mycroft if (error == 0) {
750 1.15 cgd #define putbits(name, x) \
751 1.27 mycroft if (SCARG(uap, name)) { \
752 1.31 cgd error = copyout(bits + ni * x, SCARG(uap, name), ni); \
753 1.27 mycroft if (error) \
754 1.27 mycroft goto out; \
755 1.27 mycroft }
756 1.25 mycroft putbits(in, 3);
757 1.25 mycroft putbits(ou, 4);
758 1.25 mycroft putbits(ex, 5);
759 1.15 cgd #undef putbits
760 1.15 cgd }
761 1.27 mycroft out:
762 1.27 mycroft if (ni * 6 > sizeof(smallbits))
763 1.25 mycroft free(bits, M_TEMP);
764 1.15 cgd return (error);
765 1.15 cgd }
766 1.15 cgd
767 1.22 christos int
768 1.25 mycroft selscan(p, ibitp, obitp, nfd, retval)
769 1.15 cgd struct proc *p;
770 1.25 mycroft fd_mask *ibitp, *obitp;
771 1.16 cgd int nfd;
772 1.16 cgd register_t *retval;
773 1.15 cgd {
774 1.15 cgd register struct filedesc *fdp = p->p_fd;
775 1.15 cgd register int msk, i, j, fd;
776 1.25 mycroft register fd_mask ibits, obits;
777 1.15 cgd struct file *fp;
778 1.15 cgd int n = 0;
779 1.28 mycroft static int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
780 1.28 mycroft POLLWRNORM | POLLHUP | POLLERR,
781 1.28 mycroft POLLRDBAND };
782 1.15 cgd
783 1.15 cgd for (msk = 0; msk < 3; msk++) {
784 1.15 cgd for (i = 0; i < nfd; i += NFDBITS) {
785 1.25 mycroft ibits = *ibitp++;
786 1.25 mycroft obits = 0;
787 1.25 mycroft while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
788 1.25 mycroft ibits &= ~(1 << j);
789 1.15 cgd fp = fdp->fd_ofiles[fd];
790 1.45 thorpej if (fp == NULL ||
791 1.45 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0)
792 1.15 cgd return (EBADF);
793 1.45 thorpej FILE_USE(fp);
794 1.28 mycroft if ((*fp->f_ops->fo_poll)(fp, flag[msk], p)) {
795 1.25 mycroft obits |= (1 << j);
796 1.15 cgd n++;
797 1.15 cgd }
798 1.45 thorpej FILE_UNUSE(fp, p);
799 1.15 cgd }
800 1.25 mycroft *obitp++ = obits;
801 1.15 cgd }
802 1.15 cgd }
803 1.15 cgd *retval = n;
804 1.15 cgd return (0);
805 1.15 cgd }
806 1.15 cgd
807 1.28 mycroft /*
808 1.28 mycroft * Poll system call.
809 1.28 mycroft */
810 1.28 mycroft int
811 1.28 mycroft sys_poll(p, v, retval)
812 1.28 mycroft register struct proc *p;
813 1.28 mycroft void *v;
814 1.28 mycroft register_t *retval;
815 1.28 mycroft {
816 1.28 mycroft register struct sys_poll_args /* {
817 1.28 mycroft syscallarg(struct pollfd *) fds;
818 1.29 mycroft syscallarg(u_int) nfds;
819 1.28 mycroft syscallarg(int) timeout;
820 1.28 mycroft } */ *uap = v;
821 1.28 mycroft caddr_t bits;
822 1.28 mycroft char smallbits[32 * sizeof(struct pollfd)];
823 1.28 mycroft struct timeval atv;
824 1.28 mycroft int s, ncoll, error = 0, timo;
825 1.28 mycroft size_t ni;
826 1.28 mycroft
827 1.28 mycroft if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
828 1.28 mycroft /* forgiving; slightly wrong */
829 1.28 mycroft SCARG(uap, nfds) = p->p_fd->fd_nfiles;
830 1.28 mycroft }
831 1.28 mycroft ni = SCARG(uap, nfds) * sizeof(struct pollfd);
832 1.28 mycroft if (ni > sizeof(smallbits))
833 1.28 mycroft bits = malloc(ni, M_TEMP, M_WAITOK);
834 1.28 mycroft else
835 1.28 mycroft bits = smallbits;
836 1.28 mycroft
837 1.31 cgd error = copyin(SCARG(uap, fds), bits, ni);
838 1.28 mycroft if (error)
839 1.28 mycroft goto done;
840 1.28 mycroft
841 1.30 mycroft if (SCARG(uap, timeout) != INFTIM) {
842 1.28 mycroft atv.tv_sec = SCARG(uap, timeout) / 1000;
843 1.28 mycroft atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
844 1.28 mycroft if (itimerfix(&atv)) {
845 1.28 mycroft error = EINVAL;
846 1.28 mycroft goto done;
847 1.28 mycroft }
848 1.28 mycroft s = splclock();
849 1.28 mycroft timeradd(&atv, &time, &atv);
850 1.28 mycroft timo = hzto(&atv);
851 1.28 mycroft /*
852 1.28 mycroft * Avoid inadvertently sleeping forever.
853 1.28 mycroft */
854 1.28 mycroft if (timo == 0)
855 1.28 mycroft timo = 1;
856 1.28 mycroft splx(s);
857 1.28 mycroft } else
858 1.28 mycroft timo = 0;
859 1.28 mycroft retry:
860 1.28 mycroft ncoll = nselcoll;
861 1.28 mycroft p->p_flag |= P_SELECT;
862 1.28 mycroft error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds), retval);
863 1.28 mycroft if (error || *retval)
864 1.28 mycroft goto done;
865 1.28 mycroft s = splhigh();
866 1.28 mycroft if (timo && timercmp(&time, &atv, >=)) {
867 1.28 mycroft splx(s);
868 1.28 mycroft goto done;
869 1.28 mycroft }
870 1.28 mycroft if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
871 1.28 mycroft splx(s);
872 1.28 mycroft goto retry;
873 1.28 mycroft }
874 1.28 mycroft p->p_flag &= ~P_SELECT;
875 1.28 mycroft error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
876 1.28 mycroft splx(s);
877 1.28 mycroft if (error == 0)
878 1.28 mycroft goto retry;
879 1.28 mycroft done:
880 1.28 mycroft p->p_flag &= ~P_SELECT;
881 1.28 mycroft /* poll is not restarted after signals... */
882 1.28 mycroft if (error == ERESTART)
883 1.28 mycroft error = EINTR;
884 1.28 mycroft if (error == EWOULDBLOCK)
885 1.28 mycroft error = 0;
886 1.28 mycroft if (error == 0) {
887 1.31 cgd error = copyout(bits, SCARG(uap, fds), ni);
888 1.28 mycroft if (error)
889 1.28 mycroft goto out;
890 1.28 mycroft }
891 1.28 mycroft out:
892 1.28 mycroft if (ni > sizeof(smallbits))
893 1.28 mycroft free(bits, M_TEMP);
894 1.28 mycroft return (error);
895 1.28 mycroft }
896 1.28 mycroft
897 1.28 mycroft int
898 1.28 mycroft pollscan(p, fds, nfd, retval)
899 1.28 mycroft struct proc *p;
900 1.28 mycroft struct pollfd *fds;
901 1.28 mycroft int nfd;
902 1.28 mycroft register_t *retval;
903 1.28 mycroft {
904 1.28 mycroft register struct filedesc *fdp = p->p_fd;
905 1.28 mycroft int i;
906 1.28 mycroft struct file *fp;
907 1.28 mycroft int n = 0;
908 1.28 mycroft
909 1.28 mycroft for (i = 0; i < nfd; i++, fds++) {
910 1.33 mrg if ((u_int)fds->fd >= fdp->fd_nfiles) {
911 1.28 mycroft fds->revents = POLLNVAL;
912 1.28 mycroft n++;
913 1.28 mycroft } else {
914 1.32 mrg fp = fdp->fd_ofiles[fds->fd];
915 1.45 thorpej if (fp == NULL ||
916 1.45 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0) {
917 1.32 mrg fds->revents = POLLNVAL;
918 1.28 mycroft n++;
919 1.32 mrg } else {
920 1.45 thorpej FILE_USE(fp);
921 1.32 mrg fds->revents = (*fp->f_ops->fo_poll)(fp,
922 1.32 mrg fds->events | POLLERR | POLLHUP, p);
923 1.32 mrg if (fds->revents != 0)
924 1.32 mrg n++;
925 1.45 thorpej FILE_UNUSE(fp, p);
926 1.32 mrg }
927 1.28 mycroft }
928 1.28 mycroft }
929 1.28 mycroft *retval = n;
930 1.28 mycroft return (0);
931 1.28 mycroft }
932 1.28 mycroft
933 1.15 cgd /*ARGSUSED*/
934 1.22 christos int
935 1.28 mycroft seltrue(dev, events, p)
936 1.15 cgd dev_t dev;
937 1.28 mycroft int events;
938 1.15 cgd struct proc *p;
939 1.15 cgd {
940 1.15 cgd
941 1.28 mycroft return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
942 1.15 cgd }
943 1.15 cgd
944 1.15 cgd /*
945 1.15 cgd * Record a select request.
946 1.15 cgd */
947 1.15 cgd void
948 1.15 cgd selrecord(selector, sip)
949 1.15 cgd struct proc *selector;
950 1.15 cgd struct selinfo *sip;
951 1.15 cgd {
952 1.15 cgd struct proc *p;
953 1.15 cgd pid_t mypid;
954 1.15 cgd
955 1.15 cgd mypid = selector->p_pid;
956 1.15 cgd if (sip->si_pid == mypid)
957 1.15 cgd return;
958 1.15 cgd if (sip->si_pid && (p = pfind(sip->si_pid)) &&
959 1.15 cgd p->p_wchan == (caddr_t)&selwait)
960 1.15 cgd sip->si_flags |= SI_COLL;
961 1.15 cgd else
962 1.15 cgd sip->si_pid = mypid;
963 1.15 cgd }
964 1.15 cgd
965 1.15 cgd /*
966 1.15 cgd * Do a wakeup when a selectable event occurs.
967 1.15 cgd */
968 1.15 cgd void
969 1.15 cgd selwakeup(sip)
970 1.15 cgd register struct selinfo *sip;
971 1.15 cgd {
972 1.15 cgd register struct proc *p;
973 1.15 cgd int s;
974 1.15 cgd
975 1.15 cgd if (sip->si_pid == 0)
976 1.15 cgd return;
977 1.15 cgd if (sip->si_flags & SI_COLL) {
978 1.15 cgd nselcoll++;
979 1.15 cgd sip->si_flags &= ~SI_COLL;
980 1.15 cgd wakeup((caddr_t)&selwait);
981 1.15 cgd }
982 1.15 cgd p = pfind(sip->si_pid);
983 1.15 cgd sip->si_pid = 0;
984 1.15 cgd if (p != NULL) {
985 1.15 cgd s = splhigh();
986 1.15 cgd if (p->p_wchan == (caddr_t)&selwait) {
987 1.15 cgd if (p->p_stat == SSLEEP)
988 1.15 cgd setrunnable(p);
989 1.15 cgd else
990 1.15 cgd unsleep(p);
991 1.15 cgd } else if (p->p_flag & P_SELECT)
992 1.15 cgd p->p_flag &= ~P_SELECT;
993 1.15 cgd splx(s);
994 1.15 cgd }
995 1.15 cgd }
996