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