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