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