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