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