From b577e693aa898b1a01038c0c011e5ddfcff9b97f Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sat, 7 Nov 2015 00:18:14 +0000 Subject: [PATCH] fd: implement kern.proc.nfds sysctl Intended purpose is to provide an equivalent of OpenBSD's getdtablecount syscall for the compat library.. --- sys/kern/kern_descrip.c | 24 ++++++++++++++++++++++++ sys/sys/sysctl.h | 1 + 2 files changed, 25 insertions(+) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 23b7d5489e53..a74d27f62040 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -3157,6 +3157,30 @@ filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, s return (fdtol); } +static int +sysctl_kern_proc_nfds(SYSCTL_HANDLER_ARGS) +{ + struct filedesc *fdp; + int i, count, slots; + + if (*(int *)arg1 != 0) + return (EINVAL); + + fdp = curproc->p_fd; + count = 0; + FILEDESC_SLOCK(fdp); + slots = NDSLOTS(fdp->fd_lastfile + 1); + for (i = 0; i < slots; i++) + count += bitcountl(fdp->fd_map[i]); + FILEDESC_SUNLOCK(fdp); + + return (SYSCTL_OUT(req, &count, sizeof(count))); +} + +static SYSCTL_NODE(_kern_proc, KERN_PROC_NFDS, nfds, + CTLFLAG_RD|CTLFLAG_MPSAFE, sysctl_kern_proc_nfds, + "Number of open file descriptors"); + /* * Get file structures globally. */ diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index 4e6c556736d6..206bfcf9483b 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -703,6 +703,7 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry); #define KERN_PROC_OSREL 40 /* osreldate for process binary */ #define KERN_PROC_SIGTRAMP 41 /* signal trampoline location */ #define KERN_PROC_CWD 42 /* process current working directory */ +#define KERN_PROC_NFDS 43 /* number of open file descriptors */ /* * KERN_IPC identifiers