Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.21
      1 /*
      2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  * (c) UNIX System Laboratories, Inc.
      5  * All or some portions of this file are derived from material licensed
      6  * to the University of California by American Telephone and Telegraph
      7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  * the permission of UNIX System Laboratories, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	from: @(#)kern_descrip.c	8.6 (Berkeley) 4/19/94
     39  *	$Id: kern_descrip.c,v 1.21 1994/06/22 03:00:21 mycroft Exp $
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/vnode.h>
     47 #include <sys/proc.h>
     48 #include <sys/file.h>
     49 #include <sys/socket.h>
     50 #include <sys/socketvar.h>
     51 #include <sys/stat.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/fcntl.h>
     54 #include <sys/malloc.h>
     55 #include <sys/syslog.h>
     56 #include <sys/unistd.h>
     57 #include <sys/resourcevar.h>
     58 
     59 /*
     60  * Descriptor management.
     61  */
     62 struct file *filehead;	/* head of list of open files */
     63 int nfiles;		/* actual number of open files */
     64 
     65 /*
     66  * System calls on descriptors.
     67  */
     68 
     69 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_ULTRIX) || defined(COMPAT_HPUX)
     70 /* ARGSUSED */
     71 ogetdtablesize(p, uap, retval)
     72 	struct proc *p;
     73 	void *uap;
     74 	int *retval;
     75 {
     76 
     77 	*retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
     78 	return (0);
     79 }
     80 #endif
     81 
     82 /*
     83  * Duplicate a file descriptor.
     84  */
     85 struct dup_args {
     86 	u_int	fd;
     87 };
     88 /* ARGSUSED */
     89 dup(p, uap, retval)
     90 	struct proc *p;
     91 	struct dup_args *uap;
     92 	int *retval;
     93 {
     94 	register struct filedesc *fdp;
     95 	u_int old;
     96 	int new, error;
     97 
     98 	old = uap->fd;
     99 	/*
    100 	 * XXX Compatibility
    101 	 */
    102 	if (old &~ 077) { uap->fd &= 077; return (dup2(p, uap, retval)); }
    103 
    104 	fdp = p->p_fd;
    105 	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
    106 		return (EBADF);
    107 	if (error = fdalloc(p, 0, &new))
    108 		return (error);
    109 	return (finishdup(fdp, (int)old, new, retval));
    110 }
    111 
    112 /*
    113  * Duplicate a file descriptor to a particular value.
    114  */
    115 struct dup2_args {
    116 	u_int	from;
    117 	u_int	to;
    118 };
    119 /* ARGSUSED */
    120 dup2(p, uap, retval)
    121 	struct proc *p;
    122 	struct dup2_args *uap;
    123 	int *retval;
    124 {
    125 	register struct filedesc *fdp = p->p_fd;
    126 	register u_int old = uap->from, new = uap->to;
    127 	int i, error;
    128 
    129 	if (old >= fdp->fd_nfiles ||
    130 	    fdp->fd_ofiles[old] == NULL ||
    131 	    new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    132 	    new >= maxfiles)
    133 		return (EBADF);
    134 	if (old == new) {
    135 		*retval = new;
    136 		return (0);
    137 	}
    138 	if (new >= fdp->fd_nfiles) {
    139 		if (error = fdalloc(p, new, &i))
    140 			return (error);
    141 		if (new != i)
    142 			panic("dup2: fdalloc");
    143 	} else if (fdp->fd_ofiles[new]) {
    144 		if (fdp->fd_ofileflags[new] & UF_MAPPED)
    145 			(void) munmapfd(p, new);
    146 		/*
    147 		 * dup2() must succeed even if the close has an error.
    148 		 */
    149 		(void) closef(fdp->fd_ofiles[new], p);
    150 	}
    151 	return (finishdup(fdp, (int)old, (int)new, retval));
    152 }
    153 
    154 /*
    155  * The file control system call.
    156  */
    157 struct fcntl_args {
    158 	int	fd;
    159 	int	cmd;
    160 	int	arg;
    161 };
    162 /* ARGSUSED */
    163 fcntl(p, uap, retval)
    164 	struct proc *p;
    165 	register struct fcntl_args *uap;
    166 	int *retval;
    167 {
    168 	register struct filedesc *fdp = p->p_fd;
    169 	register struct file *fp;
    170 	register char *pop;
    171 	struct vnode *vp;
    172 	int i, tmp, error, flg = F_POSIX;
    173 	struct flock fl;
    174 	u_int newmin;
    175 
    176 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    177 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    178 		return (EBADF);
    179 	pop = &fdp->fd_ofileflags[uap->fd];
    180 	switch (uap->cmd) {
    181 
    182 	case F_DUPFD:
    183 		newmin = uap->arg;
    184 		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    185 		    newmin >= maxfiles)
    186 			return (EINVAL);
    187 		if (error = fdalloc(p, newmin, &i))
    188 			return (error);
    189 		return (finishdup(fdp, uap->fd, i, retval));
    190 
    191 	case F_GETFD:
    192 		*retval = *pop & 1;
    193 		return (0);
    194 
    195 	case F_SETFD:
    196 		*pop = (*pop &~ 1) | (uap->arg & 1);
    197 		return (0);
    198 
    199 	case F_GETFL:
    200 		*retval = OFLAGS(fp->f_flag);
    201 		return (0);
    202 
    203 	case F_SETFL:
    204 		fp->f_flag &= ~FCNTLFLAGS;
    205 		fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
    206 		tmp = fp->f_flag & FNONBLOCK;
    207 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    208 		if (error)
    209 			return (error);
    210 		tmp = fp->f_flag & FASYNC;
    211 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    212 		if (!error)
    213 			return (0);
    214 		fp->f_flag &= ~FNONBLOCK;
    215 		tmp = 0;
    216 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    217 		return (error);
    218 
    219 	case F_GETOWN:
    220 		if (fp->f_type == DTYPE_SOCKET) {
    221 			*retval = ((struct socket *)fp->f_data)->so_pgid;
    222 			return (0);
    223 		}
    224 		error = (*fp->f_ops->fo_ioctl)
    225 			(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
    226 		*retval = -*retval;
    227 		return (error);
    228 
    229 	case F_SETOWN:
    230 		if (fp->f_type == DTYPE_SOCKET) {
    231 			((struct socket *)fp->f_data)->so_pgid = uap->arg;
    232 			return (0);
    233 		}
    234 		if (uap->arg <= 0) {
    235 			uap->arg = -uap->arg;
    236 		} else {
    237 			struct proc *p1 = pfind(uap->arg);
    238 			if (p1 == 0)
    239 				return (ESRCH);
    240 			uap->arg = p1->p_pgrp->pg_id;
    241 		}
    242 		return ((*fp->f_ops->fo_ioctl)
    243 			(fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
    244 
    245 	case F_SETLKW:
    246 		flg |= F_WAIT;
    247 		/* Fall into F_SETLK */
    248 
    249 	case F_SETLK:
    250 		if (fp->f_type != DTYPE_VNODE)
    251 			return (EBADF);
    252 		vp = (struct vnode *)fp->f_data;
    253 		/* Copy in the lock structure */
    254 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
    255 		if (error)
    256 			return (error);
    257 		if (fl.l_whence == SEEK_CUR)
    258 			fl.l_start += fp->f_offset;
    259 		switch (fl.l_type) {
    260 
    261 		case F_RDLCK:
    262 			if ((fp->f_flag & FREAD) == 0)
    263 				return (EBADF);
    264 			p->p_flag |= P_ADVLOCK;
    265 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    266 
    267 		case F_WRLCK:
    268 			if ((fp->f_flag & FWRITE) == 0)
    269 				return (EBADF);
    270 			p->p_flag |= P_ADVLOCK;
    271 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    272 
    273 		case F_UNLCK:
    274 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
    275 				F_POSIX));
    276 
    277 		default:
    278 			return (EINVAL);
    279 		}
    280 
    281 	case F_GETLK:
    282 		if (fp->f_type != DTYPE_VNODE)
    283 			return (EBADF);
    284 		vp = (struct vnode *)fp->f_data;
    285 		/* Copy in the lock structure */
    286 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
    287 		if (error)
    288 			return (error);
    289 		if (fl.l_whence == SEEK_CUR)
    290 			fl.l_start += fp->f_offset;
    291 		if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
    292 			return (error);
    293 		return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl)));
    294 
    295 	default:
    296 		return (EINVAL);
    297 	}
    298 	/* NOTREACHED */
    299 }
    300 
    301 /*
    302  * Common code for dup, dup2, and fcntl(F_DUPFD).
    303  */
    304 int
    305 finishdup(fdp, old, new, retval)
    306 	register struct filedesc *fdp;
    307 	register int old, new, *retval;
    308 {
    309 	register struct file *fp;
    310 
    311 	fp = fdp->fd_ofiles[old];
    312 	fdp->fd_ofiles[new] = fp;
    313 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    314 	fp->f_count++;
    315 	if (new > fdp->fd_lastfile)
    316 		fdp->fd_lastfile = new;
    317 	*retval = new;
    318 	return (0);
    319 }
    320 
    321 /*
    322  * Close a file descriptor.
    323  */
    324 struct close_args {
    325 	int	fd;
    326 };
    327 /* ARGSUSED */
    328 close(p, uap, retval)
    329 	struct proc *p;
    330 	struct close_args *uap;
    331 	int *retval;
    332 {
    333 	register struct filedesc *fdp = p->p_fd;
    334 	register struct file *fp;
    335 	register int fd = uap->fd;
    336 	register u_char *pf;
    337 
    338 	if ((unsigned)fd >= fdp->fd_nfiles ||
    339 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    340 		return (EBADF);
    341 	pf = (u_char *)&fdp->fd_ofileflags[fd];
    342 	if (*pf & UF_MAPPED)
    343 		(void) munmapfd(p, fd);
    344 	fdp->fd_ofiles[fd] = NULL;
    345 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    346 		fdp->fd_lastfile--;
    347 	if (fd < fdp->fd_freefile)
    348 		fdp->fd_freefile = fd;
    349 	*pf = 0;
    350 	return (closef(fp, p));
    351 }
    352 
    353 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    354 /*
    355  * Return status information about a file descriptor.
    356  */
    357 struct ofstat_args {
    358 	int	fd;
    359 	struct	ostat *sb;
    360 };
    361 /* ARGSUSED */
    362 ofstat(p, uap, retval)
    363 	struct proc *p;
    364 	register struct ofstat_args *uap;
    365 	int *retval;
    366 {
    367 	register struct filedesc *fdp = p->p_fd;
    368 	register struct file *fp;
    369 	struct stat ub;
    370 	struct ostat oub;
    371 	int error;
    372 
    373 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    374 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    375 		return (EBADF);
    376 	switch (fp->f_type) {
    377 
    378 	case DTYPE_VNODE:
    379 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    380 		break;
    381 
    382 	case DTYPE_SOCKET:
    383 		error = soo_stat((struct socket *)fp->f_data, &ub);
    384 		break;
    385 
    386 	default:
    387 		panic("ofstat");
    388 		/*NOTREACHED*/
    389 	}
    390 	cvtstat(&ub, &oub);
    391 	if (error == 0)
    392 		error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
    393 	return (error);
    394 }
    395 #endif /* COMPAT_43 || COMPAT_SUNOS */
    396 
    397 /*
    398  * Return status information about a file descriptor.
    399  */
    400 struct fstat_args {
    401 	int	fd;
    402 	struct	stat *sb;
    403 };
    404 /* ARGSUSED */
    405 fstat(p, uap, retval)
    406 	struct proc *p;
    407 	register struct fstat_args *uap;
    408 	int *retval;
    409 {
    410 	register struct filedesc *fdp = p->p_fd;
    411 	register struct file *fp;
    412 	struct stat ub;
    413 	int error;
    414 
    415 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    416 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    417 		return (EBADF);
    418 	switch (fp->f_type) {
    419 
    420 	case DTYPE_VNODE:
    421 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    422 		break;
    423 
    424 	case DTYPE_SOCKET:
    425 		error = soo_stat((struct socket *)fp->f_data, &ub);
    426 		break;
    427 
    428 	default:
    429 		panic("fstat");
    430 		/*NOTREACHED*/
    431 	}
    432 	if (error == 0)
    433 		error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
    434 	return (error);
    435 }
    436 
    437 /*
    438  * Return pathconf information about a file descriptor.
    439  */
    440 struct fpathconf_args {
    441 	int	fd;
    442 	int	name;
    443 };
    444 /* ARGSUSED */
    445 fpathconf(p, uap, retval)
    446 	struct proc *p;
    447 	register struct fpathconf_args *uap;
    448 	int *retval;
    449 {
    450 	struct filedesc *fdp = p->p_fd;
    451 	struct file *fp;
    452 	struct vnode *vp;
    453 
    454 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    455 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    456 		return (EBADF);
    457 	switch (fp->f_type) {
    458 
    459 	case DTYPE_SOCKET:
    460 		if (uap->name != _PC_PIPE_BUF)
    461 			return (EINVAL);
    462 		*retval = PIPE_BUF;
    463 		return (0);
    464 
    465 	case DTYPE_VNODE:
    466 		vp = (struct vnode *)fp->f_data;
    467 #ifdef notyet
    468 		return (VOP_PATHCONF(vp, uap->name, retval));
    469 #else
    470 		return (ENOSYS);
    471 #endif
    472 
    473 	default:
    474 		panic("fpathconf");
    475 	}
    476 	/*NOTREACHED*/
    477 }
    478 
    479 /*
    480  * Allocate a file descriptor for the process.
    481  */
    482 int fdexpand;
    483 
    484 fdalloc(p, want, result)
    485 	struct proc *p;
    486 	int want;
    487 	int *result;
    488 {
    489 	register struct filedesc *fdp = p->p_fd;
    490 	register int i;
    491 	int lim, last, nfiles;
    492 	struct file **newofile;
    493 	char *newofileflags;
    494 
    495 	/*
    496 	 * Search for a free descriptor starting at the higher
    497 	 * of want or fd_freefile.  If that fails, consider
    498 	 * expanding the ofile array.
    499 	 */
    500 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    501 	for (;;) {
    502 		last = min(fdp->fd_nfiles, lim);
    503 		if ((i = want) < fdp->fd_freefile)
    504 			i = fdp->fd_freefile;
    505 		for (; i < last; i++) {
    506 			if (fdp->fd_ofiles[i] == NULL) {
    507 				fdp->fd_ofileflags[i] = 0;
    508 				if (i > fdp->fd_lastfile)
    509 					fdp->fd_lastfile = 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 		bcopy(fdp->fd_ofiles, newofile,
    534 			(i = sizeof(struct file *) * fdp->fd_nfiles));
    535 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
    536 		bcopy(fdp->fd_ofileflags, newofileflags,
    537 			(i = sizeof(char) * fdp->fd_nfiles));
    538 		bzero(newofileflags + i, 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 fdavail(p, n)
    553 	struct proc *p;
    554 	register int n;
    555 {
    556 	register struct filedesc *fdp = p->p_fd;
    557 	register struct file **fpp;
    558 	register int i, lim;
    559 
    560 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    561 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
    562 		return (1);
    563 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
    564 	for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
    565 		if (*fpp == NULL && --n <= 0)
    566 			return (1);
    567 	return (0);
    568 }
    569 
    570 /*
    571  * Create a new open file structure and allocate
    572  * a file decriptor for the process that refers to it.
    573  */
    574 falloc(p, resultfp, resultfd)
    575 	register struct proc *p;
    576 	struct file **resultfp;
    577 	int *resultfd;
    578 {
    579 	register struct file *fp, *fq, **fpp;
    580 	int error, i;
    581 
    582 	if (error = fdalloc(p, 0, &i))
    583 		return (error);
    584 	if (nfiles >= maxfiles) {
    585 		tablefull("file");
    586 		return (ENFILE);
    587 	}
    588 	/*
    589 	 * Allocate a new file descriptor.
    590 	 * If the process has file descriptor zero open, add to the list
    591 	 * of open files at that point, otherwise put it at the front of
    592 	 * the list of open files.
    593 	 */
    594 	nfiles++;
    595 	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
    596 	bzero(fp, sizeof(struct file));
    597 	if (fq = p->p_fd->fd_ofiles[0])
    598 		fpp = &fq->f_filef;
    599 	else
    600 		fpp = &filehead;
    601 	p->p_fd->fd_ofiles[i] = fp;
    602 	if (fq = *fpp)
    603 		fq->f_fileb = &fp->f_filef;
    604 	fp->f_filef = fq;
    605 	fp->f_fileb = fpp;
    606 	*fpp = fp;
    607 	fp->f_count = 1;
    608 	fp->f_cred = p->p_ucred;
    609 	crhold(fp->f_cred);
    610 	if (resultfp)
    611 		*resultfp = fp;
    612 	if (resultfd)
    613 		*resultfd = i;
    614 	return (0);
    615 }
    616 
    617 /*
    618  * Free a file descriptor.
    619  */
    620 ffree(fp)
    621 	register struct file *fp;
    622 {
    623 	register struct file *fq;
    624 
    625 	if (fq = fp->f_filef)
    626 		fq->f_fileb = fp->f_fileb;
    627 	*fp->f_fileb = fq;
    628 	crfree(fp->f_cred);
    629 #ifdef DIAGNOSTIC
    630 	fp->f_filef = NULL;
    631 	fp->f_fileb = NULL;
    632 	fp->f_count = 0;
    633 #endif
    634 	nfiles--;
    635 	FREE(fp, M_FILE);
    636 }
    637 
    638 /*
    639  * Copy a filedesc structure.
    640  */
    641 struct filedesc *
    642 fdcopy(p)
    643 	struct proc *p;
    644 {
    645 	register struct filedesc *newfdp, *fdp = p->p_fd;
    646 	register struct file **fpp;
    647 	register int i;
    648 
    649 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
    650 	    M_FILEDESC, M_WAITOK);
    651 	bcopy(fdp, newfdp, sizeof(struct filedesc));
    652 	VREF(newfdp->fd_cdir);
    653 	if (newfdp->fd_rdir)
    654 		VREF(newfdp->fd_rdir);
    655 	newfdp->fd_refcnt = 1;
    656 
    657 	/*
    658 	 * If the number of open files fits in the internal arrays
    659 	 * of the open file structure, use them, otherwise allocate
    660 	 * additional memory for the number of descriptors currently
    661 	 * in use.
    662 	 */
    663 	if (newfdp->fd_lastfile < NDFILE) {
    664 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
    665 		newfdp->fd_ofileflags =
    666 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
    667 		i = NDFILE;
    668 	} else {
    669 		/*
    670 		 * Compute the smallest multiple of NDEXTENT needed
    671 		 * for the file descriptors currently in use,
    672 		 * allowing the table to shrink.
    673 		 */
    674 		i = newfdp->fd_nfiles;
    675 		while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
    676 			i /= 2;
    677 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
    678 		    M_FILEDESC, M_WAITOK);
    679 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
    680 	}
    681 	newfdp->fd_nfiles = i;
    682 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
    683 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
    684 	fpp = newfdp->fd_ofiles;
    685 	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
    686 		if (*fpp != NULL)
    687 			(*fpp)->f_count++;
    688 	return (newfdp);
    689 }
    690 
    691 /*
    692  * Release a filedesc structure.
    693  */
    694 void
    695 fdfree(p)
    696 	struct proc *p;
    697 {
    698 	register struct filedesc *fdp = p->p_fd;
    699 	struct file **fpp;
    700 	register int i;
    701 
    702 	if (--fdp->fd_refcnt > 0)
    703 		return;
    704 	fpp = fdp->fd_ofiles;
    705 	for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
    706 		if (*fpp)
    707 			(void) closef(*fpp, p);
    708 	if (fdp->fd_nfiles > NDFILE)
    709 		FREE(fdp->fd_ofiles, M_FILEDESC);
    710 	vrele(fdp->fd_cdir);
    711 	if (fdp->fd_rdir)
    712 		vrele(fdp->fd_rdir);
    713 	FREE(fdp, M_FILEDESC);
    714 }
    715 
    716 /*
    717  * Close any files on exec?
    718  */
    719 void
    720 fdcloseexec(p)
    721 	struct proc *p;
    722 {
    723 	struct filedesc *fdp = p->p_fd;
    724 	struct file **fpp;
    725 	char *fdfp;
    726 	register int i;
    727 
    728 	fpp = fdp->fd_ofiles;
    729 	fdfp = fdp->fd_ofileflags;
    730 	for (i = 0; i <= fdp->fd_lastfile; i++, fpp++, fdfp++)
    731 		if (*fpp != NULL && (*fdfp & UF_EXCLOSE)) {
    732 			if (*fdfp & UF_MAPPED)
    733 				(void) munmapfd(p, i);
    734 			(void) closef(*fpp, p);
    735 			*fpp = NULL;
    736 			*fdfp = 0;
    737 			if (i < fdp->fd_freefile)
    738 				fdp->fd_freefile = i;
    739 		}
    740 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    741 		fdp->fd_lastfile--;
    742 }
    743 
    744 /*
    745  * Internal form of close.
    746  * Decrement reference count on file structure.
    747  * Note: p may be NULL when closing a file
    748  * that was being passed in a message.
    749  */
    750 closef(fp, p)
    751 	register struct file *fp;
    752 	register struct proc *p;
    753 {
    754 	struct vnode *vp;
    755 	struct flock lf;
    756 	int error;
    757 
    758 	if (fp == NULL)
    759 		return (0);
    760 	/*
    761 	 * POSIX record locking dictates that any close releases ALL
    762 	 * locks owned by this process.  This is handled by setting
    763 	 * a flag in the unlock to free ONLY locks obeying POSIX
    764 	 * semantics, and not to free BSD-style file locks.
    765 	 * If the descriptor was in a message, POSIX-style locks
    766 	 * aren't passed with the descriptor.
    767 	 */
    768 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
    769 		lf.l_whence = SEEK_SET;
    770 		lf.l_start = 0;
    771 		lf.l_len = 0;
    772 		lf.l_type = F_UNLCK;
    773 		vp = (struct vnode *)fp->f_data;
    774 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
    775 	}
    776 	if (--fp->f_count > 0)
    777 		return (0);
    778 	if (fp->f_count < 0)
    779 		panic("closef: count < 0");
    780 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
    781 		lf.l_whence = SEEK_SET;
    782 		lf.l_start = 0;
    783 		lf.l_len = 0;
    784 		lf.l_type = F_UNLCK;
    785 		vp = (struct vnode *)fp->f_data;
    786 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
    787 	}
    788 	if (fp->f_ops)
    789 		error = (*fp->f_ops->fo_close)(fp, p);
    790 	else
    791 		error = 0;
    792 	ffree(fp);
    793 	return (error);
    794 }
    795 
    796 /*
    797  * Apply an advisory lock on a file descriptor.
    798  *
    799  * Just attempt to get a record lock of the requested type on
    800  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
    801  */
    802 struct flock_args {
    803 	int	fd;
    804 	int	how;
    805 };
    806 /* ARGSUSED */
    807 flock(p, uap, retval)
    808 	struct proc *p;
    809 	register struct flock_args *uap;
    810 	int *retval;
    811 {
    812 	register struct filedesc *fdp = p->p_fd;
    813 	register struct file *fp;
    814 	struct vnode *vp;
    815 	struct flock lf;
    816 
    817 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    818 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    819 		return (EBADF);
    820 	if (fp->f_type != DTYPE_VNODE)
    821 		return (EOPNOTSUPP);
    822 	vp = (struct vnode *)fp->f_data;
    823 	lf.l_whence = SEEK_SET;
    824 	lf.l_start = 0;
    825 	lf.l_len = 0;
    826 	if (uap->how & LOCK_UN) {
    827 		lf.l_type = F_UNLCK;
    828 		fp->f_flag &= ~FHASLOCK;
    829 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
    830 	}
    831 	if (uap->how & LOCK_EX)
    832 		lf.l_type = F_WRLCK;
    833 	else if (uap->how & LOCK_SH)
    834 		lf.l_type = F_RDLCK;
    835 	else
    836 		return (EBADF);
    837 	fp->f_flag |= FHASLOCK;
    838 	if (uap->how & LOCK_NB)
    839 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
    840 	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
    841 }
    842 
    843 /*
    844  * File Descriptor pseudo-device driver (/dev/fd/).
    845  *
    846  * Opening minor device N dup()s the file (if any) connected to file
    847  * descriptor N belonging to the calling process.  Note that this driver
    848  * consists of only the ``open()'' routine, because all subsequent
    849  * references to this file will be direct to the other driver.
    850  */
    851 /* ARGSUSED */
    852 fdopen(dev, mode, type, p)
    853 	dev_t dev;
    854 	int mode, type;
    855 	struct proc *p;
    856 {
    857 
    858 	/*
    859 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
    860 	 * the file descriptor being sought for duplication. The error
    861 	 * return ensures that the vnode for this device will be released
    862 	 * by vn_open. Open will detect this special error and take the
    863 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
    864 	 * will simply report the error.
    865 	 */
    866 	p->p_dupfd = minor(dev);
    867 	return (ENODEV);
    868 }
    869 
    870 /*
    871  * Duplicate the specified descriptor to a free descriptor.
    872  */
    873 dupfdopen(fdp, indx, dfd, mode, error)
    874 	register struct filedesc *fdp;
    875 	register int indx, dfd;
    876 	int mode;
    877 	int error;
    878 {
    879 	register struct file *wfp;
    880 	struct file *fp;
    881 
    882 	/*
    883 	 * If the to-be-dup'd fd number is greater than the allowed number
    884 	 * of file descriptors, or the fd to be dup'd has already been
    885 	 * closed, reject.  Note, check for new == old is necessary as
    886 	 * falloc could allocate an already closed to-be-dup'd descriptor
    887 	 * as the new descriptor.
    888 	 */
    889 	fp = fdp->fd_ofiles[indx];
    890 	if ((u_int)dfd >= fdp->fd_nfiles ||
    891 	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
    892 		return (EBADF);
    893 
    894 	/*
    895 	 * There are two cases of interest here.
    896 	 *
    897 	 * For ENODEV simply dup (dfd) to file descriptor
    898 	 * (indx) and return.
    899 	 *
    900 	 * For ENXIO steal away the file structure from (dfd) and
    901 	 * store it in (indx).  (dfd) is effectively closed by
    902 	 * this operation.
    903 	 *
    904 	 * Any other error code is just returned.
    905 	 */
    906 	switch (error) {
    907 	case ENODEV:
    908 		/*
    909 		 * Check that the mode the file is being opened for is a
    910 		 * subset of the mode of the existing descriptor.
    911 		 */
    912 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
    913 			return (EACCES);
    914 		fdp->fd_ofiles[indx] = wfp;
    915 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    916 		wfp->f_count++;
    917 		if (indx > fdp->fd_lastfile)
    918 			fdp->fd_lastfile = indx;
    919 		return (0);
    920 
    921 	case ENXIO:
    922 		/*
    923 		 * Steal away the file pointer from dfd, and stuff it into indx.
    924 		 */
    925 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
    926 		fdp->fd_ofiles[dfd] = NULL;
    927 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    928 		fdp->fd_ofileflags[dfd] = 0;
    929 		/*
    930 		 * Complete the clean up of the filedesc structure by
    931 		 * recomputing the various hints.
    932 		 */
    933 		if (indx > fdp->fd_lastfile)
    934 			fdp->fd_lastfile = indx;
    935 		else
    936 			while (fdp->fd_lastfile > 0 &&
    937 			       fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    938 				fdp->fd_lastfile--;
    939 			if (dfd < fdp->fd_freefile)
    940 				fdp->fd_freefile = dfd;
    941 		return (0);
    942 
    943 	default:
    944 		return (error);
    945 	}
    946 	/* NOTREACHED */
    947 }
    948