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