summaryrefslogtreecommitdiffstats
path: root/lib/libprocstat/libprocstat.c
diff options
context:
space:
mode:
authortrociny <trociny@FreeBSD.org>2013-04-20 07:59:44 +0000
committertrociny <trociny@FreeBSD.org>2013-04-20 07:59:44 +0000
commit398b48380ff7b2ea5c997d18f7395b2aa9e36b06 (patch)
tree1f8660d19df0d3b65eecb0ba0022ab0d31cea2b5 /lib/libprocstat/libprocstat.c
parentaa81fb93633764124c74da7a38b7d2b9bdfda651 (diff)
downloadFreeBSD-src-398b48380ff7b2ea5c997d18f7395b2aa9e36b06.zip
FreeBSD-src-398b48380ff7b2ea5c997d18f7395b2aa9e36b06.tar.gz
Add procstat_getrlimit function to retrieve a process resource limits info.
MFC after: 1 month
Diffstat (limited to 'lib/libprocstat/libprocstat.c')
-rw-r--r--lib/libprocstat/libprocstat.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/libprocstat/libprocstat.c b/lib/libprocstat/libprocstat.c
index 1ee0f97..04c1058 100644
--- a/lib/libprocstat/libprocstat.c
+++ b/lib/libprocstat/libprocstat.c
@@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/time.h>
+#include <sys/resourcevar.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/stat.h>
@@ -135,6 +136,10 @@ static int procstat_get_vnode_info_sysctl(struct filestat *fst,
static gid_t *procstat_getgroups_core(struct procstat_core *core,
unsigned int *count);
static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
+static int procstat_getrlimit_core(struct procstat_core *core, int which,
+ struct rlimit* rlimit);
+static int procstat_getrlimit_sysctl(pid_t pid, int which,
+ struct rlimit* rlimit);
static int procstat_getumask_core(struct procstat_core *core,
unsigned short *maskp);
static int procstat_getumask_sysctl(pid_t pid, unsigned short *maskp);
@@ -1712,3 +1717,66 @@ procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
return (-1);
}
}
+
+static int
+procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
+{
+ int error, name[5];
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_RLIMIT;
+ name[3] = pid;
+ name[4] = which;
+ len = sizeof(struct rlimit);
+ error = sysctl(name, 5, rlimit, &len, NULL, 0);
+ if (error < 0 && errno != ESRCH) {
+ warn("sysctl: kern.proc.rlimit: %d", pid);
+ return (-1);
+ }
+ if (error < 0 || len != sizeof(struct rlimit))
+ return (-1);
+ return (0);
+}
+
+static int
+procstat_getrlimit_core(struct procstat_core *core, int which,
+ struct rlimit* rlimit)
+{
+ size_t len;
+ struct rlimit* rlimits;
+
+ if (which < 0 || which >= RLIM_NLIMITS) {
+ errno = EINVAL;
+ warn("getrlimit: which");
+ return (-1);
+ }
+ rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
+ if (rlimits == NULL)
+ return (-1);
+ if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
+ free(rlimits);
+ return (-1);
+ }
+ *rlimit = rlimits[which];
+ return (0);
+}
+
+int
+procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
+ struct rlimit* rlimit)
+{
+ switch(procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (-1);
+ case PROCSTAT_SYSCTL:
+ return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
+ case PROCSTAT_CORE:
+ return (procstat_getrlimit_core(procstat->core, which, rlimit));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (-1);
+ }
+}
OpenPOWER on IntegriCloud