Add proc_nfiles(9)

(cherry picked from commit 9c3e516ad08145ad47248633b54fd1b7fc0ef981)
This commit is contained in:
Konstantin Belousov 2024-09-20 19:28:23 +03:00
parent fc9070bf1d
commit e05087ee1c
2 changed files with 36 additions and 12 deletions

View file

@ -4331,13 +4331,43 @@ filedesc_to_leader_share(struct filedesc_to_leader *fdtol, struct filedesc *fdp)
}
static int
sysctl_kern_proc_nfds(SYSCTL_HANDLER_ARGS)
filedesc_nfiles(struct filedesc *fdp)
{
NDSLOTTYPE *map;
struct filedesc *fdp;
u_int namelen;
int count, off, minoff;
if (fdp == NULL)
return (0);
count = 0;
FILEDESC_SLOCK(fdp);
map = fdp->fd_map;
off = NDSLOT(fdp->fd_nfiles - 1);
for (minoff = NDSLOT(0); off >= minoff; --off)
count += bitcountl(map[off]);
FILEDESC_SUNLOCK(fdp);
return (count);
}
int
proc_nfiles(struct proc *p)
{
struct filedesc *fdp;
int res;
PROC_LOCK(p);
fdp = fdhold(p);
PROC_UNLOCK(p);
res = filedesc_nfiles(fdp);
fddrop(fdp);
return (res);
}
static int
sysctl_kern_proc_nfds(SYSCTL_HANDLER_ARGS)
{
u_int namelen;
int count;
namelen = arg2;
if (namelen != 1)
return (EINVAL);
@ -4345,15 +4375,7 @@ sysctl_kern_proc_nfds(SYSCTL_HANDLER_ARGS)
if (*(int *)arg1 != 0)
return (EINVAL);
fdp = curproc->p_fd;
count = 0;
FILEDESC_SLOCK(fdp);
map = fdp->fd_map;
off = NDSLOT(fdp->fd_nfiles - 1);
for (minoff = NDSLOT(0); off >= minoff; --off)
count += bitcountl(map[off]);
FILEDESC_SUNLOCK(fdp);
count = filedesc_nfiles(curproc->p_fd);
return (SYSCTL_OUT(req, &count, sizeof(count)));
}

View file

@ -337,6 +337,8 @@ fd_modified(struct filedesc *fdp, int fd, seqc_t seqc)
}
#endif
int proc_nfiles(struct proc *p);
/* cdir/rdir/jdir manipulation functions. */
struct pwddesc *pdcopy(struct pwddesc *pdp);
void pdescfree(struct thread *td);