Index: sys/sys/sysctl.h =================================================================== --- sys/sys/sysctl.h (revision 227900) +++ sys/sys/sysctl.h (working copy) @@ -561,6 +561,7 @@ SYSCTL_ALLOWED_TYPES(UINT64, uint64_t *a; unsigned #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 Index: sys/sys/resource.h =================================================================== --- sys/sys/resource.h (revision 227900) +++ sys/sys/resource.h (working copy) @@ -108,7 +108,7 @@ struct rusage { */ #ifdef _RLIMIT_IDENT -static char *rlimit_ident[RLIM_NLIMITS] = { +static const char *rlimit_ident[RLIM_NLIMITS] = { "cpu", "fsize", "data", Index: sys/kern/kern_proc.c =================================================================== --- sys/kern/kern_proc.c (revision 227900) +++ sys/kern/kern_proc.c (working copy) @@ -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, k 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"); Index: usr.bin/procstat/procstat_rlimit.c =================================================================== --- usr.bin/procstat/procstat_rlimit.c (revision 0) +++ usr.bin/procstat/procstat_rlimit.c (working copy) @@ -0,0 +1,78 @@ +/*- + * Copyright (c) 2011 Mikolaj Golub + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#define _RLIMIT_IDENT +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "procstat.h" + +static struct rlimit rlimit[RLIM_NLIMITS]; + +void +procstat_rlimit(struct kinfo_proc *kipp) +{ + int error, i, name[4]; + size_t len; + + if (!hflag) + printf("%5s %-16s %-10s %12s %12s\n", "PID", "COMM", "RLIMIT", + "CURRENT", "MAX"); + name[0] = CTL_KERN; + name[1] = KERN_PROC; + name[2] = KERN_PROC_RLIMIT; + name[3] = kipp->ki_pid; + len = sizeof(rlimit); + error = sysctl(name, 4, rlimit, &len, NULL, 0); + if (error < 0 && errno != ESRCH) { + warn("sysctl: kern.proc.rlimit: %d", kipp->ki_pid); + return; + } + if (error < 0 || len != sizeof(rlimit)) + return; + + for (i = 0; i < RLIM_NLIMITS; i++) { + printf("%5d %-16s %-10s %12jd %12jd\n", kipp->ki_pid, + kipp->ki_comm, rlimit_ident[i], + rlimit[i].rlim_cur == RLIM_INFINITY ? + -1 : rlimit[i].rlim_cur, + rlimit[i].rlim_max == RLIM_INFINITY ? + -1 : rlimit[i].rlim_max); + } +} Property changes on: usr.bin/procstat/procstat_rlimit.c ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H Added: svn:eol-style ## -0,0 +1 ## +native Index: usr.bin/procstat/Makefile =================================================================== --- usr.bin/procstat/Makefile (revision 227900) +++ usr.bin/procstat/Makefile (working copy) @@ -10,6 +10,7 @@ SRCS= procstat.c \ procstat_cred.c \ procstat_files.c \ procstat_kstack.c \ + procstat_rlimit.c \ procstat_sigs.c \ procstat_threads.c \ procstat_vm.c Index: usr.bin/procstat/procstat.c =================================================================== --- usr.bin/procstat/procstat.c (revision 227900) +++ usr.bin/procstat/procstat.c (working copy) @@ -39,8 +39,8 @@ #include "procstat.h" -static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, sflag, tflag; -static int vflag, xflag; +static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, sflag; +static int tflag, vflag, xflag; int hflag, nflag, Cflag; static void @@ -50,7 +50,7 @@ usage(void) fprintf(stderr, "usage: procstat [-h] [-C] [-M core] [-N system] " "[-w interval] \n"); fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | " - "-s | -t | -v | -x] [-a | pid ...]\n"); + "-l | -s | -t | -v | -x] [-a | pid ...]\n"); exit(EX_USAGE); } @@ -72,6 +72,8 @@ procstat(struct procstat *prstat, struct kinfo_pro procstat_threads_sigs(prstat, kipp); else if (kflag) procstat_kstack(kipp, kflag); + else if (lflag) + procstat_rlimit(kipp); else if (sflag) procstat_cred(kipp); else if (tflag) @@ -123,7 +125,7 @@ main(int argc, char *argv[]) interval = 0; memf = nlistf = NULL; - while ((ch = getopt(argc, argv, "CN:M:abcefijkhstvw:x")) != -1) { + while ((ch = getopt(argc, argv, "CN:M:abcefijklhstvw:x")) != -1) { switch (ch) { case 'C': Cflag++; @@ -167,6 +169,10 @@ main(int argc, char *argv[]) kflag++; break; + case 'l': + lflag++; + break; + case 'n': nflag++; break; @@ -210,8 +216,8 @@ main(int argc, char *argv[]) argv += optind; /* We require that either 0 or 1 mode flags be set. */ - tmp = bflag + cflag + eflag + fflag + (kflag ? 1 : 0) + sflag + tflag + - vflag + xflag; + tmp = bflag + cflag + eflag + fflag + (kflag ? 1 : 0) + lflag + sflag + + tflag + vflag + xflag; if (!(tmp == 0 || tmp == 1)) usage(); Index: usr.bin/procstat/procstat.h =================================================================== --- usr.bin/procstat/procstat.h (revision 227900) +++ usr.bin/procstat/procstat.h (working copy) @@ -42,6 +42,7 @@ void procstat_cred(struct kinfo_proc *kipp); void procstat_env(struct kinfo_proc *kipp); void procstat_files(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_kstack(struct kinfo_proc *kipp, int kflag); +void procstat_rlimit(struct kinfo_proc *kipp); void procstat_sigs(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_threads(struct kinfo_proc *kipp); void procstat_threads_sigs(struct procstat *prstat, struct kinfo_proc *kipp); Index: usr.bin/procstat/procstat.1 =================================================================== --- usr.bin/procstat/procstat.1 (revision 227900) +++ usr.bin/procstat/procstat.1 (working copy) @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd November 22, 2011 +.Dd November 24, 2011 .Dt PROCSTAT 1 .Os .Sh NAME @@ -69,6 +69,8 @@ Display the stacks of kernel threads in the proces threads currently running on a CPU and threads with stacks swapped to disk. If the flag is repeated, function offsets as well as function names are printed. +.It Fl l +Display resource limits for the process. .It Fl s Display security credential information for the process. .It Fl t