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