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