kern_descrip.c revision 223866
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 223866 2011-07-08 12:19:25Z jonathan $");
39
40#include "opt_capsicum.h"
41#include "opt_compat.h"
42#include "opt_ddb.h"
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47
48#include <sys/capability.h>
49#include <sys/conf.h>
50#include <sys/domain.h>
51#include <sys/fcntl.h>
52#include <sys/file.h>
53#include <sys/filedesc.h>
54#include <sys/filio.h>
55#include <sys/jail.h>
56#include <sys/kernel.h>
57#include <sys/limits.h>
58#include <sys/lock.h>
59#include <sys/malloc.h>
60#include <sys/mount.h>
61#include <sys/mqueue.h>
62#include <sys/mutex.h>
63#include <sys/namei.h>
64#include <sys/selinfo.h>
65#include <sys/pipe.h>
66#include <sys/priv.h>
67#include <sys/proc.h>
68#include <sys/protosw.h>
69#include <sys/racct.h>
70#include <sys/resourcevar.h>
71#include <sys/signalvar.h>
72#include <sys/socketvar.h>
73#include <sys/stat.h>
74#include <sys/sx.h>
75#include <sys/syscallsubr.h>
76#include <sys/sysctl.h>
77#include <sys/sysproto.h>
78#include <sys/tty.h>
79#include <sys/unistd.h>
80#include <sys/un.h>
81#include <sys/unpcb.h>
82#include <sys/user.h>
83#include <sys/vnode.h>
84#ifdef KTRACE
85#include <sys/ktrace.h>
86#endif
87
88#include <net/vnet.h>
89
90#include <netinet/in.h>
91#include <netinet/in_pcb.h>
92
93#include <security/audit/audit.h>
94
95#include <vm/uma.h>
96#include <vm/vm.h>
97
98#include <ddb/ddb.h>
99
100static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
101static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
102		     "file desc to leader structures");
103static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
104
105static uma_zone_t file_zone;
106
107
108/* Flags for do_dup() */
109#define DUP_FIXED	0x1	/* Force fixed allocation */
110#define DUP_FCNTL	0x2	/* fcntl()-style errors */
111
112static int do_dup(struct thread *td, int flags, int old, int new,
113    register_t *retval);
114static int	fd_first_free(struct filedesc *, int, int);
115static int	fd_last_used(struct filedesc *, int, int);
116static void	fdgrowtable(struct filedesc *, int);
117static void	fdunused(struct filedesc *fdp, int fd);
118static void	fdused(struct filedesc *fdp, int fd);
119static int	fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
120static int	fill_socket_info(struct socket *so, struct kinfo_file *kif);
121static int	fill_pts_info(struct tty *tp, struct kinfo_file *kif);
122static int	fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
123
124/*
125 * A process is initially started out with NDFILE descriptors stored within
126 * this structure, selected to be enough for typical applications based on
127 * the historical limit of 20 open files (and the usage of descriptors by
128 * shells).  If these descriptors are exhausted, a larger descriptor table
129 * may be allocated, up to a process' resource limit; the internal arrays
130 * are then unused.
131 */
132#define NDFILE		20
133#define NDSLOTSIZE	sizeof(NDSLOTTYPE)
134#define	NDENTRIES	(NDSLOTSIZE * __CHAR_BIT)
135#define NDSLOT(x)	((x) / NDENTRIES)
136#define NDBIT(x)	((NDSLOTTYPE)1 << ((x) % NDENTRIES))
137#define	NDSLOTS(x)	(((x) + NDENTRIES - 1) / NDENTRIES)
138
139/*
140 * Storage required per open file descriptor.
141 */
142#define OFILESIZE (sizeof(struct file *) + sizeof(char))
143
144/*
145 * Storage to hold unused ofiles that need to be reclaimed.
146 */
147struct freetable {
148	struct file	**ft_table;
149	SLIST_ENTRY(freetable) ft_next;
150};
151
152/*
153 * Basic allocation of descriptors:
154 * one of the above, plus arrays for NDFILE descriptors.
155 */
156struct filedesc0 {
157	struct	filedesc fd_fd;
158	/*
159	 * ofiles which need to be reclaimed on free.
160	 */
161	SLIST_HEAD(,freetable) fd_free;
162	/*
163	 * These arrays are used when the number of open files is
164	 * <= NDFILE, and are then pointed to by the pointers above.
165	 */
166	struct	file *fd_dfiles[NDFILE];
167	char	fd_dfileflags[NDFILE];
168	NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
169};
170
171/*
172 * Descriptor management.
173 */
174volatile int openfiles;			/* actual number of open files */
175struct mtx sigio_lock;		/* mtx to protect pointers to sigio */
176void	(*mq_fdclose)(struct thread *td, int fd, struct file *fp);
177
178/* A mutex to protect the association between a proc and filedesc. */
179static struct mtx	fdesc_mtx;
180
181/*
182 * Find the first zero bit in the given bitmap, starting at low and not
183 * exceeding size - 1.
184 */
185static int
186fd_first_free(struct filedesc *fdp, int low, int size)
187{
188	NDSLOTTYPE *map = fdp->fd_map;
189	NDSLOTTYPE mask;
190	int off, maxoff;
191
192	if (low >= size)
193		return (low);
194
195	off = NDSLOT(low);
196	if (low % NDENTRIES) {
197		mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
198		if ((mask &= ~map[off]) != 0UL)
199			return (off * NDENTRIES + ffsl(mask) - 1);
200		++off;
201	}
202	for (maxoff = NDSLOTS(size); off < maxoff; ++off)
203		if (map[off] != ~0UL)
204			return (off * NDENTRIES + ffsl(~map[off]) - 1);
205	return (size);
206}
207
208/*
209 * Find the highest non-zero bit in the given bitmap, starting at low and
210 * not exceeding size - 1.
211 */
212static int
213fd_last_used(struct filedesc *fdp, int low, int size)
214{
215	NDSLOTTYPE *map = fdp->fd_map;
216	NDSLOTTYPE mask;
217	int off, minoff;
218
219	if (low >= size)
220		return (-1);
221
222	off = NDSLOT(size);
223	if (size % NDENTRIES) {
224		mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
225		if ((mask &= map[off]) != 0)
226			return (off * NDENTRIES + flsl(mask) - 1);
227		--off;
228	}
229	for (minoff = NDSLOT(low); off >= minoff; --off)
230		if (map[off] != 0)
231			return (off * NDENTRIES + flsl(map[off]) - 1);
232	return (low - 1);
233}
234
235static int
236fdisused(struct filedesc *fdp, int fd)
237{
238        KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
239            ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
240	return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
241}
242
243/*
244 * Mark a file descriptor as used.
245 */
246static void
247fdused(struct filedesc *fdp, int fd)
248{
249
250	FILEDESC_XLOCK_ASSERT(fdp);
251	KASSERT(!fdisused(fdp, fd),
252	    ("fd already used"));
253
254	fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
255	if (fd > fdp->fd_lastfile)
256		fdp->fd_lastfile = fd;
257	if (fd == fdp->fd_freefile)
258		fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
259}
260
261/*
262 * Mark a file descriptor as unused.
263 */
264static void
265fdunused(struct filedesc *fdp, int fd)
266{
267
268	FILEDESC_XLOCK_ASSERT(fdp);
269	KASSERT(fdisused(fdp, fd),
270	    ("fd is already unused"));
271	KASSERT(fdp->fd_ofiles[fd] == NULL,
272	    ("fd is still in use"));
273
274	fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
275	if (fd < fdp->fd_freefile)
276		fdp->fd_freefile = fd;
277	if (fd == fdp->fd_lastfile)
278		fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
279}
280
281/*
282 * System calls on descriptors.
283 */
284#ifndef _SYS_SYSPROTO_H_
285struct getdtablesize_args {
286	int	dummy;
287};
288#endif
289/* ARGSUSED */
290int
291getdtablesize(struct thread *td, struct getdtablesize_args *uap)
292{
293	struct proc *p = td->td_proc;
294	uint64_t lim;
295
296	PROC_LOCK(p);
297	td->td_retval[0] =
298	    min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
299	lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
300	PROC_UNLOCK(p);
301	if (lim < td->td_retval[0])
302		td->td_retval[0] = lim;
303	return (0);
304}
305
306/*
307 * Duplicate a file descriptor to a particular value.
308 *
309 * Note: keep in mind that a potential race condition exists when closing
310 * descriptors from a shared descriptor table (via rfork).
311 */
312#ifndef _SYS_SYSPROTO_H_
313struct dup2_args {
314	u_int	from;
315	u_int	to;
316};
317#endif
318/* ARGSUSED */
319int
320dup2(struct thread *td, struct dup2_args *uap)
321{
322
323	return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
324		    td->td_retval));
325}
326
327/*
328 * Duplicate a file descriptor.
329 */
330#ifndef _SYS_SYSPROTO_H_
331struct dup_args {
332	u_int	fd;
333};
334#endif
335/* ARGSUSED */
336int
337dup(struct thread *td, struct dup_args *uap)
338{
339
340	return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
341}
342
343/*
344 * The file control system call.
345 */
346#ifndef _SYS_SYSPROTO_H_
347struct fcntl_args {
348	int	fd;
349	int	cmd;
350	long	arg;
351};
352#endif
353/* ARGSUSED */
354int
355fcntl(struct thread *td, struct fcntl_args *uap)
356{
357	struct flock fl;
358	struct oflock ofl;
359	intptr_t arg;
360	int error;
361	int cmd;
362
363	error = 0;
364	cmd = uap->cmd;
365	switch (uap->cmd) {
366	case F_OGETLK:
367	case F_OSETLK:
368	case F_OSETLKW:
369		/*
370		 * Convert old flock structure to new.
371		 */
372		error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
373		fl.l_start = ofl.l_start;
374		fl.l_len = ofl.l_len;
375		fl.l_pid = ofl.l_pid;
376		fl.l_type = ofl.l_type;
377		fl.l_whence = ofl.l_whence;
378		fl.l_sysid = 0;
379
380		switch (uap->cmd) {
381		case F_OGETLK:
382		    cmd = F_GETLK;
383		    break;
384		case F_OSETLK:
385		    cmd = F_SETLK;
386		    break;
387		case F_OSETLKW:
388		    cmd = F_SETLKW;
389		    break;
390		}
391		arg = (intptr_t)&fl;
392		break;
393        case F_GETLK:
394        case F_SETLK:
395        case F_SETLKW:
396	case F_SETLK_REMOTE:
397                error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
398                arg = (intptr_t)&fl;
399                break;
400	default:
401		arg = uap->arg;
402		break;
403	}
404	if (error)
405		return (error);
406	error = kern_fcntl(td, uap->fd, cmd, arg);
407	if (error)
408		return (error);
409	if (uap->cmd == F_OGETLK) {
410		ofl.l_start = fl.l_start;
411		ofl.l_len = fl.l_len;
412		ofl.l_pid = fl.l_pid;
413		ofl.l_type = fl.l_type;
414		ofl.l_whence = fl.l_whence;
415		error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
416	} else if (uap->cmd == F_GETLK) {
417		error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
418	}
419	return (error);
420}
421
422static inline struct file *
423fdtofp(int fd, struct filedesc *fdp)
424{
425	struct file *fp;
426
427	FILEDESC_LOCK_ASSERT(fdp);
428	if ((unsigned)fd >= fdp->fd_nfiles ||
429	    (fp = fdp->fd_ofiles[fd]) == NULL)
430		return (NULL);
431	return (fp);
432}
433
434int
435kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
436{
437	struct filedesc *fdp;
438	struct flock *flp;
439	struct file *fp;
440	struct proc *p;
441	char *pop;
442	struct vnode *vp;
443	int error, flg, tmp;
444	int vfslocked;
445	u_int old, new;
446	uint64_t bsize;
447
448	vfslocked = 0;
449	error = 0;
450	flg = F_POSIX;
451	p = td->td_proc;
452	fdp = p->p_fd;
453
454	switch (cmd) {
455	case F_DUPFD:
456		tmp = arg;
457		error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
458		break;
459
460	case F_DUP2FD:
461		tmp = arg;
462		error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
463		break;
464
465	case F_GETFD:
466		FILEDESC_SLOCK(fdp);
467		if ((fp = fdtofp(fd, fdp)) == NULL) {
468			FILEDESC_SUNLOCK(fdp);
469			error = EBADF;
470			break;
471		}
472		pop = &fdp->fd_ofileflags[fd];
473		td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
474		FILEDESC_SUNLOCK(fdp);
475		break;
476
477	case F_SETFD:
478		FILEDESC_XLOCK(fdp);
479		if ((fp = fdtofp(fd, fdp)) == NULL) {
480			FILEDESC_XUNLOCK(fdp);
481			error = EBADF;
482			break;
483		}
484		pop = &fdp->fd_ofileflags[fd];
485		*pop = (*pop &~ UF_EXCLOSE) |
486		    (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
487		FILEDESC_XUNLOCK(fdp);
488		break;
489
490	case F_GETFL:
491		FILEDESC_SLOCK(fdp);
492		if ((fp = fdtofp(fd, fdp)) == NULL) {
493			FILEDESC_SUNLOCK(fdp);
494			error = EBADF;
495			break;
496		}
497		td->td_retval[0] = OFLAGS(fp->f_flag);
498		FILEDESC_SUNLOCK(fdp);
499		break;
500
501	case F_SETFL:
502		FILEDESC_SLOCK(fdp);
503		if ((fp = fdtofp(fd, fdp)) == NULL) {
504			FILEDESC_SUNLOCK(fdp);
505			error = EBADF;
506			break;
507		}
508		fhold(fp);
509		FILEDESC_SUNLOCK(fdp);
510		do {
511			tmp = flg = fp->f_flag;
512			tmp &= ~FCNTLFLAGS;
513			tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
514		} while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
515		tmp = fp->f_flag & FNONBLOCK;
516		error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
517		if (error) {
518			fdrop(fp, td);
519			break;
520		}
521		tmp = fp->f_flag & FASYNC;
522		error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
523		if (error == 0) {
524			fdrop(fp, td);
525			break;
526		}
527		atomic_clear_int(&fp->f_flag, FNONBLOCK);
528		tmp = 0;
529		(void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
530		fdrop(fp, td);
531		break;
532
533	case F_GETOWN:
534		FILEDESC_SLOCK(fdp);
535		if ((fp = fdtofp(fd, fdp)) == NULL) {
536			FILEDESC_SUNLOCK(fdp);
537			error = EBADF;
538			break;
539		}
540		fhold(fp);
541		FILEDESC_SUNLOCK(fdp);
542		error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
543		if (error == 0)
544			td->td_retval[0] = tmp;
545		fdrop(fp, td);
546		break;
547
548	case F_SETOWN:
549		FILEDESC_SLOCK(fdp);
550		if ((fp = fdtofp(fd, fdp)) == NULL) {
551			FILEDESC_SUNLOCK(fdp);
552			error = EBADF;
553			break;
554		}
555		fhold(fp);
556		FILEDESC_SUNLOCK(fdp);
557		tmp = arg;
558		error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
559		fdrop(fp, td);
560		break;
561
562	case F_SETLK_REMOTE:
563		error = priv_check(td, PRIV_NFS_LOCKD);
564		if (error)
565			return (error);
566		flg = F_REMOTE;
567		goto do_setlk;
568
569	case F_SETLKW:
570		flg |= F_WAIT;
571		/* FALLTHROUGH F_SETLK */
572
573	case F_SETLK:
574	do_setlk:
575		FILEDESC_SLOCK(fdp);
576		if ((fp = fdtofp(fd, fdp)) == NULL) {
577			FILEDESC_SUNLOCK(fdp);
578			error = EBADF;
579			break;
580		}
581		if (fp->f_type != DTYPE_VNODE) {
582			FILEDESC_SUNLOCK(fdp);
583			error = EBADF;
584			break;
585		}
586		flp = (struct flock *)arg;
587		if (flp->l_whence == SEEK_CUR) {
588			if (fp->f_offset < 0 ||
589			    (flp->l_start > 0 &&
590			     fp->f_offset > OFF_MAX - flp->l_start)) {
591				FILEDESC_SUNLOCK(fdp);
592				error = EOVERFLOW;
593				break;
594			}
595			flp->l_start += fp->f_offset;
596		}
597
598		/*
599		 * VOP_ADVLOCK() may block.
600		 */
601		fhold(fp);
602		FILEDESC_SUNLOCK(fdp);
603		vp = fp->f_vnode;
604		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
605		switch (flp->l_type) {
606		case F_RDLCK:
607			if ((fp->f_flag & FREAD) == 0) {
608				error = EBADF;
609				break;
610			}
611			PROC_LOCK(p->p_leader);
612			p->p_leader->p_flag |= P_ADVLOCK;
613			PROC_UNLOCK(p->p_leader);
614			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
615			    flp, flg);
616			break;
617		case F_WRLCK:
618			if ((fp->f_flag & FWRITE) == 0) {
619				error = EBADF;
620				break;
621			}
622			PROC_LOCK(p->p_leader);
623			p->p_leader->p_flag |= P_ADVLOCK;
624			PROC_UNLOCK(p->p_leader);
625			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
626			    flp, flg);
627			break;
628		case F_UNLCK:
629			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
630			    flp, flg);
631			break;
632		case F_UNLCKSYS:
633			/*
634			 * Temporary api for testing remote lock
635			 * infrastructure.
636			 */
637			if (flg != F_REMOTE) {
638				error = EINVAL;
639				break;
640			}
641			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
642			    F_UNLCKSYS, flp, flg);
643			break;
644		default:
645			error = EINVAL;
646			break;
647		}
648		VFS_UNLOCK_GIANT(vfslocked);
649		vfslocked = 0;
650		/* Check for race with close */
651		FILEDESC_SLOCK(fdp);
652		if ((unsigned) fd >= fdp->fd_nfiles ||
653		    fp != fdp->fd_ofiles[fd]) {
654			FILEDESC_SUNLOCK(fdp);
655			flp->l_whence = SEEK_SET;
656			flp->l_start = 0;
657			flp->l_len = 0;
658			flp->l_type = F_UNLCK;
659			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
660			(void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
661					   F_UNLCK, flp, F_POSIX);
662			VFS_UNLOCK_GIANT(vfslocked);
663			vfslocked = 0;
664		} else
665			FILEDESC_SUNLOCK(fdp);
666		fdrop(fp, td);
667		break;
668
669	case F_GETLK:
670		FILEDESC_SLOCK(fdp);
671		if ((fp = fdtofp(fd, fdp)) == NULL) {
672			FILEDESC_SUNLOCK(fdp);
673			error = EBADF;
674			break;
675		}
676		if (fp->f_type != DTYPE_VNODE) {
677			FILEDESC_SUNLOCK(fdp);
678			error = EBADF;
679			break;
680		}
681		flp = (struct flock *)arg;
682		if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
683		    flp->l_type != F_UNLCK) {
684			FILEDESC_SUNLOCK(fdp);
685			error = EINVAL;
686			break;
687		}
688		if (flp->l_whence == SEEK_CUR) {
689			if ((flp->l_start > 0 &&
690			    fp->f_offset > OFF_MAX - flp->l_start) ||
691			    (flp->l_start < 0 &&
692			     fp->f_offset < OFF_MIN - flp->l_start)) {
693				FILEDESC_SUNLOCK(fdp);
694				error = EOVERFLOW;
695				break;
696			}
697			flp->l_start += fp->f_offset;
698		}
699		/*
700		 * VOP_ADVLOCK() may block.
701		 */
702		fhold(fp);
703		FILEDESC_SUNLOCK(fdp);
704		vp = fp->f_vnode;
705		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
706		error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
707		    F_POSIX);
708		VFS_UNLOCK_GIANT(vfslocked);
709		vfslocked = 0;
710		fdrop(fp, td);
711		break;
712
713	case F_RDAHEAD:
714		arg = arg ? 128 * 1024: 0;
715		/* FALLTHROUGH */
716	case F_READAHEAD:
717		FILEDESC_SLOCK(fdp);
718		if ((fp = fdtofp(fd, fdp)) == NULL) {
719			FILEDESC_SUNLOCK(fdp);
720			error = EBADF;
721			break;
722		}
723		if (fp->f_type != DTYPE_VNODE) {
724			FILEDESC_SUNLOCK(fdp);
725			error = EBADF;
726			break;
727		}
728		fhold(fp);
729		FILEDESC_SUNLOCK(fdp);
730		if (arg != 0) {
731			vp = fp->f_vnode;
732			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
733			error = vn_lock(vp, LK_SHARED);
734			if (error != 0)
735				goto readahead_vnlock_fail;
736			bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
737			VOP_UNLOCK(vp, 0);
738			fp->f_seqcount = (arg + bsize - 1) / bsize;
739			do {
740				new = old = fp->f_flag;
741				new |= FRDAHEAD;
742			} while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
743readahead_vnlock_fail:
744			VFS_UNLOCK_GIANT(vfslocked);
745			vfslocked = 0;
746		} else {
747			do {
748				new = old = fp->f_flag;
749				new &= ~FRDAHEAD;
750			} while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
751		}
752		fdrop(fp, td);
753		break;
754
755	default:
756		error = EINVAL;
757		break;
758	}
759	VFS_UNLOCK_GIANT(vfslocked);
760	return (error);
761}
762
763/*
764 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
765 */
766static int
767do_dup(struct thread *td, int flags, int old, int new,
768    register_t *retval)
769{
770	struct filedesc *fdp;
771	struct proc *p;
772	struct file *fp;
773	struct file *delfp;
774	int error, holdleaders, maxfd;
775
776	p = td->td_proc;
777	fdp = p->p_fd;
778
779	/*
780	 * Verify we have a valid descriptor to dup from and possibly to
781	 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
782	 * return EINVAL when the new descriptor is out of bounds.
783	 */
784	if (old < 0)
785		return (EBADF);
786	if (new < 0)
787		return (flags & DUP_FCNTL ? EINVAL : EBADF);
788	PROC_LOCK(p);
789	maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
790	PROC_UNLOCK(p);
791	if (new >= maxfd)
792		return (flags & DUP_FCNTL ? EINVAL : EMFILE);
793
794	FILEDESC_XLOCK(fdp);
795	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
796		FILEDESC_XUNLOCK(fdp);
797		return (EBADF);
798	}
799	if (flags & DUP_FIXED && old == new) {
800		*retval = new;
801		FILEDESC_XUNLOCK(fdp);
802		return (0);
803	}
804	fp = fdp->fd_ofiles[old];
805	fhold(fp);
806
807	/*
808	 * If the caller specified a file descriptor, make sure the file
809	 * table is large enough to hold it, and grab it.  Otherwise, just
810	 * allocate a new descriptor the usual way.  Since the filedesc
811	 * lock may be temporarily dropped in the process, we have to look
812	 * out for a race.
813	 */
814	if (flags & DUP_FIXED) {
815		if (new >= fdp->fd_nfiles) {
816			/*
817			 * The resource limits are here instead of e.g. fdalloc(),
818			 * because the file descriptor table may be shared between
819			 * processes, so we can't really use racct_add()/racct_sub().
820			 * Instead of counting the number of actually allocated
821			 * descriptors, just put the limit on the size of the file
822			 * descriptor table.
823			 */
824#ifdef RACCT
825			PROC_LOCK(p);
826			error = racct_set(p, RACCT_NOFILE, new + 1);
827			PROC_UNLOCK(p);
828			if (error != 0) {
829				FILEDESC_XUNLOCK(fdp);
830				fdrop(fp, td);
831				return (EMFILE);
832			}
833#endif
834			fdgrowtable(fdp, new + 1);
835		}
836		if (fdp->fd_ofiles[new] == NULL)
837			fdused(fdp, new);
838	} else {
839		if ((error = fdalloc(td, new, &new)) != 0) {
840			FILEDESC_XUNLOCK(fdp);
841			fdrop(fp, td);
842			return (error);
843		}
844	}
845
846	/*
847	 * If the old file changed out from under us then treat it as a
848	 * bad file descriptor.  Userland should do its own locking to
849	 * avoid this case.
850	 */
851	if (fdp->fd_ofiles[old] != fp) {
852		/* we've allocated a descriptor which we won't use */
853		if (fdp->fd_ofiles[new] == NULL)
854			fdunused(fdp, new);
855		FILEDESC_XUNLOCK(fdp);
856		fdrop(fp, td);
857		return (EBADF);
858	}
859	KASSERT(old != new,
860	    ("new fd is same as old"));
861
862	/*
863	 * Save info on the descriptor being overwritten.  We cannot close
864	 * it without introducing an ownership race for the slot, since we
865	 * need to drop the filedesc lock to call closef().
866	 *
867	 * XXX this duplicates parts of close().
868	 */
869	delfp = fdp->fd_ofiles[new];
870	holdleaders = 0;
871	if (delfp != NULL) {
872		if (td->td_proc->p_fdtol != NULL) {
873			/*
874			 * Ask fdfree() to sleep to ensure that all relevant
875			 * process leaders can be traversed in closef().
876			 */
877			fdp->fd_holdleaderscount++;
878			holdleaders = 1;
879		}
880	}
881
882	/*
883	 * Duplicate the source descriptor
884	 */
885	fdp->fd_ofiles[new] = fp;
886	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
887	if (new > fdp->fd_lastfile)
888		fdp->fd_lastfile = new;
889	*retval = new;
890
891	/*
892	 * If we dup'd over a valid file, we now own the reference to it
893	 * and must dispose of it using closef() semantics (as if a
894	 * close() were performed on it).
895	 *
896	 * XXX this duplicates parts of close().
897	 */
898	if (delfp != NULL) {
899		knote_fdclose(td, new);
900		if (delfp->f_type == DTYPE_MQUEUE)
901			mq_fdclose(td, new, delfp);
902		FILEDESC_XUNLOCK(fdp);
903		(void) closef(delfp, td);
904		if (holdleaders) {
905			FILEDESC_XLOCK(fdp);
906			fdp->fd_holdleaderscount--;
907			if (fdp->fd_holdleaderscount == 0 &&
908			    fdp->fd_holdleaderswakeup != 0) {
909				fdp->fd_holdleaderswakeup = 0;
910				wakeup(&fdp->fd_holdleaderscount);
911			}
912			FILEDESC_XUNLOCK(fdp);
913		}
914	} else {
915		FILEDESC_XUNLOCK(fdp);
916	}
917	return (0);
918}
919
920/*
921 * If sigio is on the list associated with a process or process group,
922 * disable signalling from the device, remove sigio from the list and
923 * free sigio.
924 */
925void
926funsetown(struct sigio **sigiop)
927{
928	struct sigio *sigio;
929
930	SIGIO_LOCK();
931	sigio = *sigiop;
932	if (sigio == NULL) {
933		SIGIO_UNLOCK();
934		return;
935	}
936	*(sigio->sio_myref) = NULL;
937	if ((sigio)->sio_pgid < 0) {
938		struct pgrp *pg = (sigio)->sio_pgrp;
939		PGRP_LOCK(pg);
940		SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
941			     sigio, sio_pgsigio);
942		PGRP_UNLOCK(pg);
943	} else {
944		struct proc *p = (sigio)->sio_proc;
945		PROC_LOCK(p);
946		SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
947			     sigio, sio_pgsigio);
948		PROC_UNLOCK(p);
949	}
950	SIGIO_UNLOCK();
951	crfree(sigio->sio_ucred);
952	free(sigio, M_SIGIO);
953}
954
955/*
956 * Free a list of sigio structures.
957 * We only need to lock the SIGIO_LOCK because we have made ourselves
958 * inaccessible to callers of fsetown and therefore do not need to lock
959 * the proc or pgrp struct for the list manipulation.
960 */
961void
962funsetownlst(struct sigiolst *sigiolst)
963{
964	struct proc *p;
965	struct pgrp *pg;
966	struct sigio *sigio;
967
968	sigio = SLIST_FIRST(sigiolst);
969	if (sigio == NULL)
970		return;
971	p = NULL;
972	pg = NULL;
973
974	/*
975	 * Every entry of the list should belong
976	 * to a single proc or pgrp.
977	 */
978	if (sigio->sio_pgid < 0) {
979		pg = sigio->sio_pgrp;
980		PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
981	} else /* if (sigio->sio_pgid > 0) */ {
982		p = sigio->sio_proc;
983		PROC_LOCK_ASSERT(p, MA_NOTOWNED);
984	}
985
986	SIGIO_LOCK();
987	while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
988		*(sigio->sio_myref) = NULL;
989		if (pg != NULL) {
990			KASSERT(sigio->sio_pgid < 0,
991			    ("Proc sigio in pgrp sigio list"));
992			KASSERT(sigio->sio_pgrp == pg,
993			    ("Bogus pgrp in sigio list"));
994			PGRP_LOCK(pg);
995			SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
996			    sio_pgsigio);
997			PGRP_UNLOCK(pg);
998		} else /* if (p != NULL) */ {
999			KASSERT(sigio->sio_pgid > 0,
1000			    ("Pgrp sigio in proc sigio list"));
1001			KASSERT(sigio->sio_proc == p,
1002			    ("Bogus proc in sigio list"));
1003			PROC_LOCK(p);
1004			SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
1005			    sio_pgsigio);
1006			PROC_UNLOCK(p);
1007		}
1008		SIGIO_UNLOCK();
1009		crfree(sigio->sio_ucred);
1010		free(sigio, M_SIGIO);
1011		SIGIO_LOCK();
1012	}
1013	SIGIO_UNLOCK();
1014}
1015
1016/*
1017 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
1018 *
1019 * After permission checking, add a sigio structure to the sigio list for
1020 * the process or process group.
1021 */
1022int
1023fsetown(pid_t pgid, struct sigio **sigiop)
1024{
1025	struct proc *proc;
1026	struct pgrp *pgrp;
1027	struct sigio *sigio;
1028	int ret;
1029
1030	if (pgid == 0) {
1031		funsetown(sigiop);
1032		return (0);
1033	}
1034
1035	ret = 0;
1036
1037	/* Allocate and fill in the new sigio out of locks. */
1038	sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
1039	sigio->sio_pgid = pgid;
1040	sigio->sio_ucred = crhold(curthread->td_ucred);
1041	sigio->sio_myref = sigiop;
1042
1043	sx_slock(&proctree_lock);
1044	if (pgid > 0) {
1045		proc = pfind(pgid);
1046		if (proc == NULL) {
1047			ret = ESRCH;
1048			goto fail;
1049		}
1050
1051		/*
1052		 * Policy - Don't allow a process to FSETOWN a process
1053		 * in another session.
1054		 *
1055		 * Remove this test to allow maximum flexibility or
1056		 * restrict FSETOWN to the current process or process
1057		 * group for maximum safety.
1058		 */
1059		PROC_UNLOCK(proc);
1060		if (proc->p_session != curthread->td_proc->p_session) {
1061			ret = EPERM;
1062			goto fail;
1063		}
1064
1065		pgrp = NULL;
1066	} else /* if (pgid < 0) */ {
1067		pgrp = pgfind(-pgid);
1068		if (pgrp == NULL) {
1069			ret = ESRCH;
1070			goto fail;
1071		}
1072		PGRP_UNLOCK(pgrp);
1073
1074		/*
1075		 * Policy - Don't allow a process to FSETOWN a process
1076		 * in another session.
1077		 *
1078		 * Remove this test to allow maximum flexibility or
1079		 * restrict FSETOWN to the current process or process
1080		 * group for maximum safety.
1081		 */
1082		if (pgrp->pg_session != curthread->td_proc->p_session) {
1083			ret = EPERM;
1084			goto fail;
1085		}
1086
1087		proc = NULL;
1088	}
1089	funsetown(sigiop);
1090	if (pgid > 0) {
1091		PROC_LOCK(proc);
1092		/*
1093		 * Since funsetownlst() is called without the proctree
1094		 * locked, we need to check for P_WEXIT.
1095		 * XXX: is ESRCH correct?
1096		 */
1097		if ((proc->p_flag & P_WEXIT) != 0) {
1098			PROC_UNLOCK(proc);
1099			ret = ESRCH;
1100			goto fail;
1101		}
1102		SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1103		sigio->sio_proc = proc;
1104		PROC_UNLOCK(proc);
1105	} else {
1106		PGRP_LOCK(pgrp);
1107		SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1108		sigio->sio_pgrp = pgrp;
1109		PGRP_UNLOCK(pgrp);
1110	}
1111	sx_sunlock(&proctree_lock);
1112	SIGIO_LOCK();
1113	*sigiop = sigio;
1114	SIGIO_UNLOCK();
1115	return (0);
1116
1117fail:
1118	sx_sunlock(&proctree_lock);
1119	crfree(sigio->sio_ucred);
1120	free(sigio, M_SIGIO);
1121	return (ret);
1122}
1123
1124/*
1125 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1126 */
1127pid_t
1128fgetown(sigiop)
1129	struct sigio **sigiop;
1130{
1131	pid_t pgid;
1132
1133	SIGIO_LOCK();
1134	pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1135	SIGIO_UNLOCK();
1136	return (pgid);
1137}
1138
1139/*
1140 * Close a file descriptor.
1141 */
1142#ifndef _SYS_SYSPROTO_H_
1143struct close_args {
1144	int     fd;
1145};
1146#endif
1147/* ARGSUSED */
1148int
1149close(td, uap)
1150	struct thread *td;
1151	struct close_args *uap;
1152{
1153
1154	return (kern_close(td, uap->fd));
1155}
1156
1157int
1158kern_close(td, fd)
1159	struct thread *td;
1160	int fd;
1161{
1162	struct filedesc *fdp;
1163	struct file *fp;
1164	int error;
1165	int holdleaders;
1166
1167	error = 0;
1168	holdleaders = 0;
1169	fdp = td->td_proc->p_fd;
1170
1171	AUDIT_SYSCLOSE(td, fd);
1172
1173	FILEDESC_XLOCK(fdp);
1174	if ((unsigned)fd >= fdp->fd_nfiles ||
1175	    (fp = fdp->fd_ofiles[fd]) == NULL) {
1176		FILEDESC_XUNLOCK(fdp);
1177		return (EBADF);
1178	}
1179	fdp->fd_ofiles[fd] = NULL;
1180	fdp->fd_ofileflags[fd] = 0;
1181	fdunused(fdp, fd);
1182	if (td->td_proc->p_fdtol != NULL) {
1183		/*
1184		 * Ask fdfree() to sleep to ensure that all relevant
1185		 * process leaders can be traversed in closef().
1186		 */
1187		fdp->fd_holdleaderscount++;
1188		holdleaders = 1;
1189	}
1190
1191	/*
1192	 * We now hold the fp reference that used to be owned by the
1193	 * descriptor array.  We have to unlock the FILEDESC *AFTER*
1194	 * knote_fdclose to prevent a race of the fd getting opened, a knote
1195	 * added, and deleteing a knote for the new fd.
1196	 */
1197	knote_fdclose(td, fd);
1198	if (fp->f_type == DTYPE_MQUEUE)
1199		mq_fdclose(td, fd, fp);
1200	FILEDESC_XUNLOCK(fdp);
1201
1202	error = closef(fp, td);
1203	if (holdleaders) {
1204		FILEDESC_XLOCK(fdp);
1205		fdp->fd_holdleaderscount--;
1206		if (fdp->fd_holdleaderscount == 0 &&
1207		    fdp->fd_holdleaderswakeup != 0) {
1208			fdp->fd_holdleaderswakeup = 0;
1209			wakeup(&fdp->fd_holdleaderscount);
1210		}
1211		FILEDESC_XUNLOCK(fdp);
1212	}
1213	return (error);
1214}
1215
1216/*
1217 * Close open file descriptors.
1218 */
1219#ifndef _SYS_SYSPROTO_H_
1220struct closefrom_args {
1221	int	lowfd;
1222};
1223#endif
1224/* ARGSUSED */
1225int
1226closefrom(struct thread *td, struct closefrom_args *uap)
1227{
1228	struct filedesc *fdp;
1229	int fd;
1230
1231	fdp = td->td_proc->p_fd;
1232	AUDIT_ARG_FD(uap->lowfd);
1233
1234	/*
1235	 * Treat negative starting file descriptor values identical to
1236	 * closefrom(0) which closes all files.
1237	 */
1238	if (uap->lowfd < 0)
1239		uap->lowfd = 0;
1240	FILEDESC_SLOCK(fdp);
1241	for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1242		if (fdp->fd_ofiles[fd] != NULL) {
1243			FILEDESC_SUNLOCK(fdp);
1244			(void)kern_close(td, fd);
1245			FILEDESC_SLOCK(fdp);
1246		}
1247	}
1248	FILEDESC_SUNLOCK(fdp);
1249	return (0);
1250}
1251
1252#if defined(COMPAT_43)
1253/*
1254 * Return status information about a file descriptor.
1255 */
1256#ifndef _SYS_SYSPROTO_H_
1257struct ofstat_args {
1258	int	fd;
1259	struct	ostat *sb;
1260};
1261#endif
1262/* ARGSUSED */
1263int
1264ofstat(struct thread *td, struct ofstat_args *uap)
1265{
1266	struct ostat oub;
1267	struct stat ub;
1268	int error;
1269
1270	error = kern_fstat(td, uap->fd, &ub);
1271	if (error == 0) {
1272		cvtstat(&ub, &oub);
1273		error = copyout(&oub, uap->sb, sizeof(oub));
1274	}
1275	return (error);
1276}
1277#endif /* COMPAT_43 */
1278
1279/*
1280 * Return status information about a file descriptor.
1281 */
1282#ifndef _SYS_SYSPROTO_H_
1283struct fstat_args {
1284	int	fd;
1285	struct	stat *sb;
1286};
1287#endif
1288/* ARGSUSED */
1289int
1290fstat(struct thread *td, struct fstat_args *uap)
1291{
1292	struct stat ub;
1293	int error;
1294
1295	error = kern_fstat(td, uap->fd, &ub);
1296	if (error == 0)
1297		error = copyout(&ub, uap->sb, sizeof(ub));
1298	return (error);
1299}
1300
1301int
1302kern_fstat(struct thread *td, int fd, struct stat *sbp)
1303{
1304	struct file *fp;
1305	int error;
1306
1307	AUDIT_ARG_FD(fd);
1308
1309	if ((error = fget(td, fd, &fp)) != 0)
1310		return (error);
1311
1312	AUDIT_ARG_FILE(td->td_proc, fp);
1313
1314	error = fo_stat(fp, sbp, td->td_ucred, td);
1315	fdrop(fp, td);
1316#ifdef KTRACE
1317	if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1318		ktrstat(sbp);
1319#endif
1320	return (error);
1321}
1322
1323/*
1324 * Return status information about a file descriptor.
1325 */
1326#ifndef _SYS_SYSPROTO_H_
1327struct nfstat_args {
1328	int	fd;
1329	struct	nstat *sb;
1330};
1331#endif
1332/* ARGSUSED */
1333int
1334nfstat(struct thread *td, struct nfstat_args *uap)
1335{
1336	struct nstat nub;
1337	struct stat ub;
1338	int error;
1339
1340	error = kern_fstat(td, uap->fd, &ub);
1341	if (error == 0) {
1342		cvtnstat(&ub, &nub);
1343		error = copyout(&nub, uap->sb, sizeof(nub));
1344	}
1345	return (error);
1346}
1347
1348/*
1349 * Return pathconf information about a file descriptor.
1350 */
1351#ifndef _SYS_SYSPROTO_H_
1352struct fpathconf_args {
1353	int	fd;
1354	int	name;
1355};
1356#endif
1357/* ARGSUSED */
1358int
1359fpathconf(struct thread *td, struct fpathconf_args *uap)
1360{
1361	struct file *fp;
1362	struct vnode *vp;
1363	int error;
1364
1365	if ((error = fget(td, uap->fd, &fp)) != 0)
1366		return (error);
1367
1368	/* If asynchronous I/O is available, it works for all descriptors. */
1369	if (uap->name == _PC_ASYNC_IO) {
1370		td->td_retval[0] = async_io_version;
1371		goto out;
1372	}
1373	vp = fp->f_vnode;
1374	if (vp != NULL) {
1375		int vfslocked;
1376		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1377		vn_lock(vp, LK_SHARED | LK_RETRY);
1378		error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1379		VOP_UNLOCK(vp, 0);
1380		VFS_UNLOCK_GIANT(vfslocked);
1381	} else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1382		if (uap->name != _PC_PIPE_BUF) {
1383			error = EINVAL;
1384		} else {
1385			td->td_retval[0] = PIPE_BUF;
1386		error = 0;
1387		}
1388	} else {
1389		error = EOPNOTSUPP;
1390	}
1391out:
1392	fdrop(fp, td);
1393	return (error);
1394}
1395
1396/*
1397 * Grow the file table to accomodate (at least) nfd descriptors.  This may
1398 * block and drop the filedesc lock, but it will reacquire it before
1399 * returning.
1400 */
1401static void
1402fdgrowtable(struct filedesc *fdp, int nfd)
1403{
1404	struct filedesc0 *fdp0;
1405	struct freetable *fo;
1406	struct file **ntable;
1407	struct file **otable;
1408	char *nfileflags;
1409	int nnfiles, onfiles;
1410	NDSLOTTYPE *nmap;
1411
1412	FILEDESC_XLOCK_ASSERT(fdp);
1413
1414	KASSERT(fdp->fd_nfiles > 0,
1415	    ("zero-length file table"));
1416
1417	/* compute the size of the new table */
1418	onfiles = fdp->fd_nfiles;
1419	nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1420	if (nnfiles <= onfiles)
1421		/* the table is already large enough */
1422		return;
1423
1424	/* allocate a new table and (if required) new bitmaps */
1425	FILEDESC_XUNLOCK(fdp);
1426	ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable),
1427	    M_FILEDESC, M_ZERO | M_WAITOK);
1428	nfileflags = (char *)&ntable[nnfiles];
1429	if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1430		nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE,
1431		    M_FILEDESC, M_ZERO | M_WAITOK);
1432	else
1433		nmap = NULL;
1434	FILEDESC_XLOCK(fdp);
1435
1436	/*
1437	 * We now have new tables ready to go.  Since we dropped the
1438	 * filedesc lock to call malloc(), watch out for a race.
1439	 */
1440	onfiles = fdp->fd_nfiles;
1441	if (onfiles >= nnfiles) {
1442		/* we lost the race, but that's OK */
1443		free(ntable, M_FILEDESC);
1444		if (nmap != NULL)
1445			free(nmap, M_FILEDESC);
1446		return;
1447	}
1448	bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1449	bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1450	otable = fdp->fd_ofiles;
1451	fdp->fd_ofileflags = nfileflags;
1452	fdp->fd_ofiles = ntable;
1453	/*
1454	 * We must preserve ofiles until the process exits because we can't
1455	 * be certain that no threads have references to the old table via
1456	 * _fget().
1457	 */
1458	if (onfiles > NDFILE) {
1459		fo = (struct freetable *)&otable[onfiles];
1460		fdp0 = (struct filedesc0 *)fdp;
1461		fo->ft_table = otable;
1462		SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next);
1463	}
1464	if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1465		bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1466		if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1467			free(fdp->fd_map, M_FILEDESC);
1468		fdp->fd_map = nmap;
1469	}
1470	fdp->fd_nfiles = nnfiles;
1471}
1472
1473/*
1474 * Allocate a file descriptor for the process.
1475 */
1476int
1477fdalloc(struct thread *td, int minfd, int *result)
1478{
1479	struct proc *p = td->td_proc;
1480	struct filedesc *fdp = p->p_fd;
1481	int fd = -1, maxfd;
1482#ifdef RACCT
1483	int error;
1484#endif
1485
1486	FILEDESC_XLOCK_ASSERT(fdp);
1487
1488	if (fdp->fd_freefile > minfd)
1489		minfd = fdp->fd_freefile;
1490
1491	PROC_LOCK(p);
1492	maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1493	PROC_UNLOCK(p);
1494
1495	/*
1496	 * Search the bitmap for a free descriptor.  If none is found, try
1497	 * to grow the file table.  Keep at it until we either get a file
1498	 * descriptor or run into process or system limits; fdgrowtable()
1499	 * may drop the filedesc lock, so we're in a race.
1500	 */
1501	for (;;) {
1502		fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1503		if (fd >= maxfd)
1504			return (EMFILE);
1505		if (fd < fdp->fd_nfiles)
1506			break;
1507#ifdef RACCT
1508		PROC_LOCK(p);
1509		error = racct_set(p, RACCT_NOFILE, min(fdp->fd_nfiles * 2, maxfd));
1510		PROC_UNLOCK(p);
1511		if (error != 0)
1512			return (EMFILE);
1513#endif
1514		fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
1515	}
1516
1517	/*
1518	 * Perform some sanity checks, then mark the file descriptor as
1519	 * used and return it to the caller.
1520	 */
1521	KASSERT(!fdisused(fdp, fd),
1522	    ("fd_first_free() returned non-free descriptor"));
1523	KASSERT(fdp->fd_ofiles[fd] == NULL,
1524	    ("free descriptor isn't"));
1525	fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
1526	fdused(fdp, fd);
1527	*result = fd;
1528	return (0);
1529}
1530
1531/*
1532 * Check to see whether n user file descriptors are available to the process
1533 * p.
1534 */
1535int
1536fdavail(struct thread *td, int n)
1537{
1538	struct proc *p = td->td_proc;
1539	struct filedesc *fdp = td->td_proc->p_fd;
1540	struct file **fpp;
1541	int i, lim, last;
1542
1543	FILEDESC_LOCK_ASSERT(fdp);
1544
1545	/*
1546	 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1547	 *      call racct_add() from there instead of dealing with containers
1548	 *      here.
1549	 */
1550	PROC_LOCK(p);
1551	lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1552	PROC_UNLOCK(p);
1553	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1554		return (1);
1555	last = min(fdp->fd_nfiles, lim);
1556	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1557	for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1558		if (*fpp == NULL && --n <= 0)
1559			return (1);
1560	}
1561	return (0);
1562}
1563
1564/*
1565 * Create a new open file structure and allocate a file decriptor for the
1566 * process that refers to it.  We add one reference to the file for the
1567 * descriptor table and one reference for resultfp. This is to prevent us
1568 * being preempted and the entry in the descriptor table closed after we
1569 * release the FILEDESC lock.
1570 */
1571int
1572falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1573{
1574	struct file *fp;
1575	int error, fd;
1576
1577	error = falloc_noinstall(td, &fp);
1578	if (error)
1579		return (error);		/* no reference held on error */
1580
1581	error = finstall(td, fp, &fd, flags);
1582	if (error) {
1583		fdrop(fp, td);		/* one reference (fp only) */
1584		return (error);
1585	}
1586
1587	if (resultfp != NULL)
1588		*resultfp = fp;		/* copy out result */
1589	else
1590		fdrop(fp, td);		/* release local reference */
1591
1592	if (resultfd != NULL)
1593		*resultfd = fd;
1594
1595	return (0);
1596}
1597
1598/*
1599 * Create a new open file structure without allocating a file descriptor.
1600 */
1601int
1602falloc_noinstall(struct thread *td, struct file **resultfp)
1603{
1604	struct file *fp;
1605	int maxuserfiles = maxfiles - (maxfiles / 20);
1606	static struct timeval lastfail;
1607	static int curfail;
1608
1609	KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1610
1611	if ((openfiles >= maxuserfiles &&
1612	    priv_check(td, PRIV_MAXFILES) != 0) ||
1613	    openfiles >= maxfiles) {
1614		if (ppsratecheck(&lastfail, &curfail, 1)) {
1615			printf("kern.maxfiles limit exceeded by uid %i, "
1616			    "please see tuning(7).\n", td->td_ucred->cr_ruid);
1617		}
1618		return (ENFILE);
1619	}
1620	atomic_add_int(&openfiles, 1);
1621	fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1622	refcount_init(&fp->f_count, 1);
1623	fp->f_cred = crhold(td->td_ucred);
1624	fp->f_ops = &badfileops;
1625	fp->f_data = NULL;
1626	fp->f_vnode = NULL;
1627	*resultfp = fp;
1628	return (0);
1629}
1630
1631/*
1632 * Install a file in a file descriptor table.
1633 */
1634int
1635finstall(struct thread *td, struct file *fp, int *fd, int flags)
1636{
1637	struct filedesc *fdp = td->td_proc->p_fd;
1638	int error;
1639
1640	KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1641	KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1642
1643	FILEDESC_XLOCK(fdp);
1644	if ((error = fdalloc(td, 0, fd))) {
1645		FILEDESC_XUNLOCK(fdp);
1646		return (error);
1647	}
1648	fhold(fp);
1649	fdp->fd_ofiles[*fd] = fp;
1650	if ((flags & O_CLOEXEC) != 0)
1651		fdp->fd_ofileflags[*fd] |= UF_EXCLOSE;
1652	FILEDESC_XUNLOCK(fdp);
1653	return (0);
1654}
1655
1656/*
1657 * Build a new filedesc structure from another.
1658 * Copy the current, root, and jail root vnode references.
1659 */
1660struct filedesc *
1661fdinit(struct filedesc *fdp)
1662{
1663	struct filedesc0 *newfdp;
1664
1665	newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1666	FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1667	if (fdp != NULL) {
1668		FILEDESC_XLOCK(fdp);
1669		newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1670		if (newfdp->fd_fd.fd_cdir)
1671			VREF(newfdp->fd_fd.fd_cdir);
1672		newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1673		if (newfdp->fd_fd.fd_rdir)
1674			VREF(newfdp->fd_fd.fd_rdir);
1675		newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1676		if (newfdp->fd_fd.fd_jdir)
1677			VREF(newfdp->fd_fd.fd_jdir);
1678		FILEDESC_XUNLOCK(fdp);
1679	}
1680
1681	/* Create the file descriptor table. */
1682	newfdp->fd_fd.fd_refcnt = 1;
1683	newfdp->fd_fd.fd_holdcnt = 1;
1684	newfdp->fd_fd.fd_cmask = CMASK;
1685	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1686	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1687	newfdp->fd_fd.fd_nfiles = NDFILE;
1688	newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1689	newfdp->fd_fd.fd_lastfile = -1;
1690	return (&newfdp->fd_fd);
1691}
1692
1693static struct filedesc *
1694fdhold(struct proc *p)
1695{
1696	struct filedesc *fdp;
1697
1698	mtx_lock(&fdesc_mtx);
1699	fdp = p->p_fd;
1700	if (fdp != NULL)
1701		fdp->fd_holdcnt++;
1702	mtx_unlock(&fdesc_mtx);
1703	return (fdp);
1704}
1705
1706static void
1707fddrop(struct filedesc *fdp)
1708{
1709	struct filedesc0 *fdp0;
1710	struct freetable *ft;
1711	int i;
1712
1713	mtx_lock(&fdesc_mtx);
1714	i = --fdp->fd_holdcnt;
1715	mtx_unlock(&fdesc_mtx);
1716	if (i > 0)
1717		return;
1718
1719	FILEDESC_LOCK_DESTROY(fdp);
1720	fdp0 = (struct filedesc0 *)fdp;
1721	while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1722		SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1723		free(ft->ft_table, M_FILEDESC);
1724	}
1725	free(fdp, M_FILEDESC);
1726}
1727
1728/*
1729 * Share a filedesc structure.
1730 */
1731struct filedesc *
1732fdshare(struct filedesc *fdp)
1733{
1734
1735	FILEDESC_XLOCK(fdp);
1736	fdp->fd_refcnt++;
1737	FILEDESC_XUNLOCK(fdp);
1738	return (fdp);
1739}
1740
1741/*
1742 * Unshare a filedesc structure, if necessary by making a copy
1743 */
1744void
1745fdunshare(struct proc *p, struct thread *td)
1746{
1747
1748	FILEDESC_XLOCK(p->p_fd);
1749	if (p->p_fd->fd_refcnt > 1) {
1750		struct filedesc *tmp;
1751
1752		FILEDESC_XUNLOCK(p->p_fd);
1753		tmp = fdcopy(p->p_fd);
1754		fdfree(td);
1755		p->p_fd = tmp;
1756	} else
1757		FILEDESC_XUNLOCK(p->p_fd);
1758}
1759
1760/*
1761 * Copy a filedesc structure.  A NULL pointer in returns a NULL reference,
1762 * this is to ease callers, not catch errors.
1763 */
1764struct filedesc *
1765fdcopy(struct filedesc *fdp)
1766{
1767	struct filedesc *newfdp;
1768	int i;
1769
1770	/* Certain daemons might not have file descriptors. */
1771	if (fdp == NULL)
1772		return (NULL);
1773
1774	newfdp = fdinit(fdp);
1775	FILEDESC_SLOCK(fdp);
1776	while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1777		FILEDESC_SUNLOCK(fdp);
1778		FILEDESC_XLOCK(newfdp);
1779		fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1780		FILEDESC_XUNLOCK(newfdp);
1781		FILEDESC_SLOCK(fdp);
1782	}
1783	/* copy all passable descriptors (i.e. not kqueue) */
1784	newfdp->fd_freefile = -1;
1785	for (i = 0; i <= fdp->fd_lastfile; ++i) {
1786		if (fdisused(fdp, i) &&
1787		    (fdp->fd_ofiles[i]->f_ops->fo_flags & DFLAG_PASSABLE) &&
1788		    fdp->fd_ofiles[i]->f_ops != &badfileops) {
1789			newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1790			newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1791			fhold(newfdp->fd_ofiles[i]);
1792			newfdp->fd_lastfile = i;
1793		} else {
1794			if (newfdp->fd_freefile == -1)
1795				newfdp->fd_freefile = i;
1796		}
1797	}
1798	newfdp->fd_cmask = fdp->fd_cmask;
1799	FILEDESC_SUNLOCK(fdp);
1800	FILEDESC_XLOCK(newfdp);
1801	for (i = 0; i <= newfdp->fd_lastfile; ++i)
1802		if (newfdp->fd_ofiles[i] != NULL)
1803			fdused(newfdp, i);
1804	if (newfdp->fd_freefile == -1)
1805		newfdp->fd_freefile = i;
1806	FILEDESC_XUNLOCK(newfdp);
1807	return (newfdp);
1808}
1809
1810/*
1811 * Release a filedesc structure.
1812 */
1813void
1814fdfree(struct thread *td)
1815{
1816	struct filedesc *fdp;
1817	struct file **fpp;
1818	int i, locked;
1819	struct filedesc_to_leader *fdtol;
1820	struct file *fp;
1821	struct vnode *cdir, *jdir, *rdir, *vp;
1822	struct flock lf;
1823
1824	/* Certain daemons might not have file descriptors. */
1825	fdp = td->td_proc->p_fd;
1826	if (fdp == NULL)
1827		return;
1828
1829#ifdef RACCT
1830	PROC_LOCK(td->td_proc);
1831	racct_set(td->td_proc, RACCT_NOFILE, 0);
1832	PROC_UNLOCK(td->td_proc);
1833#endif
1834
1835	/* Check for special need to clear POSIX style locks */
1836	fdtol = td->td_proc->p_fdtol;
1837	if (fdtol != NULL) {
1838		FILEDESC_XLOCK(fdp);
1839		KASSERT(fdtol->fdl_refcount > 0,
1840			("filedesc_to_refcount botch: fdl_refcount=%d",
1841			 fdtol->fdl_refcount));
1842		if (fdtol->fdl_refcount == 1 &&
1843		    (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1844			for (i = 0, fpp = fdp->fd_ofiles;
1845			     i <= fdp->fd_lastfile;
1846			     i++, fpp++) {
1847				if (*fpp == NULL ||
1848				    (*fpp)->f_type != DTYPE_VNODE)
1849					continue;
1850				fp = *fpp;
1851				fhold(fp);
1852				FILEDESC_XUNLOCK(fdp);
1853				lf.l_whence = SEEK_SET;
1854				lf.l_start = 0;
1855				lf.l_len = 0;
1856				lf.l_type = F_UNLCK;
1857				vp = fp->f_vnode;
1858				locked = VFS_LOCK_GIANT(vp->v_mount);
1859				(void) VOP_ADVLOCK(vp,
1860						   (caddr_t)td->td_proc->
1861						   p_leader,
1862						   F_UNLCK,
1863						   &lf,
1864						   F_POSIX);
1865				VFS_UNLOCK_GIANT(locked);
1866				FILEDESC_XLOCK(fdp);
1867				fdrop(fp, td);
1868				fpp = fdp->fd_ofiles + i;
1869			}
1870		}
1871	retry:
1872		if (fdtol->fdl_refcount == 1) {
1873			if (fdp->fd_holdleaderscount > 0 &&
1874			    (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1875				/*
1876				 * close() or do_dup() has cleared a reference
1877				 * in a shared file descriptor table.
1878				 */
1879				fdp->fd_holdleaderswakeup = 1;
1880				sx_sleep(&fdp->fd_holdleaderscount,
1881				    FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1882				goto retry;
1883			}
1884			if (fdtol->fdl_holdcount > 0) {
1885				/*
1886				 * Ensure that fdtol->fdl_leader remains
1887				 * valid in closef().
1888				 */
1889				fdtol->fdl_wakeup = 1;
1890				sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1891				    "fdlhold", 0);
1892				goto retry;
1893			}
1894		}
1895		fdtol->fdl_refcount--;
1896		if (fdtol->fdl_refcount == 0 &&
1897		    fdtol->fdl_holdcount == 0) {
1898			fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1899			fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1900		} else
1901			fdtol = NULL;
1902		td->td_proc->p_fdtol = NULL;
1903		FILEDESC_XUNLOCK(fdp);
1904		if (fdtol != NULL)
1905			free(fdtol, M_FILEDESC_TO_LEADER);
1906	}
1907	FILEDESC_XLOCK(fdp);
1908	i = --fdp->fd_refcnt;
1909	FILEDESC_XUNLOCK(fdp);
1910	if (i > 0)
1911		return;
1912
1913	fpp = fdp->fd_ofiles;
1914	for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1915		if (*fpp) {
1916			FILEDESC_XLOCK(fdp);
1917			fp = *fpp;
1918			*fpp = NULL;
1919			FILEDESC_XUNLOCK(fdp);
1920			(void) closef(fp, td);
1921		}
1922	}
1923	FILEDESC_XLOCK(fdp);
1924
1925	/* XXX This should happen earlier. */
1926	mtx_lock(&fdesc_mtx);
1927	td->td_proc->p_fd = NULL;
1928	mtx_unlock(&fdesc_mtx);
1929
1930	if (fdp->fd_nfiles > NDFILE)
1931		free(fdp->fd_ofiles, M_FILEDESC);
1932	if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1933		free(fdp->fd_map, M_FILEDESC);
1934
1935	fdp->fd_nfiles = 0;
1936
1937	cdir = fdp->fd_cdir;
1938	fdp->fd_cdir = NULL;
1939	rdir = fdp->fd_rdir;
1940	fdp->fd_rdir = NULL;
1941	jdir = fdp->fd_jdir;
1942	fdp->fd_jdir = NULL;
1943	FILEDESC_XUNLOCK(fdp);
1944
1945	if (cdir) {
1946		locked = VFS_LOCK_GIANT(cdir->v_mount);
1947		vrele(cdir);
1948		VFS_UNLOCK_GIANT(locked);
1949	}
1950	if (rdir) {
1951		locked = VFS_LOCK_GIANT(rdir->v_mount);
1952		vrele(rdir);
1953		VFS_UNLOCK_GIANT(locked);
1954	}
1955	if (jdir) {
1956		locked = VFS_LOCK_GIANT(jdir->v_mount);
1957		vrele(jdir);
1958		VFS_UNLOCK_GIANT(locked);
1959	}
1960
1961	fddrop(fdp);
1962}
1963
1964/*
1965 * For setugid programs, we don't want to people to use that setugidness
1966 * to generate error messages which write to a file which otherwise would
1967 * otherwise be off-limits to the process.  We check for filesystems where
1968 * the vnode can change out from under us after execve (like [lin]procfs).
1969 *
1970 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1971 * sufficient.  We also don't check for setugidness since we know we are.
1972 */
1973static int
1974is_unsafe(struct file *fp)
1975{
1976	if (fp->f_type == DTYPE_VNODE) {
1977		struct vnode *vp = fp->f_vnode;
1978
1979		if ((vp->v_vflag & VV_PROCDEP) != 0)
1980			return (1);
1981	}
1982	return (0);
1983}
1984
1985/*
1986 * Make this setguid thing safe, if at all possible.
1987 */
1988void
1989setugidsafety(struct thread *td)
1990{
1991	struct filedesc *fdp;
1992	int i;
1993
1994	/* Certain daemons might not have file descriptors. */
1995	fdp = td->td_proc->p_fd;
1996	if (fdp == NULL)
1997		return;
1998
1999	/*
2000	 * Note: fdp->fd_ofiles may be reallocated out from under us while
2001	 * we are blocked in a close.  Be careful!
2002	 */
2003	FILEDESC_XLOCK(fdp);
2004	for (i = 0; i <= fdp->fd_lastfile; i++) {
2005		if (i > 2)
2006			break;
2007		if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
2008			struct file *fp;
2009
2010			knote_fdclose(td, i);
2011			/*
2012			 * NULL-out descriptor prior to close to avoid
2013			 * a race while close blocks.
2014			 */
2015			fp = fdp->fd_ofiles[i];
2016			fdp->fd_ofiles[i] = NULL;
2017			fdp->fd_ofileflags[i] = 0;
2018			fdunused(fdp, i);
2019			FILEDESC_XUNLOCK(fdp);
2020			(void) closef(fp, td);
2021			FILEDESC_XLOCK(fdp);
2022		}
2023	}
2024	FILEDESC_XUNLOCK(fdp);
2025}
2026
2027/*
2028 * If a specific file object occupies a specific file descriptor, close the
2029 * file descriptor entry and drop a reference on the file object.  This is a
2030 * convenience function to handle a subsequent error in a function that calls
2031 * falloc() that handles the race that another thread might have closed the
2032 * file descriptor out from under the thread creating the file object.
2033 */
2034void
2035fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2036{
2037
2038	FILEDESC_XLOCK(fdp);
2039	if (fdp->fd_ofiles[idx] == fp) {
2040		fdp->fd_ofiles[idx] = NULL;
2041		fdunused(fdp, idx);
2042		FILEDESC_XUNLOCK(fdp);
2043		fdrop(fp, td);
2044	} else
2045		FILEDESC_XUNLOCK(fdp);
2046}
2047
2048/*
2049 * Close any files on exec?
2050 */
2051void
2052fdcloseexec(struct thread *td)
2053{
2054	struct filedesc *fdp;
2055	int i;
2056
2057	/* Certain daemons might not have file descriptors. */
2058	fdp = td->td_proc->p_fd;
2059	if (fdp == NULL)
2060		return;
2061
2062	FILEDESC_XLOCK(fdp);
2063
2064	/*
2065	 * We cannot cache fd_ofiles or fd_ofileflags since operations
2066	 * may block and rip them out from under us.
2067	 */
2068	for (i = 0; i <= fdp->fd_lastfile; i++) {
2069		if (fdp->fd_ofiles[i] != NULL &&
2070		    (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
2071		    (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
2072			struct file *fp;
2073
2074			knote_fdclose(td, i);
2075			/*
2076			 * NULL-out descriptor prior to close to avoid
2077			 * a race while close blocks.
2078			 */
2079			fp = fdp->fd_ofiles[i];
2080			fdp->fd_ofiles[i] = NULL;
2081			fdp->fd_ofileflags[i] = 0;
2082			fdunused(fdp, i);
2083			if (fp->f_type == DTYPE_MQUEUE)
2084				mq_fdclose(td, i, fp);
2085			FILEDESC_XUNLOCK(fdp);
2086			(void) closef(fp, td);
2087			FILEDESC_XLOCK(fdp);
2088		}
2089	}
2090	FILEDESC_XUNLOCK(fdp);
2091}
2092
2093/*
2094 * It is unsafe for set[ug]id processes to be started with file
2095 * descriptors 0..2 closed, as these descriptors are given implicit
2096 * significance in the Standard C library.  fdcheckstd() will create a
2097 * descriptor referencing /dev/null for each of stdin, stdout, and
2098 * stderr that is not already open.
2099 */
2100int
2101fdcheckstd(struct thread *td)
2102{
2103	struct filedesc *fdp;
2104	register_t retval, save;
2105	int i, error, devnull;
2106
2107	fdp = td->td_proc->p_fd;
2108	if (fdp == NULL)
2109		return (0);
2110	KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2111	devnull = -1;
2112	error = 0;
2113	for (i = 0; i < 3; i++) {
2114		if (fdp->fd_ofiles[i] != NULL)
2115			continue;
2116		if (devnull < 0) {
2117			save = td->td_retval[0];
2118			error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2119			    O_RDWR, 0);
2120			devnull = td->td_retval[0];
2121			td->td_retval[0] = save;
2122			if (error)
2123				break;
2124			KASSERT(devnull == i, ("oof, we didn't get our fd"));
2125		} else {
2126			error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2127			if (error != 0)
2128				break;
2129		}
2130	}
2131	return (error);
2132}
2133
2134/*
2135 * Internal form of close.  Decrement reference count on file structure.
2136 * Note: td may be NULL when closing a file that was being passed in a
2137 * message.
2138 *
2139 * XXXRW: Giant is not required for the caller, but often will be held; this
2140 * makes it moderately likely the Giant will be recursed in the VFS case.
2141 */
2142int
2143closef(struct file *fp, struct thread *td)
2144{
2145	struct vnode *vp;
2146	struct flock lf;
2147	struct filedesc_to_leader *fdtol;
2148	struct filedesc *fdp;
2149
2150	/*
2151	 * POSIX record locking dictates that any close releases ALL
2152	 * locks owned by this process.  This is handled by setting
2153	 * a flag in the unlock to free ONLY locks obeying POSIX
2154	 * semantics, and not to free BSD-style file locks.
2155	 * If the descriptor was in a message, POSIX-style locks
2156	 * aren't passed with the descriptor, and the thread pointer
2157	 * will be NULL.  Callers should be careful only to pass a
2158	 * NULL thread pointer when there really is no owning
2159	 * context that might have locks, or the locks will be
2160	 * leaked.
2161	 */
2162	if (fp->f_type == DTYPE_VNODE && td != NULL) {
2163		int vfslocked;
2164
2165		vp = fp->f_vnode;
2166		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2167		if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2168			lf.l_whence = SEEK_SET;
2169			lf.l_start = 0;
2170			lf.l_len = 0;
2171			lf.l_type = F_UNLCK;
2172			(void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2173					   F_UNLCK, &lf, F_POSIX);
2174		}
2175		fdtol = td->td_proc->p_fdtol;
2176		if (fdtol != NULL) {
2177			/*
2178			 * Handle special case where file descriptor table is
2179			 * shared between multiple process leaders.
2180			 */
2181			fdp = td->td_proc->p_fd;
2182			FILEDESC_XLOCK(fdp);
2183			for (fdtol = fdtol->fdl_next;
2184			     fdtol != td->td_proc->p_fdtol;
2185			     fdtol = fdtol->fdl_next) {
2186				if ((fdtol->fdl_leader->p_flag &
2187				     P_ADVLOCK) == 0)
2188					continue;
2189				fdtol->fdl_holdcount++;
2190				FILEDESC_XUNLOCK(fdp);
2191				lf.l_whence = SEEK_SET;
2192				lf.l_start = 0;
2193				lf.l_len = 0;
2194				lf.l_type = F_UNLCK;
2195				vp = fp->f_vnode;
2196				(void) VOP_ADVLOCK(vp,
2197						   (caddr_t)fdtol->fdl_leader,
2198						   F_UNLCK, &lf, F_POSIX);
2199				FILEDESC_XLOCK(fdp);
2200				fdtol->fdl_holdcount--;
2201				if (fdtol->fdl_holdcount == 0 &&
2202				    fdtol->fdl_wakeup != 0) {
2203					fdtol->fdl_wakeup = 0;
2204					wakeup(fdtol);
2205				}
2206			}
2207			FILEDESC_XUNLOCK(fdp);
2208		}
2209		VFS_UNLOCK_GIANT(vfslocked);
2210	}
2211	return (fdrop(fp, td));
2212}
2213
2214/*
2215 * Initialize the file pointer with the specified properties.
2216 *
2217 * The ops are set with release semantics to be certain that the flags, type,
2218 * and data are visible when ops is.  This is to prevent ops methods from being
2219 * called with bad data.
2220 */
2221void
2222finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2223{
2224	fp->f_data = data;
2225	fp->f_flag = flag;
2226	fp->f_type = type;
2227	atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2228}
2229
2230struct file *
2231fget_unlocked(struct filedesc *fdp, int fd)
2232{
2233	struct file *fp;
2234	u_int count;
2235
2236	if (fd < 0 || fd >= fdp->fd_nfiles)
2237		return (NULL);
2238	/*
2239	 * Fetch the descriptor locklessly.  We avoid fdrop() races by
2240	 * never raising a refcount above 0.  To accomplish this we have
2241	 * to use a cmpset loop rather than an atomic_add.  The descriptor
2242	 * must be re-verified once we acquire a reference to be certain
2243	 * that the identity is still correct and we did not lose a race
2244	 * due to preemption.
2245	 */
2246	for (;;) {
2247		fp = fdp->fd_ofiles[fd];
2248		if (fp == NULL)
2249			break;
2250		count = fp->f_count;
2251		if (count == 0)
2252			continue;
2253		/*
2254		 * Use an acquire barrier to prevent caching of fd_ofiles
2255		 * so it is refreshed for verification.
2256		 */
2257		if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2258			continue;
2259		if (fp == fdp->fd_ofiles[fd])
2260			break;
2261		fdrop(fp, curthread);
2262	}
2263
2264	return (fp);
2265}
2266
2267/*
2268 * Extract the file pointer associated with the specified descriptor for the
2269 * current user process.
2270 *
2271 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2272 * returned.
2273 *
2274 * If the FGET_GETCAP flag is set, the capability itself will be returned.
2275 * Calling _fget() with FGET_GETCAP on a non-capability will return EINVAL.
2276 * Otherwise, if the file is a capability, its rights will be checked against
2277 * the capability rights mask, and if successful, the object will be unwrapped.
2278 *
2279 * If an error occured the non-zero error is returned and *fpp is set to
2280 * NULL.  Otherwise *fpp is held and set and zero is returned.  Caller is
2281 * responsible for fdrop().
2282 */
2283#define	FGET_GETCAP	0x00000001
2284static __inline int
2285_fget(struct thread *td, int fd, struct file **fpp, int flags,
2286    cap_rights_t needrights, cap_rights_t *haverights, u_char *maxprotp,
2287    int fget_flags)
2288{
2289	struct filedesc *fdp;
2290	struct file *fp;
2291#ifdef CAPABILITIES
2292	struct file *fp_fromcap;
2293	int error;
2294#endif
2295
2296	*fpp = NULL;
2297	if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2298		return (EBADF);
2299	if ((fp = fget_unlocked(fdp, fd)) == NULL)
2300		return (EBADF);
2301	if (fp->f_ops == &badfileops) {
2302		fdrop(fp, td);
2303		return (EBADF);
2304	}
2305
2306#ifdef CAPABILITIES
2307	/*
2308	 * If a capability has been requested, return the capability directly.
2309	 * Otherwise, check capability rights, extract the underlying object,
2310	 * and check its access flags.
2311	 */
2312	if (fget_flags & FGET_GETCAP) {
2313		if (fp->f_type != DTYPE_CAPABILITY) {
2314			fdrop(fp, td);
2315			return (EINVAL);
2316		}
2317	} else {
2318		if (maxprotp == NULL)
2319			error = cap_funwrap(fp, needrights, &fp_fromcap);
2320		else
2321			error = cap_funwrap_mmap(fp, needrights, maxprotp,
2322			    &fp_fromcap);
2323		if (error) {
2324			fdrop(fp, td);
2325			return (error);
2326		}
2327
2328		/*
2329		 * If we've unwrapped a file, drop the original capability
2330		 * and hold the new descriptor.  fp after this point refers to
2331		 * the actual (unwrapped) object, not the capability.
2332		 */
2333		if (fp != fp_fromcap) {
2334			fhold(fp_fromcap);
2335			fdrop(fp, td);
2336			fp = fp_fromcap;
2337		}
2338	}
2339#else /* !CAPABILITIES */
2340	KASSERT(fp->f_type != DTYPE_CAPABILITY,
2341	    ("%s: saw capability", __func__));
2342	if (maxprotp != NULL)
2343		*maxprotp = VM_PROT_ALL;
2344#endif /* CAPABILITIES */
2345
2346	/*
2347	 * FREAD and FWRITE failure return EBADF as per POSIX.
2348	 *
2349	 * Only one flag, or 0, may be specified.
2350	 */
2351	if ((flags == FREAD && (fp->f_flag & FREAD) == 0) ||
2352	    (flags == FWRITE && (fp->f_flag & FWRITE) == 0)) {
2353		fdrop(fp, td);
2354		return (EBADF);
2355	}
2356	*fpp = fp;
2357	return (0);
2358}
2359
2360int
2361fget(struct thread *td, int fd, struct file **fpp)
2362{
2363
2364	return(_fget(td, fd, fpp, 0, 0, NULL, NULL, 0));
2365}
2366
2367int
2368fget_read(struct thread *td, int fd, struct file **fpp)
2369{
2370
2371	return(_fget(td, fd, fpp, FREAD, 0, NULL, NULL, 0));
2372}
2373
2374int
2375fget_write(struct thread *td, int fd, struct file **fpp)
2376{
2377
2378	return(_fget(td, fd, fpp, FWRITE, 0, NULL, NULL, 0));
2379}
2380
2381/*
2382 * Unlike the other fget() calls, which will accept and check capability rights
2383 * but never return capabilities, fgetcap() returns the capability but doesn't
2384 * check capability rights.
2385 */
2386int
2387fgetcap(struct thread *td, int fd, struct file **fpp)
2388{
2389
2390	return (_fget(td, fd, fpp, 0, 0, NULL, NULL, FGET_GETCAP));
2391}
2392
2393
2394/*
2395 * Like fget() but loads the underlying vnode, or returns an error if the
2396 * descriptor does not represent a vnode.  Note that pipes use vnodes but
2397 * never have VM objects.  The returned vnode will be vref()'d.
2398 *
2399 * XXX: what about the unused flags ?
2400 */
2401static __inline int
2402_fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
2403{
2404	struct file *fp;
2405	int error;
2406
2407	*vpp = NULL;
2408	if ((error = _fget(td, fd, &fp, flags, 0, NULL, NULL, 0)) != 0)
2409		return (error);
2410	if (fp->f_vnode == NULL) {
2411		error = EINVAL;
2412	} else {
2413		*vpp = fp->f_vnode;
2414		vref(*vpp);
2415	}
2416	fdrop(fp, td);
2417
2418	return (error);
2419}
2420
2421int
2422fgetvp(struct thread *td, int fd, struct vnode **vpp)
2423{
2424
2425	return (_fgetvp(td, fd, vpp, 0));
2426}
2427
2428int
2429fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
2430{
2431
2432	return (_fgetvp(td, fd, vpp, FREAD));
2433}
2434
2435#ifdef notyet
2436int
2437fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
2438{
2439
2440	return (_fgetvp(td, fd, vpp, FWRITE));
2441}
2442#endif
2443
2444/*
2445 * Like fget() but loads the underlying socket, or returns an error if the
2446 * descriptor does not represent a socket.
2447 *
2448 * We bump the ref count on the returned socket.  XXX Also obtain the SX lock
2449 * in the future.
2450 *
2451 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2452 * on their file descriptor reference to prevent the socket from being free'd
2453 * during use.
2454 */
2455int
2456fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
2457{
2458	struct file *fp;
2459	int error;
2460
2461	*spp = NULL;
2462	if (fflagp != NULL)
2463		*fflagp = 0;
2464	if ((error = _fget(td, fd, &fp, 0, 0, NULL, NULL, 0)) != 0)
2465		return (error);
2466	if (fp->f_type != DTYPE_SOCKET) {
2467		error = ENOTSOCK;
2468	} else {
2469		*spp = fp->f_data;
2470		if (fflagp)
2471			*fflagp = fp->f_flag;
2472		SOCK_LOCK(*spp);
2473		soref(*spp);
2474		SOCK_UNLOCK(*spp);
2475	}
2476	fdrop(fp, td);
2477
2478	return (error);
2479}
2480
2481/*
2482 * Drop the reference count on the socket and XXX release the SX lock in the
2483 * future.  The last reference closes the socket.
2484 *
2485 * Note: fputsock() is deprecated, see comment for fgetsock().
2486 */
2487void
2488fputsock(struct socket *so)
2489{
2490
2491	ACCEPT_LOCK();
2492	SOCK_LOCK(so);
2493	CURVNET_SET(so->so_vnet);
2494	sorele(so);
2495	CURVNET_RESTORE();
2496}
2497
2498/*
2499 * Handle the last reference to a file being closed.
2500 */
2501int
2502_fdrop(struct file *fp, struct thread *td)
2503{
2504	int error;
2505
2506	error = 0;
2507	if (fp->f_count != 0)
2508		panic("fdrop: count %d", fp->f_count);
2509	if (fp->f_ops != &badfileops)
2510		error = fo_close(fp, td);
2511	/*
2512	 * The f_cdevpriv cannot be assigned non-NULL value while we
2513	 * are destroying the file.
2514	 */
2515	if (fp->f_cdevpriv != NULL)
2516		devfs_fpdrop(fp);
2517	atomic_subtract_int(&openfiles, 1);
2518	crfree(fp->f_cred);
2519	uma_zfree(file_zone, fp);
2520
2521	return (error);
2522}
2523
2524/*
2525 * Apply an advisory lock on a file descriptor.
2526 *
2527 * Just attempt to get a record lock of the requested type on the entire file
2528 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2529 */
2530#ifndef _SYS_SYSPROTO_H_
2531struct flock_args {
2532	int	fd;
2533	int	how;
2534};
2535#endif
2536/* ARGSUSED */
2537int
2538flock(struct thread *td, struct flock_args *uap)
2539{
2540	struct file *fp;
2541	struct vnode *vp;
2542	struct flock lf;
2543	int vfslocked;
2544	int error;
2545
2546	if ((error = fget(td, uap->fd, &fp)) != 0)
2547		return (error);
2548	if (fp->f_type != DTYPE_VNODE) {
2549		fdrop(fp, td);
2550		return (EOPNOTSUPP);
2551	}
2552
2553	vp = fp->f_vnode;
2554	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2555	lf.l_whence = SEEK_SET;
2556	lf.l_start = 0;
2557	lf.l_len = 0;
2558	if (uap->how & LOCK_UN) {
2559		lf.l_type = F_UNLCK;
2560		atomic_clear_int(&fp->f_flag, FHASLOCK);
2561		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2562		goto done2;
2563	}
2564	if (uap->how & LOCK_EX)
2565		lf.l_type = F_WRLCK;
2566	else if (uap->how & LOCK_SH)
2567		lf.l_type = F_RDLCK;
2568	else {
2569		error = EBADF;
2570		goto done2;
2571	}
2572	atomic_set_int(&fp->f_flag, FHASLOCK);
2573	error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2574	    (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2575done2:
2576	fdrop(fp, td);
2577	VFS_UNLOCK_GIANT(vfslocked);
2578	return (error);
2579}
2580/*
2581 * Duplicate the specified descriptor to a free descriptor.
2582 */
2583int
2584dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
2585{
2586	struct file *wfp;
2587	struct file *fp;
2588
2589	/*
2590	 * If the to-be-dup'd fd number is greater than the allowed number
2591	 * of file descriptors, or the fd to be dup'd has already been
2592	 * closed, then reject.
2593	 */
2594	FILEDESC_XLOCK(fdp);
2595	if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2596	    (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2597		FILEDESC_XUNLOCK(fdp);
2598		return (EBADF);
2599	}
2600
2601	/*
2602	 * There are two cases of interest here.
2603	 *
2604	 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2605	 *
2606	 * For ENXIO steal away the file structure from (dfd) and store it in
2607	 * (indx).  (dfd) is effectively closed by this operation.
2608	 *
2609	 * Any other error code is just returned.
2610	 */
2611	switch (error) {
2612	case ENODEV:
2613		/*
2614		 * Check that the mode the file is being opened for is a
2615		 * subset of the mode of the existing descriptor.
2616		 */
2617		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2618			FILEDESC_XUNLOCK(fdp);
2619			return (EACCES);
2620		}
2621		fp = fdp->fd_ofiles[indx];
2622		fdp->fd_ofiles[indx] = wfp;
2623		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2624		if (fp == NULL)
2625			fdused(fdp, indx);
2626		fhold(wfp);
2627		FILEDESC_XUNLOCK(fdp);
2628		if (fp != NULL)
2629			/*
2630			 * We now own the reference to fp that the ofiles[]
2631			 * array used to own.  Release it.
2632			 */
2633			fdrop(fp, td);
2634		return (0);
2635
2636	case ENXIO:
2637		/*
2638		 * Steal away the file pointer from dfd and stuff it into indx.
2639		 */
2640		fp = fdp->fd_ofiles[indx];
2641		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2642		fdp->fd_ofiles[dfd] = NULL;
2643		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2644		fdp->fd_ofileflags[dfd] = 0;
2645		fdunused(fdp, dfd);
2646		if (fp == NULL)
2647			fdused(fdp, indx);
2648		FILEDESC_XUNLOCK(fdp);
2649
2650		/*
2651		 * We now own the reference to fp that the ofiles[] array
2652		 * used to own.  Release it.
2653		 */
2654		if (fp != NULL)
2655			fdrop(fp, td);
2656		return (0);
2657
2658	default:
2659		FILEDESC_XUNLOCK(fdp);
2660		return (error);
2661	}
2662	/* NOTREACHED */
2663}
2664
2665/*
2666 * Scan all active processes and prisons to see if any of them have a current
2667 * or root directory of `olddp'. If so, replace them with the new mount point.
2668 */
2669void
2670mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2671{
2672	struct filedesc *fdp;
2673	struct prison *pr;
2674	struct proc *p;
2675	int nrele;
2676
2677	if (vrefcnt(olddp) == 1)
2678		return;
2679	nrele = 0;
2680	sx_slock(&allproc_lock);
2681	FOREACH_PROC_IN_SYSTEM(p) {
2682		fdp = fdhold(p);
2683		if (fdp == NULL)
2684			continue;
2685		FILEDESC_XLOCK(fdp);
2686		if (fdp->fd_cdir == olddp) {
2687			vref(newdp);
2688			fdp->fd_cdir = newdp;
2689			nrele++;
2690		}
2691		if (fdp->fd_rdir == olddp) {
2692			vref(newdp);
2693			fdp->fd_rdir = newdp;
2694			nrele++;
2695		}
2696		if (fdp->fd_jdir == olddp) {
2697			vref(newdp);
2698			fdp->fd_jdir = newdp;
2699			nrele++;
2700		}
2701		FILEDESC_XUNLOCK(fdp);
2702		fddrop(fdp);
2703	}
2704	sx_sunlock(&allproc_lock);
2705	if (rootvnode == olddp) {
2706		vref(newdp);
2707		rootvnode = newdp;
2708		nrele++;
2709	}
2710	mtx_lock(&prison0.pr_mtx);
2711	if (prison0.pr_root == olddp) {
2712		vref(newdp);
2713		prison0.pr_root = newdp;
2714		nrele++;
2715	}
2716	mtx_unlock(&prison0.pr_mtx);
2717	sx_slock(&allprison_lock);
2718	TAILQ_FOREACH(pr, &allprison, pr_list) {
2719		mtx_lock(&pr->pr_mtx);
2720		if (pr->pr_root == olddp) {
2721			vref(newdp);
2722			pr->pr_root = newdp;
2723			nrele++;
2724		}
2725		mtx_unlock(&pr->pr_mtx);
2726	}
2727	sx_sunlock(&allprison_lock);
2728	while (nrele--)
2729		vrele(olddp);
2730}
2731
2732struct filedesc_to_leader *
2733filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2734{
2735	struct filedesc_to_leader *fdtol;
2736
2737	fdtol = malloc(sizeof(struct filedesc_to_leader),
2738	       M_FILEDESC_TO_LEADER,
2739	       M_WAITOK);
2740	fdtol->fdl_refcount = 1;
2741	fdtol->fdl_holdcount = 0;
2742	fdtol->fdl_wakeup = 0;
2743	fdtol->fdl_leader = leader;
2744	if (old != NULL) {
2745		FILEDESC_XLOCK(fdp);
2746		fdtol->fdl_next = old->fdl_next;
2747		fdtol->fdl_prev = old;
2748		old->fdl_next = fdtol;
2749		fdtol->fdl_next->fdl_prev = fdtol;
2750		FILEDESC_XUNLOCK(fdp);
2751	} else {
2752		fdtol->fdl_next = fdtol;
2753		fdtol->fdl_prev = fdtol;
2754	}
2755	return (fdtol);
2756}
2757
2758/*
2759 * Get file structures globally.
2760 */
2761static int
2762sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2763{
2764	struct xfile xf;
2765	struct filedesc *fdp;
2766	struct file *fp;
2767	struct proc *p;
2768	int error, n;
2769
2770	error = sysctl_wire_old_buffer(req, 0);
2771	if (error != 0)
2772		return (error);
2773	if (req->oldptr == NULL) {
2774		n = 0;
2775		sx_slock(&allproc_lock);
2776		FOREACH_PROC_IN_SYSTEM(p) {
2777			if (p->p_state == PRS_NEW)
2778				continue;
2779			fdp = fdhold(p);
2780			if (fdp == NULL)
2781				continue;
2782			/* overestimates sparse tables. */
2783			if (fdp->fd_lastfile > 0)
2784				n += fdp->fd_lastfile;
2785			fddrop(fdp);
2786		}
2787		sx_sunlock(&allproc_lock);
2788		return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2789	}
2790	error = 0;
2791	bzero(&xf, sizeof(xf));
2792	xf.xf_size = sizeof(xf);
2793	sx_slock(&allproc_lock);
2794	FOREACH_PROC_IN_SYSTEM(p) {
2795		PROC_LOCK(p);
2796		if (p->p_state == PRS_NEW) {
2797			PROC_UNLOCK(p);
2798			continue;
2799		}
2800		if (p_cansee(req->td, p) != 0) {
2801			PROC_UNLOCK(p);
2802			continue;
2803		}
2804		xf.xf_pid = p->p_pid;
2805		xf.xf_uid = p->p_ucred->cr_uid;
2806		PROC_UNLOCK(p);
2807		fdp = fdhold(p);
2808		if (fdp == NULL)
2809			continue;
2810		FILEDESC_SLOCK(fdp);
2811		for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2812			if ((fp = fdp->fd_ofiles[n]) == NULL)
2813				continue;
2814			xf.xf_fd = n;
2815			xf.xf_file = fp;
2816			xf.xf_data = fp->f_data;
2817			xf.xf_vnode = fp->f_vnode;
2818			xf.xf_type = fp->f_type;
2819			xf.xf_count = fp->f_count;
2820			xf.xf_msgcount = 0;
2821			xf.xf_offset = fp->f_offset;
2822			xf.xf_flag = fp->f_flag;
2823			error = SYSCTL_OUT(req, &xf, sizeof(xf));
2824			if (error)
2825				break;
2826		}
2827		FILEDESC_SUNLOCK(fdp);
2828		fddrop(fdp);
2829		if (error)
2830			break;
2831	}
2832	sx_sunlock(&allproc_lock);
2833	return (error);
2834}
2835
2836SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2837    0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2838
2839#ifdef KINFO_OFILE_SIZE
2840CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2841#endif
2842
2843#ifdef COMPAT_FREEBSD7
2844static int
2845export_vnode_for_osysctl(struct vnode *vp, int type,
2846    struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2847{
2848	int error;
2849	char *fullpath, *freepath;
2850	int vfslocked;
2851
2852	bzero(kif, sizeof(*kif));
2853	kif->kf_structsize = sizeof(*kif);
2854
2855	vref(vp);
2856	kif->kf_fd = type;
2857	kif->kf_type = KF_TYPE_VNODE;
2858	/* This function only handles directories. */
2859	if (vp->v_type != VDIR) {
2860		vrele(vp);
2861		return (ENOTDIR);
2862	}
2863	kif->kf_vnode_type = KF_VTYPE_VDIR;
2864
2865	/*
2866	 * This is not a true file descriptor, so we set a bogus refcount
2867	 * and offset to indicate these fields should be ignored.
2868	 */
2869	kif->kf_ref_count = -1;
2870	kif->kf_offset = -1;
2871
2872	freepath = NULL;
2873	fullpath = "-";
2874	FILEDESC_SUNLOCK(fdp);
2875	vn_fullpath(curthread, vp, &fullpath, &freepath);
2876	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2877	vrele(vp);
2878	VFS_UNLOCK_GIANT(vfslocked);
2879	strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2880	if (freepath != NULL)
2881		free(freepath, M_TEMP);
2882	error = SYSCTL_OUT(req, kif, sizeof(*kif));
2883	FILEDESC_SLOCK(fdp);
2884	return (error);
2885}
2886
2887/*
2888 * Get per-process file descriptors for use by procstat(1), et al.
2889 */
2890static int
2891sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
2892{
2893	char *fullpath, *freepath;
2894	struct kinfo_ofile *kif;
2895	struct filedesc *fdp;
2896	int error, i, *name;
2897	struct socket *so;
2898	struct vnode *vp;
2899	struct file *fp;
2900	struct proc *p;
2901	struct tty *tp;
2902	int vfslocked;
2903
2904	name = (int *)arg1;
2905	if ((p = pfind((pid_t)name[0])) == NULL)
2906		return (ESRCH);
2907	if ((error = p_candebug(curthread, p))) {
2908		PROC_UNLOCK(p);
2909		return (error);
2910	}
2911	fdp = fdhold(p);
2912	PROC_UNLOCK(p);
2913	if (fdp == NULL)
2914		return (ENOENT);
2915	kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2916	FILEDESC_SLOCK(fdp);
2917	if (fdp->fd_cdir != NULL)
2918		export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
2919				fdp, req);
2920	if (fdp->fd_rdir != NULL)
2921		export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
2922				fdp, req);
2923	if (fdp->fd_jdir != NULL)
2924		export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
2925				fdp, req);
2926	for (i = 0; i < fdp->fd_nfiles; i++) {
2927		if ((fp = fdp->fd_ofiles[i]) == NULL)
2928			continue;
2929		bzero(kif, sizeof(*kif));
2930		kif->kf_structsize = sizeof(*kif);
2931		vp = NULL;
2932		so = NULL;
2933		tp = NULL;
2934		kif->kf_fd = i;
2935		switch (fp->f_type) {
2936		case DTYPE_VNODE:
2937			kif->kf_type = KF_TYPE_VNODE;
2938			vp = fp->f_vnode;
2939			break;
2940
2941		case DTYPE_SOCKET:
2942			kif->kf_type = KF_TYPE_SOCKET;
2943			so = fp->f_data;
2944			break;
2945
2946		case DTYPE_PIPE:
2947			kif->kf_type = KF_TYPE_PIPE;
2948			break;
2949
2950		case DTYPE_FIFO:
2951			kif->kf_type = KF_TYPE_FIFO;
2952			vp = fp->f_vnode;
2953			break;
2954
2955		case DTYPE_KQUEUE:
2956			kif->kf_type = KF_TYPE_KQUEUE;
2957			break;
2958
2959		case DTYPE_CRYPTO:
2960			kif->kf_type = KF_TYPE_CRYPTO;
2961			break;
2962
2963		case DTYPE_MQUEUE:
2964			kif->kf_type = KF_TYPE_MQUEUE;
2965			break;
2966
2967		case DTYPE_SHM:
2968			kif->kf_type = KF_TYPE_SHM;
2969			break;
2970
2971		case DTYPE_SEM:
2972			kif->kf_type = KF_TYPE_SEM;
2973			break;
2974
2975		case DTYPE_PTS:
2976			kif->kf_type = KF_TYPE_PTS;
2977			tp = fp->f_data;
2978			break;
2979
2980		default:
2981			kif->kf_type = KF_TYPE_UNKNOWN;
2982			break;
2983		}
2984		kif->kf_ref_count = fp->f_count;
2985		if (fp->f_flag & FREAD)
2986			kif->kf_flags |= KF_FLAG_READ;
2987		if (fp->f_flag & FWRITE)
2988			kif->kf_flags |= KF_FLAG_WRITE;
2989		if (fp->f_flag & FAPPEND)
2990			kif->kf_flags |= KF_FLAG_APPEND;
2991		if (fp->f_flag & FASYNC)
2992			kif->kf_flags |= KF_FLAG_ASYNC;
2993		if (fp->f_flag & FFSYNC)
2994			kif->kf_flags |= KF_FLAG_FSYNC;
2995		if (fp->f_flag & FNONBLOCK)
2996			kif->kf_flags |= KF_FLAG_NONBLOCK;
2997		if (fp->f_flag & O_DIRECT)
2998			kif->kf_flags |= KF_FLAG_DIRECT;
2999		if (fp->f_flag & FHASLOCK)
3000			kif->kf_flags |= KF_FLAG_HASLOCK;
3001		kif->kf_offset = fp->f_offset;
3002		if (vp != NULL) {
3003			vref(vp);
3004			switch (vp->v_type) {
3005			case VNON:
3006				kif->kf_vnode_type = KF_VTYPE_VNON;
3007				break;
3008			case VREG:
3009				kif->kf_vnode_type = KF_VTYPE_VREG;
3010				break;
3011			case VDIR:
3012				kif->kf_vnode_type = KF_VTYPE_VDIR;
3013				break;
3014			case VBLK:
3015				kif->kf_vnode_type = KF_VTYPE_VBLK;
3016				break;
3017			case VCHR:
3018				kif->kf_vnode_type = KF_VTYPE_VCHR;
3019				break;
3020			case VLNK:
3021				kif->kf_vnode_type = KF_VTYPE_VLNK;
3022				break;
3023			case VSOCK:
3024				kif->kf_vnode_type = KF_VTYPE_VSOCK;
3025				break;
3026			case VFIFO:
3027				kif->kf_vnode_type = KF_VTYPE_VFIFO;
3028				break;
3029			case VBAD:
3030				kif->kf_vnode_type = KF_VTYPE_VBAD;
3031				break;
3032			default:
3033				kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3034				break;
3035			}
3036			/*
3037			 * It is OK to drop the filedesc lock here as we will
3038			 * re-validate and re-evaluate its properties when
3039			 * the loop continues.
3040			 */
3041			freepath = NULL;
3042			fullpath = "-";
3043			FILEDESC_SUNLOCK(fdp);
3044			vn_fullpath(curthread, vp, &fullpath, &freepath);
3045			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3046			vrele(vp);
3047			VFS_UNLOCK_GIANT(vfslocked);
3048			strlcpy(kif->kf_path, fullpath,
3049			    sizeof(kif->kf_path));
3050			if (freepath != NULL)
3051				free(freepath, M_TEMP);
3052			FILEDESC_SLOCK(fdp);
3053		}
3054		if (so != NULL) {
3055			struct sockaddr *sa;
3056
3057			if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3058			    == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3059				bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3060				free(sa, M_SONAME);
3061			}
3062			if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3063			    == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3064				bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3065				free(sa, M_SONAME);
3066			}
3067			kif->kf_sock_domain =
3068			    so->so_proto->pr_domain->dom_family;
3069			kif->kf_sock_type = so->so_type;
3070			kif->kf_sock_protocol = so->so_proto->pr_protocol;
3071		}
3072		if (tp != NULL) {
3073			strlcpy(kif->kf_path, tty_devname(tp),
3074			    sizeof(kif->kf_path));
3075		}
3076		error = SYSCTL_OUT(req, kif, sizeof(*kif));
3077		if (error)
3078			break;
3079	}
3080	FILEDESC_SUNLOCK(fdp);
3081	fddrop(fdp);
3082	free(kif, M_TEMP);
3083	return (0);
3084}
3085
3086static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3087    sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3088#endif	/* COMPAT_FREEBSD7 */
3089
3090#ifdef KINFO_FILE_SIZE
3091CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3092#endif
3093
3094static int
3095export_fd_for_sysctl(void *data, int type, int fd, int fflags, int refcnt,
3096    int64_t offset, struct kinfo_file *kif, struct sysctl_req *req)
3097{
3098	struct {
3099		int	fflag;
3100		int	kf_fflag;
3101	} fflags_table[] = {
3102		{ FAPPEND, KF_FLAG_APPEND },
3103		{ FASYNC, KF_FLAG_ASYNC },
3104		{ FFSYNC, KF_FLAG_FSYNC },
3105		{ FHASLOCK, KF_FLAG_HASLOCK },
3106		{ FNONBLOCK, KF_FLAG_NONBLOCK },
3107		{ FREAD, KF_FLAG_READ },
3108		{ FWRITE, KF_FLAG_WRITE },
3109		{ O_CREAT, KF_FLAG_CREAT },
3110		{ O_DIRECT, KF_FLAG_DIRECT },
3111		{ O_EXCL, KF_FLAG_EXCL },
3112		{ O_EXEC, KF_FLAG_EXEC },
3113		{ O_EXLOCK, KF_FLAG_EXLOCK },
3114		{ O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3115		{ O_SHLOCK, KF_FLAG_SHLOCK },
3116		{ O_TRUNC, KF_FLAG_TRUNC }
3117	};
3118#define	NFFLAGS	(sizeof(fflags_table) / sizeof(*fflags_table))
3119	struct vnode *vp;
3120	int error, vfslocked;
3121	unsigned int i;
3122
3123	bzero(kif, sizeof(*kif));
3124	switch (type) {
3125	case KF_TYPE_FIFO:
3126	case KF_TYPE_VNODE:
3127		vp = (struct vnode *)data;
3128		error = fill_vnode_info(vp, kif);
3129		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3130		vrele(vp);
3131		VFS_UNLOCK_GIANT(vfslocked);
3132		break;
3133	case KF_TYPE_SOCKET:
3134		error = fill_socket_info((struct socket *)data, kif);
3135		break;
3136	case KF_TYPE_PIPE:
3137		error = fill_pipe_info((struct pipe *)data, kif);
3138		break;
3139	case KF_TYPE_PTS:
3140		error = fill_pts_info((struct tty *)data, kif);
3141		break;
3142	default:
3143		error = 0;
3144	}
3145	if (error == 0)
3146		kif->kf_status |= KF_ATTR_VALID;
3147
3148	/*
3149	 * Translate file access flags.
3150	 */
3151	for (i = 0; i < NFFLAGS; i++)
3152		if (fflags & fflags_table[i].fflag)
3153			kif->kf_flags |=  fflags_table[i].kf_fflag;
3154	kif->kf_fd = fd;
3155	kif->kf_type = type;
3156	kif->kf_ref_count = refcnt;
3157	kif->kf_offset = offset;
3158	/* Pack record size down */
3159	kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3160	    strlen(kif->kf_path) + 1;
3161	kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3162	error = SYSCTL_OUT(req, kif, kif->kf_structsize);
3163	return (error);
3164}
3165
3166/*
3167 * Get per-process file descriptors for use by procstat(1), et al.
3168 */
3169static int
3170sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3171{
3172	struct file *fp;
3173	struct filedesc *fdp;
3174	struct kinfo_file *kif;
3175	struct proc *p;
3176	struct vnode *cttyvp, *textvp, *tracevp;
3177	size_t oldidx;
3178	int64_t offset;
3179	void *data;
3180	int error, i, *name;
3181	int type, refcnt, fflags;
3182
3183	name = (int *)arg1;
3184	if ((p = pfind((pid_t)name[0])) == NULL)
3185		return (ESRCH);
3186	if ((error = p_candebug(curthread, p))) {
3187		PROC_UNLOCK(p);
3188		return (error);
3189	}
3190	/* ktrace vnode */
3191	tracevp = p->p_tracevp;
3192	if (tracevp != NULL)
3193		vref(tracevp);
3194	/* text vnode */
3195	textvp = p->p_textvp;
3196	if (textvp != NULL)
3197		vref(textvp);
3198	/* Controlling tty. */
3199	cttyvp = NULL;
3200	if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3201		cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3202		if (cttyvp != NULL)
3203			vref(cttyvp);
3204	}
3205	fdp = fdhold(p);
3206	PROC_UNLOCK(p);
3207	kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
3208	if (tracevp != NULL)
3209		export_fd_for_sysctl(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3210		    FREAD | FWRITE, -1, -1, kif, req);
3211	if (textvp != NULL)
3212		export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3213		    FREAD, -1, -1, kif, req);
3214	if (cttyvp != NULL)
3215		export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3216		    FREAD | FWRITE, -1, -1, kif, req);
3217	if (fdp == NULL)
3218		goto fail;
3219	FILEDESC_SLOCK(fdp);
3220	/* working directory */
3221	if (fdp->fd_cdir != NULL) {
3222		vref(fdp->fd_cdir);
3223		data = fdp->fd_cdir;
3224		FILEDESC_SUNLOCK(fdp);
3225		export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3226		    FREAD, -1, -1, kif, req);
3227		FILEDESC_SLOCK(fdp);
3228	}
3229	/* root directory */
3230	if (fdp->fd_rdir != NULL) {
3231		vref(fdp->fd_rdir);
3232		data = fdp->fd_rdir;
3233		FILEDESC_SUNLOCK(fdp);
3234		export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3235		    FREAD, -1, -1, kif, req);
3236		FILEDESC_SLOCK(fdp);
3237	}
3238	/* jail directory */
3239	if (fdp->fd_jdir != NULL) {
3240		vref(fdp->fd_jdir);
3241		data = fdp->fd_jdir;
3242		FILEDESC_SUNLOCK(fdp);
3243		export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3244		    FREAD, -1, -1, kif, req);
3245		FILEDESC_SLOCK(fdp);
3246	}
3247	for (i = 0; i < fdp->fd_nfiles; i++) {
3248		if ((fp = fdp->fd_ofiles[i]) == NULL)
3249			continue;
3250		data = NULL;
3251		switch (fp->f_type) {
3252		case DTYPE_VNODE:
3253			type = KF_TYPE_VNODE;
3254			vref(fp->f_vnode);
3255			data = fp->f_vnode;
3256			break;
3257
3258		case DTYPE_SOCKET:
3259			type = KF_TYPE_SOCKET;
3260			data = fp->f_data;
3261			break;
3262
3263		case DTYPE_PIPE:
3264			type = KF_TYPE_PIPE;
3265			data = fp->f_data;
3266			break;
3267
3268		case DTYPE_FIFO:
3269			type = KF_TYPE_FIFO;
3270			vref(fp->f_vnode);
3271			data = fp->f_vnode;
3272			break;
3273
3274		case DTYPE_KQUEUE:
3275			type = KF_TYPE_KQUEUE;
3276			break;
3277
3278		case DTYPE_CRYPTO:
3279			type = KF_TYPE_CRYPTO;
3280			break;
3281
3282		case DTYPE_MQUEUE:
3283			type = KF_TYPE_MQUEUE;
3284			break;
3285
3286		case DTYPE_SHM:
3287			type = KF_TYPE_SHM;
3288			break;
3289
3290		case DTYPE_SEM:
3291			type = KF_TYPE_SEM;
3292			break;
3293
3294		case DTYPE_PTS:
3295			type = KF_TYPE_PTS;
3296			data = fp->f_data;
3297			break;
3298
3299		default:
3300			type = KF_TYPE_UNKNOWN;
3301			break;
3302		}
3303		refcnt = fp->f_count;
3304		fflags = fp->f_flag;
3305		offset = fp->f_offset;
3306
3307		/*
3308		 * Create sysctl entry.
3309		 * It is OK to drop the filedesc lock here as we will
3310		 * re-validate and re-evaluate its properties when
3311		 * the loop continues.
3312		 */
3313		oldidx = req->oldidx;
3314		if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3315			FILEDESC_SUNLOCK(fdp);
3316		error = export_fd_for_sysctl(data, type, i,
3317		    fflags, refcnt, offset, kif, req);
3318		if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3319			FILEDESC_SLOCK(fdp);
3320		if (error) {
3321			if (error == ENOMEM) {
3322				/*
3323				 * The hack to keep the ABI of sysctl
3324				 * kern.proc.filedesc intact, but not
3325				 * to account a partially copied
3326				 * kinfo_file into the oldidx.
3327				 */
3328				req->oldidx = oldidx;
3329				error = 0;
3330			}
3331			break;
3332		}
3333	}
3334	FILEDESC_SUNLOCK(fdp);
3335fail:
3336	if (fdp != NULL)
3337		fddrop(fdp);
3338	free(kif, M_TEMP);
3339	return (error);
3340}
3341
3342int
3343vntype_to_kinfo(int vtype)
3344{
3345	struct {
3346		int	vtype;
3347		int	kf_vtype;
3348	} vtypes_table[] = {
3349		{ VBAD, KF_VTYPE_VBAD },
3350		{ VBLK, KF_VTYPE_VBLK },
3351		{ VCHR, KF_VTYPE_VCHR },
3352		{ VDIR, KF_VTYPE_VDIR },
3353		{ VFIFO, KF_VTYPE_VFIFO },
3354		{ VLNK, KF_VTYPE_VLNK },
3355		{ VNON, KF_VTYPE_VNON },
3356		{ VREG, KF_VTYPE_VREG },
3357		{ VSOCK, KF_VTYPE_VSOCK }
3358	};
3359#define	NVTYPES	(sizeof(vtypes_table) / sizeof(*vtypes_table))
3360	unsigned int i;
3361
3362	/*
3363	 * Perform vtype translation.
3364	 */
3365	for (i = 0; i < NVTYPES; i++)
3366		if (vtypes_table[i].vtype == vtype)
3367			break;
3368	if (i < NVTYPES)
3369		return (vtypes_table[i].kf_vtype);
3370
3371	return (KF_VTYPE_UNKNOWN);
3372}
3373
3374static int
3375fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3376{
3377	struct vattr va;
3378	char *fullpath, *freepath;
3379	int error, vfslocked;
3380
3381	if (vp == NULL)
3382		return (1);
3383	kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3384	freepath = NULL;
3385	fullpath = "-";
3386	error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3387	if (error == 0) {
3388		strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3389	}
3390	if (freepath != NULL)
3391		free(freepath, M_TEMP);
3392
3393	/*
3394	 * Retrieve vnode attributes.
3395	 */
3396	va.va_fsid = VNOVAL;
3397	va.va_rdev = NODEV;
3398	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3399	vn_lock(vp, LK_SHARED | LK_RETRY);
3400	error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3401	VOP_UNLOCK(vp, 0);
3402	VFS_UNLOCK_GIANT(vfslocked);
3403	if (error != 0)
3404		return (error);
3405	if (va.va_fsid != VNOVAL)
3406		kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3407	else
3408		kif->kf_un.kf_file.kf_file_fsid =
3409		    vp->v_mount->mnt_stat.f_fsid.val[0];
3410	kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3411	kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3412	kif->kf_un.kf_file.kf_file_size = va.va_size;
3413	kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3414	return (0);
3415}
3416
3417static int
3418fill_socket_info(struct socket *so, struct kinfo_file *kif)
3419{
3420	struct sockaddr *sa;
3421	struct inpcb *inpcb;
3422	struct unpcb *unpcb;
3423	int error;
3424
3425	if (so == NULL)
3426		return (1);
3427	kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3428	kif->kf_sock_type = so->so_type;
3429	kif->kf_sock_protocol = so->so_proto->pr_protocol;
3430	kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3431	switch(kif->kf_sock_domain) {
3432	case AF_INET:
3433	case AF_INET6:
3434		if (kif->kf_sock_protocol == IPPROTO_TCP) {
3435			if (so->so_pcb != NULL) {
3436				inpcb = (struct inpcb *)(so->so_pcb);
3437				kif->kf_un.kf_sock.kf_sock_inpcb =
3438				    (uintptr_t)inpcb->inp_ppcb;
3439			}
3440		}
3441		break;
3442	case AF_UNIX:
3443		if (so->so_pcb != NULL) {
3444			unpcb = (struct unpcb *)(so->so_pcb);
3445			if (unpcb->unp_conn) {
3446				kif->kf_un.kf_sock.kf_sock_unpconn =
3447				    (uintptr_t)unpcb->unp_conn;
3448				kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3449				    so->so_rcv.sb_state;
3450				kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3451				    so->so_snd.sb_state;
3452			}
3453		}
3454		break;
3455	}
3456	error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3457	if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3458		bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3459		free(sa, M_SONAME);
3460	}
3461	error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3462	if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3463		bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3464		free(sa, M_SONAME);
3465	}
3466	strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3467	    sizeof(kif->kf_path));
3468	return (0);
3469}
3470
3471static int
3472fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3473{
3474
3475	if (tp == NULL)
3476		return (1);
3477	kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3478	strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3479	return (0);
3480}
3481
3482static int
3483fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3484{
3485
3486	if (pi == NULL)
3487		return (1);
3488	kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3489	kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3490	kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3491	return (0);
3492}
3493
3494static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3495    sysctl_kern_proc_filedesc, "Process filedesc entries");
3496
3497#ifdef DDB
3498/*
3499 * For the purposes of debugging, generate a human-readable string for the
3500 * file type.
3501 */
3502static const char *
3503file_type_to_name(short type)
3504{
3505
3506	switch (type) {
3507	case 0:
3508		return ("zero");
3509	case DTYPE_VNODE:
3510		return ("vnod");
3511	case DTYPE_SOCKET:
3512		return ("sock");
3513	case DTYPE_PIPE:
3514		return ("pipe");
3515	case DTYPE_FIFO:
3516		return ("fifo");
3517	case DTYPE_KQUEUE:
3518		return ("kque");
3519	case DTYPE_CRYPTO:
3520		return ("crpt");
3521	case DTYPE_MQUEUE:
3522		return ("mque");
3523	case DTYPE_SHM:
3524		return ("shm");
3525	case DTYPE_SEM:
3526		return ("ksem");
3527	default:
3528		return ("unkn");
3529	}
3530}
3531
3532/*
3533 * For the purposes of debugging, identify a process (if any, perhaps one of
3534 * many) that references the passed file in its file descriptor array. Return
3535 * NULL if none.
3536 */
3537static struct proc *
3538file_to_first_proc(struct file *fp)
3539{
3540	struct filedesc *fdp;
3541	struct proc *p;
3542	int n;
3543
3544	FOREACH_PROC_IN_SYSTEM(p) {
3545		if (p->p_state == PRS_NEW)
3546			continue;
3547		fdp = p->p_fd;
3548		if (fdp == NULL)
3549			continue;
3550		for (n = 0; n < fdp->fd_nfiles; n++) {
3551			if (fp == fdp->fd_ofiles[n])
3552				return (p);
3553		}
3554	}
3555	return (NULL);
3556}
3557
3558static void
3559db_print_file(struct file *fp, int header)
3560{
3561	struct proc *p;
3562
3563	if (header)
3564		db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3565		    "File", "Type", "Data", "Flag", "GCFl", "Count",
3566		    "MCount", "Vnode", "FPID", "FCmd");
3567	p = file_to_first_proc(fp);
3568	db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3569	    file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3570	    0, fp->f_count, 0, fp->f_vnode,
3571	    p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3572}
3573
3574DB_SHOW_COMMAND(file, db_show_file)
3575{
3576	struct file *fp;
3577
3578	if (!have_addr) {
3579		db_printf("usage: show file <addr>\n");
3580		return;
3581	}
3582	fp = (struct file *)addr;
3583	db_print_file(fp, 1);
3584}
3585
3586DB_SHOW_COMMAND(files, db_show_files)
3587{
3588	struct filedesc *fdp;
3589	struct file *fp;
3590	struct proc *p;
3591	int header;
3592	int n;
3593
3594	header = 1;
3595	FOREACH_PROC_IN_SYSTEM(p) {
3596		if (p->p_state == PRS_NEW)
3597			continue;
3598		if ((fdp = p->p_fd) == NULL)
3599			continue;
3600		for (n = 0; n < fdp->fd_nfiles; ++n) {
3601			if ((fp = fdp->fd_ofiles[n]) == NULL)
3602				continue;
3603			db_print_file(fp, header);
3604			header = 0;
3605		}
3606	}
3607}
3608#endif
3609
3610SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3611    &maxfilesperproc, 0, "Maximum files allowed open per process");
3612
3613SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3614    &maxfiles, 0, "Maximum number of files");
3615
3616SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3617    __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3618
3619/* ARGSUSED*/
3620static void
3621filelistinit(void *dummy)
3622{
3623
3624	file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3625	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3626	mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3627	mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3628}
3629SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3630
3631/*-------------------------------------------------------------------*/
3632
3633static int
3634badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
3635{
3636
3637	return (EBADF);
3638}
3639
3640static int
3641badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
3642{
3643
3644	return (EINVAL);
3645}
3646
3647static int
3648badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
3649{
3650
3651	return (EBADF);
3652}
3653
3654static int
3655badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
3656{
3657
3658	return (0);
3659}
3660
3661static int
3662badfo_kqfilter(struct file *fp, struct knote *kn)
3663{
3664
3665	return (EBADF);
3666}
3667
3668static int
3669badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
3670{
3671
3672	return (EBADF);
3673}
3674
3675static int
3676badfo_close(struct file *fp, struct thread *td)
3677{
3678
3679	return (EBADF);
3680}
3681
3682struct fileops badfileops = {
3683	.fo_read = badfo_readwrite,
3684	.fo_write = badfo_readwrite,
3685	.fo_truncate = badfo_truncate,
3686	.fo_ioctl = badfo_ioctl,
3687	.fo_poll = badfo_poll,
3688	.fo_kqfilter = badfo_kqfilter,
3689	.fo_stat = badfo_stat,
3690	.fo_close = badfo_close,
3691};
3692
3693
3694/*-------------------------------------------------------------------*/
3695
3696/*
3697 * File Descriptor pseudo-device driver (/dev/fd/).
3698 *
3699 * Opening minor device N dup()s the file (if any) connected to file
3700 * descriptor N belonging to the calling process.  Note that this driver
3701 * consists of only the ``open()'' routine, because all subsequent
3702 * references to this file will be direct to the other driver.
3703 *
3704 * XXX: we could give this one a cloning event handler if necessary.
3705 */
3706
3707/* ARGSUSED */
3708static int
3709fdopen(struct cdev *dev, int mode, int type, struct thread *td)
3710{
3711
3712	/*
3713	 * XXX Kludge: set curthread->td_dupfd to contain the value of the
3714	 * the file descriptor being sought for duplication. The error
3715	 * return ensures that the vnode for this device will be released
3716	 * by vn_open. Open will detect this special error and take the
3717	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3718	 * will simply report the error.
3719	 */
3720	td->td_dupfd = dev2unit(dev);
3721	return (ENODEV);
3722}
3723
3724static struct cdevsw fildesc_cdevsw = {
3725	.d_version =	D_VERSION,
3726	.d_open =	fdopen,
3727	.d_name =	"FD",
3728};
3729
3730static void
3731fildesc_drvinit(void *unused)
3732{
3733	struct cdev *dev;
3734
3735	dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
3736	    UID_ROOT, GID_WHEEL, 0666, "fd/0");
3737	make_dev_alias(dev, "stdin");
3738	dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
3739	    UID_ROOT, GID_WHEEL, 0666, "fd/1");
3740	make_dev_alias(dev, "stdout");
3741	dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
3742	    UID_ROOT, GID_WHEEL, 0666, "fd/2");
3743	make_dev_alias(dev, "stderr");
3744}
3745
3746SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);
3747