xref: /openbsd-src/sys/kern/kern_descrip.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*	$OpenBSD: kern_descrip.c,v 1.203 2021/10/21 09:59:13 claudio Exp $	*/
2 /*	$NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_descrip.c	8.6 (Berkeley) 4/19/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/filedesc.h>
43 #include <sys/kernel.h>
44 #include <sys/vnode.h>
45 #include <sys/proc.h>
46 #include <sys/file.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/stat.h>
50 #include <sys/ioctl.h>
51 #include <sys/fcntl.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/syslog.h>
55 #include <sys/ucred.h>
56 #include <sys/unistd.h>
57 #include <sys/resourcevar.h>
58 #include <sys/mount.h>
59 #include <sys/syscallargs.h>
60 #include <sys/event.h>
61 #include <sys/pool.h>
62 #include <sys/ktrace.h>
63 #include <sys/pledge.h>
64 
65 #include <sys/pipe.h>
66 
67 /*
68  * Descriptor management.
69  *
70  * We need to block interrupts as long as `fhdlk' is being taken
71  * with and without the KERNEL_LOCK().
72  */
73 struct mutex fhdlk = MUTEX_INITIALIZER(IPL_MPFLOOR);
74 struct filelist filehead;	/* head of list of open files */
75 int numfiles;			/* actual number of open files */
76 
77 static __inline void fd_used(struct filedesc *, int);
78 static __inline void fd_unused(struct filedesc *, int);
79 static __inline int find_next_zero(u_int *, int, u_int);
80 static __inline int fd_inuse(struct filedesc *, int);
81 int finishdup(struct proc *, struct file *, int, int, register_t *, int);
82 int find_last_set(struct filedesc *, int);
83 int dodup3(struct proc *, int, int, int, register_t *);
84 
85 #define DUPF_CLOEXEC	0x01
86 #define DUPF_DUP2	0x02
87 
88 struct pool file_pool;
89 struct pool fdesc_pool;
90 
91 void
92 filedesc_init(void)
93 {
94 	pool_init(&file_pool, sizeof(struct file), 0, IPL_MPFLOOR,
95 	    PR_WAITOK, "filepl", NULL);
96 	pool_init(&fdesc_pool, sizeof(struct filedesc0), 0, IPL_NONE,
97 	    PR_WAITOK, "fdescpl", NULL);
98 	LIST_INIT(&filehead);
99 }
100 
101 static __inline int
102 find_next_zero (u_int *bitmap, int want, u_int bits)
103 {
104 	int i, off, maxoff;
105 	u_int sub;
106 
107 	if (want > bits)
108 		return -1;
109 
110 	off = want >> NDENTRYSHIFT;
111 	i = want & NDENTRYMASK;
112 	if (i) {
113 		sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
114 		if (sub != ~0)
115 			goto found;
116 		off++;
117 	}
118 
119 	maxoff = NDLOSLOTS(bits);
120 	while (off < maxoff) {
121 		if ((sub = bitmap[off]) != ~0)
122 			goto found;
123 		off++;
124 	}
125 
126 	return -1;
127 
128  found:
129 	return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
130 }
131 
132 int
133 find_last_set(struct filedesc *fd, int last)
134 {
135 	int off, i;
136 	u_int *bitmap = fd->fd_lomap;
137 
138 	off = (last - 1) >> NDENTRYSHIFT;
139 
140 	while (off >= 0 && !bitmap[off])
141 		off--;
142 	if (off < 0)
143 		return 0;
144 
145 	i = ((off + 1) << NDENTRYSHIFT) - 1;
146 	if (i >= last)
147 		i = last - 1;
148 
149 	while (i > 0 && !fd_inuse(fd, i))
150 		i--;
151 	return i;
152 }
153 
154 static __inline int
155 fd_inuse(struct filedesc *fdp, int fd)
156 {
157 	u_int off = fd >> NDENTRYSHIFT;
158 
159 	if (fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK)))
160 		return 1;
161 
162 	return 0;
163 }
164 
165 static __inline void
166 fd_used(struct filedesc *fdp, int fd)
167 {
168 	u_int off = fd >> NDENTRYSHIFT;
169 
170 	fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
171 	if (fdp->fd_lomap[off] == ~0)
172 		fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
173 
174 	if (fd > fdp->fd_lastfile)
175 		fdp->fd_lastfile = fd;
176 	fdp->fd_openfd++;
177 }
178 
179 static __inline void
180 fd_unused(struct filedesc *fdp, int fd)
181 {
182 	u_int off = fd >> NDENTRYSHIFT;
183 
184 	if (fd < fdp->fd_freefile)
185 		fdp->fd_freefile = fd;
186 
187 	if (fdp->fd_lomap[off] == ~0)
188 		fdp->fd_himap[off >> NDENTRYSHIFT] &= ~(1 << (off & NDENTRYMASK));
189 	fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
190 
191 #ifdef DIAGNOSTIC
192 	if (fd > fdp->fd_lastfile)
193 		panic("fd_unused: fd_lastfile inconsistent");
194 #endif
195 	if (fd == fdp->fd_lastfile)
196 		fdp->fd_lastfile = find_last_set(fdp, fd);
197 	fdp->fd_openfd--;
198 }
199 
200 struct file *
201 fd_iterfile(struct file *fp, struct proc *p)
202 {
203 	struct file *nfp;
204 	unsigned int count;
205 
206 	mtx_enter(&fhdlk);
207 	if (fp == NULL)
208 		nfp = LIST_FIRST(&filehead);
209 	else
210 		nfp = LIST_NEXT(fp, f_list);
211 
212 	/* don't refcount when f_count == 0 to avoid race in fdrop() */
213 	while (nfp != NULL) {
214 		count = nfp->f_count;
215 		if (count == 0) {
216 			nfp = LIST_NEXT(nfp, f_list);
217 			continue;
218 		}
219 		if (atomic_cas_uint(&nfp->f_count, count, count + 1) == count)
220 			break;
221 	}
222 	mtx_leave(&fhdlk);
223 
224 	if (fp != NULL)
225 		FRELE(fp, p);
226 
227 	return nfp;
228 }
229 
230 struct file *
231 fd_getfile(struct filedesc *fdp, int fd)
232 {
233 	struct file *fp;
234 
235 	if ((u_int)fd >= fdp->fd_nfiles)
236 		return (NULL);
237 
238 	mtx_enter(&fdp->fd_fplock);
239 	fp = fdp->fd_ofiles[fd];
240 	if (fp != NULL)
241 		atomic_inc_int(&fp->f_count);
242 	mtx_leave(&fdp->fd_fplock);
243 
244 	return (fp);
245 }
246 
247 struct file *
248 fd_getfile_mode(struct filedesc *fdp, int fd, int mode)
249 {
250 	struct file *fp;
251 
252 	KASSERT(mode != 0);
253 
254 	fp = fd_getfile(fdp, fd);
255 	if (fp == NULL)
256 		return (NULL);
257 
258 	if ((fp->f_flag & mode) == 0) {
259 		FRELE(fp, curproc);
260 		return (NULL);
261 	}
262 
263 	return (fp);
264 }
265 
266 int
267 fd_checkclosed(struct filedesc *fdp, int fd, struct file *fp)
268 {
269 	int closed;
270 
271 	mtx_enter(&fdp->fd_fplock);
272 	KASSERT(fd < fdp->fd_nfiles);
273 	closed = (fdp->fd_ofiles[fd] != fp);
274 	mtx_leave(&fdp->fd_fplock);
275 	return (closed);
276 }
277 
278 /*
279  * System calls on descriptors.
280  */
281 
282 /*
283  * Duplicate a file descriptor.
284  */
285 int
286 sys_dup(struct proc *p, void *v, register_t *retval)
287 {
288 	struct sys_dup_args /* {
289 		syscallarg(int) fd;
290 	} */ *uap = v;
291 	struct filedesc *fdp = p->p_fd;
292 	int old = SCARG(uap, fd);
293 	struct file *fp;
294 	int new;
295 	int error;
296 
297 restart:
298 	if ((fp = fd_getfile(fdp, old)) == NULL)
299 		return (EBADF);
300 	fdplock(fdp);
301 	if ((error = fdalloc(p, 0, &new)) != 0) {
302 		if (error == ENOSPC) {
303 			fdexpand(p);
304 			fdpunlock(fdp);
305 			FRELE(fp, p);
306 			goto restart;
307 		}
308 		fdpunlock(fdp);
309 		FRELE(fp, p);
310 		return (error);
311 	}
312 	/* No need for FRELE(), finishdup() uses current ref. */
313 	return (finishdup(p, fp, old, new, retval, 0));
314 }
315 
316 /*
317  * Duplicate a file descriptor to a particular value.
318  */
319 int
320 sys_dup2(struct proc *p, void *v, register_t *retval)
321 {
322 	struct sys_dup2_args /* {
323 		syscallarg(int) from;
324 		syscallarg(int) to;
325 	} */ *uap = v;
326 
327 	return (dodup3(p, SCARG(uap, from), SCARG(uap, to), 0, retval));
328 }
329 
330 int
331 sys_dup3(struct proc *p, void *v, register_t *retval)
332 {
333 	struct sys_dup3_args /* {
334 		syscallarg(int) from;
335 		syscallarg(int) to;
336 		syscallarg(int) flags;
337 	} */ *uap = v;
338 
339 	if (SCARG(uap, from) == SCARG(uap, to))
340 		return (EINVAL);
341 	if (SCARG(uap, flags) & ~O_CLOEXEC)
342 		return (EINVAL);
343 	return (dodup3(p, SCARG(uap, from), SCARG(uap, to),
344 	    SCARG(uap, flags), retval));
345 }
346 
347 int
348 dodup3(struct proc *p, int old, int new, int flags, register_t *retval)
349 {
350 	struct filedesc *fdp = p->p_fd;
351 	struct file *fp;
352 	int dupflags, error, i;
353 
354 restart:
355 	if ((fp = fd_getfile(fdp, old)) == NULL)
356 		return (EBADF);
357 	if (old == new) {
358 		/*
359 		 * NOTE! This doesn't clear the close-on-exec flag. This might
360 		 * or might not be the intended behavior from the start, but
361 		 * this is what everyone else does.
362 		 */
363 		*retval = new;
364 		FRELE(fp, p);
365 		return (0);
366 	}
367 	if ((u_int)new >= lim_cur(RLIMIT_NOFILE) ||
368 	    (u_int)new >= maxfiles) {
369 		FRELE(fp, p);
370 		return (EBADF);
371 	}
372 	fdplock(fdp);
373 	if (new >= fdp->fd_nfiles) {
374 		if ((error = fdalloc(p, new, &i)) != 0) {
375 			if (error == ENOSPC) {
376 				fdexpand(p);
377 				fdpunlock(fdp);
378 				FRELE(fp, p);
379 				goto restart;
380 			}
381 			fdpunlock(fdp);
382 			FRELE(fp, p);
383 			return (error);
384 		}
385 		if (new != i)
386 			panic("dup2: fdalloc");
387 		fd_unused(fdp, new);
388 	}
389 
390 	dupflags = DUPF_DUP2;
391 	if (flags & O_CLOEXEC)
392 		dupflags |= DUPF_CLOEXEC;
393 
394 	/* No need for FRELE(), finishdup() uses current ref. */
395 	return (finishdup(p, fp, old, new, retval, dupflags));
396 }
397 
398 /*
399  * The file control system call.
400  */
401 int
402 sys_fcntl(struct proc *p, void *v, register_t *retval)
403 {
404 	struct sys_fcntl_args /* {
405 		syscallarg(int) fd;
406 		syscallarg(int) cmd;
407 		syscallarg(void *) arg;
408 	} */ *uap = v;
409 	int fd = SCARG(uap, fd);
410 	struct filedesc *fdp = p->p_fd;
411 	struct file *fp;
412 	struct vnode *vp;
413 	int i, prev, tmp, newmin, flg = F_POSIX;
414 	struct flock fl;
415 	int error = 0;
416 
417 	error = pledge_fcntl(p, SCARG(uap, cmd));
418 	if (error)
419 		return (error);
420 
421 restart:
422 	if ((fp = fd_getfile(fdp, fd)) == NULL)
423 		return (EBADF);
424 	switch (SCARG(uap, cmd)) {
425 
426 	case F_DUPFD:
427 	case F_DUPFD_CLOEXEC:
428 		newmin = (long)SCARG(uap, arg);
429 		if ((u_int)newmin >= lim_cur(RLIMIT_NOFILE) ||
430 		    (u_int)newmin >= maxfiles) {
431 			error = EINVAL;
432 			break;
433 		}
434 		fdplock(fdp);
435 		if ((error = fdalloc(p, newmin, &i)) != 0) {
436 			if (error == ENOSPC) {
437 				fdexpand(p);
438 				fdpunlock(fdp);
439 				FRELE(fp, p);
440 				goto restart;
441 			}
442 			fdpunlock(fdp);
443 			FRELE(fp, p);
444 		} else {
445 			int dupflags = 0;
446 
447 			if (SCARG(uap, cmd) == F_DUPFD_CLOEXEC)
448 				dupflags |= DUPF_CLOEXEC;
449 
450 			/* No need for FRELE(), finishdup() uses current ref. */
451 			error = finishdup(p, fp, fd, i, retval, dupflags);
452 		}
453 		return (error);
454 
455 	case F_GETFD:
456 		fdplock(fdp);
457 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
458 		fdpunlock(fdp);
459 		break;
460 
461 	case F_SETFD:
462 		fdplock(fdp);
463 		if ((long)SCARG(uap, arg) & 1)
464 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
465 		else
466 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
467 		fdpunlock(fdp);
468 		break;
469 
470 	case F_GETFL:
471 		*retval = OFLAGS(fp->f_flag);
472 		break;
473 
474 	case F_ISATTY:
475 		vp = fp->f_data;
476 	        if (fp->f_type == DTYPE_VNODE && (vp->v_flag & VISTTY))
477 			*retval = 1;
478 		else {
479 			*retval = 0;
480 			error = ENOTTY;
481 		}
482 		break;
483 
484 	case F_SETFL:
485 		do {
486 			tmp = prev = fp->f_flag;
487 			tmp &= ~FCNTLFLAGS;
488 			tmp |= FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
489 		} while (atomic_cas_uint(&fp->f_flag, prev, tmp) != prev);
490 		tmp = fp->f_flag & FNONBLOCK;
491 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
492 		if (error)
493 			break;
494 		tmp = fp->f_flag & FASYNC;
495 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
496 		if (!error)
497 			break;
498 		atomic_clearbits_int(&fp->f_flag, FNONBLOCK);
499 		tmp = 0;
500 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
501 		break;
502 
503 	case F_GETOWN:
504 		tmp = 0;
505 		error = (*fp->f_ops->fo_ioctl)
506 			(fp, FIOGETOWN, (caddr_t)&tmp, p);
507 		*retval = tmp;
508 		break;
509 
510 	case F_SETOWN:
511 		tmp = (long)SCARG(uap, arg);
512 		error = ((*fp->f_ops->fo_ioctl)
513 			(fp, FIOSETOWN, (caddr_t)&tmp, p));
514 		break;
515 
516 	case F_SETLKW:
517 		flg |= F_WAIT;
518 		/* FALLTHROUGH */
519 
520 	case F_SETLK:
521 		error = pledge_flock(p);
522 		if (error != 0)
523 			break;
524 
525 		if (fp->f_type != DTYPE_VNODE) {
526 			error = EINVAL;
527 			break;
528 		}
529 		vp = fp->f_data;
530 		/* Copy in the lock structure */
531 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
532 		    sizeof (fl));
533 		if (error)
534 			break;
535 #ifdef KTRACE
536 		if (KTRPOINT(p, KTR_STRUCT))
537 			ktrflock(p, &fl);
538 #endif
539 		if (fl.l_whence == SEEK_CUR) {
540 			off_t offset = foffset(fp);
541 
542 			if (fl.l_start == 0 && fl.l_len < 0) {
543 				/* lockf(3) compliance hack */
544 				fl.l_len = -fl.l_len;
545 				fl.l_start = offset - fl.l_len;
546 			} else
547 				fl.l_start += offset;
548 		}
549 		switch (fl.l_type) {
550 
551 		case F_RDLCK:
552 			if ((fp->f_flag & FREAD) == 0) {
553 				error = EBADF;
554 				goto out;
555 			}
556 			atomic_setbits_int(&fdp->fd_flags, FD_ADVLOCK);
557 			error = VOP_ADVLOCK(vp, fdp, F_SETLK, &fl, flg);
558 			break;
559 
560 		case F_WRLCK:
561 			if ((fp->f_flag & FWRITE) == 0) {
562 				error = EBADF;
563 				goto out;
564 			}
565 			atomic_setbits_int(&fdp->fd_flags, FD_ADVLOCK);
566 			error = VOP_ADVLOCK(vp, fdp, F_SETLK, &fl, flg);
567 			break;
568 
569 		case F_UNLCK:
570 			error = VOP_ADVLOCK(vp, fdp, F_UNLCK, &fl, F_POSIX);
571 			goto out;
572 
573 		default:
574 			error = EINVAL;
575 			goto out;
576 		}
577 
578 		if (fd_checkclosed(fdp, fd, fp)) {
579 			/*
580 			 * We have lost the race with close() or dup2();
581 			 * unlock, pretend that we've won the race and that
582 			 * lock had been removed by close()
583 			 */
584 			fl.l_whence = SEEK_SET;
585 			fl.l_start = 0;
586 			fl.l_len = 0;
587 			VOP_ADVLOCK(vp, fdp, F_UNLCK, &fl, F_POSIX);
588 			fl.l_type = F_UNLCK;
589 		}
590 		goto out;
591 
592 
593 	case F_GETLK:
594 		error = pledge_flock(p);
595 		if (error != 0)
596 			break;
597 
598 		if (fp->f_type != DTYPE_VNODE) {
599 			error = EINVAL;
600 			break;
601 		}
602 		vp = fp->f_data;
603 		/* Copy in the lock structure */
604 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
605 		    sizeof (fl));
606 		if (error)
607 			break;
608 		if (fl.l_whence == SEEK_CUR) {
609 			off_t offset = foffset(fp);
610 
611 			if (fl.l_start == 0 && fl.l_len < 0) {
612 				/* lockf(3) compliance hack */
613 				fl.l_len = -fl.l_len;
614 				fl.l_start = offset - fl.l_len;
615 			} else
616 				fl.l_start += offset;
617 		}
618 		if (fl.l_type != F_RDLCK &&
619 		    fl.l_type != F_WRLCK &&
620 		    fl.l_type != F_UNLCK &&
621 		    fl.l_type != 0) {
622 			error = EINVAL;
623 			break;
624 		}
625 		error = VOP_ADVLOCK(vp, fdp, F_GETLK, &fl, F_POSIX);
626 		if (error)
627 			break;
628 #ifdef KTRACE
629 		if (KTRPOINT(p, KTR_STRUCT))
630 			ktrflock(p, &fl);
631 #endif
632 		error = (copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
633 		    sizeof (fl)));
634 		break;
635 
636 	default:
637 		error = EINVAL;
638 		break;
639 	}
640 out:
641 	FRELE(fp, p);
642 	return (error);
643 }
644 
645 /*
646  * Common code for dup, dup2, and fcntl(F_DUPFD).
647  */
648 int
649 finishdup(struct proc *p, struct file *fp, int old, int new,
650     register_t *retval, int dupflags)
651 {
652 	struct file *oldfp;
653 	struct filedesc *fdp = p->p_fd;
654 	int error;
655 
656 	fdpassertlocked(fdp);
657 	KASSERT(fp->f_iflags & FIF_INSERTED);
658 
659 	if (fp->f_count >= FDUP_MAX_COUNT) {
660 		error = EDEADLK;
661 		goto fail;
662 	}
663 
664 	oldfp = fd_getfile(fdp, new);
665 	if ((dupflags & DUPF_DUP2) && oldfp == NULL) {
666 		if (fd_inuse(fdp, new)) {
667 			error = EBUSY;
668 			goto fail;
669 		}
670 		fd_used(fdp, new);
671 	}
672 
673 	/*
674 	 * Use `fd_fplock' to synchronize with fd_getfile() so that
675 	 * the function no longer creates a new reference to the old file.
676 	 */
677 	mtx_enter(&fdp->fd_fplock);
678 	fdp->fd_ofiles[new] = fp;
679 	mtx_leave(&fdp->fd_fplock);
680 
681 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] & ~UF_EXCLOSE;
682 	if (dupflags & DUPF_CLOEXEC)
683 		fdp->fd_ofileflags[new] |= UF_EXCLOSE;
684 	*retval = new;
685 
686 	if (oldfp != NULL) {
687 		knote_fdclose(p, new);
688 		fdpunlock(fdp);
689 		closef(oldfp, p);
690 	} else {
691 		fdpunlock(fdp);
692 	}
693 
694 	return (0);
695 
696 fail:
697 	fdpunlock(fdp);
698 	FRELE(fp, p);
699 	return (error);
700 }
701 
702 void
703 fdinsert(struct filedesc *fdp, int fd, int flags, struct file *fp)
704 {
705 	struct file *fq;
706 
707 	fdpassertlocked(fdp);
708 
709 	mtx_enter(&fhdlk);
710 	if ((fp->f_iflags & FIF_INSERTED) == 0) {
711 		atomic_setbits_int(&fp->f_iflags, FIF_INSERTED);
712 		if ((fq = fdp->fd_ofiles[0]) != NULL) {
713 			LIST_INSERT_AFTER(fq, fp, f_list);
714 		} else {
715 			LIST_INSERT_HEAD(&filehead, fp, f_list);
716 		}
717 	}
718 	mtx_leave(&fhdlk);
719 
720 	mtx_enter(&fdp->fd_fplock);
721 	KASSERT(fdp->fd_ofiles[fd] == NULL);
722 	fdp->fd_ofiles[fd] = fp;
723 	mtx_leave(&fdp->fd_fplock);
724 
725 	fdp->fd_ofileflags[fd] |= (flags & UF_EXCLOSE);
726 }
727 
728 void
729 fdremove(struct filedesc *fdp, int fd)
730 {
731 	fdpassertlocked(fdp);
732 
733 	/*
734 	 * Use `fd_fplock' to synchronize with fd_getfile() so that
735 	 * the function no longer creates a new reference to the file.
736 	 */
737 	mtx_enter(&fdp->fd_fplock);
738 	fdp->fd_ofiles[fd] = NULL;
739 	mtx_leave(&fdp->fd_fplock);
740 
741 	fdp->fd_ofileflags[fd] = 0;
742 
743 	fd_unused(fdp, fd);
744 }
745 
746 int
747 fdrelease(struct proc *p, int fd)
748 {
749 	struct filedesc *fdp = p->p_fd;
750 	struct file *fp;
751 
752 	fdpassertlocked(fdp);
753 
754 	fp = fd_getfile(fdp, fd);
755 	if (fp == NULL) {
756 		fdpunlock(fdp);
757 		return (EBADF);
758 	}
759 	fdremove(fdp, fd);
760 	knote_fdclose(p, fd);
761 	fdpunlock(fdp);
762 	return (closef(fp, p));
763 }
764 
765 /*
766  * Close a file descriptor.
767  */
768 int
769 sys_close(struct proc *p, void *v, register_t *retval)
770 {
771 	struct sys_close_args /* {
772 		syscallarg(int) fd;
773 	} */ *uap = v;
774 	int fd = SCARG(uap, fd), error;
775 	struct filedesc *fdp = p->p_fd;
776 
777 	fdplock(fdp);
778 	/* fdrelease unlocks fdp. */
779 	error = fdrelease(p, fd);
780 
781 	return (error);
782 }
783 
784 /*
785  * Return status information about a file descriptor.
786  */
787 int
788 sys_fstat(struct proc *p, void *v, register_t *retval)
789 {
790 	struct sys_fstat_args /* {
791 		syscallarg(int) fd;
792 		syscallarg(struct stat *) sb;
793 	} */ *uap = v;
794 	int fd = SCARG(uap, fd);
795 	struct filedesc *fdp = p->p_fd;
796 	struct file *fp;
797 	struct stat ub;
798 	int error;
799 
800 	if ((fp = fd_getfile(fdp, fd)) == NULL)
801 		return (EBADF);
802 	error = (*fp->f_ops->fo_stat)(fp, &ub, p);
803 	FRELE(fp, p);
804 	if (error == 0) {
805 		/*
806 		 * Don't let non-root see generation numbers
807 		 * (for NFS security)
808 		 */
809 		if (suser(p))
810 			ub.st_gen = 0;
811 		error = copyout((caddr_t)&ub, (caddr_t)SCARG(uap, sb),
812 		    sizeof (ub));
813 	}
814 #ifdef KTRACE
815 	if (error == 0 && KTRPOINT(p, KTR_STRUCT))
816 		ktrstat(p, &ub);
817 #endif
818 	return (error);
819 }
820 
821 /*
822  * Return pathconf information about a file descriptor.
823  */
824 int
825 sys_fpathconf(struct proc *p, void *v, register_t *retval)
826 {
827 	struct sys_fpathconf_args /* {
828 		syscallarg(int) fd;
829 		syscallarg(int) name;
830 	} */ *uap = v;
831 	int fd = SCARG(uap, fd);
832 	struct filedesc *fdp = p->p_fd;
833 	struct file *fp;
834 	struct vnode *vp;
835 	int error;
836 
837 	if ((fp = fd_getfile(fdp, fd)) == NULL)
838 		return (EBADF);
839 	switch (fp->f_type) {
840 	case DTYPE_PIPE:
841 	case DTYPE_SOCKET:
842 		if (SCARG(uap, name) != _PC_PIPE_BUF) {
843 			error = EINVAL;
844 			break;
845 		}
846 		*retval = PIPE_BUF;
847 		error = 0;
848 		break;
849 
850 	case DTYPE_VNODE:
851 		vp = fp->f_data;
852 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
853 		error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
854 		VOP_UNLOCK(vp);
855 		break;
856 
857 	default:
858 		error = EOPNOTSUPP;
859 		break;
860 	}
861 	FRELE(fp, p);
862 	return (error);
863 }
864 
865 /*
866  * Allocate a file descriptor for the process.
867  */
868 int
869 fdalloc(struct proc *p, int want, int *result)
870 {
871 	struct filedesc *fdp = p->p_fd;
872 	int lim, last, i;
873 	u_int new, off;
874 
875 	fdpassertlocked(fdp);
876 
877 	/*
878 	 * Search for a free descriptor starting at the higher
879 	 * of want or fd_freefile.  If that fails, consider
880 	 * expanding the ofile array.
881 	 */
882 restart:
883 	lim = min((int)lim_cur(RLIMIT_NOFILE), maxfiles);
884 	last = min(fdp->fd_nfiles, lim);
885 	if ((i = want) < fdp->fd_freefile)
886 		i = fdp->fd_freefile;
887 	off = i >> NDENTRYSHIFT;
888 	new = find_next_zero(fdp->fd_himap, off,
889 	    (last + NDENTRIES - 1) >> NDENTRYSHIFT);
890 	if (new != -1) {
891 		i = find_next_zero(&fdp->fd_lomap[new],
892 				   new > off ? 0 : i & NDENTRYMASK,
893 				   NDENTRIES);
894 		if (i == -1) {
895 			/*
896 			 * Free file descriptor in this block was
897 			 * below want, try again with higher want.
898 			 */
899 			want = (new + 1) << NDENTRYSHIFT;
900 			goto restart;
901 		}
902 		i += (new << NDENTRYSHIFT);
903 		if (i < last) {
904 			fd_used(fdp, i);
905 			if (want <= fdp->fd_freefile)
906 				fdp->fd_freefile = i;
907 			*result = i;
908 			fdp->fd_ofileflags[i] = 0;
909 			if (ISSET(p->p_p->ps_flags, PS_PLEDGE))
910 				fdp->fd_ofileflags[i] |= UF_PLEDGED;
911 			return (0);
912 		}
913 	}
914 	if (fdp->fd_nfiles >= lim)
915 		return (EMFILE);
916 
917 	return (ENOSPC);
918 }
919 
920 void
921 fdexpand(struct proc *p)
922 {
923 	struct filedesc *fdp = p->p_fd;
924 	int nfiles, oldnfiles;
925 	size_t copylen;
926 	struct file **newofile, **oldofile;
927 	char *newofileflags;
928 	u_int *newhimap, *newlomap;
929 
930 	fdpassertlocked(fdp);
931 
932 	oldnfiles = fdp->fd_nfiles;
933 	oldofile = fdp->fd_ofiles;
934 
935 	/*
936 	 * No space in current array.
937 	 */
938 	if (fdp->fd_nfiles < NDEXTENT)
939 		nfiles = NDEXTENT;
940 	else
941 		nfiles = 2 * fdp->fd_nfiles;
942 
943 	newofile = mallocarray(nfiles, OFILESIZE, M_FILEDESC, M_WAITOK);
944 	/*
945 	 * Allocate all required chunks before calling free(9) to make
946 	 * sure that ``fd_ofiles'' stays valid if we go to sleep.
947 	 */
948 	if (NDHISLOTS(nfiles) > NDHISLOTS(fdp->fd_nfiles)) {
949 		newhimap = mallocarray(NDHISLOTS(nfiles), sizeof(u_int),
950 		    M_FILEDESC, M_WAITOK);
951 		newlomap = mallocarray(NDLOSLOTS(nfiles), sizeof(u_int),
952 		    M_FILEDESC, M_WAITOK);
953 	}
954 	newofileflags = (char *) &newofile[nfiles];
955 
956 	/*
957 	 * Copy the existing ofile and ofileflags arrays
958 	 * and zero the new portion of each array.
959 	 */
960 	copylen = sizeof(struct file *) * fdp->fd_nfiles;
961 	memcpy(newofile, fdp->fd_ofiles, copylen);
962 	memset((char *)newofile + copylen, 0,
963 	    nfiles * sizeof(struct file *) - copylen);
964 	copylen = sizeof(char) * fdp->fd_nfiles;
965 	memcpy(newofileflags, fdp->fd_ofileflags, copylen);
966 	memset(newofileflags + copylen, 0, nfiles * sizeof(char) - copylen);
967 
968 	if (NDHISLOTS(nfiles) > NDHISLOTS(fdp->fd_nfiles)) {
969 		copylen = NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int);
970 		memcpy(newhimap, fdp->fd_himap, copylen);
971 		memset((char *)newhimap + copylen, 0,
972 		    NDHISLOTS(nfiles) * sizeof(u_int) - copylen);
973 
974 		copylen = NDLOSLOTS(fdp->fd_nfiles) * sizeof(u_int);
975 		memcpy(newlomap, fdp->fd_lomap, copylen);
976 		memset((char *)newlomap + copylen, 0,
977 		    NDLOSLOTS(nfiles) * sizeof(u_int) - copylen);
978 
979 		if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
980 			free(fdp->fd_himap, M_FILEDESC,
981 			    NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int));
982 			free(fdp->fd_lomap, M_FILEDESC,
983 			    NDLOSLOTS(fdp->fd_nfiles) * sizeof(u_int));
984 		}
985 		fdp->fd_himap = newhimap;
986 		fdp->fd_lomap = newlomap;
987 	}
988 
989 	mtx_enter(&fdp->fd_fplock);
990 	fdp->fd_ofiles = newofile;
991 	mtx_leave(&fdp->fd_fplock);
992 
993 	fdp->fd_ofileflags = newofileflags;
994 	fdp->fd_nfiles = nfiles;
995 
996 	if (oldnfiles > NDFILE)
997 		free(oldofile, M_FILEDESC, oldnfiles * OFILESIZE);
998 }
999 
1000 /*
1001  * Create a new open file structure and allocate
1002  * a file descriptor for the process that refers to it.
1003  */
1004 int
1005 falloc(struct proc *p, struct file **resultfp, int *resultfd)
1006 {
1007 	struct file *fp;
1008 	int error, i;
1009 
1010 	KASSERT(resultfp != NULL);
1011 	KASSERT(resultfd != NULL);
1012 
1013 	fdpassertlocked(p->p_fd);
1014 restart:
1015 	if ((error = fdalloc(p, 0, &i)) != 0) {
1016 		if (error == ENOSPC) {
1017 			fdexpand(p);
1018 			goto restart;
1019 		}
1020 		return (error);
1021 	}
1022 
1023 	fp = fnew(p);
1024 	if (fp == NULL) {
1025 		fd_unused(p->p_fd, i);
1026 		return (ENFILE);
1027 	}
1028 
1029 	FREF(fp);
1030 	*resultfp = fp;
1031 	*resultfd = i;
1032 
1033 	return (0);
1034 }
1035 
1036 struct file *
1037 fnew(struct proc *p)
1038 {
1039 	struct file *fp;
1040 	int nfiles;
1041 
1042 	nfiles = atomic_inc_int_nv(&numfiles);
1043 	if (nfiles > maxfiles) {
1044 		atomic_dec_int(&numfiles);
1045 		tablefull("file");
1046 		return (NULL);
1047 	}
1048 
1049 	fp = pool_get(&file_pool, PR_WAITOK|PR_ZERO);
1050 	/*
1051 	 * We need to block interrupts as long as `f_mtx' is being taken
1052 	 * with and without the KERNEL_LOCK().
1053 	 */
1054 	mtx_init(&fp->f_mtx, IPL_MPFLOOR);
1055 	fp->f_count = 1;
1056 	fp->f_cred = p->p_ucred;
1057 	crhold(fp->f_cred);
1058 
1059 	return (fp);
1060 }
1061 
1062 /*
1063  * Build a new filedesc structure.
1064  */
1065 struct filedesc *
1066 fdinit(void)
1067 {
1068 	struct filedesc0 *newfdp;
1069 
1070 	newfdp = pool_get(&fdesc_pool, PR_WAITOK|PR_ZERO);
1071 	rw_init(&newfdp->fd_fd.fd_lock, "fdlock");
1072 	mtx_init(&newfdp->fd_fd.fd_fplock, IPL_MPFLOOR);
1073 	LIST_INIT(&newfdp->fd_fd.fd_kqlist);
1074 
1075 	/* Create the file descriptor table. */
1076 	newfdp->fd_fd.fd_refcnt = 1;
1077 	newfdp->fd_fd.fd_cmask = S_IWGRP|S_IWOTH;
1078 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1079 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1080 	newfdp->fd_fd.fd_nfiles = NDFILE;
1081 	newfdp->fd_fd.fd_himap = newfdp->fd_dhimap;
1082 	newfdp->fd_fd.fd_lomap = newfdp->fd_dlomap;
1083 
1084 	newfdp->fd_fd.fd_freefile = 0;
1085 	newfdp->fd_fd.fd_lastfile = 0;
1086 
1087 	return (&newfdp->fd_fd);
1088 }
1089 
1090 /*
1091  * Share a filedesc structure.
1092  */
1093 struct filedesc *
1094 fdshare(struct process *pr)
1095 {
1096 	pr->ps_fd->fd_refcnt++;
1097 	return (pr->ps_fd);
1098 }
1099 
1100 /*
1101  * Copy a filedesc structure.
1102  */
1103 struct filedesc *
1104 fdcopy(struct process *pr)
1105 {
1106 	struct filedesc *newfdp, *fdp = pr->ps_fd;
1107 	int i;
1108 
1109 	newfdp = fdinit();
1110 
1111 	fdplock(fdp);
1112 	if (fdp->fd_cdir) {
1113 		vref(fdp->fd_cdir);
1114 		newfdp->fd_cdir = fdp->fd_cdir;
1115 	}
1116 	if (fdp->fd_rdir) {
1117 		vref(fdp->fd_rdir);
1118 		newfdp->fd_rdir = fdp->fd_rdir;
1119 	}
1120 
1121 	/*
1122 	 * If the number of open files fits in the internal arrays
1123 	 * of the open file structure, use them, otherwise allocate
1124 	 * additional memory for the number of descriptors currently
1125 	 * in use.
1126 	 */
1127 	if (fdp->fd_lastfile >= NDFILE) {
1128 		/*
1129 		 * Compute the smallest multiple of NDEXTENT needed
1130 		 * for the file descriptors currently in use,
1131 		 * allowing the table to shrink.
1132 		 */
1133 		i = fdp->fd_nfiles;
1134 		while (i >= 2 * NDEXTENT && i > fdp->fd_lastfile * 2)
1135 			i /= 2;
1136 		newfdp->fd_ofiles = mallocarray(i, OFILESIZE, M_FILEDESC,
1137 		    M_WAITOK | M_ZERO);
1138 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1139 		newfdp->fd_nfiles = i;
1140 	}
1141 	if (NDHISLOTS(newfdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
1142 		newfdp->fd_himap = mallocarray(NDHISLOTS(newfdp->fd_nfiles),
1143 		    sizeof(u_int), M_FILEDESC, M_WAITOK | M_ZERO);
1144 		newfdp->fd_lomap = mallocarray(NDLOSLOTS(newfdp->fd_nfiles),
1145 		    sizeof(u_int), M_FILEDESC, M_WAITOK | M_ZERO);
1146 	}
1147 	newfdp->fd_freefile = fdp->fd_freefile;
1148 	newfdp->fd_flags = fdp->fd_flags;
1149 	newfdp->fd_cmask = fdp->fd_cmask;
1150 
1151 	for (i = 0; i <= fdp->fd_lastfile; i++) {
1152 		struct file *fp = fdp->fd_ofiles[i];
1153 
1154 		if (fp != NULL) {
1155 			/*
1156 			 * XXX Gruesome hack. If count gets too high, fail
1157 			 * to copy an fd, since fdcopy()'s callers do not
1158 			 * permit it to indicate failure yet.
1159 			 * Meanwhile, kqueue files have to be
1160 			 * tied to the process that opened them to enforce
1161 			 * their internal consistency, so close them here.
1162 			 */
1163 			if (fp->f_count >= FDUP_MAX_COUNT ||
1164 			    fp->f_type == DTYPE_KQUEUE) {
1165 				if (i < newfdp->fd_freefile)
1166 					newfdp->fd_freefile = i;
1167 				continue;
1168 			}
1169 
1170 			FREF(fp);
1171 			newfdp->fd_ofiles[i] = fp;
1172 			newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1173 			fd_used(newfdp, i);
1174 		}
1175 	}
1176 	fdpunlock(fdp);
1177 
1178 	return (newfdp);
1179 }
1180 
1181 /*
1182  * Release a filedesc structure.
1183  */
1184 void
1185 fdfree(struct proc *p)
1186 {
1187 	struct filedesc *fdp = p->p_fd;
1188 	struct file *fp;
1189 	int fd;
1190 
1191 	if (--fdp->fd_refcnt > 0)
1192 		return;
1193 	for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
1194 		fp = fdp->fd_ofiles[fd];
1195 		if (fp != NULL) {
1196 			fdp->fd_ofiles[fd] = NULL;
1197 			knote_fdclose(p, fd);
1198 			 /* closef() expects a refcount of 2 */
1199 			FREF(fp);
1200 			(void) closef(fp, p);
1201 		}
1202 	}
1203 	p->p_fd = NULL;
1204 	if (fdp->fd_nfiles > NDFILE)
1205 		free(fdp->fd_ofiles, M_FILEDESC, fdp->fd_nfiles * OFILESIZE);
1206 	if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
1207 		free(fdp->fd_himap, M_FILEDESC,
1208 		    NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int));
1209 		free(fdp->fd_lomap, M_FILEDESC,
1210 		    NDLOSLOTS(fdp->fd_nfiles) * sizeof(u_int));
1211 	}
1212 	if (fdp->fd_cdir)
1213 		vrele(fdp->fd_cdir);
1214 	if (fdp->fd_rdir)
1215 		vrele(fdp->fd_rdir);
1216 	pool_put(&fdesc_pool, fdp);
1217 }
1218 
1219 /*
1220  * Internal form of close.
1221  * Decrement reference count on file structure.
1222  * Note: p may be NULL when closing a file
1223  * that was being passed in a message.
1224  *
1225  * The fp must have its usecount bumped and will be FRELEd here.
1226  */
1227 int
1228 closef(struct file *fp, struct proc *p)
1229 {
1230 	struct filedesc *fdp;
1231 
1232 	if (fp == NULL)
1233 		return (0);
1234 
1235 	KASSERTMSG(fp->f_count >= 2, "count (%u) < 2", fp->f_count);
1236 
1237 	atomic_dec_int(&fp->f_count);
1238 
1239 	/*
1240 	 * POSIX record locking dictates that any close releases ALL
1241 	 * locks owned by this process.  This is handled by setting
1242 	 * a flag in the unlock to free ONLY locks obeying POSIX
1243 	 * semantics, and not to free BSD-style file locks.
1244 	 * If the descriptor was in a message, POSIX-style locks
1245 	 * aren't passed with the descriptor.
1246 	 */
1247 
1248 	if (p && ((fdp = p->p_fd) != NULL) &&
1249 	    (fdp->fd_flags & FD_ADVLOCK) &&
1250 	    fp->f_type == DTYPE_VNODE) {
1251 		struct vnode *vp = fp->f_data;
1252 		struct flock lf;
1253 
1254 		lf.l_whence = SEEK_SET;
1255 		lf.l_start = 0;
1256 		lf.l_len = 0;
1257 		lf.l_type = F_UNLCK;
1258 		(void) VOP_ADVLOCK(vp, fdp, F_UNLCK, &lf, F_POSIX);
1259 	}
1260 
1261 	return (FRELE(fp, p));
1262 }
1263 
1264 int
1265 fdrop(struct file *fp, struct proc *p)
1266 {
1267 	int error;
1268 
1269 	KASSERTMSG(fp->f_count == 0, "count (%u) != 0", fp->f_count);
1270 
1271 	mtx_enter(&fhdlk);
1272 	if (fp->f_iflags & FIF_INSERTED)
1273 		LIST_REMOVE(fp, f_list);
1274 	mtx_leave(&fhdlk);
1275 
1276 	if (fp->f_ops)
1277 		error = (*fp->f_ops->fo_close)(fp, p);
1278 	else
1279 		error = 0;
1280 
1281 	crfree(fp->f_cred);
1282 	atomic_dec_int(&numfiles);
1283 	pool_put(&file_pool, fp);
1284 
1285 	return (error);
1286 }
1287 
1288 /*
1289  * Apply an advisory lock on a file descriptor.
1290  *
1291  * Just attempt to get a record lock of the requested type on
1292  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1293  */
1294 int
1295 sys_flock(struct proc *p, void *v, register_t *retval)
1296 {
1297 	struct sys_flock_args /* {
1298 		syscallarg(int) fd;
1299 		syscallarg(int) how;
1300 	} */ *uap = v;
1301 	int fd = SCARG(uap, fd);
1302 	int how = SCARG(uap, how);
1303 	struct filedesc *fdp = p->p_fd;
1304 	struct file *fp;
1305 	struct vnode *vp;
1306 	struct flock lf;
1307 	int error;
1308 
1309 	if ((fp = fd_getfile(fdp, fd)) == NULL)
1310 		return (EBADF);
1311 	if (fp->f_type != DTYPE_VNODE) {
1312 		error = EOPNOTSUPP;
1313 		goto out;
1314 	}
1315 	vp = fp->f_data;
1316 	lf.l_whence = SEEK_SET;
1317 	lf.l_start = 0;
1318 	lf.l_len = 0;
1319 	if (how & LOCK_UN) {
1320 		lf.l_type = F_UNLCK;
1321 		atomic_clearbits_int(&fp->f_iflags, FIF_HASLOCK);
1322 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1323 		goto out;
1324 	}
1325 	if (how & LOCK_EX)
1326 		lf.l_type = F_WRLCK;
1327 	else if (how & LOCK_SH)
1328 		lf.l_type = F_RDLCK;
1329 	else {
1330 		error = EINVAL;
1331 		goto out;
1332 	}
1333 	atomic_setbits_int(&fp->f_iflags, FIF_HASLOCK);
1334 	if (how & LOCK_NB)
1335 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
1336 	else
1337 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
1338 out:
1339 	FRELE(fp, p);
1340 	return (error);
1341 }
1342 
1343 /*
1344  * File Descriptor pseudo-device driver (/dev/fd/).
1345  *
1346  * Opening minor device N dup()s the file (if any) connected to file
1347  * descriptor N belonging to the calling process.  Note that this driver
1348  * consists of only the ``open()'' routine, because all subsequent
1349  * references to this file will be direct to the other driver.
1350  */
1351 int
1352 filedescopen(dev_t dev, int mode, int type, struct proc *p)
1353 {
1354 
1355 	/*
1356 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1357 	 * the file descriptor being sought for duplication. The error
1358 	 * return ensures that the vnode for this device will be released
1359 	 * by vn_open. Open will detect this special error and take the
1360 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1361 	 * will simply report the error.
1362 	 */
1363 	p->p_dupfd = minor(dev);
1364 	return (ENODEV);
1365 }
1366 
1367 /*
1368  * Duplicate the specified descriptor to a free descriptor.
1369  */
1370 int
1371 dupfdopen(struct proc *p, int indx, int mode)
1372 {
1373 	struct filedesc *fdp = p->p_fd;
1374 	int dupfd = p->p_dupfd;
1375 	struct file *wfp;
1376 
1377 	fdpassertlocked(fdp);
1378 
1379 	/*
1380 	 * Assume that the filename was user-specified; applications do
1381 	 * not tend to open /dev/fd/# when they can just call dup()
1382 	 */
1383 	if ((p->p_p->ps_flags & (PS_SUGIDEXEC | PS_SUGID))) {
1384 		if (p->p_descfd == 255)
1385 			return (EPERM);
1386 		if (p->p_descfd != dupfd)
1387 			return (EPERM);
1388 	}
1389 
1390 	/*
1391 	 * If the to-be-dup'd fd number is greater than the allowed number
1392 	 * of file descriptors, or the fd to be dup'd has already been
1393 	 * closed, reject. Note, there is no need to check for new == old
1394 	 * because fd_getfile will return NULL if the file at indx is
1395 	 * newly created by falloc.
1396 	 */
1397 	if ((wfp = fd_getfile(fdp, dupfd)) == NULL)
1398 		return (EBADF);
1399 
1400 	/*
1401 	 * Check that the mode the file is being opened for is a
1402 	 * subset of the mode of the existing descriptor.
1403 	 */
1404 	if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
1405 		FRELE(wfp, p);
1406 		return (EACCES);
1407 	}
1408 	if (wfp->f_count >= FDUP_MAX_COUNT) {
1409 		FRELE(wfp, p);
1410 		return (EDEADLK);
1411 	}
1412 
1413 	KASSERT(wfp->f_iflags & FIF_INSERTED);
1414 
1415 	mtx_enter(&fdp->fd_fplock);
1416 	KASSERT(fdp->fd_ofiles[indx] == NULL);
1417 	fdp->fd_ofiles[indx] = wfp;
1418 	mtx_leave(&fdp->fd_fplock);
1419 
1420 	fdp->fd_ofileflags[indx] = (fdp->fd_ofileflags[indx] & UF_EXCLOSE) |
1421 	    (fdp->fd_ofileflags[dupfd] & ~UF_EXCLOSE);
1422 
1423 	return (0);
1424 }
1425 
1426 /*
1427  * Close any files on exec?
1428  */
1429 void
1430 fdcloseexec(struct proc *p)
1431 {
1432 	struct filedesc *fdp = p->p_fd;
1433 	int fd;
1434 
1435 	fdplock(fdp);
1436 	for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
1437 		fdp->fd_ofileflags[fd] &= ~UF_PLEDGED;
1438 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE) {
1439 			/* fdrelease() unlocks fdp. */
1440 			(void) fdrelease(p, fd);
1441 			fdplock(fdp);
1442 		}
1443 	}
1444 	fdpunlock(fdp);
1445 }
1446 
1447 int
1448 sys_closefrom(struct proc *p, void *v, register_t *retval)
1449 {
1450 	struct sys_closefrom_args *uap = v;
1451 	struct filedesc *fdp = p->p_fd;
1452 	u_int startfd, i;
1453 
1454 	startfd = SCARG(uap, fd);
1455 	fdplock(fdp);
1456 
1457 	if (startfd > fdp->fd_lastfile) {
1458 		fdpunlock(fdp);
1459 		return (EBADF);
1460 	}
1461 
1462 	for (i = startfd; i <= fdp->fd_lastfile; i++) {
1463 		/* fdrelease() unlocks fdp. */
1464 		fdrelease(p, i);
1465 		fdplock(fdp);
1466 	}
1467 
1468 	fdpunlock(fdp);
1469 	return (0);
1470 }
1471 
1472 int
1473 sys_getdtablecount(struct proc *p, void *v, register_t *retval)
1474 {
1475 	*retval = p->p_fd->fd_openfd;
1476 	return (0);
1477 }
1478