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