usr.bin/procstat

Add -l flag to display resource limits.

PR:		bin/161257
Reviewed by:	kib
MFC after:	2 weeks
This commit is contained in:
Mikolaj Golub 2011-11-24 20:54:06 +00:00
parent 9e7d058351
commit 598585e835
5 changed files with 95 additions and 7 deletions

View File

@ -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

View File

@ -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 process, excluding stacks of
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

View File

@ -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_proc *kipp)
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();

View File

@ -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);

View File

@ -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 <sys/param.h>
#include <sys/time.h>
#define _RLIMIT_IDENT
#include <sys/resourcevar.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <err.h>
#include <errno.h>
#include <libprocstat.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}
}