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