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