Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.153.2.4
      1 /*	$NetBSD: kern_descrip.c,v 1.153.2.4 2007/05/13 17:36:33 ad 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. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.153.2.4 2007/05/13 17:36:33 ad Exp $");
     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/namei.h>
     50 #include <sys/socket.h>
     51 #include <sys/socketvar.h>
     52 #include <sys/stat.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/fcntl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/pool.h>
     57 #include <sys/syslog.h>
     58 #include <sys/unistd.h>
     59 #include <sys/resourcevar.h>
     60 #include <sys/conf.h>
     61 #include <sys/event.h>
     62 #include <sys/kauth.h>
     63 
     64 #include <sys/mount.h>
     65 #include <sys/syscallargs.h>
     66 
     67 /*
     68  * Descriptor management.
     69  */
     70 struct filelist	filehead;	/* head of list of open files */
     71 int		nfiles;		/* actual number of open files */
     72 POOL_INIT(file_pool, sizeof(struct file), 0, 0, 0, "filepl",
     73     &pool_allocator_nointr, IPL_NONE);
     74 POOL_INIT(cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
     75     &pool_allocator_nointr, IPL_NONE);
     76 POOL_INIT(filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
     77     &pool_allocator_nointr, IPL_NONE);
     78 
     79 /* Global file list lock */
     80 kmutex_t filelist_lock;
     81 
     82 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
     83 MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
     84 MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
     85 
     86 static inline int
     87 find_next_zero(uint32_t *bitmap, int want, u_int bits)
     88 {
     89 	int i, off, maxoff;
     90 	uint32_t sub;
     91 
     92 	if (want > bits)
     93 		return -1;
     94 
     95 	off = want >> NDENTRYSHIFT;
     96 	i = want & NDENTRYMASK;
     97 	if (i) {
     98 		sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
     99 		if (sub != ~0)
    100 			goto found;
    101 		off++;
    102 	}
    103 
    104 	maxoff = NDLOSLOTS(bits);
    105 	while (off < maxoff) {
    106 		if ((sub = bitmap[off]) != ~0)
    107 			goto found;
    108 		off++;
    109 	}
    110 
    111 	return (-1);
    112 
    113  found:
    114 	return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
    115 }
    116 
    117 static int
    118 find_last_set(struct filedesc *fd, int last)
    119 {
    120 	int off, i;
    121 	struct file **ofiles = fd->fd_ofiles;
    122 	uint32_t *bitmap = fd->fd_lomap;
    123 
    124 	off = (last - 1) >> NDENTRYSHIFT;
    125 
    126 	while (off >= 0 && !bitmap[off])
    127 		off--;
    128 
    129 	if (off < 0)
    130 		return (-1);
    131 
    132 	i = ((off + 1) << NDENTRYSHIFT) - 1;
    133 	if (i >= last)
    134 		i = last - 1;
    135 
    136 	while (i > 0 && ofiles[i] == NULL)
    137 		i--;
    138 
    139 	return (i);
    140 }
    141 
    142 static inline void
    143 fd_used(struct filedesc *fdp, int fd)
    144 {
    145 	u_int off = fd >> NDENTRYSHIFT;
    146 
    147 	KASSERT(rw_write_held(&fdp->fd_lock));
    148 	KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
    149 
    150 	fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
    151 	if (fdp->fd_lomap[off] == ~0) {
    152 		KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    153 		    (1 << (off & NDENTRYMASK))) == 0);
    154 		fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
    155 	}
    156 
    157 	if (fd > fdp->fd_lastfile)
    158 		fdp->fd_lastfile = fd;
    159 }
    160 
    161 static inline void
    162 fd_unused(struct filedesc *fdp, int fd)
    163 {
    164 	u_int off = fd >> NDENTRYSHIFT;
    165 
    166 	KASSERT(rw_write_held(&fdp->fd_lock));
    167 	if (fd < fdp->fd_freefile)
    168 		fdp->fd_freefile = fd;
    169 
    170 	if (fdp->fd_lomap[off] == ~0) {
    171 		KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    172 		    (1 << (off & NDENTRYMASK))) != 0);
    173 		fdp->fd_himap[off >> NDENTRYSHIFT] &=
    174 		    ~(1 << (off & NDENTRYMASK));
    175 	}
    176 	KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
    177 	fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
    178 
    179 #ifdef DIAGNOSTIC
    180 	if (fd > fdp->fd_lastfile)
    181 		panic("fd_unused: fd_lastfile inconsistent");
    182 #endif
    183 	if (fd == fdp->fd_lastfile)
    184 		fdp->fd_lastfile = find_last_set(fdp, fd);
    185 }
    186 
    187 /*
    188  * Lookup the file structure corresponding to a file descriptor
    189  * and return it locked.
    190  * Note: typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'
    191  * The locking strategy has been optimised for this case, i.e.
    192  * fd_getfile() returns the file locked while FILE_USE() will increment
    193  * the file's use count and unlock.
    194  */
    195 struct file *
    196 fd_getfile(struct filedesc *fdp, int fd)
    197 {
    198 	struct file *fp;
    199 
    200 	rw_enter(&fdp->fd_lock, RW_READER);
    201 	if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
    202 		rw_exit(&fdp->fd_lock);
    203 		return (NULL);
    204 	}
    205 
    206 	mutex_enter(&fp->f_lock);
    207 	if (FILE_IS_USABLE(fp) == 0) {
    208 		mutex_exit(&fp->f_lock);
    209 		rw_exit(&fdp->fd_lock);
    210 		return (NULL);
    211 	}
    212 	rw_exit(&fdp->fd_lock);
    213 
    214 	return (fp);
    215 }
    216 
    217 /*
    218  * Common code for dup, dup2, and fcntl(F_DUPFD).
    219  */
    220 static int
    221 finishdup(struct lwp *l, int old, int new, register_t *retval)
    222 {
    223 	struct filedesc	*fdp;
    224 	struct file	*fp, *delfp;
    225 
    226 	fdp = l->l_proc->p_fd;
    227 
    228 	/*
    229 	 * If there is a file in the new slot, remember it so we
    230 	 * can close it after we've finished the dup.  We need
    231 	 * to do it after the dup is finished, since closing
    232 	 * the file may block.
    233 	 *
    234 	 * Note: `old' is already used for us.
    235 	 * Note: Caller already marked `new' slot "used".
    236 	 */
    237 	rw_enter(&fdp->fd_lock, RW_WRITER);
    238 	delfp = fdp->fd_ofiles[new];
    239 
    240 	fp = fdp->fd_ofiles[old];
    241 	KDASSERT(fp != NULL);
    242 	fdp->fd_ofiles[new] = fp;
    243 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    244 	rw_exit(&fdp->fd_lock);
    245 
    246 	*retval = new;
    247 	mutex_enter(&fp->f_lock);
    248 	fp->f_count++;
    249 	FILE_UNUSE_HAVELOCK(fp, l);
    250 
    251 	if (delfp != NULL) {
    252 		mutex_enter(&delfp->f_lock);
    253 		FILE_USE(delfp);
    254 		if (new < fdp->fd_knlistsize)
    255 			knote_fdclose(l, new);
    256 		(void) closef(delfp, l);
    257 	}
    258 	return (0);
    259 }
    260 
    261 /*
    262  * Initialize the descriptor system.
    263  */
    264 void
    265 filedesc_init(void)
    266 {
    267 
    268 	mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
    269 }
    270 
    271 /*
    272  * System calls on descriptors.
    273  */
    274 
    275 /*
    276  * Duplicate a file descriptor.
    277  */
    278 /* ARGSUSED */
    279 int
    280 sys_dup(struct lwp *l, void *v, register_t *retval)
    281 {
    282 	struct sys_dup_args /* {
    283 		syscallarg(int)	fd;
    284 	} */ *uap = v;
    285 	struct file	*fp;
    286 	struct filedesc	*fdp;
    287 	struct proc	*p;
    288 	int		old, new, error;
    289 
    290 	p = l->l_proc;
    291 	fdp = p->p_fd;
    292 	old = SCARG(uap, fd);
    293 
    294  restart:
    295 	if ((fp = fd_getfile(fdp, old)) == NULL)
    296 		return (EBADF);
    297 
    298 	FILE_USE(fp);
    299 
    300 	if ((error = fdalloc(p, 0, &new)) != 0) {
    301 		if (error == ENOSPC) {
    302 			fdexpand(p);
    303 			FILE_UNUSE(fp, l);
    304 			goto restart;
    305 		}
    306 		FILE_UNUSE(fp, l);
    307 		return (error);
    308 	}
    309 
    310 	/* finishdup() will unuse the descriptors for us */
    311 	return (finishdup(l, old, new, retval));
    312 }
    313 
    314 /*
    315  * Duplicate a file descriptor to a particular value.
    316  */
    317 /* ARGSUSED */
    318 int
    319 sys_dup2(struct lwp *l, void *v, register_t *retval)
    320 {
    321 	struct sys_dup2_args /* {
    322 		syscallarg(int)	from;
    323 		syscallarg(int)	to;
    324 	} */ *uap = v;
    325 	struct file	*fp;
    326 	struct filedesc	*fdp;
    327 	struct proc	*p;
    328 	int		old, new, i, error;
    329 
    330 	p = l->l_proc;
    331 	fdp = p->p_fd;
    332 	old = SCARG(uap, from);
    333 	new = SCARG(uap, to);
    334 
    335  restart:
    336 	if ((fp = fd_getfile(fdp, old)) == NULL)
    337 		return (EBADF);
    338 
    339 	if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    340 	    (u_int)new >= maxfiles) {
    341 		mutex_exit(&fp->f_lock);
    342 		return (EBADF);
    343 	}
    344 
    345 	if (old == new) {
    346 		mutex_exit(&fp->f_lock);
    347 		*retval = new;
    348 		return (0);
    349 	}
    350 
    351 	FILE_USE(fp);
    352 
    353 	if (new >= fdp->fd_nfiles) {
    354 		if ((error = fdalloc(p, new, &i)) != 0) {
    355 			if (error == ENOSPC) {
    356 				fdexpand(p);
    357 				FILE_UNUSE(fp, l);
    358 				goto restart;
    359 			}
    360 			FILE_UNUSE(fp, l);
    361 			return (error);
    362 		}
    363 		if (new != i)
    364 			panic("dup2: fdalloc");
    365 	} else {
    366 		rw_enter(&fdp->fd_lock, RW_WRITER);
    367 		/*
    368 		 * Mark `new' slot "used" only if it was empty.
    369 		 */
    370 		if (fdp->fd_ofiles[new] == NULL)
    371 			fd_used(fdp, new);
    372 		rw_exit(&fdp->fd_lock);
    373 	}
    374 
    375 	/*
    376 	 * finishdup() will close the file that's in the `new'
    377 	 * slot, if there's one there.
    378 	 */
    379 
    380 	/* finishdup() will unuse the descriptors for us */
    381 	return (finishdup(l, old, new, retval));
    382 }
    383 
    384 /*
    385  * fcntl call which is being passed to the file's fs.
    386  */
    387 static int
    388 fcntl_forfs(int fd, struct lwp *l, int cmd, void *arg)
    389 {
    390 	struct file	*fp;
    391 	struct filedesc	*fdp;
    392 	int		error;
    393 	u_int		size;
    394 	void		*data, *memp;
    395 #define STK_PARAMS	128
    396 	char		stkbuf[STK_PARAMS];
    397 
    398 	/* fd's value was validated in sys_fcntl before calling this routine */
    399 	fdp = l->l_proc->p_fd;
    400 	fp = fdp->fd_ofiles[fd];
    401 
    402 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
    403 		return (EBADF);
    404 
    405 	/*
    406 	 * Interpret high order word to find amount of data to be
    407 	 * copied to/from the user's address space.
    408 	 */
    409 	size = (size_t)F_PARAM_LEN(cmd);
    410 	if (size > F_PARAM_MAX)
    411 		return (EINVAL);
    412 	memp = NULL;
    413 	if (size > sizeof(stkbuf)) {
    414 		memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
    415 		data = memp;
    416 	} else
    417 		data = stkbuf;
    418 	if (cmd & F_FSIN) {
    419 		if (size) {
    420 			error = copyin(arg, data, size);
    421 			if (error) {
    422 				if (memp)
    423 					free(memp, M_IOCTLOPS);
    424 				return (error);
    425 			}
    426 		} else
    427 			*(void **)data = arg;
    428 	} else if ((cmd & F_FSOUT) && size)
    429 		/*
    430 		 * Zero the buffer so the user always
    431 		 * gets back something deterministic.
    432 		 */
    433 		memset(data, 0, size);
    434 	else if (cmd & F_FSVOID)
    435 		*(void **)data = arg;
    436 
    437 
    438 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, l);
    439 
    440 	/*
    441 	 * Copy any data to user, size was
    442 	 * already set and checked above.
    443 	 */
    444 	if (error == 0 && (cmd & F_FSOUT) && size)
    445 		error = copyout(data, arg, size);
    446 	if (memp)
    447 		free(memp, M_IOCTLOPS);
    448 	return (error);
    449 }
    450 
    451 /*
    452  * The file control system call.
    453  */
    454 /* ARGSUSED */
    455 int
    456 sys_fcntl(struct lwp *l, void *v, register_t *retval)
    457 {
    458 	struct sys_fcntl_args /* {
    459 		syscallarg(int)		fd;
    460 		syscallarg(int)		cmd;
    461 		syscallarg(void *)	arg;
    462 	} */ *uap = v;
    463 	struct filedesc *fdp;
    464 	struct file	*fp;
    465 	struct proc	*p;
    466 	struct vnode	*vp;
    467 	int		fd, i, tmp, error, flg, cmd, newmin;
    468 	struct flock	fl;
    469 
    470 	p = l->l_proc;
    471 	fd = SCARG(uap, fd);
    472 	cmd = SCARG(uap, cmd);
    473 	fdp = p->p_fd;
    474 	error = 0;
    475 	flg = F_POSIX;
    476 
    477 	switch (cmd) {
    478 	case F_CLOSEM:
    479 		if (fd < 0)
    480 			return EBADF;
    481 		while (fdp->fd_lastfile >= fd)
    482 			fdrelease(l, fdp->fd_lastfile);
    483 		return 0;
    484 
    485 	case F_MAXFD:
    486 		*retval = fdp->fd_lastfile;
    487 		return 0;
    488 
    489 	default:
    490 		/* Handled below */
    491 		break;
    492 	}
    493 
    494  restart:
    495 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    496 		return (EBADF);
    497 
    498 	FILE_USE(fp);
    499 
    500 	if ((cmd & F_FSCTL)) {
    501 		error = fcntl_forfs(fd, l, cmd, SCARG(uap, arg));
    502 		goto out;
    503 	}
    504 
    505 	switch (cmd) {
    506 
    507 	case F_DUPFD:
    508 		newmin = (long)SCARG(uap, arg);
    509 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    510 		    (u_int)newmin >= maxfiles) {
    511 			error = EINVAL;
    512 			goto out;
    513 		}
    514 		if ((error = fdalloc(p, newmin, &i)) != 0) {
    515 			if (error == ENOSPC) {
    516 				fdexpand(p);
    517 				FILE_UNUSE(fp, l);
    518 				goto restart;
    519 			}
    520 			goto out;
    521 		}
    522 
    523 		/* finishdup() will unuse the descriptors for us */
    524 		return (finishdup(l, fd, i, retval));
    525 
    526 	case F_GETFD:
    527 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
    528 		break;
    529 
    530 	case F_SETFD:
    531 		if ((long)SCARG(uap, arg) & 1)
    532 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
    533 		else
    534 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
    535 		break;
    536 
    537 	case F_GETFL:
    538 		*retval = OFLAGS(fp->f_flag);
    539 		break;
    540 
    541 	case F_SETFL:
    542 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    543 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp, l);
    544 		if (error)
    545 			break;
    546 		i = tmp ^ fp->f_flag;
    547 		if (i & FNONBLOCK) {
    548 			int flgs = tmp & FNONBLOCK;
    549 			error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs, l);
    550 			if (error)
    551 				goto reset_fcntl;
    552 		}
    553 		if (i & FASYNC) {
    554 			int flgs = tmp & FASYNC;
    555 			error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs, l);
    556 			if (error) {
    557 				if (i & FNONBLOCK) {
    558 					tmp = fp->f_flag & FNONBLOCK;
    559 					(void)(*fp->f_ops->fo_ioctl)(fp,
    560 						FIONBIO, &tmp, l);
    561 				}
    562 				goto reset_fcntl;
    563 			}
    564 		}
    565 		fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
    566 		break;
    567 	    reset_fcntl:
    568 		(void)(*fp->f_ops->fo_fcntl)(fp, F_SETFL, &fp->f_flag, l);
    569 		break;
    570 
    571 	case F_GETOWN:
    572 		error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp, l);
    573 		*retval = tmp;
    574 		break;
    575 
    576 	case F_SETOWN:
    577 		tmp = (int)(intptr_t) SCARG(uap, arg);
    578 		error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp, l);
    579 		break;
    580 
    581 	case F_SETLKW:
    582 		flg |= F_WAIT;
    583 		/* Fall into F_SETLK */
    584 
    585 	case F_SETLK:
    586 		if (fp->f_type != DTYPE_VNODE) {
    587 			error = EINVAL;
    588 			goto out;
    589 		}
    590 		vp = (struct vnode *)fp->f_data;
    591 		/* Copy in the lock structure */
    592 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
    593 		if (error)
    594 			goto out;
    595 		if (fl.l_whence == SEEK_CUR)
    596 			fl.l_start += fp->f_offset;
    597 		switch (fl.l_type) {
    598 		case F_RDLCK:
    599 			if ((fp->f_flag & FREAD) == 0) {
    600 				error = EBADF;
    601 				goto out;
    602 			}
    603 			if ((p->p_flag & PK_ADVLOCK) == 0) {
    604 				mutex_enter(&p->p_mutex);
    605 				p->p_flag |= PK_ADVLOCK;
    606 				mutex_exit(&p->p_mutex);
    607 			}
    608 			error = VOP_ADVLOCK(vp, p, F_SETLK, &fl, flg);
    609 			goto out;
    610 
    611 		case F_WRLCK:
    612 			if ((fp->f_flag & FWRITE) == 0) {
    613 				error = EBADF;
    614 				goto out;
    615 			}
    616 			if ((p->p_flag & PK_ADVLOCK) == 0) {
    617 				mutex_enter(&p->p_mutex);
    618 				p->p_flag |= PK_ADVLOCK;
    619 				mutex_exit(&p->p_mutex);
    620 			}
    621 			error = VOP_ADVLOCK(vp, p, F_SETLK, &fl, flg);
    622 			goto out;
    623 
    624 		case F_UNLCK:
    625 			error = VOP_ADVLOCK(vp, p, F_UNLCK, &fl, F_POSIX);
    626 			goto out;
    627 
    628 		default:
    629 			error = EINVAL;
    630 			goto out;
    631 		}
    632 
    633 	case F_GETLK:
    634 		if (fp->f_type != DTYPE_VNODE) {
    635 			error = EINVAL;
    636 			goto out;
    637 		}
    638 		vp = (struct vnode *)fp->f_data;
    639 		/* Copy in the lock structure */
    640 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
    641 		if (error)
    642 			goto out;
    643 		if (fl.l_whence == SEEK_CUR)
    644 			fl.l_start += fp->f_offset;
    645 		if (fl.l_type != F_RDLCK &&
    646 		    fl.l_type != F_WRLCK &&
    647 		    fl.l_type != F_UNLCK) {
    648 			error = EINVAL;
    649 			goto out;
    650 		}
    651 		error = VOP_ADVLOCK(vp, p, F_GETLK, &fl, F_POSIX);
    652 		if (error)
    653 			goto out;
    654 		error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
    655 		break;
    656 
    657 	default:
    658 		error = EINVAL;
    659 	}
    660 
    661  out:
    662 	FILE_UNUSE(fp, l);
    663 	return (error);
    664 }
    665 
    666 void
    667 fdremove(struct filedesc *fdp, int fd)
    668 {
    669 
    670 	rw_enter(&fdp->fd_lock, RW_WRITER);
    671 	fdp->fd_ofiles[fd] = NULL;
    672 	fd_unused(fdp, fd);
    673 	rw_exit(&fdp->fd_lock);
    674 }
    675 
    676 int
    677 fdrelease(struct lwp *l, int fd)
    678 {
    679 	struct proc *p = l->l_proc;
    680 	struct filedesc	*fdp;
    681 	struct file	**fpp, *fp;
    682 
    683 	fdp = p->p_fd;
    684 	rw_enter(&fdp->fd_lock, RW_WRITER);
    685 	if (fd < 0 || fd > fdp->fd_lastfile)
    686 		goto badf;
    687 	fpp = &fdp->fd_ofiles[fd];
    688 	fp = *fpp;
    689 	if (fp == NULL)
    690 		goto badf;
    691 
    692 	mutex_enter(&fp->f_lock);
    693 	if (!FILE_IS_USABLE(fp)) {
    694 		mutex_exit(&fp->f_lock);
    695 		goto badf;
    696 	}
    697 
    698 	FILE_USE(fp);
    699 
    700 	*fpp = NULL;
    701 	fdp->fd_ofileflags[fd] = 0;
    702 	fd_unused(fdp, fd);
    703 	rw_exit(&fdp->fd_lock);
    704 	if (fd < fdp->fd_knlistsize)
    705 		knote_fdclose(l, fd);
    706 	return (closef(fp, l));
    707 
    708 badf:
    709 	rw_exit(&fdp->fd_lock);
    710 	return (EBADF);
    711 }
    712 
    713 /*
    714  * Close a file descriptor.
    715  */
    716 /* ARGSUSED */
    717 int
    718 sys_close(struct lwp *l, void *v, register_t *retval)
    719 {
    720 	struct sys_close_args /* {
    721 		syscallarg(int)	fd;
    722 	} */ *uap = v;
    723 	int		fd;
    724 	struct filedesc	*fdp;
    725 	struct proc *p;
    726 
    727 	p = l->l_proc;
    728 	fd = SCARG(uap, fd);
    729 	fdp = p->p_fd;
    730 
    731 #if 0
    732 	if (fd_getfile(fdp, fd) == NULL)
    733 		return (EBADF);
    734 #endif
    735 
    736 	return (fdrelease(l, fd));
    737 }
    738 
    739 /*
    740  * Return status information about a file descriptor.
    741  * Common function for compat code.
    742  */
    743 int
    744 do_sys_fstat(struct lwp *l, int fd, struct stat *sb)
    745 {
    746 	struct file	*fp;
    747 	int		error;
    748 
    749 	fp = fd_getfile(l->l_proc->p_fd, fd);
    750 	if (fp == NULL)
    751 		return EBADF;
    752 
    753 	FILE_USE(fp);
    754 	error = (*fp->f_ops->fo_stat)(fp, sb, l);
    755 	FILE_UNUSE(fp, l);
    756 
    757 	return error;
    758 }
    759 
    760 /*
    761  * Return status information about a file descriptor.
    762  */
    763 /* ARGSUSED */
    764 int
    765 sys___fstat30(struct lwp *l, void *v, register_t *retval)
    766 {
    767 	struct sys___fstat30_args /* {
    768 		syscallarg(int)			fd;
    769 		syscallarg(struct stat *)	sb;
    770 	} */ *uap = v;
    771 	struct stat	sb;
    772 	int		error;
    773 
    774 	error = do_sys_fstat(l, SCARG(uap, fd), &sb);
    775 
    776 	if (error == 0)
    777 		error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
    778 
    779 	return (error);
    780 }
    781 
    782 /*
    783  * Return pathconf information about a file descriptor.
    784  */
    785 /* ARGSUSED */
    786 int
    787 sys_fpathconf(struct lwp *l, void *v, register_t *retval)
    788 {
    789 	struct sys_fpathconf_args /* {
    790 		syscallarg(int)	fd;
    791 		syscallarg(int)	name;
    792 	} */ *uap = v;
    793 	int		fd;
    794 	struct filedesc	*fdp;
    795 	struct file	*fp;
    796 	struct proc 	*p;
    797 	struct vnode	*vp;
    798 	int		error;
    799 
    800 	p = l->l_proc;
    801 	fd = SCARG(uap, fd);
    802 	fdp = p->p_fd;
    803 	error = 0;
    804 
    805 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    806 		return (EBADF);
    807 
    808 	FILE_USE(fp);
    809 
    810 	switch (fp->f_type) {
    811 
    812 	case DTYPE_SOCKET:
    813 	case DTYPE_PIPE:
    814 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    815 			error = EINVAL;
    816 		else
    817 			*retval = PIPE_BUF;
    818 		break;
    819 
    820 	case DTYPE_VNODE:
    821 		vp = (struct vnode *)fp->f_data;
    822 		error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
    823 		break;
    824 
    825 	case DTYPE_KQUEUE:
    826 		error = EINVAL;
    827 		break;
    828 
    829 	default:
    830 		error = EOPNOTSUPP;
    831 		break;
    832 	}
    833 
    834 	FILE_UNUSE(fp, l);
    835 	return (error);
    836 }
    837 
    838 /*
    839  * Allocate a file descriptor for the process.
    840  */
    841 int	fdexpanded;		/* XXX: what else uses this? */
    842 
    843 int
    844 fdalloc(struct proc *p, int want, int *result)
    845 {
    846 	struct filedesc	*fdp;
    847 	int i, lim, last, error;
    848 	u_int off, new;
    849 
    850 	fdp = p->p_fd;
    851 	rw_enter(&fdp->fd_lock, RW_WRITER);
    852 
    853 	/*
    854 	 * Search for a free descriptor starting at the higher
    855 	 * of want or fd_freefile.  If that fails, consider
    856 	 * expanding the ofile array.
    857 	 */
    858 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    859 	last = min(fdp->fd_nfiles, lim);
    860  again:
    861 	if ((i = want) < fdp->fd_freefile)
    862 		i = fdp->fd_freefile;
    863 	off = i >> NDENTRYSHIFT;
    864 	new = find_next_zero(fdp->fd_himap, off,
    865 	    (last + NDENTRIES - 1) >> NDENTRYSHIFT);
    866 	if (new != -1) {
    867 		i = find_next_zero(&fdp->fd_lomap[new],
    868 		    new > off ? 0 : i & NDENTRYMASK, NDENTRIES);
    869 		if (i == -1) {
    870 			/*
    871 			 * free file descriptor in this block was
    872 			 * below want, try again with higher want.
    873 			 */
    874 			want = (new + 1) << NDENTRYSHIFT;
    875 			goto again;
    876 		}
    877 		i += (new << NDENTRYSHIFT);
    878 		if (i < last) {
    879 			if (fdp->fd_ofiles[i] == NULL) {
    880 				fd_used(fdp, i);
    881 				if (want <= fdp->fd_freefile)
    882 					fdp->fd_freefile = i;
    883 				*result = i;
    884 				error = 0;
    885 				goto out;
    886 			}
    887 		}
    888 	}
    889 
    890 	/* No space in current array.  Expand or let the caller do it. */
    891 	error = (fdp->fd_nfiles >= lim) ? EMFILE : ENOSPC;
    892 
    893 out:
    894 	rw_exit(&fdp->fd_lock);
    895 	return (error);
    896 }
    897 
    898 void
    899 fdexpand(struct proc *p)
    900 {
    901 	struct filedesc	*fdp;
    902 	int		i, numfiles, oldnfiles;
    903 	struct file	**newofile;
    904 	char		*newofileflags;
    905 	uint32_t	*newhimap = NULL, *newlomap = NULL;
    906 
    907 	fdp = p->p_fd;
    908 
    909 restart:
    910 	oldnfiles = fdp->fd_nfiles;
    911 
    912 	if (oldnfiles < NDEXTENT)
    913 		numfiles = NDEXTENT;
    914 	else
    915 		numfiles = 2 * oldnfiles;
    916 
    917 	newofile = malloc(numfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
    918 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
    919 		newhimap = malloc(NDHISLOTS(numfiles) * sizeof(uint32_t),
    920 		    M_FILEDESC, M_WAITOK);
    921 		newlomap = malloc(NDLOSLOTS(numfiles) * sizeof(uint32_t),
    922 		    M_FILEDESC, M_WAITOK);
    923 	}
    924 
    925 	rw_enter(&fdp->fd_lock, RW_WRITER);
    926 	/* lock fdp */
    927 	if (fdp->fd_nfiles != oldnfiles) {
    928 		/* fdp changed; retry */
    929 		rw_exit(&fdp->fd_lock);
    930 		free(newofile, M_FILEDESC);
    931 		if (newhimap != NULL) free(newhimap, M_FILEDESC);
    932 		if (newlomap != NULL) free(newlomap, M_FILEDESC);
    933 		goto restart;
    934 	}
    935 
    936 	newofileflags = (char *) &newofile[numfiles];
    937 	/*
    938 	 * Copy the existing ofile and ofileflags arrays
    939 	 * and zero the new portion of each array.
    940 	 */
    941 	memcpy(newofile, fdp->fd_ofiles,
    942 	    (i = sizeof(struct file *) * fdp->fd_nfiles));
    943 	memset((char *)newofile + i, 0,
    944 	    numfiles * sizeof(struct file *) - i);
    945 	memcpy(newofileflags, fdp->fd_ofileflags,
    946 	    (i = sizeof(char) * fdp->fd_nfiles));
    947 	memset(newofileflags + i, 0, numfiles * sizeof(char) - i);
    948 	if (oldnfiles > NDFILE)
    949 		free(fdp->fd_ofiles, M_FILEDESC);
    950 
    951 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
    952 		memcpy(newhimap, fdp->fd_himap,
    953 		    (i = NDHISLOTS(oldnfiles) * sizeof(uint32_t)));
    954 		memset((char *)newhimap + i, 0,
    955 		    NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
    956 
    957 		memcpy(newlomap, fdp->fd_lomap,
    958 		    (i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t)));
    959 		memset((char *)newlomap + i, 0,
    960 		    NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
    961 
    962 		if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
    963 			free(fdp->fd_himap, M_FILEDESC);
    964 			free(fdp->fd_lomap, M_FILEDESC);
    965 		}
    966 		fdp->fd_himap = newhimap;
    967 		fdp->fd_lomap = newlomap;
    968 	}
    969 
    970 	fdp->fd_ofiles = newofile;
    971 	fdp->fd_ofileflags = newofileflags;
    972 	fdp->fd_nfiles = numfiles;
    973 
    974 	rw_exit(&fdp->fd_lock);
    975 
    976 	fdexpanded++;
    977 }
    978 
    979 /*
    980  * Create a new open file structure and allocate
    981  * a file descriptor for the process that refers to it.
    982  */
    983 int
    984 falloc(struct lwp *l, struct file **resultfp, int *resultfd)
    985 {
    986 	struct filedesc	*fdp;
    987 	struct file	*fp, *fq;
    988 	struct proc	*p;
    989 	int		error, i;
    990 
    991 	p = l->l_proc;
    992 	fdp = p->p_fd;
    993 
    994  restart:
    995 	if ((error = fdalloc(p, 0, &i)) != 0) {
    996 		if (error == ENOSPC) {
    997 			fdexpand(p);
    998 			goto restart;
    999 		}
   1000 		return (error);
   1001 	}
   1002 
   1003 	fp = pool_get(&file_pool, PR_WAITOK);
   1004 	memset(fp, 0, sizeof(struct file));
   1005 	mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
   1006 	mutex_enter(&filelist_lock);
   1007 	if (nfiles >= maxfiles) {
   1008 		tablefull("file", "increase kern.maxfiles or MAXFILES");
   1009 		mutex_exit(&filelist_lock);
   1010 		rw_enter(&fdp->fd_lock, RW_WRITER);
   1011 		fd_unused(fdp, i);
   1012 		rw_exit(&fdp->fd_lock);
   1013 		mutex_destroy(&fp->f_lock);
   1014 		pool_put(&file_pool, fp);
   1015 		return (ENFILE);
   1016 	}
   1017 	/*
   1018 	 * Allocate a new file descriptor.
   1019 	 * If the process has file descriptor zero open, add to the list
   1020 	 * of open files at that point, otherwise put it at the front of
   1021 	 * the list of open files.
   1022 	 */
   1023 	nfiles++;
   1024 	fp->f_iflags = FIF_LARVAL;
   1025 	cv_init(&fp->f_cv, "closef");
   1026 	rw_enter(&fdp->fd_lock, RW_WRITER);	/* XXXAD check order */
   1027 	if ((fq = fdp->fd_ofiles[0]) != NULL) {
   1028 		LIST_INSERT_AFTER(fq, fp, f_list);
   1029 	} else {
   1030 		LIST_INSERT_HEAD(&filehead, fp, f_list);
   1031 	}
   1032 	KDASSERT(fdp->fd_ofiles[i] == NULL);
   1033 	fdp->fd_ofiles[i] = fp;
   1034 	fp->f_count = 1;
   1035 	fp->f_cred = l->l_cred;
   1036 	kauth_cred_hold(fp->f_cred);
   1037 	if (resultfp) {
   1038 		fp->f_usecount = 1;
   1039 		*resultfp = fp;
   1040 	}
   1041 	mutex_exit(&filelist_lock);
   1042 	rw_exit(&fdp->fd_lock);
   1043 	if (resultfd)
   1044 		*resultfd = i;
   1045 
   1046 	return (0);
   1047 }
   1048 
   1049 /*
   1050  * Free a file descriptor.
   1051  */
   1052 void
   1053 ffree(struct file *fp)
   1054 {
   1055 	kauth_cred_t cred;
   1056 
   1057 #ifdef DIAGNOSTIC
   1058 	if (fp->f_usecount)
   1059 		panic("ffree");
   1060 #endif
   1061 
   1062 	mutex_enter(&filelist_lock);
   1063 	LIST_REMOVE(fp, f_list);
   1064 	cred = fp->f_cred;
   1065 #ifdef DIAGNOSTIC
   1066 	fp->f_cred = NULL;
   1067 	fp->f_count = 0; /* What's the point? */
   1068 #endif
   1069 	nfiles--;
   1070 	mutex_exit(&filelist_lock);
   1071 	mutex_destroy(&fp->f_lock);
   1072 	cv_destroy(&fp->f_cv);
   1073 	pool_put(&file_pool, fp);
   1074 	kauth_cred_free(cred);
   1075 }
   1076 
   1077 /*
   1078  * Create an initial cwdinfo structure, using the same current and root
   1079  * directories as p.
   1080  */
   1081 struct cwdinfo *
   1082 cwdinit(struct proc *p)
   1083 {
   1084 	struct cwdinfo *cwdi;
   1085 	struct cwdinfo *copy;
   1086 
   1087 	cwdi = pool_get(&cwdi_pool, PR_WAITOK);
   1088 	copy = p->p_cwdi;
   1089 
   1090 	rw_init(&cwdi->cwdi_lock);
   1091 
   1092 	rw_enter(&copy->cwdi_lock, RW_READER);
   1093 	cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
   1094 	if (cwdi->cwdi_cdir)
   1095 		VREF(cwdi->cwdi_cdir);
   1096 	cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
   1097 	if (cwdi->cwdi_rdir)
   1098 		VREF(cwdi->cwdi_rdir);
   1099 	cwdi->cwdi_cmask =  p->p_cwdi->cwdi_cmask;
   1100 	cwdi->cwdi_refcnt = 1;
   1101 	rw_exit(&copy->cwdi_lock);
   1102 
   1103 	return (cwdi);
   1104 }
   1105 
   1106 /*
   1107  * Make p2 share p1's cwdinfo.
   1108  */
   1109 void
   1110 cwdshare(struct proc *p1, struct proc *p2)
   1111 {
   1112 	struct cwdinfo *cwdi = p1->p_cwdi;
   1113 
   1114 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1115 	cwdi->cwdi_refcnt++;
   1116 	rw_exit(&cwdi->cwdi_lock);
   1117 	p2->p_cwdi = cwdi;
   1118 }
   1119 
   1120 /*
   1121  * Make this process not share its cwdinfo structure, maintaining
   1122  * all cwdinfo state.
   1123  */
   1124 void
   1125 cwdunshare(struct proc *p)
   1126 {
   1127 	struct cwdinfo *oldcwdi, *newcwdi;
   1128 
   1129 	if (p->p_cwdi->cwdi_refcnt == 1)
   1130 		return;
   1131 
   1132 	newcwdi = cwdinit(p);
   1133 	oldcwdi = p->p_cwdi;
   1134 	p->p_cwdi = newcwdi;
   1135 	cwdfree(oldcwdi);
   1136 }
   1137 
   1138 /*
   1139  * Release a cwdinfo structure.
   1140  */
   1141 void
   1142 cwdfree(struct cwdinfo *cwdi)
   1143 {
   1144 	int n;
   1145 
   1146 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1147 	n = --cwdi->cwdi_refcnt;
   1148 	rw_exit(&cwdi->cwdi_lock);
   1149 	if (n > 0)
   1150 		return;
   1151 
   1152 	vrele(cwdi->cwdi_cdir);
   1153 	if (cwdi->cwdi_rdir)
   1154 		vrele(cwdi->cwdi_rdir);
   1155 	rw_destroy(&cwdi->cwdi_lock);
   1156 	pool_put(&cwdi_pool, cwdi);
   1157 }
   1158 
   1159 /*
   1160  * Create an initial filedesc structure, using the same current and root
   1161  * directories as p.
   1162  */
   1163 struct filedesc *
   1164 fdinit(struct proc *p)
   1165 {
   1166 	struct filedesc0 *newfdp;
   1167 
   1168 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
   1169 	memset(newfdp, 0, sizeof(struct filedesc0));
   1170 
   1171 	fdinit1(newfdp);
   1172 
   1173 	return (&newfdp->fd_fd);
   1174 }
   1175 
   1176 /*
   1177  * Initialize a file descriptor table.
   1178  */
   1179 void
   1180 fdinit1(struct filedesc0 *newfdp)
   1181 {
   1182 
   1183 	newfdp->fd_fd.fd_refcnt = 1;
   1184 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
   1185 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
   1186 	newfdp->fd_fd.fd_nfiles = NDFILE;
   1187 	newfdp->fd_fd.fd_knlistsize = -1;
   1188 	newfdp->fd_fd.fd_himap = newfdp->fd_dhimap;
   1189 	newfdp->fd_fd.fd_lomap = newfdp->fd_dlomap;
   1190 	newfdp->fd_fd.fd_lastfile = -1;
   1191 	rw_init(&newfdp->fd_fd.fd_lock);
   1192 }
   1193 
   1194 /*
   1195  * Make p2 share p1's filedesc structure.
   1196  */
   1197 void
   1198 fdshare(struct proc *p1, struct proc *p2)
   1199 {
   1200 	struct filedesc *fdp = p1->p_fd;
   1201 
   1202 	rw_enter(&fdp->fd_lock, RW_WRITER);
   1203 	p2->p_fd = fdp;
   1204 	fdp->fd_refcnt++;
   1205 	rw_exit(&fdp->fd_lock);
   1206 }
   1207 
   1208 /*
   1209  * Make this process not share its filedesc structure, maintaining
   1210  * all file descriptor state.
   1211  */
   1212 void
   1213 fdunshare(struct lwp *l)
   1214 {
   1215 	struct proc *p = l->l_proc;
   1216 	struct filedesc *newfd;
   1217 
   1218 	if (p->p_fd->fd_refcnt == 1)
   1219 		return;
   1220 
   1221 	newfd = fdcopy(p);
   1222 	fdfree(l);
   1223 	p->p_fd = newfd;
   1224 }
   1225 
   1226 /*
   1227  * Clear a process's fd table.
   1228  */
   1229 void
   1230 fdclear(struct lwp *l)
   1231 {
   1232 	struct proc *p = l->l_proc;
   1233 	struct filedesc *newfd;
   1234 
   1235 	newfd = fdinit(p);
   1236 	fdfree(l);
   1237 	p->p_fd = newfd;
   1238 }
   1239 
   1240 /*
   1241  * Copy a filedesc structure.
   1242  */
   1243 struct filedesc *
   1244 fdcopy(struct proc *p)
   1245 {
   1246 	struct filedesc	*newfdp, *fdp;
   1247 	struct file	**fpp, **nfpp;
   1248 	int		i, numfiles, lastfile;
   1249 
   1250 	fdp = p->p_fd;
   1251 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
   1252 	newfdp->fd_refcnt = 1;
   1253 	rw_init(&newfdp->fd_lock);
   1254 
   1255 restart:
   1256 	numfiles = fdp->fd_nfiles;
   1257 	lastfile = fdp->fd_lastfile;
   1258 
   1259 	/*
   1260 	 * If the number of open files fits in the internal arrays
   1261 	 * of the open file structure, use them, otherwise allocate
   1262 	 * additional memory for the number of descriptors currently
   1263 	 * in use.
   1264 	 */
   1265 	if (lastfile < NDFILE) {
   1266 		i = NDFILE;
   1267 	} else {
   1268 		/*
   1269 		 * Compute the smallest multiple of NDEXTENT needed
   1270 		 * for the file descriptors currently in use,
   1271 		 * allowing the table to shrink.
   1272 		 */
   1273 		i = numfiles;
   1274 		while (i >= 2 * NDEXTENT && i > lastfile * 2)
   1275 			i /= 2;
   1276 		newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
   1277 	}
   1278 	if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
   1279 		newfdp->fd_himap = malloc(NDHISLOTS(i) * sizeof(uint32_t),
   1280 		    M_FILEDESC, M_WAITOK);
   1281 		newfdp->fd_lomap = malloc(NDLOSLOTS(i) * sizeof(uint32_t),
   1282 		    M_FILEDESC, M_WAITOK);
   1283 	}
   1284 
   1285 	rw_enter(&fdp->fd_lock, RW_READER);
   1286 	if (numfiles != fdp->fd_nfiles || lastfile != fdp->fd_lastfile) {
   1287 		rw_exit(&fdp->fd_lock);
   1288 		if (i > NDFILE)
   1289 			free(newfdp->fd_ofiles, M_FILEDESC);
   1290 		if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
   1291 			free(newfdp->fd_himap, M_FILEDESC);
   1292 			free(newfdp->fd_lomap, M_FILEDESC);
   1293 		}
   1294 		goto restart;
   1295 	}
   1296 
   1297 	if (lastfile < NDFILE) {
   1298 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
   1299 		newfdp->fd_ofileflags =
   1300 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
   1301 	} else {
   1302 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
   1303 	}
   1304 	if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
   1305 		newfdp->fd_himap =
   1306 		    ((struct filedesc0 *) newfdp)->fd_dhimap;
   1307 		newfdp->fd_lomap =
   1308 		    ((struct filedesc0 *) newfdp)->fd_dlomap;
   1309 	}
   1310 
   1311 	newfdp->fd_nfiles = i;
   1312 	newfdp->fd_lastfile = lastfile;
   1313 	newfdp->fd_freefile = fdp->fd_freefile;
   1314 
   1315 	/* Clear the entries that will not be copied over.
   1316 	 * Avoid calling memset with 0 size (i.e. when
   1317 	 * lastfile == i-1 */
   1318 	if (lastfile < (i-1))
   1319 		memset(newfdp->fd_ofiles + lastfile + 1, 0,
   1320 		    (i - lastfile - 1) * sizeof(struct file **));
   1321 	memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
   1322 	if (i < NDENTRIES * NDENTRIES)
   1323 		i = NDENTRIES * NDENTRIES; /* size of inlined bitmaps */
   1324 	memcpy(newfdp->fd_himap, fdp->fd_himap, NDHISLOTS(i)*sizeof(uint32_t));
   1325 	memcpy(newfdp->fd_lomap, fdp->fd_lomap, NDLOSLOTS(i)*sizeof(uint32_t));
   1326 
   1327 	fpp = fdp->fd_ofiles;
   1328 	nfpp = newfdp->fd_ofiles;
   1329 	for (i = 0; i <= lastfile; i++, fpp++, nfpp++) {
   1330 		if ((*nfpp = *fpp) == NULL)
   1331 			continue;
   1332 
   1333 		if ((*fpp)->f_type == DTYPE_KQUEUE)
   1334 			/* kq descriptors cannot be copied. */
   1335 			fdremove(newfdp, i);
   1336 		else {
   1337 			mutex_enter(&(*fpp)->f_lock);
   1338 			(*fpp)->f_count++;
   1339 			mutex_exit(&(*fpp)->f_lock);
   1340 		}
   1341 	}
   1342 
   1343 	rw_exit(&fdp->fd_lock);
   1344 
   1345 	newfdp->fd_knlist = NULL;
   1346 	newfdp->fd_knlistsize = -1;
   1347 	newfdp->fd_knhash = NULL;
   1348 	newfdp->fd_knhashmask = 0;
   1349 
   1350 	return (newfdp);
   1351 }
   1352 
   1353 /*
   1354  * Release a filedesc structure.
   1355  */
   1356 void
   1357 fdfree(struct lwp *l)
   1358 {
   1359 	struct proc	*p = l->l_proc;
   1360 	struct filedesc	*fdp;
   1361 	struct file	**fpp, *fp;
   1362 	int		i;
   1363 
   1364 	fdp = p->p_fd;
   1365 	rw_enter(&fdp->fd_lock, RW_WRITER);
   1366 	i = --fdp->fd_refcnt;
   1367 	rw_exit(&fdp->fd_lock);
   1368 	if (i > 0)
   1369 		return;
   1370 
   1371 	rw_destroy(&fdp->fd_lock);
   1372 	fpp = fdp->fd_ofiles;
   1373 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
   1374 		fp = *fpp;
   1375 		if (fp != NULL) {
   1376 			*fpp = NULL;
   1377 			mutex_enter(&fp->f_lock);
   1378 			FILE_USE(fp);
   1379 			if ((fdp->fd_lastfile - i) < fdp->fd_knlistsize)
   1380 				knote_fdclose(l, fdp->fd_lastfile - i);
   1381 			(void) closef(fp, l);
   1382 		}
   1383 	}
   1384 	p->p_fd = NULL;
   1385 	if (fdp->fd_nfiles > NDFILE)
   1386 		free(fdp->fd_ofiles, M_FILEDESC);
   1387 	if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
   1388 		free(fdp->fd_himap, M_FILEDESC);
   1389 		free(fdp->fd_lomap, M_FILEDESC);
   1390 	}
   1391 	if (fdp->fd_knlist)
   1392 		free(fdp->fd_knlist, M_KEVENT);
   1393 	if (fdp->fd_knhash)
   1394 		hashdone(fdp->fd_knhash, M_KEVENT);
   1395 	pool_put(&filedesc0_pool, fdp);
   1396 }
   1397 
   1398 /*
   1399  * Internal form of close.
   1400  * Decrement reference count on file structure.
   1401  * Note: p may be NULL when closing a file
   1402  * that was being passed in a message.
   1403  *
   1404  * Note: we expect the caller is holding a usecount, and expects us
   1405  * to drop it (the caller thinks the file is going away forever).
   1406  */
   1407 int
   1408 closef(struct file *fp, struct lwp *l)
   1409 {
   1410 	struct proc	*p = l->l_proc;
   1411 	struct vnode	*vp;
   1412 	struct flock	lf;
   1413 	int		error;
   1414 
   1415 	if (fp == NULL)
   1416 		return (0);
   1417 
   1418 	/*
   1419 	 * POSIX record locking dictates that any close releases ALL
   1420 	 * locks owned by this process.  This is handled by setting
   1421 	 * a flag in the unlock to free ONLY locks obeying POSIX
   1422 	 * semantics, and not to free BSD-style file locks.
   1423 	 * If the descriptor was in a message, POSIX-style locks
   1424 	 * aren't passed with the descriptor.
   1425 	 */
   1426 	if ((p->p_flag & PK_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
   1427 		lf.l_whence = SEEK_SET;
   1428 		lf.l_start = 0;
   1429 		lf.l_len = 0;
   1430 		lf.l_type = F_UNLCK;
   1431 		vp = (struct vnode *)fp->f_data;
   1432 		(void) VOP_ADVLOCK(vp, p, F_UNLCK, &lf, F_POSIX);
   1433 	}
   1434 
   1435 	/*
   1436 	 * If WANTCLOSE is set, then the reference count on the file
   1437 	 * is 0, but there were multiple users of the file.  This can
   1438 	 * happen if a filedesc structure is shared by multiple
   1439 	 * processes.
   1440 	 */
   1441 	mutex_enter(&fp->f_lock);
   1442 	if (fp->f_iflags & FIF_WANTCLOSE) {
   1443 		/*
   1444 		 * Another user of the file is already closing, and is
   1445 		 * simply waiting for other users of the file to drain.
   1446 		 * Release our usecount, and wake up the closer if it
   1447 		 * is the only remaining use.
   1448 		 */
   1449 #ifdef DIAGNOSTIC
   1450 		if (fp->f_count != 0)
   1451 			panic("closef: wantclose and count != 0");
   1452 		if (fp->f_usecount < 2)
   1453 			panic("closef: wantclose and usecount < 2");
   1454 #endif
   1455 		if (--fp->f_usecount == 1)
   1456 			cv_broadcast(&fp->f_cv);
   1457 		mutex_exit(&fp->f_lock);
   1458 		return (0);
   1459 	} else {
   1460 		/*
   1461 		 * Decrement the reference count.  If we were not the
   1462 		 * last reference, then release our use and just
   1463 		 * return.
   1464 		 */
   1465 		if (--fp->f_count > 0) {
   1466 #ifdef DIAGNOSTIC
   1467 			if (fp->f_usecount < 1)
   1468 				panic("closef: no wantclose and usecount < 1");
   1469 #endif
   1470 			fp->f_usecount--;
   1471 			mutex_exit(&fp->f_lock);
   1472 			return (0);
   1473 		}
   1474 	}
   1475 
   1476 	/*
   1477 	 * The reference count is now 0.  However, there may be
   1478 	 * multiple potential users of this file.  This can happen
   1479 	 * if multiple processes shared a single filedesc structure.
   1480 	 *
   1481 	 * Notify these potential users that the file is closing.
   1482 	 * This will prevent them from adding additional uses to
   1483 	 * the file.
   1484 	 */
   1485 	fp->f_iflags |= FIF_WANTCLOSE;
   1486 
   1487 	/*
   1488 	 * We expect the caller to add a use to the file.  So, if we
   1489 	 * are the last user, usecount will be 1.  If it is not, we
   1490 	 * must wait for the usecount to drain.  When it drains back
   1491 	 * to 1, we will be awakened so that we may proceed with the
   1492 	 * close.
   1493 	 */
   1494 #ifdef DIAGNOSTIC
   1495 	if (fp->f_usecount < 1)
   1496 		panic("closef: usecount < 1");
   1497 #endif
   1498 	while (fp->f_usecount > 1)
   1499 		cv_wait(&fp->f_cv, &fp->f_lock);
   1500 #ifdef DIAGNOSTIC
   1501 	if (fp->f_usecount != 1)
   1502 		panic("closef: usecount != 1");
   1503 #endif
   1504 
   1505 	mutex_exit(&fp->f_lock);
   1506 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
   1507 		lf.l_whence = SEEK_SET;
   1508 		lf.l_start = 0;
   1509 		lf.l_len = 0;
   1510 		lf.l_type = F_UNLCK;
   1511 		vp = (struct vnode *)fp->f_data;
   1512 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
   1513 	}
   1514 	if (fp->f_ops)
   1515 		error = (*fp->f_ops->fo_close)(fp, l);
   1516 	else
   1517 		error = 0;
   1518 
   1519 	/* Nothing references the file now, drop the final use (us). */
   1520 	fp->f_usecount--;
   1521 
   1522 	ffree(fp);
   1523 	return (error);
   1524 }
   1525 
   1526 /*
   1527  * Apply an advisory lock on a file descriptor.
   1528  *
   1529  * Just attempt to get a record lock of the requested type on
   1530  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
   1531  */
   1532 /* ARGSUSED */
   1533 int
   1534 sys_flock(struct lwp *l, void *v, register_t *retval)
   1535 {
   1536 	struct sys_flock_args /* {
   1537 		syscallarg(int)	fd;
   1538 		syscallarg(int)	how;
   1539 	} */ *uap = v;
   1540 	int		fd, how, error;
   1541 	struct proc	*p;
   1542 	struct filedesc	*fdp;
   1543 	struct file	*fp;
   1544 	struct vnode	*vp;
   1545 	struct flock	lf;
   1546 
   1547 	p = l->l_proc;
   1548 	fd = SCARG(uap, fd);
   1549 	how = SCARG(uap, how);
   1550 	fdp = p->p_fd;
   1551 	error = 0;
   1552 
   1553 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1554 		return (EBADF);
   1555 
   1556 	FILE_USE(fp);
   1557 
   1558 	if (fp->f_type != DTYPE_VNODE) {
   1559 		error = EOPNOTSUPP;
   1560 		goto out;
   1561 	}
   1562 
   1563 	vp = (struct vnode *)fp->f_data;
   1564 	lf.l_whence = SEEK_SET;
   1565 	lf.l_start = 0;
   1566 	lf.l_len = 0;
   1567 	if (how & LOCK_UN) {
   1568 		lf.l_type = F_UNLCK;
   1569 		fp->f_flag &= ~FHASLOCK;
   1570 		error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
   1571 		goto out;
   1572 	}
   1573 	if (how & LOCK_EX)
   1574 		lf.l_type = F_WRLCK;
   1575 	else if (how & LOCK_SH)
   1576 		lf.l_type = F_RDLCK;
   1577 	else {
   1578 		error = EINVAL;
   1579 		goto out;
   1580 	}
   1581 	fp->f_flag |= FHASLOCK;
   1582 	if (how & LOCK_NB)
   1583 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
   1584 	else
   1585 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf,
   1586 		    F_FLOCK|F_WAIT);
   1587  out:
   1588 	FILE_UNUSE(fp, l);
   1589 	return (error);
   1590 }
   1591 
   1592 /* ARGSUSED */
   1593 int
   1594 sys_posix_fadvise(struct lwp *l, void *v, register_t *retval)
   1595 {
   1596 	const struct sys_posix_fadvise_args /* {
   1597 		syscallarg(int) fd;
   1598 		syscallarg(off_t) offset;
   1599 		syscallarg(off_t) len;
   1600 		syscallarg(int) advice;
   1601 	} */ *uap = v;
   1602 	const int fd = SCARG(uap, fd);
   1603 	const int advice = SCARG(uap, advice);
   1604 	struct proc *p = l->l_proc;
   1605 	struct file *fp;
   1606 	int error = 0;
   1607 
   1608 	fp = fd_getfile(p->p_fd, fd);
   1609 	if (fp == NULL) {
   1610 		error = EBADF;
   1611 		goto out;
   1612 	}
   1613 	FILE_USE(fp);
   1614 
   1615 	if (fp->f_type != DTYPE_VNODE) {
   1616 		if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
   1617 			error = ESPIPE;
   1618 		} else {
   1619 			error = EOPNOTSUPP;
   1620 		}
   1621 		goto out;
   1622 	}
   1623 
   1624 	switch (advice) {
   1625 	case POSIX_FADV_NORMAL:
   1626 	case POSIX_FADV_RANDOM:
   1627 	case POSIX_FADV_SEQUENTIAL:
   1628 		KASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
   1629 		KASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
   1630 		KASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
   1631 
   1632 		/*
   1633 		 * we ignore offset and size.
   1634 		 */
   1635 
   1636 		fp->f_advice = advice;
   1637 		break;
   1638 
   1639 	case POSIX_FADV_WILLNEED:
   1640 	case POSIX_FADV_DONTNEED:
   1641 	case POSIX_FADV_NOREUSE:
   1642 
   1643 		/*
   1644 		 * not implemented yet.
   1645 		 */
   1646 
   1647 		break;
   1648 	default:
   1649 		error = EINVAL;
   1650 		break;
   1651 	}
   1652 out:
   1653 	if (fp != NULL) {
   1654 		FILE_UNUSE(fp, l);
   1655 	}
   1656 	*retval = error;
   1657 	return 0;
   1658 }
   1659 
   1660 /*
   1661  * File Descriptor pseudo-device driver (/dev/fd/).
   1662  *
   1663  * Opening minor device N dup()s the file (if any) connected to file
   1664  * descriptor N belonging to the calling process.  Note that this driver
   1665  * consists of only the ``open()'' routine, because all subsequent
   1666  * references to this file will be direct to the other driver.
   1667  */
   1668 /* ARGSUSED */
   1669 static int
   1670 filedescopen(dev_t dev, int mode, int type, struct lwp *l)
   1671 {
   1672 
   1673 	/*
   1674 	 * XXX Kludge: set dupfd to contain the value of the
   1675 	 * the file descriptor being sought for duplication. The error
   1676 	 * return ensures that the vnode for this device will be released
   1677 	 * by vn_open. Open will detect this special error and take the
   1678 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
   1679 	 * will simply report the error.
   1680 	 */
   1681 	l->l_dupfd = minor(dev);	/* XXX */
   1682 	return EDUPFD;
   1683 }
   1684 
   1685 const struct cdevsw filedesc_cdevsw = {
   1686 	filedescopen, noclose, noread, nowrite, noioctl,
   1687 	    nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
   1688 };
   1689 
   1690 /*
   1691  * Duplicate the specified descriptor to a free descriptor.
   1692  *
   1693  * 'indx' has been fdalloc'ed (and will be fdremove'ed on error) by the caller.
   1694  */
   1695 int
   1696 dupfdopen(struct lwp *l, int indx, int dfd, int mode, int error)
   1697 {
   1698 	struct proc	*p = l->l_proc;
   1699 	struct filedesc *fdp;
   1700 	struct file	*wfp;
   1701 
   1702 	fdp = p->p_fd;
   1703 
   1704 	/* should be cleared by the caller */
   1705 	KASSERT(fdp->fd_ofiles[indx] == NULL);
   1706 
   1707 	/*
   1708 	 * If the to-be-dup'd fd number is greater than the allowed number
   1709 	 * of file descriptors, or the fd to be dup'd has already been
   1710 	 * closed, reject.
   1711 	 */
   1712 
   1713 	/*
   1714 	 * Note, in the case of indx == dfd, fd_getfile below returns NULL.
   1715 	 */
   1716 	if ((wfp = fd_getfile(fdp, dfd)) == NULL)
   1717 		return (EBADF);
   1718 
   1719 	FILE_USE(wfp);
   1720 
   1721 	/*
   1722 	 * There are two cases of interest here.
   1723 	 *
   1724 	 * For EDUPFD simply dup (dfd) to file descriptor
   1725 	 * (indx) and return.
   1726 	 *
   1727 	 * For EMOVEFD steal away the file structure from (dfd) and
   1728 	 * store it in (indx).  (dfd) is effectively closed by
   1729 	 * this operation.
   1730 	 *
   1731 	 * Any other error code is just returned.
   1732 	 */
   1733 	switch (error) {
   1734 	case EDUPFD:
   1735 		/*
   1736 		 * Check that the mode the file is being opened for is a
   1737 		 * subset of the mode of the existing descriptor.
   1738 		 */
   1739 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
   1740 			FILE_UNUSE(wfp, l);
   1741 			return (EACCES);
   1742 		}
   1743 		rw_enter(&fdp->fd_lock, RW_WRITER);
   1744 		fdp->fd_ofiles[indx] = wfp;
   1745 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1746 		rw_exit(&fdp->fd_lock);
   1747 		mutex_enter(&wfp->f_lock);
   1748 		wfp->f_count++;
   1749 		/* 'indx' has been fd_used'ed by caller */
   1750 		FILE_UNUSE_HAVELOCK(wfp, l);
   1751 		return (0);
   1752 
   1753 	case EMOVEFD:
   1754 		/*
   1755 		 * Steal away the file pointer from dfd, and stuff it into indx.
   1756 		 */
   1757 		rw_enter(&fdp->fd_lock, RW_WRITER);
   1758 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
   1759 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1760 		fdp->fd_ofiles[dfd] = NULL;
   1761 		fdp->fd_ofileflags[dfd] = 0;
   1762 		/*
   1763 		 * Complete the clean up of the filedesc structure by
   1764 		 * recomputing the various hints.
   1765 		 */
   1766 		/* 'indx' has been fd_used'ed by caller */
   1767 		fd_unused(fdp, dfd);
   1768 		rw_exit(&fdp->fd_lock);
   1769 		FILE_UNUSE(wfp, l);
   1770 		return (0);
   1771 
   1772 	default:
   1773 		FILE_UNUSE(wfp, l);
   1774 		return (error);
   1775 	}
   1776 	/* NOTREACHED */
   1777 }
   1778 
   1779 /*
   1780  * Close any files on exec?
   1781  */
   1782 void
   1783 fdcloseexec(struct lwp *l)
   1784 {
   1785 	struct proc	*p = l->l_proc;
   1786 	struct filedesc *fdp;
   1787 	int		fd;
   1788 
   1789 	fdunshare(l);
   1790 	cwdunshare(p);
   1791 
   1792 	fdp = p->p_fd;
   1793 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
   1794 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
   1795 			(void) fdrelease(l, fd);
   1796 }
   1797 
   1798 /*
   1799  * It is unsafe for set[ug]id processes to be started with file
   1800  * descriptors 0..2 closed, as these descriptors are given implicit
   1801  * significance in the Standard C library.  fdcheckstd() will create a
   1802  * descriptor referencing /dev/null for each of stdin, stdout, and
   1803  * stderr that is not already open.
   1804  */
   1805 #define CHECK_UPTO 3
   1806 int
   1807 fdcheckstd(struct lwp *l)
   1808 {
   1809 	struct proc *p;
   1810 	struct nameidata nd;
   1811 	struct filedesc *fdp;
   1812 	struct file *fp;
   1813 	struct file *devnullfp = NULL;	/* Quell compiler warning */
   1814 	struct proc *pp;
   1815 	register_t retval;
   1816 	int fd, i, error, flags = FREAD|FWRITE, devnull = -1;
   1817 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
   1818 
   1819 	p = l->l_proc;
   1820 	closed[0] = '\0';
   1821 	if ((fdp = p->p_fd) == NULL)
   1822 		return (0);
   1823 	for (i = 0; i < CHECK_UPTO; i++) {
   1824 		if (fdp->fd_ofiles[i] != NULL)
   1825 			continue;
   1826 		snprintf(which, sizeof(which), ",%d", i);
   1827 		strlcat(closed, which, sizeof(closed));
   1828 		if (devnullfp == NULL) {
   1829 			if ((error = falloc(l, &fp, &fd)) != 0)
   1830 				return (error);
   1831 			NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
   1832 			    l);
   1833 			if ((error = vn_open(&nd, flags, 0)) != 0) {
   1834 				FILE_UNUSE(fp, l);
   1835 				ffree(fp);
   1836 				fdremove(p->p_fd, fd);
   1837 				return (error);
   1838 			}
   1839 			fp->f_data = nd.ni_vp;
   1840 			fp->f_flag = flags;
   1841 			fp->f_ops = &vnops;
   1842 			fp->f_type = DTYPE_VNODE;
   1843 			VOP_UNLOCK(nd.ni_vp, 0);
   1844 			devnull = fd;
   1845 			devnullfp = fp;
   1846 			FILE_SET_MATURE(fp);
   1847 		} else {
   1848 restart:
   1849 			if ((error = fdalloc(p, 0, &fd)) != 0) {
   1850 				if (error == ENOSPC) {
   1851 					fdexpand(p);
   1852 					goto restart;
   1853 				}
   1854 				return (error);
   1855 			}
   1856 
   1857 			mutex_enter(&devnullfp->f_lock);
   1858 			FILE_USE(devnullfp);
   1859 			/* finishdup() will unuse the descriptors for us */
   1860 			if ((error = finishdup(l, devnull, fd, &retval)) != 0)
   1861 				return (error);
   1862 		}
   1863 	}
   1864 	if (devnullfp)
   1865 		FILE_UNUSE(devnullfp, l);
   1866 	if (closed[0] != '\0') {
   1867 		mutex_enter(&proclist_lock);
   1868 		pp = p->p_pptr;
   1869 		mutex_enter(&pp->p_mutex);
   1870 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
   1871 		    "was invoked by uid %d ppid %d (%s) "
   1872 		    "with fd %s closed\n",
   1873 		    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
   1874 		    pp->p_pid, pp->p_comm, &closed[1]);
   1875 		mutex_exit(&pp->p_mutex);
   1876 		mutex_exit(&proclist_lock);
   1877 	}
   1878 	return (0);
   1879 }
   1880 #undef CHECK_UPTO
   1881 
   1882 /*
   1883  * Sets descriptor owner. If the owner is a process, 'pgid'
   1884  * is set to positive value, process ID. If the owner is process group,
   1885  * 'pgid' is set to -pg_id.
   1886  */
   1887 int
   1888 fsetown(struct proc *p, pid_t *pgid, int cmd, const void *data)
   1889 {
   1890 	int id = *(const int *)data;
   1891 	int error;
   1892 
   1893 	switch (cmd) {
   1894 	case TIOCSPGRP:
   1895 		if (id < 0)
   1896 			return (EINVAL);
   1897 		id = -id;
   1898 		break;
   1899 	default:
   1900 		break;
   1901 	}
   1902 
   1903 	if (id > 0 && !pfind(id))
   1904 		return (ESRCH);
   1905 	else if (id < 0 && (error = pgid_in_session(p, -id)))
   1906 		return (error);
   1907 
   1908 	*pgid = id;
   1909 	return (0);
   1910 }
   1911 
   1912 /*
   1913  * Return descriptor owner information. If the value is positive,
   1914  * it's process ID. If it's negative, it's process group ID and
   1915  * needs the sign removed before use.
   1916  */
   1917 int
   1918 fgetown(struct proc *p, pid_t pgid, int cmd, void *data)
   1919 {
   1920 	switch (cmd) {
   1921 	case TIOCGPGRP:
   1922 		*(int *)data = -pgid;
   1923 		break;
   1924 	default:
   1925 		*(int *)data = pgid;
   1926 		break;
   1927 	}
   1928 	return (0);
   1929 }
   1930 
   1931 /*
   1932  * Send signal to descriptor owner, either process or process group.
   1933  */
   1934 void
   1935 fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
   1936 {
   1937 	struct proc *p1;
   1938 	struct pgrp *pgrp;
   1939 	ksiginfo_t ksi;
   1940 
   1941 	KSI_INIT(&ksi);
   1942 	ksi.ksi_signo = signo;
   1943 	ksi.ksi_code = code;
   1944 	ksi.ksi_band = band;
   1945 
   1946 	/*
   1947 	 * Since we may be called from an interrupt context, we must use
   1948 	 * the proclist_mutex.
   1949 	 */
   1950 	mutex_enter(&proclist_mutex);
   1951 	if (pgid > 0 && (p1 = p_find(pgid, PFIND_LOCKED)))
   1952 		kpsignal(p1, &ksi, fdescdata);
   1953 	else if (pgid < 0 && (pgrp = pg_find(-pgid, PFIND_LOCKED)))
   1954 		kpgsignal(pgrp, &ksi, fdescdata, 0);
   1955 	mutex_exit(&proclist_mutex);
   1956 }
   1957 
   1958 int
   1959 fdclone(struct lwp *l, struct file *fp, int fd, int flag,
   1960     const struct fileops *fops, void *data)
   1961 {
   1962 	fp->f_flag = flag;
   1963 	fp->f_type = DTYPE_MISC;
   1964 	fp->f_ops = fops;
   1965 	fp->f_data = data;
   1966 
   1967 	l->l_dupfd = fd;
   1968 
   1969 	FILE_SET_MATURE(fp);
   1970 	FILE_UNUSE(fp, l);
   1971 	return EMOVEFD;
   1972 }
   1973 
   1974 /* ARGSUSED */
   1975 int
   1976 fnullop_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
   1977 {
   1978 
   1979 	if (cmd == F_SETFL)
   1980 		return 0;
   1981 
   1982 	return EOPNOTSUPP;
   1983 }
   1984 
   1985 /* ARGSUSED */
   1986 int
   1987 fnullop_poll(struct file *fp, int which, struct lwp *l)
   1988 {
   1989 
   1990 	return 0;
   1991 }
   1992 
   1993 
   1994 /* ARGSUSED */
   1995 int
   1996 fnullop_kqfilter(struct file *fp, struct knote *kn)
   1997 {
   1998 
   1999 	return 0;
   2000 }
   2001 
   2002 /* ARGSUSED */
   2003 int
   2004 fbadop_stat(struct file *fp, struct stat *sb, struct lwp *l)
   2005 {
   2006 
   2007 	return EOPNOTSUPP;
   2008 }
   2009