37 #include <sys/cdefs.h>
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>
59 #include <sys/limits.h>
61 #include <sys/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/mqueue.h>
65 #include <sys/mutex.h>
66 #include <sys/namei.h>
67 #include <sys/selinfo.h>
71 #include <sys/procdesc.h>
72 #include <sys/protosw.h>
73 #include <sys/racct.h>
74 #include <sys/resourcevar.h>
76 #include <sys/signalvar.h>
77 #include <sys/socketvar.h>
80 #include <sys/syscallsubr.h>
81 #include <sys/sysctl.h>
82 #include <sys/sysproto.h>
84 #include <sys/unistd.h>
86 #include <sys/unpcb.h>
88 #include <sys/vnode.h>
90 #include <sys/ktrace.h>
95 #include <netinet/in.h>
96 #include <netinet/in_pcb.h>
98 #include <security/audit/audit.h>
105 static MALLOC_DEFINE(M_FILEDESC,
"filedesc",
"Open file descriptor table");
106 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER,
"filedesc_to_leader",
107 "file desc to leader structures");
117 #define DUP_FIXED 0x1
118 #define DUP_FCNTL 0x2
119 #define DUP_CLOEXEC 0x4
121 static int do_dup(
struct thread *td,
int flags,
int old,
int new,
126 static void fdunused(
struct filedesc *fdp,
int fd);
127 static void fdused(
struct filedesc *fdp,
int fd);
130 static int fill_pts_info(
struct tty *tp,
struct kinfo_file *kif);
131 static int fill_pipe_info(
struct pipe *pi,
struct kinfo_file *kif);
133 struct kinfo_file *kif);
134 static int fill_sem_info(
struct file *fp,
struct kinfo_file *kif);
135 static int fill_shm_info(
struct file *fp,
struct kinfo_file *kif);
136 static int getmaxfd(
struct proc *p);
147 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
148 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
149 #define NDSLOT(x) ((x) / NDENTRIES)
150 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
151 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
156 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
171 struct filedesc fd_fd;
180 struct file *fd_dfiles[
NDFILE];
181 char fd_dfileflags[NDFILE];
182 NDSLOTTYPE fd_dmap[
NDSLOTS(NDFILE)];
202 NDSLOTTYPE *map = fdp->fd_map;
211 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
212 if ((mask &= ~map[off]) != 0UL)
213 return (off * NDENTRIES + ffsl(mask) - 1);
216 for (maxoff =
NDSLOTS(size); off < maxoff; ++off)
217 if (map[off] != ~0UL)
218 return (off * NDENTRIES + ffsl(~map[off]) - 1);
229 NDSLOTTYPE *map = fdp->fd_map;
238 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
239 if ((mask &= map[off]) != 0)
240 return (off * NDENTRIES + flsl(mask) - 1);
243 for (minoff =
NDSLOT(low); off >= minoff; --off)
245 return (off * NDENTRIES + flsl(map[off]) - 1);
252 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
253 (
"file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
254 return ((fdp->fd_map[
NDSLOT(fd)] &
NDBIT(fd)) != 0);
264 FILEDESC_XLOCK_ASSERT(fdp);
266 (
"fd already used"));
269 if (fd > fdp->fd_lastfile)
270 fdp->fd_lastfile =
fd;
271 if (fd == fdp->fd_freefile)
282 FILEDESC_XLOCK_ASSERT(fdp);
284 (
"fd is already unused"));
285 KASSERT(fdp->fd_ofiles[fd] == NULL,
286 (
"fd is still in use"));
289 if (fd < fdp->fd_freefile)
290 fdp->fd_freefile =
fd;
291 if (fd == fdp->fd_lastfile)
298 #ifndef _SYS_SYSPROTO_H_
307 struct proc *p = td->td_proc;
315 if (lim < td->td_retval[0])
316 td->td_retval[0] = lim;
326 #ifndef _SYS_SYSPROTO_H_
344 #ifndef _SYS_SYSPROTO_H_
354 return (
do_dup(td, 0, (
int)uap->
fd, 0, td->td_retval));
360 #ifndef _SYS_SYSPROTO_H_
386 error = copyin((
void *)(intptr_t)uap->
arg, &ofl,
sizeof(ofl));
387 fl.l_start = ofl.l_start;
388 fl.l_len = ofl.l_len;
389 fl.l_pid = ofl.l_pid;
390 fl.l_type = ofl.l_type;
391 fl.l_whence = ofl.l_whence;
411 error = copyin((
void *)(intptr_t)uap->
arg, &fl,
sizeof(fl));
423 if (uap->
cmd == F_OGETLK) {
424 ofl.l_start = fl.l_start;
425 ofl.l_len = fl.l_len;
426 ofl.l_pid = fl.l_pid;
427 ofl.l_type = fl.l_type;
428 ofl.l_whence = fl.l_whence;
429 error = copyout(&ofl, (
void *)(intptr_t)uap->
arg,
sizeof(ofl));
430 }
else if (uap->
cmd == F_GETLK) {
431 error = copyout(&fl, (
void *)(intptr_t)uap->
arg,
sizeof(fl));
436 static inline struct file *
441 FILEDESC_LOCK_ASSERT(fdp);
442 if ((
unsigned)fd >= fdp->fd_nfiles ||
443 (fp = fdp->fd_ofiles[fd]) == NULL)
449 fdunwrap(
int fd, cap_rights_t rights,
struct filedesc *fdp,
struct file **fpp)
457 if ((*fpp)->f_type == DTYPE_CAPABILITY) {
471 struct filedesc *fdp;
495 case F_DUPFD_CLOEXEC:
506 case F_DUP2FD_CLOEXEC:
514 if ((fp =
fdtofp(fd, fdp)) == NULL) {
515 FILEDESC_SUNLOCK(fdp);
519 pop = &fdp->fd_ofileflags[
fd];
520 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
521 FILEDESC_SUNLOCK(fdp);
526 if ((fp =
fdtofp(fd, fdp)) == NULL) {
527 FILEDESC_XUNLOCK(fdp);
531 pop = &fdp->fd_ofileflags[
fd];
532 *pop = (*pop &~ UF_EXCLOSE) |
533 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
534 FILEDESC_XUNLOCK(fdp);
539 error =
fdunwrap(fd, CAP_FCNTL, fdp, &fp);
541 FILEDESC_SUNLOCK(fdp);
544 td->td_retval[0] = OFLAGS(fp->f_flag);
545 FILEDESC_SUNLOCK(fdp);
550 error =
fdunwrap(fd, CAP_FCNTL, fdp, &fp);
552 FILEDESC_SUNLOCK(fdp);
556 FILEDESC_SUNLOCK(fdp);
558 tmp = flg = fp->f_flag;
560 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
561 }
while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
562 tmp = fp->f_flag & FNONBLOCK;
563 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
568 tmp = fp->f_flag & FASYNC;
569 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
574 atomic_clear_int(&fp->f_flag, FNONBLOCK);
576 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
582 error =
fdunwrap(fd, CAP_FCNTL, fdp, &fp);
584 FILEDESC_SUNLOCK(fdp);
588 FILEDESC_SUNLOCK(fdp);
589 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
591 td->td_retval[0] = tmp;
597 error =
fdunwrap(fd, CAP_FCNTL, fdp, &fp);
599 FILEDESC_SUNLOCK(fdp);
603 FILEDESC_SUNLOCK(fdp);
605 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
623 error =
fdunwrap(fd, CAP_FLOCK, fdp, &fp);
625 FILEDESC_SUNLOCK(fdp);
628 if (fp->f_type != DTYPE_VNODE) {
629 FILEDESC_SUNLOCK(fdp);
633 flp = (
struct flock *)arg;
634 if (flp->l_whence == SEEK_CUR) {
635 foffset = foffset_get(fp);
638 foffset > OFF_MAX - flp->l_start)) {
639 FILEDESC_SUNLOCK(fdp);
643 flp->l_start += foffset;
650 FILEDESC_SUNLOCK(fdp);
652 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
653 switch (flp->l_type) {
655 if ((fp->f_flag & FREAD) == 0) {
659 PROC_LOCK(p->p_leader);
660 p->p_leader->p_flag |= P_ADVLOCK;
661 PROC_UNLOCK(p->p_leader);
662 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
666 if ((fp->f_flag & FWRITE) == 0) {
670 PROC_LOCK(p->p_leader);
671 p->p_leader->p_flag |= P_ADVLOCK;
672 PROC_UNLOCK(p->p_leader);
673 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
677 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
685 if (flg != F_REMOTE) {
689 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
690 F_UNLCKSYS, flp, flg);
696 VFS_UNLOCK_GIANT(vfslocked);
700 if ((
unsigned) fd >= fdp->fd_nfiles ||
701 fp != fdp->fd_ofiles[fd]) {
702 FILEDESC_SUNLOCK(fdp);
703 flp->l_whence = SEEK_SET;
706 flp->l_type = F_UNLCK;
707 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
708 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
709 F_UNLCK, flp, F_POSIX);
710 VFS_UNLOCK_GIANT(vfslocked);
713 FILEDESC_SUNLOCK(fdp);
719 error =
fdunwrap(fd, CAP_FLOCK, fdp, &fp);
721 FILEDESC_SUNLOCK(fdp);
724 if (fp->f_type != DTYPE_VNODE) {
725 FILEDESC_SUNLOCK(fdp);
729 flp = (
struct flock *)arg;
730 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
731 flp->l_type != F_UNLCK) {
732 FILEDESC_SUNLOCK(fdp);
736 if (flp->l_whence == SEEK_CUR) {
737 foffset = foffset_get(fp);
738 if ((flp->l_start > 0 &&
739 foffset > OFF_MAX - flp->l_start) ||
741 foffset < OFF_MIN - flp->l_start)) {
742 FILEDESC_SUNLOCK(fdp);
746 flp->l_start += foffset;
752 FILEDESC_SUNLOCK(fdp);
754 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
755 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
757 VFS_UNLOCK_GIANT(vfslocked);
763 arg = arg ? 128 * 1024: 0;
767 if ((fp =
fdtofp(fd, fdp)) == NULL) {
768 FILEDESC_SUNLOCK(fdp);
772 if (fp->f_type != DTYPE_VNODE) {
773 FILEDESC_SUNLOCK(fdp);
778 FILEDESC_SUNLOCK(fdp);
781 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
782 error = vn_lock(vp, LK_SHARED);
784 goto readahead_vnlock_fail;
785 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
787 fp->f_seqcount = (arg + bsize - 1) / bsize;
789 new = old = fp->f_flag;
791 }
while (!atomic_cmpset_rel_int(&fp->f_flag, old,
new));
792 readahead_vnlock_fail:
793 VFS_UNLOCK_GIANT(vfslocked);
797 new = old = fp->f_flag;
799 }
while (!atomic_cmpset_rel_int(&fp->f_flag, old,
new));
808 VFS_UNLOCK_GIANT(vfslocked);
828 do_dup(
struct thread *td,
int flags,
int old,
int new,
831 struct filedesc *fdp;
835 int error, holdleaders, maxfd;
848 return (flags &
DUP_FCNTL ? EINVAL : EBADF);
851 return (flags &
DUP_FCNTL ? EINVAL : EBADF);
854 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
855 FILEDESC_XUNLOCK(fdp);
861 fdp->fd_ofileflags[
new] |= UF_EXCLOSE;
862 FILEDESC_XUNLOCK(fdp);
865 fp = fdp->fd_ofiles[old];
876 if (
new >= fdp->fd_nfiles) {
887 error =
racct_set(p, RACCT_NOFILE,
new + 1);
890 FILEDESC_XUNLOCK(fdp);
897 if (fdp->fd_ofiles[
new] == NULL)
900 if ((error =
fdalloc(td,
new, &
new)) != 0) {
901 FILEDESC_XUNLOCK(fdp);
912 if (fdp->fd_ofiles[old] != fp) {
914 if (fdp->fd_ofiles[
new] == NULL)
916 FILEDESC_XUNLOCK(fdp);
921 (
"new fd is same as old"));
930 delfp = fdp->fd_ofiles[
new];
933 if (td->td_proc->p_fdtol != NULL) {
938 fdp->fd_holdleaderscount++;
946 fdp->fd_ofiles[
new] = fp;
948 fdp->fd_ofileflags[
new] = fdp->fd_ofileflags[old] | UF_EXCLOSE;
950 fdp->fd_ofileflags[
new] = fdp->fd_ofileflags[old] & ~UF_EXCLOSE;
951 if (
new > fdp->fd_lastfile)
952 fdp->fd_lastfile =
new;
964 if (delfp->f_type == DTYPE_MQUEUE)
966 FILEDESC_XUNLOCK(fdp);
970 fdp->fd_holdleaderscount--;
971 if (fdp->fd_holdleaderscount == 0 &&
972 fdp->fd_holdleaderswakeup != 0) {
973 fdp->fd_holdleaderswakeup = 0;
974 wakeup(&fdp->fd_holdleaderscount);
976 FILEDESC_XUNLOCK(fdp);
979 FILEDESC_XUNLOCK(fdp);
1000 *(sigio->sio_myref) = NULL;
1001 if ((sigio)->sio_pgid < 0) {
1002 struct pgrp *pg = (sigio)->sio_pgrp;
1004 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
1005 sigio, sio_pgsigio);
1008 struct proc *p = (sigio)->sio_proc;
1010 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
1011 sigio, sio_pgsigio);
1015 crfree(sigio->sio_ucred);
1016 free(sigio, M_SIGIO);
1030 struct sigio *sigio;
1032 sigio = SLIST_FIRST(sigiolst);
1042 if (sigio->sio_pgid < 0) {
1043 pg = sigio->sio_pgrp;
1044 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
1046 p = sigio->sio_proc;
1047 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1051 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
1052 *(sigio->sio_myref) = NULL;
1054 KASSERT(sigio->sio_pgid < 0,
1055 (
"Proc sigio in pgrp sigio list"));
1056 KASSERT(sigio->sio_pgrp == pg,
1057 (
"Bogus pgrp in sigio list"));
1059 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
1063 KASSERT(sigio->sio_pgid > 0,
1064 (
"Pgrp sigio in proc sigio list"));
1065 KASSERT(sigio->sio_proc == p,
1066 (
"Bogus proc in sigio list"));
1068 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
1073 crfree(sigio->sio_ucred);
1074 free(sigio, M_SIGIO);
1091 struct sigio *sigio;
1102 sigio =
malloc(
sizeof(
struct sigio), M_SIGIO, M_WAITOK);
1103 sigio->sio_pgid = pgid;
1104 sigio->sio_ucred =
crhold(curthread->td_ucred);
1105 sigio->sio_myref = sigiop;
1124 if (proc->p_session != curthread->td_proc->p_session) {
1146 if (pgrp->pg_session != curthread->td_proc->p_session) {
1161 if ((proc->p_flag & P_WEXIT) != 0) {
1166 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1167 sigio->sio_proc = proc;
1171 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1172 sigio->sio_pgrp = pgrp;
1183 crfree(sigio->sio_ucred);
1184 free(sigio, M_SIGIO);
1193 struct sigio **sigiop;
1198 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1206 #ifndef _SYS_SYSPROTO_H_
1226 struct filedesc *fdp;
1227 struct file *fp, *fp_object;
1233 fdp = td->td_proc->p_fd;
1235 AUDIT_SYSCLOSE(td, fd);
1237 FILEDESC_XLOCK(fdp);
1238 if ((
unsigned)fd >= fdp->fd_nfiles ||
1239 (fp = fdp->fd_ofiles[fd]) == NULL) {
1240 FILEDESC_XUNLOCK(fdp);
1243 fdp->fd_ofiles[
fd] = NULL;
1244 fdp->fd_ofileflags[
fd] = 0;
1246 if (td->td_proc->p_fdtol != NULL) {
1251 fdp->fd_holdleaderscount++;
1268 if (fp_object->f_type == DTYPE_MQUEUE)
1270 FILEDESC_XUNLOCK(fdp);
1274 FILEDESC_XLOCK(fdp);
1275 fdp->fd_holdleaderscount--;
1276 if (fdp->fd_holdleaderscount == 0 &&
1277 fdp->fd_holdleaderswakeup != 0) {
1278 fdp->fd_holdleaderswakeup = 0;
1279 wakeup(&fdp->fd_holdleaderscount);
1281 FILEDESC_XUNLOCK(fdp);
1289 #ifndef _SYS_SYSPROTO_H_
1298 struct filedesc *fdp;
1301 fdp = td->td_proc->p_fd;
1302 AUDIT_ARG_FD(uap->
lowfd);
1310 FILEDESC_SLOCK(fdp);
1311 for (fd = uap->
lowfd; fd < fdp->fd_nfiles; fd++) {
1312 if (fdp->fd_ofiles[fd] != NULL) {
1313 FILEDESC_SUNLOCK(fdp);
1315 FILEDESC_SLOCK(fdp);
1318 FILEDESC_SUNLOCK(fdp);
1322 #if defined(COMPAT_43)
1326 #ifndef _SYS_SYSPROTO_H_
1327 struct ofstat_args {
1334 ofstat(
struct thread *td,
struct ofstat_args *uap)
1343 error = copyout(&oub, uap->sb,
sizeof(oub));
1352 #ifndef _SYS_SYSPROTO_H_
1367 error = copyout(&ub, uap->
sb,
sizeof(ub));
1379 if ((error =
fget(td, fd, CAP_FSTAT, &fp)) != 0)
1382 AUDIT_ARG_FILE(td->td_proc, fp);
1384 error = fo_stat(fp, sbp, td->td_ucred, td);
1387 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1396 #ifndef _SYS_SYSPROTO_H_
1413 error = copyout(&nub, uap->
sb,
sizeof(nub));
1421 #ifndef _SYS_SYSPROTO_H_
1435 if ((error =
fget(td, uap->
fd, CAP_FPATHCONF, &fp)) != 0)
1439 if (uap->
name == _PC_ASYNC_IO) {
1446 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1447 vn_lock(vp, LK_SHARED | LK_RETRY);
1448 error = VOP_PATHCONF(vp, uap->
name, td->td_retval);
1450 VFS_UNLOCK_GIANT(vfslocked);
1451 }
else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1452 if (uap->
name != _PC_PIPE_BUF) {
1455 td->td_retval[0] = PIPE_BUF;
1474 struct filedesc0 *fdp0;
1476 struct file **ntable;
1477 struct file **otable;
1479 int nnfiles, onfiles;
1482 FILEDESC_XLOCK_ASSERT(fdp);
1484 KASSERT(fdp->fd_nfiles > 0,
1485 (
"zero-length file table"));
1488 onfiles = fdp->fd_nfiles;
1490 if (nnfiles <= onfiles)
1495 FILEDESC_XUNLOCK(fdp);
1497 M_FILEDESC, M_ZERO | M_WAITOK);
1498 nfileflags = (
char *)&ntable[nnfiles];
1501 M_FILEDESC, M_ZERO | M_WAITOK);
1504 FILEDESC_XLOCK(fdp);
1510 onfiles = fdp->fd_nfiles;
1511 if (onfiles >= nnfiles) {
1513 free(ntable, M_FILEDESC);
1515 free(nmap, M_FILEDESC);
1518 bcopy(fdp->fd_ofiles, ntable, onfiles *
sizeof(*ntable));
1519 bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1520 otable = fdp->fd_ofiles;
1521 fdp->fd_ofileflags = nfileflags;
1522 fdp->fd_ofiles = ntable;
1529 fo = (
struct freetable *)&otable[onfiles];
1530 fdp0 = (
struct filedesc0 *)fdp;
1532 SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next);
1535 bcopy(fdp->fd_map, nmap,
NDSLOTS(onfiles) *
sizeof(*nmap));
1537 free(fdp->fd_map, M_FILEDESC);
1540 fdp->fd_nfiles = nnfiles;
1549 struct proc *p = td->td_proc;
1550 struct filedesc *fdp = p->p_fd;
1556 FILEDESC_XLOCK_ASSERT(fdp);
1558 if (fdp->fd_freefile > minfd)
1559 minfd = fdp->fd_freefile;
1573 if (fd < fdp->fd_nfiles)
1577 error =
racct_set(p, RACCT_NOFILE, min(fdp->fd_nfiles * 2, maxfd));
1590 (
"fd_first_free() returned non-free descriptor"));
1591 KASSERT(fdp->fd_ofiles[fd] == NULL,
1592 (
"free descriptor isn't"));
1593 fdp->fd_ofileflags[
fd] = 0;
1603 fdallocn(
struct thread *td,
int minfd,
int *fds,
int n)
1605 struct proc *p = td->td_proc;
1606 struct filedesc *fdp = p->p_fd;
1609 FILEDESC_XLOCK_ASSERT(fdp);
1614 for (i = 0; i < n; i++)
1615 if (
fdalloc(td, 0, &fds[i]) != 0)
1619 for (i--; i >= 0; i--)
1634 struct proc *p = td->td_proc;
1635 struct filedesc *fdp = td->td_proc->p_fd;
1639 FILEDESC_LOCK_ASSERT(fdp);
1647 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1649 last = min(fdp->fd_nfiles, lim);
1650 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1651 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1652 if (*fpp == NULL && --n <= 0)
1666 falloc(
struct thread *td,
struct file **resultfp,
int *resultfd,
int flags)
1675 error =
finstall(td, fp, &fd, flags);
1681 if (resultfp != NULL)
1686 if (resultfd != NULL)
1700 static struct timeval lastfail;
1703 KASSERT(resultfp != NULL, (
"%s: resultfp == NULL", __func__));
1709 printf(
"kern.maxfiles limit exceeded by uid %i, "
1710 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1715 fp = uma_zalloc(
file_zone, M_WAITOK | M_ZERO);
1716 refcount_init(&fp->f_count, 1);
1717 fp->f_cred =
crhold(td->td_ucred);
1731 struct filedesc *fdp = td->td_proc->p_fd;
1734 KASSERT(fd != NULL, (
"%s: fd == NULL", __func__));
1735 KASSERT(fp != NULL, (
"%s: fp == NULL", __func__));
1737 FILEDESC_XLOCK(fdp);
1738 if ((error =
fdalloc(td, 0, fd))) {
1739 FILEDESC_XUNLOCK(fdp);
1743 fdp->fd_ofiles[*
fd] = fp;
1744 if ((flags & O_CLOEXEC) != 0)
1745 fdp->fd_ofileflags[*
fd] |= UF_EXCLOSE;
1746 FILEDESC_XUNLOCK(fdp);
1757 struct filedesc0 *newfdp;
1759 newfdp =
malloc(
sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1760 FILEDESC_LOCK_INIT(&newfdp->
fd_fd);
1762 FILEDESC_XLOCK(fdp);
1763 newfdp->
fd_fd.fd_cdir = fdp->fd_cdir;
1764 if (newfdp->
fd_fd.fd_cdir)
1765 VREF(newfdp->
fd_fd.fd_cdir);
1766 newfdp->
fd_fd.fd_rdir = fdp->fd_rdir;
1767 if (newfdp->
fd_fd.fd_rdir)
1768 VREF(newfdp->
fd_fd.fd_rdir);
1769 newfdp->
fd_fd.fd_jdir = fdp->fd_jdir;
1770 if (newfdp->
fd_fd.fd_jdir)
1771 VREF(newfdp->
fd_fd.fd_jdir);
1772 FILEDESC_XUNLOCK(fdp);
1776 newfdp->
fd_fd.fd_refcnt = 1;
1777 newfdp->
fd_fd.fd_holdcnt = 1;
1778 newfdp->
fd_fd.fd_cmask = CMASK;
1779 newfdp->
fd_fd.fd_ofiles = newfdp->fd_dfiles;
1780 newfdp->
fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1782 newfdp->
fd_fd.fd_map = newfdp->fd_dmap;
1783 newfdp->
fd_fd.fd_lastfile = -1;
1784 return (&newfdp->
fd_fd);
1787 static struct filedesc *
1790 struct filedesc *fdp;
1803 struct filedesc0 *fdp0;
1808 i = --fdp->fd_holdcnt;
1813 FILEDESC_LOCK_DESTROY(fdp);
1814 fdp0 = (
struct filedesc0 *)fdp;
1815 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1816 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1819 free(fdp, M_FILEDESC);
1829 FILEDESC_XLOCK(fdp);
1831 FILEDESC_XUNLOCK(fdp);
1842 FILEDESC_XLOCK(p->p_fd);
1843 if (p->p_fd->fd_refcnt > 1) {
1844 struct filedesc *tmp;
1846 FILEDESC_XUNLOCK(p->p_fd);
1851 FILEDESC_XUNLOCK(p->p_fd);
1861 struct filedesc *newfdp;
1869 FILEDESC_SLOCK(fdp);
1870 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1871 FILEDESC_SUNLOCK(fdp);
1872 FILEDESC_XLOCK(newfdp);
1874 FILEDESC_XUNLOCK(newfdp);
1875 FILEDESC_SLOCK(fdp);
1878 newfdp->fd_freefile = -1;
1879 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1881 (fdp->fd_ofiles[i]->f_ops->fo_flags & DFLAG_PASSABLE) &&
1883 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1884 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1885 fhold(newfdp->fd_ofiles[i]);
1886 newfdp->fd_lastfile = i;
1888 if (newfdp->fd_freefile == -1)
1889 newfdp->fd_freefile = i;
1892 newfdp->fd_cmask = fdp->fd_cmask;
1893 FILEDESC_SUNLOCK(fdp);
1894 FILEDESC_XLOCK(newfdp);
1895 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1896 if (newfdp->fd_ofiles[i] != NULL)
1898 if (newfdp->fd_freefile == -1)
1899 newfdp->fd_freefile = i;
1900 FILEDESC_XUNLOCK(newfdp);
1910 struct filedesc *fdp;
1913 struct filedesc_to_leader *fdtol;
1915 struct vnode *cdir, *jdir, *rdir, *vp;
1919 fdp = td->td_proc->p_fd;
1924 PROC_LOCK(td->td_proc);
1925 racct_set(td->td_proc, RACCT_NOFILE, 0);
1926 PROC_UNLOCK(td->td_proc);
1930 fdtol = td->td_proc->p_fdtol;
1931 if (fdtol != NULL) {
1932 FILEDESC_XLOCK(fdp);
1933 KASSERT(fdtol->fdl_refcount > 0,
1934 (
"filedesc_to_refcount botch: fdl_refcount=%d",
1935 fdtol->fdl_refcount));
1936 if (fdtol->fdl_refcount == 1 &&
1937 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1938 for (i = 0, fpp = fdp->fd_ofiles;
1939 i <= fdp->fd_lastfile;
1942 (*fpp)->f_type != DTYPE_VNODE)
1946 FILEDESC_XUNLOCK(fdp);
1947 lf.l_whence = SEEK_SET;
1950 lf.l_type = F_UNLCK;
1952 locked = VFS_LOCK_GIANT(vp->v_mount);
1953 (void) VOP_ADVLOCK(vp,
1954 (caddr_t)td->td_proc->
1959 VFS_UNLOCK_GIANT(locked);
1960 FILEDESC_XLOCK(fdp);
1962 fpp = fdp->fd_ofiles + i;
1966 if (fdtol->fdl_refcount == 1) {
1967 if (fdp->fd_holdleaderscount > 0 &&
1968 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1973 fdp->fd_holdleaderswakeup = 1;
1974 sx_sleep(&fdp->fd_holdleaderscount,
1975 FILEDESC_LOCK(fdp), PLOCK,
"fdlhold", 0);
1978 if (fdtol->fdl_holdcount > 0) {
1983 fdtol->fdl_wakeup = 1;
1984 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1989 fdtol->fdl_refcount--;
1990 if (fdtol->fdl_refcount == 0 &&
1991 fdtol->fdl_holdcount == 0) {
1992 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1993 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1996 td->td_proc->p_fdtol = NULL;
1997 FILEDESC_XUNLOCK(fdp);
1999 free(fdtol, M_FILEDESC_TO_LEADER);
2001 FILEDESC_XLOCK(fdp);
2002 i = --fdp->fd_refcnt;
2003 FILEDESC_XUNLOCK(fdp);
2007 fpp = fdp->fd_ofiles;
2008 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
2010 FILEDESC_XLOCK(fdp);
2013 FILEDESC_XUNLOCK(fdp);
2017 FILEDESC_XLOCK(fdp);
2021 td->td_proc->p_fd = NULL;
2024 if (fdp->fd_nfiles >
NDFILE)
2025 free(fdp->fd_ofiles, M_FILEDESC);
2027 free(fdp->fd_map, M_FILEDESC);
2031 cdir = fdp->fd_cdir;
2032 fdp->fd_cdir = NULL;
2033 rdir = fdp->fd_rdir;
2034 fdp->fd_rdir = NULL;
2035 jdir = fdp->fd_jdir;
2036 fdp->fd_jdir = NULL;
2037 FILEDESC_XUNLOCK(fdp);
2040 locked = VFS_LOCK_GIANT(cdir->v_mount);
2042 VFS_UNLOCK_GIANT(locked);
2045 locked = VFS_LOCK_GIANT(rdir->v_mount);
2047 VFS_UNLOCK_GIANT(locked);
2050 locked = VFS_LOCK_GIANT(jdir->v_mount);
2052 VFS_UNLOCK_GIANT(locked);
2070 if (fp->f_type == DTYPE_VNODE) {
2071 struct vnode *vp = fp->f_vnode;
2073 if ((vp->v_vflag & VV_PROCDEP) != 0)
2085 struct filedesc *fdp;
2089 fdp = td->td_proc->p_fd;
2097 FILEDESC_XLOCK(fdp);
2098 for (i = 0; i <= fdp->fd_lastfile; i++) {
2101 if (fdp->fd_ofiles[i] &&
is_unsafe(fdp->fd_ofiles[i])) {
2109 fp = fdp->fd_ofiles[i];
2110 fdp->fd_ofiles[i] = NULL;
2111 fdp->fd_ofileflags[i] = 0;
2113 FILEDESC_XUNLOCK(fdp);
2115 FILEDESC_XLOCK(fdp);
2118 FILEDESC_XUNLOCK(fdp);
2129 fdclose(
struct filedesc *fdp,
struct file *fp,
int idx,
struct thread *td)
2132 FILEDESC_XLOCK(fdp);
2133 if (fdp->fd_ofiles[idx] == fp) {
2134 fdp->fd_ofiles[idx] = NULL;
2136 FILEDESC_XUNLOCK(fdp);
2139 FILEDESC_XUNLOCK(fdp);
2148 struct filedesc *fdp;
2152 fdp = td->td_proc->p_fd;
2156 FILEDESC_XLOCK(fdp);
2162 for (i = 0; i <= fdp->fd_lastfile; i++) {
2163 if (fdp->fd_ofiles[i] != NULL &&
2164 (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
2165 (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
2173 fp = fdp->fd_ofiles[i];
2174 fdp->fd_ofiles[i] = NULL;
2175 fdp->fd_ofileflags[i] = 0;
2177 if (fp->f_type == DTYPE_MQUEUE)
2179 FILEDESC_XUNLOCK(fdp);
2181 FILEDESC_XLOCK(fdp);
2184 FILEDESC_XUNLOCK(fdp);
2197 struct filedesc *fdp;
2198 register_t retval, save;
2199 int i, error, devnull;
2201 fdp = td->td_proc->p_fd;
2204 KASSERT(fdp->fd_refcnt == 1, (
"the fdtable should not be shared"));
2207 for (i = 0; i < 3; i++) {
2208 if (fdp->fd_ofiles[i] != NULL)
2211 save = td->td_retval[0];
2212 error =
kern_open(td,
"/dev/null", UIO_SYSSPACE,
2214 devnull = td->td_retval[0];
2215 td->td_retval[0] = save;
2218 KASSERT(devnull == i, (
"oof, we didn't get our fd"));
2241 struct filedesc_to_leader *fdtol;
2242 struct filedesc *fdp;
2243 struct file *fp_object;
2261 if ((fp_object->f_type == DTYPE_VNODE) && (td != NULL)) {
2264 vp = fp_object->f_vnode;
2265 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2266 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2267 lf.l_whence = SEEK_SET;
2270 lf.l_type = F_UNLCK;
2271 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2272 F_UNLCK, &lf, F_POSIX);
2274 fdtol = td->td_proc->p_fdtol;
2275 if (fdtol != NULL) {
2280 fdp = td->td_proc->p_fd;
2281 FILEDESC_XLOCK(fdp);
2282 for (fdtol = fdtol->fdl_next;
2283 fdtol != td->td_proc->p_fdtol;
2284 fdtol = fdtol->fdl_next) {
2285 if ((fdtol->fdl_leader->p_flag &
2288 fdtol->fdl_holdcount++;
2289 FILEDESC_XUNLOCK(fdp);
2290 lf.l_whence = SEEK_SET;
2293 lf.l_type = F_UNLCK;
2294 vp = fp_object->f_vnode;
2295 (void) VOP_ADVLOCK(vp,
2296 (caddr_t)fdtol->fdl_leader,
2297 F_UNLCK, &lf, F_POSIX);
2298 FILEDESC_XLOCK(fdp);
2299 fdtol->fdl_holdcount--;
2300 if (fdtol->fdl_holdcount == 0 &&
2301 fdtol->fdl_wakeup != 0) {
2302 fdtol->fdl_wakeup = 0;
2306 FILEDESC_XUNLOCK(fdp);
2308 VFS_UNLOCK_GIANT(vfslocked);
2310 return (fdrop(fp, td));
2326 atomic_store_rel_ptr((
volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2335 if (fd < 0 || fd >= fdp->fd_nfiles)
2346 fp = fdp->fd_ofiles[
fd];
2349 count = fp->f_count;
2356 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2358 if (fp == fdp->fd_ofiles[fd])
2360 fdrop(fp, curthread);
2382 #define FGET_GETCAP 0x00000001
2384 _fget(
struct thread *td,
int fd,
struct file **fpp,
int flags,
2385 cap_rights_t needrights, cap_rights_t *haverightsp, u_char *maxprotp,
2388 struct filedesc *fdp;
2391 struct file *fp_fromcap;
2396 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2409 if (haverightsp != NULL) {
2410 if (fp->f_type == DTYPE_CAPABILITY)
2411 *haverightsp = cap_rights(fp);
2413 *haverightsp = CAP_MASK_VALID;
2422 if (fp->f_type != DTYPE_CAPABILITY) {
2427 if (maxprotp == NULL)
2442 if (fp != fp_fromcap) {
2449 KASSERT(fp->f_type != DTYPE_CAPABILITY,
2450 (
"%s: saw capability", __func__));
2451 if (maxprotp != NULL)
2452 *maxprotp = VM_PROT_ALL;
2462 if ((fp->f_flag & flags) == 0)
2466 if ((fp->f_flag & (FREAD | FEXEC)) == 0 ||
2467 ((fp->f_flag & FWRITE) != 0))
2473 KASSERT(0, (
"wrong flags"));
2486 fget(
struct thread *td,
int fd, cap_rights_t rights,
struct file **fpp)
2489 return(
_fget(td, fd, fpp, 0, rights, NULL, NULL, 0));
2493 fget_mmap(
struct thread *td,
int fd, cap_rights_t rights, u_char *maxprotp,
2497 return (
_fget(td, fd, fpp, 0, rights, NULL, maxprotp, 0));
2501 fget_read(
struct thread *td,
int fd, cap_rights_t rights,
struct file **fpp)
2504 return(
_fget(td, fd, fpp, FREAD, rights, NULL, NULL, 0));
2508 fget_write(
struct thread *td,
int fd, cap_rights_t rights,
struct file **fpp)
2511 return (
_fget(td, fd, fpp, FWRITE, rights, NULL, NULL, 0));
2535 _fgetvp(
struct thread *td,
int fd,
int flags, cap_rights_t needrights,
2536 cap_rights_t *haverightsp,
struct vnode **vpp)
2542 if ((error =
_fget(td, fd, &fp, flags, needrights, haverightsp,
2545 if (fp->f_vnode == NULL) {
2557 fgetvp(
struct thread *td,
int fd, cap_rights_t rights,
struct vnode **vpp)
2560 return (
_fgetvp(td, fd, 0, rights, NULL, vpp));
2567 return (
_fgetvp(td, fd, 0, need, have, vpp));
2574 return (
_fgetvp(td, fd, FREAD, rights, NULL, vpp));
2581 return (
_fgetvp(td, fd, FEXEC, rights, NULL, vpp));
2586 fgetvp_write(
struct thread *td,
int fd, cap_rights_t rights,
2590 return (
_fgetvp(td, fd, FWRITE, rights, NULL, vpp));
2606 fgetsock(
struct thread *td,
int fd, cap_rights_t rights,
struct socket **spp,
2615 if ((error =
_fget(td, fd, &fp, 0, rights, NULL, NULL, 0)) != 0)
2617 if (fp->f_type != DTYPE_SOCKET) {
2622 *fflagp = fp->f_flag;
2644 CURVNET_SET(so->so_vnet);
2661 if (fp->f_count != 0)
2662 panic(
"fdrop: count %d", fp->f_count);
2664 error = fo_close(fp, td);
2667 free(fp->f_advice, M_FADVISE);
2679 #ifndef _SYS_SYSPROTO_H_
2695 if ((error =
fget(td, uap->
fd, CAP_FLOCK, &fp)) != 0)
2697 if (fp->f_type != DTYPE_VNODE) {
2699 return (EOPNOTSUPP);
2703 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2704 lf.l_whence = SEEK_SET;
2707 if (uap->
how & LOCK_UN) {
2708 lf.l_type = F_UNLCK;
2709 atomic_clear_int(&fp->f_flag, FHASLOCK);
2710 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2713 if (uap->
how & LOCK_EX)
2714 lf.l_type = F_WRLCK;
2715 else if (uap->
how & LOCK_SH)
2716 lf.l_type = F_RDLCK;
2721 atomic_set_int(&fp->f_flag, FHASLOCK);
2722 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2723 (uap->
how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2726 VFS_UNLOCK_GIANT(vfslocked);
2733 dupfdopen(
struct thread *td,
struct filedesc *fdp,
int indx,
int dfd,
int mode,
int error)
2743 FILEDESC_XLOCK(fdp);
2744 if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2745 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2746 FILEDESC_XUNLOCK(fdp);
2766 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2767 FILEDESC_XUNLOCK(fdp);
2770 fp = fdp->fd_ofiles[indx];
2771 fdp->fd_ofiles[indx] = wfp;
2772 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2776 FILEDESC_XUNLOCK(fdp);
2789 fp = fdp->fd_ofiles[indx];
2790 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2791 fdp->fd_ofiles[dfd] = NULL;
2792 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2793 fdp->fd_ofileflags[dfd] = 0;
2797 FILEDESC_XUNLOCK(fdp);
2808 FILEDESC_XUNLOCK(fdp);
2821 struct filedesc *fdp;
2830 FOREACH_PROC_IN_SYSTEM(p) {
2834 FILEDESC_XLOCK(fdp);
2835 if (fdp->fd_cdir == olddp) {
2837 fdp->fd_cdir = newdp;
2840 if (fdp->fd_rdir == olddp) {
2842 fdp->fd_rdir = newdp;
2845 if (fdp->fd_jdir == olddp) {
2847 fdp->fd_jdir = newdp;
2850 FILEDESC_XUNLOCK(fdp);
2860 if (
prison0.pr_root == olddp) {
2867 TAILQ_FOREACH(pr, &
allprison, pr_list) {
2868 mtx_lock(&pr->pr_mtx);
2869 if (pr->pr_root == olddp) {
2871 pr->pr_root = newdp;
2874 mtx_unlock(&pr->pr_mtx);
2881 struct filedesc_to_leader *
2884 struct filedesc_to_leader *fdtol;
2886 fdtol =
malloc(
sizeof(
struct filedesc_to_leader),
2887 M_FILEDESC_TO_LEADER,
2889 fdtol->fdl_refcount = 1;
2890 fdtol->fdl_holdcount = 0;
2891 fdtol->fdl_wakeup = 0;
2892 fdtol->fdl_leader = leader;
2894 FILEDESC_XLOCK(fdp);
2895 fdtol->fdl_next = old->fdl_next;
2896 fdtol->fdl_prev = old;
2897 old->fdl_next = fdtol;
2898 fdtol->fdl_next->fdl_prev = fdtol;
2899 FILEDESC_XUNLOCK(fdp);
2901 fdtol->fdl_next = fdtol;
2902 fdtol->fdl_prev = fdtol;
2914 struct filedesc *fdp;
2922 if (req->oldptr == NULL) {
2925 FOREACH_PROC_IN_SYSTEM(p) {
2926 if (p->p_state == PRS_NEW)
2932 if (fdp->fd_lastfile > 0)
2933 n += fdp->fd_lastfile;
2937 return (SYSCTL_OUT(req, 0, n *
sizeof(xf)));
2940 bzero(&xf,
sizeof(xf));
2941 xf.xf_size =
sizeof(xf);
2943 FOREACH_PROC_IN_SYSTEM(p) {
2945 if (p->p_state == PRS_NEW) {
2953 xf.xf_pid = p->p_pid;
2954 xf.xf_uid = p->p_ucred->cr_uid;
2959 FILEDESC_SLOCK(fdp);
2960 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2961 if ((fp = fdp->fd_ofiles[n]) == NULL)
2965 xf.xf_data = fp->f_data;
2966 xf.xf_vnode = fp->f_vnode;
2967 xf.xf_type = fp->f_type;
2968 xf.xf_count = fp->f_count;
2970 xf.xf_offset = foffset_get(fp);
2971 xf.xf_flag = fp->f_flag;
2972 error = SYSCTL_OUT(req, &xf,
sizeof(xf));
2976 FILEDESC_SUNLOCK(fdp);
2985 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2988 #ifdef KINFO_OFILE_SIZE
2989 CTASSERT(
sizeof(
struct kinfo_ofile) == KINFO_OFILE_SIZE);
2992 #ifdef COMPAT_FREEBSD7
2994 export_vnode_for_osysctl(
struct vnode *vp,
int type,
2995 struct kinfo_ofile *kif,
struct filedesc *fdp,
struct sysctl_req *req)
2998 char *fullpath, *freepath;
3001 bzero(kif,
sizeof(*kif));
3002 kif->kf_structsize =
sizeof(*kif);
3006 kif->kf_type = KF_TYPE_VNODE;
3008 if (vp->v_type != VDIR) {
3012 kif->kf_vnode_type = KF_VTYPE_VDIR;
3018 kif->kf_ref_count = -1;
3019 kif->kf_offset = -1;
3023 FILEDESC_SUNLOCK(fdp);
3025 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3027 VFS_UNLOCK_GIANT(vfslocked);
3028 strlcpy(kif->kf_path, fullpath,
sizeof(kif->kf_path));
3029 if (freepath != NULL)
3030 free(freepath, M_TEMP);
3031 error = SYSCTL_OUT(req, kif,
sizeof(*kif));
3032 FILEDESC_SLOCK(fdp);
3040 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
3042 char *fullpath, *freepath;
3043 struct kinfo_ofile *kif;
3044 struct filedesc *fdp;
3045 int error, i, *
name;
3046 struct shmfd *shmfd;
3056 error =
pget((pid_t)name[0], PGET_CANDEBUG, &p);
3063 kif =
malloc(
sizeof(*kif), M_TEMP, M_WAITOK);
3064 FILEDESC_SLOCK(fdp);
3065 if (fdp->fd_cdir != NULL)
3066 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
3068 if (fdp->fd_rdir != NULL)
3069 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
3071 if (fdp->fd_jdir != NULL)
3072 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
3074 for (i = 0; i < fdp->fd_nfiles; i++) {
3075 if ((fp = fdp->fd_ofiles[i]) == NULL)
3077 bzero(kif,
sizeof(*kif));
3078 kif->kf_structsize =
sizeof(*kif);
3093 if (fp->f_type == DTYPE_CAPABILITY) {
3094 kif->kf_flags |= KF_FLAG_CAPABILITY;
3098 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3099 (
"sysctl_kern_proc_ofiledesc: saw capability"));
3101 switch (fp->f_type) {
3103 kif->kf_type = KF_TYPE_VNODE;
3108 kif->kf_type = KF_TYPE_SOCKET;
3113 kif->kf_type = KF_TYPE_PIPE;
3117 kif->kf_type = KF_TYPE_FIFO;
3122 kif->kf_type = KF_TYPE_KQUEUE;
3126 kif->kf_type = KF_TYPE_CRYPTO;
3130 kif->kf_type = KF_TYPE_MQUEUE;
3134 kif->kf_type = KF_TYPE_SHM;
3139 kif->kf_type = KF_TYPE_SEM;
3144 kif->kf_type = KF_TYPE_PTS;
3149 case DTYPE_PROCDESC:
3150 kif->kf_type = KF_TYPE_PROCDESC;
3155 kif->kf_type = KF_TYPE_UNKNOWN;
3158 kif->kf_ref_count = fp->f_count;
3159 if (fp->f_flag & FREAD)
3160 kif->kf_flags |= KF_FLAG_READ;
3161 if (fp->f_flag & FWRITE)
3162 kif->kf_flags |= KF_FLAG_WRITE;
3163 if (fp->f_flag & FAPPEND)
3164 kif->kf_flags |= KF_FLAG_APPEND;
3165 if (fp->f_flag & FASYNC)
3166 kif->kf_flags |= KF_FLAG_ASYNC;
3167 if (fp->f_flag & FFSYNC)
3168 kif->kf_flags |= KF_FLAG_FSYNC;
3169 if (fp->f_flag & FNONBLOCK)
3170 kif->kf_flags |= KF_FLAG_NONBLOCK;
3171 if (fp->f_flag & O_DIRECT)
3172 kif->kf_flags |= KF_FLAG_DIRECT;
3173 if (fp->f_flag & FHASLOCK)
3174 kif->kf_flags |= KF_FLAG_HASLOCK;
3175 kif->kf_offset = foffset_get(fp);
3178 switch (vp->v_type) {
3180 kif->kf_vnode_type = KF_VTYPE_VNON;
3183 kif->kf_vnode_type = KF_VTYPE_VREG;
3186 kif->kf_vnode_type = KF_VTYPE_VDIR;
3189 kif->kf_vnode_type = KF_VTYPE_VBLK;
3192 kif->kf_vnode_type = KF_VTYPE_VCHR;
3195 kif->kf_vnode_type = KF_VTYPE_VLNK;
3198 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3201 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3204 kif->kf_vnode_type = KF_VTYPE_VBAD;
3207 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3217 FILEDESC_SUNLOCK(fdp);
3219 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3221 VFS_UNLOCK_GIANT(vfslocked);
3222 strlcpy(kif->kf_path, fullpath,
3223 sizeof(kif->kf_path));
3224 if (freepath != NULL)
3225 free(freepath, M_TEMP);
3226 FILEDESC_SLOCK(fdp);
3229 struct sockaddr *sa;
3231 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3232 == 0 && sa->sa_len <=
sizeof(kif->kf_sa_local)) {
3233 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3236 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3237 == 0 && sa->sa_len <=
sizeof(kif->kf_sa_peer)) {
3238 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3241 kif->kf_sock_domain =
3242 so->so_proto->pr_domain->dom_family;
3243 kif->kf_sock_type = so->so_type;
3244 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3247 strlcpy(kif->kf_path, tty_devname(tp),
3248 sizeof(kif->kf_path));
3251 shm_path(shmfd, kif->kf_path,
sizeof(kif->kf_path));
3253 ksem_info(ks, kif->kf_path,
sizeof(kif->kf_path), NULL);
3254 error = SYSCTL_OUT(req, kif,
sizeof(*kif));
3258 FILEDESC_SUNLOCK(fdp);
3264 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3265 sysctl_kern_proc_ofiledesc,
"Process ofiledesc entries");
3268 #ifdef KINFO_FILE_SIZE
3269 CTASSERT(
sizeof(
struct kinfo_file) == KINFO_FILE_SIZE);
3276 struct kinfo_file kif;
3281 int64_t offset,
int fd_is_cap, cap_rights_t fd_cap_rights,
3287 } fflags_table[] = {
3288 { FAPPEND, KF_FLAG_APPEND },
3289 { FASYNC, KF_FLAG_ASYNC },
3290 { FFSYNC, KF_FLAG_FSYNC },
3291 { FHASLOCK, KF_FLAG_HASLOCK },
3292 { FNONBLOCK, KF_FLAG_NONBLOCK },
3293 { FREAD, KF_FLAG_READ },
3294 { FWRITE, KF_FLAG_WRITE },
3295 { O_CREAT, KF_FLAG_CREAT },
3296 { O_DIRECT, KF_FLAG_DIRECT },
3297 { O_EXCL, KF_FLAG_EXCL },
3298 { O_EXEC, KF_FLAG_EXEC },
3299 { O_EXLOCK, KF_FLAG_EXLOCK },
3300 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3301 { O_SHLOCK, KF_FLAG_SHLOCK },
3302 { O_TRUNC, KF_FLAG_TRUNC }
3304 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3305 struct kinfo_file *kif;
3307 int error, locked, vfslocked;
3313 bzero(kif,
sizeof(*kif));
3314 locked = efbuf->
fdp != NULL;
3319 FILEDESC_SUNLOCK(efbuf->
fdp);
3322 vp = (
struct vnode *)data;
3324 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3326 VFS_UNLOCK_GIANT(vfslocked);
3328 case KF_TYPE_SOCKET:
3337 case KF_TYPE_PROCDESC:
3350 kif->kf_status |= KF_ATTR_VALID;
3356 if (fflags & fflags_table[i].fflag)
3357 kif->kf_flags |= fflags_table[i].kf_fflag;
3359 kif->kf_flags |= KF_FLAG_CAPABILITY;
3361 kif->kf_cap_rights = fd_cap_rights;
3363 kif->kf_type =
type;
3364 kif->kf_ref_count = refcnt;
3365 kif->kf_offset = offset;
3367 kif->kf_structsize = offsetof(
struct kinfo_file, kf_path) +
3368 strlen(kif->kf_path) + 1;
3369 kif->kf_structsize = roundup(kif->kf_structsize,
sizeof(uint64_t));
3371 if (efbuf->
remainder < kif->kf_structsize) {
3374 if (efbuf->
fdp != NULL && !locked)
3375 FILEDESC_SLOCK(efbuf->
fdp);
3381 FILEDESC_SUNLOCK(efbuf->
fdp);
3382 error =
sbuf_bcat(efbuf->
sb, kif, kif->kf_structsize);
3383 if (efbuf->
fdp != NULL)
3384 FILEDESC_SLOCK(efbuf->
fdp);
3397 struct filedesc *fdp;
3399 struct vnode *cttyvp, *textvp, *tracevp;
3403 int fd_is_cap,
type, refcnt, fflags;
3404 cap_rights_t fd_cap_rights;
3406 PROC_LOCK_ASSERT(p, MA_OWNED);
3409 tracevp = p->p_tracevp;
3410 if (tracevp != NULL)
3413 textvp = p->p_textvp;
3418 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3419 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3425 efbuf =
malloc(
sizeof(*efbuf), M_TEMP, M_WAITOK);
3429 if (tracevp != NULL)
3431 FREAD | FWRITE, -1, -1, 0, 0, efbuf);
3434 FREAD, -1, -1, 0, 0, efbuf);
3437 FREAD | FWRITE, -1, -1, 0, 0, efbuf);
3442 FILEDESC_SLOCK(fdp);
3444 if (fdp->fd_cdir != NULL) {
3446 data = fdp->fd_cdir;
3448 FREAD, -1, -1, 0, 0, efbuf);
3451 if (fdp->fd_rdir != NULL) {
3453 data = fdp->fd_rdir;
3455 FREAD, -1, -1, 0, 0, efbuf);
3458 if (fdp->fd_jdir != NULL) {
3460 data = fdp->fd_jdir;
3462 FREAD, -1, -1, 0, 0, efbuf);
3464 for (i = 0; i < fdp->fd_nfiles; i++) {
3465 if ((fp = fdp->fd_ofiles[i]) == NULL)
3477 if (fp->f_type == DTYPE_CAPABILITY) {
3479 fd_cap_rights = cap_rights(fp);
3483 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3484 (
"sysctl_kern_proc_filedesc: saw capability"));
3486 switch (fp->f_type) {
3488 type = KF_TYPE_VNODE;
3494 type = KF_TYPE_SOCKET;
3499 type = KF_TYPE_PIPE;
3504 type = KF_TYPE_FIFO;
3510 type = KF_TYPE_KQUEUE;
3514 type = KF_TYPE_CRYPTO;
3518 type = KF_TYPE_MQUEUE;
3537 case DTYPE_PROCDESC:
3538 type = KF_TYPE_PROCDESC;
3544 type = KF_TYPE_UNKNOWN;
3547 refcnt = fp->f_count;
3548 fflags = fp->f_flag;
3549 offset = foffset_get(fp);
3558 offset, fd_is_cap, fd_cap_rights, efbuf);
3562 FILEDESC_SUNLOCK(fdp);
3565 free(efbuf, M_TEMP);
3569 #define FILEDESC_SBUF_SIZE (sizeof(struct kinfo_file) * 5)
3580 int error, error2, *
name;
3585 error =
pget((pid_t)name[0], PGET_CANDEBUG, &p);
3590 maxlen = req->oldptr != NULL ? req->oldlen : -1;
3594 return (error != 0 ? error : error2);
3603 } vtypes_table[] = {
3604 { VBAD, KF_VTYPE_VBAD },
3605 { VBLK, KF_VTYPE_VBLK },
3606 { VCHR, KF_VTYPE_VCHR },
3607 { VDIR, KF_VTYPE_VDIR },
3608 { VFIFO, KF_VTYPE_VFIFO },
3609 { VLNK, KF_VTYPE_VLNK },
3610 { VNON, KF_VTYPE_VNON },
3611 { VREG, KF_VTYPE_VREG },
3612 { VSOCK, KF_VTYPE_VSOCK }
3614 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3621 if (vtypes_table[i].vtype == vtype)
3624 return (vtypes_table[i].kf_vtype);
3626 return (KF_VTYPE_UNKNOWN);
3633 char *fullpath, *freepath;
3634 int error, vfslocked;
3641 error =
vn_fullpath(curthread, vp, &fullpath, &freepath);
3643 strlcpy(kif->kf_path, fullpath,
sizeof(kif->kf_path));
3645 if (freepath != NULL)
3646 free(freepath, M_TEMP);
3651 va.va_fsid = VNOVAL;
3653 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3654 vn_lock(vp, LK_SHARED | LK_RETRY);
3655 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3657 VFS_UNLOCK_GIANT(vfslocked);
3660 if (va.va_fsid != VNOVAL)
3661 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3663 kif->kf_un.kf_file.kf_file_fsid =
3664 vp->v_mount->mnt_stat.f_fsid.val[0];
3665 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3666 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3667 kif->kf_un.kf_file.kf_file_size = va.va_size;
3668 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3675 struct sockaddr *sa;
3676 struct inpcb *inpcb;
3677 struct unpcb *unpcb;
3682 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3683 kif->kf_sock_type = so->so_type;
3684 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3685 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3686 switch(kif->kf_sock_domain) {
3689 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3690 if (so->so_pcb != NULL) {
3691 inpcb = (
struct inpcb *)(so->so_pcb);
3692 kif->kf_un.kf_sock.kf_sock_inpcb =
3693 (uintptr_t)inpcb->inp_ppcb;
3698 if (so->so_pcb != NULL) {
3699 unpcb = (
struct unpcb *)(so->so_pcb);
3700 if (unpcb->unp_conn) {
3701 kif->kf_un.kf_sock.kf_sock_unpconn =
3702 (uintptr_t)unpcb->unp_conn;
3703 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3704 so->so_rcv.sb_state;
3705 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3706 so->so_snd.sb_state;
3711 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3712 if (error == 0 && sa->sa_len <=
sizeof(kif->kf_sa_local)) {
3713 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3716 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3717 if (error == 0 && sa->sa_len <=
sizeof(kif->kf_sa_peer)) {
3718 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3721 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3722 sizeof(kif->kf_path));
3732 kif->kf_un.kf_pts.kf_pts_dev =
tty_udev(tp);
3733 strlcpy(kif->kf_path, tty_devname(tp),
sizeof(kif->kf_path));
3743 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3744 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3745 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3755 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3766 if (fp->f_data == NULL)
3768 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3772 ksem_info(fp->f_data, kif->kf_path,
sizeof(kif->kf_path),
3773 &kif->kf_un.kf_sem.kf_sem_value);
3774 kif->kf_un.kf_sem.kf_sem_mode = sb.st_mode;
3785 if (fp->f_data == NULL)
3787 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3789 shm_path(fp->f_data, kif->kf_path,
sizeof(kif->kf_path));
3790 kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
3791 kif->kf_un.kf_file.kf_file_size = sb.st_size;
3795 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3804 file_type_to_name(
short type)
3838 static struct proc *
3839 file_to_first_proc(
struct file *fp)
3841 struct filedesc *fdp;
3845 FOREACH_PROC_IN_SYSTEM(p) {
3846 if (p->p_state == PRS_NEW)
3851 for (n = 0; n < fdp->fd_nfiles; n++) {
3852 if (fp == fdp->fd_ofiles[n])
3860 db_print_file(
struct file *fp,
int header)
3865 db_printf(
"%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3866 "File",
"Type",
"Data",
"Flag",
"GCFl",
"Count",
3867 "MCount",
"Vnode",
"FPID",
"FCmd");
3868 p = file_to_first_proc(fp);
3869 db_printf(
"%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3870 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3871 0, fp->f_count, 0, fp->f_vnode,
3872 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm :
"-");
3875 DB_SHOW_COMMAND(file, db_show_file)
3880 db_printf(
"usage: show file <addr>\n");
3883 fp = (
struct file *)addr;
3884 db_print_file(fp, 1);
3887 DB_SHOW_COMMAND(files, db_show_files)
3889 struct filedesc *fdp;
3896 FOREACH_PROC_IN_SYSTEM(p) {
3897 if (p->p_state == PRS_NEW)
3899 if ((fdp = p->p_fd) == NULL)
3901 for (n = 0; n < fdp->fd_nfiles; ++n) {
3902 if ((fp = fdp->fd_ofiles[n]) == NULL)
3904 db_print_file(fp, header);
3915 &
maxfiles, 0,
"Maximum number of files");
3918 __DEVOLATILE(
int *, &
openfiles), 0,
"System-wide number of open files");
3925 file_zone = uma_zcreate(
"Files",
sizeof(
struct file), NULL, NULL,
3926 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3936 int flags,
struct thread *td)
3951 badfo_ioctl(
struct file *fp, u_long com,
void *data,
struct ucred *active_cred,
3959 badfo_poll(
struct file *fp,
int events,
struct ucred *active_cred,
3974 badfo_stat(
struct file *fp,
struct stat *sb,
struct ucred *active_cred,
3997 badfo_chown(
struct file *fp, uid_t uid, gid_t gid,
struct ucred *active_cred,
4026 invfo_chown(
struct file *fp, uid_t uid, gid_t gid,
struct ucred *active_cred,
4059 td->td_dupfd = dev2unit(dev);
4064 .d_version = D_VERSION,
4075 UID_ROOT, GID_WHEEL, 0666,
"fd/0");
4078 UID_ROOT, GID_WHEEL, 0666,
"fd/1");
4081 UID_ROOT, GID_WHEEL, 0666,
"fd/2");
static int fill_sem_info(struct file *fp, struct kinfo_file *kif)
int fgetvp_rights(struct thread *td, int fd, cap_rights_t need, cap_rights_t *have, struct vnode **vpp)
static int badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
pid_t fgetown(struct sigio **sigiop)
int fsetown(pid_t pgid, struct sigio **sigiop)
int kern_close(struct thread *td, int fd)
rlim_t lim_cur(struct proc *p, int which)
int sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
static void fdunused(struct filedesc *fdp, int fd)
int invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
struct file * fget_unlocked(struct filedesc *fdp, int fd)
static int sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
int ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
#define FILEDESC_SBUF_SIZE
static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD, sysctl_kern_proc_filedesc,"Process filedesc entries")
struct cdev * make_dev_alias(struct cdev *pdev, const char *fmt,...)
int racct_set(struct proc *p, int resource, uint64_t amount)
void knote_fdclose(struct thread *td, int fd)
void mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
int sys_fstat(struct thread *td, struct fstat_args *uap)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
void setugidsafety(struct thread *td)
int _fdrop(struct file *fp, struct thread *td)
struct filedesc_to_leader * filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
static int fd_last_used(struct filedesc *, int, int)
static void fdgrowtable(struct filedesc *, int)
int kern_fstat(struct thread *td, int fd, struct stat *sbp)
static MALLOC_DEFINE(M_FILEDESC,"filedesc","Open file descriptor table")
static int do_dup(struct thread *td, int flags, int old, int new, register_t *retval)
int cap_funwrap(struct file *fp_cap, cap_rights_t rights, struct file **fpp)
void(* mq_fdclose)(struct thread *td, int fd, struct file *fp)
int sys_dup(struct thread *td, struct dup_args *uap)
static int fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
CTASSERT(MAXSHELLCMDLEN >=MAXINTERP+3)
void panic(const char *fmt,...)
static int badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
void funsetownlst(struct sigiolst *sigiolst)
void knote(struct knlist *list, long hint, int lockflags)
int fgetcap(struct thread *td, int fd, struct file **fpp)
int vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
int sys_closefrom(struct thread *td, struct closefrom_args *uap)
int sys_dup2(struct thread *td, struct dup2_args *uap)
int falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
int fdalloc(struct thread *td, int minfd, int *result)
int vrefcnt(struct vnode *vp)
SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0, sysctl_kern_file,"S,xfile","Entire file table")
static int badfo_close(struct file *fp, struct thread *td)
static int badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
struct cdev * make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
static uma_zone_t file_zone
int fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
int sys_flock(struct thread *td, struct flock_args *uap)
int invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, struct thread *td)
void funsetown(struct sigio **sigiop)
void(* ksem_info)(struct ksem *ks, char *path, size_t size, uint32_t *value)
int priv_check(struct thread *td, int priv)
struct proc * pfind(pid_t pid)
void vref(struct vnode *vp)
int kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
int fgetvp_exec(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
void fdunshare(struct proc *p, struct thread *td)
void fdcloseexec(struct thread *td)
int fdavail(struct thread *td, int n)
int vntype_to_kinfo(int vtype)
static int badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
MALLOC_DECLARE(M_FADVISE)
static void fildesc_drvinit(void *unused)
dev_t tty_udev(struct tty *tp)
int kern_proc_filedesc_out(struct proc *p, struct sbuf *sb, ssize_t maxlen)
static int is_unsafe(struct file *fp)
struct prisonlist allprison
struct filedesc * fdinit(struct filedesc *fdp)
static __inline int _fget(struct thread *td, int fd, struct file **fpp, int flags, cap_rights_t needrights, cap_rights_t *haverightsp, u_char *maxprotp, int fget_flags)
SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,&maxfilesperproc, 0,"Maximum files allowed open per process")
void fputsock(struct socket *so)
int finstall(struct thread *td, struct file *fp, int *fd, int flags)
int kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags, int mode)
static int fdisused(struct filedesc *fdp, int fd)
int closef(struct file *fp, struct thread *td)
static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
int falloc_noinstall(struct thread *td, struct file **resultfp)
void crfree(struct ucred *cr)
static void filelistinit(void *dummy)
int sys_fcntl(struct thread *td, struct fcntl_args *uap)
static int badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
struct pgrp * pgfind(pid_t pgid)
static int badfo_kqfilter(struct file *fp, struct knote *kn)
int fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp, struct file **fpp)
void fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
int fdcheckstd(struct thread *td)
static int export_fd_to_sb(void *data, int type, int fd, int fflags, int refcnt, int64_t offset, int fd_is_cap, cap_rights_t fd_cap_rights, struct export_fd_buf *efbuf)
static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
static int fdopen(struct cdev *dev, int mode, int type, struct thread *td)
struct ucred * crhold(struct ucred *cr)
static void fddrop(struct filedesc *fdp)
int cap_funwrap_mmap(struct file *fp_cap, cap_rights_t rights, u_char *maxprotp, struct file **fpp)
static void fdused(struct filedesc *fdp, int fd)
void free(void *addr, struct malloc_type *mtp)
static int fill_pts_info(struct tty *tp, struct kinfo_file *kif)
int printf(const char *fmt,...)
void fdfree(struct thread *td)
int sys_close(struct thread *td, struct close_args *uap)
void sbuf_delete(struct sbuf *s)
int fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp, u_int *fflagp)
static struct file * fdtofp(int fd, struct filedesc *fdp)
void cvtnstat(struct stat *sb, struct nstat *nsb)
static int fill_shm_info(struct file *fp, struct kinfo_file *kif)
int sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
void finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
struct filedesc * fdcopy(struct filedesc *fdp)
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
void shm_path(struct shmfd *shmfd, char *path, size_t size)
void vrele(struct vnode *vp)
int sys_nfstat(struct thread *td, struct nfstat_args *uap)
int sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
int fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
int sbuf_finish(struct sbuf *s)
int pget(pid_t pid, int flags, struct proc **pp)
static int badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, struct thread *td)
int fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
static int sysctl_kern_file(SYSCTL_HANDLER_ARGS)
SLIST_HEAD(et_eventtimers_list, eventtimer)
static __inline int _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights, cap_rights_t *haverightsp, struct vnode **vpp)
int sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
int fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
int fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
static int fill_socket_info(struct socket *so, struct kinfo_file *kif)
static int fdunwrap(int fd, cap_rights_t rights, struct filedesc *fdp, struct file **fpp)
static struct cdevsw fildesc_cdevsw
static int badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
uint64_t racct_get_limit(struct proc *p, int resource)
int fdallocn(struct thread *td, int minfd, int *fds, int n)
static struct pollrec pr[POLL_LIST_LEN]
int p_cansee(struct thread *td, struct proc *p)
static struct mtx fdesc_mtx
int dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
static int fd_first_free(struct filedesc *, int, int)
struct filedesc * fdshare(struct filedesc *fdp)
struct sbuf * sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, struct sysctl_req *req)
static struct filedesc * fdhold(struct proc *p)
static int getmaxfd(struct proc *p)
struct fileops badfileops