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