1227956Strociny/*-
2227956Strociny * Copyright (c) 2011 Mikolaj Golub
3227956Strociny * All rights reserved.
4227956Strociny *
5227956Strociny * Redistribution and use in source and binary forms, with or without
6227956Strociny * modification, are permitted provided that the following conditions
7227956Strociny * are met:
8227956Strociny * 1. Redistributions of source code must retain the above copyright
9227956Strociny *    notice, this list of conditions and the following disclaimer.
10227956Strociny * 2. Redistributions in binary form must reproduce the above copyright
11227956Strociny *    notice, this list of conditions and the following disclaimer in the
12227956Strociny *    documentation and/or other materials provided with the distribution.
13227956Strociny *
14227956Strociny * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15227956Strociny * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16227956Strociny * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17227956Strociny * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18227956Strociny * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19227956Strociny * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20227956Strociny * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21227956Strociny * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22227956Strociny * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23227956Strociny * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24227956Strociny * SUCH DAMAGE.
25227956Strociny *
26227956Strociny * $FreeBSD$
27227956Strociny */
28227956Strociny
29227956Strociny#include <sys/param.h>
30227956Strociny#include <sys/time.h>
31227956Strociny#include <sys/resourcevar.h>
32227956Strociny#include <sys/sysctl.h>
33227956Strociny#include <sys/user.h>
34227956Strociny
35227956Strociny#include <err.h>
36227956Strociny#include <errno.h>
37227956Strociny#include <libprocstat.h>
38228446Strociny#include <libutil.h>
39227956Strociny#include <limits.h>
40227956Strociny#include <stdio.h>
41227956Strociny#include <stdlib.h>
42227956Strociny#include <string.h>
43227956Strociny
44227956Strociny#include "procstat.h"
45227956Strociny
46228446Strocinystatic struct {
47228446Strociny	const char *name;
48228446Strociny	const char *suffix;
49228446Strociny} rlimit_param[13] = {
50228446Strociny	{"cputime",          "sec"},
51228446Strociny	{"filesize",         "B  "},
52228446Strociny	{"datasize",         "B  "},
53228446Strociny	{"stacksize",        "B  "},
54228446Strociny	{"coredumpsize",     "B  "},
55228446Strociny	{"memoryuse",        "B  "},
56228446Strociny	{"memorylocked",     "B  "},
57228446Strociny	{"maxprocesses",     "   "},
58228446Strociny	{"openfiles",        "   "},
59228446Strociny	{"sbsize",           "B  "},
60228446Strociny	{"vmemoryuse",       "B  "},
61228446Strociny	{"pseudo-terminals", "   "},
62228446Strociny	{"swapuse",          "B  "},
63228446Strociny};
64228446Strociny
65228446Strociny#if RLIM_NLIMITS > 13
66228446Strociny#error "Resource limits have grown. Add new entries to rlimit_param[]."
67228446Strociny#endif
68228446Strociny
69238086Strocinystatic const char *
70238086Strocinyhumanize_rlimit(int indx, rlim_t limit)
71228446Strociny{
72228446Strociny	static char buf[14];
73228446Strociny	int scale;
74228446Strociny
75228446Strociny	if (limit == RLIM_INFINITY)
76228446Strociny		return ("infinity     ");
77228446Strociny
78228446Strociny	scale = humanize_number(buf, sizeof(buf) - 1, (int64_t)limit,
79228446Strociny	    rlimit_param[indx].suffix, HN_AUTOSCALE | HN_GETSCALE, HN_DECIMAL);
80228446Strociny	(void)humanize_number(buf, sizeof(buf) - 1, (int64_t)limit,
81228446Strociny	    rlimit_param[indx].suffix, HN_AUTOSCALE, HN_DECIMAL);
82228446Strociny	/* Pad with one space if there is no suffix prefix. */
83228446Strociny	if (scale == 0)
84228446Strociny		sprintf(buf + strlen(buf), " ");
85228446Strociny	return (buf);
86228446Strociny}
87228446Strociny
88227956Strocinyvoid
89249675Strocinyprocstat_rlimit(struct procstat *prstat, struct kinfo_proc *kipp)
90227956Strociny{
91230548Strociny	struct rlimit rlimit;
92249675Strociny	int i;
93227956Strociny
94228446Strociny	if (!hflag) {
95228446Strociny		printf("%5s %-16s %-16s %16s %16s\n",
96228446Strociny		    "PID", "COMM", "RLIMIT", "SOFT     ", "HARD     ");
97228446Strociny	}
98230471Strociny	for (i = 0; i < RLIM_NLIMITS; i++) {
99249675Strociny		if (procstat_getrlimit(prstat, kipp, i, &rlimit) == -1)
100230471Strociny			return;
101228446Strociny		printf("%5d %-16s %-16s ", kipp->ki_pid, kipp->ki_comm,
102228446Strociny		    rlimit_param[i].name);
103230548Strociny		printf("%16s ", humanize_rlimit(i, rlimit.rlim_cur));
104230548Strociny		printf("%16s\n", humanize_rlimit(i, rlimit.rlim_max));
105230548Strociny	}
106227956Strociny}
107