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