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