fd: implement kern.proc.nfds sysctl

Intended purpose is to provide an equivalent of OpenBSD's getdtablecount
syscall for the compat library..
This commit is contained in:
Mateusz Guzik 2015-11-07 00:18:14 +00:00
parent 9a54871fd8
commit b577e693aa
2 changed files with 25 additions and 0 deletions

View File

@ -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.
*/

View File

@ -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