kern_descrip.c revision 137686
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)kern_descrip.c	8.6 (Berkeley) 4/19/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/kern_descrip.c 137686 2004-11-14 09:21:01Z phk $");
39
40#include "opt_compat.h"
41
42#include <sys/param.h>
43#include <sys/limits.h>
44#include <sys/systm.h>
45#include <sys/syscallsubr.h>
46#include <sys/sysproto.h>
47#include <sys/conf.h>
48#include <sys/filedesc.h>
49#include <sys/lock.h>
50#include <sys/jail.h>
51#include <sys/kernel.h>
52#include <sys/limits.h>
53#include <sys/malloc.h>
54#include <sys/mutex.h>
55#include <sys/sysctl.h>
56#include <sys/vnode.h>
57#include <sys/mount.h>
58#include <sys/proc.h>
59#include <sys/namei.h>
60#include <sys/file.h>
61#include <sys/stat.h>
62#include <sys/filio.h>
63#include <sys/fcntl.h>
64#include <sys/unistd.h>
65#include <sys/resourcevar.h>
66#include <sys/event.h>
67#include <sys/sx.h>
68#include <sys/socketvar.h>
69#include <sys/signalvar.h>
70
71#include <vm/vm.h>
72#include <vm/vm_extern.h>
73#include <vm/uma.h>
74
75static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
76static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
77		     "file desc to leader structures");
78static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
79
80static uma_zone_t file_zone;
81
82static	 d_open_t  fdopen;
83#define	NUMFDESC 64
84
85#define	CDEV_MAJOR 22
86static struct cdevsw fildesc_cdevsw = {
87	.d_version =	D_VERSION,
88	.d_flags =	D_NEEDGIANT,
89	.d_open =	fdopen,
90	.d_name =	"FD",
91	.d_maj =	CDEV_MAJOR,
92};
93
94/* How to treat 'new' parameter when allocating a fd for do_dup(). */
95enum dup_type { DUP_VARIABLE, DUP_FIXED };
96
97static int do_dup(struct thread *td, enum dup_type type, int old, int new,
98    register_t *retval);
99static int	fd_first_free(struct filedesc *, int, int);
100static int	fd_last_used(struct filedesc *, int, int);
101static void	fdgrowtable(struct filedesc *, int);
102static void	fdunused(struct filedesc *fdp, int fd);
103
104/*
105 * A process is initially started out with NDFILE descriptors stored within
106 * this structure, selected to be enough for typical applications based on
107 * the historical limit of 20 open files (and the usage of descriptors by
108 * shells).  If these descriptors are exhausted, a larger descriptor table
109 * may be allocated, up to a process' resource limit; the internal arrays
110 * are then unused.
111 */
112#define NDFILE		20
113#define NDSLOTSIZE	sizeof(NDSLOTTYPE)
114#define	NDENTRIES	(NDSLOTSIZE * __CHAR_BIT)
115#define NDSLOT(x)	((x) / NDENTRIES)
116#define NDBIT(x)	((NDSLOTTYPE)1 << ((x) % NDENTRIES))
117#define	NDSLOTS(x)	(((x) + NDENTRIES - 1) / NDENTRIES)
118
119/*
120 * Storage required per open file descriptor.
121 */
122#define OFILESIZE (sizeof(struct file *) + sizeof(char))
123
124/*
125 * Basic allocation of descriptors:
126 * one of the above, plus arrays for NDFILE descriptors.
127 */
128struct filedesc0 {
129	struct	filedesc fd_fd;
130	/*
131	 * These arrays are used when the number of open files is
132	 * <= NDFILE, and are then pointed to by the pointers above.
133	 */
134	struct	file *fd_dfiles[NDFILE];
135	char	fd_dfileflags[NDFILE];
136	NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
137};
138
139/*
140 * Descriptor management.
141 */
142struct filelist filehead;	/* head of list of open files */
143int nfiles;			/* actual number of open files */
144struct sx filelist_lock;	/* sx to protect filelist */
145struct mtx sigio_lock;		/* mtx to protect pointers to sigio */
146
147/*
148 * Find the first zero bit in the given bitmap, starting at low and not
149 * exceeding size - 1.
150 */
151static int
152fd_first_free(struct filedesc *fdp, int low, int size)
153{
154	NDSLOTTYPE *map = fdp->fd_map;
155	NDSLOTTYPE mask;
156	int off, maxoff;
157
158	if (low >= size)
159		return (low);
160
161	off = NDSLOT(low);
162	if (low % NDENTRIES) {
163		mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
164		if ((mask &= ~map[off]) != 0UL)
165			return (off * NDENTRIES + ffsl(mask) - 1);
166		++off;
167	}
168	for (maxoff = NDSLOTS(size); off < maxoff; ++off)
169		if (map[off] != ~0UL)
170			return (off * NDENTRIES + ffsl(~map[off]) - 1);
171	return (size);
172}
173
174/*
175 * Find the highest non-zero bit in the given bitmap, starting at low and
176 * not exceeding size - 1.
177 */
178static int
179fd_last_used(struct filedesc *fdp, int low, int size)
180{
181	NDSLOTTYPE *map = fdp->fd_map;
182	NDSLOTTYPE mask;
183	int off, minoff;
184
185	if (low >= size)
186		return (-1);
187
188	off = NDSLOT(size);
189	if (size % NDENTRIES) {
190		mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
191		if ((mask &= map[off]) != 0)
192			return (off * NDENTRIES + flsl(mask) - 1);
193		--off;
194	}
195	for (minoff = NDSLOT(low); off >= minoff; --off)
196		if (map[off] != 0)
197			return (off * NDENTRIES + flsl(map[off]) - 1);
198	return (size - 1);
199}
200
201static int
202fdisused(struct filedesc *fdp, int fd)
203{
204        KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
205            ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
206	return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
207}
208
209/*
210 * Mark a file descriptor as used.
211 */
212void
213fdused(struct filedesc *fdp, int fd)
214{
215	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
216	KASSERT(!fdisused(fdp, fd),
217	    ("fd already used"));
218	fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
219	if (fd > fdp->fd_lastfile)
220		fdp->fd_lastfile = fd;
221	if (fd == fdp->fd_freefile)
222		fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
223}
224
225/*
226 * Mark a file descriptor as unused.
227 */
228static void
229fdunused(struct filedesc *fdp, int fd)
230{
231	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
232	KASSERT(fdisused(fdp, fd),
233	    ("fd is already unused"));
234	KASSERT(fdp->fd_ofiles[fd] == NULL,
235	    ("fd is still in use"));
236	fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
237	if (fd < fdp->fd_freefile)
238		fdp->fd_freefile = fd;
239	if (fd == fdp->fd_lastfile)
240		fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
241}
242
243/*
244 * System calls on descriptors.
245 */
246#ifndef _SYS_SYSPROTO_H_
247struct getdtablesize_args {
248	int	dummy;
249};
250#endif
251/*
252 * MPSAFE
253 */
254/* ARGSUSED */
255int
256getdtablesize(td, uap)
257	struct thread *td;
258	struct getdtablesize_args *uap;
259{
260	struct proc *p = td->td_proc;
261
262	PROC_LOCK(p);
263	td->td_retval[0] =
264	    min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
265	PROC_UNLOCK(p);
266	return (0);
267}
268
269/*
270 * Duplicate a file descriptor to a particular value.
271 *
272 * note: keep in mind that a potential race condition exists when closing
273 * descriptors from a shared descriptor table (via rfork).
274 */
275#ifndef _SYS_SYSPROTO_H_
276struct dup2_args {
277	u_int	from;
278	u_int	to;
279};
280#endif
281/*
282 * MPSAFE
283 */
284/* ARGSUSED */
285int
286dup2(td, uap)
287	struct thread *td;
288	struct dup2_args *uap;
289{
290
291	return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
292		    td->td_retval));
293}
294
295/*
296 * Duplicate a file descriptor.
297 */
298#ifndef _SYS_SYSPROTO_H_
299struct dup_args {
300	u_int	fd;
301};
302#endif
303/*
304 * MPSAFE
305 */
306/* ARGSUSED */
307int
308dup(td, uap)
309	struct thread *td;
310	struct dup_args *uap;
311{
312
313	return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
314}
315
316/*
317 * The file control system call.
318 */
319#ifndef _SYS_SYSPROTO_H_
320struct fcntl_args {
321	int	fd;
322	int	cmd;
323	long	arg;
324};
325#endif
326/*
327 * MPSAFE
328 */
329/* ARGSUSED */
330int
331fcntl(td, uap)
332	struct thread *td;
333	struct fcntl_args *uap;
334{
335	struct flock fl;
336	intptr_t arg;
337	int error;
338
339	error = 0;
340	switch (uap->cmd) {
341	case F_GETLK:
342	case F_SETLK:
343	case F_SETLKW:
344		error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
345		arg = (intptr_t)&fl;
346		break;
347	default:
348		arg = uap->arg;
349		break;
350	}
351	if (error)
352		return (error);
353	error = kern_fcntl(td, uap->fd, uap->cmd, arg);
354	if (error)
355		return (error);
356	if (uap->cmd == F_GETLK)
357		error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
358	return (error);
359}
360
361int
362kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
363{
364	struct filedesc *fdp;
365	struct flock *flp;
366	struct file *fp;
367	struct proc *p;
368	char *pop;
369	struct vnode *vp;
370	u_int newmin;
371	int error, flg, tmp;
372	int giant_locked;
373
374	/*
375	 * XXXRW: Some fcntl() calls require Giant -- others don't.  Try to
376	 * avoid grabbing Giant for calls we know don't need it.
377	 */
378	switch (cmd) {
379	case F_DUPFD:
380	case F_GETFD:
381	case F_SETFD:
382	case F_GETFL:
383		giant_locked = 0;
384		break;
385
386	default:
387		giant_locked = 1;
388		mtx_lock(&Giant);
389	}
390
391	error = 0;
392	flg = F_POSIX;
393	p = td->td_proc;
394	fdp = p->p_fd;
395	FILEDESC_LOCK(fdp);
396	if ((unsigned)fd >= fdp->fd_nfiles ||
397	    (fp = fdp->fd_ofiles[fd]) == NULL) {
398		FILEDESC_UNLOCK(fdp);
399		error = EBADF;
400		goto done2;
401	}
402	pop = &fdp->fd_ofileflags[fd];
403
404	switch (cmd) {
405	case F_DUPFD:
406		/* mtx_assert(&Giant, MA_NOTOWNED); */
407		FILEDESC_UNLOCK(fdp);
408		newmin = arg;
409		PROC_LOCK(p);
410		if (newmin >= lim_cur(p, RLIMIT_NOFILE) ||
411		    newmin >= maxfilesperproc) {
412			PROC_UNLOCK(p);
413			error = EINVAL;
414			break;
415		}
416		PROC_UNLOCK(p);
417		error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
418		break;
419
420	case F_GETFD:
421		/* mtx_assert(&Giant, MA_NOTOWNED); */
422		td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
423		FILEDESC_UNLOCK(fdp);
424		break;
425
426	case F_SETFD:
427		/* mtx_assert(&Giant, MA_NOTOWNED); */
428		*pop = (*pop &~ UF_EXCLOSE) |
429		    (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
430		FILEDESC_UNLOCK(fdp);
431		break;
432
433	case F_GETFL:
434		/* mtx_assert(&Giant, MA_NOTOWNED); */
435		FILE_LOCK(fp);
436		FILEDESC_UNLOCK(fdp);
437		td->td_retval[0] = OFLAGS(fp->f_flag);
438		FILE_UNLOCK(fp);
439		break;
440
441	case F_SETFL:
442		mtx_assert(&Giant, MA_OWNED);
443		FILE_LOCK(fp);
444		FILEDESC_UNLOCK(fdp);
445		fhold_locked(fp);
446		fp->f_flag &= ~FCNTLFLAGS;
447		fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
448		FILE_UNLOCK(fp);
449		tmp = fp->f_flag & FNONBLOCK;
450		error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
451		if (error) {
452			fdrop(fp, td);
453			break;
454		}
455		tmp = fp->f_flag & FASYNC;
456		error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
457		if (error == 0) {
458			fdrop(fp, td);
459			break;
460		}
461		FILE_LOCK(fp);
462		fp->f_flag &= ~FNONBLOCK;
463		FILE_UNLOCK(fp);
464		tmp = 0;
465		(void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
466		fdrop(fp, td);
467		break;
468
469	case F_GETOWN:
470		mtx_assert(&Giant, MA_OWNED);
471		fhold(fp);
472		FILEDESC_UNLOCK(fdp);
473		error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
474		if (error == 0)
475			td->td_retval[0] = tmp;
476		fdrop(fp, td);
477		break;
478
479	case F_SETOWN:
480		mtx_assert(&Giant, MA_OWNED);
481		fhold(fp);
482		FILEDESC_UNLOCK(fdp);
483		tmp = arg;
484		error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
485		fdrop(fp, td);
486		break;
487
488	case F_SETLKW:
489		mtx_assert(&Giant, MA_OWNED);
490		flg |= F_WAIT;
491		/* FALLTHROUGH F_SETLK */
492
493	case F_SETLK:
494		mtx_assert(&Giant, MA_OWNED);
495		if (fp->f_type != DTYPE_VNODE) {
496			FILEDESC_UNLOCK(fdp);
497			error = EBADF;
498			break;
499		}
500
501		flp = (struct flock *)arg;
502		if (flp->l_whence == SEEK_CUR) {
503			if (fp->f_offset < 0 ||
504			    (flp->l_start > 0 &&
505			     fp->f_offset > OFF_MAX - flp->l_start)) {
506				FILEDESC_UNLOCK(fdp);
507				error = EOVERFLOW;
508				break;
509			}
510			flp->l_start += fp->f_offset;
511		}
512
513		/*
514		 * VOP_ADVLOCK() may block.
515		 */
516		fhold(fp);
517		FILEDESC_UNLOCK(fdp);
518		vp = fp->f_vnode;
519
520		switch (flp->l_type) {
521		case F_RDLCK:
522			if ((fp->f_flag & FREAD) == 0) {
523				error = EBADF;
524				break;
525			}
526			PROC_LOCK(p->p_leader);
527			p->p_leader->p_flag |= P_ADVLOCK;
528			PROC_UNLOCK(p->p_leader);
529			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
530			    flp, flg);
531			break;
532		case F_WRLCK:
533			if ((fp->f_flag & FWRITE) == 0) {
534				error = EBADF;
535				break;
536			}
537			PROC_LOCK(p->p_leader);
538			p->p_leader->p_flag |= P_ADVLOCK;
539			PROC_UNLOCK(p->p_leader);
540			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
541			    flp, flg);
542			break;
543		case F_UNLCK:
544			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
545			    flp, F_POSIX);
546			break;
547		default:
548			error = EINVAL;
549			break;
550		}
551		/* Check for race with close */
552		FILEDESC_LOCK_FAST(fdp);
553		if ((unsigned) fd >= fdp->fd_nfiles ||
554		    fp != fdp->fd_ofiles[fd]) {
555			FILEDESC_UNLOCK_FAST(fdp);
556			flp->l_whence = SEEK_SET;
557			flp->l_start = 0;
558			flp->l_len = 0;
559			flp->l_type = F_UNLCK;
560			(void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
561					   F_UNLCK, flp, F_POSIX);
562		} else
563			FILEDESC_UNLOCK_FAST(fdp);
564		fdrop(fp, td);
565		break;
566
567	case F_GETLK:
568		mtx_assert(&Giant, MA_OWNED);
569		if (fp->f_type != DTYPE_VNODE) {
570			FILEDESC_UNLOCK(fdp);
571			error = EBADF;
572			break;
573		}
574		flp = (struct flock *)arg;
575		if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
576		    flp->l_type != F_UNLCK) {
577			FILEDESC_UNLOCK(fdp);
578			error = EINVAL;
579			break;
580		}
581		if (flp->l_whence == SEEK_CUR) {
582			if ((flp->l_start > 0 &&
583			    fp->f_offset > OFF_MAX - flp->l_start) ||
584			    (flp->l_start < 0 &&
585			     fp->f_offset < OFF_MIN - flp->l_start)) {
586				FILEDESC_UNLOCK(fdp);
587				error = EOVERFLOW;
588				break;
589			}
590			flp->l_start += fp->f_offset;
591		}
592		/*
593		 * VOP_ADVLOCK() may block.
594		 */
595		fhold(fp);
596		FILEDESC_UNLOCK(fdp);
597		vp = fp->f_vnode;
598		error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
599		    F_POSIX);
600		fdrop(fp, td);
601		break;
602	default:
603		FILEDESC_UNLOCK(fdp);
604		error = EINVAL;
605		break;
606	}
607done2:
608	if (giant_locked)
609		mtx_unlock(&Giant);
610	return (error);
611}
612
613/*
614 * Common code for dup, dup2, and fcntl(F_DUPFD).
615 */
616static int
617do_dup(td, type, old, new, retval)
618	enum dup_type type;
619	int old, new;
620	register_t *retval;
621	struct thread *td;
622{
623	struct filedesc *fdp;
624	struct proc *p;
625	struct file *fp;
626	struct file *delfp;
627	int error, holdleaders, maxfd;
628
629	KASSERT((type == DUP_VARIABLE || type == DUP_FIXED),
630	    ("invalid dup type %d", type));
631
632	p = td->td_proc;
633	fdp = p->p_fd;
634
635	/*
636	 * Verify we have a valid descriptor to dup from and possibly to
637	 * dup to.
638	 */
639	if (old < 0 || new < 0)
640		return (EBADF);
641	PROC_LOCK(p);
642	maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
643	PROC_UNLOCK(p);
644	if (new >= maxfd)
645		return (EMFILE);
646
647	FILEDESC_LOCK(fdp);
648	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
649		FILEDESC_UNLOCK(fdp);
650		return (EBADF);
651	}
652	if (type == DUP_FIXED && old == new) {
653		*retval = new;
654		FILEDESC_UNLOCK(fdp);
655		return (0);
656	}
657	fp = fdp->fd_ofiles[old];
658	fhold(fp);
659
660	/*
661	 * If the caller specified a file descriptor, make sure the file
662	 * table is large enough to hold it, and grab it.  Otherwise, just
663	 * allocate a new descriptor the usual way.  Since the filedesc
664	 * lock may be temporarily dropped in the process, we have to look
665	 * out for a race.
666	 */
667	if (type == DUP_FIXED) {
668		if (new >= fdp->fd_nfiles)
669			fdgrowtable(fdp, new + 1);
670		if (fdp->fd_ofiles[new] == NULL)
671			fdused(fdp, new);
672	} else {
673		if ((error = fdalloc(td, new, &new)) != 0) {
674			FILEDESC_UNLOCK(fdp);
675			fdrop(fp, td);
676			return (error);
677		}
678	}
679
680	/*
681	 * If the old file changed out from under us then treat it as a
682	 * bad file descriptor.  Userland should do its own locking to
683	 * avoid this case.
684	 */
685	if (fdp->fd_ofiles[old] != fp) {
686		/* we've allocated a descriptor which we won't use */
687		if (fdp->fd_ofiles[new] == NULL)
688			fdunused(fdp, new);
689		FILEDESC_UNLOCK(fdp);
690		fdrop(fp, td);
691		return (EBADF);
692	}
693	KASSERT(old != new,
694	    ("new fd is same as old"));
695
696	/*
697	 * Save info on the descriptor being overwritten.  We cannot close
698	 * it without introducing an ownership race for the slot, since we
699	 * need to drop the filedesc lock to call closef().
700	 *
701	 * XXX this duplicates parts of close().
702	 */
703	delfp = fdp->fd_ofiles[new];
704	holdleaders = 0;
705	if (delfp != NULL) {
706		if (td->td_proc->p_fdtol != NULL) {
707			/*
708			 * Ask fdfree() to sleep to ensure that all relevant
709			 * process leaders can be traversed in closef().
710			 */
711			fdp->fd_holdleaderscount++;
712			holdleaders = 1;
713		}
714	}
715
716	/*
717	 * Duplicate the source descriptor
718	 */
719	fdp->fd_ofiles[new] = fp;
720	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
721	if (new > fdp->fd_lastfile)
722		fdp->fd_lastfile = new;
723	*retval = new;
724
725	/*
726	 * If we dup'd over a valid file, we now own the reference to it
727	 * and must dispose of it using closef() semantics (as if a
728	 * close() were performed on it).
729	 *
730	 * XXX this duplicates parts of close().
731	 */
732	if (delfp != NULL) {
733		knote_fdclose(td, new);
734		FILEDESC_UNLOCK(fdp);
735		mtx_lock(&Giant);
736		(void) closef(delfp, td);
737		mtx_unlock(&Giant);
738		if (holdleaders) {
739			FILEDESC_LOCK_FAST(fdp);
740			fdp->fd_holdleaderscount--;
741			if (fdp->fd_holdleaderscount == 0 &&
742			    fdp->fd_holdleaderswakeup != 0) {
743				fdp->fd_holdleaderswakeup = 0;
744				wakeup(&fdp->fd_holdleaderscount);
745			}
746			FILEDESC_UNLOCK_FAST(fdp);
747		}
748	} else {
749		FILEDESC_UNLOCK(fdp);
750	}
751	return (0);
752}
753
754/*
755 * If sigio is on the list associated with a process or process group,
756 * disable signalling from the device, remove sigio from the list and
757 * free sigio.
758 */
759void
760funsetown(sigiop)
761	struct sigio **sigiop;
762{
763	struct sigio *sigio;
764
765	SIGIO_LOCK();
766	sigio = *sigiop;
767	if (sigio == NULL) {
768		SIGIO_UNLOCK();
769		return;
770	}
771	*(sigio->sio_myref) = NULL;
772	if ((sigio)->sio_pgid < 0) {
773		struct pgrp *pg = (sigio)->sio_pgrp;
774		PGRP_LOCK(pg);
775		SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
776			     sigio, sio_pgsigio);
777		PGRP_UNLOCK(pg);
778	} else {
779		struct proc *p = (sigio)->sio_proc;
780		PROC_LOCK(p);
781		SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
782			     sigio, sio_pgsigio);
783		PROC_UNLOCK(p);
784	}
785	SIGIO_UNLOCK();
786	crfree(sigio->sio_ucred);
787	FREE(sigio, M_SIGIO);
788}
789
790/*
791 * Free a list of sigio structures.
792 * We only need to lock the SIGIO_LOCK because we have made ourselves
793 * inaccessable to callers of fsetown and therefore do not need to lock
794 * the proc or pgrp struct for the list manipulation.
795 */
796void
797funsetownlst(sigiolst)
798	struct sigiolst *sigiolst;
799{
800	struct proc *p;
801	struct pgrp *pg;
802	struct sigio *sigio;
803
804	sigio = SLIST_FIRST(sigiolst);
805	if (sigio == NULL)
806		return;
807	p = NULL;
808	pg = NULL;
809
810	/*
811	 * Every entry of the list should belong
812	 * to a single proc or pgrp.
813	 */
814	if (sigio->sio_pgid < 0) {
815		pg = sigio->sio_pgrp;
816		PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
817	} else /* if (sigio->sio_pgid > 0) */ {
818		p = sigio->sio_proc;
819		PROC_LOCK_ASSERT(p, MA_NOTOWNED);
820	}
821
822	SIGIO_LOCK();
823	while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
824		*(sigio->sio_myref) = NULL;
825		if (pg != NULL) {
826			KASSERT(sigio->sio_pgid < 0,
827			    ("Proc sigio in pgrp sigio list"));
828			KASSERT(sigio->sio_pgrp == pg,
829			    ("Bogus pgrp in sigio list"));
830			PGRP_LOCK(pg);
831			SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
832			    sio_pgsigio);
833			PGRP_UNLOCK(pg);
834		} else /* if (p != NULL) */ {
835			KASSERT(sigio->sio_pgid > 0,
836			    ("Pgrp sigio in proc sigio list"));
837			KASSERT(sigio->sio_proc == p,
838			    ("Bogus proc in sigio list"));
839			PROC_LOCK(p);
840			SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
841			    sio_pgsigio);
842			PROC_UNLOCK(p);
843		}
844		SIGIO_UNLOCK();
845		crfree(sigio->sio_ucred);
846		FREE(sigio, M_SIGIO);
847		SIGIO_LOCK();
848	}
849	SIGIO_UNLOCK();
850}
851
852/*
853 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
854 *
855 * After permission checking, add a sigio structure to the sigio list for
856 * the process or process group.
857 */
858int
859fsetown(pgid, sigiop)
860	pid_t pgid;
861	struct sigio **sigiop;
862{
863	struct proc *proc;
864	struct pgrp *pgrp;
865	struct sigio *sigio;
866	int ret;
867
868	if (pgid == 0) {
869		funsetown(sigiop);
870		return (0);
871	}
872
873	ret = 0;
874
875	/* Allocate and fill in the new sigio out of locks. */
876	MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
877	sigio->sio_pgid = pgid;
878	sigio->sio_ucred = crhold(curthread->td_ucred);
879	sigio->sio_myref = sigiop;
880
881	sx_slock(&proctree_lock);
882	if (pgid > 0) {
883		proc = pfind(pgid);
884		if (proc == NULL) {
885			ret = ESRCH;
886			goto fail;
887		}
888
889		/*
890		 * Policy - Don't allow a process to FSETOWN a process
891		 * in another session.
892		 *
893		 * Remove this test to allow maximum flexibility or
894		 * restrict FSETOWN to the current process or process
895		 * group for maximum safety.
896		 */
897		PROC_UNLOCK(proc);
898		if (proc->p_session != curthread->td_proc->p_session) {
899			ret = EPERM;
900			goto fail;
901		}
902
903		pgrp = NULL;
904	} else /* if (pgid < 0) */ {
905		pgrp = pgfind(-pgid);
906		if (pgrp == NULL) {
907			ret = ESRCH;
908			goto fail;
909		}
910		PGRP_UNLOCK(pgrp);
911
912		/*
913		 * Policy - Don't allow a process to FSETOWN a process
914		 * in another session.
915		 *
916		 * Remove this test to allow maximum flexibility or
917		 * restrict FSETOWN to the current process or process
918		 * group for maximum safety.
919		 */
920		if (pgrp->pg_session != curthread->td_proc->p_session) {
921			ret = EPERM;
922			goto fail;
923		}
924
925		proc = NULL;
926	}
927	funsetown(sigiop);
928	if (pgid > 0) {
929		PROC_LOCK(proc);
930		/*
931		 * Since funsetownlst() is called without the proctree
932		 * locked, we need to check for P_WEXIT.
933		 * XXX: is ESRCH correct?
934		 */
935		if ((proc->p_flag & P_WEXIT) != 0) {
936			PROC_UNLOCK(proc);
937			ret = ESRCH;
938			goto fail;
939		}
940		SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
941		sigio->sio_proc = proc;
942		PROC_UNLOCK(proc);
943	} else {
944		PGRP_LOCK(pgrp);
945		SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
946		sigio->sio_pgrp = pgrp;
947		PGRP_UNLOCK(pgrp);
948	}
949	sx_sunlock(&proctree_lock);
950	SIGIO_LOCK();
951	*sigiop = sigio;
952	SIGIO_UNLOCK();
953	return (0);
954
955fail:
956	sx_sunlock(&proctree_lock);
957	crfree(sigio->sio_ucred);
958	FREE(sigio, M_SIGIO);
959	return (ret);
960}
961
962/*
963 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
964 */
965pid_t
966fgetown(sigiop)
967	struct sigio **sigiop;
968{
969	pid_t pgid;
970
971	SIGIO_LOCK();
972	pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
973	SIGIO_UNLOCK();
974	return (pgid);
975}
976
977/*
978 * Close a file descriptor.
979 */
980#ifndef _SYS_SYSPROTO_H_
981struct close_args {
982	int     fd;
983};
984#endif
985/*
986 * MPSAFE
987 */
988/* ARGSUSED */
989int
990close(td, uap)
991	struct thread *td;
992	struct close_args *uap;
993{
994	struct filedesc *fdp;
995	struct file *fp;
996	int fd, error;
997	int holdleaders;
998
999	fd = uap->fd;
1000	error = 0;
1001	holdleaders = 0;
1002	fdp = td->td_proc->p_fd;
1003	mtx_lock(&Giant);
1004	FILEDESC_LOCK(fdp);
1005	if ((unsigned)fd >= fdp->fd_nfiles ||
1006	    (fp = fdp->fd_ofiles[fd]) == NULL) {
1007		FILEDESC_UNLOCK(fdp);
1008		mtx_unlock(&Giant);
1009		return (EBADF);
1010	}
1011	fdp->fd_ofiles[fd] = NULL;
1012	fdp->fd_ofileflags[fd] = 0;
1013	fdunused(fdp, fd);
1014	if (td->td_proc->p_fdtol != NULL) {
1015		/*
1016		 * Ask fdfree() to sleep to ensure that all relevant
1017		 * process leaders can be traversed in closef().
1018		 */
1019		fdp->fd_holdleaderscount++;
1020		holdleaders = 1;
1021	}
1022
1023	/*
1024	 * we now hold the fp reference that used to be owned by the descriptor
1025	 * array.
1026	 * We have to unlock the FILEDESC *AFTER* knote_fdclose to prevent a
1027	 * race of the fd getting opened, a knote added, and deleteing a knote
1028	 * for the new fd.
1029	 */
1030	knote_fdclose(td, fd);
1031	FILEDESC_UNLOCK(fdp);
1032
1033	error = closef(fp, td);
1034	mtx_unlock(&Giant);
1035	if (holdleaders) {
1036		FILEDESC_LOCK_FAST(fdp);
1037		fdp->fd_holdleaderscount--;
1038		if (fdp->fd_holdleaderscount == 0 &&
1039		    fdp->fd_holdleaderswakeup != 0) {
1040			fdp->fd_holdleaderswakeup = 0;
1041			wakeup(&fdp->fd_holdleaderscount);
1042		}
1043		FILEDESC_UNLOCK_FAST(fdp);
1044	}
1045	return (error);
1046}
1047
1048#if defined(COMPAT_43)
1049/*
1050 * Return status information about a file descriptor.
1051 */
1052#ifndef _SYS_SYSPROTO_H_
1053struct ofstat_args {
1054	int	fd;
1055	struct	ostat *sb;
1056};
1057#endif
1058/*
1059 * MPSAFE
1060 */
1061/* ARGSUSED */
1062int
1063ofstat(td, uap)
1064	struct thread *td;
1065	struct ofstat_args *uap;
1066{
1067	struct file *fp;
1068	struct stat ub;
1069	struct ostat oub;
1070	int error;
1071
1072	if ((error = fget(td, uap->fd, &fp)) != 0)
1073		goto done2;
1074	error = fo_stat(fp, &ub, td->td_ucred, td);
1075	if (error == 0) {
1076		cvtstat(&ub, &oub);
1077		error = copyout(&oub, uap->sb, sizeof(oub));
1078	}
1079	fdrop(fp, td);
1080done2:
1081	return (error);
1082}
1083#endif /* COMPAT_43 */
1084
1085/*
1086 * Return status information about a file descriptor.
1087 */
1088#ifndef _SYS_SYSPROTO_H_
1089struct fstat_args {
1090	int	fd;
1091	struct	stat *sb;
1092};
1093#endif
1094/*
1095 * MPSAFE
1096 */
1097/* ARGSUSED */
1098int
1099fstat(td, uap)
1100	struct thread *td;
1101	struct fstat_args *uap;
1102{
1103	struct file *fp;
1104	struct stat ub;
1105	int error;
1106
1107	if ((error = fget(td, uap->fd, &fp)) != 0)
1108		goto done2;
1109	error = fo_stat(fp, &ub, td->td_ucred, td);
1110	if (error == 0)
1111		error = copyout(&ub, uap->sb, sizeof(ub));
1112	fdrop(fp, td);
1113done2:
1114	return (error);
1115}
1116
1117/*
1118 * Return status information about a file descriptor.
1119 */
1120#ifndef _SYS_SYSPROTO_H_
1121struct nfstat_args {
1122	int	fd;
1123	struct	nstat *sb;
1124};
1125#endif
1126/*
1127 * MPSAFE
1128 */
1129/* ARGSUSED */
1130int
1131nfstat(td, uap)
1132	struct thread *td;
1133	struct nfstat_args *uap;
1134{
1135	struct file *fp;
1136	struct stat ub;
1137	struct nstat nub;
1138	int error;
1139
1140	if ((error = fget(td, uap->fd, &fp)) != 0)
1141		goto done2;
1142	error = fo_stat(fp, &ub, td->td_ucred, td);
1143	if (error == 0) {
1144		cvtnstat(&ub, &nub);
1145		error = copyout(&nub, uap->sb, sizeof(nub));
1146	}
1147	fdrop(fp, td);
1148done2:
1149	return (error);
1150}
1151
1152/*
1153 * Return pathconf information about a file descriptor.
1154 */
1155#ifndef _SYS_SYSPROTO_H_
1156struct fpathconf_args {
1157	int	fd;
1158	int	name;
1159};
1160#endif
1161/*
1162 * MPSAFE
1163 */
1164/* ARGSUSED */
1165int
1166fpathconf(td, uap)
1167	struct thread *td;
1168	struct fpathconf_args *uap;
1169{
1170	struct file *fp;
1171	struct vnode *vp;
1172	int error;
1173
1174	if ((error = fget(td, uap->fd, &fp)) != 0)
1175		return (error);
1176
1177	/* If asynchronous I/O is available, it works for all descriptors. */
1178	if (uap->name == _PC_ASYNC_IO) {
1179		td->td_retval[0] = async_io_version;
1180		goto out;
1181	}
1182	vp = fp->f_vnode;
1183	if (vp != NULL) {
1184		mtx_lock(&Giant);
1185		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1186		error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1187		VOP_UNLOCK(vp, 0, td);
1188		mtx_unlock(&Giant);
1189	} else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1190		if (uap->name != _PC_PIPE_BUF) {
1191			error = EINVAL;
1192		} else {
1193			td->td_retval[0] = PIPE_BUF;
1194		error = 0;
1195		}
1196	} else {
1197		error = EOPNOTSUPP;
1198	}
1199out:
1200	fdrop(fp, td);
1201	return (error);
1202}
1203
1204/*
1205 * Grow the file table to accomodate (at least) nfd descriptors.  This may
1206 * block and drop the filedesc lock, but it will reacquire it before
1207 * returing.
1208 */
1209static void
1210fdgrowtable(struct filedesc *fdp, int nfd)
1211{
1212	struct file **ntable;
1213	char *nfileflags;
1214	int nnfiles, onfiles;
1215	NDSLOTTYPE *nmap;
1216
1217	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1218
1219	KASSERT(fdp->fd_nfiles > 0,
1220	    ("zero-length file table"));
1221
1222	/* compute the size of the new table */
1223	onfiles = fdp->fd_nfiles;
1224	nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1225	if (nnfiles <= onfiles)
1226		/* the table is already large enough */
1227		return;
1228
1229	/* allocate a new table and (if required) new bitmaps */
1230	FILEDESC_UNLOCK(fdp);
1231	MALLOC(ntable, struct file **, nnfiles * OFILESIZE,
1232	    M_FILEDESC, M_ZERO | M_WAITOK);
1233	nfileflags = (char *)&ntable[nnfiles];
1234	if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1235		MALLOC(nmap, NDSLOTTYPE *, NDSLOTS(nnfiles) * NDSLOTSIZE,
1236		    M_FILEDESC, M_ZERO | M_WAITOK);
1237	else
1238		nmap = NULL;
1239	FILEDESC_LOCK(fdp);
1240
1241	/*
1242	 * We now have new tables ready to go.  Since we dropped the
1243	 * filedesc lock to call malloc(), watch out for a race.
1244	 */
1245	onfiles = fdp->fd_nfiles;
1246	if (onfiles >= nnfiles) {
1247		/* we lost the race, but that's OK */
1248		free(ntable, M_FILEDESC);
1249		if (nmap != NULL)
1250			free(nmap, M_FILEDESC);
1251		return;
1252	}
1253	bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1254	bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1255	if (onfiles > NDFILE)
1256		free(fdp->fd_ofiles, M_FILEDESC);
1257	fdp->fd_ofiles = ntable;
1258	fdp->fd_ofileflags = nfileflags;
1259	if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1260		bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1261		if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1262			free(fdp->fd_map, M_FILEDESC);
1263		fdp->fd_map = nmap;
1264	}
1265	fdp->fd_nfiles = nnfiles;
1266}
1267
1268/*
1269 * Allocate a file descriptor for the process.
1270 */
1271int
1272fdalloc(struct thread *td, int minfd, int *result)
1273{
1274	struct proc *p = td->td_proc;
1275	struct filedesc *fdp = p->p_fd;
1276	int fd = -1, maxfd;
1277
1278	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1279
1280	PROC_LOCK(p);
1281	maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1282	PROC_UNLOCK(p);
1283
1284	/*
1285	 * Search the bitmap for a free descriptor.  If none is found, try
1286	 * to grow the file table.  Keep at it until we either get a file
1287	 * descriptor or run into process or system limits; fdgrowtable()
1288	 * may drop the filedesc lock, so we're in a race.
1289	 */
1290	for (;;) {
1291		fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1292		if (fd >= maxfd)
1293			return (EMFILE);
1294		if (fd < fdp->fd_nfiles)
1295			break;
1296		fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
1297	}
1298
1299	/*
1300	 * Perform some sanity checks, then mark the file descriptor as
1301	 * used and return it to the caller.
1302	 */
1303	KASSERT(!fdisused(fdp, fd),
1304	    ("fd_first_free() returned non-free descriptor"));
1305	KASSERT(fdp->fd_ofiles[fd] == NULL,
1306	    ("free descriptor isn't"));
1307	fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
1308	fdused(fdp, fd);
1309	fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
1310	*result = fd;
1311	return (0);
1312}
1313
1314/*
1315 * Check to see whether n user file descriptors
1316 * are available to the process p.
1317 */
1318int
1319fdavail(td, n)
1320	struct thread *td;
1321	int n;
1322{
1323	struct proc *p = td->td_proc;
1324	struct filedesc *fdp = td->td_proc->p_fd;
1325	struct file **fpp;
1326	int i, lim, last;
1327
1328	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1329
1330	PROC_LOCK(p);
1331	lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1332	PROC_UNLOCK(p);
1333	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1334		return (1);
1335	last = min(fdp->fd_nfiles, lim);
1336	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1337	for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1338		if (*fpp == NULL && --n <= 0)
1339			return (1);
1340	}
1341	return (0);
1342}
1343
1344/*
1345 * Create a new open file structure and allocate
1346 * a file decriptor for the process that refers to it.
1347 * We add one reference to the file for the descriptor table
1348 * and one reference for resultfp. This is to prevent us being
1349 * prempted and the entry in the descriptor table closed after
1350 * we release the FILEDESC lock.
1351 */
1352int
1353falloc(td, resultfp, resultfd)
1354	struct thread *td;
1355	struct file **resultfp;
1356	int *resultfd;
1357{
1358	struct proc *p = td->td_proc;
1359	struct file *fp, *fq;
1360	int error, i;
1361	int maxuserfiles = maxfiles - (maxfiles / 20);
1362	static struct timeval lastfail;
1363	static int curfail;
1364
1365	fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1366	sx_xlock(&filelist_lock);
1367	if ((nfiles >= maxuserfiles && (td->td_ucred->cr_ruid != 0 ||
1368	   jailed(td->td_ucred))) || nfiles >= maxfiles) {
1369		if (ppsratecheck(&lastfail, &curfail, 1)) {
1370			printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
1371				td->td_ucred->cr_ruid);
1372		}
1373		sx_xunlock(&filelist_lock);
1374		uma_zfree(file_zone, fp);
1375		return (ENFILE);
1376	}
1377	nfiles++;
1378
1379	/*
1380	 * If the process has file descriptor zero open, add the new file
1381	 * descriptor to the list of open files at that point, otherwise
1382	 * put it at the front of the list of open files.
1383	 */
1384	fp->f_mtxp = mtx_pool_alloc(mtxpool_sleep);
1385	fp->f_count = 1;
1386	if (resultfp)
1387		fp->f_count++;
1388	fp->f_cred = crhold(td->td_ucred);
1389	fp->f_ops = &badfileops;
1390	fp->f_data = NULL;
1391	fp->f_vnode = NULL;
1392	FILEDESC_LOCK(p->p_fd);
1393	if ((fq = p->p_fd->fd_ofiles[0])) {
1394		LIST_INSERT_AFTER(fq, fp, f_list);
1395	} else {
1396		LIST_INSERT_HEAD(&filehead, fp, f_list);
1397	}
1398	sx_xunlock(&filelist_lock);
1399	if ((error = fdalloc(td, 0, &i))) {
1400		FILEDESC_UNLOCK(p->p_fd);
1401		fdrop(fp, td);
1402		if (resultfp)
1403			fdrop(fp, td);
1404		return (error);
1405	}
1406	p->p_fd->fd_ofiles[i] = fp;
1407	FILEDESC_UNLOCK(p->p_fd);
1408	if (resultfp)
1409		*resultfp = fp;
1410	if (resultfd)
1411		*resultfd = i;
1412	return (0);
1413}
1414
1415/*
1416 * Free a file descriptor.
1417 */
1418void
1419ffree(fp)
1420	struct file *fp;
1421{
1422
1423	KASSERT(fp->f_count == 0, ("ffree: fp_fcount not 0!"));
1424	sx_xlock(&filelist_lock);
1425	LIST_REMOVE(fp, f_list);
1426	nfiles--;
1427	sx_xunlock(&filelist_lock);
1428	crfree(fp->f_cred);
1429	uma_zfree(file_zone, fp);
1430}
1431
1432/*
1433 * Build a new filedesc structure from another.
1434 * Copy the current, root, and jail root vnode references.
1435 */
1436struct filedesc *
1437fdinit(fdp)
1438	struct filedesc *fdp;
1439{
1440	struct filedesc0 *newfdp;
1441
1442	newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1443	mtx_init(&newfdp->fd_fd.fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
1444	if (fdp != NULL) {
1445		FILEDESC_LOCK(fdp);
1446		newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1447		if (newfdp->fd_fd.fd_cdir)
1448			VREF(newfdp->fd_fd.fd_cdir);
1449		newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1450		if (newfdp->fd_fd.fd_rdir)
1451			VREF(newfdp->fd_fd.fd_rdir);
1452		newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1453		if (newfdp->fd_fd.fd_jdir)
1454			VREF(newfdp->fd_fd.fd_jdir);
1455		FILEDESC_UNLOCK(fdp);
1456	}
1457
1458	/* Create the file descriptor table. */
1459	newfdp->fd_fd.fd_refcnt = 1;
1460	newfdp->fd_fd.fd_cmask = CMASK;
1461	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1462	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1463	newfdp->fd_fd.fd_nfiles = NDFILE;
1464	newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1465	return (&newfdp->fd_fd);
1466}
1467
1468/*
1469 * Share a filedesc structure.
1470 */
1471struct filedesc *
1472fdshare(fdp)
1473	struct filedesc *fdp;
1474{
1475	FILEDESC_LOCK_FAST(fdp);
1476	fdp->fd_refcnt++;
1477	FILEDESC_UNLOCK_FAST(fdp);
1478	return (fdp);
1479}
1480
1481/*
1482 * Copy a filedesc structure.
1483 * A NULL pointer in returns a NULL reference, this is to ease callers,
1484 * not catch errors.
1485 */
1486struct filedesc *
1487fdcopy(fdp)
1488	struct filedesc *fdp;
1489{
1490	struct filedesc *newfdp;
1491	int i;
1492
1493	/* Certain daemons might not have file descriptors. */
1494	if (fdp == NULL)
1495		return (NULL);
1496
1497	newfdp = fdinit(fdp);
1498	FILEDESC_LOCK_FAST(fdp);
1499	while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1500		FILEDESC_UNLOCK_FAST(fdp);
1501		FILEDESC_LOCK(newfdp);
1502		fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1503		FILEDESC_UNLOCK(newfdp);
1504		FILEDESC_LOCK_FAST(fdp);
1505	}
1506	/* copy everything except kqueue descriptors */
1507	newfdp->fd_freefile = -1;
1508	for (i = 0; i <= fdp->fd_lastfile; ++i) {
1509		if (fdisused(fdp, i) &&
1510		    fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE) {
1511			newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1512			newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1513			fhold(newfdp->fd_ofiles[i]);
1514			newfdp->fd_lastfile = i;
1515		} else {
1516			if (newfdp->fd_freefile == -1)
1517				newfdp->fd_freefile = i;
1518		}
1519	}
1520	FILEDESC_UNLOCK_FAST(fdp);
1521	FILEDESC_LOCK(newfdp);
1522	for (i = 0; i <= newfdp->fd_lastfile; ++i)
1523		if (newfdp->fd_ofiles[i] != NULL)
1524			fdused(newfdp, i);
1525	FILEDESC_UNLOCK(newfdp);
1526	FILEDESC_LOCK_FAST(fdp);
1527	if (newfdp->fd_freefile == -1)
1528		newfdp->fd_freefile = i;
1529	newfdp->fd_cmask = fdp->fd_cmask;
1530	FILEDESC_UNLOCK_FAST(fdp);
1531	return (newfdp);
1532}
1533
1534/* A mutex to protect the association between a proc and filedesc. */
1535struct mtx	fdesc_mtx;
1536MTX_SYSINIT(fdesc, &fdesc_mtx, "fdesc", MTX_DEF);
1537
1538/*
1539 * Release a filedesc structure.
1540 */
1541void
1542fdfree(td)
1543	struct thread *td;
1544{
1545	struct filedesc *fdp;
1546	struct file **fpp;
1547	int i;
1548	struct filedesc_to_leader *fdtol;
1549	struct file *fp;
1550	struct vnode *vp;
1551	struct flock lf;
1552
1553	GIANT_REQUIRED;		/* VFS */
1554
1555	/* Certain daemons might not have file descriptors. */
1556	fdp = td->td_proc->p_fd;
1557	if (fdp == NULL)
1558		return;
1559
1560	/* Check for special need to clear POSIX style locks */
1561	fdtol = td->td_proc->p_fdtol;
1562	if (fdtol != NULL) {
1563		FILEDESC_LOCK(fdp);
1564		KASSERT(fdtol->fdl_refcount > 0,
1565			("filedesc_to_refcount botch: fdl_refcount=%d",
1566			 fdtol->fdl_refcount));
1567		if (fdtol->fdl_refcount == 1 &&
1568		    (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1569			i = 0;
1570			fpp = fdp->fd_ofiles;
1571			for (i = 0, fpp = fdp->fd_ofiles;
1572			     i <= fdp->fd_lastfile;
1573			     i++, fpp++) {
1574				if (*fpp == NULL ||
1575				    (*fpp)->f_type != DTYPE_VNODE)
1576					continue;
1577				fp = *fpp;
1578				fhold(fp);
1579				FILEDESC_UNLOCK(fdp);
1580				lf.l_whence = SEEK_SET;
1581				lf.l_start = 0;
1582				lf.l_len = 0;
1583				lf.l_type = F_UNLCK;
1584				vp = fp->f_vnode;
1585				(void) VOP_ADVLOCK(vp,
1586						   (caddr_t)td->td_proc->
1587						   p_leader,
1588						   F_UNLCK,
1589						   &lf,
1590						   F_POSIX);
1591				FILEDESC_LOCK(fdp);
1592				fdrop(fp, td);
1593				fpp = fdp->fd_ofiles + i;
1594			}
1595		}
1596	retry:
1597		if (fdtol->fdl_refcount == 1) {
1598			if (fdp->fd_holdleaderscount > 0 &&
1599			    (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1600				/*
1601				 * close() or do_dup() has cleared a reference
1602				 * in a shared file descriptor table.
1603				 */
1604				fdp->fd_holdleaderswakeup = 1;
1605				msleep(&fdp->fd_holdleaderscount, &fdp->fd_mtx,
1606				       PLOCK, "fdlhold", 0);
1607				goto retry;
1608			}
1609			if (fdtol->fdl_holdcount > 0) {
1610				/*
1611				 * Ensure that fdtol->fdl_leader
1612				 * remains valid in closef().
1613				 */
1614				fdtol->fdl_wakeup = 1;
1615				msleep(fdtol, &fdp->fd_mtx,
1616				       PLOCK, "fdlhold", 0);
1617				goto retry;
1618			}
1619		}
1620		fdtol->fdl_refcount--;
1621		if (fdtol->fdl_refcount == 0 &&
1622		    fdtol->fdl_holdcount == 0) {
1623			fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1624			fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1625		} else
1626			fdtol = NULL;
1627		td->td_proc->p_fdtol = NULL;
1628		FILEDESC_UNLOCK(fdp);
1629		if (fdtol != NULL)
1630			FREE(fdtol, M_FILEDESC_TO_LEADER);
1631	}
1632	FILEDESC_LOCK(fdp);
1633	if (--fdp->fd_refcnt > 0) {
1634		FILEDESC_UNLOCK(fdp);
1635		return;
1636	}
1637
1638	/*
1639	 * We are the last reference to the structure, so we can
1640	 * safely assume it will not change out from under us.
1641	 */
1642	FILEDESC_UNLOCK(fdp);
1643	fpp = fdp->fd_ofiles;
1644	for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1645		if (*fpp)
1646			(void) closef(*fpp, td);
1647	}
1648
1649	/* XXX This should happen earlier. */
1650	mtx_lock(&fdesc_mtx);
1651	td->td_proc->p_fd = NULL;
1652	mtx_unlock(&fdesc_mtx);
1653
1654	if (fdp->fd_nfiles > NDFILE)
1655		FREE(fdp->fd_ofiles, M_FILEDESC);
1656	if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1657		FREE(fdp->fd_map, M_FILEDESC);
1658	if (fdp->fd_cdir)
1659		vrele(fdp->fd_cdir);
1660	if (fdp->fd_rdir)
1661		vrele(fdp->fd_rdir);
1662	if (fdp->fd_jdir)
1663		vrele(fdp->fd_jdir);
1664	mtx_destroy(&fdp->fd_mtx);
1665	FREE(fdp, M_FILEDESC);
1666}
1667
1668/*
1669 * For setugid programs, we don't want to people to use that setugidness
1670 * to generate error messages which write to a file which otherwise would
1671 * otherwise be off-limits to the process.  We check for filesystems where
1672 * the vnode can change out from under us after execve (like [lin]procfs).
1673 *
1674 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1675 * sufficient.  We also don't for check setugidness since we know we are.
1676 */
1677static int
1678is_unsafe(struct file *fp)
1679{
1680	if (fp->f_type == DTYPE_VNODE) {
1681		struct vnode *vp = fp->f_vnode;
1682
1683		if ((vp->v_vflag & VV_PROCDEP) != 0)
1684			return (1);
1685	}
1686	return (0);
1687}
1688
1689/*
1690 * Make this setguid thing safe, if at all possible.
1691 */
1692void
1693setugidsafety(td)
1694	struct thread *td;
1695{
1696	struct filedesc *fdp;
1697	int i;
1698
1699	/* Certain daemons might not have file descriptors. */
1700	fdp = td->td_proc->p_fd;
1701	if (fdp == NULL)
1702		return;
1703
1704	/*
1705	 * Note: fdp->fd_ofiles may be reallocated out from under us while
1706	 * we are blocked in a close.  Be careful!
1707	 */
1708	FILEDESC_LOCK(fdp);
1709	for (i = 0; i <= fdp->fd_lastfile; i++) {
1710		if (i > 2)
1711			break;
1712		if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1713			struct file *fp;
1714
1715			knote_fdclose(td, i);
1716			/*
1717			 * NULL-out descriptor prior to close to avoid
1718			 * a race while close blocks.
1719			 */
1720			fp = fdp->fd_ofiles[i];
1721			fdp->fd_ofiles[i] = NULL;
1722			fdp->fd_ofileflags[i] = 0;
1723			fdunused(fdp, i);
1724			FILEDESC_UNLOCK(fdp);
1725			(void) closef(fp, td);
1726			FILEDESC_LOCK(fdp);
1727		}
1728	}
1729	FILEDESC_UNLOCK(fdp);
1730}
1731
1732void
1733fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
1734{
1735
1736	FILEDESC_LOCK(fdp);
1737	if (fdp->fd_ofiles[idx] == fp) {
1738		fdp->fd_ofiles[idx] = NULL;
1739		fdunused(fdp, idx);
1740		FILEDESC_UNLOCK(fdp);
1741		fdrop(fp, td);
1742	} else {
1743		FILEDESC_UNLOCK(fdp);
1744	}
1745}
1746
1747/*
1748 * Close any files on exec?
1749 */
1750void
1751fdcloseexec(td)
1752	struct thread *td;
1753{
1754	struct filedesc *fdp;
1755	int i;
1756
1757	/* Certain daemons might not have file descriptors. */
1758	fdp = td->td_proc->p_fd;
1759	if (fdp == NULL)
1760		return;
1761
1762	FILEDESC_LOCK(fdp);
1763
1764	/*
1765	 * We cannot cache fd_ofiles or fd_ofileflags since operations
1766	 * may block and rip them out from under us.
1767	 */
1768	for (i = 0; i <= fdp->fd_lastfile; i++) {
1769		if (fdp->fd_ofiles[i] != NULL &&
1770		    (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1771			struct file *fp;
1772
1773			knote_fdclose(td, i);
1774			/*
1775			 * NULL-out descriptor prior to close to avoid
1776			 * a race while close blocks.
1777			 */
1778			fp = fdp->fd_ofiles[i];
1779			fdp->fd_ofiles[i] = NULL;
1780			fdp->fd_ofileflags[i] = 0;
1781			fdunused(fdp, i);
1782			FILEDESC_UNLOCK(fdp);
1783			(void) closef(fp, td);
1784			FILEDESC_LOCK(fdp);
1785		}
1786	}
1787	FILEDESC_UNLOCK(fdp);
1788}
1789
1790/*
1791 * It is unsafe for set[ug]id processes to be started with file
1792 * descriptors 0..2 closed, as these descriptors are given implicit
1793 * significance in the Standard C library.  fdcheckstd() will create a
1794 * descriptor referencing /dev/null for each of stdin, stdout, and
1795 * stderr that is not already open.
1796 */
1797int
1798fdcheckstd(td)
1799	struct thread *td;
1800{
1801	struct nameidata nd;
1802	struct filedesc *fdp;
1803	struct file *fp;
1804	register_t retval;
1805	int fd, i, error, flags, devnull;
1806
1807	GIANT_REQUIRED;		/* VFS */
1808
1809	fdp = td->td_proc->p_fd;
1810	if (fdp == NULL)
1811		return (0);
1812	KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
1813	devnull = -1;
1814	error = 0;
1815	for (i = 0; i < 3; i++) {
1816		if (fdp->fd_ofiles[i] != NULL)
1817			continue;
1818		if (devnull < 0) {
1819			error = falloc(td, &fp, &fd);
1820			if (error != 0)
1821				break;
1822			/* Note extra ref on `fp' held for us by falloc(). */
1823			KASSERT(fd == i, ("oof, we didn't get our fd"));
1824			NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
1825			    td);
1826			flags = FREAD | FWRITE;
1827			error = vn_open(&nd, &flags, 0, -1);
1828			if (error != 0) {
1829				/*
1830				 * Someone may have closed the entry in the
1831				 * file descriptor table, so check it hasn't
1832				 * changed before dropping the reference count.
1833				 */
1834				FILEDESC_LOCK(fdp);
1835				KASSERT(fdp->fd_ofiles[fd] == fp,
1836				    ("table not shared, how did it change?"));
1837				fdp->fd_ofiles[fd] = NULL;
1838				fdunused(fdp, fd);
1839				FILEDESC_UNLOCK(fdp);
1840				fdrop(fp, td);
1841				fdrop(fp, td);
1842				break;
1843			}
1844			NDFREE(&nd, NDF_ONLY_PNBUF);
1845			fp->f_flag = flags;
1846			fp->f_vnode = nd.ni_vp;
1847			if (fp->f_data == NULL)
1848				fp->f_data = nd.ni_vp;
1849			if (fp->f_ops == &badfileops)
1850				fp->f_ops = &vnops;
1851			fp->f_type = DTYPE_VNODE;
1852			VOP_UNLOCK(nd.ni_vp, 0, td);
1853			devnull = fd;
1854			fdrop(fp, td);
1855		} else {
1856			error = do_dup(td, DUP_FIXED, devnull, i, &retval);
1857			if (error != 0)
1858				break;
1859		}
1860	}
1861	return (error);
1862}
1863
1864/*
1865 * Internal form of close.
1866 * Decrement reference count on file structure.
1867 * Note: td may be NULL when closing a file
1868 * that was being passed in a message.
1869 */
1870int
1871closef(fp, td)
1872	struct file *fp;
1873	struct thread *td;
1874{
1875	struct vnode *vp;
1876	struct flock lf;
1877	struct filedesc_to_leader *fdtol;
1878	struct filedesc *fdp;
1879
1880	if (fp == NULL)
1881		return (0);
1882	/*
1883	 * POSIX record locking dictates that any close releases ALL
1884	 * locks owned by this process.  This is handled by setting
1885	 * a flag in the unlock to free ONLY locks obeying POSIX
1886	 * semantics, and not to free BSD-style file locks.
1887	 * If the descriptor was in a message, POSIX-style locks
1888	 * aren't passed with the descriptor.
1889	 */
1890	if (td != NULL &&
1891	    fp->f_type == DTYPE_VNODE) {
1892		if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1893			lf.l_whence = SEEK_SET;
1894			lf.l_start = 0;
1895			lf.l_len = 0;
1896			lf.l_type = F_UNLCK;
1897			vp = fp->f_vnode;
1898			(void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
1899					   F_UNLCK, &lf, F_POSIX);
1900		}
1901		fdtol = td->td_proc->p_fdtol;
1902		if (fdtol != NULL) {
1903			/*
1904			 * Handle special case where file descriptor table
1905			 * is shared between multiple process leaders.
1906			 */
1907			fdp = td->td_proc->p_fd;
1908			FILEDESC_LOCK(fdp);
1909			for (fdtol = fdtol->fdl_next;
1910			     fdtol != td->td_proc->p_fdtol;
1911			     fdtol = fdtol->fdl_next) {
1912				if ((fdtol->fdl_leader->p_flag &
1913				     P_ADVLOCK) == 0)
1914					continue;
1915				fdtol->fdl_holdcount++;
1916				FILEDESC_UNLOCK(fdp);
1917				lf.l_whence = SEEK_SET;
1918				lf.l_start = 0;
1919				lf.l_len = 0;
1920				lf.l_type = F_UNLCK;
1921				vp = fp->f_vnode;
1922				(void) VOP_ADVLOCK(vp,
1923						   (caddr_t)fdtol->fdl_leader,
1924						   F_UNLCK, &lf, F_POSIX);
1925				FILEDESC_LOCK(fdp);
1926				fdtol->fdl_holdcount--;
1927				if (fdtol->fdl_holdcount == 0 &&
1928				    fdtol->fdl_wakeup != 0) {
1929					fdtol->fdl_wakeup = 0;
1930					wakeup(fdtol);
1931				}
1932			}
1933			FILEDESC_UNLOCK(fdp);
1934		}
1935	}
1936	return (fdrop(fp, td));
1937}
1938
1939/*
1940 * Drop reference on struct file passed in, may call closef if the
1941 * reference hits zero.
1942 */
1943int
1944fdrop(fp, td)
1945	struct file *fp;
1946	struct thread *td;
1947{
1948
1949	FILE_LOCK(fp);
1950	return (fdrop_locked(fp, td));
1951}
1952
1953/*
1954 * Extract the file pointer associated with the specified descriptor for
1955 * the current user process.
1956 *
1957 * If the descriptor doesn't exist, EBADF is returned.
1958 *
1959 * If the descriptor exists but doesn't match 'flags' then
1960 * return EBADF for read attempts and EINVAL for write attempts.
1961 *
1962 * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
1963 * It should be droped with fdrop().
1964 * If it is not set, then the refcount will not be bumped however the
1965 * thread's filedesc struct will be returned locked (for fgetsock).
1966 *
1967 * If an error occured the non-zero error is returned and *fpp is set to NULL.
1968 * Otherwise *fpp is set and zero is returned.
1969 */
1970static __inline int
1971_fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
1972{
1973	struct filedesc *fdp;
1974	struct file *fp;
1975
1976	*fpp = NULL;
1977	if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
1978		return (EBADF);
1979	FILEDESC_LOCK(fdp);
1980	if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
1981		FILEDESC_UNLOCK(fdp);
1982		return (EBADF);
1983	}
1984
1985	/*
1986	 * Note: FREAD failures returns EBADF to maintain backwards
1987	 * compatibility with what routines returned before.
1988	 *
1989	 * Only one flag, or 0, may be specified.
1990	 */
1991	if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
1992		FILEDESC_UNLOCK(fdp);
1993		return (EBADF);
1994	}
1995	if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
1996		FILEDESC_UNLOCK(fdp);
1997		return (EINVAL);
1998	}
1999	if (hold) {
2000		fhold(fp);
2001		FILEDESC_UNLOCK(fdp);
2002	}
2003	*fpp = fp;
2004	return (0);
2005}
2006
2007int
2008fget(struct thread *td, int fd, struct file **fpp)
2009{
2010
2011	return(_fget(td, fd, fpp, 0, 1));
2012}
2013
2014int
2015fget_read(struct thread *td, int fd, struct file **fpp)
2016{
2017
2018	return(_fget(td, fd, fpp, FREAD, 1));
2019}
2020
2021int
2022fget_write(struct thread *td, int fd, struct file **fpp)
2023{
2024
2025	return(_fget(td, fd, fpp, FWRITE, 1));
2026}
2027
2028/*
2029 * Like fget() but loads the underlying vnode, or returns an error if
2030 * the descriptor does not represent a vnode.  Note that pipes use vnodes
2031 * but never have VM objects (so VOP_GETVOBJECT() calls will return an
2032 * error).  The returned vnode will be vref()d.
2033 */
2034static __inline int
2035_fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
2036{
2037	struct file *fp;
2038	int error;
2039
2040	GIANT_REQUIRED;		/* VFS */
2041
2042	*vpp = NULL;
2043	if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
2044		return (error);
2045	if (fp->f_vnode == NULL) {
2046		error = EINVAL;
2047	} else {
2048		*vpp = fp->f_vnode;
2049		vref(*vpp);
2050	}
2051	FILEDESC_UNLOCK(td->td_proc->p_fd);
2052	return (error);
2053}
2054
2055int
2056fgetvp(struct thread *td, int fd, struct vnode **vpp)
2057{
2058
2059	return (_fgetvp(td, fd, vpp, 0));
2060}
2061
2062int
2063fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
2064{
2065
2066	return (_fgetvp(td, fd, vpp, FREAD));
2067}
2068
2069int
2070fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
2071{
2072
2073	return (_fgetvp(td, fd, vpp, FWRITE));
2074}
2075
2076/*
2077 * Like fget() but loads the underlying socket, or returns an error if
2078 * the descriptor does not represent a socket.
2079 *
2080 * We bump the ref count on the returned socket.  XXX Also obtain the SX
2081 * lock in the future.
2082 */
2083int
2084fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
2085{
2086	struct file *fp;
2087	int error;
2088
2089	NET_ASSERT_GIANT();
2090
2091	*spp = NULL;
2092	if (fflagp != NULL)
2093		*fflagp = 0;
2094	if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
2095		return (error);
2096	if (fp->f_type != DTYPE_SOCKET) {
2097		error = ENOTSOCK;
2098	} else {
2099		*spp = fp->f_data;
2100		if (fflagp)
2101			*fflagp = fp->f_flag;
2102		SOCK_LOCK(*spp);
2103		soref(*spp);
2104		SOCK_UNLOCK(*spp);
2105	}
2106	FILEDESC_UNLOCK(td->td_proc->p_fd);
2107	return (error);
2108}
2109
2110/*
2111 * Drop the reference count on the the socket and XXX release the SX lock in
2112 * the future.  The last reference closes the socket.
2113 */
2114void
2115fputsock(struct socket *so)
2116{
2117
2118	NET_ASSERT_GIANT();
2119	ACCEPT_LOCK();
2120	SOCK_LOCK(so);
2121	sorele(so);
2122}
2123
2124/*
2125 * Drop reference on struct file passed in, may call closef if the
2126 * reference hits zero.
2127 * Expects struct file locked, and will unlock it.
2128 */
2129int
2130fdrop_locked(fp, td)
2131	struct file *fp;
2132	struct thread *td;
2133{
2134	int error;
2135
2136	FILE_LOCK_ASSERT(fp, MA_OWNED);
2137
2138	if (--fp->f_count > 0) {
2139		FILE_UNLOCK(fp);
2140		return (0);
2141	}
2142	/* We have the last ref so we can proceed without the file lock. */
2143	FILE_UNLOCK(fp);
2144	if (fp->f_count < 0)
2145		panic("fdrop: count < 0");
2146	if (fp->f_ops != &badfileops)
2147		error = fo_close(fp, td);
2148	else
2149		error = 0;
2150	ffree(fp);
2151	return (error);
2152}
2153
2154/*
2155 * Apply an advisory lock on a file descriptor.
2156 *
2157 * Just attempt to get a record lock of the requested type on
2158 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2159 */
2160#ifndef _SYS_SYSPROTO_H_
2161struct flock_args {
2162	int	fd;
2163	int	how;
2164};
2165#endif
2166/*
2167 * MPSAFE
2168 */
2169/* ARGSUSED */
2170int
2171flock(td, uap)
2172	struct thread *td;
2173	struct flock_args *uap;
2174{
2175	struct file *fp;
2176	struct vnode *vp;
2177	struct flock lf;
2178	int error;
2179
2180	if ((error = fget(td, uap->fd, &fp)) != 0)
2181		return (error);
2182	if (fp->f_type != DTYPE_VNODE) {
2183		fdrop(fp, td);
2184		return (EOPNOTSUPP);
2185	}
2186
2187	mtx_lock(&Giant);
2188	vp = fp->f_vnode;
2189	lf.l_whence = SEEK_SET;
2190	lf.l_start = 0;
2191	lf.l_len = 0;
2192	if (uap->how & LOCK_UN) {
2193		lf.l_type = F_UNLCK;
2194		FILE_LOCK(fp);
2195		fp->f_flag &= ~FHASLOCK;
2196		FILE_UNLOCK(fp);
2197		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2198		goto done2;
2199	}
2200	if (uap->how & LOCK_EX)
2201		lf.l_type = F_WRLCK;
2202	else if (uap->how & LOCK_SH)
2203		lf.l_type = F_RDLCK;
2204	else {
2205		error = EBADF;
2206		goto done2;
2207	}
2208	FILE_LOCK(fp);
2209	fp->f_flag |= FHASLOCK;
2210	FILE_UNLOCK(fp);
2211	error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2212	    (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2213done2:
2214	fdrop(fp, td);
2215	mtx_unlock(&Giant);
2216	return (error);
2217}
2218
2219/*
2220 * File Descriptor pseudo-device driver (/dev/fd/).
2221 *
2222 * Opening minor device N dup()s the file (if any) connected to file
2223 * descriptor N belonging to the calling process.  Note that this driver
2224 * consists of only the ``open()'' routine, because all subsequent
2225 * references to this file will be direct to the other driver.
2226 */
2227/* ARGSUSED */
2228static int
2229fdopen(dev, mode, type, td)
2230	struct cdev *dev;
2231	int mode, type;
2232	struct thread *td;
2233{
2234
2235	/*
2236	 * XXX Kludge: set curthread->td_dupfd to contain the value of the
2237	 * the file descriptor being sought for duplication. The error
2238	 * return ensures that the vnode for this device will be released
2239	 * by vn_open. Open will detect this special error and take the
2240	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
2241	 * will simply report the error.
2242	 */
2243	td->td_dupfd = dev2unit(dev);
2244	return (ENODEV);
2245}
2246
2247/*
2248 * Duplicate the specified descriptor to a free descriptor.
2249 */
2250int
2251dupfdopen(td, fdp, indx, dfd, mode, error)
2252	struct thread *td;
2253	struct filedesc *fdp;
2254	int indx, dfd;
2255	int mode;
2256	int error;
2257{
2258	struct file *wfp;
2259	struct file *fp;
2260
2261	/*
2262	 * If the to-be-dup'd fd number is greater than the allowed number
2263	 * of file descriptors, or the fd to be dup'd has already been
2264	 * closed, then reject.
2265	 */
2266	FILEDESC_LOCK(fdp);
2267	if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2268	    (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2269		FILEDESC_UNLOCK(fdp);
2270		return (EBADF);
2271	}
2272
2273	/*
2274	 * There are two cases of interest here.
2275	 *
2276	 * For ENODEV simply dup (dfd) to file descriptor
2277	 * (indx) and return.
2278	 *
2279	 * For ENXIO steal away the file structure from (dfd) and
2280	 * store it in (indx).  (dfd) is effectively closed by
2281	 * this operation.
2282	 *
2283	 * Any other error code is just returned.
2284	 */
2285	switch (error) {
2286	case ENODEV:
2287		/*
2288		 * Check that the mode the file is being opened for is a
2289		 * subset of the mode of the existing descriptor.
2290		 */
2291		FILE_LOCK(wfp);
2292		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2293			FILE_UNLOCK(wfp);
2294			FILEDESC_UNLOCK(fdp);
2295			return (EACCES);
2296		}
2297		fp = fdp->fd_ofiles[indx];
2298		fdp->fd_ofiles[indx] = wfp;
2299		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2300		if (fp == NULL)
2301			fdused(fdp, indx);
2302		fhold_locked(wfp);
2303		FILE_UNLOCK(wfp);
2304		if (fp != NULL)
2305			FILE_LOCK(fp);
2306		FILEDESC_UNLOCK(fdp);
2307		/*
2308		 * We now own the reference to fp that the ofiles[] array
2309		 * used to own.  Release it.
2310		 */
2311		if (fp != NULL)
2312			fdrop_locked(fp, td);
2313		return (0);
2314
2315	case ENXIO:
2316		/*
2317		 * Steal away the file pointer from dfd and stuff it into indx.
2318		 */
2319		fp = fdp->fd_ofiles[indx];
2320		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2321		fdp->fd_ofiles[dfd] = NULL;
2322		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2323		fdp->fd_ofileflags[dfd] = 0;
2324		fdunused(fdp, dfd);
2325		if (fp == NULL)
2326			fdused(fdp, indx);
2327		if (fp != NULL)
2328			FILE_LOCK(fp);
2329		FILEDESC_UNLOCK(fdp);
2330
2331		/*
2332		 * we now own the reference to fp that the ofiles[] array
2333		 * used to own.  Release it.
2334		 */
2335		if (fp != NULL)
2336			fdrop_locked(fp, td);
2337		return (0);
2338
2339	default:
2340		FILEDESC_UNLOCK(fdp);
2341		return (error);
2342	}
2343	/* NOTREACHED */
2344}
2345
2346struct filedesc_to_leader *
2347filedesc_to_leader_alloc(struct filedesc_to_leader *old,
2348			 struct filedesc *fdp,
2349			 struct proc *leader)
2350{
2351	struct filedesc_to_leader *fdtol;
2352
2353	MALLOC(fdtol, struct filedesc_to_leader *,
2354	       sizeof(struct filedesc_to_leader),
2355	       M_FILEDESC_TO_LEADER,
2356	       M_WAITOK);
2357	fdtol->fdl_refcount = 1;
2358	fdtol->fdl_holdcount = 0;
2359	fdtol->fdl_wakeup = 0;
2360	fdtol->fdl_leader = leader;
2361	if (old != NULL) {
2362		FILEDESC_LOCK(fdp);
2363		fdtol->fdl_next = old->fdl_next;
2364		fdtol->fdl_prev = old;
2365		old->fdl_next = fdtol;
2366		fdtol->fdl_next->fdl_prev = fdtol;
2367		FILEDESC_UNLOCK(fdp);
2368	} else {
2369		fdtol->fdl_next = fdtol;
2370		fdtol->fdl_prev = fdtol;
2371	}
2372	return (fdtol);
2373}
2374
2375/*
2376 * Get file structures.
2377 */
2378static int
2379sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2380{
2381	struct xfile xf;
2382	struct filedesc *fdp;
2383	struct file *fp;
2384	struct proc *p;
2385	int error, n;
2386
2387	/*
2388	 * Note: because the number of file descriptors is calculated
2389	 * in different ways for sizing vs returning the data,
2390	 * there is information leakage from the first loop.  However,
2391	 * it is of a similar order of magnitude to the leakage from
2392	 * global system statistics such as kern.openfiles.
2393	 */
2394	error = sysctl_wire_old_buffer(req, 0);
2395	if (error != 0)
2396		return (error);
2397	if (req->oldptr == NULL) {
2398		n = 16;		/* A slight overestimate. */
2399		sx_slock(&filelist_lock);
2400		LIST_FOREACH(fp, &filehead, f_list) {
2401			/*
2402			 * We should grab the lock, but this is an
2403			 * estimate, so does it really matter?
2404			 */
2405			/* mtx_lock(fp->f_mtxp); */
2406			n += fp->f_count;
2407			/* mtx_unlock(f->f_mtxp); */
2408		}
2409		sx_sunlock(&filelist_lock);
2410		return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2411	}
2412	error = 0;
2413	bzero(&xf, sizeof(xf));
2414	xf.xf_size = sizeof(xf);
2415	sx_slock(&allproc_lock);
2416	LIST_FOREACH(p, &allproc, p_list) {
2417		if (p->p_state == PRS_NEW)
2418			continue;
2419		PROC_LOCK(p);
2420		if (p_cansee(req->td, p) != 0) {
2421			PROC_UNLOCK(p);
2422			continue;
2423		}
2424		xf.xf_pid = p->p_pid;
2425		xf.xf_uid = p->p_ucred->cr_uid;
2426		PROC_UNLOCK(p);
2427		mtx_lock(&fdesc_mtx);
2428		if ((fdp = p->p_fd) == NULL) {
2429			mtx_unlock(&fdesc_mtx);
2430			continue;
2431		}
2432		FILEDESC_LOCK(fdp);
2433		for (n = 0; n < fdp->fd_nfiles; ++n) {
2434			if ((fp = fdp->fd_ofiles[n]) == NULL)
2435				continue;
2436			xf.xf_fd = n;
2437			xf.xf_file = fp;
2438			xf.xf_data = fp->f_data;
2439			xf.xf_vnode = fp->f_vnode;
2440			xf.xf_type = fp->f_type;
2441			xf.xf_count = fp->f_count;
2442			xf.xf_msgcount = fp->f_msgcount;
2443			xf.xf_offset = fp->f_offset;
2444			xf.xf_flag = fp->f_flag;
2445			error = SYSCTL_OUT(req, &xf, sizeof(xf));
2446			if (error)
2447				break;
2448		}
2449		FILEDESC_UNLOCK(fdp);
2450		mtx_unlock(&fdesc_mtx);
2451		if (error)
2452			break;
2453	}
2454	sx_sunlock(&allproc_lock);
2455	return (error);
2456}
2457
2458SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2459    0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2460
2461SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2462    &maxfilesperproc, 0, "Maximum files allowed open per process");
2463
2464SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2465    &maxfiles, 0, "Maximum number of files");
2466
2467SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2468    &nfiles, 0, "System-wide number of open files");
2469
2470static void
2471fildesc_drvinit(void *unused)
2472{
2473	struct cdev *dev;
2474
2475	dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
2476	make_dev_alias(dev, "stdin");
2477	dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
2478	make_dev_alias(dev, "stdout");
2479	dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
2480	make_dev_alias(dev, "stderr");
2481}
2482
2483static fo_rdwr_t	badfo_readwrite;
2484static fo_ioctl_t	badfo_ioctl;
2485static fo_poll_t	badfo_poll;
2486static fo_kqfilter_t	badfo_kqfilter;
2487static fo_stat_t	badfo_stat;
2488static fo_close_t	badfo_close;
2489
2490struct fileops badfileops = {
2491	.fo_read = badfo_readwrite,
2492	.fo_write = badfo_readwrite,
2493	.fo_ioctl = badfo_ioctl,
2494	.fo_poll = badfo_poll,
2495	.fo_kqfilter = badfo_kqfilter,
2496	.fo_stat = badfo_stat,
2497	.fo_close = badfo_close,
2498};
2499
2500static int
2501badfo_readwrite(fp, uio, active_cred, flags, td)
2502	struct file *fp;
2503	struct uio *uio;
2504	struct ucred *active_cred;
2505	struct thread *td;
2506	int flags;
2507{
2508
2509	return (EBADF);
2510}
2511
2512static int
2513badfo_ioctl(fp, com, data, active_cred, td)
2514	struct file *fp;
2515	u_long com;
2516	void *data;
2517	struct ucred *active_cred;
2518	struct thread *td;
2519{
2520
2521	return (EBADF);
2522}
2523
2524static int
2525badfo_poll(fp, events, active_cred, td)
2526	struct file *fp;
2527	int events;
2528	struct ucred *active_cred;
2529	struct thread *td;
2530{
2531
2532	return (0);
2533}
2534
2535static int
2536badfo_kqfilter(fp, kn)
2537	struct file *fp;
2538	struct knote *kn;
2539{
2540
2541	return (0);
2542}
2543
2544static int
2545badfo_stat(fp, sb, active_cred, td)
2546	struct file *fp;
2547	struct stat *sb;
2548	struct ucred *active_cred;
2549	struct thread *td;
2550{
2551
2552	return (EBADF);
2553}
2554
2555static int
2556badfo_close(fp, td)
2557	struct file *fp;
2558	struct thread *td;
2559{
2560
2561	return (EBADF);
2562}
2563
2564SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
2565					fildesc_drvinit,NULL)
2566
2567static void filelistinit(void *);
2568SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
2569
2570/* ARGSUSED*/
2571static void
2572filelistinit(dummy)
2573	void *dummy;
2574{
2575
2576	file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
2577	    NULL, NULL, UMA_ALIGN_PTR, 0);
2578	sx_init(&filelist_lock, "filelist lock");
2579	mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
2580}
2581