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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/domain.h>
48 #include <sys/fcntl.h>
50 #include <sys/filedesc.h>
51 #include <sys/filio.h>
53 #include <sys/kernel.h>
54 #include <sys/limits.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/mqueue.h>
59 #include <sys/mutex.h>
60 #include <sys/namei.h>
63 #include <sys/protosw.h>
64 #include <sys/resourcevar.h>
65 #include <sys/signalvar.h>
66 #include <sys/socketvar.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysproto.h>
72 #include <sys/unistd.h>
74 #include <sys/vnode.h>
76 #include <security/audit/audit.h>
82 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
83 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
84 "file desc to leader structures");
85 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
87 static uma_zone_t file_zone;
90 /* How to treat 'new' parameter when allocating a fd for do_dup(). */
91 enum dup_type { DUP_VARIABLE, DUP_FIXED };
93 static int do_dup(struct thread *td, enum dup_type type, int old, int new,
95 static int fd_first_free(struct filedesc *, int, int);
96 static int fd_last_used(struct filedesc *, int, int);
97 static void fdgrowtable(struct filedesc *, int);
98 static void fdunused(struct filedesc *fdp, int fd);
99 static void fdused(struct filedesc *fdp, int fd);
102 * A process is initially started out with NDFILE descriptors stored within
103 * this structure, selected to be enough for typical applications based on
104 * the historical limit of 20 open files (and the usage of descriptors by
105 * shells). If these descriptors are exhausted, a larger descriptor table
106 * may be allocated, up to a process' resource limit; the internal arrays
110 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
111 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
112 #define NDSLOT(x) ((x) / NDENTRIES)
113 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
114 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
117 * Storage required per open file descriptor.
119 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
122 * Basic allocation of descriptors:
123 * one of the above, plus arrays for NDFILE descriptors.
126 struct filedesc fd_fd;
128 * These arrays are used when the number of open files is
129 * <= NDFILE, and are then pointed to by the pointers above.
131 struct file *fd_dfiles[NDFILE];
132 char fd_dfileflags[NDFILE];
133 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
137 * Descriptor management.
139 volatile int openfiles; /* actual number of open files */
140 struct mtx sigio_lock; /* mtx to protect pointers to sigio */
141 void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
143 /* A mutex to protect the association between a proc and filedesc. */
144 static struct mtx fdesc_mtx;
147 * Find the first zero bit in the given bitmap, starting at low and not
148 * exceeding size - 1.
151 fd_first_free(struct filedesc *fdp, int low, int size)
153 NDSLOTTYPE *map = fdp->fd_map;
161 if (low % NDENTRIES) {
162 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
163 if ((mask &= ~map[off]) != 0UL)
164 return (off * NDENTRIES + ffsl(mask) - 1);
167 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
168 if (map[off] != ~0UL)
169 return (off * NDENTRIES + ffsl(~map[off]) - 1);
174 * Find the highest non-zero bit in the given bitmap, starting at low and
175 * not exceeding size - 1.
178 fd_last_used(struct filedesc *fdp, int low, int size)
180 NDSLOTTYPE *map = fdp->fd_map;
188 if (size % NDENTRIES) {
189 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
190 if ((mask &= map[off]) != 0)
191 return (off * NDENTRIES + flsl(mask) - 1);
194 for (minoff = NDSLOT(low); off >= minoff; --off)
196 return (off * NDENTRIES + flsl(map[off]) - 1);
201 fdisused(struct filedesc *fdp, int fd)
203 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
204 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
205 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
209 * Mark a file descriptor as used.
212 fdused(struct filedesc *fdp, int fd)
215 FILEDESC_XLOCK_ASSERT(fdp);
216 KASSERT(!fdisused(fdp, fd),
217 ("fd already used"));
219 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
220 if (fd > fdp->fd_lastfile)
221 fdp->fd_lastfile = fd;
222 if (fd == fdp->fd_freefile)
223 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
227 * Mark a file descriptor as unused.
230 fdunused(struct filedesc *fdp, int fd)
233 FILEDESC_XLOCK_ASSERT(fdp);
234 KASSERT(fdisused(fdp, fd),
235 ("fd is already unused"));
236 KASSERT(fdp->fd_ofiles[fd] == NULL,
237 ("fd is still in use"));
239 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
240 if (fd < fdp->fd_freefile)
241 fdp->fd_freefile = fd;
242 if (fd == fdp->fd_lastfile)
243 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
247 * System calls on descriptors.
249 #ifndef _SYS_SYSPROTO_H_
250 struct getdtablesize_args {
256 getdtablesize(struct thread *td, struct getdtablesize_args *uap)
258 struct proc *p = td->td_proc;
262 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
268 * Duplicate a file descriptor to a particular value.
270 * Note: keep in mind that a potential race condition exists when closing
271 * descriptors from a shared descriptor table (via rfork).
273 #ifndef _SYS_SYSPROTO_H_
281 dup2(struct thread *td, struct dup2_args *uap)
284 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
289 * Duplicate a file descriptor.
291 #ifndef _SYS_SYSPROTO_H_
298 dup(struct thread *td, struct dup_args *uap)
301 return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
305 * The file control system call.
307 #ifndef _SYS_SYSPROTO_H_
316 fcntl(struct thread *td, struct fcntl_args *uap)
327 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
336 error = kern_fcntl(td, uap->fd, uap->cmd, arg);
339 if (uap->cmd == F_GETLK)
340 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
344 static inline struct file *
345 fdtofp(int fd, struct filedesc *fdp)
349 FILEDESC_LOCK_ASSERT(fdp);
350 if ((unsigned)fd >= fdp->fd_nfiles ||
351 (fp = fdp->fd_ofiles[fd]) == NULL)
357 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
359 struct filedesc *fdp;
378 if ((fp = fdtofp(fd, fdp)) == NULL) {
379 FILEDESC_SUNLOCK(fdp);
383 FILEDESC_SUNLOCK(fdp);
386 if (newmin >= lim_cur(p, RLIMIT_NOFILE) ||
387 newmin >= maxfilesperproc) {
393 error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
398 if ((fp = fdtofp(fd, fdp)) == NULL) {
399 FILEDESC_SUNLOCK(fdp);
403 pop = &fdp->fd_ofileflags[fd];
404 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
405 FILEDESC_SUNLOCK(fdp);
410 if ((fp = fdtofp(fd, fdp)) == NULL) {
411 FILEDESC_XUNLOCK(fdp);
415 pop = &fdp->fd_ofileflags[fd];
416 *pop = (*pop &~ UF_EXCLOSE) |
417 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
418 FILEDESC_XUNLOCK(fdp);
423 if ((fp = fdtofp(fd, fdp)) == NULL) {
424 FILEDESC_SUNLOCK(fdp);
428 td->td_retval[0] = OFLAGS(fp->f_flag);
429 FILEDESC_SUNLOCK(fdp);
434 if ((fp = fdtofp(fd, fdp)) == NULL) {
435 FILEDESC_SUNLOCK(fdp);
440 FILEDESC_SUNLOCK(fdp);
442 tmp = flg = fp->f_flag;
444 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
445 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
446 tmp = fp->f_flag & FNONBLOCK;
447 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
452 tmp = fp->f_flag & FASYNC;
453 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
458 atomic_clear_int(&fp->f_flag, FNONBLOCK);
460 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
466 if ((fp = fdtofp(fd, fdp)) == NULL) {
467 FILEDESC_SUNLOCK(fdp);
472 FILEDESC_SUNLOCK(fdp);
473 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
475 td->td_retval[0] = tmp;
481 if ((fp = fdtofp(fd, fdp)) == NULL) {
482 FILEDESC_SUNLOCK(fdp);
487 FILEDESC_SUNLOCK(fdp);
489 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
495 /* FALLTHROUGH F_SETLK */
499 if ((fp = fdtofp(fd, fdp)) == NULL) {
500 FILEDESC_SUNLOCK(fdp);
504 if (fp->f_type != DTYPE_VNODE) {
505 FILEDESC_SUNLOCK(fdp);
509 flp = (struct flock *)arg;
510 if (flp->l_whence == SEEK_CUR) {
511 if (fp->f_offset < 0 ||
513 fp->f_offset > OFF_MAX - flp->l_start)) {
514 FILEDESC_SUNLOCK(fdp);
518 flp->l_start += fp->f_offset;
522 * VOP_ADVLOCK() may block.
525 FILEDESC_SUNLOCK(fdp);
527 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
528 switch (flp->l_type) {
530 if ((fp->f_flag & FREAD) == 0) {
534 PROC_LOCK(p->p_leader);
535 p->p_leader->p_flag |= P_ADVLOCK;
536 PROC_UNLOCK(p->p_leader);
537 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
541 if ((fp->f_flag & FWRITE) == 0) {
545 PROC_LOCK(p->p_leader);
546 p->p_leader->p_flag |= P_ADVLOCK;
547 PROC_UNLOCK(p->p_leader);
548 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
552 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
559 VFS_UNLOCK_GIANT(vfslocked);
561 /* Check for race with close */
563 if ((unsigned) fd >= fdp->fd_nfiles ||
564 fp != fdp->fd_ofiles[fd]) {
565 FILEDESC_SUNLOCK(fdp);
566 flp->l_whence = SEEK_SET;
569 flp->l_type = F_UNLCK;
570 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
571 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
572 F_UNLCK, flp, F_POSIX);
573 VFS_UNLOCK_GIANT(vfslocked);
576 FILEDESC_SUNLOCK(fdp);
582 if ((fp = fdtofp(fd, fdp)) == NULL) {
583 FILEDESC_SUNLOCK(fdp);
587 if (fp->f_type != DTYPE_VNODE) {
588 FILEDESC_SUNLOCK(fdp);
592 flp = (struct flock *)arg;
593 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
594 flp->l_type != F_UNLCK) {
595 FILEDESC_SUNLOCK(fdp);
599 if (flp->l_whence == SEEK_CUR) {
600 if ((flp->l_start > 0 &&
601 fp->f_offset > OFF_MAX - flp->l_start) ||
603 fp->f_offset < OFF_MIN - flp->l_start)) {
604 FILEDESC_SUNLOCK(fdp);
608 flp->l_start += fp->f_offset;
611 * VOP_ADVLOCK() may block.
614 FILEDESC_SUNLOCK(fdp);
616 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
617 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
619 VFS_UNLOCK_GIANT(vfslocked);
627 VFS_UNLOCK_GIANT(vfslocked);
632 * Common code for dup, dup2, and fcntl(F_DUPFD).
635 do_dup(struct thread *td, enum dup_type type, int old, int new,
638 struct filedesc *fdp;
642 int error, holdleaders, maxfd;
644 KASSERT((type == DUP_VARIABLE || type == DUP_FIXED),
645 ("invalid dup type %d", type));
651 * Verify we have a valid descriptor to dup from and possibly to
654 if (old < 0 || new < 0)
657 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
663 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
664 FILEDESC_XUNLOCK(fdp);
667 if (type == DUP_FIXED && old == new) {
669 FILEDESC_XUNLOCK(fdp);
672 fp = fdp->fd_ofiles[old];
676 * If the caller specified a file descriptor, make sure the file
677 * table is large enough to hold it, and grab it. Otherwise, just
678 * allocate a new descriptor the usual way. Since the filedesc
679 * lock may be temporarily dropped in the process, we have to look
682 if (type == DUP_FIXED) {
683 if (new >= fdp->fd_nfiles)
684 fdgrowtable(fdp, new + 1);
685 if (fdp->fd_ofiles[new] == NULL)
688 if ((error = fdalloc(td, new, &new)) != 0) {
689 FILEDESC_XUNLOCK(fdp);
696 * If the old file changed out from under us then treat it as a
697 * bad file descriptor. Userland should do its own locking to
700 if (fdp->fd_ofiles[old] != fp) {
701 /* we've allocated a descriptor which we won't use */
702 if (fdp->fd_ofiles[new] == NULL)
704 FILEDESC_XUNLOCK(fdp);
709 ("new fd is same as old"));
712 * Save info on the descriptor being overwritten. We cannot close
713 * it without introducing an ownership race for the slot, since we
714 * need to drop the filedesc lock to call closef().
716 * XXX this duplicates parts of close().
718 delfp = fdp->fd_ofiles[new];
721 if (td->td_proc->p_fdtol != NULL) {
723 * Ask fdfree() to sleep to ensure that all relevant
724 * process leaders can be traversed in closef().
726 fdp->fd_holdleaderscount++;
732 * Duplicate the source descriptor
734 fdp->fd_ofiles[new] = fp;
735 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
736 if (new > fdp->fd_lastfile)
737 fdp->fd_lastfile = new;
741 * If we dup'd over a valid file, we now own the reference to it
742 * and must dispose of it using closef() semantics (as if a
743 * close() were performed on it).
745 * XXX this duplicates parts of close().
748 knote_fdclose(td, new);
749 if (delfp->f_type == DTYPE_MQUEUE)
750 mq_fdclose(td, new, delfp);
751 FILEDESC_XUNLOCK(fdp);
752 (void) closef(delfp, td);
755 fdp->fd_holdleaderscount--;
756 if (fdp->fd_holdleaderscount == 0 &&
757 fdp->fd_holdleaderswakeup != 0) {
758 fdp->fd_holdleaderswakeup = 0;
759 wakeup(&fdp->fd_holdleaderscount);
761 FILEDESC_XUNLOCK(fdp);
764 FILEDESC_XUNLOCK(fdp);
770 * If sigio is on the list associated with a process or process group,
771 * disable signalling from the device, remove sigio from the list and
775 funsetown(struct sigio **sigiop)
785 *(sigio->sio_myref) = NULL;
786 if ((sigio)->sio_pgid < 0) {
787 struct pgrp *pg = (sigio)->sio_pgrp;
789 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
793 struct proc *p = (sigio)->sio_proc;
795 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
800 crfree(sigio->sio_ucred);
801 FREE(sigio, M_SIGIO);
805 * Free a list of sigio structures.
806 * We only need to lock the SIGIO_LOCK because we have made ourselves
807 * inaccessible to callers of fsetown and therefore do not need to lock
808 * the proc or pgrp struct for the list manipulation.
811 funsetownlst(struct sigiolst *sigiolst)
817 sigio = SLIST_FIRST(sigiolst);
824 * Every entry of the list should belong
825 * to a single proc or pgrp.
827 if (sigio->sio_pgid < 0) {
828 pg = sigio->sio_pgrp;
829 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
830 } else /* if (sigio->sio_pgid > 0) */ {
832 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
836 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
837 *(sigio->sio_myref) = NULL;
839 KASSERT(sigio->sio_pgid < 0,
840 ("Proc sigio in pgrp sigio list"));
841 KASSERT(sigio->sio_pgrp == pg,
842 ("Bogus pgrp in sigio list"));
844 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
847 } else /* if (p != NULL) */ {
848 KASSERT(sigio->sio_pgid > 0,
849 ("Pgrp sigio in proc sigio list"));
850 KASSERT(sigio->sio_proc == p,
851 ("Bogus proc in sigio list"));
853 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
858 crfree(sigio->sio_ucred);
859 FREE(sigio, M_SIGIO);
866 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
868 * After permission checking, add a sigio structure to the sigio list for
869 * the process or process group.
872 fsetown(pid_t pgid, struct sigio **sigiop)
886 /* Allocate and fill in the new sigio out of locks. */
887 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
888 sigio->sio_pgid = pgid;
889 sigio->sio_ucred = crhold(curthread->td_ucred);
890 sigio->sio_myref = sigiop;
892 sx_slock(&proctree_lock);
901 * Policy - Don't allow a process to FSETOWN a process
902 * in another session.
904 * Remove this test to allow maximum flexibility or
905 * restrict FSETOWN to the current process or process
906 * group for maximum safety.
909 if (proc->p_session != curthread->td_proc->p_session) {
915 } else /* if (pgid < 0) */ {
916 pgrp = pgfind(-pgid);
924 * Policy - Don't allow a process to FSETOWN a process
925 * in another session.
927 * Remove this test to allow maximum flexibility or
928 * restrict FSETOWN to the current process or process
929 * group for maximum safety.
931 if (pgrp->pg_session != curthread->td_proc->p_session) {
942 * Since funsetownlst() is called without the proctree
943 * locked, we need to check for P_WEXIT.
944 * XXX: is ESRCH correct?
946 if ((proc->p_flag & P_WEXIT) != 0) {
951 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
952 sigio->sio_proc = proc;
956 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
957 sigio->sio_pgrp = pgrp;
960 sx_sunlock(&proctree_lock);
967 sx_sunlock(&proctree_lock);
968 crfree(sigio->sio_ucred);
969 FREE(sigio, M_SIGIO);
974 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
978 struct sigio **sigiop;
983 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
989 * Close a file descriptor.
991 #ifndef _SYS_SYSPROTO_H_
1000 struct close_args *uap;
1003 return (kern_close(td, uap->fd));
1011 struct filedesc *fdp;
1018 fdp = td->td_proc->p_fd;
1020 AUDIT_SYSCLOSE(td, fd);
1022 FILEDESC_XLOCK(fdp);
1023 if ((unsigned)fd >= fdp->fd_nfiles ||
1024 (fp = fdp->fd_ofiles[fd]) == NULL) {
1025 FILEDESC_XUNLOCK(fdp);
1028 fdp->fd_ofiles[fd] = NULL;
1029 fdp->fd_ofileflags[fd] = 0;
1031 if (td->td_proc->p_fdtol != NULL) {
1033 * Ask fdfree() to sleep to ensure that all relevant
1034 * process leaders can be traversed in closef().
1036 fdp->fd_holdleaderscount++;
1041 * We now hold the fp reference that used to be owned by the
1042 * descriptor array. We have to unlock the FILEDESC *AFTER*
1043 * knote_fdclose to prevent a race of the fd getting opened, a knote
1044 * added, and deleteing a knote for the new fd.
1046 knote_fdclose(td, fd);
1047 if (fp->f_type == DTYPE_MQUEUE)
1048 mq_fdclose(td, fd, fp);
1049 FILEDESC_XUNLOCK(fdp);
1051 error = closef(fp, td);
1053 FILEDESC_XLOCK(fdp);
1054 fdp->fd_holdleaderscount--;
1055 if (fdp->fd_holdleaderscount == 0 &&
1056 fdp->fd_holdleaderswakeup != 0) {
1057 fdp->fd_holdleaderswakeup = 0;
1058 wakeup(&fdp->fd_holdleaderscount);
1060 FILEDESC_XUNLOCK(fdp);
1065 #if defined(COMPAT_43)
1067 * Return status information about a file descriptor.
1069 #ifndef _SYS_SYSPROTO_H_
1070 struct ofstat_args {
1077 ofstat(struct thread *td, struct ofstat_args *uap)
1083 error = kern_fstat(td, uap->fd, &ub);
1086 error = copyout(&oub, uap->sb, sizeof(oub));
1090 #endif /* COMPAT_43 */
1093 * Return status information about a file descriptor.
1095 #ifndef _SYS_SYSPROTO_H_
1103 fstat(struct thread *td, struct fstat_args *uap)
1108 error = kern_fstat(td, uap->fd, &ub);
1110 error = copyout(&ub, uap->sb, sizeof(ub));
1115 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1122 if ((error = fget(td, fd, &fp)) != 0)
1125 AUDIT_ARG(file, td->td_proc, fp);
1127 error = fo_stat(fp, sbp, td->td_ucred, td);
1133 * Return status information about a file descriptor.
1135 #ifndef _SYS_SYSPROTO_H_
1136 struct nfstat_args {
1143 nfstat(struct thread *td, struct nfstat_args *uap)
1149 error = kern_fstat(td, uap->fd, &ub);
1151 cvtnstat(&ub, &nub);
1152 error = copyout(&nub, uap->sb, sizeof(nub));
1158 * Return pathconf information about a file descriptor.
1160 #ifndef _SYS_SYSPROTO_H_
1161 struct fpathconf_args {
1168 fpathconf(struct thread *td, struct fpathconf_args *uap)
1174 if ((error = fget(td, uap->fd, &fp)) != 0)
1177 /* If asynchronous I/O is available, it works for all descriptors. */
1178 if (uap->name == _PC_ASYNC_IO) {
1179 td->td_retval[0] = async_io_version;
1185 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1186 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1187 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1189 VFS_UNLOCK_GIANT(vfslocked);
1190 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1191 if (uap->name != _PC_PIPE_BUF) {
1194 td->td_retval[0] = PIPE_BUF;
1206 * Grow the file table to accomodate (at least) nfd descriptors. This may
1207 * block and drop the filedesc lock, but it will reacquire it before
1211 fdgrowtable(struct filedesc *fdp, int nfd)
1213 struct file **ntable;
1215 int nnfiles, onfiles;
1218 FILEDESC_XLOCK_ASSERT(fdp);
1220 KASSERT(fdp->fd_nfiles > 0,
1221 ("zero-length file table"));
1223 /* compute the size of the new table */
1224 onfiles = fdp->fd_nfiles;
1225 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1226 if (nnfiles <= onfiles)
1227 /* the table is already large enough */
1230 /* allocate a new table and (if required) new bitmaps */
1231 FILEDESC_XUNLOCK(fdp);
1232 MALLOC(ntable, struct file **, nnfiles * OFILESIZE,
1233 M_FILEDESC, M_ZERO | M_WAITOK);
1234 nfileflags = (char *)&ntable[nnfiles];
1235 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1236 MALLOC(nmap, NDSLOTTYPE *, NDSLOTS(nnfiles) * NDSLOTSIZE,
1237 M_FILEDESC, M_ZERO | M_WAITOK);
1240 FILEDESC_XLOCK(fdp);
1243 * We now have new tables ready to go. Since we dropped the
1244 * filedesc lock to call malloc(), watch out for a race.
1246 onfiles = fdp->fd_nfiles;
1247 if (onfiles >= nnfiles) {
1248 /* we lost the race, but that's OK */
1249 free(ntable, M_FILEDESC);
1251 free(nmap, M_FILEDESC);
1254 bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1255 bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1256 if (onfiles > NDFILE)
1257 free(fdp->fd_ofiles, M_FILEDESC);
1258 fdp->fd_ofiles = ntable;
1259 fdp->fd_ofileflags = nfileflags;
1260 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1261 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1262 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1263 free(fdp->fd_map, M_FILEDESC);
1266 fdp->fd_nfiles = nnfiles;
1270 * Allocate a file descriptor for the process.
1273 fdalloc(struct thread *td, int minfd, int *result)
1275 struct proc *p = td->td_proc;
1276 struct filedesc *fdp = p->p_fd;
1279 FILEDESC_XLOCK_ASSERT(fdp);
1281 if (fdp->fd_freefile > minfd)
1282 minfd = fdp->fd_freefile;
1285 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1289 * Search the bitmap for a free descriptor. If none is found, try
1290 * to grow the file table. Keep at it until we either get a file
1291 * descriptor or run into process or system limits; fdgrowtable()
1292 * may drop the filedesc lock, so we're in a race.
1295 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1298 if (fd < fdp->fd_nfiles)
1300 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
1304 * Perform some sanity checks, then mark the file descriptor as
1305 * used and return it to the caller.
1307 KASSERT(!fdisused(fdp, fd),
1308 ("fd_first_free() returned non-free descriptor"));
1309 KASSERT(fdp->fd_ofiles[fd] == NULL,
1310 ("free descriptor isn't"));
1311 fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
1318 * Check to see whether n user file descriptors are available to the process
1322 fdavail(struct thread *td, int n)
1324 struct proc *p = td->td_proc;
1325 struct filedesc *fdp = td->td_proc->p_fd;
1329 FILEDESC_LOCK_ASSERT(fdp);
1332 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1334 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1336 last = min(fdp->fd_nfiles, lim);
1337 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1338 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1339 if (*fpp == NULL && --n <= 0)
1346 * Create a new open file structure and allocate a file decriptor for the
1347 * process that refers to it. We add one reference to the file for the
1348 * descriptor table and one reference for resultfp. This is to prevent us
1349 * being preempted and the entry in the descriptor table closed after we
1350 * release the FILEDESC lock.
1353 falloc(struct thread *td, struct file **resultfp, int *resultfd)
1355 struct proc *p = td->td_proc;
1358 int maxuserfiles = maxfiles - (maxfiles / 20);
1359 static struct timeval lastfail;
1362 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1363 if ((openfiles >= maxuserfiles &&
1364 priv_check(td, PRIV_MAXFILES) != 0) ||
1365 openfiles >= maxfiles) {
1366 if (ppsratecheck(&lastfail, &curfail, 1)) {
1367 printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
1368 td->td_ucred->cr_ruid);
1370 uma_zfree(file_zone, fp);
1373 atomic_add_int(&openfiles, 1);
1376 * If the process has file descriptor zero open, add the new file
1377 * descriptor to the list of open files at that point, otherwise
1378 * put it at the front of the list of open files.
1383 fp->f_cred = crhold(td->td_ucred);
1384 fp->f_ops = &badfileops;
1387 FILEDESC_XLOCK(p->p_fd);
1388 if ((error = fdalloc(td, 0, &i))) {
1389 FILEDESC_XUNLOCK(p->p_fd);
1395 p->p_fd->fd_ofiles[i] = fp;
1396 FILEDESC_XUNLOCK(p->p_fd);
1405 * Build a new filedesc structure from another.
1406 * Copy the current, root, and jail root vnode references.
1409 fdinit(struct filedesc *fdp)
1411 struct filedesc0 *newfdp;
1413 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1414 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1416 FILEDESC_XLOCK(fdp);
1417 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1418 if (newfdp->fd_fd.fd_cdir)
1419 VREF(newfdp->fd_fd.fd_cdir);
1420 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1421 if (newfdp->fd_fd.fd_rdir)
1422 VREF(newfdp->fd_fd.fd_rdir);
1423 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1424 if (newfdp->fd_fd.fd_jdir)
1425 VREF(newfdp->fd_fd.fd_jdir);
1426 FILEDESC_XUNLOCK(fdp);
1429 /* Create the file descriptor table. */
1430 newfdp->fd_fd.fd_refcnt = 1;
1431 newfdp->fd_fd.fd_holdcnt = 1;
1432 newfdp->fd_fd.fd_cmask = CMASK;
1433 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1434 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1435 newfdp->fd_fd.fd_nfiles = NDFILE;
1436 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1437 newfdp->fd_fd.fd_lastfile = -1;
1438 return (&newfdp->fd_fd);
1441 static struct filedesc *
1442 fdhold(struct proc *p)
1444 struct filedesc *fdp;
1446 mtx_lock(&fdesc_mtx);
1450 mtx_unlock(&fdesc_mtx);
1455 fddrop(struct filedesc *fdp)
1459 mtx_lock(&fdesc_mtx);
1460 i = --fdp->fd_holdcnt;
1461 mtx_unlock(&fdesc_mtx);
1465 FILEDESC_LOCK_DESTROY(fdp);
1466 FREE(fdp, M_FILEDESC);
1470 * Share a filedesc structure.
1473 fdshare(struct filedesc *fdp)
1476 FILEDESC_XLOCK(fdp);
1478 FILEDESC_XUNLOCK(fdp);
1483 * Unshare a filedesc structure, if necessary by making a copy
1486 fdunshare(struct proc *p, struct thread *td)
1489 FILEDESC_XLOCK(p->p_fd);
1490 if (p->p_fd->fd_refcnt > 1) {
1491 struct filedesc *tmp;
1493 FILEDESC_XUNLOCK(p->p_fd);
1494 tmp = fdcopy(p->p_fd);
1498 FILEDESC_XUNLOCK(p->p_fd);
1502 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1503 * this is to ease callers, not catch errors.
1506 fdcopy(struct filedesc *fdp)
1508 struct filedesc *newfdp;
1511 /* Certain daemons might not have file descriptors. */
1515 newfdp = fdinit(fdp);
1516 FILEDESC_SLOCK(fdp);
1517 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1518 FILEDESC_SUNLOCK(fdp);
1519 FILEDESC_XLOCK(newfdp);
1520 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1521 FILEDESC_XUNLOCK(newfdp);
1522 FILEDESC_SLOCK(fdp);
1524 /* copy everything except kqueue descriptors */
1525 newfdp->fd_freefile = -1;
1526 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1527 if (fdisused(fdp, i) &&
1528 fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE) {
1529 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1530 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1531 fhold(newfdp->fd_ofiles[i]);
1532 newfdp->fd_lastfile = i;
1534 if (newfdp->fd_freefile == -1)
1535 newfdp->fd_freefile = i;
1538 FILEDESC_SUNLOCK(fdp);
1539 FILEDESC_XLOCK(newfdp);
1540 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1541 if (newfdp->fd_ofiles[i] != NULL)
1543 FILEDESC_XUNLOCK(newfdp);
1544 FILEDESC_SLOCK(fdp);
1545 if (newfdp->fd_freefile == -1)
1546 newfdp->fd_freefile = i;
1547 newfdp->fd_cmask = fdp->fd_cmask;
1548 FILEDESC_SUNLOCK(fdp);
1553 * Release a filedesc structure.
1556 fdfree(struct thread *td)
1558 struct filedesc *fdp;
1561 struct filedesc_to_leader *fdtol;
1563 struct vnode *cdir, *jdir, *rdir, *vp;
1566 /* Certain daemons might not have file descriptors. */
1567 fdp = td->td_proc->p_fd;
1571 /* Check for special need to clear POSIX style locks */
1572 fdtol = td->td_proc->p_fdtol;
1573 if (fdtol != NULL) {
1574 FILEDESC_XLOCK(fdp);
1575 KASSERT(fdtol->fdl_refcount > 0,
1576 ("filedesc_to_refcount botch: fdl_refcount=%d",
1577 fdtol->fdl_refcount));
1578 if (fdtol->fdl_refcount == 1 &&
1579 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1580 for (i = 0, fpp = fdp->fd_ofiles;
1581 i <= fdp->fd_lastfile;
1584 (*fpp)->f_type != DTYPE_VNODE)
1588 FILEDESC_XUNLOCK(fdp);
1589 lf.l_whence = SEEK_SET;
1592 lf.l_type = F_UNLCK;
1594 locked = VFS_LOCK_GIANT(vp->v_mount);
1595 (void) VOP_ADVLOCK(vp,
1596 (caddr_t)td->td_proc->
1601 VFS_UNLOCK_GIANT(locked);
1602 FILEDESC_XLOCK(fdp);
1604 fpp = fdp->fd_ofiles + i;
1608 if (fdtol->fdl_refcount == 1) {
1609 if (fdp->fd_holdleaderscount > 0 &&
1610 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1612 * close() or do_dup() has cleared a reference
1613 * in a shared file descriptor table.
1615 fdp->fd_holdleaderswakeup = 1;
1616 sx_sleep(&fdp->fd_holdleaderscount,
1617 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1620 if (fdtol->fdl_holdcount > 0) {
1622 * Ensure that fdtol->fdl_leader remains
1623 * valid in closef().
1625 fdtol->fdl_wakeup = 1;
1626 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1631 fdtol->fdl_refcount--;
1632 if (fdtol->fdl_refcount == 0 &&
1633 fdtol->fdl_holdcount == 0) {
1634 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1635 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1638 td->td_proc->p_fdtol = NULL;
1639 FILEDESC_XUNLOCK(fdp);
1641 FREE(fdtol, M_FILEDESC_TO_LEADER);
1643 FILEDESC_XLOCK(fdp);
1644 i = --fdp->fd_refcnt;
1645 FILEDESC_XUNLOCK(fdp);
1649 * We are the last reference to the structure, so we can
1650 * safely assume it will not change out from under us.
1652 fpp = fdp->fd_ofiles;
1653 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1655 (void) closef(*fpp, td);
1657 FILEDESC_XLOCK(fdp);
1659 /* XXX This should happen earlier. */
1660 mtx_lock(&fdesc_mtx);
1661 td->td_proc->p_fd = NULL;
1662 mtx_unlock(&fdesc_mtx);
1664 if (fdp->fd_nfiles > NDFILE)
1665 FREE(fdp->fd_ofiles, M_FILEDESC);
1666 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1667 FREE(fdp->fd_map, M_FILEDESC);
1671 cdir = fdp->fd_cdir;
1672 fdp->fd_cdir = NULL;
1673 rdir = fdp->fd_rdir;
1674 fdp->fd_rdir = NULL;
1675 jdir = fdp->fd_jdir;
1676 fdp->fd_jdir = NULL;
1677 FILEDESC_XUNLOCK(fdp);
1680 locked = VFS_LOCK_GIANT(cdir->v_mount);
1682 VFS_UNLOCK_GIANT(locked);
1685 locked = VFS_LOCK_GIANT(rdir->v_mount);
1687 VFS_UNLOCK_GIANT(locked);
1690 locked = VFS_LOCK_GIANT(jdir->v_mount);
1692 VFS_UNLOCK_GIANT(locked);
1699 * For setugid programs, we don't want to people to use that setugidness
1700 * to generate error messages which write to a file which otherwise would
1701 * otherwise be off-limits to the process. We check for filesystems where
1702 * the vnode can change out from under us after execve (like [lin]procfs).
1704 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1705 * sufficient. We also don't check for setugidness since we know we are.
1708 is_unsafe(struct file *fp)
1710 if (fp->f_type == DTYPE_VNODE) {
1711 struct vnode *vp = fp->f_vnode;
1713 if ((vp->v_vflag & VV_PROCDEP) != 0)
1720 * Make this setguid thing safe, if at all possible.
1723 setugidsafety(struct thread *td)
1725 struct filedesc *fdp;
1728 /* Certain daemons might not have file descriptors. */
1729 fdp = td->td_proc->p_fd;
1734 * Note: fdp->fd_ofiles may be reallocated out from under us while
1735 * we are blocked in a close. Be careful!
1737 FILEDESC_XLOCK(fdp);
1738 for (i = 0; i <= fdp->fd_lastfile; i++) {
1741 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1744 knote_fdclose(td, i);
1746 * NULL-out descriptor prior to close to avoid
1747 * a race while close blocks.
1749 fp = fdp->fd_ofiles[i];
1750 fdp->fd_ofiles[i] = NULL;
1751 fdp->fd_ofileflags[i] = 0;
1753 FILEDESC_XUNLOCK(fdp);
1754 (void) closef(fp, td);
1755 FILEDESC_XLOCK(fdp);
1758 FILEDESC_XUNLOCK(fdp);
1762 * If a specific file object occupies a specific file descriptor, close the
1763 * file descriptor entry and drop a reference on the file object. This is a
1764 * convenience function to handle a subsequent error in a function that calls
1765 * falloc() that handles the race that another thread might have closed the
1766 * file descriptor out from under the thread creating the file object.
1769 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
1772 FILEDESC_XLOCK(fdp);
1773 if (fdp->fd_ofiles[idx] == fp) {
1774 fdp->fd_ofiles[idx] = NULL;
1776 FILEDESC_XUNLOCK(fdp);
1779 FILEDESC_XUNLOCK(fdp);
1783 * Close any files on exec?
1786 fdcloseexec(struct thread *td)
1788 struct filedesc *fdp;
1791 /* Certain daemons might not have file descriptors. */
1792 fdp = td->td_proc->p_fd;
1796 FILEDESC_XLOCK(fdp);
1799 * We cannot cache fd_ofiles or fd_ofileflags since operations
1800 * may block and rip them out from under us.
1802 for (i = 0; i <= fdp->fd_lastfile; i++) {
1803 if (fdp->fd_ofiles[i] != NULL &&
1804 (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
1805 (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
1808 knote_fdclose(td, i);
1810 * NULL-out descriptor prior to close to avoid
1811 * a race while close blocks.
1813 fp = fdp->fd_ofiles[i];
1814 fdp->fd_ofiles[i] = NULL;
1815 fdp->fd_ofileflags[i] = 0;
1817 if (fp->f_type == DTYPE_MQUEUE)
1818 mq_fdclose(td, i, fp);
1819 FILEDESC_XUNLOCK(fdp);
1820 (void) closef(fp, td);
1821 FILEDESC_XLOCK(fdp);
1824 FILEDESC_XUNLOCK(fdp);
1828 * It is unsafe for set[ug]id processes to be started with file
1829 * descriptors 0..2 closed, as these descriptors are given implicit
1830 * significance in the Standard C library. fdcheckstd() will create a
1831 * descriptor referencing /dev/null for each of stdin, stdout, and
1832 * stderr that is not already open.
1835 fdcheckstd(struct thread *td)
1837 struct filedesc *fdp;
1838 register_t retval, save;
1839 int i, error, devnull;
1841 fdp = td->td_proc->p_fd;
1844 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
1847 for (i = 0; i < 3; i++) {
1848 if (fdp->fd_ofiles[i] != NULL)
1851 save = td->td_retval[0];
1852 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
1854 devnull = td->td_retval[0];
1855 KASSERT(devnull == i, ("oof, we didn't get our fd"));
1856 td->td_retval[0] = save;
1860 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
1869 * Internal form of close. Decrement reference count on file structure.
1870 * Note: td may be NULL when closing a file that was being passed in a
1873 * XXXRW: Giant is not required for the caller, but often will be held; this
1874 * makes it moderately likely the Giant will be recursed in the VFS case.
1877 closef(struct file *fp, struct thread *td)
1881 struct filedesc_to_leader *fdtol;
1882 struct filedesc *fdp;
1885 * POSIX record locking dictates that any close releases ALL
1886 * locks owned by this process. This is handled by setting
1887 * a flag in the unlock to free ONLY locks obeying POSIX
1888 * semantics, and not to free BSD-style file locks.
1889 * If the descriptor was in a message, POSIX-style locks
1890 * aren't passed with the descriptor, and the thread pointer
1891 * will be NULL. Callers should be careful only to pass a
1892 * NULL thread pointer when there really is no owning
1893 * context that might have locks, or the locks will be
1896 if (fp->f_type == DTYPE_VNODE && td != NULL) {
1900 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1901 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1902 lf.l_whence = SEEK_SET;
1905 lf.l_type = F_UNLCK;
1906 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
1907 F_UNLCK, &lf, F_POSIX);
1909 fdtol = td->td_proc->p_fdtol;
1910 if (fdtol != NULL) {
1912 * Handle special case where file descriptor table is
1913 * shared between multiple process leaders.
1915 fdp = td->td_proc->p_fd;
1916 FILEDESC_XLOCK(fdp);
1917 for (fdtol = fdtol->fdl_next;
1918 fdtol != td->td_proc->p_fdtol;
1919 fdtol = fdtol->fdl_next) {
1920 if ((fdtol->fdl_leader->p_flag &
1923 fdtol->fdl_holdcount++;
1924 FILEDESC_XUNLOCK(fdp);
1925 lf.l_whence = SEEK_SET;
1928 lf.l_type = F_UNLCK;
1930 (void) VOP_ADVLOCK(vp,
1931 (caddr_t)fdtol->fdl_leader,
1932 F_UNLCK, &lf, F_POSIX);
1933 FILEDESC_XLOCK(fdp);
1934 fdtol->fdl_holdcount--;
1935 if (fdtol->fdl_holdcount == 0 &&
1936 fdtol->fdl_wakeup != 0) {
1937 fdtol->fdl_wakeup = 0;
1941 FILEDESC_XUNLOCK(fdp);
1943 VFS_UNLOCK_GIANT(vfslocked);
1945 return (fdrop(fp, td));
1949 * Initialize the file pointer with the specified properties.
1951 * The ops are set with release semantics to be certain that the flags, type,
1952 * and data are visible when ops is. This is to prevent ops methods from being
1953 * called with bad data.
1956 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
1961 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
1966 * Extract the file pointer associated with the specified descriptor for the
1967 * current user process.
1969 * If the descriptor doesn't exist, EBADF is returned.
1971 * If the descriptor exists but doesn't match 'flags' then return EBADF for
1972 * read attempts and EINVAL for write attempts.
1974 * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
1975 * It should be dropped with fdrop(). If it is not set, then the refcount
1976 * will not be bumped however the thread's filedesc struct will be returned
1977 * locked (for fgetsock).
1979 * If an error occured the non-zero error is returned and *fpp is set to
1980 * NULL. Otherwise *fpp is set and zero is returned.
1983 _fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
1985 struct filedesc *fdp;
1989 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
1991 FILEDESC_SLOCK(fdp);
1992 if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
1993 FILEDESC_SUNLOCK(fdp);
1998 * FREAD and FWRITE failure return EBADF as per POSIX.
2000 * Only one flag, or 0, may be specified.
2002 if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
2003 FILEDESC_SUNLOCK(fdp);
2006 if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
2007 FILEDESC_SUNLOCK(fdp);
2012 FILEDESC_SUNLOCK(fdp);
2019 fget(struct thread *td, int fd, struct file **fpp)
2022 return(_fget(td, fd, fpp, 0, 1));
2026 fget_read(struct thread *td, int fd, struct file **fpp)
2029 return(_fget(td, fd, fpp, FREAD, 1));
2033 fget_write(struct thread *td, int fd, struct file **fpp)
2036 return(_fget(td, fd, fpp, FWRITE, 1));
2040 * Like fget() but loads the underlying vnode, or returns an error if the
2041 * descriptor does not represent a vnode. Note that pipes use vnodes but
2042 * never have VM objects. The returned vnode will be vref()'d.
2044 * XXX: what about the unused flags ?
2047 _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
2053 if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
2055 if (fp->f_vnode == NULL) {
2061 FILEDESC_SUNLOCK(td->td_proc->p_fd);
2066 fgetvp(struct thread *td, int fd, struct vnode **vpp)
2069 return (_fgetvp(td, fd, vpp, 0));
2073 fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
2076 return (_fgetvp(td, fd, vpp, FREAD));
2081 fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
2084 return (_fgetvp(td, fd, vpp, FWRITE));
2089 * Like fget() but loads the underlying socket, or returns an error if the
2090 * descriptor does not represent a socket.
2092 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2095 * XXXRW: fgetsock() and fputsock() are deprecated, as consumers should rely
2096 * on their file descriptor reference to prevent the socket from being free'd
2100 fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
2108 if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
2110 if (fp->f_type != DTYPE_SOCKET) {
2115 *fflagp = fp->f_flag;
2120 FILEDESC_SUNLOCK(td->td_proc->p_fd);
2125 * Drop the reference count on the socket and XXX release the SX lock in the
2126 * future. The last reference closes the socket.
2128 * XXXRW: fputsock() is deprecated, see comment for fgetsock().
2131 fputsock(struct socket *so)
2140 * Handle the last reference to a file being closed.
2143 _fdrop(struct file *fp, struct thread *td)
2148 if (fp->f_count != 0)
2149 panic("fdrop: count %d", fp->f_count);
2150 if (fp->f_ops != &badfileops)
2151 error = fo_close(fp, td);
2152 atomic_subtract_int(&openfiles, 1);
2154 uma_zfree(file_zone, fp);
2160 * Apply an advisory lock on a file descriptor.
2162 * Just attempt to get a record lock of the requested type on the entire file
2163 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2165 #ifndef _SYS_SYSPROTO_H_
2173 flock(struct thread *td, struct flock_args *uap)
2181 if ((error = fget(td, uap->fd, &fp)) != 0)
2183 if (fp->f_type != DTYPE_VNODE) {
2185 return (EOPNOTSUPP);
2189 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2190 lf.l_whence = SEEK_SET;
2193 if (uap->how & LOCK_UN) {
2194 lf.l_type = F_UNLCK;
2195 atomic_clear_int(&fp->f_flag, FHASLOCK);
2196 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2199 if (uap->how & LOCK_EX)
2200 lf.l_type = F_WRLCK;
2201 else if (uap->how & LOCK_SH)
2202 lf.l_type = F_RDLCK;
2207 atomic_set_int(&fp->f_flag, FHASLOCK);
2208 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2209 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2212 VFS_UNLOCK_GIANT(vfslocked);
2216 * Duplicate the specified descriptor to a free descriptor.
2219 dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
2225 * If the to-be-dup'd fd number is greater than the allowed number
2226 * of file descriptors, or the fd to be dup'd has already been
2227 * closed, then reject.
2229 FILEDESC_XLOCK(fdp);
2230 if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2231 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2232 FILEDESC_XUNLOCK(fdp);
2237 * There are two cases of interest here.
2239 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2241 * For ENXIO steal away the file structure from (dfd) and store it in
2242 * (indx). (dfd) is effectively closed by this operation.
2244 * Any other error code is just returned.
2249 * Check that the mode the file is being opened for is a
2250 * subset of the mode of the existing descriptor.
2252 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2253 FILEDESC_XUNLOCK(fdp);
2256 fp = fdp->fd_ofiles[indx];
2257 fdp->fd_ofiles[indx] = wfp;
2258 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2262 FILEDESC_XUNLOCK(fdp);
2265 * We now own the reference to fp that the ofiles[]
2266 * array used to own. Release it.
2273 * Steal away the file pointer from dfd and stuff it into indx.
2275 fp = fdp->fd_ofiles[indx];
2276 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2277 fdp->fd_ofiles[dfd] = NULL;
2278 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2279 fdp->fd_ofileflags[dfd] = 0;
2283 FILEDESC_XUNLOCK(fdp);
2286 * We now own the reference to fp that the ofiles[] array
2287 * used to own. Release it.
2294 FILEDESC_XUNLOCK(fdp);
2301 * Scan all active processes to see if any of them have a current or root
2302 * directory of `olddp'. If so, replace them with the new mount point.
2305 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2307 struct filedesc *fdp;
2311 if (vrefcnt(olddp) == 1)
2313 sx_slock(&allproc_lock);
2314 FOREACH_PROC_IN_SYSTEM(p) {
2319 FILEDESC_XLOCK(fdp);
2320 if (fdp->fd_cdir == olddp) {
2322 fdp->fd_cdir = newdp;
2325 if (fdp->fd_rdir == olddp) {
2327 fdp->fd_rdir = newdp;
2330 FILEDESC_XUNLOCK(fdp);
2335 sx_sunlock(&allproc_lock);
2336 if (rootvnode == olddp) {
2343 struct filedesc_to_leader *
2344 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2346 struct filedesc_to_leader *fdtol;
2348 MALLOC(fdtol, struct filedesc_to_leader *,
2349 sizeof(struct filedesc_to_leader),
2350 M_FILEDESC_TO_LEADER,
2352 fdtol->fdl_refcount = 1;
2353 fdtol->fdl_holdcount = 0;
2354 fdtol->fdl_wakeup = 0;
2355 fdtol->fdl_leader = leader;
2357 FILEDESC_XLOCK(fdp);
2358 fdtol->fdl_next = old->fdl_next;
2359 fdtol->fdl_prev = old;
2360 old->fdl_next = fdtol;
2361 fdtol->fdl_next->fdl_prev = fdtol;
2362 FILEDESC_XUNLOCK(fdp);
2364 fdtol->fdl_next = fdtol;
2365 fdtol->fdl_prev = fdtol;
2371 * Get file structures globally.
2374 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2377 struct filedesc *fdp;
2382 error = sysctl_wire_old_buffer(req, 0);
2385 if (req->oldptr == NULL) {
2387 sx_slock(&allproc_lock);
2388 FOREACH_PROC_IN_SYSTEM(p) {
2389 if (p->p_state == PRS_NEW)
2394 /* overestimates sparse tables. */
2395 if (fdp->fd_lastfile > 0)
2396 n += fdp->fd_lastfile;
2399 sx_sunlock(&allproc_lock);
2400 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2403 bzero(&xf, sizeof(xf));
2404 xf.xf_size = sizeof(xf);
2405 sx_slock(&allproc_lock);
2406 FOREACH_PROC_IN_SYSTEM(p) {
2407 if (p->p_state == PRS_NEW)
2410 if (p_cansee(req->td, p) != 0) {
2414 xf.xf_pid = p->p_pid;
2415 xf.xf_uid = p->p_ucred->cr_uid;
2420 FILEDESC_SLOCK(fdp);
2421 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2422 if ((fp = fdp->fd_ofiles[n]) == NULL)
2426 xf.xf_data = fp->f_data;
2427 xf.xf_vnode = fp->f_vnode;
2428 xf.xf_type = fp->f_type;
2429 xf.xf_count = fp->f_count;
2431 xf.xf_offset = fp->f_offset;
2432 xf.xf_flag = fp->f_flag;
2433 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2437 FILEDESC_SUNLOCK(fdp);
2442 sx_sunlock(&allproc_lock);
2446 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2447 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2450 export_vnode_for_sysctl(struct vnode *vp, int type,
2451 struct kinfo_file *kif, struct filedesc *fdp, struct sysctl_req *req)
2454 char *fullpath, *freepath;
2457 bzero(kif, sizeof(*kif));
2458 kif->kf_structsize = sizeof(*kif);
2462 kif->kf_type = KF_TYPE_VNODE;
2463 /* This function only handles directories. */
2464 KASSERT(vp->v_type == VDIR, ("export_vnode_for_sysctl: vnode not directory"));
2465 kif->kf_vnode_type = KF_VTYPE_VDIR;
2468 * This is not a true file descriptor, so we set a bogus refcount
2469 * and offset to indicate these fields should be ignored.
2471 kif->kf_ref_count = -1;
2472 kif->kf_offset = -1;
2476 FILEDESC_SUNLOCK(fdp);
2477 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2478 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2479 vn_fullpath(curthread, vp, &fullpath, &freepath);
2481 VFS_UNLOCK_GIANT(vfslocked);
2482 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2483 if (freepath != NULL)
2484 free(freepath, M_TEMP);
2485 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2486 FILEDESC_SLOCK(fdp);
2491 * Get per-process file descriptors for use by procstat(1), et al.
2494 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
2496 char *fullpath, *freepath;
2497 struct kinfo_file *kif;
2498 struct filedesc *fdp;
2499 int error, i, *name;
2507 if ((p = pfind((pid_t)name[0])) == NULL)
2509 if ((error = p_candebug(curthread, p))) {
2515 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2516 FILEDESC_SLOCK(fdp);
2517 if (fdp->fd_cdir != NULL)
2518 export_vnode_for_sysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
2520 if (fdp->fd_rdir != NULL)
2521 export_vnode_for_sysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
2523 if (fdp->fd_jdir != NULL)
2524 export_vnode_for_sysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
2526 for (i = 0; i < fdp->fd_nfiles; i++) {
2527 if ((fp = fdp->fd_ofiles[i]) == NULL)
2529 bzero(kif, sizeof(*kif));
2530 kif->kf_structsize = sizeof(*kif);
2534 switch (fp->f_type) {
2536 kif->kf_type = KF_TYPE_VNODE;
2541 kif->kf_type = KF_TYPE_SOCKET;
2546 kif->kf_type = KF_TYPE_PIPE;
2550 kif->kf_type = KF_TYPE_FIFO;
2556 kif->kf_type = KF_TYPE_KQUEUE;
2560 kif->kf_type = KF_TYPE_CRYPTO;
2564 kif->kf_type = KF_TYPE_MQUEUE;
2568 kif->kf_type = KF_TYPE_SHM;
2572 kif->kf_type = KF_TYPE_UNKNOWN;
2575 kif->kf_ref_count = fp->f_count;
2576 if (fp->f_flag & FREAD)
2577 kif->kf_flags |= KF_FLAG_READ;
2578 if (fp->f_flag & FWRITE)
2579 kif->kf_flags |= KF_FLAG_WRITE;
2580 if (fp->f_flag & FAPPEND)
2581 kif->kf_flags |= KF_FLAG_APPEND;
2582 if (fp->f_flag & FASYNC)
2583 kif->kf_flags |= KF_FLAG_ASYNC;
2584 if (fp->f_flag & FFSYNC)
2585 kif->kf_flags |= KF_FLAG_FSYNC;
2586 if (fp->f_flag & FNONBLOCK)
2587 kif->kf_flags |= KF_FLAG_NONBLOCK;
2588 if (fp->f_flag & O_DIRECT)
2589 kif->kf_flags |= KF_FLAG_DIRECT;
2590 if (fp->f_flag & FHASLOCK)
2591 kif->kf_flags |= KF_FLAG_HASLOCK;
2592 kif->kf_offset = fp->f_offset;
2595 switch (vp->v_type) {
2597 kif->kf_vnode_type = KF_VTYPE_VNON;
2600 kif->kf_vnode_type = KF_VTYPE_VREG;
2603 kif->kf_vnode_type = KF_VTYPE_VDIR;
2606 kif->kf_vnode_type = KF_VTYPE_VBLK;
2609 kif->kf_vnode_type = KF_VTYPE_VCHR;
2612 kif->kf_vnode_type = KF_VTYPE_VLNK;
2615 kif->kf_vnode_type = KF_VTYPE_VSOCK;
2618 kif->kf_vnode_type = KF_VTYPE_VFIFO;
2621 kif->kf_vnode_type = KF_VTYPE_VBAD;
2624 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
2628 * It is OK to drop the filedesc lock here as we will
2629 * re-validate and re-evaluate its properties when
2630 * the loop continues.
2634 FILEDESC_SUNLOCK(fdp);
2635 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2636 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2637 vn_fullpath(curthread, vp, &fullpath, &freepath);
2639 VFS_UNLOCK_GIANT(vfslocked);
2640 strlcpy(kif->kf_path, fullpath,
2641 sizeof(kif->kf_path));
2642 if (freepath != NULL)
2643 free(freepath, M_TEMP);
2644 FILEDESC_SLOCK(fdp);
2647 struct sockaddr *sa;
2649 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
2650 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
2651 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
2654 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
2655 == 00 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
2656 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
2659 kif->kf_sock_domain =
2660 so->so_proto->pr_domain->dom_family;
2661 kif->kf_sock_type = so->so_type;
2662 kif->kf_sock_protocol = so->so_proto->pr_protocol;
2664 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2668 FILEDESC_SUNLOCK(fdp);
2674 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
2675 sysctl_kern_proc_filedesc, "Process filedesc entries");
2679 * For the purposes of debugging, generate a human-readable string for the
2683 file_type_to_name(short type)
2711 * For the purposes of debugging, identify a process (if any, perhaps one of
2712 * many) that references the passed file in its file descriptor array. Return
2715 static struct proc *
2716 file_to_first_proc(struct file *fp)
2718 struct filedesc *fdp;
2722 FOREACH_PROC_IN_SYSTEM(p) {
2723 if (p->p_state == PRS_NEW)
2728 for (n = 0; n < fdp->fd_nfiles; n++) {
2729 if (fp == fdp->fd_ofiles[n])
2737 db_print_file(struct file *fp, int header)
2742 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
2743 "File", "Type", "Data", "Flag", "GCFl", "Count",
2744 "MCount", "Vnode", "FPID", "FCmd");
2745 p = file_to_first_proc(fp);
2746 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
2747 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
2748 0, fp->f_count, 0, fp->f_vnode,
2749 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
2752 DB_SHOW_COMMAND(file, db_show_file)
2757 db_printf("usage: show file <addr>\n");
2760 fp = (struct file *)addr;
2761 db_print_file(fp, 1);
2764 DB_SHOW_COMMAND(files, db_show_files)
2766 struct filedesc *fdp;
2773 FOREACH_PROC_IN_SYSTEM(p) {
2774 if (p->p_state == PRS_NEW)
2776 if ((fdp = p->p_fd) == NULL)
2778 for (n = 0; n < fdp->fd_nfiles; ++n) {
2779 if ((fp = fdp->fd_ofiles[n]) == NULL)
2781 db_print_file(fp, header);
2788 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2789 &maxfilesperproc, 0, "Maximum files allowed open per process");
2791 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2792 &maxfiles, 0, "Maximum number of files");
2794 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2795 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
2799 filelistinit(void *dummy)
2802 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
2803 NULL, NULL, UMA_ALIGN_PTR, 0);
2804 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
2805 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
2807 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
2809 /*-------------------------------------------------------------------*/
2812 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
2819 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
2826 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
2833 badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
2840 badfo_kqfilter(struct file *fp, struct knote *kn)
2847 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
2854 badfo_close(struct file *fp, struct thread *td)
2860 struct fileops badfileops = {
2861 .fo_read = badfo_readwrite,
2862 .fo_write = badfo_readwrite,
2863 .fo_truncate = badfo_truncate,
2864 .fo_ioctl = badfo_ioctl,
2865 .fo_poll = badfo_poll,
2866 .fo_kqfilter = badfo_kqfilter,
2867 .fo_stat = badfo_stat,
2868 .fo_close = badfo_close,
2872 /*-------------------------------------------------------------------*/
2875 * File Descriptor pseudo-device driver (/dev/fd/).
2877 * Opening minor device N dup()s the file (if any) connected to file
2878 * descriptor N belonging to the calling process. Note that this driver
2879 * consists of only the ``open()'' routine, because all subsequent
2880 * references to this file will be direct to the other driver.
2882 * XXX: we could give this one a cloning event handler if necessary.
2887 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
2891 * XXX Kludge: set curthread->td_dupfd to contain the value of the
2892 * the file descriptor being sought for duplication. The error
2893 * return ensures that the vnode for this device will be released
2894 * by vn_open. Open will detect this special error and take the
2895 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
2896 * will simply report the error.
2898 td->td_dupfd = dev2unit(dev);
2902 static struct cdevsw fildesc_cdevsw = {
2903 .d_version = D_VERSION,
2904 .d_flags = D_NEEDGIANT,
2910 fildesc_drvinit(void *unused)
2914 dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
2915 make_dev_alias(dev, "stdin");
2916 dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
2917 make_dev_alias(dev, "stdout");
2918 dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
2919 make_dev_alias(dev, "stderr");
2922 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL)