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