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