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