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