sysctl: add KERN_PROC_RLIMIT_USAGE

(cherry picked from commit c85d3064c4)
This commit is contained in:
Konstantin Belousov 2024-09-22 20:01:56 +03:00
parent a23e9b154d
commit d9aa256201
2 changed files with 51 additions and 0 deletions

View file

@ -1744,3 +1744,53 @@ chgpipecnt(struct uidinfo *uip, int diff, rlim_t max)
return (chglimit(uip, &uip->ui_pipecnt, diff, max, "pipecnt"));
}
static int
sysctl_kern_proc_rlimit_usage(SYSCTL_HANDLER_ARGS)
{
rlim_t resval[RLIM_NLIMITS];
struct proc *p;
size_t len;
int error, *name, i;
name = (int *)arg1;
if ((u_int)arg2 != 1 && (u_int)arg2 != 2)
return (EINVAL);
if (req->newptr != NULL)
return (EINVAL);
error = pget((pid_t)name[0], PGET_WANTREAD, &p);
if (error != 0)
return (error);
if ((u_int)arg2 == 1) {
len = sizeof(resval);
memset(resval, 0, sizeof(resval));
for (i = 0; i < RLIM_NLIMITS; i++) {
error = getrlimitusage_one(p, (unsigned)i, 0,
&resval[i]);
if (error == ENXIO) {
resval[i] = -1;
error = 0;
} else if (error != 0) {
break;
}
}
} else {
len = sizeof(resval[0]);
error = getrlimitusage_one(p, (unsigned)name[1], 0,
&resval[0]);
if (error == ENXIO) {
resval[0] = -1;
error = 0;
}
}
if (error == 0)
error = SYSCTL_OUT(req, resval, len);
PRELE(p);
return (error);
}
static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT_USAGE, rlimit_usage,
CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
sysctl_kern_proc_rlimit_usage,
"Process limited resources usage info");

View file

@ -1042,6 +1042,7 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
#define KERN_PROC_NFDS 43 /* number of open file descriptors */
#define KERN_PROC_SIGFASTBLK 44 /* address of fastsigblk magic word */
#define KERN_PROC_VM_LAYOUT 45 /* virtual address space layout info */
#define KERN_PROC_RLIMIT_USAGE 46 /* array of rlim_t */
/*
* KERN_IPC identifiers