procstat_rlimit.c revision 230471
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: head/usr.bin/procstat/procstat_rlimit.c 230471 2012-01-22 20:26:46Z trociny $
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
69227956Strocinystatic struct rlimit rlimit[RLIM_NLIMITS];
70227956Strociny
71228446Strocinystatic
72228446Strocinyconst char *humanize_rlimit(int indx, rlim_t limit)
73228446Strociny{
74228446Strociny	static char buf[14];
75228446Strociny	int scale;
76228446Strociny
77228446Strociny	if (limit == RLIM_INFINITY)
78228446Strociny		return ("infinity     ");
79228446Strociny
80228446Strociny	scale = humanize_number(buf, sizeof(buf) - 1, (int64_t)limit,
81228446Strociny	    rlimit_param[indx].suffix, HN_AUTOSCALE | HN_GETSCALE, HN_DECIMAL);
82228446Strociny	(void)humanize_number(buf, sizeof(buf) - 1, (int64_t)limit,
83228446Strociny	    rlimit_param[indx].suffix, HN_AUTOSCALE, HN_DECIMAL);
84228446Strociny	/* Pad with one space if there is no suffix prefix. */
85228446Strociny	if (scale == 0)
86228446Strociny		sprintf(buf + strlen(buf), " ");
87228446Strociny	return (buf);
88228446Strociny}
89228446Strociny
90227956Strocinyvoid
91227956Strocinyprocstat_rlimit(struct kinfo_proc *kipp)
92227956Strociny{
93230471Strociny	int error, i, name[5];
94227956Strociny	size_t len;
95227956Strociny
96228446Strociny	if (!hflag) {
97228446Strociny		printf("%5s %-16s %-16s %16s %16s\n",
98228446Strociny		    "PID", "COMM", "RLIMIT", "SOFT     ", "HARD     ");
99228446Strociny	}
100230471Strociny	len = sizeof(struct rlimit);
101227956Strociny	name[0] = CTL_KERN;
102227956Strociny	name[1] = KERN_PROC;
103227956Strociny	name[2] = KERN_PROC_RLIMIT;
104227956Strociny	name[3] = kipp->ki_pid;
105230471Strociny	for (i = 0; i < RLIM_NLIMITS; i++) {
106230471Strociny		name[4] = i;
107230471Strociny		error = sysctl(name, 5, &rlimit[i], &len, NULL, 0);
108230471Strociny		if (error < 0 && errno != ESRCH) {
109230471Strociny			warn("sysctl: kern.proc.rlimit: %d", kipp->ki_pid);
110230471Strociny			return;
111230471Strociny		}
112230471Strociny		if (error < 0 || len != sizeof(struct rlimit))
113230471Strociny			return;
114227956Strociny
115228446Strociny		printf("%5d %-16s %-16s ", kipp->ki_pid, kipp->ki_comm,
116228446Strociny		    rlimit_param[i].name);
117228446Strociny		printf("%16s ", humanize_rlimit(i, rlimit[i].rlim_cur));
118228446Strociny		printf("%16s\n", humanize_rlimit(i, rlimit[i].rlim_max));
119227956Strociny        }
120227956Strociny}
121