procstat_rlimit.c revision 230548
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 230548 2012-01-25 20:13:37Z 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 69228446Strocinystatic 70228446Strocinyconst char *humanize_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 89227956Strocinyprocstat_rlimit(struct kinfo_proc *kipp) 90227956Strociny{ 91230548Strociny struct rlimit rlimit; 92230471Strociny int error, i, name[5]; 93227956Strociny size_t len; 94227956Strociny 95228446Strociny if (!hflag) { 96228446Strociny printf("%5s %-16s %-16s %16s %16s\n", 97228446Strociny "PID", "COMM", "RLIMIT", "SOFT ", "HARD "); 98228446Strociny } 99230471Strociny len = sizeof(struct rlimit); 100227956Strociny name[0] = CTL_KERN; 101227956Strociny name[1] = KERN_PROC; 102227956Strociny name[2] = KERN_PROC_RLIMIT; 103227956Strociny name[3] = kipp->ki_pid; 104230471Strociny for (i = 0; i < RLIM_NLIMITS; i++) { 105230471Strociny name[4] = i; 106230548Strociny error = sysctl(name, 5, &rlimit, &len, NULL, 0); 107230471Strociny if (error < 0 && errno != ESRCH) { 108230471Strociny warn("sysctl: kern.proc.rlimit: %d", kipp->ki_pid); 109230471Strociny return; 110230471Strociny } 111230471Strociny if (error < 0 || len != sizeof(struct rlimit)) 112230471Strociny return; 113227956Strociny 114228446Strociny printf("%5d %-16s %-16s ", kipp->ki_pid, kipp->ki_comm, 115228446Strociny rlimit_param[i].name); 116230548Strociny printf("%16s ", humanize_rlimit(i, rlimit.rlim_cur)); 117230548Strociny printf("%16s\n", humanize_rlimit(i, rlimit.rlim_max)); 118230548Strociny } 119227956Strociny} 120