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