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_capsicum.h"
41 #include "opt_compat.h"
43 #include "opt_ktrace.h"
44 #include "opt_procdesc.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/capability.h>
51 #include <sys/domain.h>
52 #include <sys/fcntl.h>
54 #include <sys/filedesc.h>
55 #include <sys/filio.h>
57 #include <sys/kernel.h>
58 #include <sys/limits.h>
60 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/mqueue.h>
64 #include <sys/mutex.h>
65 #include <sys/namei.h>
66 #include <sys/selinfo.h>
70 #include <sys/procdesc.h>
71 #include <sys/protosw.h>
72 #include <sys/racct.h>
73 #include <sys/resourcevar.h>
74 #include <sys/signalvar.h>
75 #include <sys/socketvar.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysproto.h>
82 #include <sys/unistd.h>
84 #include <sys/unpcb.h>
86 #include <sys/vnode.h>
88 #include <sys/ktrace.h>
93 #include <netinet/in.h>
94 #include <netinet/in_pcb.h>
96 #include <security/audit/audit.h>
103 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
104 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
105 "file desc to leader structures");
106 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
107 MALLOC_DEFINE(M_FILECAPS, "filecaps", "descriptor capabilities");
109 MALLOC_DECLARE(M_FADVISE);
111 static uma_zone_t file_zone;
114 static int closefp(struct filedesc *fdp, int fd, struct file *fp,
115 struct thread *td, int holdleaders);
116 static int fd_first_free(struct filedesc *fdp, int low, int size);
117 static int fd_last_used(struct filedesc *fdp, int size);
118 static void fdgrowtable(struct filedesc *fdp, int nfd);
119 static void fdunused(struct filedesc *fdp, int fd);
120 static void fdused(struct filedesc *fdp, int fd);
121 static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
122 static int fill_procdesc_info(struct procdesc *pdp,
123 struct kinfo_file *kif);
124 static int fill_pts_info(struct tty *tp, struct kinfo_file *kif);
125 static int fill_shm_info(struct file *fp, struct kinfo_file *kif);
126 static int fill_socket_info(struct socket *so, struct kinfo_file *kif);
127 static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
132 * - An array of open file descriptors (fd_ofiles)
133 * - An array of file flags (fd_ofileflags)
134 * - A bitmap recording which descriptors are in use (fd_map)
136 * A process starts out with NDFILE descriptors. The value of NDFILE has
137 * been selected based the historical limit of 20 open files, and an
138 * assumption that the majority of processes, especially short-lived
139 * processes like shells, will never need more.
141 * If this initial allocation is exhausted, a larger descriptor table and
142 * map are allocated dynamically, and the pointers in the process's struct
143 * filedesc are updated to point to those. This is repeated every time
144 * the process runs out of file descriptors (provided it hasn't hit its
147 * Since threads may hold references to individual descriptor table
148 * entries, the tables are never freed. Instead, they are placed on a
149 * linked list and freed only when the struct filedesc is released.
152 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
153 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
154 #define NDSLOT(x) ((x) / NDENTRIES)
155 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
156 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
159 * SLIST entry used to keep track of ofiles which must be reclaimed when
163 struct filedescent *ft_table;
164 SLIST_ENTRY(freetable) ft_next;
168 * Initial allocation: a filedesc structure + the head of SLIST used to
169 * keep track of old ofiles + enough space for NDFILE descriptors.
172 struct filedesc fd_fd;
173 SLIST_HEAD(, freetable) fd_free;
174 struct filedescent fd_dfiles[NDFILE];
175 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
179 * Descriptor management.
181 volatile int openfiles; /* actual number of open files */
182 struct mtx sigio_lock; /* mtx to protect pointers to sigio */
183 void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
185 /* A mutex to protect the association between a proc and filedesc. */
186 static struct mtx fdesc_mtx;
189 * If low >= size, just return low. Otherwise find the first zero bit in the
190 * given bitmap, starting at low and not exceeding size - 1. Return size if
194 fd_first_free(struct filedesc *fdp, int low, int size)
196 NDSLOTTYPE *map = fdp->fd_map;
204 if (low % NDENTRIES) {
205 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
206 if ((mask &= ~map[off]) != 0UL)
207 return (off * NDENTRIES + ffsl(mask) - 1);
210 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
211 if (map[off] != ~0UL)
212 return (off * NDENTRIES + ffsl(~map[off]) - 1);
217 * Find the highest non-zero bit in the given bitmap, starting at 0 and
218 * not exceeding size - 1. Return -1 if not found.
221 fd_last_used(struct filedesc *fdp, int size)
223 NDSLOTTYPE *map = fdp->fd_map;
228 if (size % NDENTRIES) {
229 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
230 if ((mask &= map[off]) != 0)
231 return (off * NDENTRIES + flsl(mask) - 1);
234 for (minoff = NDSLOT(0); off >= minoff; --off)
236 return (off * NDENTRIES + flsl(map[off]) - 1);
241 fdisused(struct filedesc *fdp, int fd)
244 FILEDESC_LOCK_ASSERT(fdp);
246 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
247 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
249 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
253 * Mark a file descriptor as used.
256 fdused(struct filedesc *fdp, int fd)
259 FILEDESC_XLOCK_ASSERT(fdp);
261 KASSERT(!fdisused(fdp, fd), ("fd=%d is already used", fd));
263 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
264 if (fd > fdp->fd_lastfile)
265 fdp->fd_lastfile = fd;
266 if (fd == fdp->fd_freefile)
267 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
271 * Mark a file descriptor as unused.
274 fdunused(struct filedesc *fdp, int fd)
277 FILEDESC_XLOCK_ASSERT(fdp);
279 KASSERT(fdisused(fdp, fd), ("fd=%d is already unused", fd));
280 KASSERT(fdp->fd_ofiles[fd].fde_file == NULL,
281 ("fd=%d is still in use", fd));
283 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
284 if (fd < fdp->fd_freefile)
285 fdp->fd_freefile = fd;
286 if (fd == fdp->fd_lastfile)
287 fdp->fd_lastfile = fd_last_used(fdp, fd);
291 * Free a file descriptor.
294 fdfree(struct filedesc *fdp, int fd)
296 struct filedescent *fde;
298 fde = &fdp->fd_ofiles[fd];
299 filecaps_free(&fde->fde_caps);
300 bzero(fde, sizeof(*fde));
305 * System calls on descriptors.
307 #ifndef _SYS_SYSPROTO_H_
308 struct getdtablesize_args {
314 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
316 struct proc *p = td->td_proc;
321 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
322 lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
324 if (lim < td->td_retval[0])
325 td->td_retval[0] = lim;
330 * Duplicate a file descriptor to a particular value.
332 * Note: keep in mind that a potential race condition exists when closing
333 * descriptors from a shared descriptor table (via rfork).
335 #ifndef _SYS_SYSPROTO_H_
343 sys_dup2(struct thread *td, struct dup2_args *uap)
346 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
351 * Duplicate a file descriptor.
353 #ifndef _SYS_SYSPROTO_H_
360 sys_dup(struct thread *td, struct dup_args *uap)
363 return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
367 * The file control system call.
369 #ifndef _SYS_SYSPROTO_H_
378 sys_fcntl(struct thread *td, struct fcntl_args *uap)
393 * Convert old flock structure to new.
395 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
396 fl.l_start = ofl.l_start;
397 fl.l_len = ofl.l_len;
398 fl.l_pid = ofl.l_pid;
399 fl.l_type = ofl.l_type;
400 fl.l_whence = ofl.l_whence;
420 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
429 error = kern_fcntl(td, uap->fd, cmd, arg);
432 if (uap->cmd == F_OGETLK) {
433 ofl.l_start = fl.l_start;
434 ofl.l_len = fl.l_len;
435 ofl.l_pid = fl.l_pid;
436 ofl.l_type = fl.l_type;
437 ofl.l_whence = fl.l_whence;
438 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
439 } else if (uap->cmd == F_GETLK) {
440 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
446 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
448 struct filedesc *fdp;
450 struct file *fp, *fp2;
451 struct filedescent *fde;
467 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
470 case F_DUPFD_CLOEXEC:
472 error = do_dup(td, DUP_FCNTL | DUP_CLOEXEC, fd, tmp,
478 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
481 case F_DUP2FD_CLOEXEC:
483 error = do_dup(td, DUP_FIXED | DUP_CLOEXEC, fd, tmp,
489 if ((fp = fget_locked(fdp, fd)) == NULL) {
490 FILEDESC_SUNLOCK(fdp);
494 fde = &fdp->fd_ofiles[fd];
496 (fde->fde_flags & UF_EXCLOSE) ? FD_CLOEXEC : 0;
497 FILEDESC_SUNLOCK(fdp);
502 if ((fp = fget_locked(fdp, fd)) == NULL) {
503 FILEDESC_XUNLOCK(fdp);
507 fde = &fdp->fd_ofiles[fd];
508 fde->fde_flags = (fde->fde_flags & ~UF_EXCLOSE) |
509 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
510 FILEDESC_XUNLOCK(fdp);
514 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_GETFL, &fp, NULL);
517 td->td_retval[0] = OFLAGS(fp->f_flag);
522 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_SETFL, &fp, NULL);
526 tmp = flg = fp->f_flag;
528 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
529 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
530 tmp = fp->f_flag & FNONBLOCK;
531 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
536 tmp = fp->f_flag & FASYNC;
537 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
542 atomic_clear_int(&fp->f_flag, FNONBLOCK);
544 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
549 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_GETOWN, &fp, NULL);
552 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
554 td->td_retval[0] = tmp;
559 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_SETOWN, &fp, NULL);
563 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
568 error = priv_check(td, PRIV_NFS_LOCKD);
576 /* FALLTHROUGH F_SETLK */
580 error = fget_unlocked(fdp, fd, CAP_FLOCK, 0, &fp, NULL);
583 if (fp->f_type != DTYPE_VNODE) {
589 flp = (struct flock *)arg;
590 if (flp->l_whence == SEEK_CUR) {
591 foffset = foffset_get(fp);
594 foffset > OFF_MAX - flp->l_start)) {
595 FILEDESC_SUNLOCK(fdp);
600 flp->l_start += foffset;
604 switch (flp->l_type) {
606 if ((fp->f_flag & FREAD) == 0) {
610 PROC_LOCK(p->p_leader);
611 p->p_leader->p_flag |= P_ADVLOCK;
612 PROC_UNLOCK(p->p_leader);
613 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
617 if ((fp->f_flag & FWRITE) == 0) {
621 PROC_LOCK(p->p_leader);
622 p->p_leader->p_flag |= P_ADVLOCK;
623 PROC_UNLOCK(p->p_leader);
624 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
628 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
633 * Temporary api for testing remote lock
636 if (flg != F_REMOTE) {
640 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
641 F_UNLCKSYS, flp, flg);
647 if (error != 0 || flp->l_type == F_UNLCK ||
648 flp->l_type == F_UNLCKSYS) {
654 * Check for a race with close.
656 * The vnode is now advisory locked (or unlocked, but this case
657 * is not really important) as the caller requested.
658 * We had to drop the filedesc lock, so we need to recheck if
659 * the descriptor is still valid, because if it was closed
660 * in the meantime we need to remove advisory lock from the
661 * vnode - close on any descriptor leading to an advisory
662 * locked vnode, removes that lock.
663 * We will return 0 on purpose in that case, as the result of
664 * successful advisory lock might have been externally visible
665 * already. This is fine - effectively we pretend to the caller
666 * that the closing thread was a bit slower and that the
667 * advisory lock succeeded before the close.
669 error = fget_unlocked(fdp, fd, 0, 0, &fp2, NULL);
675 flp->l_whence = SEEK_SET;
678 flp->l_type = F_UNLCK;
679 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
680 F_UNLCK, flp, F_POSIX);
687 error = fget_unlocked(fdp, fd, CAP_FLOCK, 0, &fp, NULL);
690 if (fp->f_type != DTYPE_VNODE) {
695 flp = (struct flock *)arg;
696 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
697 flp->l_type != F_UNLCK) {
702 if (flp->l_whence == SEEK_CUR) {
703 foffset = foffset_get(fp);
704 if ((flp->l_start > 0 &&
705 foffset > OFF_MAX - flp->l_start) ||
707 foffset < OFF_MIN - flp->l_start)) {
708 FILEDESC_SUNLOCK(fdp);
713 flp->l_start += foffset;
716 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
722 arg = arg ? 128 * 1024: 0;
725 error = fget_unlocked(fdp, fd, 0, 0, &fp, NULL);
728 if (fp->f_type != DTYPE_VNODE) {
735 error = vn_lock(vp, LK_SHARED);
740 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
742 fp->f_seqcount = (arg + bsize - 1) / bsize;
744 new = old = fp->f_flag;
746 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
749 new = old = fp->f_flag;
751 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
764 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
767 do_dup(struct thread *td, int flags, int old, int new,
770 struct filedesc *fdp;
771 struct filedescent *oldfde, *newfde;
781 * Verify we have a valid descriptor to dup from and possibly to
782 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
783 * return EINVAL when the new descriptor is out of bounds.
788 return (flags & DUP_FCNTL ? EINVAL : EBADF);
790 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
793 return (flags & DUP_FCNTL ? EINVAL : EBADF);
796 if (fget_locked(fdp, old) == NULL) {
797 FILEDESC_XUNLOCK(fdp);
800 oldfde = &fdp->fd_ofiles[old];
801 if (flags & DUP_FIXED && old == new) {
803 if (flags & DUP_CLOEXEC)
804 fdp->fd_ofiles[new].fde_flags |= UF_EXCLOSE;
805 FILEDESC_XUNLOCK(fdp);
808 fp = oldfde->fde_file;
812 * If the caller specified a file descriptor, make sure the file
813 * table is large enough to hold it, and grab it. Otherwise, just
814 * allocate a new descriptor the usual way.
816 if (flags & DUP_FIXED) {
817 if (new >= fdp->fd_nfiles) {
819 * The resource limits are here instead of e.g.
820 * fdalloc(), because the file descriptor table may be
821 * shared between processes, so we can't really use
822 * racct_add()/racct_sub(). Instead of counting the
823 * number of actually allocated descriptors, just put
824 * the limit on the size of the file descriptor table.
828 error = racct_set(p, RACCT_NOFILE, new + 1);
831 FILEDESC_XUNLOCK(fdp);
836 fdgrowtable(fdp, new + 1);
837 oldfde = &fdp->fd_ofiles[old];
839 newfde = &fdp->fd_ofiles[new];
840 if (newfde->fde_file == NULL)
843 if ((error = fdalloc(td, new, &new)) != 0) {
844 FILEDESC_XUNLOCK(fdp);
848 newfde = &fdp->fd_ofiles[new];
851 KASSERT(fp == oldfde->fde_file, ("old fd has been modified"));
852 KASSERT(old != new, ("new fd is same as old"));
854 delfp = newfde->fde_file;
857 * Duplicate the source descriptor.
860 filecaps_copy(&oldfde->fde_caps, &newfde->fde_caps);
861 if ((flags & DUP_CLOEXEC) != 0)
862 newfde->fde_flags = oldfde->fde_flags | UF_EXCLOSE;
864 newfde->fde_flags = oldfde->fde_flags & ~UF_EXCLOSE;
865 if (new > fdp->fd_lastfile)
866 fdp->fd_lastfile = new;
870 (void) closefp(fdp, new, delfp, td, 1);
871 /* closefp() drops the FILEDESC lock for us. */
873 FILEDESC_XUNLOCK(fdp);
880 * If sigio is on the list associated with a process or process group,
881 * disable signalling from the device, remove sigio from the list and
885 funsetown(struct sigio **sigiop)
895 *(sigio->sio_myref) = NULL;
896 if ((sigio)->sio_pgid < 0) {
897 struct pgrp *pg = (sigio)->sio_pgrp;
899 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
903 struct proc *p = (sigio)->sio_proc;
905 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
910 crfree(sigio->sio_ucred);
911 free(sigio, M_SIGIO);
915 * Free a list of sigio structures.
916 * We only need to lock the SIGIO_LOCK because we have made ourselves
917 * inaccessible to callers of fsetown and therefore do not need to lock
918 * the proc or pgrp struct for the list manipulation.
921 funsetownlst(struct sigiolst *sigiolst)
927 sigio = SLIST_FIRST(sigiolst);
934 * Every entry of the list should belong
935 * to a single proc or pgrp.
937 if (sigio->sio_pgid < 0) {
938 pg = sigio->sio_pgrp;
939 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
940 } else /* if (sigio->sio_pgid > 0) */ {
942 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
946 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
947 *(sigio->sio_myref) = NULL;
949 KASSERT(sigio->sio_pgid < 0,
950 ("Proc sigio in pgrp sigio list"));
951 KASSERT(sigio->sio_pgrp == pg,
952 ("Bogus pgrp in sigio list"));
954 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
957 } else /* if (p != NULL) */ {
958 KASSERT(sigio->sio_pgid > 0,
959 ("Pgrp sigio in proc sigio list"));
960 KASSERT(sigio->sio_proc == p,
961 ("Bogus proc in sigio list"));
963 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
968 crfree(sigio->sio_ucred);
969 free(sigio, M_SIGIO);
976 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
978 * After permission checking, add a sigio structure to the sigio list for
979 * the process or process group.
982 fsetown(pid_t pgid, struct sigio **sigiop)
996 /* Allocate and fill in the new sigio out of locks. */
997 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
998 sigio->sio_pgid = pgid;
999 sigio->sio_ucred = crhold(curthread->td_ucred);
1000 sigio->sio_myref = sigiop;
1002 sx_slock(&proctree_lock);
1011 * Policy - Don't allow a process to FSETOWN a process
1012 * in another session.
1014 * Remove this test to allow maximum flexibility or
1015 * restrict FSETOWN to the current process or process
1016 * group for maximum safety.
1019 if (proc->p_session != curthread->td_proc->p_session) {
1025 } else /* if (pgid < 0) */ {
1026 pgrp = pgfind(-pgid);
1034 * Policy - Don't allow a process to FSETOWN a process
1035 * in another session.
1037 * Remove this test to allow maximum flexibility or
1038 * restrict FSETOWN to the current process or process
1039 * group for maximum safety.
1041 if (pgrp->pg_session != curthread->td_proc->p_session) {
1052 * Since funsetownlst() is called without the proctree
1053 * locked, we need to check for P_WEXIT.
1054 * XXX: is ESRCH correct?
1056 if ((proc->p_flag & P_WEXIT) != 0) {
1061 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1062 sigio->sio_proc = proc;
1066 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1067 sigio->sio_pgrp = pgrp;
1070 sx_sunlock(&proctree_lock);
1077 sx_sunlock(&proctree_lock);
1078 crfree(sigio->sio_ucred);
1079 free(sigio, M_SIGIO);
1084 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1088 struct sigio **sigiop;
1093 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1099 * Function drops the filedesc lock on return.
1102 closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td,
1107 FILEDESC_XLOCK_ASSERT(fdp);
1110 if (td->td_proc->p_fdtol != NULL) {
1112 * Ask fdfree() to sleep to ensure that all relevant
1113 * process leaders can be traversed in closef().
1115 fdp->fd_holdleaderscount++;
1122 * We now hold the fp reference that used to be owned by the
1123 * descriptor array. We have to unlock the FILEDESC *AFTER*
1124 * knote_fdclose to prevent a race of the fd getting opened, a knote
1125 * added, and deleteing a knote for the new fd.
1127 knote_fdclose(td, fd);
1130 * We need to notify mqueue if the object is of type mqueue.
1132 if (fp->f_type == DTYPE_MQUEUE)
1133 mq_fdclose(td, fd, fp);
1134 FILEDESC_XUNLOCK(fdp);
1136 error = closef(fp, td);
1138 FILEDESC_XLOCK(fdp);
1139 fdp->fd_holdleaderscount--;
1140 if (fdp->fd_holdleaderscount == 0 &&
1141 fdp->fd_holdleaderswakeup != 0) {
1142 fdp->fd_holdleaderswakeup = 0;
1143 wakeup(&fdp->fd_holdleaderscount);
1145 FILEDESC_XUNLOCK(fdp);
1151 * Close a file descriptor.
1153 #ifndef _SYS_SYSPROTO_H_
1162 struct close_args *uap;
1165 return (kern_close(td, uap->fd));
1173 struct filedesc *fdp;
1176 fdp = td->td_proc->p_fd;
1178 AUDIT_SYSCLOSE(td, fd);
1180 FILEDESC_XLOCK(fdp);
1181 if ((fp = fget_locked(fdp, fd)) == NULL) {
1182 FILEDESC_XUNLOCK(fdp);
1187 /* closefp() drops the FILEDESC lock for us. */
1188 return (closefp(fdp, fd, fp, td, 1));
1192 * Close open file descriptors.
1194 #ifndef _SYS_SYSPROTO_H_
1195 struct closefrom_args {
1201 sys_closefrom(struct thread *td, struct closefrom_args *uap)
1203 struct filedesc *fdp;
1206 fdp = td->td_proc->p_fd;
1207 AUDIT_ARG_FD(uap->lowfd);
1210 * Treat negative starting file descriptor values identical to
1211 * closefrom(0) which closes all files.
1215 FILEDESC_SLOCK(fdp);
1216 for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1217 if (fdp->fd_ofiles[fd].fde_file != NULL) {
1218 FILEDESC_SUNLOCK(fdp);
1219 (void)kern_close(td, fd);
1220 FILEDESC_SLOCK(fdp);
1223 FILEDESC_SUNLOCK(fdp);
1227 #if defined(COMPAT_43)
1229 * Return status information about a file descriptor.
1231 #ifndef _SYS_SYSPROTO_H_
1232 struct ofstat_args {
1239 ofstat(struct thread *td, struct ofstat_args *uap)
1245 error = kern_fstat(td, uap->fd, &ub);
1248 error = copyout(&oub, uap->sb, sizeof(oub));
1252 #endif /* COMPAT_43 */
1255 * Return status information about a file descriptor.
1257 #ifndef _SYS_SYSPROTO_H_
1265 sys_fstat(struct thread *td, struct fstat_args *uap)
1270 error = kern_fstat(td, uap->fd, &ub);
1272 error = copyout(&ub, uap->sb, sizeof(ub));
1277 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1284 if ((error = fget(td, fd, CAP_FSTAT, &fp)) != 0)
1287 AUDIT_ARG_FILE(td->td_proc, fp);
1289 error = fo_stat(fp, sbp, td->td_ucred, td);
1292 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1299 * Return status information about a file descriptor.
1301 #ifndef _SYS_SYSPROTO_H_
1302 struct nfstat_args {
1309 sys_nfstat(struct thread *td, struct nfstat_args *uap)
1315 error = kern_fstat(td, uap->fd, &ub);
1317 cvtnstat(&ub, &nub);
1318 error = copyout(&nub, uap->sb, sizeof(nub));
1324 * Return pathconf information about a file descriptor.
1326 #ifndef _SYS_SYSPROTO_H_
1327 struct fpathconf_args {
1334 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
1340 if ((error = fget(td, uap->fd, CAP_FPATHCONF, &fp)) != 0)
1343 /* If asynchronous I/O is available, it works for all descriptors. */
1344 if (uap->name == _PC_ASYNC_IO) {
1345 td->td_retval[0] = async_io_version;
1350 vn_lock(vp, LK_SHARED | LK_RETRY);
1351 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1353 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1354 if (uap->name != _PC_PIPE_BUF) {
1357 td->td_retval[0] = PIPE_BUF;
1369 * Initialize filecaps structure.
1372 filecaps_init(struct filecaps *fcaps)
1375 bzero(fcaps, sizeof(*fcaps));
1376 fcaps->fc_nioctls = -1;
1380 * Copy filecaps structure allocating memory for ioctls array if needed.
1383 filecaps_copy(const struct filecaps *src, struct filecaps *dst)
1388 if (src->fc_ioctls != NULL) {
1389 KASSERT(src->fc_nioctls > 0,
1390 ("fc_ioctls != NULL, but fc_nioctls=%hd", src->fc_nioctls));
1392 size = sizeof(src->fc_ioctls[0]) * src->fc_nioctls;
1393 dst->fc_ioctls = malloc(size, M_FILECAPS, M_WAITOK);
1394 bcopy(src->fc_ioctls, dst->fc_ioctls, size);
1399 * Move filecaps structure to the new place and clear the old place.
1402 filecaps_move(struct filecaps *src, struct filecaps *dst)
1406 bzero(src, sizeof(*src));
1410 * Fill the given filecaps structure with full rights.
1413 filecaps_fill(struct filecaps *fcaps)
1416 fcaps->fc_rights = CAP_ALL;
1417 fcaps->fc_ioctls = NULL;
1418 fcaps->fc_nioctls = -1;
1419 fcaps->fc_fcntls = CAP_FCNTL_ALL;
1423 * Free memory allocated within filecaps structure.
1426 filecaps_free(struct filecaps *fcaps)
1429 free(fcaps->fc_ioctls, M_FILECAPS);
1430 bzero(fcaps, sizeof(*fcaps));
1434 * Validate the given filecaps structure.
1437 filecaps_validate(const struct filecaps *fcaps, const char *func)
1440 KASSERT((fcaps->fc_rights & ~CAP_MASK_VALID) == 0,
1441 ("%s: invalid rights", func));
1442 KASSERT((fcaps->fc_fcntls & ~CAP_FCNTL_ALL) == 0,
1443 ("%s: invalid fcntls", func));
1444 KASSERT(fcaps->fc_fcntls == 0 || (fcaps->fc_rights & CAP_FCNTL) != 0,
1445 ("%s: fcntls without CAP_FCNTL", func));
1446 KASSERT(fcaps->fc_ioctls != NULL ? fcaps->fc_nioctls > 0 :
1447 (fcaps->fc_nioctls == -1 || fcaps->fc_nioctls == 0),
1448 ("%s: invalid ioctls", func));
1449 KASSERT(fcaps->fc_nioctls == 0 || (fcaps->fc_rights & CAP_IOCTL) != 0,
1450 ("%s: ioctls without CAP_IOCTL", func));
1454 * Grow the file table to accomodate (at least) nfd descriptors.
1457 fdgrowtable(struct filedesc *fdp, int nfd)
1459 struct filedesc0 *fdp0;
1460 struct freetable *ft;
1461 struct filedescent *ntable;
1462 struct filedescent *otable;
1463 int nnfiles, onfiles;
1464 NDSLOTTYPE *nmap, *omap;
1466 FILEDESC_XLOCK_ASSERT(fdp);
1468 KASSERT(fdp->fd_nfiles > 0, ("zero-length file table"));
1470 /* save old values */
1471 onfiles = fdp->fd_nfiles;
1472 otable = fdp->fd_ofiles;
1475 /* compute the size of the new table */
1476 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1477 if (nnfiles <= onfiles)
1478 /* the table is already large enough */
1482 * Allocate a new table and map. We need enough space for the
1483 * file entries themselves and the struct freetable we will use
1484 * when we decommission the table and place it on the freelist.
1485 * We place the struct freetable in the middle so we don't have
1486 * to worry about padding.
1488 ntable = malloc(nnfiles * sizeof(ntable[0]) + sizeof(struct freetable),
1489 M_FILEDESC, M_ZERO | M_WAITOK);
1490 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, M_FILEDESC,
1493 /* copy the old data over and point at the new tables */
1494 memcpy(ntable, otable, onfiles * sizeof(*otable));
1495 memcpy(nmap, omap, NDSLOTS(onfiles) * sizeof(*omap));
1497 /* update the pointers and counters */
1498 fdp->fd_nfiles = nnfiles;
1499 memcpy(ntable, otable, onfiles * sizeof(ntable[0]));
1500 fdp->fd_ofiles = ntable;
1504 * Do not free the old file table, as some threads may still
1505 * reference entries within it. Instead, place it on a freelist
1506 * which will be processed when the struct filedesc is released.
1508 * Do, however, free the old map.
1510 * Note that if onfiles == NDFILE, we're dealing with the original
1511 * static allocation contained within (struct filedesc0 *)fdp,
1512 * which must not be freed.
1514 if (onfiles > NDFILE) {
1515 ft = (struct freetable *)&otable[onfiles];
1516 fdp0 = (struct filedesc0 *)fdp;
1517 ft->ft_table = otable;
1518 SLIST_INSERT_HEAD(&fdp0->fd_free, ft, ft_next);
1519 free(omap, M_FILEDESC);
1524 * Allocate a file descriptor for the process.
1527 fdalloc(struct thread *td, int minfd, int *result)
1529 struct proc *p = td->td_proc;
1530 struct filedesc *fdp = p->p_fd;
1531 int fd = -1, maxfd, allocfd;
1536 FILEDESC_XLOCK_ASSERT(fdp);
1538 if (fdp->fd_freefile > minfd)
1539 minfd = fdp->fd_freefile;
1542 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1546 * Search the bitmap for a free descriptor starting at minfd.
1547 * If none is found, grow the file table.
1549 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1552 if (fd >= fdp->fd_nfiles) {
1553 allocfd = min(fd * 2, maxfd);
1556 error = racct_set(p, RACCT_NOFILE, allocfd);
1562 * fd is already equal to first free descriptor >= minfd, so
1563 * we only need to grow the table and we are done.
1565 fdgrowtable(fdp, allocfd);
1569 * Perform some sanity checks, then mark the file descriptor as
1570 * used and return it to the caller.
1572 KASSERT(fd >= 0 && fd < min(maxfd, fdp->fd_nfiles),
1573 ("invalid descriptor %d", fd));
1574 KASSERT(!fdisused(fdp, fd),
1575 ("fd_first_free() returned non-free descriptor"));
1576 KASSERT(fdp->fd_ofiles[fd].fde_file == NULL,
1577 ("file descriptor isn't free"));
1578 KASSERT(fdp->fd_ofiles[fd].fde_flags == 0, ("file flags are set"));
1585 * Check to see whether n user file descriptors are available to the process
1589 fdavail(struct thread *td, int n)
1591 struct proc *p = td->td_proc;
1592 struct filedesc *fdp = td->td_proc->p_fd;
1595 FILEDESC_LOCK_ASSERT(fdp);
1598 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1599 * call racct_add() from there instead of dealing with containers
1603 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1605 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1607 last = min(fdp->fd_nfiles, lim);
1608 for (i = fdp->fd_freefile; i < last; i++) {
1609 if (fdp->fd_ofiles[i].fde_file == NULL && --n <= 0)
1616 * Create a new open file structure and allocate a file decriptor for the
1617 * process that refers to it. We add one reference to the file for the
1618 * descriptor table and one reference for resultfp. This is to prevent us
1619 * being preempted and the entry in the descriptor table closed after we
1620 * release the FILEDESC lock.
1623 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1628 error = falloc_noinstall(td, &fp);
1630 return (error); /* no reference held on error */
1632 error = finstall(td, fp, &fd, flags, NULL);
1634 fdrop(fp, td); /* one reference (fp only) */
1638 if (resultfp != NULL)
1639 *resultfp = fp; /* copy out result */
1641 fdrop(fp, td); /* release local reference */
1643 if (resultfd != NULL)
1650 * Create a new open file structure without allocating a file descriptor.
1653 falloc_noinstall(struct thread *td, struct file **resultfp)
1656 int maxuserfiles = maxfiles - (maxfiles / 20);
1657 static struct timeval lastfail;
1660 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1662 if ((openfiles >= maxuserfiles &&
1663 priv_check(td, PRIV_MAXFILES) != 0) ||
1664 openfiles >= maxfiles) {
1665 if (ppsratecheck(&lastfail, &curfail, 1)) {
1666 printf("kern.maxfiles limit exceeded by uid %i, "
1667 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1671 atomic_add_int(&openfiles, 1);
1672 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1673 refcount_init(&fp->f_count, 1);
1674 fp->f_cred = crhold(td->td_ucred);
1675 fp->f_ops = &badfileops;
1683 * Install a file in a file descriptor table.
1686 finstall(struct thread *td, struct file *fp, int *fd, int flags,
1687 struct filecaps *fcaps)
1689 struct filedesc *fdp = td->td_proc->p_fd;
1690 struct filedescent *fde;
1693 KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1694 KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1696 filecaps_validate(fcaps, __func__);
1698 FILEDESC_XLOCK(fdp);
1699 if ((error = fdalloc(td, 0, fd))) {
1700 FILEDESC_XUNLOCK(fdp);
1704 fde = &fdp->fd_ofiles[*fd];
1706 if ((flags & O_CLOEXEC) != 0)
1707 fde->fde_flags |= UF_EXCLOSE;
1709 filecaps_move(fcaps, &fde->fde_caps);
1711 filecaps_fill(&fde->fde_caps);
1712 FILEDESC_XUNLOCK(fdp);
1717 * Build a new filedesc structure from another.
1718 * Copy the current, root, and jail root vnode references.
1721 fdinit(struct filedesc *fdp)
1723 struct filedesc0 *newfdp;
1725 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1726 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1728 FILEDESC_XLOCK(fdp);
1729 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1730 if (newfdp->fd_fd.fd_cdir)
1731 VREF(newfdp->fd_fd.fd_cdir);
1732 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1733 if (newfdp->fd_fd.fd_rdir)
1734 VREF(newfdp->fd_fd.fd_rdir);
1735 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1736 if (newfdp->fd_fd.fd_jdir)
1737 VREF(newfdp->fd_fd.fd_jdir);
1738 FILEDESC_XUNLOCK(fdp);
1741 /* Create the file descriptor table. */
1742 newfdp->fd_fd.fd_refcnt = 1;
1743 newfdp->fd_fd.fd_holdcnt = 1;
1744 newfdp->fd_fd.fd_cmask = CMASK;
1745 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1746 newfdp->fd_fd.fd_nfiles = NDFILE;
1747 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1748 newfdp->fd_fd.fd_lastfile = -1;
1749 return (&newfdp->fd_fd);
1752 static struct filedesc *
1753 fdhold(struct proc *p)
1755 struct filedesc *fdp;
1757 mtx_lock(&fdesc_mtx);
1761 mtx_unlock(&fdesc_mtx);
1766 fddrop(struct filedesc *fdp)
1768 struct filedesc0 *fdp0;
1769 struct freetable *ft;
1772 mtx_lock(&fdesc_mtx);
1773 i = --fdp->fd_holdcnt;
1774 mtx_unlock(&fdesc_mtx);
1778 FILEDESC_LOCK_DESTROY(fdp);
1779 fdp0 = (struct filedesc0 *)fdp;
1780 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1781 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1782 free(ft->ft_table, M_FILEDESC);
1784 free(fdp, M_FILEDESC);
1788 * Share a filedesc structure.
1791 fdshare(struct filedesc *fdp)
1794 FILEDESC_XLOCK(fdp);
1796 FILEDESC_XUNLOCK(fdp);
1801 * Unshare a filedesc structure, if necessary by making a copy
1804 fdunshare(struct proc *p, struct thread *td)
1807 FILEDESC_XLOCK(p->p_fd);
1808 if (p->p_fd->fd_refcnt > 1) {
1809 struct filedesc *tmp;
1811 FILEDESC_XUNLOCK(p->p_fd);
1812 tmp = fdcopy(p->p_fd);
1816 FILEDESC_XUNLOCK(p->p_fd);
1820 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1821 * this is to ease callers, not catch errors.
1824 fdcopy(struct filedesc *fdp)
1826 struct filedesc *newfdp;
1827 struct filedescent *nfde, *ofde;
1830 /* Certain daemons might not have file descriptors. */
1834 newfdp = fdinit(fdp);
1835 FILEDESC_SLOCK(fdp);
1836 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1837 FILEDESC_SUNLOCK(fdp);
1838 FILEDESC_XLOCK(newfdp);
1839 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1840 FILEDESC_XUNLOCK(newfdp);
1841 FILEDESC_SLOCK(fdp);
1843 /* copy all passable descriptors (i.e. not kqueue) */
1844 newfdp->fd_freefile = -1;
1845 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1846 ofde = &fdp->fd_ofiles[i];
1847 if (fdisused(fdp, i) &&
1848 (ofde->fde_file->f_ops->fo_flags & DFLAG_PASSABLE) &&
1849 ofde->fde_file->f_ops != &badfileops) {
1850 nfde = &newfdp->fd_ofiles[i];
1852 filecaps_copy(&ofde->fde_caps, &nfde->fde_caps);
1853 fhold(nfde->fde_file);
1854 newfdp->fd_lastfile = i;
1856 if (newfdp->fd_freefile == -1)
1857 newfdp->fd_freefile = i;
1860 newfdp->fd_cmask = fdp->fd_cmask;
1861 FILEDESC_SUNLOCK(fdp);
1862 FILEDESC_XLOCK(newfdp);
1863 for (i = 0; i <= newfdp->fd_lastfile; ++i) {
1864 if (newfdp->fd_ofiles[i].fde_file != NULL)
1867 if (newfdp->fd_freefile == -1)
1868 newfdp->fd_freefile = i;
1869 FILEDESC_XUNLOCK(newfdp);
1874 * Release a filedesc structure.
1877 fdescfree(struct thread *td)
1879 struct filedesc *fdp;
1881 struct filedesc_to_leader *fdtol;
1883 struct vnode *cdir, *jdir, *rdir, *vp;
1886 /* Certain daemons might not have file descriptors. */
1887 fdp = td->td_proc->p_fd;
1892 PROC_LOCK(td->td_proc);
1893 racct_set(td->td_proc, RACCT_NOFILE, 0);
1894 PROC_UNLOCK(td->td_proc);
1897 /* Check for special need to clear POSIX style locks */
1898 fdtol = td->td_proc->p_fdtol;
1899 if (fdtol != NULL) {
1900 FILEDESC_XLOCK(fdp);
1901 KASSERT(fdtol->fdl_refcount > 0,
1902 ("filedesc_to_refcount botch: fdl_refcount=%d",
1903 fdtol->fdl_refcount));
1904 if (fdtol->fdl_refcount == 1 &&
1905 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1906 for (i = 0; i <= fdp->fd_lastfile; i++) {
1907 fp = fdp->fd_ofiles[i].fde_file;
1908 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1911 FILEDESC_XUNLOCK(fdp);
1912 lf.l_whence = SEEK_SET;
1915 lf.l_type = F_UNLCK;
1917 (void) VOP_ADVLOCK(vp,
1918 (caddr_t)td->td_proc->p_leader, F_UNLCK,
1920 FILEDESC_XLOCK(fdp);
1925 if (fdtol->fdl_refcount == 1) {
1926 if (fdp->fd_holdleaderscount > 0 &&
1927 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1929 * close() or do_dup() has cleared a reference
1930 * in a shared file descriptor table.
1932 fdp->fd_holdleaderswakeup = 1;
1933 sx_sleep(&fdp->fd_holdleaderscount,
1934 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1937 if (fdtol->fdl_holdcount > 0) {
1939 * Ensure that fdtol->fdl_leader remains
1940 * valid in closef().
1942 fdtol->fdl_wakeup = 1;
1943 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1948 fdtol->fdl_refcount--;
1949 if (fdtol->fdl_refcount == 0 &&
1950 fdtol->fdl_holdcount == 0) {
1951 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1952 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1955 td->td_proc->p_fdtol = NULL;
1956 FILEDESC_XUNLOCK(fdp);
1958 free(fdtol, M_FILEDESC_TO_LEADER);
1960 FILEDESC_XLOCK(fdp);
1961 i = --fdp->fd_refcnt;
1962 FILEDESC_XUNLOCK(fdp);
1966 for (i = 0; i <= fdp->fd_lastfile; i++) {
1967 fp = fdp->fd_ofiles[i].fde_file;
1969 FILEDESC_XLOCK(fdp);
1971 FILEDESC_XUNLOCK(fdp);
1972 (void) closef(fp, td);
1975 FILEDESC_XLOCK(fdp);
1977 /* XXX This should happen earlier. */
1978 mtx_lock(&fdesc_mtx);
1979 td->td_proc->p_fd = NULL;
1980 mtx_unlock(&fdesc_mtx);
1982 if (fdp->fd_nfiles > NDFILE)
1983 free(fdp->fd_ofiles, M_FILEDESC);
1984 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1985 free(fdp->fd_map, M_FILEDESC);
1989 cdir = fdp->fd_cdir;
1990 fdp->fd_cdir = NULL;
1991 rdir = fdp->fd_rdir;
1992 fdp->fd_rdir = NULL;
1993 jdir = fdp->fd_jdir;
1994 fdp->fd_jdir = NULL;
1995 FILEDESC_XUNLOCK(fdp);
2008 * For setugid programs, we don't want to people to use that setugidness
2009 * to generate error messages which write to a file which otherwise would
2010 * otherwise be off-limits to the process. We check for filesystems where
2011 * the vnode can change out from under us after execve (like [lin]procfs).
2013 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
2014 * sufficient. We also don't check for setugidness since we know we are.
2017 is_unsafe(struct file *fp)
2019 if (fp->f_type == DTYPE_VNODE) {
2020 struct vnode *vp = fp->f_vnode;
2022 if ((vp->v_vflag & VV_PROCDEP) != 0)
2029 * Make this setguid thing safe, if at all possible.
2032 setugidsafety(struct thread *td)
2034 struct filedesc *fdp;
2038 /* Certain daemons might not have file descriptors. */
2039 fdp = td->td_proc->p_fd;
2044 * Note: fdp->fd_ofiles may be reallocated out from under us while
2045 * we are blocked in a close. Be careful!
2047 FILEDESC_XLOCK(fdp);
2048 for (i = 0; i <= fdp->fd_lastfile; i++) {
2051 fp = fdp->fd_ofiles[i].fde_file;
2052 if (fp != NULL && is_unsafe(fp)) {
2053 knote_fdclose(td, i);
2055 * NULL-out descriptor prior to close to avoid
2056 * a race while close blocks.
2059 FILEDESC_XUNLOCK(fdp);
2060 (void) closef(fp, td);
2061 FILEDESC_XLOCK(fdp);
2064 FILEDESC_XUNLOCK(fdp);
2068 * If a specific file object occupies a specific file descriptor, close the
2069 * file descriptor entry and drop a reference on the file object. This is a
2070 * convenience function to handle a subsequent error in a function that calls
2071 * falloc() that handles the race that another thread might have closed the
2072 * file descriptor out from under the thread creating the file object.
2075 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2078 FILEDESC_XLOCK(fdp);
2079 if (fdp->fd_ofiles[idx].fde_file == fp) {
2081 FILEDESC_XUNLOCK(fdp);
2084 FILEDESC_XUNLOCK(fdp);
2088 * Close any files on exec?
2091 fdcloseexec(struct thread *td)
2093 struct filedesc *fdp;
2094 struct filedescent *fde;
2098 /* Certain daemons might not have file descriptors. */
2099 fdp = td->td_proc->p_fd;
2104 * We cannot cache fd_ofiles since operations
2105 * may block and rip them out from under us.
2107 FILEDESC_XLOCK(fdp);
2108 for (i = 0; i <= fdp->fd_lastfile; i++) {
2109 fde = &fdp->fd_ofiles[i];
2111 if (fp != NULL && (fp->f_type == DTYPE_MQUEUE ||
2112 (fde->fde_flags & UF_EXCLOSE))) {
2114 (void) closefp(fdp, i, fp, td, 0);
2115 /* closefp() drops the FILEDESC lock. */
2116 FILEDESC_XLOCK(fdp);
2119 FILEDESC_XUNLOCK(fdp);
2123 * It is unsafe for set[ug]id processes to be started with file
2124 * descriptors 0..2 closed, as these descriptors are given implicit
2125 * significance in the Standard C library. fdcheckstd() will create a
2126 * descriptor referencing /dev/null for each of stdin, stdout, and
2127 * stderr that is not already open.
2130 fdcheckstd(struct thread *td)
2132 struct filedesc *fdp;
2133 register_t retval, save;
2134 int i, error, devnull;
2136 fdp = td->td_proc->p_fd;
2139 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2142 for (i = 0; i < 3; i++) {
2143 if (fdp->fd_ofiles[i].fde_file != NULL)
2146 save = td->td_retval[0];
2147 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2149 devnull = td->td_retval[0];
2150 td->td_retval[0] = save;
2153 KASSERT(devnull == i, ("oof, we didn't get our fd"));
2155 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2164 * Internal form of close. Decrement reference count on file structure.
2165 * Note: td may be NULL when closing a file that was being passed in a
2168 * XXXRW: Giant is not required for the caller, but often will be held; this
2169 * makes it moderately likely the Giant will be recursed in the VFS case.
2172 closef(struct file *fp, struct thread *td)
2176 struct filedesc_to_leader *fdtol;
2177 struct filedesc *fdp;
2180 * POSIX record locking dictates that any close releases ALL
2181 * locks owned by this process. This is handled by setting
2182 * a flag in the unlock to free ONLY locks obeying POSIX
2183 * semantics, and not to free BSD-style file locks.
2184 * If the descriptor was in a message, POSIX-style locks
2185 * aren't passed with the descriptor, and the thread pointer
2186 * will be NULL. Callers should be careful only to pass a
2187 * NULL thread pointer when there really is no owning
2188 * context that might have locks, or the locks will be
2191 if (fp->f_type == DTYPE_VNODE && td != NULL) {
2193 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2194 lf.l_whence = SEEK_SET;
2197 lf.l_type = F_UNLCK;
2198 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2199 F_UNLCK, &lf, F_POSIX);
2201 fdtol = td->td_proc->p_fdtol;
2202 if (fdtol != NULL) {
2204 * Handle special case where file descriptor table is
2205 * shared between multiple process leaders.
2207 fdp = td->td_proc->p_fd;
2208 FILEDESC_XLOCK(fdp);
2209 for (fdtol = fdtol->fdl_next;
2210 fdtol != td->td_proc->p_fdtol;
2211 fdtol = fdtol->fdl_next) {
2212 if ((fdtol->fdl_leader->p_flag &
2215 fdtol->fdl_holdcount++;
2216 FILEDESC_XUNLOCK(fdp);
2217 lf.l_whence = SEEK_SET;
2220 lf.l_type = F_UNLCK;
2222 (void) VOP_ADVLOCK(vp,
2223 (caddr_t)fdtol->fdl_leader, F_UNLCK, &lf,
2225 FILEDESC_XLOCK(fdp);
2226 fdtol->fdl_holdcount--;
2227 if (fdtol->fdl_holdcount == 0 &&
2228 fdtol->fdl_wakeup != 0) {
2229 fdtol->fdl_wakeup = 0;
2233 FILEDESC_XUNLOCK(fdp);
2236 return (fdrop(fp, td));
2240 * Initialize the file pointer with the specified properties.
2242 * The ops are set with release semantics to be certain that the flags, type,
2243 * and data are visible when ops is. This is to prevent ops methods from being
2244 * called with bad data.
2247 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2252 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2256 fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t needrights,
2257 int needfcntl, struct file **fpp, cap_rights_t *haverightsp)
2262 cap_rights_t haverights;
2266 if (fd < 0 || fd >= fdp->fd_nfiles)
2269 * Fetch the descriptor locklessly. We avoid fdrop() races by
2270 * never raising a refcount above 0. To accomplish this we have
2271 * to use a cmpset loop rather than an atomic_add. The descriptor
2272 * must be re-verified once we acquire a reference to be certain
2273 * that the identity is still correct and we did not lose a race
2274 * due to preemption.
2277 fp = fdp->fd_ofiles[fd].fde_file;
2281 haverights = cap_rights(fdp, fd);
2282 error = cap_check(haverights, needrights);
2285 if ((needrights & CAP_FCNTL) != 0) {
2286 error = cap_fcntl_check(fdp, fd, needfcntl);
2291 count = fp->f_count;
2295 * Use an acquire barrier to prevent caching of fd_ofiles
2296 * so it is refreshed for verification.
2298 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2300 if (fp == fdp->fd_ofiles[fd].fde_file)
2302 fdrop(fp, curthread);
2305 if (haverightsp != NULL) {
2307 *haverightsp = haverights;
2309 *haverightsp = CAP_ALL;
2316 * Extract the file pointer associated with the specified descriptor for the
2317 * current user process.
2319 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2322 * File's rights will be checked against the capability rights mask.
2324 * If an error occured the non-zero error is returned and *fpp is set to
2325 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is
2326 * responsible for fdrop().
2329 _fget(struct thread *td, int fd, struct file **fpp, int flags,
2330 cap_rights_t needrights, u_char *maxprotp)
2332 struct filedesc *fdp;
2334 cap_rights_t haverights;
2338 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2340 if (maxprotp != NULL)
2341 needrights |= CAP_MMAP;
2342 error = fget_unlocked(fdp, fd, needrights, 0, &fp, &haverights);
2345 if (fp->f_ops == &badfileops) {
2352 * If requested, convert capability rights to access flags.
2354 if (maxprotp != NULL)
2355 *maxprotp = cap_rights_to_vmprot(haverights);
2356 #else /* !CAPABILITIES */
2357 if (maxprotp != NULL)
2358 *maxprotp = VM_PROT_ALL;
2359 #endif /* CAPABILITIES */
2362 * FREAD and FWRITE failure return EBADF as per POSIX.
2368 if ((fp->f_flag & flags) == 0)
2372 if ((fp->f_flag & (FREAD | FEXEC)) == 0 ||
2373 ((fp->f_flag & FWRITE) != 0))
2379 KASSERT(0, ("wrong flags"));
2392 fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2395 return(_fget(td, fd, fpp, 0, rights, NULL));
2399 fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp,
2403 return (_fget(td, fd, fpp, 0, rights, maxprotp));
2407 fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2410 return(_fget(td, fd, fpp, FREAD, rights, NULL));
2414 fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2417 return (_fget(td, fd, fpp, FWRITE, rights, NULL));
2421 * Like fget() but loads the underlying vnode, or returns an error if the
2422 * descriptor does not represent a vnode. Note that pipes use vnodes but
2423 * never have VM objects. The returned vnode will be vref()'d.
2425 * XXX: what about the unused flags ?
2428 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights,
2435 error = _fget(td, fd, &fp, flags, needrights, NULL);
2438 if (fp->f_vnode == NULL) {
2450 fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2453 return (_fgetvp(td, fd, 0, rights, vpp));
2457 fgetvp_rights(struct thread *td, int fd, cap_rights_t need,
2458 struct filecaps *havecaps, struct vnode **vpp)
2460 struct filedesc *fdp;
2466 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2469 fp = fget_locked(fdp, fd);
2470 if (fp == NULL || fp->f_ops == &badfileops)
2474 error = cap_check(cap_rights(fdp, fd), need);
2479 if (fp->f_vnode == NULL)
2484 filecaps_copy(&fdp->fd_ofiles[fd].fde_caps, havecaps);
2490 fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2493 return (_fgetvp(td, fd, FREAD, rights, vpp));
2497 fgetvp_exec(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2500 return (_fgetvp(td, fd, FEXEC, rights, vpp));
2505 fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
2509 return (_fgetvp(td, fd, FWRITE, rights, vpp));
2514 * Like fget() but loads the underlying socket, or returns an error if the
2515 * descriptor does not represent a socket.
2517 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2520 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2521 * on their file descriptor reference to prevent the socket from being free'd
2525 fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp,
2534 if ((error = _fget(td, fd, &fp, 0, rights, NULL)) != 0)
2536 if (fp->f_type != DTYPE_SOCKET) {
2541 *fflagp = fp->f_flag;
2552 * Drop the reference count on the socket and XXX release the SX lock in the
2553 * future. The last reference closes the socket.
2555 * Note: fputsock() is deprecated, see comment for fgetsock().
2558 fputsock(struct socket *so)
2563 CURVNET_SET(so->so_vnet);
2569 * Handle the last reference to a file being closed.
2572 _fdrop(struct file *fp, struct thread *td)
2577 if (fp->f_count != 0)
2578 panic("fdrop: count %d", fp->f_count);
2579 if (fp->f_ops != &badfileops)
2580 error = fo_close(fp, td);
2581 atomic_subtract_int(&openfiles, 1);
2583 free(fp->f_advice, M_FADVISE);
2584 uma_zfree(file_zone, fp);
2590 * Apply an advisory lock on a file descriptor.
2592 * Just attempt to get a record lock of the requested type on the entire file
2593 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2595 #ifndef _SYS_SYSPROTO_H_
2603 sys_flock(struct thread *td, struct flock_args *uap)
2610 if ((error = fget(td, uap->fd, CAP_FLOCK, &fp)) != 0)
2612 if (fp->f_type != DTYPE_VNODE) {
2614 return (EOPNOTSUPP);
2618 lf.l_whence = SEEK_SET;
2621 if (uap->how & LOCK_UN) {
2622 lf.l_type = F_UNLCK;
2623 atomic_clear_int(&fp->f_flag, FHASLOCK);
2624 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2627 if (uap->how & LOCK_EX)
2628 lf.l_type = F_WRLCK;
2629 else if (uap->how & LOCK_SH)
2630 lf.l_type = F_RDLCK;
2635 atomic_set_int(&fp->f_flag, FHASLOCK);
2636 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2637 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2643 * Duplicate the specified descriptor to a free descriptor.
2646 dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
2647 int openerror, int *indxp)
2652 KASSERT(openerror == ENODEV || openerror == ENXIO,
2653 ("unexpected error %d in %s", openerror, __func__));
2656 * If the to-be-dup'd fd number is greater than the allowed number
2657 * of file descriptors, or the fd to be dup'd has already been
2658 * closed, then reject.
2660 FILEDESC_XLOCK(fdp);
2661 if ((fp = fget_locked(fdp, dfd)) == NULL) {
2662 FILEDESC_XUNLOCK(fdp);
2666 error = fdalloc(td, 0, &indx);
2668 FILEDESC_XUNLOCK(fdp);
2673 * There are two cases of interest here.
2675 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2677 * For ENXIO steal away the file structure from (dfd) and store it in
2678 * (indx). (dfd) is effectively closed by this operation.
2680 switch (openerror) {
2683 * Check that the mode the file is being opened for is a
2684 * subset of the mode of the existing descriptor.
2686 if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
2687 fdunused(fdp, indx);
2688 FILEDESC_XUNLOCK(fdp);
2692 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2693 filecaps_copy(&fdp->fd_ofiles[dfd].fde_caps,
2694 &fdp->fd_ofiles[indx].fde_caps);
2698 * Steal away the file pointer from dfd and stuff it into indx.
2700 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2701 bzero(&fdp->fd_ofiles[dfd], sizeof(fdp->fd_ofiles[dfd]));
2705 FILEDESC_XUNLOCK(fdp);
2711 * Scan all active processes and prisons to see if any of them have a current
2712 * or root directory of `olddp'. If so, replace them with the new mount point.
2715 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2717 struct filedesc *fdp;
2722 if (vrefcnt(olddp) == 1)
2725 sx_slock(&allproc_lock);
2726 FOREACH_PROC_IN_SYSTEM(p) {
2730 FILEDESC_XLOCK(fdp);
2731 if (fdp->fd_cdir == olddp) {
2733 fdp->fd_cdir = newdp;
2736 if (fdp->fd_rdir == olddp) {
2738 fdp->fd_rdir = newdp;
2741 if (fdp->fd_jdir == olddp) {
2743 fdp->fd_jdir = newdp;
2746 FILEDESC_XUNLOCK(fdp);
2749 sx_sunlock(&allproc_lock);
2750 if (rootvnode == olddp) {
2755 mtx_lock(&prison0.pr_mtx);
2756 if (prison0.pr_root == olddp) {
2758 prison0.pr_root = newdp;
2761 mtx_unlock(&prison0.pr_mtx);
2762 sx_slock(&allprison_lock);
2763 TAILQ_FOREACH(pr, &allprison, pr_list) {
2764 mtx_lock(&pr->pr_mtx);
2765 if (pr->pr_root == olddp) {
2767 pr->pr_root = newdp;
2770 mtx_unlock(&pr->pr_mtx);
2772 sx_sunlock(&allprison_lock);
2777 struct filedesc_to_leader *
2778 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2780 struct filedesc_to_leader *fdtol;
2782 fdtol = malloc(sizeof(struct filedesc_to_leader),
2783 M_FILEDESC_TO_LEADER,
2785 fdtol->fdl_refcount = 1;
2786 fdtol->fdl_holdcount = 0;
2787 fdtol->fdl_wakeup = 0;
2788 fdtol->fdl_leader = leader;
2790 FILEDESC_XLOCK(fdp);
2791 fdtol->fdl_next = old->fdl_next;
2792 fdtol->fdl_prev = old;
2793 old->fdl_next = fdtol;
2794 fdtol->fdl_next->fdl_prev = fdtol;
2795 FILEDESC_XUNLOCK(fdp);
2797 fdtol->fdl_next = fdtol;
2798 fdtol->fdl_prev = fdtol;
2804 * Get file structures globally.
2807 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2810 struct filedesc *fdp;
2815 error = sysctl_wire_old_buffer(req, 0);
2818 if (req->oldptr == NULL) {
2820 sx_slock(&allproc_lock);
2821 FOREACH_PROC_IN_SYSTEM(p) {
2822 if (p->p_state == PRS_NEW)
2827 /* overestimates sparse tables. */
2828 if (fdp->fd_lastfile > 0)
2829 n += fdp->fd_lastfile;
2832 sx_sunlock(&allproc_lock);
2833 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2836 bzero(&xf, sizeof(xf));
2837 xf.xf_size = sizeof(xf);
2838 sx_slock(&allproc_lock);
2839 FOREACH_PROC_IN_SYSTEM(p) {
2841 if (p->p_state == PRS_NEW) {
2845 if (p_cansee(req->td, p) != 0) {
2849 xf.xf_pid = p->p_pid;
2850 xf.xf_uid = p->p_ucred->cr_uid;
2855 FILEDESC_SLOCK(fdp);
2856 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2857 if ((fp = fdp->fd_ofiles[n].fde_file) == NULL)
2861 xf.xf_data = fp->f_data;
2862 xf.xf_vnode = fp->f_vnode;
2863 xf.xf_type = fp->f_type;
2864 xf.xf_count = fp->f_count;
2866 xf.xf_offset = foffset_get(fp);
2867 xf.xf_flag = fp->f_flag;
2868 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2872 FILEDESC_SUNLOCK(fdp);
2877 sx_sunlock(&allproc_lock);
2881 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2882 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2884 #ifdef KINFO_OFILE_SIZE
2885 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2888 #ifdef COMPAT_FREEBSD7
2890 export_vnode_for_osysctl(struct vnode *vp, int type,
2891 struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2894 char *fullpath, *freepath;
2896 bzero(kif, sizeof(*kif));
2897 kif->kf_structsize = sizeof(*kif);
2901 kif->kf_type = KF_TYPE_VNODE;
2902 /* This function only handles directories. */
2903 if (vp->v_type != VDIR) {
2907 kif->kf_vnode_type = KF_VTYPE_VDIR;
2910 * This is not a true file descriptor, so we set a bogus refcount
2911 * and offset to indicate these fields should be ignored.
2913 kif->kf_ref_count = -1;
2914 kif->kf_offset = -1;
2918 FILEDESC_SUNLOCK(fdp);
2919 vn_fullpath(curthread, vp, &fullpath, &freepath);
2921 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2922 if (freepath != NULL)
2923 free(freepath, M_TEMP);
2924 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2925 FILEDESC_SLOCK(fdp);
2930 * Get per-process file descriptors for use by procstat(1), et al.
2933 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
2935 char *fullpath, *freepath;
2936 struct kinfo_ofile *kif;
2937 struct filedesc *fdp;
2938 int error, i, *name;
2939 struct shmfd *shmfd;
2947 if ((p = pfind((pid_t)name[0])) == NULL)
2949 if ((error = p_candebug(curthread, p))) {
2957 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2958 FILEDESC_SLOCK(fdp);
2959 if (fdp->fd_cdir != NULL)
2960 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
2962 if (fdp->fd_rdir != NULL)
2963 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
2965 if (fdp->fd_jdir != NULL)
2966 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
2968 for (i = 0; i < fdp->fd_nfiles; i++) {
2969 if ((fp = fdp->fd_ofiles[i].fde_file) == NULL)
2971 bzero(kif, sizeof(*kif));
2972 kif->kf_structsize = sizeof(*kif);
2979 switch (fp->f_type) {
2981 kif->kf_type = KF_TYPE_VNODE;
2986 kif->kf_type = KF_TYPE_SOCKET;
2991 kif->kf_type = KF_TYPE_PIPE;
2995 kif->kf_type = KF_TYPE_FIFO;
3000 kif->kf_type = KF_TYPE_KQUEUE;
3004 kif->kf_type = KF_TYPE_CRYPTO;
3008 kif->kf_type = KF_TYPE_MQUEUE;
3012 kif->kf_type = KF_TYPE_SHM;
3017 kif->kf_type = KF_TYPE_SEM;
3021 kif->kf_type = KF_TYPE_PTS;
3026 case DTYPE_PROCDESC:
3027 kif->kf_type = KF_TYPE_PROCDESC;
3032 kif->kf_type = KF_TYPE_UNKNOWN;
3035 kif->kf_ref_count = fp->f_count;
3036 if (fp->f_flag & FREAD)
3037 kif->kf_flags |= KF_FLAG_READ;
3038 if (fp->f_flag & FWRITE)
3039 kif->kf_flags |= KF_FLAG_WRITE;
3040 if (fp->f_flag & FAPPEND)
3041 kif->kf_flags |= KF_FLAG_APPEND;
3042 if (fp->f_flag & FASYNC)
3043 kif->kf_flags |= KF_FLAG_ASYNC;
3044 if (fp->f_flag & FFSYNC)
3045 kif->kf_flags |= KF_FLAG_FSYNC;
3046 if (fp->f_flag & FNONBLOCK)
3047 kif->kf_flags |= KF_FLAG_NONBLOCK;
3048 if (fp->f_flag & O_DIRECT)
3049 kif->kf_flags |= KF_FLAG_DIRECT;
3050 if (fp->f_flag & FHASLOCK)
3051 kif->kf_flags |= KF_FLAG_HASLOCK;
3052 kif->kf_offset = foffset_get(fp);
3055 switch (vp->v_type) {
3057 kif->kf_vnode_type = KF_VTYPE_VNON;
3060 kif->kf_vnode_type = KF_VTYPE_VREG;
3063 kif->kf_vnode_type = KF_VTYPE_VDIR;
3066 kif->kf_vnode_type = KF_VTYPE_VBLK;
3069 kif->kf_vnode_type = KF_VTYPE_VCHR;
3072 kif->kf_vnode_type = KF_VTYPE_VLNK;
3075 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3078 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3081 kif->kf_vnode_type = KF_VTYPE_VBAD;
3084 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3088 * It is OK to drop the filedesc lock here as we will
3089 * re-validate and re-evaluate its properties when
3090 * the loop continues.
3094 FILEDESC_SUNLOCK(fdp);
3095 vn_fullpath(curthread, vp, &fullpath, &freepath);
3097 strlcpy(kif->kf_path, fullpath,
3098 sizeof(kif->kf_path));
3099 if (freepath != NULL)
3100 free(freepath, M_TEMP);
3101 FILEDESC_SLOCK(fdp);
3104 struct sockaddr *sa;
3106 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3107 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3108 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3111 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3112 == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3113 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3116 kif->kf_sock_domain =
3117 so->so_proto->pr_domain->dom_family;
3118 kif->kf_sock_type = so->so_type;
3119 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3122 strlcpy(kif->kf_path, tty_devname(tp),
3123 sizeof(kif->kf_path));
3126 shm_path(shmfd, kif->kf_path, sizeof(kif->kf_path));
3127 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3131 FILEDESC_SUNLOCK(fdp);
3137 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3138 sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3139 #endif /* COMPAT_FREEBSD7 */
3141 #ifdef KINFO_FILE_SIZE
3142 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3146 export_fd_for_sysctl(void *data, int type, int fd, int fflags, int refcnt,
3147 int64_t offset, cap_rights_t fd_cap_rights, struct kinfo_file *kif,
3148 struct sysctl_req *req)
3153 } fflags_table[] = {
3154 { FAPPEND, KF_FLAG_APPEND },
3155 { FASYNC, KF_FLAG_ASYNC },
3156 { FFSYNC, KF_FLAG_FSYNC },
3157 { FHASLOCK, KF_FLAG_HASLOCK },
3158 { FNONBLOCK, KF_FLAG_NONBLOCK },
3159 { FREAD, KF_FLAG_READ },
3160 { FWRITE, KF_FLAG_WRITE },
3161 { O_CREAT, KF_FLAG_CREAT },
3162 { O_DIRECT, KF_FLAG_DIRECT },
3163 { O_EXCL, KF_FLAG_EXCL },
3164 { O_EXEC, KF_FLAG_EXEC },
3165 { O_EXLOCK, KF_FLAG_EXLOCK },
3166 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3167 { O_SHLOCK, KF_FLAG_SHLOCK },
3168 { O_TRUNC, KF_FLAG_TRUNC }
3170 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3175 bzero(kif, sizeof(*kif));
3179 vp = (struct vnode *)data;
3180 error = fill_vnode_info(vp, kif);
3183 case KF_TYPE_SOCKET:
3184 error = fill_socket_info((struct socket *)data, kif);
3187 error = fill_pipe_info((struct pipe *)data, kif);
3190 error = fill_pts_info((struct tty *)data, kif);
3192 case KF_TYPE_PROCDESC:
3193 error = fill_procdesc_info((struct procdesc *)data, kif);
3196 error = fill_shm_info((struct file *)data, kif);
3202 kif->kf_status |= KF_ATTR_VALID;
3205 * Translate file access flags.
3207 for (i = 0; i < NFFLAGS; i++)
3208 if (fflags & fflags_table[i].fflag)
3209 kif->kf_flags |= fflags_table[i].kf_fflag;
3210 kif->kf_cap_rights = fd_cap_rights;
3212 kif->kf_type = type;
3213 kif->kf_ref_count = refcnt;
3214 kif->kf_offset = offset;
3215 /* Pack record size down */
3216 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3217 strlen(kif->kf_path) + 1;
3218 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3219 error = SYSCTL_OUT(req, kif, kif->kf_structsize);
3224 * Get per-process file descriptors for use by procstat(1), et al.
3227 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3230 struct filedesc *fdp;
3231 struct kinfo_file *kif;
3233 struct vnode *cttyvp, *textvp, *tracevp;
3237 int error, i, *name;
3238 int type, refcnt, fflags;
3239 cap_rights_t fd_cap_rights;
3242 if ((p = pfind((pid_t)name[0])) == NULL)
3244 if ((error = p_candebug(curthread, p))) {
3249 tracevp = p->p_tracevp;
3250 if (tracevp != NULL)
3253 textvp = p->p_textvp;
3256 /* Controlling tty. */
3258 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3259 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3265 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
3266 if (tracevp != NULL)
3267 export_fd_for_sysctl(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3268 FREAD | FWRITE, -1, -1, 0, kif, req);
3270 export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3271 FREAD, -1, -1, 0, kif, req);
3273 export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3274 FREAD | FWRITE, -1, -1, 0, kif, req);
3277 FILEDESC_SLOCK(fdp);
3278 /* working directory */
3279 if (fdp->fd_cdir != NULL) {
3281 data = fdp->fd_cdir;
3282 FILEDESC_SUNLOCK(fdp);
3283 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3284 FREAD, -1, -1, 0, kif, req);
3285 FILEDESC_SLOCK(fdp);
3287 /* root directory */
3288 if (fdp->fd_rdir != NULL) {
3290 data = fdp->fd_rdir;
3291 FILEDESC_SUNLOCK(fdp);
3292 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3293 FREAD, -1, -1, 0, kif, req);
3294 FILEDESC_SLOCK(fdp);
3296 /* jail directory */
3297 if (fdp->fd_jdir != NULL) {
3299 data = fdp->fd_jdir;
3300 FILEDESC_SUNLOCK(fdp);
3301 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3302 FREAD, -1, -1, 0, kif, req);
3303 FILEDESC_SLOCK(fdp);
3305 for (i = 0; i < fdp->fd_nfiles; i++) {
3306 if ((fp = fdp->fd_ofiles[i].fde_file) == NULL)
3310 fd_cap_rights = cap_rights(fdp, i);
3311 #else /* !CAPABILITIES */
3314 switch (fp->f_type) {
3316 type = KF_TYPE_VNODE;
3322 type = KF_TYPE_SOCKET;
3327 type = KF_TYPE_PIPE;
3332 type = KF_TYPE_FIFO;
3338 type = KF_TYPE_KQUEUE;
3342 type = KF_TYPE_CRYPTO;
3346 type = KF_TYPE_MQUEUE;
3364 case DTYPE_PROCDESC:
3365 type = KF_TYPE_PROCDESC;
3371 type = KF_TYPE_UNKNOWN;
3374 refcnt = fp->f_count;
3375 fflags = fp->f_flag;
3376 offset = foffset_get(fp);
3379 * Create sysctl entry.
3380 * It is OK to drop the filedesc lock here as we will
3381 * re-validate and re-evaluate its properties when
3382 * the loop continues.
3384 oldidx = req->oldidx;
3385 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3386 FILEDESC_SUNLOCK(fdp);
3387 error = export_fd_for_sysctl(data, type, i, fflags, refcnt,
3388 offset, fd_cap_rights, kif, req);
3389 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3390 FILEDESC_SLOCK(fdp);
3392 if (error == ENOMEM) {
3394 * The hack to keep the ABI of sysctl
3395 * kern.proc.filedesc intact, but not
3396 * to account a partially copied
3397 * kinfo_file into the oldidx.
3399 req->oldidx = oldidx;
3405 FILEDESC_SUNLOCK(fdp);
3414 vntype_to_kinfo(int vtype)
3419 } vtypes_table[] = {
3420 { VBAD, KF_VTYPE_VBAD },
3421 { VBLK, KF_VTYPE_VBLK },
3422 { VCHR, KF_VTYPE_VCHR },
3423 { VDIR, KF_VTYPE_VDIR },
3424 { VFIFO, KF_VTYPE_VFIFO },
3425 { VLNK, KF_VTYPE_VLNK },
3426 { VNON, KF_VTYPE_VNON },
3427 { VREG, KF_VTYPE_VREG },
3428 { VSOCK, KF_VTYPE_VSOCK }
3430 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3434 * Perform vtype translation.
3436 for (i = 0; i < NVTYPES; i++)
3437 if (vtypes_table[i].vtype == vtype)
3440 return (vtypes_table[i].kf_vtype);
3442 return (KF_VTYPE_UNKNOWN);
3446 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3449 char *fullpath, *freepath;
3454 kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3457 error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3459 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3461 if (freepath != NULL)
3462 free(freepath, M_TEMP);
3465 * Retrieve vnode attributes.
3467 va.va_fsid = VNOVAL;
3469 vn_lock(vp, LK_SHARED | LK_RETRY);
3470 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3474 if (va.va_fsid != VNOVAL)
3475 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3477 kif->kf_un.kf_file.kf_file_fsid =
3478 vp->v_mount->mnt_stat.f_fsid.val[0];
3479 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3480 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3481 kif->kf_un.kf_file.kf_file_size = va.va_size;
3482 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3487 fill_socket_info(struct socket *so, struct kinfo_file *kif)
3489 struct sockaddr *sa;
3490 struct inpcb *inpcb;
3491 struct unpcb *unpcb;
3496 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3497 kif->kf_sock_type = so->so_type;
3498 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3499 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3500 switch(kif->kf_sock_domain) {
3503 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3504 if (so->so_pcb != NULL) {
3505 inpcb = (struct inpcb *)(so->so_pcb);
3506 kif->kf_un.kf_sock.kf_sock_inpcb =
3507 (uintptr_t)inpcb->inp_ppcb;
3512 if (so->so_pcb != NULL) {
3513 unpcb = (struct unpcb *)(so->so_pcb);
3514 if (unpcb->unp_conn) {
3515 kif->kf_un.kf_sock.kf_sock_unpconn =
3516 (uintptr_t)unpcb->unp_conn;
3517 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3518 so->so_rcv.sb_state;
3519 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3520 so->so_snd.sb_state;
3525 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3526 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3527 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3530 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3531 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3532 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3535 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3536 sizeof(kif->kf_path));
3541 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3546 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3547 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3552 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3557 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3558 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3559 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3564 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
3569 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3574 fill_shm_info(struct file *fp, struct kinfo_file *kif)
3580 if (fp->f_data == NULL)
3582 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3584 shm_path(fp->f_data, kif->kf_path, sizeof(kif->kf_path));
3585 kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
3586 kif->kf_un.kf_file.kf_file_size = sb.st_size;
3590 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3591 sysctl_kern_proc_filedesc, "Process filedesc entries");
3595 * For the purposes of debugging, generate a human-readable string for the
3599 file_type_to_name(short type)
3629 * For the purposes of debugging, identify a process (if any, perhaps one of
3630 * many) that references the passed file in its file descriptor array. Return
3633 static struct proc *
3634 file_to_first_proc(struct file *fp)
3636 struct filedesc *fdp;
3640 FOREACH_PROC_IN_SYSTEM(p) {
3641 if (p->p_state == PRS_NEW)
3646 for (n = 0; n < fdp->fd_nfiles; n++) {
3647 if (fp == fdp->fd_ofiles[n].fde_file)
3655 db_print_file(struct file *fp, int header)
3660 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3661 "File", "Type", "Data", "Flag", "GCFl", "Count",
3662 "MCount", "Vnode", "FPID", "FCmd");
3663 p = file_to_first_proc(fp);
3664 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3665 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3666 0, fp->f_count, 0, fp->f_vnode,
3667 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3670 DB_SHOW_COMMAND(file, db_show_file)
3675 db_printf("usage: show file <addr>\n");
3678 fp = (struct file *)addr;
3679 db_print_file(fp, 1);
3682 DB_SHOW_COMMAND(files, db_show_files)
3684 struct filedesc *fdp;
3691 FOREACH_PROC_IN_SYSTEM(p) {
3692 if (p->p_state == PRS_NEW)
3694 if ((fdp = p->p_fd) == NULL)
3696 for (n = 0; n < fdp->fd_nfiles; ++n) {
3697 if ((fp = fdp->fd_ofiles[n].fde_file) == NULL)
3699 db_print_file(fp, header);
3706 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3707 &maxfilesperproc, 0, "Maximum files allowed open per process");
3709 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3710 &maxfiles, 0, "Maximum number of files");
3712 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3713 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3717 filelistinit(void *dummy)
3720 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3721 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3722 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3723 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3725 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3727 /*-------------------------------------------------------------------*/
3730 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred,
3731 int flags, struct thread *td)
3738 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
3746 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
3754 badfo_poll(struct file *fp, int events, struct ucred *active_cred,
3762 badfo_kqfilter(struct file *fp, struct knote *kn)
3769 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
3777 badfo_close(struct file *fp, struct thread *td)
3784 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3792 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3799 struct fileops badfileops = {
3800 .fo_read = badfo_readwrite,
3801 .fo_write = badfo_readwrite,
3802 .fo_truncate = badfo_truncate,
3803 .fo_ioctl = badfo_ioctl,
3804 .fo_poll = badfo_poll,
3805 .fo_kqfilter = badfo_kqfilter,
3806 .fo_stat = badfo_stat,
3807 .fo_close = badfo_close,
3808 .fo_chmod = badfo_chmod,
3809 .fo_chown = badfo_chown,
3813 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3821 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3828 /*-------------------------------------------------------------------*/
3831 * File Descriptor pseudo-device driver (/dev/fd/).
3833 * Opening minor device N dup()s the file (if any) connected to file
3834 * descriptor N belonging to the calling process. Note that this driver
3835 * consists of only the ``open()'' routine, because all subsequent
3836 * references to this file will be direct to the other driver.
3838 * XXX: we could give this one a cloning event handler if necessary.
3843 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
3847 * XXX Kludge: set curthread->td_dupfd to contain the value of the
3848 * the file descriptor being sought for duplication. The error
3849 * return ensures that the vnode for this device will be released
3850 * by vn_open. Open will detect this special error and take the
3851 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3852 * will simply report the error.
3854 td->td_dupfd = dev2unit(dev);
3858 static struct cdevsw fildesc_cdevsw = {
3859 .d_version = D_VERSION,
3865 fildesc_drvinit(void *unused)
3869 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
3870 UID_ROOT, GID_WHEEL, 0666, "fd/0");
3871 make_dev_alias(dev, "stdin");
3872 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
3873 UID_ROOT, GID_WHEEL, 0666, "fd/1");
3874 make_dev_alias(dev, "stdout");
3875 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
3876 UID_ROOT, GID_WHEEL, 0666, "fd/2");
3877 make_dev_alias(dev, "stderr");
3880 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);