Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.52
      1  1.52     perry /*	$NetBSD: kern_descrip.c,v 1.52 1998/07/31 22:50:49 perry 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.51      fvdl  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     41  1.16       cgd  */
     42  1.50       mrg 
     43  1.50       mrg #include "opt_uvm.h"
     44  1.16       cgd 
     45  1.16       cgd #include <sys/param.h>
     46  1.16       cgd #include <sys/systm.h>
     47  1.16       cgd #include <sys/filedesc.h>
     48  1.16       cgd #include <sys/kernel.h>
     49  1.16       cgd #include <sys/vnode.h>
     50  1.16       cgd #include <sys/proc.h>
     51  1.16       cgd #include <sys/file.h>
     52  1.16       cgd #include <sys/socket.h>
     53  1.16       cgd #include <sys/socketvar.h>
     54  1.16       cgd #include <sys/stat.h>
     55  1.16       cgd #include <sys/ioctl.h>
     56  1.16       cgd #include <sys/fcntl.h>
     57  1.16       cgd #include <sys/malloc.h>
     58  1.16       cgd #include <sys/syslog.h>
     59  1.17       cgd #include <sys/unistd.h>
     60  1.16       cgd #include <sys/resourcevar.h>
     61  1.42  christos #include <sys/conf.h>
     62  1.16       cgd 
     63  1.25       cgd #include <sys/mount.h>
     64  1.25       cgd #include <sys/syscallargs.h>
     65  1.25       cgd 
     66  1.38  christos #include <vm/vm.h>
     67  1.38  christos 
     68  1.16       cgd /*
     69  1.16       cgd  * Descriptor management.
     70  1.16       cgd  */
     71  1.24   mycroft struct filelist filehead;	/* head of list of open files */
     72  1.24   mycroft int nfiles;			/* actual number of open files */
     73  1.16       cgd 
     74  1.38  christos static __inline void fd_used __P((struct filedesc *, int));
     75  1.38  christos static __inline void fd_unused __P((struct filedesc *, int));
     76  1.38  christos int finishdup __P((struct filedesc *, int, int, register_t *));
     77  1.38  christos 
     78  1.38  christos static __inline void
     79  1.27   mycroft fd_used(fdp, fd)
     80  1.27   mycroft 	register struct filedesc *fdp;
     81  1.27   mycroft 	register int fd;
     82  1.27   mycroft {
     83  1.27   mycroft 
     84  1.27   mycroft 	if (fd > fdp->fd_lastfile)
     85  1.27   mycroft 		fdp->fd_lastfile = fd;
     86  1.27   mycroft }
     87  1.27   mycroft 
     88  1.38  christos static __inline void
     89  1.27   mycroft fd_unused(fdp, fd)
     90  1.27   mycroft 	register struct filedesc *fdp;
     91  1.27   mycroft 	register int fd;
     92  1.27   mycroft {
     93  1.27   mycroft 
     94  1.27   mycroft 	if (fd < fdp->fd_freefile)
     95  1.27   mycroft 		fdp->fd_freefile = fd;
     96  1.27   mycroft #ifdef DIAGNOSTIC
     97  1.27   mycroft 	if (fd > fdp->fd_lastfile)
     98  1.27   mycroft 		panic("fd_unused: fd_lastfile inconsistent");
     99  1.27   mycroft #endif
    100  1.27   mycroft 	if (fd == fdp->fd_lastfile) {
    101  1.27   mycroft 		do {
    102  1.27   mycroft 			fd--;
    103  1.27   mycroft 		} while (fd >= 0 && fdp->fd_ofiles[fd] == NULL);
    104  1.27   mycroft 		fdp->fd_lastfile = fd;
    105  1.27   mycroft 	}
    106  1.27   mycroft }
    107  1.27   mycroft 
    108  1.16       cgd /*
    109  1.16       cgd  * System calls on descriptors.
    110  1.16       cgd  */
    111  1.18       cgd 
    112  1.16       cgd /*
    113  1.16       cgd  * Duplicate a file descriptor.
    114  1.16       cgd  */
    115  1.16       cgd /* ARGSUSED */
    116  1.38  christos int
    117  1.37   mycroft sys_dup(p, v, retval)
    118  1.16       cgd 	struct proc *p;
    119  1.36   thorpej 	void *v;
    120  1.36   thorpej 	register_t *retval;
    121  1.36   thorpej {
    122  1.37   mycroft 	struct sys_dup_args /* {
    123  1.45   mycroft 		syscallarg(int) fd;
    124  1.36   thorpej 	} */ *uap = v;
    125  1.27   mycroft 	register struct filedesc *fdp = p->p_fd;
    126  1.27   mycroft 	register int old = SCARG(uap, fd);
    127  1.27   mycroft 	int new;
    128  1.27   mycroft 	int error;
    129  1.16       cgd 
    130  1.27   mycroft 	if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
    131  1.16       cgd 		return (EBADF);
    132  1.38  christos 	if ((error = fdalloc(p, 0, &new)) != 0)
    133  1.16       cgd 		return (error);
    134  1.27   mycroft 	return (finishdup(fdp, old, new, retval));
    135  1.16       cgd }
    136  1.16       cgd 
    137  1.16       cgd /*
    138  1.16       cgd  * Duplicate a file descriptor to a particular value.
    139  1.16       cgd  */
    140  1.16       cgd /* ARGSUSED */
    141  1.38  christos int
    142  1.37   mycroft sys_dup2(p, v, retval)
    143  1.16       cgd 	struct proc *p;
    144  1.36   thorpej 	void *v;
    145  1.36   thorpej 	register_t *retval;
    146  1.36   thorpej {
    147  1.37   mycroft 	struct sys_dup2_args /* {
    148  1.45   mycroft 		syscallarg(int) from;
    149  1.45   mycroft 		syscallarg(int) to;
    150  1.36   thorpej 	} */ *uap = v;
    151  1.16       cgd 	register struct filedesc *fdp = p->p_fd;
    152  1.27   mycroft 	register int old = SCARG(uap, from), new = SCARG(uap, to);
    153  1.16       cgd 	int i, error;
    154  1.16       cgd 
    155  1.27   mycroft 	if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL ||
    156  1.27   mycroft 	    (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    157  1.27   mycroft 	    (u_int)new >= maxfiles)
    158  1.16       cgd 		return (EBADF);
    159  1.17       cgd 	if (old == new) {
    160  1.17       cgd 		*retval = new;
    161  1.16       cgd 		return (0);
    162  1.17       cgd 	}
    163  1.16       cgd 	if (new >= fdp->fd_nfiles) {
    164  1.38  christos 		if ((error = fdalloc(p, new, &i)) != 0)
    165  1.16       cgd 			return (error);
    166  1.16       cgd 		if (new != i)
    167  1.16       cgd 			panic("dup2: fdalloc");
    168  1.27   mycroft 	} else {
    169  1.34   mycroft 		(void) fdrelease(p, new);
    170  1.16       cgd 	}
    171  1.27   mycroft 	return (finishdup(fdp, old, new, retval));
    172  1.16       cgd }
    173  1.16       cgd 
    174  1.16       cgd /*
    175  1.16       cgd  * The file control system call.
    176  1.16       cgd  */
    177  1.16       cgd /* ARGSUSED */
    178  1.38  christos int
    179  1.37   mycroft sys_fcntl(p, v, retval)
    180  1.16       cgd 	struct proc *p;
    181  1.36   thorpej 	void *v;
    182  1.36   thorpej 	register_t *retval;
    183  1.36   thorpej {
    184  1.37   mycroft 	register struct sys_fcntl_args /* {
    185  1.25       cgd 		syscallarg(int) fd;
    186  1.25       cgd 		syscallarg(int) cmd;
    187  1.26       cgd 		syscallarg(void *) arg;
    188  1.36   thorpej 	} */ *uap = v;
    189  1.27   mycroft 	int fd = SCARG(uap, fd);
    190  1.16       cgd 	register struct filedesc *fdp = p->p_fd;
    191  1.16       cgd 	register struct file *fp;
    192  1.16       cgd 	struct vnode *vp;
    193  1.16       cgd 	int i, tmp, error, flg = F_POSIX;
    194  1.16       cgd 	struct flock fl;
    195  1.27   mycroft 	int newmin;
    196  1.16       cgd 
    197  1.27   mycroft 	if ((u_int)fd >= fdp->fd_nfiles ||
    198  1.27   mycroft 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    199  1.16       cgd 		return (EBADF);
    200  1.25       cgd 	switch (SCARG(uap, cmd)) {
    201  1.17       cgd 
    202  1.16       cgd 	case F_DUPFD:
    203  1.30       cgd 		newmin = (long)SCARG(uap, arg);
    204  1.27   mycroft 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    205  1.27   mycroft 		    (u_int)newmin >= maxfiles)
    206  1.16       cgd 			return (EINVAL);
    207  1.38  christos 		if ((error = fdalloc(p, newmin, &i)) != 0)
    208  1.16       cgd 			return (error);
    209  1.27   mycroft 		return (finishdup(fdp, fd, i, retval));
    210  1.16       cgd 
    211  1.16       cgd 	case F_GETFD:
    212  1.27   mycroft 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
    213  1.16       cgd 		return (0);
    214  1.16       cgd 
    215  1.16       cgd 	case F_SETFD:
    216  1.27   mycroft 		if ((long)SCARG(uap, arg) & 1)
    217  1.27   mycroft 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
    218  1.27   mycroft 		else
    219  1.27   mycroft 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
    220  1.16       cgd 		return (0);
    221  1.16       cgd 
    222  1.16       cgd 	case F_GETFL:
    223  1.16       cgd 		*retval = OFLAGS(fp->f_flag);
    224  1.16       cgd 		return (0);
    225  1.16       cgd 
    226  1.16       cgd 	case F_SETFL:
    227  1.16       cgd 		fp->f_flag &= ~FCNTLFLAGS;
    228  1.25       cgd 		fp->f_flag |= FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    229  1.16       cgd 		tmp = fp->f_flag & FNONBLOCK;
    230  1.16       cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    231  1.16       cgd 		if (error)
    232  1.16       cgd 			return (error);
    233  1.16       cgd 		tmp = fp->f_flag & FASYNC;
    234  1.16       cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    235  1.16       cgd 		if (!error)
    236  1.16       cgd 			return (0);
    237  1.16       cgd 		fp->f_flag &= ~FNONBLOCK;
    238  1.16       cgd 		tmp = 0;
    239  1.16       cgd 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    240  1.16       cgd 		return (error);
    241  1.16       cgd 
    242  1.16       cgd 	case F_GETOWN:
    243  1.16       cgd 		if (fp->f_type == DTYPE_SOCKET) {
    244  1.16       cgd 			*retval = ((struct socket *)fp->f_data)->so_pgid;
    245  1.16       cgd 			return (0);
    246  1.16       cgd 		}
    247  1.16       cgd 		error = (*fp->f_ops->fo_ioctl)
    248  1.41       cgd 			(fp, TIOCGPGRP, (caddr_t)retval, p);
    249  1.16       cgd 		*retval = -*retval;
    250  1.16       cgd 		return (error);
    251  1.16       cgd 
    252  1.16       cgd 	case F_SETOWN:
    253  1.16       cgd 		if (fp->f_type == DTYPE_SOCKET) {
    254  1.25       cgd 			((struct socket *)fp->f_data)->so_pgid =
    255  1.25       cgd 			    (long)SCARG(uap, arg);
    256  1.16       cgd 			return (0);
    257  1.16       cgd 		}
    258  1.25       cgd 		if ((long)SCARG(uap, arg) <= 0) {
    259  1.25       cgd 			SCARG(uap, arg) = (void *)(-(long)SCARG(uap, arg));
    260  1.16       cgd 		} else {
    261  1.25       cgd 			struct proc *p1 = pfind((long)SCARG(uap, arg));
    262  1.16       cgd 			if (p1 == 0)
    263  1.16       cgd 				return (ESRCH);
    264  1.26       cgd 			SCARG(uap, arg) = (void *)(long)p1->p_pgrp->pg_id;
    265  1.16       cgd 		}
    266  1.16       cgd 		return ((*fp->f_ops->fo_ioctl)
    267  1.41       cgd 			(fp, TIOCSPGRP, (caddr_t)&SCARG(uap, arg), p));
    268  1.16       cgd 
    269  1.16       cgd 	case F_SETLKW:
    270  1.16       cgd 		flg |= F_WAIT;
    271  1.16       cgd 		/* Fall into F_SETLK */
    272  1.16       cgd 
    273  1.16       cgd 	case F_SETLK:
    274  1.16       cgd 		if (fp->f_type != DTYPE_VNODE)
    275  1.16       cgd 			return (EBADF);
    276  1.16       cgd 		vp = (struct vnode *)fp->f_data;
    277  1.16       cgd 		/* Copy in the lock structure */
    278  1.25       cgd 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    279  1.52     perry 		    sizeof(fl));
    280  1.16       cgd 		if (error)
    281  1.16       cgd 			return (error);
    282  1.16       cgd 		if (fl.l_whence == SEEK_CUR)
    283  1.16       cgd 			fl.l_start += fp->f_offset;
    284  1.16       cgd 		switch (fl.l_type) {
    285  1.16       cgd 		case F_RDLCK:
    286  1.16       cgd 			if ((fp->f_flag & FREAD) == 0)
    287  1.16       cgd 				return (EBADF);
    288  1.16       cgd 			p->p_flag |= P_ADVLOCK;
    289  1.16       cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    290  1.16       cgd 
    291  1.16       cgd 		case F_WRLCK:
    292  1.16       cgd 			if ((fp->f_flag & FWRITE) == 0)
    293  1.16       cgd 				return (EBADF);
    294  1.16       cgd 			p->p_flag |= P_ADVLOCK;
    295  1.16       cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    296  1.16       cgd 
    297  1.16       cgd 		case F_UNLCK:
    298  1.16       cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
    299  1.16       cgd 				F_POSIX));
    300  1.16       cgd 
    301  1.16       cgd 		default:
    302  1.16       cgd 			return (EINVAL);
    303  1.16       cgd 		}
    304  1.16       cgd 
    305  1.16       cgd 	case F_GETLK:
    306  1.16       cgd 		if (fp->f_type != DTYPE_VNODE)
    307  1.16       cgd 			return (EBADF);
    308  1.16       cgd 		vp = (struct vnode *)fp->f_data;
    309  1.16       cgd 		/* Copy in the lock structure */
    310  1.25       cgd 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    311  1.52     perry 		    sizeof(fl));
    312  1.16       cgd 		if (error)
    313  1.16       cgd 			return (error);
    314  1.16       cgd 		if (fl.l_whence == SEEK_CUR)
    315  1.16       cgd 			fl.l_start += fp->f_offset;
    316  1.43    kleink 		if (fl.l_type != F_RDLCK &&
    317  1.43    kleink 		    fl.l_type != F_WRLCK &&
    318  1.43    kleink 		    fl.l_type != F_UNLCK)
    319  1.43    kleink 			return (EINVAL);
    320  1.38  christos 		error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX);
    321  1.38  christos 		if (error)
    322  1.16       cgd 			return (error);
    323  1.25       cgd 		return (copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
    324  1.52     perry 		    sizeof(fl)));
    325  1.16       cgd 
    326  1.16       cgd 	default:
    327  1.16       cgd 		return (EINVAL);
    328  1.16       cgd 	}
    329  1.16       cgd 	/* NOTREACHED */
    330  1.16       cgd }
    331  1.16       cgd 
    332  1.16       cgd /*
    333  1.17       cgd  * Common code for dup, dup2, and fcntl(F_DUPFD).
    334  1.17       cgd  */
    335  1.17       cgd int
    336  1.17       cgd finishdup(fdp, old, new, retval)
    337  1.17       cgd 	register struct filedesc *fdp;
    338  1.25       cgd 	register int old, new;
    339  1.25       cgd 	register_t *retval;
    340  1.17       cgd {
    341  1.17       cgd 	register struct file *fp;
    342  1.17       cgd 
    343  1.17       cgd 	fp = fdp->fd_ofiles[old];
    344  1.17       cgd 	fdp->fd_ofiles[new] = fp;
    345  1.17       cgd 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    346  1.17       cgd 	fp->f_count++;
    347  1.27   mycroft 	fd_used(fdp, new);
    348  1.17       cgd 	*retval = new;
    349  1.17       cgd 	return (0);
    350  1.17       cgd }
    351  1.17       cgd 
    352  1.27   mycroft int
    353  1.34   mycroft fdrelease(p, fd)
    354  1.27   mycroft 	struct proc *p;
    355  1.27   mycroft 	int fd;
    356  1.27   mycroft {
    357  1.27   mycroft 	register struct filedesc *fdp = p->p_fd;
    358  1.27   mycroft 	register struct file **fpp, *fp;
    359  1.27   mycroft 	register char *pf;
    360  1.27   mycroft 
    361  1.27   mycroft 	fpp = &fdp->fd_ofiles[fd];
    362  1.27   mycroft 	fp = *fpp;
    363  1.27   mycroft 	if (fp == NULL)
    364  1.27   mycroft 		return (EBADF);
    365  1.27   mycroft 	pf = &fdp->fd_ofileflags[fd];
    366  1.49       mrg #if defined(UVM)
    367  1.49       mrg 	if (*pf & UF_MAPPED) {
    368  1.49       mrg 		/* XXX: USELESS? XXXCDC check it */
    369  1.49       mrg 		p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
    370  1.49       mrg 	}
    371  1.49       mrg #else
    372  1.27   mycroft 	if (*pf & UF_MAPPED)
    373  1.27   mycroft 		(void) munmapfd(p, fd);
    374  1.49       mrg #endif
    375  1.27   mycroft 	*fpp = NULL;
    376  1.27   mycroft 	*pf = 0;
    377  1.27   mycroft 	fd_unused(fdp, fd);
    378  1.27   mycroft 	return (closef(fp, p));
    379  1.27   mycroft }
    380  1.27   mycroft 
    381  1.17       cgd /*
    382  1.16       cgd  * Close a file descriptor.
    383  1.16       cgd  */
    384  1.16       cgd /* ARGSUSED */
    385  1.38  christos int
    386  1.37   mycroft sys_close(p, v, retval)
    387  1.16       cgd 	struct proc *p;
    388  1.36   thorpej 	void *v;
    389  1.36   thorpej 	register_t *retval;
    390  1.36   thorpej {
    391  1.37   mycroft 	struct sys_close_args /* {
    392  1.25       cgd 		syscallarg(int) fd;
    393  1.36   thorpej 	} */ *uap = v;
    394  1.27   mycroft 	int fd = SCARG(uap, fd);
    395  1.16       cgd 	register struct filedesc *fdp = p->p_fd;
    396  1.16       cgd 
    397  1.27   mycroft 	if ((u_int)fd >= fdp->fd_nfiles)
    398  1.16       cgd 		return (EBADF);
    399  1.34   mycroft 	return (fdrelease(p, fd));
    400  1.16       cgd }
    401  1.16       cgd 
    402  1.17       cgd /*
    403  1.17       cgd  * Return status information about a file descriptor.
    404  1.17       cgd  */
    405  1.16       cgd /* ARGSUSED */
    406  1.38  christos int
    407  1.47   thorpej sys___fstat13(p, v, retval)
    408  1.16       cgd 	struct proc *p;
    409  1.36   thorpej 	void *v;
    410  1.36   thorpej 	register_t *retval;
    411  1.36   thorpej {
    412  1.47   thorpej 	register struct sys___fstat13_args /* {
    413  1.25       cgd 		syscallarg(int) fd;
    414  1.25       cgd 		syscallarg(struct stat *) sb;
    415  1.36   thorpej 	} */ *uap = v;
    416  1.27   mycroft 	int fd = SCARG(uap, fd);
    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.27   mycroft 	if ((u_int)fd >= fdp->fd_nfiles ||
    423  1.27   mycroft 	    (fp = fdp->fd_ofiles[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.52     perry 		error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
    441  1.16       cgd 	return (error);
    442  1.16       cgd }
    443  1.16       cgd 
    444  1.16       cgd /*
    445  1.16       cgd  * Return pathconf information about a file descriptor.
    446  1.16       cgd  */
    447  1.16       cgd /* ARGSUSED */
    448  1.38  christos int
    449  1.37   mycroft sys_fpathconf(p, v, retval)
    450  1.16       cgd 	struct proc *p;
    451  1.36   thorpej 	void *v;
    452  1.36   thorpej 	register_t *retval;
    453  1.36   thorpej {
    454  1.37   mycroft 	register struct sys_fpathconf_args /* {
    455  1.25       cgd 		syscallarg(int) fd;
    456  1.25       cgd 		syscallarg(int) name;
    457  1.36   thorpej 	} */ *uap = v;
    458  1.27   mycroft 	int fd = SCARG(uap, fd);
    459  1.17       cgd 	struct filedesc *fdp = p->p_fd;
    460  1.17       cgd 	struct file *fp;
    461  1.17       cgd 	struct vnode *vp;
    462  1.17       cgd 
    463  1.27   mycroft 	if ((u_int)fd >= fdp->fd_nfiles ||
    464  1.27   mycroft 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    465  1.17       cgd 		return (EBADF);
    466  1.17       cgd 	switch (fp->f_type) {
    467  1.16       cgd 
    468  1.17       cgd 	case DTYPE_SOCKET:
    469  1.25       cgd 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    470  1.17       cgd 			return (EINVAL);
    471  1.17       cgd 		*retval = PIPE_BUF;
    472  1.17       cgd 		return (0);
    473  1.17       cgd 
    474  1.17       cgd 	case DTYPE_VNODE:
    475  1.17       cgd 		vp = (struct vnode *)fp->f_data;
    476  1.25       cgd 		return (VOP_PATHCONF(vp, SCARG(uap, name), retval));
    477  1.17       cgd 
    478  1.17       cgd 	default:
    479  1.17       cgd 		panic("fpathconf");
    480  1.17       cgd 	}
    481  1.17       cgd 	/*NOTREACHED*/
    482  1.16       cgd }
    483  1.16       cgd 
    484  1.16       cgd /*
    485  1.16       cgd  * Allocate a file descriptor for the process.
    486  1.16       cgd  */
    487  1.16       cgd int fdexpand;
    488  1.16       cgd 
    489  1.38  christos int
    490  1.16       cgd fdalloc(p, want, result)
    491  1.16       cgd 	struct proc *p;
    492  1.16       cgd 	int want;
    493  1.16       cgd 	int *result;
    494  1.16       cgd {
    495  1.16       cgd 	register struct filedesc *fdp = p->p_fd;
    496  1.16       cgd 	register int i;
    497  1.16       cgd 	int lim, last, nfiles;
    498  1.16       cgd 	struct file **newofile;
    499  1.16       cgd 	char *newofileflags;
    500  1.16       cgd 
    501  1.16       cgd 	/*
    502  1.16       cgd 	 * Search for a free descriptor starting at the higher
    503  1.16       cgd 	 * of want or fd_freefile.  If that fails, consider
    504  1.16       cgd 	 * expanding the ofile array.
    505  1.16       cgd 	 */
    506  1.17       cgd 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    507  1.16       cgd 	for (;;) {
    508  1.16       cgd 		last = min(fdp->fd_nfiles, lim);
    509  1.16       cgd 		if ((i = want) < fdp->fd_freefile)
    510  1.16       cgd 			i = fdp->fd_freefile;
    511  1.16       cgd 		for (; i < last; i++) {
    512  1.16       cgd 			if (fdp->fd_ofiles[i] == NULL) {
    513  1.27   mycroft 				fd_used(fdp, i);
    514  1.16       cgd 				if (want <= fdp->fd_freefile)
    515  1.16       cgd 					fdp->fd_freefile = i;
    516  1.16       cgd 				*result = i;
    517  1.16       cgd 				return (0);
    518  1.16       cgd 			}
    519  1.16       cgd 		}
    520  1.16       cgd 
    521  1.16       cgd 		/*
    522  1.16       cgd 		 * No space in current array.  Expand?
    523  1.16       cgd 		 */
    524  1.16       cgd 		if (fdp->fd_nfiles >= lim)
    525  1.16       cgd 			return (EMFILE);
    526  1.16       cgd 		if (fdp->fd_nfiles < NDEXTENT)
    527  1.16       cgd 			nfiles = NDEXTENT;
    528  1.16       cgd 		else
    529  1.16       cgd 			nfiles = 2 * fdp->fd_nfiles;
    530  1.16       cgd 		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
    531  1.16       cgd 		    M_FILEDESC, M_WAITOK);
    532  1.16       cgd 		newofileflags = (char *) &newofile[nfiles];
    533  1.16       cgd 		/*
    534  1.16       cgd 		 * Copy the existing ofile and ofileflags arrays
    535  1.16       cgd 		 * and zero the new portion of each array.
    536  1.16       cgd 		 */
    537  1.16       cgd 		bcopy(fdp->fd_ofiles, newofile,
    538  1.16       cgd 			(i = sizeof(struct file *) * fdp->fd_nfiles));
    539  1.16       cgd 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
    540  1.16       cgd 		bcopy(fdp->fd_ofileflags, newofileflags,
    541  1.16       cgd 			(i = sizeof(char) * fdp->fd_nfiles));
    542  1.16       cgd 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
    543  1.16       cgd 		if (fdp->fd_nfiles > NDFILE)
    544  1.16       cgd 			FREE(fdp->fd_ofiles, M_FILEDESC);
    545  1.16       cgd 		fdp->fd_ofiles = newofile;
    546  1.16       cgd 		fdp->fd_ofileflags = newofileflags;
    547  1.16       cgd 		fdp->fd_nfiles = nfiles;
    548  1.16       cgd 		fdexpand++;
    549  1.16       cgd 	}
    550  1.16       cgd }
    551  1.16       cgd 
    552  1.16       cgd /*
    553  1.16       cgd  * Check to see whether n user file descriptors
    554  1.16       cgd  * are available to the process p.
    555  1.16       cgd  */
    556  1.38  christos int
    557  1.16       cgd fdavail(p, n)
    558  1.16       cgd 	struct proc *p;
    559  1.16       cgd 	register int n;
    560  1.16       cgd {
    561  1.16       cgd 	register struct filedesc *fdp = p->p_fd;
    562  1.16       cgd 	register struct file **fpp;
    563  1.17       cgd 	register int i, lim;
    564  1.16       cgd 
    565  1.17       cgd 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    566  1.17       cgd 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
    567  1.16       cgd 		return (1);
    568  1.16       cgd 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
    569  1.16       cgd 	for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
    570  1.16       cgd 		if (*fpp == NULL && --n <= 0)
    571  1.16       cgd 			return (1);
    572  1.16       cgd 	return (0);
    573  1.16       cgd }
    574  1.16       cgd 
    575  1.16       cgd /*
    576  1.16       cgd  * Create a new open file structure and allocate
    577  1.16       cgd  * a file decriptor for the process that refers to it.
    578  1.16       cgd  */
    579  1.38  christos int
    580  1.16       cgd falloc(p, resultfp, resultfd)
    581  1.16       cgd 	register struct proc *p;
    582  1.16       cgd 	struct file **resultfp;
    583  1.16       cgd 	int *resultfd;
    584  1.16       cgd {
    585  1.24   mycroft 	register struct file *fp, *fq;
    586  1.16       cgd 	int error, i;
    587  1.16       cgd 
    588  1.38  christos 	if ((error = fdalloc(p, 0, &i)) != 0)
    589  1.16       cgd 		return (error);
    590  1.16       cgd 	if (nfiles >= maxfiles) {
    591  1.16       cgd 		tablefull("file");
    592  1.16       cgd 		return (ENFILE);
    593  1.16       cgd 	}
    594  1.16       cgd 	/*
    595  1.16       cgd 	 * Allocate a new file descriptor.
    596  1.16       cgd 	 * If the process has file descriptor zero open, add to the list
    597  1.16       cgd 	 * of open files at that point, otherwise put it at the front of
    598  1.16       cgd 	 * the list of open files.
    599  1.16       cgd 	 */
    600  1.16       cgd 	nfiles++;
    601  1.16       cgd 	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
    602  1.17       cgd 	bzero(fp, sizeof(struct file));
    603  1.38  christos 	if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
    604  1.24   mycroft 		LIST_INSERT_AFTER(fq, fp, f_list);
    605  1.24   mycroft 	} else {
    606  1.24   mycroft 		LIST_INSERT_HEAD(&filehead, fp, f_list);
    607  1.24   mycroft 	}
    608  1.16       cgd 	p->p_fd->fd_ofiles[i] = fp;
    609  1.16       cgd 	fp->f_count = 1;
    610  1.16       cgd 	fp->f_cred = p->p_ucred;
    611  1.16       cgd 	crhold(fp->f_cred);
    612  1.16       cgd 	if (resultfp)
    613  1.16       cgd 		*resultfp = fp;
    614  1.16       cgd 	if (resultfd)
    615  1.16       cgd 		*resultfd = i;
    616  1.16       cgd 	return (0);
    617  1.16       cgd }
    618  1.16       cgd 
    619  1.16       cgd /*
    620  1.16       cgd  * Free a file descriptor.
    621  1.16       cgd  */
    622  1.38  christos void
    623  1.16       cgd ffree(fp)
    624  1.16       cgd 	register struct file *fp;
    625  1.16       cgd {
    626  1.24   mycroft 	LIST_REMOVE(fp, f_list);
    627  1.16       cgd 	crfree(fp->f_cred);
    628  1.16       cgd #ifdef DIAGNOSTIC
    629  1.16       cgd 	fp->f_count = 0;
    630  1.16       cgd #endif
    631  1.16       cgd 	nfiles--;
    632  1.16       cgd 	FREE(fp, M_FILE);
    633  1.48   thorpej }
    634  1.48   thorpej 
    635  1.48   thorpej /*
    636  1.48   thorpej  * Create an initial filedesc structure, using the same current and root
    637  1.48   thorpej  * directories as p.
    638  1.48   thorpej  */
    639  1.48   thorpej struct filedesc *
    640  1.48   thorpej fdinit(p)
    641  1.48   thorpej 	struct proc *p;
    642  1.48   thorpej {
    643  1.48   thorpej 	struct filedesc0 *newfdp;
    644  1.48   thorpej 	struct filedesc *fdp = p->p_fd;
    645  1.48   thorpej 
    646  1.48   thorpej 	MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
    647  1.48   thorpej 	    M_FILEDESC, M_WAITOK);
    648  1.48   thorpej 	bzero(newfdp, sizeof(struct filedesc0));
    649  1.48   thorpej 	newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
    650  1.48   thorpej 	VREF(newfdp->fd_fd.fd_cdir);
    651  1.48   thorpej 	newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
    652  1.48   thorpej 	if (newfdp->fd_fd.fd_rdir)
    653  1.48   thorpej 		VREF(newfdp->fd_fd.fd_rdir);
    654  1.48   thorpej 
    655  1.48   thorpej 	fdinit1(newfdp);
    656  1.48   thorpej 
    657  1.48   thorpej 	return (&newfdp->fd_fd);
    658  1.48   thorpej }
    659  1.48   thorpej 
    660  1.48   thorpej /*
    661  1.48   thorpej  * Initialize a file descriptor table.
    662  1.48   thorpej  */
    663  1.48   thorpej void
    664  1.48   thorpej fdinit1(newfdp)
    665  1.48   thorpej 	struct filedesc0 *newfdp;
    666  1.48   thorpej {
    667  1.48   thorpej 	extern int cmask;		/* init_main.c */
    668  1.48   thorpej 
    669  1.48   thorpej 	newfdp->fd_fd.fd_refcnt = 1;
    670  1.48   thorpej 	newfdp->fd_fd.fd_cmask = cmask;
    671  1.48   thorpej 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
    672  1.48   thorpej 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
    673  1.48   thorpej 	newfdp->fd_fd.fd_nfiles = NDFILE;
    674  1.48   thorpej }
    675  1.48   thorpej 
    676  1.48   thorpej /*
    677  1.48   thorpej  * Make p2 share p1's filedesc structure.
    678  1.48   thorpej  */
    679  1.48   thorpej void
    680  1.48   thorpej fdshare(p1, p2)
    681  1.48   thorpej 	struct proc *p1, *p2;
    682  1.48   thorpej {
    683  1.48   thorpej 
    684  1.48   thorpej 	p2->p_fd = p1->p_fd;
    685  1.48   thorpej 	p1->p_fd->fd_refcnt++;
    686  1.48   thorpej }
    687  1.48   thorpej 
    688  1.48   thorpej /*
    689  1.48   thorpej  * Make this process not share its filedesc structure, maintaining
    690  1.48   thorpej  * all file descriptor state.
    691  1.48   thorpej  */
    692  1.48   thorpej void
    693  1.48   thorpej fdunshare(p)
    694  1.48   thorpej 	struct proc *p;
    695  1.48   thorpej {
    696  1.48   thorpej 	struct filedesc *newfd;
    697  1.48   thorpej 
    698  1.48   thorpej 	if (p->p_fd->fd_refcnt == 1)
    699  1.48   thorpej 		return;
    700  1.48   thorpej 
    701  1.48   thorpej 	newfd = fdcopy(p);
    702  1.48   thorpej 	fdfree(p);
    703  1.48   thorpej 	p->p_fd = newfd;
    704  1.48   thorpej }
    705  1.48   thorpej 
    706  1.48   thorpej /*
    707  1.48   thorpej  * Clear a process's fd table.
    708  1.48   thorpej  */
    709  1.48   thorpej void
    710  1.48   thorpej fdclear(p)
    711  1.48   thorpej 	struct proc *p;
    712  1.48   thorpej {
    713  1.48   thorpej 	struct filedesc *newfd;
    714  1.48   thorpej 
    715  1.48   thorpej 	newfd = fdinit(p);
    716  1.48   thorpej 	fdfree(p);
    717  1.48   thorpej 	p->p_fd = newfd;
    718  1.16       cgd }
    719  1.16       cgd 
    720  1.16       cgd /*
    721  1.16       cgd  * Copy a filedesc structure.
    722  1.16       cgd  */
    723  1.16       cgd struct filedesc *
    724  1.16       cgd fdcopy(p)
    725  1.16       cgd 	struct proc *p;
    726  1.16       cgd {
    727  1.16       cgd 	register struct filedesc *newfdp, *fdp = p->p_fd;
    728  1.16       cgd 	register struct file **fpp;
    729  1.16       cgd 	register int i;
    730  1.16       cgd 
    731  1.16       cgd 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
    732  1.16       cgd 	    M_FILEDESC, M_WAITOK);
    733  1.16       cgd 	bcopy(fdp, newfdp, sizeof(struct filedesc));
    734  1.16       cgd 	VREF(newfdp->fd_cdir);
    735  1.16       cgd 	if (newfdp->fd_rdir)
    736  1.16       cgd 		VREF(newfdp->fd_rdir);
    737  1.16       cgd 	newfdp->fd_refcnt = 1;
    738  1.16       cgd 
    739  1.16       cgd 	/*
    740  1.16       cgd 	 * If the number of open files fits in the internal arrays
    741  1.16       cgd 	 * of the open file structure, use them, otherwise allocate
    742  1.16       cgd 	 * additional memory for the number of descriptors currently
    743  1.16       cgd 	 * in use.
    744  1.16       cgd 	 */
    745  1.16       cgd 	if (newfdp->fd_lastfile < NDFILE) {
    746  1.16       cgd 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
    747  1.16       cgd 		newfdp->fd_ofileflags =
    748  1.16       cgd 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
    749  1.16       cgd 		i = NDFILE;
    750  1.16       cgd 	} else {
    751  1.16       cgd 		/*
    752  1.16       cgd 		 * Compute the smallest multiple of NDEXTENT needed
    753  1.16       cgd 		 * for the file descriptors currently in use,
    754  1.16       cgd 		 * allowing the table to shrink.
    755  1.16       cgd 		 */
    756  1.16       cgd 		i = newfdp->fd_nfiles;
    757  1.27   mycroft 		while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
    758  1.16       cgd 			i /= 2;
    759  1.16       cgd 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
    760  1.16       cgd 		    M_FILEDESC, M_WAITOK);
    761  1.16       cgd 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
    762  1.16       cgd 	}
    763  1.16       cgd 	newfdp->fd_nfiles = i;
    764  1.16       cgd 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
    765  1.16       cgd 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
    766  1.16       cgd 	fpp = newfdp->fd_ofiles;
    767  1.27   mycroft 	for (i = newfdp->fd_lastfile; i >= 0; i--, fpp++)
    768  1.16       cgd 		if (*fpp != NULL)
    769  1.16       cgd 			(*fpp)->f_count++;
    770  1.16       cgd 	return (newfdp);
    771  1.16       cgd }
    772  1.16       cgd 
    773  1.16       cgd /*
    774  1.16       cgd  * Release a filedesc structure.
    775  1.16       cgd  */
    776  1.16       cgd void
    777  1.16       cgd fdfree(p)
    778  1.16       cgd 	struct proc *p;
    779  1.16       cgd {
    780  1.16       cgd 	register struct filedesc *fdp = p->p_fd;
    781  1.32   mycroft 	register struct file **fpp, *fp;
    782  1.16       cgd 	register int i;
    783  1.16       cgd 
    784  1.16       cgd 	if (--fdp->fd_refcnt > 0)
    785  1.16       cgd 		return;
    786  1.16       cgd 	fpp = fdp->fd_ofiles;
    787  1.32   mycroft 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
    788  1.32   mycroft 		fp = *fpp;
    789  1.32   mycroft 		if (fp != NULL) {
    790  1.32   mycroft 			*fpp = NULL;
    791  1.32   mycroft 			(void) closef(fp, p);
    792  1.32   mycroft 		}
    793  1.32   mycroft 	}
    794  1.32   mycroft 	p->p_fd = NULL;
    795  1.16       cgd 	if (fdp->fd_nfiles > NDFILE)
    796  1.16       cgd 		FREE(fdp->fd_ofiles, M_FILEDESC);
    797  1.16       cgd 	vrele(fdp->fd_cdir);
    798  1.16       cgd 	if (fdp->fd_rdir)
    799  1.16       cgd 		vrele(fdp->fd_rdir);
    800  1.16       cgd 	FREE(fdp, M_FILEDESC);
    801  1.16       cgd }
    802  1.16       cgd 
    803  1.16       cgd /*
    804  1.16       cgd  * Internal form of close.
    805  1.16       cgd  * Decrement reference count on file structure.
    806  1.17       cgd  * Note: p may be NULL when closing a file
    807  1.17       cgd  * that was being passed in a message.
    808  1.16       cgd  */
    809  1.38  christos int
    810  1.16       cgd closef(fp, p)
    811  1.16       cgd 	register struct file *fp;
    812  1.16       cgd 	register struct proc *p;
    813  1.16       cgd {
    814  1.16       cgd 	struct vnode *vp;
    815  1.16       cgd 	struct flock lf;
    816  1.16       cgd 	int error;
    817  1.16       cgd 
    818  1.16       cgd 	if (fp == NULL)
    819  1.16       cgd 		return (0);
    820  1.16       cgd 	/*
    821  1.16       cgd 	 * POSIX record locking dictates that any close releases ALL
    822  1.16       cgd 	 * locks owned by this process.  This is handled by setting
    823  1.16       cgd 	 * a flag in the unlock to free ONLY locks obeying POSIX
    824  1.16       cgd 	 * semantics, and not to free BSD-style file locks.
    825  1.17       cgd 	 * If the descriptor was in a message, POSIX-style locks
    826  1.17       cgd 	 * aren't passed with the descriptor.
    827  1.16       cgd 	 */
    828  1.16       cgd 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
    829  1.16       cgd 		lf.l_whence = SEEK_SET;
    830  1.16       cgd 		lf.l_start = 0;
    831  1.16       cgd 		lf.l_len = 0;
    832  1.16       cgd 		lf.l_type = F_UNLCK;
    833  1.16       cgd 		vp = (struct vnode *)fp->f_data;
    834  1.16       cgd 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
    835  1.16       cgd 	}
    836  1.16       cgd 	if (--fp->f_count > 0)
    837  1.16       cgd 		return (0);
    838  1.16       cgd 	if (fp->f_count < 0)
    839  1.16       cgd 		panic("closef: count < 0");
    840  1.16       cgd 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
    841  1.16       cgd 		lf.l_whence = SEEK_SET;
    842  1.16       cgd 		lf.l_start = 0;
    843  1.16       cgd 		lf.l_len = 0;
    844  1.16       cgd 		lf.l_type = F_UNLCK;
    845  1.16       cgd 		vp = (struct vnode *)fp->f_data;
    846  1.16       cgd 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
    847  1.16       cgd 	}
    848  1.17       cgd 	if (fp->f_ops)
    849  1.17       cgd 		error = (*fp->f_ops->fo_close)(fp, p);
    850  1.17       cgd 	else
    851  1.17       cgd 		error = 0;
    852  1.16       cgd 	ffree(fp);
    853  1.16       cgd 	return (error);
    854  1.16       cgd }
    855  1.16       cgd 
    856  1.16       cgd /*
    857  1.16       cgd  * Apply an advisory lock on a file descriptor.
    858  1.16       cgd  *
    859  1.16       cgd  * Just attempt to get a record lock of the requested type on
    860  1.16       cgd  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
    861  1.16       cgd  */
    862  1.16       cgd /* ARGSUSED */
    863  1.38  christos int
    864  1.37   mycroft sys_flock(p, v, retval)
    865  1.16       cgd 	struct proc *p;
    866  1.36   thorpej 	void *v;
    867  1.36   thorpej 	register_t *retval;
    868  1.36   thorpej {
    869  1.37   mycroft 	register struct sys_flock_args /* {
    870  1.25       cgd 		syscallarg(int) fd;
    871  1.25       cgd 		syscallarg(int) how;
    872  1.36   thorpej 	} */ *uap = v;
    873  1.27   mycroft 	int fd = SCARG(uap, fd);
    874  1.27   mycroft 	int how = SCARG(uap, how);
    875  1.16       cgd 	register struct filedesc *fdp = p->p_fd;
    876  1.16       cgd 	register struct file *fp;
    877  1.16       cgd 	struct vnode *vp;
    878  1.16       cgd 	struct flock lf;
    879  1.16       cgd 
    880  1.27   mycroft 	if ((u_int)fd >= fdp->fd_nfiles ||
    881  1.27   mycroft 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    882  1.16       cgd 		return (EBADF);
    883  1.16       cgd 	if (fp->f_type != DTYPE_VNODE)
    884  1.16       cgd 		return (EOPNOTSUPP);
    885  1.16       cgd 	vp = (struct vnode *)fp->f_data;
    886  1.16       cgd 	lf.l_whence = SEEK_SET;
    887  1.16       cgd 	lf.l_start = 0;
    888  1.16       cgd 	lf.l_len = 0;
    889  1.27   mycroft 	if (how & LOCK_UN) {
    890  1.16       cgd 		lf.l_type = F_UNLCK;
    891  1.16       cgd 		fp->f_flag &= ~FHASLOCK;
    892  1.16       cgd 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
    893  1.16       cgd 	}
    894  1.27   mycroft 	if (how & LOCK_EX)
    895  1.16       cgd 		lf.l_type = F_WRLCK;
    896  1.27   mycroft 	else if (how & LOCK_SH)
    897  1.16       cgd 		lf.l_type = F_RDLCK;
    898  1.16       cgd 	else
    899  1.44      phil 		return (EINVAL);
    900  1.16       cgd 	fp->f_flag |= FHASLOCK;
    901  1.27   mycroft 	if (how & LOCK_NB)
    902  1.16       cgd 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
    903  1.16       cgd 	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
    904  1.16       cgd }
    905  1.16       cgd 
    906  1.16       cgd /*
    907  1.16       cgd  * File Descriptor pseudo-device driver (/dev/fd/).
    908  1.16       cgd  *
    909  1.16       cgd  * Opening minor device N dup()s the file (if any) connected to file
    910  1.16       cgd  * descriptor N belonging to the calling process.  Note that this driver
    911  1.16       cgd  * consists of only the ``open()'' routine, because all subsequent
    912  1.16       cgd  * references to this file will be direct to the other driver.
    913  1.16       cgd  */
    914  1.16       cgd /* ARGSUSED */
    915  1.28   mycroft int
    916  1.40  christos filedescopen(dev, mode, type, p)
    917  1.16       cgd 	dev_t dev;
    918  1.16       cgd 	int mode, type;
    919  1.16       cgd 	struct proc *p;
    920  1.16       cgd {
    921  1.16       cgd 
    922  1.28   mycroft 	/*
    923  1.28   mycroft 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
    924  1.28   mycroft 	 * the file descriptor being sought for duplication. The error
    925  1.28   mycroft 	 * return ensures that the vnode for this device will be released
    926  1.28   mycroft 	 * by vn_open. Open will detect this special error and take the
    927  1.28   mycroft 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
    928  1.28   mycroft 	 * will simply report the error.
    929  1.28   mycroft 	 */
    930  1.28   mycroft 	p->p_dupfd = minor(dev);
    931  1.28   mycroft 	return (ENODEV);
    932  1.27   mycroft }
    933  1.27   mycroft 
    934  1.28   mycroft /*
    935  1.28   mycroft  * Duplicate the specified descriptor to a free descriptor.
    936  1.28   mycroft  */
    937  1.27   mycroft int
    938  1.28   mycroft dupfdopen(fdp, indx, dfd, mode, error)
    939  1.28   mycroft 	register struct filedesc *fdp;
    940  1.28   mycroft 	register int indx, dfd;
    941  1.27   mycroft 	int mode;
    942  1.28   mycroft 	int error;
    943  1.28   mycroft {
    944  1.28   mycroft 	register struct file *wfp;
    945  1.27   mycroft 	struct file *fp;
    946  1.27   mycroft 
    947  1.27   mycroft 	/*
    948  1.27   mycroft 	 * If the to-be-dup'd fd number is greater than the allowed number
    949  1.27   mycroft 	 * of file descriptors, or the fd to be dup'd has already been
    950  1.27   mycroft 	 * closed, reject.  Note, check for new == old is necessary as
    951  1.27   mycroft 	 * falloc could allocate an already closed to-be-dup'd descriptor
    952  1.27   mycroft 	 * as the new descriptor.
    953  1.27   mycroft 	 */
    954  1.28   mycroft 	fp = fdp->fd_ofiles[indx];
    955  1.28   mycroft 	if ((u_int)dfd >= fdp->fd_nfiles ||
    956  1.28   mycroft 	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
    957  1.27   mycroft 		return (EBADF);
    958  1.27   mycroft 
    959  1.27   mycroft 	/*
    960  1.28   mycroft 	 * There are two cases of interest here.
    961  1.28   mycroft 	 *
    962  1.28   mycroft 	 * For ENODEV simply dup (dfd) to file descriptor
    963  1.28   mycroft 	 * (indx) and return.
    964  1.28   mycroft 	 *
    965  1.28   mycroft 	 * For ENXIO steal away the file structure from (dfd) and
    966  1.28   mycroft 	 * store it in (indx).  (dfd) is effectively closed by
    967  1.28   mycroft 	 * this operation.
    968  1.28   mycroft 	 *
    969  1.28   mycroft 	 * Any other error code is just returned.
    970  1.27   mycroft 	 */
    971  1.28   mycroft 	switch (error) {
    972  1.28   mycroft 	case ENODEV:
    973  1.28   mycroft 		/*
    974  1.28   mycroft 		 * Check that the mode the file is being opened for is a
    975  1.28   mycroft 		 * subset of the mode of the existing descriptor.
    976  1.28   mycroft 		 */
    977  1.28   mycroft 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
    978  1.28   mycroft 			return (EACCES);
    979  1.28   mycroft 		fdp->fd_ofiles[indx] = wfp;
    980  1.28   mycroft 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    981  1.28   mycroft 		wfp->f_count++;
    982  1.28   mycroft 		fd_used(fdp, indx);
    983  1.28   mycroft 		return (0);
    984  1.27   mycroft 
    985  1.28   mycroft 	case ENXIO:
    986  1.28   mycroft 		/*
    987  1.28   mycroft 		 * Steal away the file pointer from dfd, and stuff it into indx.
    988  1.28   mycroft 		 */
    989  1.28   mycroft 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
    990  1.28   mycroft 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    991  1.28   mycroft 		fdp->fd_ofiles[dfd] = NULL;
    992  1.28   mycroft 		fdp->fd_ofileflags[dfd] = 0;
    993  1.28   mycroft 		/*
    994  1.28   mycroft 		 * Complete the clean up of the filedesc structure by
    995  1.28   mycroft 		 * recomputing the various hints.
    996  1.28   mycroft 		 */
    997  1.28   mycroft 		fd_used(fdp, indx);
    998  1.28   mycroft 		fd_unused(fdp, dfd);
    999  1.28   mycroft 		return (0);
   1000  1.16       cgd 
   1001  1.28   mycroft 	default:
   1002  1.28   mycroft 		return (error);
   1003  1.28   mycroft 	}
   1004  1.28   mycroft 	/* NOTREACHED */
   1005  1.27   mycroft }
   1006  1.16       cgd 
   1007  1.27   mycroft /*
   1008  1.27   mycroft  * Close any files on exec?
   1009  1.27   mycroft  */
   1010  1.27   mycroft void
   1011  1.27   mycroft fdcloseexec(p)
   1012  1.27   mycroft 	struct proc *p;
   1013  1.27   mycroft {
   1014  1.27   mycroft 	register struct filedesc *fdp = p->p_fd;
   1015  1.27   mycroft 	register int fd;
   1016  1.16       cgd 
   1017  1.27   mycroft 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
   1018  1.27   mycroft 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
   1019  1.34   mycroft 			(void) fdrelease(p, fd);
   1020  1.16       cgd }
   1021