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