Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.72.2.4
      1 /*	$NetBSD: kern_descrip.c,v 1.72.2.4 2001/08/24 00:11:26 nathanw 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.8 (Berkeley) 2/14/95
     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/lwp.h>
     49 #include <sys/proc.h>
     50 #include <sys/file.h>
     51 #include <sys/socket.h>
     52 #include <sys/socketvar.h>
     53 #include <sys/stat.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/fcntl.h>
     56 #include <sys/malloc.h>
     57 #include <sys/pool.h>
     58 #include <sys/syslog.h>
     59 #include <sys/unistd.h>
     60 #include <sys/resourcevar.h>
     61 #include <sys/conf.h>
     62 
     63 #include <sys/mount.h>
     64 #include <sys/syscallargs.h>
     65 
     66 /*
     67  * Descriptor management.
     68  */
     69 struct filelist	filehead;	/* head of list of open files */
     70 int		nfiles;		/* actual number of open files */
     71 struct pool	file_pool;	/* memory pool for file structures */
     72 struct pool	cwdi_pool;	/* memory pool for cwdinfo structures */
     73 struct pool	filedesc0_pool;	/* memory pool for filedesc0 structures */
     74 
     75 static __inline void	fd_used(struct filedesc *, int);
     76 static __inline void	fd_unused(struct filedesc *, int);
     77 int			finishdup(struct proc *, int, int, register_t *);
     78 int			fcntl_forfs(int, struct proc *, int, void *);
     79 
     80 static __inline void
     81 fd_used(struct filedesc *fdp, int fd)
     82 {
     83 
     84 	if (fd > fdp->fd_lastfile)
     85 		fdp->fd_lastfile = fd;
     86 }
     87 
     88 static __inline void
     89 fd_unused(struct filedesc *fdp, int fd)
     90 {
     91 
     92 	if (fd < fdp->fd_freefile)
     93 		fdp->fd_freefile = fd;
     94 #ifdef DIAGNOSTIC
     95 	if (fd > fdp->fd_lastfile)
     96 		panic("fd_unused: fd_lastfile inconsistent");
     97 #endif
     98 	if (fd == fdp->fd_lastfile) {
     99 		do {
    100 			fd--;
    101 		} while (fd >= 0 && fdp->fd_ofiles[fd] == NULL);
    102 		fdp->fd_lastfile = fd;
    103 	}
    104 }
    105 
    106 struct file *
    107 fd_getfile(struct filedesc *fdp, int fd)
    108 {
    109 	struct file *fp;
    110 
    111 	if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
    112 		return (NULL);
    113 
    114 	if (FILE_IS_USABLE(fp) == 0)
    115 		return (NULL);
    116 
    117 	return (fp);
    118 }
    119 
    120 /*
    121  * System calls on descriptors.
    122  */
    123 
    124 /*
    125  * Duplicate a file descriptor.
    126  */
    127 /* ARGSUSED */
    128 int
    129 sys_dup(struct lwp *l, void *v, register_t *retval)
    130 {
    131 	struct sys_dup_args /* {
    132 		syscallarg(int)	fd;
    133 	} */ *uap = v;
    134 	struct file 	*fp;
    135 	struct filedesc *fdp;
    136 	struct proc 	*p;
    137 	int 		old, new, error;
    138 
    139 	p = l->l_proc;
    140 	fdp = p->p_fd;
    141 	old = SCARG(uap, fd);
    142 
    143  restart:
    144 	if ((fp = fd_getfile(fdp, old)) == NULL)
    145 		return (EBADF);
    146 
    147 	FILE_USE(fp);
    148 
    149 	if ((error = fdalloc(p, 0, &new)) != 0) {
    150 		if (error == ENOSPC) {
    151 			fdexpand(p);
    152 			FILE_UNUSE(fp, p);
    153 			goto restart;
    154 		}
    155 		FILE_UNUSE(fp, p);
    156 		return (error);
    157 	}
    158 
    159 	/* finishdup() will unuse the descriptors for us */
    160 	return (finishdup(p, old, new, retval));
    161 }
    162 
    163 /*
    164  * Duplicate a file descriptor to a particular value.
    165  */
    166 /* ARGSUSED */
    167 int
    168 sys_dup2(struct lwp *l, void *v, register_t *retval)
    169 {
    170 	struct sys_dup2_args /* {
    171 		syscallarg(int)	from;
    172 		syscallarg(int)	to;
    173 	} */ *uap = v;
    174 	struct file	*fp;
    175 	struct filedesc	*fdp;
    176 	struct proc	*p;
    177 	int		old, new, i, error;
    178 
    179 	p = l->l_proc;
    180 	fdp = p->p_fd;
    181 	old = SCARG(uap, from);
    182 	new = SCARG(uap, to);
    183 
    184  restart:
    185 	if ((fp = fd_getfile(fdp, old)) == NULL)
    186 		return (EBADF);
    187 
    188 	if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    189 	    (u_int)new >= maxfiles)
    190 		return (EBADF);
    191 
    192 	if (old == new) {
    193 		*retval = new;
    194 		return (0);
    195 	}
    196 
    197 	FILE_USE(fp);
    198 
    199 	if (new >= fdp->fd_nfiles) {
    200 		if ((error = fdalloc(p, new, &i)) != 0) {
    201 			if (error == ENOSPC) {
    202 				fdexpand(p);
    203 				FILE_UNUSE(fp, p);
    204 				goto restart;
    205 			}
    206 			FILE_UNUSE(fp, p);
    207 			return (error);
    208 		}
    209 		if (new != i)
    210 			panic("dup2: fdalloc");
    211 	}
    212 
    213 	/*
    214 	 * finishdup() will close the file that's in the `new'
    215 	 * slot, if there's one there.
    216 	 */
    217 
    218 	/* finishdup() will unuse the descriptors for us */
    219 	return (finishdup(p, old, new, retval));
    220 }
    221 
    222 /*
    223  * The file control system call.
    224  */
    225 /* ARGSUSED */
    226 int
    227 sys_fcntl(struct lwp *l, void *v, register_t *retval)
    228 {
    229 	struct sys_fcntl_args /* {
    230 		syscallarg(int)		fd;
    231 		syscallarg(int)		cmd;
    232 		syscallarg(void *)	arg;
    233 	} */ *uap = v;
    234 	struct filedesc *fdp;
    235 	struct file	*fp;
    236 	struct proc	*p;
    237 	struct vnode	*vp;
    238 	int		fd, i, tmp, error, flg, cmd, newmin;
    239 	struct flock	fl;
    240 
    241 	p = l->l_proc;
    242 	fd = SCARG(uap, fd);
    243 	fdp = p->p_fd;
    244 	error = 0;
    245 	flg = F_POSIX;
    246 
    247  restart:
    248 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    249 		return (EBADF);
    250 
    251 	FILE_USE(fp);
    252 
    253 	cmd = SCARG(uap, cmd);
    254 	if ((cmd & F_FSCTL)) {
    255 		error = fcntl_forfs(fd, p, cmd, SCARG(uap, arg));
    256 		goto out;
    257 	}
    258 
    259 	switch (cmd) {
    260 
    261 	case F_DUPFD:
    262 		newmin = (long)SCARG(uap, arg);
    263 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    264 		    (u_int)newmin >= maxfiles) {
    265 			error = EINVAL;
    266 			goto out;
    267 		}
    268 		if ((error = fdalloc(p, newmin, &i)) != 0) {
    269 			if (error == ENOSPC) {
    270 				fdexpand(p);
    271 				FILE_UNUSE(fp, p);
    272 				goto restart;
    273 			}
    274 			goto out;
    275 		}
    276 
    277 		/* finishdup() will unuse the descriptors for us */
    278 		return (finishdup(p, fd, i, retval));
    279 
    280 	case F_GETFD:
    281 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
    282 		break;
    283 
    284 	case F_SETFD:
    285 		if ((long)SCARG(uap, arg) & 1)
    286 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
    287 		else
    288 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
    289 		break;
    290 
    291 	case F_GETFL:
    292 		*retval = OFLAGS(fp->f_flag);
    293 		break;
    294 
    295 	case F_SETFL:
    296 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    297 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, (caddr_t)&tmp, p);
    298 		if (error)
    299 			goto out;
    300 		fp->f_flag &= ~FCNTLFLAGS;
    301 		fp->f_flag |= tmp;
    302 		tmp = fp->f_flag & FNONBLOCK;
    303 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    304 		if (error)
    305 			goto out;
    306 		tmp = fp->f_flag & FASYNC;
    307 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    308 		if (error == 0)
    309 			goto out;
    310 		fp->f_flag &= ~FNONBLOCK;
    311 		tmp = 0;
    312 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    313 		break;
    314 
    315 	case F_GETOWN:
    316 		if (fp->f_type == DTYPE_SOCKET) {
    317 			*retval = ((struct socket *)fp->f_data)->so_pgid;
    318 			goto out;
    319 		}
    320 		error = (*fp->f_ops->fo_ioctl)
    321 			(fp, TIOCGPGRP, (caddr_t)retval, p);
    322 		*retval = -*retval;
    323 		break;
    324 
    325 	case F_SETOWN:
    326 		if (fp->f_type == DTYPE_SOCKET) {
    327 			((struct socket *)fp->f_data)->so_pgid =
    328 			    (long)SCARG(uap, arg);
    329 			goto out;
    330 		}
    331 		if ((long)SCARG(uap, arg) <= 0) {
    332 			tmp = (-(long)SCARG(uap, arg));
    333 		} else {
    334 			struct proc *p1 = pfind((long)SCARG(uap, arg));
    335 			if (p1 == 0) {
    336 				error = ESRCH;
    337 				goto out;
    338 			}
    339 			tmp = (long)p1->p_pgrp->pg_id;
    340 		}
    341 		error = (*fp->f_ops->fo_ioctl)
    342 		    (fp, TIOCSPGRP, (caddr_t)&tmp, p);
    343 		break;
    344 
    345 	case F_SETLKW:
    346 		flg |= F_WAIT;
    347 		/* Fall into F_SETLK */
    348 
    349 	case F_SETLK:
    350 		if (fp->f_type != DTYPE_VNODE) {
    351 			error = EINVAL;
    352 			goto out;
    353 		}
    354 		vp = (struct vnode *)fp->f_data;
    355 		/* Copy in the lock structure */
    356 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    357 		    sizeof(fl));
    358 		if (error)
    359 			goto out;
    360 		if (fl.l_whence == SEEK_CUR)
    361 			fl.l_start += fp->f_offset;
    362 		switch (fl.l_type) {
    363 		case F_RDLCK:
    364 			if ((fp->f_flag & FREAD) == 0) {
    365 				error = EBADF;
    366 				goto out;
    367 			}
    368 			p->p_flag |= P_ADVLOCK;
    369 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
    370 			goto out;
    371 
    372 		case F_WRLCK:
    373 			if ((fp->f_flag & FWRITE) == 0) {
    374 				error = EBADF;
    375 				goto out;
    376 			}
    377 			p->p_flag |= P_ADVLOCK;
    378 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
    379 			goto out;
    380 
    381 		case F_UNLCK:
    382 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
    383 			    F_POSIX);
    384 			goto out;
    385 
    386 		default:
    387 			error = EINVAL;
    388 			goto out;
    389 		}
    390 
    391 	case F_GETLK:
    392 		if (fp->f_type != DTYPE_VNODE) {
    393 			error = EINVAL;
    394 			goto out;
    395 		}
    396 		vp = (struct vnode *)fp->f_data;
    397 		/* Copy in the lock structure */
    398 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    399 		    sizeof(fl));
    400 		if (error)
    401 			goto out;
    402 		if (fl.l_whence == SEEK_CUR)
    403 			fl.l_start += fp->f_offset;
    404 		if (fl.l_type != F_RDLCK &&
    405 		    fl.l_type != F_WRLCK &&
    406 		    fl.l_type != F_UNLCK) {
    407 			error = EINVAL;
    408 			goto out;
    409 		}
    410 		error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX);
    411 		if (error)
    412 			goto out;
    413 		error = copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
    414 		    sizeof(fl));
    415 		break;
    416 
    417 	default:
    418 		error = EINVAL;
    419 	}
    420 
    421  out:
    422 	FILE_UNUSE(fp, p);
    423 	return (error);
    424 }
    425 
    426 /*
    427  * Common code for dup, dup2, and fcntl(F_DUPFD).
    428  */
    429 int
    430 finishdup(struct proc *p, int old, int new, register_t *retval)
    431 {
    432 	struct filedesc	*fdp;
    433 	struct file	*fp, *delfp;
    434 
    435 	fdp = p->p_fd;
    436 
    437 	/*
    438 	 * If there is a file in the new slot, remember it so we
    439 	 * can close it after we've finished the dup.  We need
    440 	 * to do it after the dup is finished, since closing
    441 	 * the file may block.
    442 	 *
    443 	 * Note: `old' is already used for us.
    444 	 */
    445 	delfp = fdp->fd_ofiles[new];
    446 
    447 	fp = fdp->fd_ofiles[old];
    448 	fdp->fd_ofiles[new] = fp;
    449 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    450 	fp->f_count++;
    451 	/*
    452 	 * Note, don't have to mark it "used" in the table if there
    453 	 * was already a file in the `new' slot.
    454 	 */
    455 	if (delfp == NULL)
    456 		fd_used(fdp, new);
    457 	*retval = new;
    458 	FILE_UNUSE(fp, p);
    459 
    460 	if (delfp != NULL) {
    461 		FILE_USE(delfp);
    462 		(void) closef(delfp, p);
    463 	}
    464 	return (0);
    465 }
    466 
    467 void
    468 fdremove(struct filedesc *fdp, int fd)
    469 {
    470 
    471 	fdp->fd_ofiles[fd] = NULL;
    472 	fd_unused(fdp, fd);
    473 }
    474 
    475 int
    476 fdrelease(struct proc *p, int fd)
    477 {
    478 	struct filedesc	*fdp;
    479 	struct file	**fpp, *fp;
    480 
    481 	fdp = p->p_fd;
    482 	fpp = &fdp->fd_ofiles[fd];
    483 	fp = *fpp;
    484 	if (fp == NULL)
    485 		return (EBADF);
    486 
    487 	FILE_USE(fp);
    488 
    489 	*fpp = NULL;
    490 	fdp->fd_ofileflags[fd] = 0;
    491 	fd_unused(fdp, fd);
    492 	return (closef(fp, p));
    493 }
    494 
    495 /*
    496  * Close a file descriptor.
    497  */
    498 /* ARGSUSED */
    499 int
    500 sys_close(struct lwp *l, void *v, register_t *retval)
    501 {
    502 	struct sys_close_args /* {
    503 		syscallarg(int)	fd;
    504 	} */ *uap = v;
    505 	int		fd;
    506 	struct filedesc	*fdp;
    507 	struct proc *p;
    508 	struct file	*fp;
    509 
    510 	p = l->l_proc;
    511 	fd = SCARG(uap, fd);
    512 	fdp = p->p_fd;
    513 
    514 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    515 		return (EBADF);
    516 
    517 	return (fdrelease(p, fd));
    518 }
    519 
    520 /*
    521  * Return status information about a file descriptor.
    522  */
    523 /* ARGSUSED */
    524 int
    525 sys___fstat13(struct lwp *l, void *v, register_t *retval)
    526 {
    527 	struct sys___fstat13_args /* {
    528 		syscallarg(int)			fd;
    529 		syscallarg(struct stat *)	sb;
    530 	} */ *uap = v;
    531 	int		fd;
    532 	struct filedesc	*fdp;
    533 	struct file	*fp;
    534 	struct proc	*p;
    535 	struct stat	ub;
    536 	int		error;
    537 
    538 	p = l->l_proc;
    539 	fd = SCARG(uap, fd);
    540 	fdp = p->p_fd;
    541 
    542 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    543 		return (EBADF);
    544 
    545 	FILE_USE(fp);
    546 	error = (*fp->f_ops->fo_stat)(fp, &ub, p);
    547 	FILE_UNUSE(fp, p);
    548 
    549 	if (error == 0)
    550 		error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
    551 
    552 	return (error);
    553 }
    554 
    555 /*
    556  * Return pathconf information about a file descriptor.
    557  */
    558 /* ARGSUSED */
    559 int
    560 sys_fpathconf(struct lwp *l, void *v, register_t *retval)
    561 {
    562 	struct sys_fpathconf_args /* {
    563 		syscallarg(int)	fd;
    564 		syscallarg(int)	name;
    565 	} */ *uap = v;
    566 	int		fd;
    567 	struct filedesc	*fdp;
    568 	struct file	*fp;
    569 	struct proc 	*p;
    570 	struct vnode	*vp;
    571 	int		error;
    572 
    573 	p = l->l_proc;
    574 	fd = SCARG(uap, fd);
    575 	fdp = p->p_fd;
    576 	error = 0;
    577 
    578 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    579 		return (EBADF);
    580 
    581 	FILE_USE(fp);
    582 
    583 	switch (fp->f_type) {
    584 
    585 	case DTYPE_SOCKET:
    586 	case DTYPE_PIPE:
    587 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    588 			error = EINVAL;
    589 		else
    590 			*retval = PIPE_BUF;
    591 		break;
    592 
    593 	case DTYPE_VNODE:
    594 		vp = (struct vnode *)fp->f_data;
    595 		error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
    596 		break;
    597 
    598 	default:
    599 		panic("fpathconf");
    600 	}
    601 
    602 	FILE_UNUSE(fp, p);
    603 	return (error);
    604 }
    605 
    606 /*
    607  * Allocate a file descriptor for the process.
    608  */
    609 int	fdexpanded;		/* XXX: what else uses this? */
    610 
    611 int
    612 fdalloc(struct proc *p, int want, int *result)
    613 {
    614 	struct filedesc	*fdp;
    615 	int i, lim, last;
    616 
    617 	fdp = p->p_fd;
    618 
    619 	/*
    620 	 * Search for a free descriptor starting at the higher
    621 	 * of want or fd_freefile.  If that fails, consider
    622 	 * expanding the ofile array.
    623 	 */
    624 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    625 	for (;;) {
    626 		last = min(fdp->fd_nfiles, lim);
    627 		if ((i = want) < fdp->fd_freefile)
    628 			i = fdp->fd_freefile;
    629 		for (; i < last; i++) {
    630 			if (fdp->fd_ofiles[i] == NULL) {
    631 				fd_used(fdp, i);
    632 				if (want <= fdp->fd_freefile)
    633 					fdp->fd_freefile = i;
    634 				*result = i;
    635 				return (0);
    636 			}
    637 		}
    638 
    639 		/* No space in current array.  Expand? */
    640 		if (fdp->fd_nfiles >= lim)
    641 			return (EMFILE);
    642 
    643 		/* Let the caller do it. */
    644 		return (ENOSPC);
    645 	}
    646 }
    647 
    648 void
    649 fdexpand(struct proc *p)
    650 {
    651 	struct filedesc	*fdp;
    652 	int		i, nfiles;
    653 	struct file	**newofile;
    654 	char		*newofileflags;
    655 
    656 	fdp = p->p_fd;
    657 
    658 	if (fdp->fd_nfiles < NDEXTENT)
    659 		nfiles = NDEXTENT;
    660 	else
    661 		nfiles = 2 * fdp->fd_nfiles;
    662 	newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
    663 	newofileflags = (char *) &newofile[nfiles];
    664 	/*
    665 	 * Copy the existing ofile and ofileflags arrays
    666 	 * and zero the new portion of each array.
    667 	 */
    668 	memcpy(newofile, fdp->fd_ofiles,
    669 		(i = sizeof(struct file *) * fdp->fd_nfiles));
    670 	memset((char *)newofile + i, 0,
    671 	    nfiles * sizeof(struct file *) - i);
    672 	memcpy(newofileflags, fdp->fd_ofileflags,
    673 	    (i = sizeof(char) * fdp->fd_nfiles));
    674 	memset(newofileflags + i, 0, nfiles * sizeof(char) - i);
    675 	if (fdp->fd_nfiles > NDFILE)
    676 		free(fdp->fd_ofiles, M_FILEDESC);
    677 	fdp->fd_ofiles = newofile;
    678 	fdp->fd_ofileflags = newofileflags;
    679 	fdp->fd_nfiles = nfiles;
    680 	fdexpanded++;
    681 }
    682 
    683 /*
    684  * Check to see whether n user file descriptors
    685  * are available to the process p.
    686  */
    687 int
    688 fdavail(struct proc *p, int n)
    689 {
    690 	struct filedesc	*fdp;
    691 	struct file	**fpp;
    692 	int		i, lim;
    693 
    694 	fdp = p->p_fd;
    695 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    696 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
    697 		return (1);
    698 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
    699 	for (i = min(lim,fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++)
    700 		if (*fpp == NULL && --n <= 0)
    701 			return (1);
    702 	return (0);
    703 }
    704 
    705 /*
    706  * Initialize the data structures necessary for managing files.
    707  */
    708 void
    709 finit(void)
    710 {
    711 
    712 	pool_init(&file_pool, sizeof(struct file), 0, 0, 0, "filepl",
    713 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILE);
    714 	pool_init(&cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
    715 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
    716 	pool_init(&filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
    717 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
    718 }
    719 
    720 /*
    721  * Create a new open file structure and allocate
    722  * a file decriptor for the process that refers to it.
    723  */
    724 int
    725 falloc(struct proc *p, struct file **resultfp, int *resultfd)
    726 {
    727 	struct file	*fp, *fq;
    728 	int		error, i;
    729 
    730  restart:
    731 	if ((error = fdalloc(p, 0, &i)) != 0) {
    732 		if (error == ENOSPC) {
    733 			fdexpand(p);
    734 			goto restart;
    735 		}
    736 		return (error);
    737 	}
    738 	if (nfiles >= maxfiles) {
    739 		tablefull("file", "increase kern.maxfiles or MAXFILES");
    740 		return (ENFILE);
    741 	}
    742 	/*
    743 	 * Allocate a new file descriptor.
    744 	 * If the process has file descriptor zero open, add to the list
    745 	 * of open files at that point, otherwise put it at the front of
    746 	 * the list of open files.
    747 	 */
    748 	nfiles++;
    749 	fp = pool_get(&file_pool, PR_WAITOK);
    750 	memset(fp, 0, sizeof(struct file));
    751 	fp->f_iflags = FIF_LARVAL;
    752 	if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
    753 		LIST_INSERT_AFTER(fq, fp, f_list);
    754 	} else {
    755 		LIST_INSERT_HEAD(&filehead, fp, f_list);
    756 	}
    757 	p->p_fd->fd_ofiles[i] = fp;
    758 	fp->f_count = 1;
    759 	fp->f_cred = p->p_ucred;
    760 	crhold(fp->f_cred);
    761 	if (resultfp) {
    762 		FILE_USE(fp);
    763 		*resultfp = fp;
    764 	}
    765 	if (resultfd)
    766 		*resultfd = i;
    767 	return (0);
    768 }
    769 
    770 /*
    771  * Free a file descriptor.
    772  */
    773 void
    774 ffree(struct file *fp)
    775 {
    776 
    777 #ifdef DIAGNOSTIC
    778 	if (fp->f_usecount)
    779 		panic("ffree");
    780 #endif
    781 
    782 	LIST_REMOVE(fp, f_list);
    783 	crfree(fp->f_cred);
    784 #ifdef DIAGNOSTIC
    785 	fp->f_count = 0;
    786 #endif
    787 	nfiles--;
    788 	pool_put(&file_pool, fp);
    789 }
    790 
    791 /*
    792  * Create an initial cwdinfo structure, using the same current and root
    793  * directories as p.
    794  */
    795 struct cwdinfo *
    796 cwdinit(struct proc *p)
    797 {
    798 	struct cwdinfo *cwdi;
    799 
    800 	cwdi = pool_get(&cwdi_pool, PR_WAITOK);
    801 
    802 	cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
    803 	if (cwdi->cwdi_cdir)
    804 		VREF(cwdi->cwdi_cdir);
    805 	cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
    806 	if (cwdi->cwdi_rdir)
    807 		VREF(cwdi->cwdi_rdir);
    808 	cwdi->cwdi_cmask =  p->p_cwdi->cwdi_cmask;
    809 	cwdi->cwdi_refcnt = 1;
    810 
    811 	return (cwdi);
    812 }
    813 
    814 /*
    815  * Make p2 share p1's cwdinfo.
    816  */
    817 void
    818 cwdshare(struct proc *p1, struct proc *p2)
    819 {
    820 
    821 	p2->p_cwdi = p1->p_cwdi;
    822 	p1->p_cwdi->cwdi_refcnt++;
    823 }
    824 
    825 /*
    826  * Make this process not share its cwdinfo structure, maintaining
    827  * all cwdinfo state.
    828  */
    829 void
    830 cwdunshare(struct proc *p)
    831 {
    832 	struct cwdinfo *newcwdi;
    833 
    834 	if (p->p_cwdi->cwdi_refcnt == 1)
    835 		return;
    836 
    837 	newcwdi = cwdinit(p);
    838 	cwdfree(p);
    839 	p->p_cwdi = newcwdi;
    840 }
    841 
    842 /*
    843  * Release a cwdinfo structure.
    844  */
    845 void
    846 cwdfree(struct proc *p)
    847 {
    848 	struct cwdinfo *cwdi;
    849 
    850 	cwdi = p->p_cwdi;
    851 	if (--cwdi->cwdi_refcnt > 0)
    852 		return;
    853 
    854 	p->p_cwdi = NULL;
    855 
    856 	vrele(cwdi->cwdi_cdir);
    857 	if (cwdi->cwdi_rdir)
    858 		vrele(cwdi->cwdi_rdir);
    859 	pool_put(&cwdi_pool, cwdi);
    860 }
    861 
    862 /*
    863  * Create an initial filedesc structure, using the same current and root
    864  * directories as p.
    865  */
    866 struct filedesc *
    867 fdinit(struct proc *p)
    868 {
    869 	struct filedesc0 *newfdp;
    870 
    871 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
    872 	memset(newfdp, 0, sizeof(struct filedesc0));
    873 
    874 	fdinit1(newfdp);
    875 
    876 	return (&newfdp->fd_fd);
    877 }
    878 
    879 /*
    880  * Initialize a file descriptor table.
    881  */
    882 void
    883 fdinit1(struct filedesc0 *newfdp)
    884 {
    885 
    886 	newfdp->fd_fd.fd_refcnt = 1;
    887 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
    888 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
    889 	newfdp->fd_fd.fd_nfiles = NDFILE;
    890 }
    891 
    892 /*
    893  * Make p2 share p1's filedesc structure.
    894  */
    895 void
    896 fdshare(struct proc *p1, struct proc *p2)
    897 {
    898 
    899 	p2->p_fd = p1->p_fd;
    900 	p1->p_fd->fd_refcnt++;
    901 }
    902 
    903 /*
    904  * Make this process not share its filedesc structure, maintaining
    905  * all file descriptor state.
    906  */
    907 void
    908 fdunshare(struct proc *p)
    909 {
    910 	struct filedesc *newfd;
    911 
    912 	if (p->p_fd->fd_refcnt == 1)
    913 		return;
    914 
    915 	newfd = fdcopy(p);
    916 	fdfree(p);
    917 	p->p_fd = newfd;
    918 }
    919 
    920 /*
    921  * Clear a process's fd table.
    922  */
    923 void
    924 fdclear(struct proc *p)
    925 {
    926 	struct filedesc *newfd;
    927 
    928 	newfd = fdinit(p);
    929 	fdfree(p);
    930 	p->p_fd = newfd;
    931 }
    932 
    933 /*
    934  * Copy a filedesc structure.
    935  */
    936 struct filedesc *
    937 fdcopy(struct proc *p)
    938 {
    939 	struct filedesc	*newfdp, *fdp;
    940 	struct file	**fpp;
    941 	int		i;
    942 
    943 	fdp = p->p_fd;
    944 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
    945 	memcpy(newfdp, fdp, sizeof(struct filedesc));
    946 	newfdp->fd_refcnt = 1;
    947 
    948 	/*
    949 	 * If the number of open files fits in the internal arrays
    950 	 * of the open file structure, use them, otherwise allocate
    951 	 * additional memory for the number of descriptors currently
    952 	 * in use.
    953 	 */
    954 	if (newfdp->fd_lastfile < NDFILE) {
    955 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
    956 		newfdp->fd_ofileflags =
    957 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
    958 		i = NDFILE;
    959 	} else {
    960 		/*
    961 		 * Compute the smallest multiple of NDEXTENT needed
    962 		 * for the file descriptors currently in use,
    963 		 * allowing the table to shrink.
    964 		 */
    965 		i = newfdp->fd_nfiles;
    966 		while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
    967 			i /= 2;
    968 		newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
    969 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
    970 	}
    971 	newfdp->fd_nfiles = i;
    972 	memcpy(newfdp->fd_ofiles, fdp->fd_ofiles, i * sizeof(struct file **));
    973 	memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
    974 	fpp = newfdp->fd_ofiles;
    975 	for (i = newfdp->fd_lastfile; i >= 0; i--, fpp++)
    976 		if (*fpp != NULL)
    977 			(*fpp)->f_count++;
    978 	return (newfdp);
    979 }
    980 
    981 /*
    982  * Release a filedesc structure.
    983  */
    984 void
    985 fdfree(struct proc *p)
    986 {
    987 	struct filedesc	*fdp;
    988 	struct file	**fpp, *fp;
    989 	int		i;
    990 
    991 	fdp = p->p_fd;
    992 	if (--fdp->fd_refcnt > 0)
    993 		return;
    994 	fpp = fdp->fd_ofiles;
    995 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
    996 		fp = *fpp;
    997 		if (fp != NULL) {
    998 			*fpp = NULL;
    999 			FILE_USE(fp);
   1000 			(void) closef(fp, p);
   1001 		}
   1002 	}
   1003 	p->p_fd = NULL;
   1004 	if (fdp->fd_nfiles > NDFILE)
   1005 		free(fdp->fd_ofiles, M_FILEDESC);
   1006 	pool_put(&filedesc0_pool, fdp);
   1007 }
   1008 
   1009 /*
   1010  * Internal form of close.
   1011  * Decrement reference count on file structure.
   1012  * Note: p may be NULL when closing a file
   1013  * that was being passed in a message.
   1014  *
   1015  * Note: we expect the caller is holding a usecount, and expects us
   1016  * to drop it (the caller thinks the file is going away forever).
   1017  */
   1018 int
   1019 closef(struct file *fp, struct proc *p)
   1020 {
   1021 	struct vnode	*vp;
   1022 	struct flock	lf;
   1023 	int		error;
   1024 
   1025 	if (fp == NULL)
   1026 		return (0);
   1027 
   1028 	/*
   1029 	 * POSIX record locking dictates that any close releases ALL
   1030 	 * locks owned by this process.  This is handled by setting
   1031 	 * a flag in the unlock to free ONLY locks obeying POSIX
   1032 	 * semantics, and not to free BSD-style file locks.
   1033 	 * If the descriptor was in a message, POSIX-style locks
   1034 	 * aren't passed with the descriptor.
   1035 	 */
   1036 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
   1037 		lf.l_whence = SEEK_SET;
   1038 		lf.l_start = 0;
   1039 		lf.l_len = 0;
   1040 		lf.l_type = F_UNLCK;
   1041 		vp = (struct vnode *)fp->f_data;
   1042 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
   1043 	}
   1044 
   1045 	/*
   1046 	 * If WANTCLOSE is set, then the reference count on the file
   1047 	 * is 0, but there were multiple users of the file.  This can
   1048 	 * happen if a filedesc structure is shared by multiple
   1049 	 * processes.
   1050 	 */
   1051 	if (fp->f_iflags & FIF_WANTCLOSE) {
   1052 		/*
   1053 		 * Another user of the file is already closing, and is
   1054 		 * simply waiting for other users of the file to drain.
   1055 		 * Release our usecount, and wake up the closer if it
   1056 		 * is the only remaining use.
   1057 		 */
   1058 #ifdef DIAGNOSTIC
   1059 		if (fp->f_count != 0)
   1060 			panic("closef: wantclose and count != 0");
   1061 		if (fp->f_usecount < 2)
   1062 			panic("closef: wantclose and usecount < 2");
   1063 #endif
   1064 		if (--fp->f_usecount == 1)
   1065 			wakeup(&fp->f_usecount);
   1066 		return (0);
   1067 	} else {
   1068 		/*
   1069 		 * Decrement the reference count.  If we were not the
   1070 		 * last reference, then release our use and just
   1071 		 * return.
   1072 		 */
   1073 		if (--fp->f_count > 0) {
   1074 #ifdef DIAGNOSTIC
   1075 			if (fp->f_usecount < 1)
   1076 				panic("closef: no wantclose and usecount < 1");
   1077 #endif
   1078 			fp->f_usecount--;
   1079 			return (0);
   1080 		}
   1081 		if (fp->f_count < 0)
   1082 			panic("closef: count < 0");
   1083 	}
   1084 
   1085 	/*
   1086 	 * The reference count is now 0.  However, there may be
   1087 	 * multiple potential users of this file.  This can happen
   1088 	 * if multiple processes shared a single filedesc structure.
   1089 	 *
   1090 	 * Notify these potential users that the file is closing.
   1091 	 * This will prevent them from adding additional uses to
   1092 	 * the file.
   1093 	 */
   1094 	fp->f_iflags |= FIF_WANTCLOSE;
   1095 
   1096 	/*
   1097 	 * We expect the caller to add a use to the file.  So, if we
   1098 	 * are the last user, usecount will be 1.  If it is not, we
   1099 	 * must wait for the usecount to drain.  When it drains back
   1100 	 * to 1, we will be awakened so that we may proceed with the
   1101 	 * close.
   1102 	 */
   1103 #ifdef DIAGNOSTIC
   1104 	if (fp->f_usecount < 1)
   1105 		panic("closef: usecount < 1");
   1106 #endif
   1107 	while (fp->f_usecount > 1)
   1108 		(void) tsleep(&fp->f_usecount, PRIBIO, "closef", 0);
   1109 #ifdef DIAGNOSTIC
   1110 	if (fp->f_usecount != 1)
   1111 		panic("closef: usecount != 1");
   1112 #endif
   1113 
   1114 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
   1115 		lf.l_whence = SEEK_SET;
   1116 		lf.l_start = 0;
   1117 		lf.l_len = 0;
   1118 		lf.l_type = F_UNLCK;
   1119 		vp = (struct vnode *)fp->f_data;
   1120 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
   1121 	}
   1122 	if (fp->f_ops)
   1123 		error = (*fp->f_ops->fo_close)(fp, p);
   1124 	else
   1125 		error = 0;
   1126 
   1127 	/* Nothing references the file now, drop the final use (us). */
   1128 	fp->f_usecount--;
   1129 
   1130 	ffree(fp);
   1131 	return (error);
   1132 }
   1133 
   1134 /*
   1135  * Apply an advisory lock on a file descriptor.
   1136  *
   1137  * Just attempt to get a record lock of the requested type on
   1138  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
   1139  */
   1140 /* ARGSUSED */
   1141 int
   1142 sys_flock(struct lwp *l, void *v, register_t *retval)
   1143 {
   1144 	struct sys_flock_args /* {
   1145 		syscallarg(int)	fd;
   1146 		syscallarg(int)	how;
   1147 	} */ *uap = v;
   1148 	int		fd, how, error;
   1149 	struct proc	*p;
   1150 	struct filedesc	*fdp;
   1151 	struct file	*fp;
   1152 	struct vnode	*vp;
   1153 	struct flock	lf;
   1154 
   1155 	p = l->l_proc;
   1156 	fd = SCARG(uap, fd);
   1157 	how = SCARG(uap, how);
   1158 	fdp = p->p_fd;
   1159 	error = 0;
   1160 
   1161 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1162 		return (EBADF);
   1163 
   1164 	FILE_USE(fp);
   1165 
   1166 	if (fp->f_type != DTYPE_VNODE) {
   1167 		error = EOPNOTSUPP;
   1168 		goto out;
   1169 	}
   1170 
   1171 	vp = (struct vnode *)fp->f_data;
   1172 	lf.l_whence = SEEK_SET;
   1173 	lf.l_start = 0;
   1174 	lf.l_len = 0;
   1175 	if (how & LOCK_UN) {
   1176 		lf.l_type = F_UNLCK;
   1177 		fp->f_flag &= ~FHASLOCK;
   1178 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
   1179 		goto out;
   1180 	}
   1181 	if (how & LOCK_EX)
   1182 		lf.l_type = F_WRLCK;
   1183 	else if (how & LOCK_SH)
   1184 		lf.l_type = F_RDLCK;
   1185 	else {
   1186 		error = EINVAL;
   1187 		goto out;
   1188 	}
   1189 	fp->f_flag |= FHASLOCK;
   1190 	if (how & LOCK_NB)
   1191 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
   1192 	else
   1193 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
   1194 		    F_FLOCK|F_WAIT);
   1195  out:
   1196 	FILE_UNUSE(fp, p);
   1197 	return (error);
   1198 }
   1199 
   1200 /*
   1201  * File Descriptor pseudo-device driver (/dev/fd/).
   1202  *
   1203  * Opening minor device N dup()s the file (if any) connected to file
   1204  * descriptor N belonging to the calling process.  Note that this driver
   1205  * consists of only the ``open()'' routine, because all subsequent
   1206  * references to this file will be direct to the other driver.
   1207  */
   1208 /* ARGSUSED */
   1209 int
   1210 filedescopen(dev_t dev, int mode, int type, struct proc *p)
   1211 {
   1212 
   1213 	/*
   1214 	 * XXX Kludge: set p->p_dupfd to contain the value of the
   1215 	 * the file descriptor being sought for duplication. The error
   1216 	 * return ensures that the vnode for this device will be released
   1217 	 * by vn_open. Open will detect this special error and take the
   1218 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
   1219 	 * will simply report the error.
   1220 	 */
   1221 	p->p_dupfd = minor(dev);
   1222 	return (ENODEV);
   1223 }
   1224 
   1225 /*
   1226  * Duplicate the specified descriptor to a free descriptor.
   1227  */
   1228 int
   1229 dupfdopen(struct proc *p, int indx, int dfd, int mode, int error)
   1230 {
   1231 	struct filedesc	*fdp;
   1232 	struct file	*wfp, *fp;
   1233 
   1234 	fdp = p->p_fd;
   1235 	/*
   1236 	 * If the to-be-dup'd fd number is greater than the allowed number
   1237 	 * of file descriptors, or the fd to be dup'd has already been
   1238 	 * closed, reject.  Note, check for new == old is necessary as
   1239 	 * falloc could allocate an already closed to-be-dup'd descriptor
   1240 	 * as the new descriptor.
   1241 	 */
   1242 	fp = fdp->fd_ofiles[indx];
   1243 
   1244 	if ((wfp = fd_getfile(fdp, dfd)) == NULL)
   1245 		return (EBADF);
   1246 
   1247 	if (fp == wfp)
   1248 		return (EBADF);
   1249 
   1250 	FILE_USE(wfp);
   1251 
   1252 	/*
   1253 	 * There are two cases of interest here.
   1254 	 *
   1255 	 * For ENODEV simply dup (dfd) to file descriptor
   1256 	 * (indx) and return.
   1257 	 *
   1258 	 * For ENXIO steal away the file structure from (dfd) and
   1259 	 * store it in (indx).  (dfd) is effectively closed by
   1260 	 * this operation.
   1261 	 *
   1262 	 * Any other error code is just returned.
   1263 	 */
   1264 	switch (error) {
   1265 	case ENODEV:
   1266 		/*
   1267 		 * Check that the mode the file is being opened for is a
   1268 		 * subset of the mode of the existing descriptor.
   1269 		 */
   1270 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
   1271 			FILE_UNUSE(wfp, p);
   1272 			return (EACCES);
   1273 		}
   1274 		fdp->fd_ofiles[indx] = wfp;
   1275 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1276 		wfp->f_count++;
   1277 		fd_used(fdp, indx);
   1278 		FILE_UNUSE(wfp, p);
   1279 		return (0);
   1280 
   1281 	case ENXIO:
   1282 		/*
   1283 		 * Steal away the file pointer from dfd, and stuff it into indx.
   1284 		 */
   1285 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
   1286 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1287 		fdp->fd_ofiles[dfd] = NULL;
   1288 		fdp->fd_ofileflags[dfd] = 0;
   1289 		/*
   1290 		 * Complete the clean up of the filedesc structure by
   1291 		 * recomputing the various hints.
   1292 		 */
   1293 		fd_used(fdp, indx);
   1294 		fd_unused(fdp, dfd);
   1295 		FILE_UNUSE(wfp, p);
   1296 		return (0);
   1297 
   1298 	default:
   1299 		FILE_UNUSE(wfp, p);
   1300 		return (error);
   1301 	}
   1302 	/* NOTREACHED */
   1303 }
   1304 
   1305 /*
   1306  * fcntl call which is being passed to the file's fs.
   1307  */
   1308 int
   1309 fcntl_forfs(int fd, struct proc *p, int cmd, void *arg)
   1310 {
   1311 	struct file	*fp;
   1312 	struct filedesc	*fdp;
   1313 	int		error;
   1314 	u_int		size;
   1315 	caddr_t		data, memp;
   1316 #define STK_PARAMS	128
   1317 	char		stkbuf[STK_PARAMS];
   1318 
   1319 	/* fd's value was validated in sys_fcntl before calling this routine */
   1320 	fdp = p->p_fd;
   1321 	fp = fdp->fd_ofiles[fd];
   1322 
   1323 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
   1324 		return (EBADF);
   1325 
   1326 	/*
   1327 	 * Interpret high order word to find amount of data to be
   1328 	 * copied to/from the user's address space.
   1329 	 */
   1330 	size = (size_t)F_PARAM_LEN(cmd);
   1331 	if (size > F_PARAM_MAX)
   1332 		return (EINVAL);
   1333 	memp = NULL;
   1334 	if (size > sizeof(stkbuf)) {
   1335 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
   1336 		data = memp;
   1337 	} else
   1338 		data = stkbuf;
   1339 	if (cmd & F_FSIN) {
   1340 		if (size) {
   1341 			error = copyin(arg, data, size);
   1342 			if (error) {
   1343 				if (memp)
   1344 					free(memp, M_IOCTLOPS);
   1345 				return (error);
   1346 			}
   1347 		} else
   1348 			*(caddr_t *)data = arg;
   1349 	} else if ((cmd & F_FSOUT) && size)
   1350 		/*
   1351 		 * Zero the buffer so the user always
   1352 		 * gets back something deterministic.
   1353 		 */
   1354 		memset(data, 0, size);
   1355 	else if (cmd & F_FSVOID)
   1356 		*(caddr_t *)data = arg;
   1357 
   1358 
   1359 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, p);
   1360 
   1361 	/*
   1362 	 * Copy any data to user, size was
   1363 	 * already set and checked above.
   1364 	 */
   1365 	if (error == 0 && (cmd & F_FSOUT) && size)
   1366 		error = copyout(data, arg, size);
   1367 	if (memp)
   1368 		free(memp, M_IOCTLOPS);
   1369 	return (error);
   1370 }
   1371 
   1372 /*
   1373  * Close any files on exec?
   1374  */
   1375 void
   1376 fdcloseexec(struct proc *p)
   1377 {
   1378 	struct filedesc	*fdp;
   1379 	int		fd;
   1380 
   1381 	fdunshare(p);
   1382 	cwdunshare(p);
   1383 
   1384 	fdp = p->p_fd;
   1385 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
   1386 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
   1387 			(void) fdrelease(p, fd);
   1388 }
   1389