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