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