sys_generic.c revision 1.100.2.3 1 /* $NetBSD: sys_generic.c,v 1.100.2.3 2007/04/10 00:22:12 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.3 2007/04/10 00:22:12 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) {
875
876 #define putbits(name, x) \
877 if (u_ ## name) { \
878 error = copyout(bits + ni * x, u_ ## name, ni); \
879 if (error) \
880 goto out; \
881 }
882 putbits(in, 3);
883 putbits(ou, 4);
884 putbits(ex, 5);
885 #undef putbits
886 }
887 out:
888 if (bits != smallbits)
889 kmem_free(bits, ni * 6);
890 return (error);
891 }
892
893 int
894 selscan(struct lwp *l, fd_mask *ibitp, fd_mask *obitp, int nfd,
895 register_t *retval)
896 {
897 static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
898 POLLWRNORM | POLLHUP | POLLERR,
899 POLLRDBAND };
900 struct proc *p = l->l_proc;
901 struct filedesc *fdp;
902 int msk, i, j, fd, n;
903 fd_mask ibits, obits;
904 struct file *fp;
905
906 fdp = p->p_fd;
907 n = 0;
908 for (msk = 0; msk < 3; msk++) {
909 for (i = 0; i < nfd; i += NFDBITS) {
910 ibits = *ibitp++;
911 obits = 0;
912 while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
913 ibits &= ~(1 << j);
914 if ((fp = fd_getfile(fdp, fd)) == NULL)
915 return (EBADF);
916 FILE_USE(fp);
917 if ((*fp->f_ops->fo_poll)(fp, flag[msk], l)) {
918 obits |= (1 << j);
919 n++;
920 }
921 FILE_UNUSE(fp, l);
922 }
923 *obitp++ = obits;
924 }
925 }
926 *retval = n;
927 return (0);
928 }
929
930 /*
931 * Poll system call.
932 */
933 int
934 sys_poll(struct lwp *l, void *v, register_t *retval)
935 {
936 struct sys_poll_args /* {
937 syscallarg(struct pollfd *) fds;
938 syscallarg(u_int) nfds;
939 syscallarg(int) timeout;
940 } */ * const uap = v;
941 struct timeval atv, *tv = NULL;
942
943 if (SCARG(uap, timeout) != INFTIM) {
944 atv.tv_sec = SCARG(uap, timeout) / 1000;
945 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
946 tv = &atv;
947 }
948
949 return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
950 tv, NULL);
951 }
952
953 /*
954 * Poll system call.
955 */
956 int
957 sys_pollts(struct lwp *l, void *v, register_t *retval)
958 {
959 struct sys_pollts_args /* {
960 syscallarg(struct pollfd *) fds;
961 syscallarg(u_int) nfds;
962 syscallarg(const struct timespec *) ts;
963 syscallarg(const sigset_t *) mask;
964 } */ * const uap = v;
965 struct timespec ats;
966 struct timeval atv, *tv = NULL;
967 sigset_t amask, *mask = NULL;
968 int error;
969
970 if (SCARG(uap, ts)) {
971 error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
972 if (error)
973 return error;
974 atv.tv_sec = ats.tv_sec;
975 atv.tv_usec = ats.tv_nsec / 1000;
976 tv = &atv;
977 }
978 if (SCARG(uap, mask)) {
979 error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
980 if (error)
981 return error;
982 mask = &amask;
983 }
984
985 return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
986 tv, mask);
987 }
988
989 int
990 pollcommon(struct lwp *l, register_t *retval,
991 struct pollfd *u_fds, u_int nfds,
992 struct timeval *tv, sigset_t *mask)
993 {
994 char smallbits[32 * sizeof(struct pollfd)];
995 struct proc * const p = l->l_proc;
996 void * bits;
997 sigset_t oldmask;
998 int ncoll, error, timo;
999 size_t ni;
1000 struct timeval sleeptv;
1001
1002 if (nfds > p->p_fd->fd_nfiles) {
1003 /* forgiving; slightly wrong */
1004 nfds = p->p_fd->fd_nfiles;
1005 }
1006 ni = nfds * sizeof(struct pollfd);
1007 if (ni > sizeof(smallbits))
1008 bits = kmem_alloc(ni, KM_SLEEP);
1009 else
1010 bits = smallbits;
1011
1012 error = copyin(u_fds, bits, ni);
1013 if (error)
1014 goto done;
1015
1016 timo = 0;
1017 if (tv && inittimeleft(tv, &sleeptv) == -1) {
1018 error = EINVAL;
1019 goto done;
1020 }
1021
1022 if (mask) {
1023 sigminusset(&sigcantmask, mask);
1024 mutex_enter(&p->p_smutex);
1025 oldmask = l->l_sigmask;
1026 l->l_sigmask = *mask;
1027 mutex_exit(&p->p_smutex);
1028 } else
1029 oldmask = l->l_sigmask; /* XXXgcc */
1030
1031 mutex_enter(&select_lock);
1032 for (;;) {
1033 ncoll = nselcoll;
1034 l->l_selflag = 1;
1035 mutex_exit(&select_lock);
1036
1037 error = pollscan(l, (struct pollfd *)bits, nfds, retval);
1038
1039 mutex_enter(&select_lock);
1040 if (error || *retval)
1041 break;
1042 if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
1043 break;
1044 if (l->l_selflag == 0 || nselcoll != ncoll)
1045 continue;
1046 l->l_selflag = 0;
1047 error = cv_timedwait_sig(&select_cv, &select_lock, timo);
1048 if (error != 0)
1049 break;
1050 }
1051 selclear();
1052 mutex_exit(&select_lock);
1053
1054 if (mask) {
1055 mutex_enter(&p->p_smutex);
1056 l->l_sigmask = oldmask;
1057 mutex_exit(&p->p_smutex);
1058 }
1059 done:
1060 /* poll is not restarted after signals... */
1061 if (error == ERESTART)
1062 error = EINTR;
1063 if (error == EWOULDBLOCK)
1064 error = 0;
1065 if (error == 0) {
1066 error = copyout(bits, u_fds, ni);
1067 if (error)
1068 goto out;
1069 }
1070 out:
1071 if (bits != smallbits)
1072 kmem_free(bits, ni);
1073 return (error);
1074 }
1075
1076 int
1077 pollscan(struct lwp *l, struct pollfd *fds, int nfd, register_t *retval)
1078 {
1079 struct proc *p = l->l_proc;
1080 struct filedesc *fdp;
1081 int i, n;
1082 struct file *fp;
1083
1084 fdp = p->p_fd;
1085 n = 0;
1086 for (i = 0; i < nfd; i++, fds++) {
1087 if (fds->fd >= fdp->fd_nfiles) {
1088 fds->revents = POLLNVAL;
1089 n++;
1090 } else if (fds->fd < 0) {
1091 fds->revents = 0;
1092 } else {
1093 if ((fp = fd_getfile(fdp, fds->fd)) == NULL) {
1094 fds->revents = POLLNVAL;
1095 n++;
1096 } else {
1097 FILE_USE(fp);
1098 fds->revents = (*fp->f_ops->fo_poll)(fp,
1099 fds->events | POLLERR | POLLHUP, l);
1100 if (fds->revents != 0)
1101 n++;
1102 FILE_UNUSE(fp, l);
1103 }
1104 }
1105 }
1106 *retval = n;
1107 return (0);
1108 }
1109
1110 /*ARGSUSED*/
1111 int
1112 seltrue(dev_t dev, int events, struct lwp *l)
1113 {
1114
1115 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1116 }
1117
1118 /*
1119 * Record a select request.
1120 */
1121 void
1122 selrecord(struct lwp *selector, struct selinfo *sip)
1123 {
1124
1125 mutex_enter(&select_lock);
1126 if (sip->sel_lwp == NULL) {
1127 /* First waiter. */
1128 sip->sel_lwp = selector;
1129 TAILQ_INSERT_TAIL(&selector->l_selwait, sip, sel_chain);
1130 } else if (sip->sel_lwp != selector) {
1131 /* More than 2 waiters. */
1132 sip->sel_collision = true;
1133 }
1134 mutex_exit(&select_lock);
1135 }
1136
1137 /*
1138 * Do a wakeup when a selectable event occurs.
1139 */
1140 void
1141 selwakeup(struct selinfo *sip)
1142 {
1143 struct lwp *l;
1144
1145 mutex_enter(&select_lock);
1146 if (sip->sel_collision) {
1147 /* Multiple waiters - just notify everybody. */
1148 nselcoll++;
1149 sip->sel_collision = false;
1150 cv_broadcast(&select_cv);
1151 } else if (sip->sel_lwp != NULL) {
1152 /* Only one LWP waiting. */
1153 l = sip->sel_lwp;
1154 if (l->l_selflag != 0) {
1155 /* Not yet asleep - make it go around again. */
1156 l->l_selflag = 0;
1157 } else {
1158 /*
1159 * If it's sleeping, wake it up. If not, it's already
1160 * awake but hasn't had a chance to remove itself from
1161 * the selector yet.
1162 */
1163 lwp_lock(l);
1164 if (l->l_wchan == &select_cv) {
1165 /* lwp_unsleep() releases the LWP lock. */
1166 lwp_unsleep(l);
1167 } else
1168 lwp_unlock(l);
1169 }
1170 }
1171 mutex_exit(&select_lock);
1172 }
1173
1174 void
1175 selnotify(struct selinfo *sip, long knhint)
1176 {
1177
1178 selwakeup(sip);
1179 KNOTE(&sip->sel_klist, knhint);
1180 }
1181
1182 /*
1183 * Remove an LWP from all objects that it is waiting for.
1184 */
1185 static void
1186 selclear(void)
1187 {
1188 struct selinfo *sip;
1189 struct lwp *l = curlwp;
1190
1191 KASSERT(mutex_owned(&select_lock));
1192
1193 TAILQ_FOREACH(sip, &l->l_selwait, sel_chain) {
1194 KASSERT(sip->sel_lwp == l);
1195 sip->sel_lwp = NULL;
1196 }
1197 TAILQ_INIT(&l->l_selwait);
1198 }
1199
1200 /*
1201 * Initialize the select/poll system calls.
1202 */
1203 void
1204 selsysinit(void)
1205 {
1206
1207 mutex_init(&select_lock, MUTEX_DRIVER, IPL_VM);
1208 cv_init(&select_cv, "select");
1209 }
1210