Home | History | Annotate | Line # | Download | only in kern
sys_generic.c revision 1.22
      1 /*	$NetBSD: sys_generic.c,v 1.22 1996/02/04 02:16:53 christos 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.5 (Berkeley) 1/21/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/filedesc.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/file.h>
     48 #include <sys/proc.h>
     49 #include <sys/socketvar.h>
     50 #include <sys/signalvar.h>
     51 #include <sys/uio.h>
     52 #include <sys/kernel.h>
     53 #include <sys/stat.h>
     54 #include <sys/malloc.h>
     55 #ifdef KTRACE
     56 #include <sys/ktrace.h>
     57 #endif
     58 
     59 #include <sys/mount.h>
     60 #include <sys/syscallargs.h>
     61 
     62 #include <kern/kern_extern.h>
     63 
     64 int selscan __P((struct proc *, fd_set *, fd_set *, int, register_t *));
     65 int seltrue __P((dev_t, int, struct proc *));
     66 
     67 /*
     68  * Read system call.
     69  */
     70 /* ARGSUSED */
     71 int
     72 sys_read(p, v, retval)
     73 	struct proc *p;
     74 	void *v;
     75 	register_t *retval;
     76 {
     77 	register struct sys_read_args /* {
     78 		syscallarg(int) fd;
     79 		syscallarg(char *) buf;
     80 		syscallarg(u_int) nbyte;
     81 	} */ *uap = v;
     82 	register struct file *fp;
     83 	register struct filedesc *fdp = p->p_fd;
     84 	struct uio auio;
     85 	struct iovec aiov;
     86 	long cnt, error = 0;
     87 #ifdef KTRACE
     88 	struct iovec ktriov;
     89 #endif
     90 
     91 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
     92 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
     93 	    (fp->f_flag & FREAD) == 0)
     94 		return (EBADF);
     95 	aiov.iov_base = (caddr_t)SCARG(uap, buf);
     96 	aiov.iov_len = SCARG(uap, nbyte);
     97 	auio.uio_iov = &aiov;
     98 	auio.uio_iovcnt = 1;
     99 	auio.uio_resid = SCARG(uap, nbyte);
    100 	auio.uio_rw = UIO_READ;
    101 	auio.uio_segflg = UIO_USERSPACE;
    102 	auio.uio_procp = p;
    103 	if (auio.uio_resid < 0)
    104 		return EINVAL;
    105 #ifdef KTRACE
    106 	/*
    107 	 * if tracing, save a copy of iovec
    108 	 */
    109 	if (KTRPOINT(p, KTR_GENIO))
    110 		ktriov = aiov;
    111 #endif
    112 	cnt = SCARG(uap, nbyte);
    113 	error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
    114 	if (error)
    115 		if (auio.uio_resid != cnt && (error == ERESTART ||
    116 		    error == EINTR || error == EWOULDBLOCK))
    117 			error = 0;
    118 	cnt -= auio.uio_resid;
    119 #ifdef KTRACE
    120 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    121 		ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, &ktriov,
    122 		    cnt, error);
    123 #endif
    124 	*retval = cnt;
    125 	return (error);
    126 }
    127 
    128 /*
    129  * Scatter read system call.
    130  */
    131 int
    132 sys_readv(p, v, retval)
    133 	struct proc *p;
    134 	void *v;
    135 	register_t *retval;
    136 {
    137 	register struct sys_readv_args /* {
    138 		syscallarg(int) fd;
    139 		syscallarg(struct iovec *) iovp;
    140 		syscallarg(u_int) iovcnt;
    141 	} */ *uap = v;
    142 	register struct file *fp;
    143 	register struct filedesc *fdp = p->p_fd;
    144 	struct uio auio;
    145 	register struct iovec *iov;
    146 	struct iovec *needfree;
    147 	struct iovec aiov[UIO_SMALLIOV];
    148 	long i, cnt, error = 0;
    149 	u_int iovlen;
    150 #ifdef KTRACE
    151 	struct iovec *ktriov = NULL;
    152 #endif
    153 
    154 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    155 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    156 	    (fp->f_flag & FREAD) == 0)
    157 		return (EBADF);
    158 	/* note: can't use iovlen until iovcnt is validated */
    159 	iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
    160 	if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
    161 		if (SCARG(uap, iovcnt) > UIO_MAXIOV)
    162 			return (EINVAL);
    163 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
    164 		needfree = iov;
    165 	} else {
    166 		iov = aiov;
    167 		needfree = NULL;
    168 	}
    169 	auio.uio_iov = iov;
    170 	auio.uio_iovcnt = SCARG(uap, iovcnt);
    171 	auio.uio_rw = UIO_READ;
    172 	auio.uio_segflg = UIO_USERSPACE;
    173 	auio.uio_procp = p;
    174 	error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen);
    175 	if (error)
    176 		goto done;
    177 	auio.uio_resid = 0;
    178 	for (i = 0; i < SCARG(uap, iovcnt); i++) {
    179 #if 0
    180 		/* Cannot happen iov_len is unsigned */
    181 		if (iov->iov_len < 0) {
    182 			error = EINVAL;
    183 			goto done;
    184 		}
    185 #endif
    186 		auio.uio_resid += iov->iov_len;
    187 		if (auio.uio_resid < 0) {
    188 			error = EINVAL;
    189 			goto done;
    190 		}
    191 		iov++;
    192 	}
    193 #ifdef KTRACE
    194 	/*
    195 	 * if tracing, save a copy of iovec
    196 	 */
    197 	if (KTRPOINT(p, KTR_GENIO))  {
    198 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    199 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
    200 	}
    201 #endif
    202 	cnt = auio.uio_resid;
    203 	error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
    204 	if (error)
    205 		if (auio.uio_resid != cnt && (error == ERESTART ||
    206 		    error == EINTR || error == EWOULDBLOCK))
    207 			error = 0;
    208 	cnt -= auio.uio_resid;
    209 #ifdef KTRACE
    210 	if (ktriov != NULL) {
    211 		if (error == 0)
    212 			ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, ktriov,
    213 			    cnt, error);
    214 		FREE(ktriov, M_TEMP);
    215 	}
    216 #endif
    217 	*retval = cnt;
    218 done:
    219 	if (needfree)
    220 		FREE(needfree, M_IOV);
    221 	return (error);
    222 }
    223 
    224 /*
    225  * Write system call
    226  */
    227 int
    228 sys_write(p, v, retval)
    229 	struct proc *p;
    230 	void *v;
    231 	register_t *retval;
    232 {
    233 	register struct sys_write_args /* {
    234 		syscallarg(int) fd;
    235 		syscallarg(char *) buf;
    236 		syscallarg(u_int) nbyte;
    237 	} */ *uap = v;
    238 	register struct file *fp;
    239 	register struct filedesc *fdp = p->p_fd;
    240 	struct uio auio;
    241 	struct iovec aiov;
    242 	long cnt, error = 0;
    243 #ifdef KTRACE
    244 	struct iovec ktriov;
    245 #endif
    246 
    247 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    248 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    249 	    (fp->f_flag & FWRITE) == 0)
    250 		return (EBADF);
    251 	aiov.iov_base = (caddr_t)SCARG(uap, buf);
    252 	aiov.iov_len = SCARG(uap, nbyte);
    253 	auio.uio_iov = &aiov;
    254 	auio.uio_iovcnt = 1;
    255 	auio.uio_resid = SCARG(uap, nbyte);
    256 	auio.uio_rw = UIO_WRITE;
    257 	auio.uio_segflg = UIO_USERSPACE;
    258 	auio.uio_procp = p;
    259 	if (auio.uio_resid < 0)
    260 		return EINVAL;
    261 #ifdef KTRACE
    262 	/*
    263 	 * if tracing, save a copy of iovec
    264 	 */
    265 	if (KTRPOINT(p, KTR_GENIO))
    266 		ktriov = aiov;
    267 #endif
    268 	cnt = SCARG(uap, nbyte);
    269 	error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
    270 	if (error) {
    271 		if (auio.uio_resid != cnt && (error == ERESTART ||
    272 		    error == EINTR || error == EWOULDBLOCK))
    273 			error = 0;
    274 		if (error == EPIPE)
    275 			psignal(p, SIGPIPE);
    276 	}
    277 	cnt -= auio.uio_resid;
    278 #ifdef KTRACE
    279 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    280 		ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
    281 		    &ktriov, cnt, error);
    282 #endif
    283 	*retval = cnt;
    284 	return (error);
    285 }
    286 
    287 /*
    288  * Gather write system call
    289  */
    290 int
    291 sys_writev(p, v, retval)
    292 	struct proc *p;
    293 	void *v;
    294 	register_t *retval;
    295 {
    296 	register struct sys_writev_args /* {
    297 		syscallarg(int) fd;
    298 		syscallarg(struct iovec *) iovp;
    299 		syscallarg(u_int) iovcnt;
    300 	} */ *uap = v;
    301 	register struct file *fp;
    302 	register struct filedesc *fdp = p->p_fd;
    303 	struct uio auio;
    304 	register struct iovec *iov;
    305 	struct iovec *needfree;
    306 	struct iovec aiov[UIO_SMALLIOV];
    307 	long i, cnt, error = 0;
    308 	u_int iovlen;
    309 #ifdef KTRACE
    310 	struct iovec *ktriov = NULL;
    311 #endif
    312 
    313 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    314 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    315 	    (fp->f_flag & FWRITE) == 0)
    316 		return (EBADF);
    317 	/* note: can't use iovlen until iovcnt is validated */
    318 	iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
    319 	if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
    320 		if (SCARG(uap, iovcnt) > UIO_MAXIOV)
    321 			return (EINVAL);
    322 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
    323 		needfree = iov;
    324 	} else {
    325 		iov = aiov;
    326 		needfree = NULL;
    327 	}
    328 	auio.uio_iov = iov;
    329 	auio.uio_iovcnt = SCARG(uap, iovcnt);
    330 	auio.uio_rw = UIO_WRITE;
    331 	auio.uio_segflg = UIO_USERSPACE;
    332 	auio.uio_procp = p;
    333 	error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen);
    334 	if (error)
    335 		goto done;
    336 	auio.uio_resid = 0;
    337 	for (i = 0; i < SCARG(uap, iovcnt); i++) {
    338 #if 0
    339 		/* Cannot happen iov_len is unsigned */
    340 		if (iov->iov_len < 0) {
    341 			error = EINVAL;
    342 			goto done;
    343 		}
    344 #endif
    345 		auio.uio_resid += iov->iov_len;
    346 		if (auio.uio_resid < 0) {
    347 			error = EINVAL;
    348 			goto done;
    349 		}
    350 		iov++;
    351 	}
    352 #ifdef KTRACE
    353 	/*
    354 	 * if tracing, save a copy of iovec
    355 	 */
    356 	if (KTRPOINT(p, KTR_GENIO))  {
    357 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    358 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
    359 	}
    360 #endif
    361 	cnt = auio.uio_resid;
    362 	error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
    363 	if (error) {
    364 		if (auio.uio_resid != cnt && (error == ERESTART ||
    365 		    error == EINTR || error == EWOULDBLOCK))
    366 			error = 0;
    367 		if (error == EPIPE)
    368 			psignal(p, SIGPIPE);
    369 	}
    370 	cnt -= auio.uio_resid;
    371 #ifdef KTRACE
    372 	if (ktriov != NULL) {
    373 		if (error == 0)
    374 			ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
    375 				ktriov, cnt, error);
    376 		FREE(ktriov, M_TEMP);
    377 	}
    378 #endif
    379 	*retval = cnt;
    380 done:
    381 	if (needfree)
    382 		FREE(needfree, M_IOV);
    383 	return (error);
    384 }
    385 
    386 /*
    387  * Ioctl system call
    388  */
    389 /* ARGSUSED */
    390 int
    391 sys_ioctl(p, v, retval)
    392 	struct proc *p;
    393 	void *v;
    394 	register_t *retval;
    395 {
    396 	register struct sys_ioctl_args /* {
    397 		syscallarg(int) fd;
    398 		syscallarg(u_long) com;
    399 		syscallarg(caddr_t) data;
    400 	} */ *uap = v;
    401 	register struct file *fp;
    402 	register struct filedesc *fdp;
    403 	register u_long com;
    404 	register int error;
    405 	register u_int size;
    406 	caddr_t data, memp;
    407 	int tmp;
    408 #define STK_PARAMS	128
    409 	char stkbuf[STK_PARAMS];
    410 
    411 	fdp = p->p_fd;
    412 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
    413 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    414 		return (EBADF);
    415 
    416 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
    417 		return (EBADF);
    418 
    419 	switch (com = SCARG(uap, com)) {
    420 	case FIONCLEX:
    421 		fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
    422 		return (0);
    423 	case FIOCLEX:
    424 		fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
    425 		return (0);
    426 	}
    427 
    428 	/*
    429 	 * Interpret high order word to find amount of data to be
    430 	 * copied to/from the user's address space.
    431 	 */
    432 	size = IOCPARM_LEN(com);
    433 	if (size > IOCPARM_MAX)
    434 		return (ENOTTY);
    435 	memp = NULL;
    436 	if (size > sizeof (stkbuf)) {
    437 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
    438 		data = memp;
    439 	} else
    440 		data = stkbuf;
    441 	if (com&IOC_IN) {
    442 		if (size) {
    443 			error = copyin(SCARG(uap, data), data, (u_int)size);
    444 			if (error) {
    445 				if (memp)
    446 					free(memp, M_IOCTLOPS);
    447 				return (error);
    448 			}
    449 		} else
    450 			*(caddr_t *)data = SCARG(uap, data);
    451 	} else if ((com&IOC_OUT) && size)
    452 		/*
    453 		 * Zero the buffer so the user always
    454 		 * gets back something deterministic.
    455 		 */
    456 		bzero(data, size);
    457 	else if (com&IOC_VOID)
    458 		*(caddr_t *)data = SCARG(uap, data);
    459 
    460 	switch (com) {
    461 
    462 	case FIONBIO:
    463 		if ((tmp = *(int *)data) != 0)
    464 			fp->f_flag |= FNONBLOCK;
    465 		else
    466 			fp->f_flag &= ~FNONBLOCK;
    467 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    468 		break;
    469 
    470 	case FIOASYNC:
    471 		if ((tmp = *(int *)data) != 0)
    472 			fp->f_flag |= FASYNC;
    473 		else
    474 			fp->f_flag &= ~FASYNC;
    475 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    476 		break;
    477 
    478 	case FIOSETOWN:
    479 		tmp = *(int *)data;
    480 		if (fp->f_type == DTYPE_SOCKET) {
    481 			((struct socket *)fp->f_data)->so_pgid = tmp;
    482 			error = 0;
    483 			break;
    484 		}
    485 		if (tmp <= 0) {
    486 			tmp = -tmp;
    487 		} else {
    488 			struct proc *p1 = pfind(tmp);
    489 			if (p1 == 0) {
    490 				error = ESRCH;
    491 				break;
    492 			}
    493 			tmp = p1->p_pgrp->pg_id;
    494 		}
    495 		error = (*fp->f_ops->fo_ioctl)
    496 			(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
    497 		break;
    498 
    499 	case FIOGETOWN:
    500 		if (fp->f_type == DTYPE_SOCKET) {
    501 			error = 0;
    502 			*(int *)data = ((struct socket *)fp->f_data)->so_pgid;
    503 			break;
    504 		}
    505 		error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
    506 		*(int *)data = -*(int *)data;
    507 		break;
    508 
    509 	default:
    510 		error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
    511 		/*
    512 		 * Copy any data to user, size was
    513 		 * already set and checked above.
    514 		 */
    515 		if (error == 0 && (com&IOC_OUT) && size)
    516 			error = copyout(data, SCARG(uap, data), (u_int)size);
    517 		break;
    518 	}
    519 	if (memp)
    520 		free(memp, M_IOCTLOPS);
    521 	return (error);
    522 }
    523 
    524 int	selwait, nselcoll;
    525 
    526 /*
    527  * Select system call.
    528  */
    529 int
    530 sys_select(p, v, retval)
    531 	register struct proc *p;
    532 	void *v;
    533 	register_t *retval;
    534 {
    535 	register struct sys_select_args /* {
    536 		syscallarg(u_int) nd;
    537 		syscallarg(fd_set *) in;
    538 		syscallarg(fd_set *) ou;
    539 		syscallarg(fd_set *) ex;
    540 		syscallarg(struct timeval *) tv;
    541 	} */ *uap = v;
    542 	fd_set ibits[3], obits[3];
    543 	struct timeval atv;
    544 	int s, ncoll, error = 0, timo;
    545 	u_int ni;
    546 
    547 	bzero((caddr_t)ibits, sizeof(ibits));
    548 	bzero((caddr_t)obits, sizeof(obits));
    549 	if (SCARG(uap, nd) > FD_SETSIZE)
    550 		return (EINVAL);
    551 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
    552 		/* forgiving; slightly wrong */
    553 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
    554 	}
    555 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
    556 
    557 #define	getbits(name, x) \
    558 	if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, name), \
    559 	    (caddr_t)&ibits[x], ni))) \
    560 		goto done;
    561 	getbits(in, 0);
    562 	getbits(ou, 1);
    563 	getbits(ex, 2);
    564 #undef	getbits
    565 
    566 	if (SCARG(uap, tv)) {
    567 		error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
    568 			sizeof (atv));
    569 		if (error)
    570 			goto done;
    571 		if (itimerfix(&atv)) {
    572 			error = EINVAL;
    573 			goto done;
    574 		}
    575 		s = splclock();
    576 		timeradd(&atv, &time, &atv);
    577 		timo = hzto(&atv);
    578 		/*
    579 		 * Avoid inadvertently sleeping forever.
    580 		 */
    581 		if (timo == 0)
    582 			timo = 1;
    583 		splx(s);
    584 	} else
    585 		timo = 0;
    586 retry:
    587 	ncoll = nselcoll;
    588 	p->p_flag |= P_SELECT;
    589 	error = selscan(p, ibits, obits, SCARG(uap, nd), retval);
    590 	if (error || *retval)
    591 		goto done;
    592 	s = splhigh();
    593 	/* this should be timercmp(&time, &atv, >=) */
    594 	if (SCARG(uap, tv) && (time.tv_sec > atv.tv_sec ||
    595 	    (time.tv_sec == atv.tv_sec && time.tv_usec >= atv.tv_usec))) {
    596 		splx(s);
    597 		goto done;
    598 	}
    599 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
    600 		splx(s);
    601 		goto retry;
    602 	}
    603 	p->p_flag &= ~P_SELECT;
    604 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
    605 	splx(s);
    606 	if (error == 0)
    607 		goto retry;
    608 done:
    609 	p->p_flag &= ~P_SELECT;
    610 	/* select is not restarted after signals... */
    611 	if (error == ERESTART)
    612 		error = EINTR;
    613 	if (error == EWOULDBLOCK)
    614 		error = 0;
    615 #define	putbits(name, x) \
    616 	if (SCARG(uap, name) && (error2 = copyout((caddr_t)&obits[x], \
    617 	    (caddr_t)SCARG(uap, name), ni))) \
    618 		error = error2;
    619 	if (error == 0) {
    620 		int error2;
    621 
    622 		putbits(in, 0);
    623 		putbits(ou, 1);
    624 		putbits(ex, 2);
    625 #undef putbits
    626 	}
    627 	return (error);
    628 }
    629 
    630 int
    631 selscan(p, ibits, obits, nfd, retval)
    632 	struct proc *p;
    633 	fd_set *ibits, *obits;
    634 	int nfd;
    635 	register_t *retval;
    636 {
    637 	register struct filedesc *fdp = p->p_fd;
    638 	register int msk, i, j, fd;
    639 	register fd_mask bits;
    640 	struct file *fp;
    641 	int n = 0;
    642 	static int flag[3] = { FREAD, FWRITE, 0 };
    643 
    644 	for (msk = 0; msk < 3; msk++) {
    645 		for (i = 0; i < nfd; i += NFDBITS) {
    646 			bits = ibits[msk].fds_bits[i/NFDBITS];
    647 			while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
    648 				bits &= ~(1 << j);
    649 				fp = fdp->fd_ofiles[fd];
    650 				if (fp == NULL)
    651 					return (EBADF);
    652 				if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
    653 					FD_SET(fd, &obits[msk]);
    654 					n++;
    655 				}
    656 			}
    657 		}
    658 	}
    659 	*retval = n;
    660 	return (0);
    661 }
    662 
    663 /*ARGSUSED*/
    664 int
    665 seltrue(dev, flag, p)
    666 	dev_t dev;
    667 	int flag;
    668 	struct proc *p;
    669 {
    670 
    671 	return (1);
    672 }
    673 
    674 /*
    675  * Record a select request.
    676  */
    677 void
    678 selrecord(selector, sip)
    679 	struct proc *selector;
    680 	struct selinfo *sip;
    681 {
    682 	struct proc *p;
    683 	pid_t mypid;
    684 
    685 	mypid = selector->p_pid;
    686 	if (sip->si_pid == mypid)
    687 		return;
    688 	if (sip->si_pid && (p = pfind(sip->si_pid)) &&
    689 	    p->p_wchan == (caddr_t)&selwait)
    690 		sip->si_flags |= SI_COLL;
    691 	else
    692 		sip->si_pid = mypid;
    693 }
    694 
    695 /*
    696  * Do a wakeup when a selectable event occurs.
    697  */
    698 void
    699 selwakeup(sip)
    700 	register struct selinfo *sip;
    701 {
    702 	register struct proc *p;
    703 	int s;
    704 
    705 	if (sip->si_pid == 0)
    706 		return;
    707 	if (sip->si_flags & SI_COLL) {
    708 		nselcoll++;
    709 		sip->si_flags &= ~SI_COLL;
    710 		wakeup((caddr_t)&selwait);
    711 	}
    712 	p = pfind(sip->si_pid);
    713 	sip->si_pid = 0;
    714 	if (p != NULL) {
    715 		s = splhigh();
    716 		if (p->p_wchan == (caddr_t)&selwait) {
    717 			if (p->p_stat == SSLEEP)
    718 				setrunnable(p);
    719 			else
    720 				unsleep(p);
    721 		} else if (p->p_flag & P_SELECT)
    722 			p->p_flag &= ~P_SELECT;
    723 		splx(s);
    724 	}
    725 }
    726