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