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