From 9e7d0583513142cff5bf65db5b22f89f56030600 Mon Sep 17 00:00:00 2001 From: Mikolaj Golub Date: Thu, 24 Nov 2011 20:43:37 +0000 Subject: [PATCH] Add sysctl to get process resource limits. Reviewed by: kib MFC after: 2 weeks --- sys/kern/kern_proc.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ sys/sys/sysctl.h | 1 + 2 files changed, 47 insertions(+) diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 6d1430cc041d..35f13b9f6fa6 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -2389,6 +2390,48 @@ sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) return (error); } +/* + * This sysctl allows a process to retrieve the resource limits for + * another process. + */ +static int +sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS) +{ + int *name = (int*) arg1; + u_int namelen = arg2; + struct plimit *limp; + struct proc *p; + int error = 0; + + if (namelen != 1) + return (EINVAL); + + p = pfind((pid_t)name[0]); + if (p == NULL) + return (ESRCH); + + if ((error = p_cansee(curthread, p)) != 0) { + PROC_UNLOCK(p); + return (error); + } + + /* + * Check the request size. We alow sizes smaller rlimit array for + * backward binary compatibility: the number of resource limits may + * grow. + */ + if (sizeof(limp->pl_rlimit) < req->oldlen) { + PROC_UNLOCK(p); + return (EINVAL); + } + + limp = lim_hold(p->p_limit); + PROC_UNLOCK(p); + error = SYSCTL_OUT(req, limp->pl_rlimit, req->oldlen); + lim_free(limp); + return (error); +} + SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| @@ -2484,3 +2527,6 @@ static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); + +static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RD | + CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit, "Process resource limits"); diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index 99ea342b80b5..db08a3a7802d 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -561,6 +561,7 @@ SYSCTL_ALLOWED_TYPES(UINT64, uint64_t *a; unsigned long long *b; ); #define KERN_PROC_GROUPS 34 /* process groups */ #define KERN_PROC_ENV 35 /* get environment */ #define KERN_PROC_AUXV 36 /* get ELF auxiliary vector */ +#define KERN_PROC_RLIMIT 37 /* process resource limits */ /* * KERN_IPC identifiers