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