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