Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 20 Apr 2013 07:59:45 +0000 (UTC)
From:      Mikolaj Golub <trociny@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r249674 - head/lib/libprocstat
Message-ID:  <201304200759.r3K7xjLc099398@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trociny
Date: Sat Apr 20 07:59:44 2013
New Revision: 249674
URL: http://svnweb.freebsd.org/changeset/base/249674

Log:
  Add procstat_getrlimit function to retrieve a process resource limits info.
  
  MFC after:	1 month

Modified:
  head/lib/libprocstat/Symbol.map
  head/lib/libprocstat/core.c
  head/lib/libprocstat/core.h
  head/lib/libprocstat/libprocstat.3
  head/lib/libprocstat/libprocstat.c
  head/lib/libprocstat/libprocstat.h

Modified: head/lib/libprocstat/Symbol.map
==============================================================================
--- head/lib/libprocstat/Symbol.map	Sat Apr 20 07:58:20 2013	(r249673)
+++ head/lib/libprocstat/Symbol.map	Sat Apr 20 07:59:44 2013	(r249674)
@@ -20,6 +20,7 @@ FBSD_1.3 {
 	procstat_freevmmap;
 	procstat_get_shm_info;
 	procstat_getgroups;
+	procstat_getrlimit;
 	procstat_getumask;
 	procstat_getvmmap;
 	procstat_open_core;

Modified: head/lib/libprocstat/core.c
==============================================================================
--- head/lib/libprocstat/core.c	Sat Apr 20 07:58:20 2013	(r249673)
+++ head/lib/libprocstat/core.c	Sat Apr 20 07:59:44 2013	(r249674)
@@ -175,6 +175,10 @@ procstat_core_get(struct procstat_core *
 		n_type = NT_PROCSTAT_UMASK;
 		structsize = sizeof(u_short);
 		break;
+	case PSC_TYPE_RLIMIT:
+		n_type = NT_PROCSTAT_RLIMIT;
+		structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
+		break;
 	default:
 		warnx("unknown core stat type: %d", type);
 		return (NULL);

Modified: head/lib/libprocstat/core.h
==============================================================================
--- head/lib/libprocstat/core.h	Sat Apr 20 07:58:20 2013	(r249673)
+++ head/lib/libprocstat/core.h	Sat Apr 20 07:59:44 2013	(r249674)
@@ -35,6 +35,7 @@ enum psc_type {
 	PSC_TYPE_VMMAP,
 	PSC_TYPE_GROUPS,
 	PSC_TYPE_UMASK,
+	PSC_TYPE_RLIMIT,
 };
 
 struct procstat_core;

Modified: head/lib/libprocstat/libprocstat.3
==============================================================================
--- head/lib/libprocstat/libprocstat.3	Sat Apr 20 07:58:20 2013	(r249673)
+++ head/lib/libprocstat/libprocstat.3	Sat Apr 20 07:59:44 2013	(r249674)
@@ -128,6 +128,13 @@
 .Fa "unsigned int *count"
 .Fc
 .Ft "int"
+.Fo procstat_getrlimit
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "int which"
+.Fa "struct rlimit* rlimit"
+.Fc
+.Ft "int"
 .Fo procstat_getumask
 .Fa "struct procstat *procstat"
 .Fa "struct kinfo_proc *kp"
@@ -261,6 +268,16 @@ The caller is responsible to free the al
 function call.
 .Pp
 The
+.Fn procstat_getrlimit
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, resource index
+.Fa which ,
+and returns the actual resource limit in the 4th reference parameter.
+.Pp
+The
 .Fn procstat_getumask
 function gets a pointer to the
 .Vt procstat

Modified: head/lib/libprocstat/libprocstat.c
==============================================================================
--- head/lib/libprocstat/libprocstat.c	Sat Apr 20 07:58:20 2013	(r249673)
+++ head/lib/libprocstat/libprocstat.c	Sat Apr 20 07:59:44 2013	(r249674)
@@ -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_sysct
 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 *procs
 		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);
+	}
+}

Modified: head/lib/libprocstat/libprocstat.h
==============================================================================
--- head/lib/libprocstat/libprocstat.h	Sat Apr 20 07:58:20 2013	(r249673)
+++ head/lib/libprocstat/libprocstat.h	Sat Apr 20 07:59:44 2013	(r249674)
@@ -91,6 +91,7 @@
 
 struct kinfo_vmentry;
 struct procstat;
+struct rlimit;
 struct filestat {
 	int	fs_type;	/* Descriptor type. */
 	int	fs_flags;	/* filestat specific flags. */
@@ -170,6 +171,8 @@ gid_t	*procstat_getgroups(struct procsta
     unsigned int *count);
 int	procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
     unsigned short* umask);
+int	procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
+    int which, struct rlimit* rlimit);
 struct kinfo_vmentry	*procstat_getvmmap(struct procstat *procstat,
     struct kinfo_proc *kp, unsigned int *count);
 struct procstat	*procstat_open_core(const char *filename);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201304200759.r3K7xjLc099398>