Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.26
      1  1.26      cgd /*	$NetBSD: kern_descrip.c,v 1.26 1994/10/30 21:47:38 cgd Exp $	*/
      2  1.22      cgd 
      3  1.16      cgd /*
      4  1.17      cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  1.17      cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.16      cgd  * (c) UNIX System Laboratories, Inc.
      7  1.16      cgd  * All or some portions of this file are derived from material licensed
      8  1.16      cgd  * to the University of California by American Telephone and Telegraph
      9  1.16      cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.16      cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.16      cgd  *
     12  1.16      cgd  * Redistribution and use in source and binary forms, with or without
     13  1.16      cgd  * modification, are permitted provided that the following conditions
     14  1.16      cgd  * are met:
     15  1.16      cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.16      cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.16      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.16      cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.16      cgd  *    documentation and/or other materials provided with the distribution.
     20  1.16      cgd  * 3. All advertising materials mentioning features or use of this software
     21  1.16      cgd  *    must display the following acknowledgement:
     22  1.16      cgd  *	This product includes software developed by the University of
     23  1.16      cgd  *	California, Berkeley and its contributors.
     24  1.16      cgd  * 4. Neither the name of the University nor the names of its contributors
     25  1.16      cgd  *    may be used to endorse or promote products derived from this software
     26  1.16      cgd  *    without specific prior written permission.
     27  1.16      cgd  *
     28  1.16      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.16      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.16      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.16      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.16      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.16      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.16      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.16      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.16      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.16      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.16      cgd  * SUCH DAMAGE.
     39  1.16      cgd  *
     40  1.22      cgd  *	@(#)kern_descrip.c	8.6 (Berkeley) 4/19/94
     41  1.16      cgd  */
     42  1.16      cgd 
     43  1.16      cgd #include <sys/param.h>
     44  1.16      cgd #include <sys/systm.h>
     45  1.16      cgd #include <sys/filedesc.h>
     46  1.16      cgd #include <sys/kernel.h>
     47  1.16      cgd #include <sys/vnode.h>
     48  1.16      cgd #include <sys/proc.h>
     49  1.16      cgd #include <sys/file.h>
     50  1.16      cgd #include <sys/socket.h>
     51  1.16      cgd #include <sys/socketvar.h>
     52  1.16      cgd #include <sys/stat.h>
     53  1.16      cgd #include <sys/ioctl.h>
     54  1.16      cgd #include <sys/fcntl.h>
     55  1.16      cgd #include <sys/malloc.h>
     56  1.16      cgd #include <sys/syslog.h>
     57  1.17      cgd #include <sys/unistd.h>
     58  1.16      cgd #include <sys/resourcevar.h>
     59  1.16      cgd 
     60  1.25      cgd #include <sys/mount.h>
     61  1.25      cgd #include <sys/syscallargs.h>
     62  1.25      cgd 
     63  1.16      cgd /*
     64  1.16      cgd  * Descriptor management.
     65  1.16      cgd  */
     66  1.24  mycroft struct filelist filehead;	/* head of list of open files */
     67  1.24  mycroft int nfiles;			/* actual number of open files */
     68  1.16      cgd 
     69  1.16      cgd /*
     70  1.16      cgd  * System calls on descriptors.
     71  1.16      cgd  */
     72  1.18      cgd 
     73  1.21  mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_ULTRIX) || defined(COMPAT_HPUX)
     74  1.16      cgd /* ARGSUSED */
     75  1.25      cgd compat_43_getdtablesize(p, uap, retval)
     76  1.16      cgd 	struct proc *p;
     77  1.17      cgd 	void *uap;
     78  1.25      cgd 	register_t *retval;
     79  1.16      cgd {
     80  1.16      cgd 
     81  1.17      cgd 	*retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
     82  1.16      cgd 	return (0);
     83  1.16      cgd }
     84  1.18      cgd #endif
     85  1.16      cgd 
     86  1.16      cgd /*
     87  1.16      cgd  * Duplicate a file descriptor.
     88  1.16      cgd  */
     89  1.16      cgd /* ARGSUSED */
     90  1.16      cgd dup(p, uap, retval)
     91  1.16      cgd 	struct proc *p;
     92  1.25      cgd 	struct dup_args /* {
     93  1.25      cgd 		syscallarg(u_int) fd;
     94  1.25      cgd 	} */ *uap;
     95  1.25      cgd 	register_t *retval;
     96  1.16      cgd {
     97  1.17      cgd 	register struct filedesc *fdp;
     98  1.17      cgd 	u_int old;
     99  1.17      cgd 	int new, error;
    100  1.16      cgd 
    101  1.25      cgd 	old = SCARG(uap, fd);
    102  1.16      cgd 	/*
    103  1.16      cgd 	 * XXX Compatibility
    104  1.16      cgd 	 */
    105  1.25      cgd 	if (old &~ 077) {
    106  1.25      cgd 		SCARG(uap, fd) &= 077;
    107  1.25      cgd 		return (dup2(p, uap, retval));
    108  1.25      cgd 	}
    109  1.16      cgd 
    110  1.17      cgd 	fdp = p->p_fd;
    111  1.17      cgd 	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
    112  1.16      cgd 		return (EBADF);
    113  1.17      cgd 	if (error = fdalloc(p, 0, &new))
    114  1.16      cgd 		return (error);
    115  1.17      cgd 	return (finishdup(fdp, (int)old, new, retval));
    116  1.16      cgd }
    117  1.16      cgd 
    118  1.16      cgd /*
    119  1.16      cgd  * Duplicate a file descriptor to a particular value.
    120  1.16      cgd  */
    121  1.16      cgd /* ARGSUSED */
    122  1.16      cgd dup2(p, uap, retval)
    123  1.16      cgd 	struct proc *p;
    124  1.25      cgd 	struct dup2_args /* {
    125  1.25      cgd 		syscallarg(u_int) from;
    126  1.25      cgd 		syscallarg(u_int) to;
    127  1.25      cgd 	} */ *uap;
    128  1.25      cgd 	register_t *retval;
    129  1.16      cgd {
    130  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    131  1.25      cgd 	register u_int old = SCARG(uap, from), new = SCARG(uap, to);
    132  1.16      cgd 	int i, error;
    133  1.16      cgd 
    134  1.16      cgd 	if (old >= fdp->fd_nfiles ||
    135  1.17      cgd 	    fdp->fd_ofiles[old] == NULL ||
    136  1.16      cgd 	    new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    137  1.17      cgd 	    new >= maxfiles)
    138  1.16      cgd 		return (EBADF);
    139  1.17      cgd 	if (old == new) {
    140  1.17      cgd 		*retval = new;
    141  1.16      cgd 		return (0);
    142  1.17      cgd 	}
    143  1.16      cgd 	if (new >= fdp->fd_nfiles) {
    144  1.16      cgd 		if (error = fdalloc(p, new, &i))
    145  1.16      cgd 			return (error);
    146  1.16      cgd 		if (new != i)
    147  1.16      cgd 			panic("dup2: fdalloc");
    148  1.16      cgd 	} else if (fdp->fd_ofiles[new]) {
    149  1.16      cgd 		if (fdp->fd_ofileflags[new] & UF_MAPPED)
    150  1.16      cgd 			(void) munmapfd(p, new);
    151  1.16      cgd 		/*
    152  1.16      cgd 		 * dup2() must succeed even if the close has an error.
    153  1.16      cgd 		 */
    154  1.16      cgd 		(void) closef(fdp->fd_ofiles[new], p);
    155  1.16      cgd 	}
    156  1.17      cgd 	return (finishdup(fdp, (int)old, (int)new, retval));
    157  1.16      cgd }
    158  1.16      cgd 
    159  1.16      cgd /*
    160  1.16      cgd  * The file control system call.
    161  1.16      cgd  */
    162  1.16      cgd /* ARGSUSED */
    163  1.16      cgd fcntl(p, uap, retval)
    164  1.16      cgd 	struct proc *p;
    165  1.25      cgd 	register struct fcntl_args /* {
    166  1.25      cgd 		syscallarg(int) fd;
    167  1.25      cgd 		syscallarg(int) cmd;
    168  1.26      cgd 		syscallarg(void *) arg;
    169  1.25      cgd 	} */ *uap;
    170  1.25      cgd 	register_t *retval;
    171  1.16      cgd {
    172  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    173  1.16      cgd 	register struct file *fp;
    174  1.16      cgd 	register char *pop;
    175  1.16      cgd 	struct vnode *vp;
    176  1.16      cgd 	int i, tmp, error, flg = F_POSIX;
    177  1.16      cgd 	struct flock fl;
    178  1.26      cgd 	u_long newmin;
    179  1.16      cgd 
    180  1.25      cgd 	if ((unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
    181  1.25      cgd 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    182  1.16      cgd 		return (EBADF);
    183  1.25      cgd 	pop = &fdp->fd_ofileflags[SCARG(uap, fd)];
    184  1.25      cgd 	switch (SCARG(uap, cmd)) {
    185  1.17      cgd 
    186  1.16      cgd 	case F_DUPFD:
    187  1.26      cgd 		newmin = (u_long)SCARG(uap, arg);
    188  1.17      cgd 		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    189  1.17      cgd 		    newmin >= maxfiles)
    190  1.16      cgd 			return (EINVAL);
    191  1.17      cgd 		if (error = fdalloc(p, newmin, &i))
    192  1.16      cgd 			return (error);
    193  1.25      cgd 		return (finishdup(fdp, SCARG(uap, fd), i, retval));
    194  1.16      cgd 
    195  1.16      cgd 	case F_GETFD:
    196  1.16      cgd 		*retval = *pop & 1;
    197  1.16      cgd 		return (0);
    198  1.16      cgd 
    199  1.16      cgd 	case F_SETFD:
    200  1.25      cgd 		*pop = (*pop &~ 1) | ((long)SCARG(uap, arg) & 1);
    201  1.16      cgd 		return (0);
    202  1.16      cgd 
    203  1.16      cgd 	case F_GETFL:
    204  1.16      cgd 		*retval = OFLAGS(fp->f_flag);
    205  1.16      cgd 		return (0);
    206  1.16      cgd 
    207  1.16      cgd 	case F_SETFL:
    208  1.16      cgd 		fp->f_flag &= ~FCNTLFLAGS;
    209  1.25      cgd 		fp->f_flag |= FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    210  1.16      cgd 		tmp = fp->f_flag & FNONBLOCK;
    211  1.16      cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    212  1.16      cgd 		if (error)
    213  1.16      cgd 			return (error);
    214  1.16      cgd 		tmp = fp->f_flag & FASYNC;
    215  1.16      cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    216  1.16      cgd 		if (!error)
    217  1.16      cgd 			return (0);
    218  1.16      cgd 		fp->f_flag &= ~FNONBLOCK;
    219  1.16      cgd 		tmp = 0;
    220  1.16      cgd 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    221  1.16      cgd 		return (error);
    222  1.16      cgd 
    223  1.16      cgd 	case F_GETOWN:
    224  1.16      cgd 		if (fp->f_type == DTYPE_SOCKET) {
    225  1.16      cgd 			*retval = ((struct socket *)fp->f_data)->so_pgid;
    226  1.16      cgd 			return (0);
    227  1.16      cgd 		}
    228  1.16      cgd 		error = (*fp->f_ops->fo_ioctl)
    229  1.16      cgd 			(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
    230  1.16      cgd 		*retval = -*retval;
    231  1.16      cgd 		return (error);
    232  1.16      cgd 
    233  1.16      cgd 	case F_SETOWN:
    234  1.16      cgd 		if (fp->f_type == DTYPE_SOCKET) {
    235  1.25      cgd 			((struct socket *)fp->f_data)->so_pgid =
    236  1.25      cgd 			    (long)SCARG(uap, arg);
    237  1.16      cgd 			return (0);
    238  1.16      cgd 		}
    239  1.25      cgd 		if ((long)SCARG(uap, arg) <= 0) {
    240  1.25      cgd 			SCARG(uap, arg) = (void *)(-(long)SCARG(uap, arg));
    241  1.16      cgd 		} else {
    242  1.25      cgd 			struct proc *p1 = pfind((long)SCARG(uap, arg));
    243  1.16      cgd 			if (p1 == 0)
    244  1.16      cgd 				return (ESRCH);
    245  1.26      cgd 			SCARG(uap, arg) = (void *)(long)p1->p_pgrp->pg_id;
    246  1.16      cgd 		}
    247  1.16      cgd 		return ((*fp->f_ops->fo_ioctl)
    248  1.25      cgd 			(fp, (int)TIOCSPGRP, (caddr_t)&SCARG(uap, arg), p));
    249  1.16      cgd 
    250  1.16      cgd 	case F_SETLKW:
    251  1.16      cgd 		flg |= F_WAIT;
    252  1.16      cgd 		/* Fall into F_SETLK */
    253  1.16      cgd 
    254  1.16      cgd 	case F_SETLK:
    255  1.16      cgd 		if (fp->f_type != DTYPE_VNODE)
    256  1.16      cgd 			return (EBADF);
    257  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    258  1.16      cgd 		/* Copy in the lock structure */
    259  1.25      cgd 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    260  1.25      cgd 		    sizeof (fl));
    261  1.16      cgd 		if (error)
    262  1.16      cgd 			return (error);
    263  1.16      cgd 		if (fl.l_whence == SEEK_CUR)
    264  1.16      cgd 			fl.l_start += fp->f_offset;
    265  1.16      cgd 		switch (fl.l_type) {
    266  1.16      cgd 
    267  1.16      cgd 		case F_RDLCK:
    268  1.16      cgd 			if ((fp->f_flag & FREAD) == 0)
    269  1.16      cgd 				return (EBADF);
    270  1.16      cgd 			p->p_flag |= P_ADVLOCK;
    271  1.16      cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    272  1.16      cgd 
    273  1.16      cgd 		case F_WRLCK:
    274  1.16      cgd 			if ((fp->f_flag & FWRITE) == 0)
    275  1.16      cgd 				return (EBADF);
    276  1.16      cgd 			p->p_flag |= P_ADVLOCK;
    277  1.16      cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    278  1.16      cgd 
    279  1.16      cgd 		case F_UNLCK:
    280  1.16      cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
    281  1.16      cgd 				F_POSIX));
    282  1.16      cgd 
    283  1.16      cgd 		default:
    284  1.16      cgd 			return (EINVAL);
    285  1.16      cgd 		}
    286  1.16      cgd 
    287  1.16      cgd 	case F_GETLK:
    288  1.16      cgd 		if (fp->f_type != DTYPE_VNODE)
    289  1.16      cgd 			return (EBADF);
    290  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    291  1.16      cgd 		/* Copy in the lock structure */
    292  1.25      cgd 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    293  1.25      cgd 		    sizeof (fl));
    294  1.16      cgd 		if (error)
    295  1.16      cgd 			return (error);
    296  1.16      cgd 		if (fl.l_whence == SEEK_CUR)
    297  1.16      cgd 			fl.l_start += fp->f_offset;
    298  1.16      cgd 		if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
    299  1.16      cgd 			return (error);
    300  1.25      cgd 		return (copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
    301  1.25      cgd 		    sizeof (fl)));
    302  1.16      cgd 
    303  1.16      cgd 	default:
    304  1.16      cgd 		return (EINVAL);
    305  1.16      cgd 	}
    306  1.16      cgd 	/* NOTREACHED */
    307  1.16      cgd }
    308  1.16      cgd 
    309  1.16      cgd /*
    310  1.17      cgd  * Common code for dup, dup2, and fcntl(F_DUPFD).
    311  1.17      cgd  */
    312  1.17      cgd int
    313  1.17      cgd finishdup(fdp, old, new, retval)
    314  1.17      cgd 	register struct filedesc *fdp;
    315  1.25      cgd 	register int old, new;
    316  1.25      cgd 	register_t *retval;
    317  1.17      cgd {
    318  1.17      cgd 	register struct file *fp;
    319  1.17      cgd 
    320  1.17      cgd 	fp = fdp->fd_ofiles[old];
    321  1.17      cgd 	fdp->fd_ofiles[new] = fp;
    322  1.17      cgd 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    323  1.17      cgd 	fp->f_count++;
    324  1.17      cgd 	if (new > fdp->fd_lastfile)
    325  1.17      cgd 		fdp->fd_lastfile = new;
    326  1.17      cgd 	*retval = new;
    327  1.17      cgd 	return (0);
    328  1.17      cgd }
    329  1.17      cgd 
    330  1.17      cgd /*
    331  1.16      cgd  * Close a file descriptor.
    332  1.16      cgd  */
    333  1.16      cgd /* ARGSUSED */
    334  1.16      cgd close(p, uap, retval)
    335  1.16      cgd 	struct proc *p;
    336  1.25      cgd 	struct close_args /* {
    337  1.25      cgd 		syscallarg(int) fd;
    338  1.25      cgd 	} */ *uap;
    339  1.25      cgd 	register_t *retval;
    340  1.16      cgd {
    341  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    342  1.16      cgd 	register struct file *fp;
    343  1.25      cgd 	register int fd = SCARG(uap, fd);
    344  1.16      cgd 	register u_char *pf;
    345  1.16      cgd 
    346  1.16      cgd 	if ((unsigned)fd >= fdp->fd_nfiles ||
    347  1.16      cgd 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    348  1.16      cgd 		return (EBADF);
    349  1.16      cgd 	pf = (u_char *)&fdp->fd_ofileflags[fd];
    350  1.16      cgd 	if (*pf & UF_MAPPED)
    351  1.16      cgd 		(void) munmapfd(p, fd);
    352  1.16      cgd 	fdp->fd_ofiles[fd] = NULL;
    353  1.16      cgd 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    354  1.16      cgd 		fdp->fd_lastfile--;
    355  1.16      cgd 	if (fd < fdp->fd_freefile)
    356  1.16      cgd 		fdp->fd_freefile = fd;
    357  1.16      cgd 	*pf = 0;
    358  1.16      cgd 	return (closef(fp, p));
    359  1.16      cgd }
    360  1.16      cgd 
    361  1.23  mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2)
    362  1.16      cgd /*
    363  1.16      cgd  * Return status information about a file descriptor.
    364  1.16      cgd  */
    365  1.16      cgd /* ARGSUSED */
    366  1.25      cgd compat_43_fstat(p, uap, retval)
    367  1.16      cgd 	struct proc *p;
    368  1.25      cgd 	register struct compat_43_fstat_args /* {
    369  1.25      cgd 		syscallarg(int) fd;
    370  1.25      cgd 		syscallarg(struct ostat *) sb;
    371  1.25      cgd 	} */ *uap;
    372  1.25      cgd 	register_t *retval;
    373  1.16      cgd {
    374  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    375  1.16      cgd 	register struct file *fp;
    376  1.17      cgd 	struct stat ub;
    377  1.17      cgd 	struct ostat oub;
    378  1.16      cgd 	int error;
    379  1.16      cgd 
    380  1.25      cgd 	if ((unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
    381  1.25      cgd 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    382  1.16      cgd 		return (EBADF);
    383  1.16      cgd 	switch (fp->f_type) {
    384  1.16      cgd 
    385  1.16      cgd 	case DTYPE_VNODE:
    386  1.17      cgd 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    387  1.16      cgd 		break;
    388  1.16      cgd 
    389  1.16      cgd 	case DTYPE_SOCKET:
    390  1.17      cgd 		error = soo_stat((struct socket *)fp->f_data, &ub);
    391  1.16      cgd 		break;
    392  1.16      cgd 
    393  1.16      cgd 	default:
    394  1.16      cgd 		panic("ofstat");
    395  1.16      cgd 		/*NOTREACHED*/
    396  1.16      cgd 	}
    397  1.17      cgd 	cvtstat(&ub, &oub);
    398  1.17      cgd 	if (error == 0)
    399  1.25      cgd 		error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
    400  1.25      cgd 		    sizeof (oub));
    401  1.16      cgd 	return (error);
    402  1.16      cgd }
    403  1.23  mycroft #endif /* COMPAT_43 || COMPAT_SUNOS || COMPAT_IBCS2 */
    404  1.16      cgd 
    405  1.17      cgd /*
    406  1.17      cgd  * Return status information about a file descriptor.
    407  1.17      cgd  */
    408  1.16      cgd /* ARGSUSED */
    409  1.16      cgd fstat(p, uap, retval)
    410  1.16      cgd 	struct proc *p;
    411  1.25      cgd 	register struct fstat_args /* {
    412  1.25      cgd 		syscallarg(int) fd;
    413  1.25      cgd 		syscallarg(struct stat *) sb;
    414  1.25      cgd 	} */ *uap;
    415  1.25      cgd 	register_t *retval;
    416  1.16      cgd {
    417  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    418  1.16      cgd 	register struct file *fp;
    419  1.16      cgd 	struct stat ub;
    420  1.16      cgd 	int error;
    421  1.16      cgd 
    422  1.25      cgd 	if ((unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
    423  1.25      cgd 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    424  1.16      cgd 		return (EBADF);
    425  1.16      cgd 	switch (fp->f_type) {
    426  1.16      cgd 
    427  1.16      cgd 	case DTYPE_VNODE:
    428  1.16      cgd 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    429  1.16      cgd 		break;
    430  1.16      cgd 
    431  1.16      cgd 	case DTYPE_SOCKET:
    432  1.16      cgd 		error = soo_stat((struct socket *)fp->f_data, &ub);
    433  1.16      cgd 		break;
    434  1.16      cgd 
    435  1.16      cgd 	default:
    436  1.16      cgd 		panic("fstat");
    437  1.16      cgd 		/*NOTREACHED*/
    438  1.16      cgd 	}
    439  1.16      cgd 	if (error == 0)
    440  1.25      cgd 		error = copyout((caddr_t)&ub, (caddr_t)SCARG(uap, sb),
    441  1.25      cgd 		    sizeof (ub));
    442  1.16      cgd 	return (error);
    443  1.16      cgd }
    444  1.16      cgd 
    445  1.16      cgd /*
    446  1.16      cgd  * Return pathconf information about a file descriptor.
    447  1.16      cgd  */
    448  1.16      cgd /* ARGSUSED */
    449  1.16      cgd fpathconf(p, uap, retval)
    450  1.16      cgd 	struct proc *p;
    451  1.25      cgd 	register struct fpathconf_args /* {
    452  1.25      cgd 		syscallarg(int) fd;
    453  1.25      cgd 		syscallarg(int) name;
    454  1.25      cgd 	} */ *uap;
    455  1.25      cgd 	register_t *retval;
    456  1.16      cgd {
    457  1.17      cgd 	struct filedesc *fdp = p->p_fd;
    458  1.17      cgd 	struct file *fp;
    459  1.17      cgd 	struct vnode *vp;
    460  1.17      cgd 
    461  1.25      cgd 	if ((unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
    462  1.25      cgd 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    463  1.17      cgd 		return (EBADF);
    464  1.17      cgd 	switch (fp->f_type) {
    465  1.16      cgd 
    466  1.17      cgd 	case DTYPE_SOCKET:
    467  1.25      cgd 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    468  1.17      cgd 			return (EINVAL);
    469  1.17      cgd 		*retval = PIPE_BUF;
    470  1.17      cgd 		return (0);
    471  1.17      cgd 
    472  1.17      cgd 	case DTYPE_VNODE:
    473  1.17      cgd 		vp = (struct vnode *)fp->f_data;
    474  1.17      cgd #ifdef notyet
    475  1.25      cgd 		return (VOP_PATHCONF(vp, SCARG(uap, name), retval));
    476  1.17      cgd #else
    477  1.17      cgd 		return (ENOSYS);
    478  1.17      cgd #endif
    479  1.17      cgd 
    480  1.17      cgd 	default:
    481  1.17      cgd 		panic("fpathconf");
    482  1.17      cgd 	}
    483  1.17      cgd 	/*NOTREACHED*/
    484  1.16      cgd }
    485  1.16      cgd 
    486  1.16      cgd /*
    487  1.16      cgd  * Allocate a file descriptor for the process.
    488  1.16      cgd  */
    489  1.16      cgd int fdexpand;
    490  1.16      cgd 
    491  1.16      cgd fdalloc(p, want, result)
    492  1.16      cgd 	struct proc *p;
    493  1.16      cgd 	int want;
    494  1.16      cgd 	int *result;
    495  1.16      cgd {
    496  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    497  1.16      cgd 	register int i;
    498  1.16      cgd 	int lim, last, nfiles;
    499  1.16      cgd 	struct file **newofile;
    500  1.16      cgd 	char *newofileflags;
    501  1.16      cgd 
    502  1.16      cgd 	/*
    503  1.16      cgd 	 * Search for a free descriptor starting at the higher
    504  1.16      cgd 	 * of want or fd_freefile.  If that fails, consider
    505  1.16      cgd 	 * expanding the ofile array.
    506  1.16      cgd 	 */
    507  1.17      cgd 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    508  1.16      cgd 	for (;;) {
    509  1.16      cgd 		last = min(fdp->fd_nfiles, lim);
    510  1.16      cgd 		if ((i = want) < fdp->fd_freefile)
    511  1.16      cgd 			i = fdp->fd_freefile;
    512  1.16      cgd 		for (; i < last; i++) {
    513  1.16      cgd 			if (fdp->fd_ofiles[i] == NULL) {
    514  1.16      cgd 				fdp->fd_ofileflags[i] = 0;
    515  1.16      cgd 				if (i > fdp->fd_lastfile)
    516  1.16      cgd 					fdp->fd_lastfile = i;
    517  1.16      cgd 				if (want <= fdp->fd_freefile)
    518  1.16      cgd 					fdp->fd_freefile = i;
    519  1.16      cgd 				*result = i;
    520  1.16      cgd 				return (0);
    521  1.16      cgd 			}
    522  1.16      cgd 		}
    523  1.16      cgd 
    524  1.16      cgd 		/*
    525  1.16      cgd 		 * No space in current array.  Expand?
    526  1.16      cgd 		 */
    527  1.16      cgd 		if (fdp->fd_nfiles >= lim)
    528  1.16      cgd 			return (EMFILE);
    529  1.16      cgd 		if (fdp->fd_nfiles < NDEXTENT)
    530  1.16      cgd 			nfiles = NDEXTENT;
    531  1.16      cgd 		else
    532  1.16      cgd 			nfiles = 2 * fdp->fd_nfiles;
    533  1.16      cgd 		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
    534  1.16      cgd 		    M_FILEDESC, M_WAITOK);
    535  1.16      cgd 		newofileflags = (char *) &newofile[nfiles];
    536  1.16      cgd 		/*
    537  1.16      cgd 		 * Copy the existing ofile and ofileflags arrays
    538  1.16      cgd 		 * and zero the new portion of each array.
    539  1.16      cgd 		 */
    540  1.16      cgd 		bcopy(fdp->fd_ofiles, newofile,
    541  1.16      cgd 			(i = sizeof(struct file *) * fdp->fd_nfiles));
    542  1.16      cgd 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
    543  1.16      cgd 		bcopy(fdp->fd_ofileflags, newofileflags,
    544  1.16      cgd 			(i = sizeof(char) * fdp->fd_nfiles));
    545  1.16      cgd 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
    546  1.16      cgd 		if (fdp->fd_nfiles > NDFILE)
    547  1.16      cgd 			FREE(fdp->fd_ofiles, M_FILEDESC);
    548  1.16      cgd 		fdp->fd_ofiles = newofile;
    549  1.16      cgd 		fdp->fd_ofileflags = newofileflags;
    550  1.16      cgd 		fdp->fd_nfiles = nfiles;
    551  1.16      cgd 		fdexpand++;
    552  1.16      cgd 	}
    553  1.16      cgd }
    554  1.16      cgd 
    555  1.16      cgd /*
    556  1.16      cgd  * Check to see whether n user file descriptors
    557  1.16      cgd  * are available to the process p.
    558  1.16      cgd  */
    559  1.16      cgd fdavail(p, n)
    560  1.16      cgd 	struct proc *p;
    561  1.16      cgd 	register int n;
    562  1.16      cgd {
    563  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    564  1.16      cgd 	register struct file **fpp;
    565  1.17      cgd 	register int i, lim;
    566  1.16      cgd 
    567  1.17      cgd 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    568  1.17      cgd 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
    569  1.16      cgd 		return (1);
    570  1.16      cgd 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
    571  1.16      cgd 	for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
    572  1.16      cgd 		if (*fpp == NULL && --n <= 0)
    573  1.16      cgd 			return (1);
    574  1.16      cgd 	return (0);
    575  1.16      cgd }
    576  1.16      cgd 
    577  1.16      cgd /*
    578  1.16      cgd  * Create a new open file structure and allocate
    579  1.16      cgd  * a file decriptor for the process that refers to it.
    580  1.16      cgd  */
    581  1.16      cgd falloc(p, resultfp, resultfd)
    582  1.16      cgd 	register struct proc *p;
    583  1.16      cgd 	struct file **resultfp;
    584  1.16      cgd 	int *resultfd;
    585  1.16      cgd {
    586  1.24  mycroft 	register struct file *fp, *fq;
    587  1.16      cgd 	int error, i;
    588  1.16      cgd 
    589  1.16      cgd 	if (error = fdalloc(p, 0, &i))
    590  1.16      cgd 		return (error);
    591  1.16      cgd 	if (nfiles >= maxfiles) {
    592  1.16      cgd 		tablefull("file");
    593  1.16      cgd 		return (ENFILE);
    594  1.16      cgd 	}
    595  1.16      cgd 	/*
    596  1.16      cgd 	 * Allocate a new file descriptor.
    597  1.16      cgd 	 * If the process has file descriptor zero open, add to the list
    598  1.16      cgd 	 * of open files at that point, otherwise put it at the front of
    599  1.16      cgd 	 * the list of open files.
    600  1.16      cgd 	 */
    601  1.16      cgd 	nfiles++;
    602  1.16      cgd 	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
    603  1.17      cgd 	bzero(fp, sizeof(struct file));
    604  1.24  mycroft 	if (fq = p->p_fd->fd_ofiles[0]) {
    605  1.24  mycroft 		LIST_INSERT_AFTER(fq, fp, f_list);
    606  1.24  mycroft 	} else {
    607  1.24  mycroft 		LIST_INSERT_HEAD(&filehead, fp, f_list);
    608  1.24  mycroft 	}
    609  1.16      cgd 	p->p_fd->fd_ofiles[i] = fp;
    610  1.16      cgd 	fp->f_count = 1;
    611  1.16      cgd 	fp->f_cred = p->p_ucred;
    612  1.16      cgd 	crhold(fp->f_cred);
    613  1.16      cgd 	if (resultfp)
    614  1.16      cgd 		*resultfp = fp;
    615  1.16      cgd 	if (resultfd)
    616  1.16      cgd 		*resultfd = i;
    617  1.16      cgd 	return (0);
    618  1.16      cgd }
    619  1.16      cgd 
    620  1.16      cgd /*
    621  1.16      cgd  * Free a file descriptor.
    622  1.16      cgd  */
    623  1.16      cgd ffree(fp)
    624  1.16      cgd 	register struct file *fp;
    625  1.16      cgd {
    626  1.16      cgd 	register struct file *fq;
    627  1.16      cgd 
    628  1.24  mycroft 	LIST_REMOVE(fp, f_list);
    629  1.16      cgd 	crfree(fp->f_cred);
    630  1.16      cgd #ifdef DIAGNOSTIC
    631  1.16      cgd 	fp->f_count = 0;
    632  1.16      cgd #endif
    633  1.16      cgd 	nfiles--;
    634  1.16      cgd 	FREE(fp, M_FILE);
    635  1.16      cgd }
    636  1.16      cgd 
    637  1.16      cgd /*
    638  1.16      cgd  * Copy a filedesc structure.
    639  1.16      cgd  */
    640  1.16      cgd struct filedesc *
    641  1.16      cgd fdcopy(p)
    642  1.16      cgd 	struct proc *p;
    643  1.16      cgd {
    644  1.16      cgd 	register struct filedesc *newfdp, *fdp = p->p_fd;
    645  1.16      cgd 	register struct file **fpp;
    646  1.16      cgd 	register int i;
    647  1.16      cgd 
    648  1.16      cgd 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
    649  1.16      cgd 	    M_FILEDESC, M_WAITOK);
    650  1.16      cgd 	bcopy(fdp, newfdp, sizeof(struct filedesc));
    651  1.16      cgd 	VREF(newfdp->fd_cdir);
    652  1.16      cgd 	if (newfdp->fd_rdir)
    653  1.16      cgd 		VREF(newfdp->fd_rdir);
    654  1.16      cgd 	newfdp->fd_refcnt = 1;
    655  1.16      cgd 
    656  1.16      cgd 	/*
    657  1.16      cgd 	 * If the number of open files fits in the internal arrays
    658  1.16      cgd 	 * of the open file structure, use them, otherwise allocate
    659  1.16      cgd 	 * additional memory for the number of descriptors currently
    660  1.16      cgd 	 * in use.
    661  1.16      cgd 	 */
    662  1.16      cgd 	if (newfdp->fd_lastfile < NDFILE) {
    663  1.16      cgd 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
    664  1.16      cgd 		newfdp->fd_ofileflags =
    665  1.16      cgd 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
    666  1.16      cgd 		i = NDFILE;
    667  1.16      cgd 	} else {
    668  1.16      cgd 		/*
    669  1.16      cgd 		 * Compute the smallest multiple of NDEXTENT needed
    670  1.16      cgd 		 * for the file descriptors currently in use,
    671  1.16      cgd 		 * allowing the table to shrink.
    672  1.16      cgd 		 */
    673  1.16      cgd 		i = newfdp->fd_nfiles;
    674  1.17      cgd 		while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
    675  1.16      cgd 			i /= 2;
    676  1.16      cgd 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
    677  1.16      cgd 		    M_FILEDESC, M_WAITOK);
    678  1.16      cgd 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
    679  1.16      cgd 	}
    680  1.16      cgd 	newfdp->fd_nfiles = i;
    681  1.16      cgd 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
    682  1.16      cgd 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
    683  1.16      cgd 	fpp = newfdp->fd_ofiles;
    684  1.16      cgd 	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
    685  1.16      cgd 		if (*fpp != NULL)
    686  1.16      cgd 			(*fpp)->f_count++;
    687  1.16      cgd 	return (newfdp);
    688  1.16      cgd }
    689  1.16      cgd 
    690  1.16      cgd /*
    691  1.16      cgd  * Release a filedesc structure.
    692  1.16      cgd  */
    693  1.16      cgd void
    694  1.16      cgd fdfree(p)
    695  1.16      cgd 	struct proc *p;
    696  1.16      cgd {
    697  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    698  1.16      cgd 	struct file **fpp;
    699  1.16      cgd 	register int i;
    700  1.16      cgd 
    701  1.16      cgd 	if (--fdp->fd_refcnt > 0)
    702  1.16      cgd 		return;
    703  1.16      cgd 	fpp = fdp->fd_ofiles;
    704  1.16      cgd 	for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
    705  1.16      cgd 		if (*fpp)
    706  1.16      cgd 			(void) closef(*fpp, p);
    707  1.16      cgd 	if (fdp->fd_nfiles > NDFILE)
    708  1.16      cgd 		FREE(fdp->fd_ofiles, M_FILEDESC);
    709  1.16      cgd 	vrele(fdp->fd_cdir);
    710  1.16      cgd 	if (fdp->fd_rdir)
    711  1.16      cgd 		vrele(fdp->fd_rdir);
    712  1.16      cgd 	FREE(fdp, M_FILEDESC);
    713  1.16      cgd }
    714  1.16      cgd 
    715  1.16      cgd /*
    716  1.16      cgd  * Close any files on exec?
    717  1.16      cgd  */
    718  1.16      cgd void
    719  1.16      cgd fdcloseexec(p)
    720  1.16      cgd 	struct proc *p;
    721  1.16      cgd {
    722  1.16      cgd 	struct filedesc *fdp = p->p_fd;
    723  1.16      cgd 	struct file **fpp;
    724  1.16      cgd 	char *fdfp;
    725  1.16      cgd 	register int i;
    726  1.16      cgd 
    727  1.16      cgd 	fpp = fdp->fd_ofiles;
    728  1.16      cgd 	fdfp = fdp->fd_ofileflags;
    729  1.16      cgd 	for (i = 0; i <= fdp->fd_lastfile; i++, fpp++, fdfp++)
    730  1.16      cgd 		if (*fpp != NULL && (*fdfp & UF_EXCLOSE)) {
    731  1.16      cgd 			if (*fdfp & UF_MAPPED)
    732  1.16      cgd 				(void) munmapfd(p, i);
    733  1.16      cgd 			(void) closef(*fpp, p);
    734  1.16      cgd 			*fpp = NULL;
    735  1.16      cgd 			*fdfp = 0;
    736  1.16      cgd 			if (i < fdp->fd_freefile)
    737  1.16      cgd 				fdp->fd_freefile = i;
    738  1.16      cgd 		}
    739  1.16      cgd 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    740  1.16      cgd 		fdp->fd_lastfile--;
    741  1.16      cgd }
    742  1.16      cgd 
    743  1.16      cgd /*
    744  1.16      cgd  * Internal form of close.
    745  1.16      cgd  * Decrement reference count on file structure.
    746  1.17      cgd  * Note: p may be NULL when closing a file
    747  1.17      cgd  * that was being passed in a message.
    748  1.16      cgd  */
    749  1.16      cgd closef(fp, p)
    750  1.16      cgd 	register struct file *fp;
    751  1.16      cgd 	register struct proc *p;
    752  1.16      cgd {
    753  1.16      cgd 	struct vnode *vp;
    754  1.16      cgd 	struct flock lf;
    755  1.16      cgd 	int error;
    756  1.16      cgd 
    757  1.16      cgd 	if (fp == NULL)
    758  1.16      cgd 		return (0);
    759  1.16      cgd 	/*
    760  1.16      cgd 	 * POSIX record locking dictates that any close releases ALL
    761  1.16      cgd 	 * locks owned by this process.  This is handled by setting
    762  1.16      cgd 	 * a flag in the unlock to free ONLY locks obeying POSIX
    763  1.16      cgd 	 * semantics, and not to free BSD-style file locks.
    764  1.17      cgd 	 * If the descriptor was in a message, POSIX-style locks
    765  1.17      cgd 	 * aren't passed with the descriptor.
    766  1.16      cgd 	 */
    767  1.16      cgd 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
    768  1.16      cgd 		lf.l_whence = SEEK_SET;
    769  1.16      cgd 		lf.l_start = 0;
    770  1.16      cgd 		lf.l_len = 0;
    771  1.16      cgd 		lf.l_type = F_UNLCK;
    772  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    773  1.16      cgd 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
    774  1.16      cgd 	}
    775  1.16      cgd 	if (--fp->f_count > 0)
    776  1.16      cgd 		return (0);
    777  1.16      cgd 	if (fp->f_count < 0)
    778  1.16      cgd 		panic("closef: count < 0");
    779  1.16      cgd 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
    780  1.16      cgd 		lf.l_whence = SEEK_SET;
    781  1.16      cgd 		lf.l_start = 0;
    782  1.16      cgd 		lf.l_len = 0;
    783  1.16      cgd 		lf.l_type = F_UNLCK;
    784  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    785  1.16      cgd 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
    786  1.16      cgd 	}
    787  1.17      cgd 	if (fp->f_ops)
    788  1.17      cgd 		error = (*fp->f_ops->fo_close)(fp, p);
    789  1.17      cgd 	else
    790  1.17      cgd 		error = 0;
    791  1.16      cgd 	ffree(fp);
    792  1.16      cgd 	return (error);
    793  1.16      cgd }
    794  1.16      cgd 
    795  1.16      cgd /*
    796  1.16      cgd  * Apply an advisory lock on a file descriptor.
    797  1.16      cgd  *
    798  1.16      cgd  * Just attempt to get a record lock of the requested type on
    799  1.16      cgd  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
    800  1.16      cgd  */
    801  1.16      cgd /* ARGSUSED */
    802  1.16      cgd flock(p, uap, retval)
    803  1.16      cgd 	struct proc *p;
    804  1.25      cgd 	register struct flock_args /* {
    805  1.25      cgd 		syscallarg(int) fd;
    806  1.25      cgd 		syscallarg(int) how;
    807  1.25      cgd 	} */ *uap;
    808  1.25      cgd 	register_t *retval;
    809  1.16      cgd {
    810  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    811  1.16      cgd 	register struct file *fp;
    812  1.16      cgd 	struct vnode *vp;
    813  1.16      cgd 	struct flock lf;
    814  1.16      cgd 
    815  1.25      cgd 	if ((unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
    816  1.25      cgd 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    817  1.16      cgd 		return (EBADF);
    818  1.16      cgd 	if (fp->f_type != DTYPE_VNODE)
    819  1.16      cgd 		return (EOPNOTSUPP);
    820  1.16      cgd 	vp = (struct vnode *)fp->f_data;
    821  1.16      cgd 	lf.l_whence = SEEK_SET;
    822  1.16      cgd 	lf.l_start = 0;
    823  1.16      cgd 	lf.l_len = 0;
    824  1.25      cgd 	if (SCARG(uap, how) & LOCK_UN) {
    825  1.16      cgd 		lf.l_type = F_UNLCK;
    826  1.16      cgd 		fp->f_flag &= ~FHASLOCK;
    827  1.16      cgd 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
    828  1.16      cgd 	}
    829  1.25      cgd 	if (SCARG(uap, how) & LOCK_EX)
    830  1.16      cgd 		lf.l_type = F_WRLCK;
    831  1.25      cgd 	else if (SCARG(uap, how) & LOCK_SH)
    832  1.16      cgd 		lf.l_type = F_RDLCK;
    833  1.16      cgd 	else
    834  1.16      cgd 		return (EBADF);
    835  1.16      cgd 	fp->f_flag |= FHASLOCK;
    836  1.25      cgd 	if (SCARG(uap, how) & LOCK_NB)
    837  1.16      cgd 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
    838  1.16      cgd 	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
    839  1.16      cgd }
    840  1.16      cgd 
    841  1.16      cgd /*
    842  1.16      cgd  * File Descriptor pseudo-device driver (/dev/fd/).
    843  1.16      cgd  *
    844  1.16      cgd  * Opening minor device N dup()s the file (if any) connected to file
    845  1.16      cgd  * descriptor N belonging to the calling process.  Note that this driver
    846  1.16      cgd  * consists of only the ``open()'' routine, because all subsequent
    847  1.16      cgd  * references to this file will be direct to the other driver.
    848  1.16      cgd  */
    849  1.16      cgd /* ARGSUSED */
    850  1.16      cgd fdopen(dev, mode, type, p)
    851  1.16      cgd 	dev_t dev;
    852  1.16      cgd 	int mode, type;
    853  1.16      cgd 	struct proc *p;
    854  1.16      cgd {
    855  1.16      cgd 
    856  1.16      cgd 	/*
    857  1.16      cgd 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
    858  1.16      cgd 	 * the file descriptor being sought for duplication. The error
    859  1.16      cgd 	 * return ensures that the vnode for this device will be released
    860  1.16      cgd 	 * by vn_open. Open will detect this special error and take the
    861  1.16      cgd 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
    862  1.16      cgd 	 * will simply report the error.
    863  1.16      cgd 	 */
    864  1.17      cgd 	p->p_dupfd = minor(dev);
    865  1.16      cgd 	return (ENODEV);
    866  1.16      cgd }
    867  1.16      cgd 
    868  1.16      cgd /*
    869  1.16      cgd  * Duplicate the specified descriptor to a free descriptor.
    870  1.16      cgd  */
    871  1.16      cgd dupfdopen(fdp, indx, dfd, mode, error)
    872  1.16      cgd 	register struct filedesc *fdp;
    873  1.16      cgd 	register int indx, dfd;
    874  1.16      cgd 	int mode;
    875  1.16      cgd 	int error;
    876  1.16      cgd {
    877  1.16      cgd 	register struct file *wfp;
    878  1.16      cgd 	struct file *fp;
    879  1.16      cgd 
    880  1.16      cgd 	/*
    881  1.16      cgd 	 * If the to-be-dup'd fd number is greater than the allowed number
    882  1.16      cgd 	 * of file descriptors, or the fd to be dup'd has already been
    883  1.16      cgd 	 * closed, reject.  Note, check for new == old is necessary as
    884  1.16      cgd 	 * falloc could allocate an already closed to-be-dup'd descriptor
    885  1.16      cgd 	 * as the new descriptor.
    886  1.16      cgd 	 */
    887  1.16      cgd 	fp = fdp->fd_ofiles[indx];
    888  1.16      cgd 	if ((u_int)dfd >= fdp->fd_nfiles ||
    889  1.16      cgd 	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
    890  1.16      cgd 		return (EBADF);
    891  1.16      cgd 
    892  1.16      cgd 	/*
    893  1.16      cgd 	 * There are two cases of interest here.
    894  1.16      cgd 	 *
    895  1.16      cgd 	 * For ENODEV simply dup (dfd) to file descriptor
    896  1.16      cgd 	 * (indx) and return.
    897  1.16      cgd 	 *
    898  1.16      cgd 	 * For ENXIO steal away the file structure from (dfd) and
    899  1.16      cgd 	 * store it in (indx).  (dfd) is effectively closed by
    900  1.16      cgd 	 * this operation.
    901  1.16      cgd 	 *
    902  1.16      cgd 	 * Any other error code is just returned.
    903  1.16      cgd 	 */
    904  1.16      cgd 	switch (error) {
    905  1.16      cgd 	case ENODEV:
    906  1.16      cgd 		/*
    907  1.17      cgd 		 * Check that the mode the file is being opened for is a
    908  1.17      cgd 		 * subset of the mode of the existing descriptor.
    909  1.16      cgd 		 */
    910  1.16      cgd 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
    911  1.16      cgd 			return (EACCES);
    912  1.16      cgd 		fdp->fd_ofiles[indx] = wfp;
    913  1.16      cgd 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    914  1.16      cgd 		wfp->f_count++;
    915  1.16      cgd 		if (indx > fdp->fd_lastfile)
    916  1.16      cgd 			fdp->fd_lastfile = indx;
    917  1.16      cgd 		return (0);
    918  1.16      cgd 
    919  1.16      cgd 	case ENXIO:
    920  1.16      cgd 		/*
    921  1.16      cgd 		 * Steal away the file pointer from dfd, and stuff it into indx.
    922  1.16      cgd 		 */
    923  1.16      cgd 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
    924  1.16      cgd 		fdp->fd_ofiles[dfd] = NULL;
    925  1.16      cgd 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    926  1.16      cgd 		fdp->fd_ofileflags[dfd] = 0;
    927  1.16      cgd 		/*
    928  1.17      cgd 		 * Complete the clean up of the filedesc structure by
    929  1.17      cgd 		 * recomputing the various hints.
    930  1.16      cgd 		 */
    931  1.16      cgd 		if (indx > fdp->fd_lastfile)
    932  1.16      cgd 			fdp->fd_lastfile = indx;
    933  1.16      cgd 		else
    934  1.16      cgd 			while (fdp->fd_lastfile > 0 &&
    935  1.16      cgd 			       fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    936  1.16      cgd 				fdp->fd_lastfile--;
    937  1.16      cgd 			if (dfd < fdp->fd_freefile)
    938  1.16      cgd 				fdp->fd_freefile = dfd;
    939  1.16      cgd 		return (0);
    940  1.16      cgd 
    941  1.16      cgd 	default:
    942  1.16      cgd 		return (error);
    943  1.16      cgd 	}
    944  1.17      cgd 	/* NOTREACHED */
    945  1.16      cgd }
    946