sys_generic.c revision 1.54.2.4 1 /* $NetBSD: sys_generic.c,v 1.54.2.4 2001/11/14 19:16:41 nathanw 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.54.2.4 2001/11/14 19:16:41 nathanw 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/lwp.h>
54 #include <sys/proc.h>
55 #include <sys/socketvar.h>
56 #include <sys/signalvar.h>
57 #include <sys/uio.h>
58 #include <sys/kernel.h>
59 #include <sys/stat.h>
60 #include <sys/malloc.h>
61 #include <sys/poll.h>
62 #ifdef KTRACE
63 #include <sys/ktrace.h>
64 #endif
65
66 #include <sys/mount.h>
67 #include <sys/syscallargs.h>
68
69 int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
70 int pollscan __P((struct proc *, struct pollfd *, int, register_t *));
71
72 /*
73 * Read system call.
74 */
75 /* ARGSUSED */
76 int
77 sys_read(struct lwp *l, void *v, register_t *retval)
78 {
79 struct sys_read_args /* {
80 syscallarg(int) fd;
81 syscallarg(void *) buf;
82 syscallarg(size_t) nbyte;
83 } */ *uap = v;
84 int fd;
85 struct file *fp;
86 struct proc *p;
87 struct filedesc *fdp;
88
89 fd = SCARG(uap, fd);
90 p = l->l_proc;
91 fdp = p->p_fd;
92
93 if ((fp = fd_getfile(fdp, fd)) == NULL)
94 return (EBADF);
95
96 if ((fp->f_flag & FREAD) == 0)
97 return (EBADF);
98
99 FILE_USE(fp);
100
101 /* dofileread() will unuse the descriptor for us */
102 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
103 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
104 }
105
106 int
107 dofileread(struct proc *p, int fd, struct file *fp, void *buf, size_t nbyte,
108 off_t *offset, int flags, register_t *retval)
109 {
110 struct uio auio;
111 struct iovec aiov;
112 long cnt, error;
113 #ifdef KTRACE
114 struct iovec ktriov;
115 #endif
116 error = 0;
117
118 aiov.iov_base = (caddr_t)buf;
119 aiov.iov_len = nbyte;
120 auio.uio_iov = &aiov;
121 auio.uio_iovcnt = 1;
122 auio.uio_resid = nbyte;
123 auio.uio_rw = UIO_READ;
124 auio.uio_segflg = UIO_USERSPACE;
125 auio.uio_procp = p;
126
127 /*
128 * Reads return ssize_t because -1 is returned on error. Therefore
129 * we must restrict the length to SSIZE_MAX to avoid garbage return
130 * values.
131 */
132 if (auio.uio_resid > SSIZE_MAX) {
133 error = EINVAL;
134 goto out;
135 }
136
137 #ifdef KTRACE
138 /*
139 * if tracing, save a copy of iovec
140 */
141 if (KTRPOINT(p, KTR_GENIO))
142 ktriov = aiov;
143 #endif
144 cnt = auio.uio_resid;
145 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
146 if (error)
147 if (auio.uio_resid != cnt && (error == ERESTART ||
148 error == EINTR || error == EWOULDBLOCK))
149 error = 0;
150 cnt -= auio.uio_resid;
151 #ifdef KTRACE
152 if (KTRPOINT(p, KTR_GENIO) && error == 0)
153 ktrgenio(p, fd, UIO_READ, &ktriov, cnt, error);
154 #endif
155 *retval = cnt;
156 out:
157 FILE_UNUSE(fp, p);
158 return (error);
159 }
160
161 /*
162 * Scatter read system call.
163 */
164 int
165 sys_readv(struct lwp *l, void *v, register_t *retval)
166 {
167 struct sys_readv_args /* {
168 syscallarg(int) fd;
169 syscallarg(const struct iovec *) iovp;
170 syscallarg(int) iovcnt;
171 } */ *uap = v;
172 int fd;
173 struct file *fp;
174 struct proc *p;
175 struct filedesc *fdp;
176
177 fd = SCARG(uap, fd);
178 p = l->l_proc;
179 fdp = p->p_fd;
180
181 if ((fp = fd_getfile(fdp, fd)) == NULL)
182 return (EBADF);
183
184 if ((fp->f_flag & FREAD) == 0)
185 return (EBADF);
186
187 FILE_USE(fp);
188
189 /* dofilereadv() will unuse the descriptor for us */
190 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
191 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
192 }
193
194 int
195 dofilereadv(struct proc *p, int fd, struct file *fp, const struct iovec *iovp,
196 int iovcnt, off_t *offset, int flags, register_t *retval)
197 {
198 struct uio auio;
199 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
200 long i, cnt, error;
201 u_int iovlen;
202 #ifdef KTRACE
203 struct iovec *ktriov;
204 #endif
205
206 error = 0;
207 #ifdef KTRACE
208 ktriov = NULL;
209 #endif
210 /* note: can't use iovlen until iovcnt is validated */
211 iovlen = iovcnt * sizeof(struct iovec);
212 if ((u_int)iovcnt > UIO_SMALLIOV) {
213 if ((u_int)iovcnt > IOV_MAX) {
214 error = EINVAL;
215 goto out;
216 }
217 iov = malloc(iovlen, M_IOV, M_WAITOK);
218 needfree = iov;
219 } else if ((u_int)iovcnt > 0) {
220 iov = aiov;
221 needfree = NULL;
222 } else {
223 error = EINVAL;
224 goto out;
225 }
226
227 auio.uio_iov = iov;
228 auio.uio_iovcnt = iovcnt;
229 auio.uio_rw = UIO_READ;
230 auio.uio_segflg = UIO_USERSPACE;
231 auio.uio_procp = p;
232 error = copyin(iovp, iov, iovlen);
233 if (error)
234 goto done;
235 auio.uio_resid = 0;
236 for (i = 0; i < iovcnt; i++) {
237 auio.uio_resid += iov->iov_len;
238 /*
239 * Reads return ssize_t because -1 is returned on error.
240 * Therefore we must restrict the length to SSIZE_MAX to
241 * avoid garbage return values.
242 */
243 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
244 error = EINVAL;
245 goto done;
246 }
247 iov++;
248 }
249 #ifdef KTRACE
250 /*
251 * if tracing, save a copy of iovec
252 */
253 if (KTRPOINT(p, KTR_GENIO)) {
254 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
255 memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
256 }
257 #endif
258 cnt = auio.uio_resid;
259 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
260 if (error)
261 if (auio.uio_resid != cnt && (error == ERESTART ||
262 error == EINTR || error == EWOULDBLOCK))
263 error = 0;
264 cnt -= auio.uio_resid;
265 #ifdef KTRACE
266 if (ktriov != NULL) {
267 if (error == 0)
268 ktrgenio(p, fd, UIO_READ, ktriov, cnt, error);
269 free(ktriov, M_TEMP);
270 }
271 #endif
272 *retval = cnt;
273 done:
274 if (needfree)
275 free(needfree, M_IOV);
276 out:
277 FILE_UNUSE(fp, p);
278 return (error);
279 }
280
281 /*
282 * Write system call
283 */
284 int
285 sys_write(struct lwp *l, void *v, register_t *retval)
286 {
287 struct sys_write_args /* {
288 syscallarg(int) fd;
289 syscallarg(const void *) buf;
290 syscallarg(size_t) nbyte;
291 } */ *uap = v;
292 int fd;
293 struct file *fp;
294 struct proc *p;
295 struct filedesc *fdp;
296
297 fd = SCARG(uap, fd);
298 p = l->l_proc;
299 fdp = p->p_fd;
300
301 if ((fp = fd_getfile(fdp, fd)) == NULL)
302 return (EBADF);
303
304 if ((fp->f_flag & FWRITE) == 0)
305 return (EBADF);
306
307 FILE_USE(fp);
308
309 /* dofilewrite() will unuse the descriptor for us */
310 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
311 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
312 }
313
314 int
315 dofilewrite(struct proc *p, int fd, struct file *fp, const void *buf,
316 size_t nbyte, off_t *offset, int flags, register_t *retval)
317 {
318 struct uio auio;
319 struct iovec aiov;
320 long cnt, error;
321 #ifdef KTRACE
322 struct iovec ktriov;
323 #endif
324
325 error = 0;
326 aiov.iov_base = (caddr_t)buf; /* XXX kills const */
327 aiov.iov_len = nbyte;
328 auio.uio_iov = &aiov;
329 auio.uio_iovcnt = 1;
330 auio.uio_resid = nbyte;
331 auio.uio_rw = UIO_WRITE;
332 auio.uio_segflg = UIO_USERSPACE;
333 auio.uio_procp = p;
334
335 /*
336 * Writes return ssize_t because -1 is returned on error. Therefore
337 * we must restrict the length to SSIZE_MAX to avoid garbage return
338 * values.
339 */
340 if (auio.uio_resid > SSIZE_MAX) {
341 error = EINVAL;
342 goto out;
343 }
344
345 #ifdef KTRACE
346 /*
347 * if tracing, save a copy of iovec
348 */
349 if (KTRPOINT(p, KTR_GENIO))
350 ktriov = aiov;
351 #endif
352 cnt = auio.uio_resid;
353 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
354 if (error) {
355 if (auio.uio_resid != cnt && (error == ERESTART ||
356 error == EINTR || error == EWOULDBLOCK))
357 error = 0;
358 if (error == EPIPE)
359 psignal(p, SIGPIPE);
360 }
361 cnt -= auio.uio_resid;
362 #ifdef KTRACE
363 if (KTRPOINT(p, KTR_GENIO) && error == 0)
364 ktrgenio(p, fd, UIO_WRITE, &ktriov, cnt, error);
365 #endif
366 *retval = cnt;
367 out:
368 FILE_UNUSE(fp, p);
369 return (error);
370 }
371
372 /*
373 * Gather write system call
374 */
375 int
376 sys_writev(struct lwp *l, void *v, register_t *retval)
377 {
378 struct sys_writev_args /* {
379 syscallarg(int) fd;
380 syscallarg(const struct iovec *) iovp;
381 syscallarg(int) iovcnt;
382 } */ *uap = v;
383 int fd;
384 struct file *fp;
385 struct proc *p;
386 struct filedesc *fdp;
387
388 fd = SCARG(uap, fd);
389 p = l->l_proc;
390 fdp = p->p_fd;
391
392 if ((fp = fd_getfile(fdp, fd)) == NULL)
393 return (EBADF);
394
395 if ((fp->f_flag & FWRITE) == 0)
396 return (EBADF);
397
398 FILE_USE(fp);
399
400 /* dofilewritev() will unuse the descriptor for us */
401 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
402 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
403 }
404
405 int
406 dofilewritev(struct proc *p, int fd, struct file *fp, const struct iovec *iovp,
407 int iovcnt, off_t *offset, int flags, register_t *retval)
408 {
409 struct uio auio;
410 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
411 long i, cnt, error;
412 u_int iovlen;
413 #ifdef KTRACE
414 struct iovec *ktriov;
415 #endif
416
417 error = 0;
418 #ifdef KTRACE
419 ktriov = NULL;
420 #endif
421 /* note: can't use iovlen until iovcnt is validated */
422 iovlen = iovcnt * sizeof(struct iovec);
423 if ((u_int)iovcnt > UIO_SMALLIOV) {
424 if ((u_int)iovcnt > IOV_MAX)
425 return (EINVAL);
426 iov = malloc(iovlen, M_IOV, M_WAITOK);
427 needfree = iov;
428 } else if ((u_int)iovcnt > 0) {
429 iov = aiov;
430 needfree = NULL;
431 } else {
432 error = EINVAL;
433 goto out;
434 }
435
436 auio.uio_iov = iov;
437 auio.uio_iovcnt = iovcnt;
438 auio.uio_rw = UIO_WRITE;
439 auio.uio_segflg = UIO_USERSPACE;
440 auio.uio_procp = p;
441 error = copyin(iovp, iov, iovlen);
442 if (error)
443 goto done;
444 auio.uio_resid = 0;
445 for (i = 0; i < iovcnt; i++) {
446 auio.uio_resid += iov->iov_len;
447 /*
448 * Writes return ssize_t because -1 is returned on error.
449 * Therefore we must restrict the length to SSIZE_MAX to
450 * avoid garbage return values.
451 */
452 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
453 error = EINVAL;
454 goto done;
455 }
456 iov++;
457 }
458 #ifdef KTRACE
459 /*
460 * if tracing, save a copy of iovec
461 */
462 if (KTRPOINT(p, KTR_GENIO)) {
463 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
464 memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
465 }
466 #endif
467 cnt = auio.uio_resid;
468 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
469 if (error) {
470 if (auio.uio_resid != cnt && (error == ERESTART ||
471 error == EINTR || error == EWOULDBLOCK))
472 error = 0;
473 if (error == EPIPE)
474 psignal(p, SIGPIPE);
475 }
476 cnt -= auio.uio_resid;
477 #ifdef KTRACE
478 if (KTRPOINT(p, KTR_GENIO))
479 if (error == 0) {
480 ktrgenio(p, fd, UIO_WRITE, ktriov, cnt, error);
481 free(ktriov, M_TEMP);
482 }
483 #endif
484 *retval = cnt;
485 done:
486 if (needfree)
487 free(needfree, M_IOV);
488 out:
489 FILE_UNUSE(fp, p);
490 return (error);
491 }
492
493 /*
494 * Ioctl system call
495 */
496 /* ARGSUSED */
497 int
498 sys_ioctl(struct lwp *l, void *v, register_t *retval)
499 {
500 struct sys_ioctl_args /* {
501 syscallarg(int) fd;
502 syscallarg(u_long) com;
503 syscallarg(caddr_t) data;
504 } */ *uap = v;
505 struct file *fp;
506 struct proc *p;
507 struct filedesc *fdp;
508 u_long com;
509 int error;
510 u_int size;
511 caddr_t data, memp;
512 int tmp;
513 #define STK_PARAMS 128
514 u_long stkbuf[STK_PARAMS/sizeof(u_long)];
515
516 error = 0;
517 p = l->l_proc;
518 fdp = p->p_fd;
519
520 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
521 return (EBADF);
522
523 FILE_USE(fp);
524
525 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
526 error = EBADF;
527 goto out;
528 }
529
530 switch (com = SCARG(uap, com)) {
531 case FIONCLEX:
532 fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
533 goto out;
534
535 case FIOCLEX:
536 fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
537 goto out;
538 }
539
540 /*
541 * Interpret high order word to find amount of data to be
542 * copied to/from the user's address space.
543 */
544 size = IOCPARM_LEN(com);
545 if (size > IOCPARM_MAX) {
546 error = ENOTTY;
547 goto out;
548 }
549 memp = NULL;
550 if (size > sizeof(stkbuf)) {
551 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
552 data = memp;
553 } else
554 data = (caddr_t)stkbuf;
555 if (com&IOC_IN) {
556 if (size) {
557 error = copyin(SCARG(uap, data), data, size);
558 if (error) {
559 if (memp)
560 free(memp, M_IOCTLOPS);
561 goto out;
562 }
563 } else
564 *(caddr_t *)data = SCARG(uap, data);
565 } else if ((com&IOC_OUT) && size)
566 /*
567 * Zero the buffer so the user always
568 * gets back something deterministic.
569 */
570 memset(data, 0, size);
571 else if (com&IOC_VOID)
572 *(caddr_t *)data = SCARG(uap, data);
573
574 switch (com) {
575
576 case FIONBIO:
577 if ((tmp = *(int *)data) != 0)
578 fp->f_flag |= FNONBLOCK;
579 else
580 fp->f_flag &= ~FNONBLOCK;
581 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
582 break;
583
584 case FIOASYNC:
585 if ((tmp = *(int *)data) != 0)
586 fp->f_flag |= FASYNC;
587 else
588 fp->f_flag &= ~FASYNC;
589 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
590 break;
591
592 case FIOSETOWN:
593 tmp = *(int *)data;
594 if (fp->f_type == DTYPE_SOCKET) {
595 ((struct socket *)fp->f_data)->so_pgid = tmp;
596 error = 0;
597 break;
598 }
599 if (tmp <= 0) {
600 tmp = -tmp;
601 } else {
602 struct proc *p1 = pfind(tmp);
603 if (p1 == 0) {
604 error = ESRCH;
605 break;
606 }
607 tmp = p1->p_pgrp->pg_id;
608 }
609 error = (*fp->f_ops->fo_ioctl)
610 (fp, TIOCSPGRP, (caddr_t)&tmp, p);
611 break;
612
613 case FIOGETOWN:
614 if (fp->f_type == DTYPE_SOCKET) {
615 error = 0;
616 *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
617 break;
618 }
619 error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
620 if (error == 0)
621 *(int *)data = -*(int *)data;
622 break;
623
624 default:
625 error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
626 /*
627 * Copy any data to user, size was
628 * already set and checked above.
629 */
630 if (error == 0 && (com&IOC_OUT) && size)
631 error = copyout(data, SCARG(uap, data), size);
632 break;
633 }
634 if (memp)
635 free(memp, M_IOCTLOPS);
636 out:
637 FILE_UNUSE(fp, p);
638 return (error);
639 }
640
641 int selwait, nselcoll;
642
643 /*
644 * Select system call.
645 */
646 int
647 sys_select(struct lwp *l, void *v, register_t *retval)
648 {
649 struct sys_select_args /* {
650 syscallarg(int) nd;
651 syscallarg(fd_set *) in;
652 syscallarg(fd_set *) ou;
653 syscallarg(fd_set *) ex;
654 syscallarg(struct timeval *) tv;
655 } */ *uap = v;
656 struct proc *p;
657 caddr_t bits;
658 char smallbits[howmany(FD_SETSIZE, NFDBITS) *
659 sizeof(fd_mask) * 6];
660 struct timeval atv;
661 int s, ncoll, error, timo;
662 size_t ni;
663
664 error = 0;
665 p = l->l_proc;
666 if (SCARG(uap, nd) < 0)
667 return (EINVAL);
668 if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
669 /* forgiving; slightly wrong */
670 SCARG(uap, nd) = p->p_fd->fd_nfiles;
671 }
672 ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
673 if (ni * 6 > sizeof(smallbits))
674 bits = malloc(ni * 6, M_TEMP, M_WAITOK);
675 else
676 bits = smallbits;
677
678 #define getbits(name, x) \
679 if (SCARG(uap, name)) { \
680 error = copyin(SCARG(uap, name), bits + ni * x, ni); \
681 if (error) \
682 goto done; \
683 } else \
684 memset(bits + ni * x, 0, ni);
685 getbits(in, 0);
686 getbits(ou, 1);
687 getbits(ex, 2);
688 #undef getbits
689
690 if (SCARG(uap, tv)) {
691 error = copyin(SCARG(uap, tv), (caddr_t)&atv,
692 sizeof(atv));
693 if (error)
694 goto done;
695 if (itimerfix(&atv)) {
696 error = EINVAL;
697 goto done;
698 }
699 s = splclock();
700 timeradd(&atv, &time, &atv);
701 splx(s);
702 } else
703 timo = 0;
704 retry:
705 ncoll = nselcoll;
706 l->l_flag |= L_SELECT;
707 error = selscan(p, (fd_mask *)(bits + ni * 0),
708 (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
709 if (error || *retval)
710 goto done;
711 if (SCARG(uap, tv)) {
712 /*
713 * We have to recalculate the timeout on every retry.
714 */
715 timo = hzto(&atv);
716 if (timo <= 0)
717 goto done;
718 }
719 s = splsched();
720 if ((l->l_flag & L_SELECT) == 0 || nselcoll != ncoll) {
721 splx(s);
722 goto retry;
723 }
724 l->l_flag &= ~L_SELECT;
725 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
726 splx(s);
727 if (error == 0)
728 goto retry;
729 done:
730 l->l_flag &= ~L_SELECT;
731 /* select is not restarted after signals... */
732 if (error == ERESTART)
733 error = EINTR;
734 if (error == EWOULDBLOCK)
735 error = 0;
736 if (error == 0) {
737
738 #define putbits(name, x) \
739 if (SCARG(uap, name)) { \
740 error = copyout(bits + ni * x, SCARG(uap, name), ni); \
741 if (error) \
742 goto out; \
743 }
744 putbits(in, 3);
745 putbits(ou, 4);
746 putbits(ex, 5);
747 #undef putbits
748 }
749 out:
750 if (ni * 6 > sizeof(smallbits))
751 free(bits, M_TEMP);
752 return (error);
753 }
754
755 int
756 selscan(struct proc *p, fd_mask *ibitp, fd_mask *obitp, int nfd,
757 register_t *retval)
758 {
759 struct filedesc *fdp;
760 int msk, i, j, fd, n;
761 fd_mask ibits, obits;
762 struct file *fp;
763 static int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
764 POLLWRNORM | POLLHUP | POLLERR,
765 POLLRDBAND };
766
767 fdp = p->p_fd;
768 n = 0;
769 for (msk = 0; msk < 3; msk++) {
770 for (i = 0; i < nfd; i += NFDBITS) {
771 ibits = *ibitp++;
772 obits = 0;
773 while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
774 ibits &= ~(1 << j);
775 if ((fp = fd_getfile(fdp, fd)) == NULL)
776 return (EBADF);
777 FILE_USE(fp);
778 if ((*fp->f_ops->fo_poll)(fp, flag[msk], p)) {
779 obits |= (1 << j);
780 n++;
781 }
782 FILE_UNUSE(fp, p);
783 }
784 *obitp++ = obits;
785 }
786 }
787 *retval = n;
788 return (0);
789 }
790
791 /*
792 * Poll system call.
793 */
794 int
795 sys_poll(struct lwp *l, void *v, register_t *retval)
796 {
797 struct sys_poll_args /* {
798 syscallarg(struct pollfd *) fds;
799 syscallarg(u_int) nfds;
800 syscallarg(int) timeout;
801 } */ *uap = v;
802 struct proc *p;
803 caddr_t bits;
804 char smallbits[32 * sizeof(struct pollfd)];
805 struct timeval atv;
806 int s, ncoll, error, timo;
807 size_t ni;
808
809 error = 0;
810 p = l->l_proc;
811 if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
812 /* forgiving; slightly wrong */
813 SCARG(uap, nfds) = p->p_fd->fd_nfiles;
814 }
815 ni = SCARG(uap, nfds) * sizeof(struct pollfd);
816 if (ni > sizeof(smallbits))
817 bits = malloc(ni, M_TEMP, M_WAITOK);
818 else
819 bits = smallbits;
820
821 error = copyin(SCARG(uap, fds), bits, ni);
822 if (error)
823 goto done;
824
825 if (SCARG(uap, timeout) != INFTIM) {
826 atv.tv_sec = SCARG(uap, timeout) / 1000;
827 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
828 if (itimerfix(&atv)) {
829 error = EINVAL;
830 goto done;
831 }
832 s = splclock();
833 timeradd(&atv, &time, &atv);
834 splx(s);
835 } else
836 timo = 0;
837 retry:
838 ncoll = nselcoll;
839 l->l_flag |= L_SELECT;
840 error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds), retval);
841 if (error || *retval)
842 goto done;
843 if (SCARG(uap, timeout) != INFTIM) {
844 /*
845 * We have to recalculate the timeout on every retry.
846 */
847 timo = hzto(&atv);
848 if (timo <= 0)
849 goto done;
850 }
851 s = splsched();
852 if ((l->l_flag & L_SELECT) == 0 || nselcoll != ncoll) {
853 splx(s);
854 goto retry;
855 }
856 l->l_flag &= ~L_SELECT;
857 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
858 splx(s);
859 if (error == 0)
860 goto retry;
861 done:
862 l->l_flag &= ~L_SELECT;
863 /* poll is not restarted after signals... */
864 if (error == ERESTART)
865 error = EINTR;
866 if (error == EWOULDBLOCK)
867 error = 0;
868 if (error == 0) {
869 error = copyout(bits, SCARG(uap, fds), ni);
870 if (error)
871 goto out;
872 }
873 out:
874 if (ni > sizeof(smallbits))
875 free(bits, M_TEMP);
876 return (error);
877 }
878
879 int
880 pollscan(struct proc *p, struct pollfd *fds, int nfd, register_t *retval)
881 {
882 struct filedesc *fdp;
883 int i, n;
884 struct file *fp;
885
886 fdp = p->p_fd;
887 n = 0;
888 for (i = 0; i < nfd; i++, fds++) {
889 if ((u_int)fds->fd >= fdp->fd_nfiles) {
890 fds->revents = POLLNVAL;
891 n++;
892 } else {
893 if ((fp = fd_getfile(fdp, fds->fd)) == NULL) {
894 fds->revents = POLLNVAL;
895 n++;
896 } else {
897 FILE_USE(fp);
898 fds->revents = (*fp->f_ops->fo_poll)(fp,
899 fds->events | POLLERR | POLLHUP, p);
900 if (fds->revents != 0)
901 n++;
902 FILE_UNUSE(fp, p);
903 }
904 }
905 }
906 *retval = n;
907 return (0);
908 }
909
910 /*ARGSUSED*/
911 int
912 seltrue(dev_t dev, int events, struct proc *p)
913 {
914
915 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
916 }
917
918 /*
919 * Record a select request.
920 */
921 void
922 selrecord(struct proc *selector, struct selinfo *sip)
923 {
924 struct lwp *l;
925 struct proc *p;
926 pid_t mypid;
927 int collision;
928
929 mypid = selector->p_pid;
930 if (sip->si_pid == mypid)
931 return;
932
933 collision = 0;
934 if (sip->si_pid && (p = pfind(sip->si_pid))) {
935 for (l = LIST_FIRST(&p->p_lwps); l != NULL;
936 l = LIST_NEXT(l, l_sibling)) {
937 if (l->l_wchan == (caddr_t)&selwait) {
938 collision = 1;
939 sip->si_flags |= SI_COLL;
940 }
941 }
942 }
943
944 if (collision == 0) {
945 sip->si_flags &= ~SI_COLL;
946 sip->si_pid = mypid;
947 }
948 }
949
950 /*
951 * Do a wakeup when a selectable event occurs.
952 */
953 void
954 selwakeup(sip)
955 struct selinfo *sip;
956 {
957 struct lwp *l;
958 struct proc *p;
959 int s;
960
961 if (sip->si_pid == 0)
962 return;
963 if (sip->si_flags & SI_COLL) {
964 nselcoll++;
965 sip->si_flags &= ~SI_COLL;
966 wakeup((caddr_t)&selwait);
967 }
968 p = pfind(sip->si_pid);
969 sip->si_pid = 0;
970 if (p != NULL) {
971 for (l = LIST_FIRST(&p->p_lwps); l != NULL;
972 l = LIST_NEXT(l, l_sibling)) {
973 SCHED_LOCK(s);
974 if (l->l_wchan == (caddr_t)&selwait) {
975 if (l->l_stat == LSSLEEP)
976 setrunnable(l);
977 else
978 unsleep(l);
979 } else if (l->l_flag & L_SELECT)
980 l->l_flag &= ~L_SELECT;
981 SCHED_UNLOCK(s);
982 }
983 }
984 }
985