procstat_bin.c revision 287486
1174199Srwatson/*- 2174199Srwatson * Copyright (c) 2007 Robert N. M. Watson 3287486Sallanjude * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org> 4174199Srwatson * All rights reserved. 5174199Srwatson * 6174199Srwatson * Redistribution and use in source and binary forms, with or without 7174199Srwatson * modification, are permitted provided that the following conditions 8174199Srwatson * are met: 9174199Srwatson * 1. Redistributions of source code must retain the above copyright 10174199Srwatson * notice, this list of conditions and the following disclaimer. 11174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright 12174199Srwatson * notice, this list of conditions and the following disclaimer in the 13174199Srwatson * documentation and/or other materials provided with the distribution. 14174199Srwatson * 15174199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16174199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17174199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18174199Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19174199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20174199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21174199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22174199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23174199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24174199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25174199Srwatson * SUCH DAMAGE. 26174199Srwatson * 27174199Srwatson * $FreeBSD: head/usr.bin/procstat/procstat_bin.c 287486 2015-09-05 17:02:01Z allanjude $ 28174199Srwatson */ 29174199Srwatson 30186567Srwatson#include <sys/param.h> 31174199Srwatson#include <sys/sysctl.h> 32174530Srwatson#include <sys/user.h> 33174199Srwatson 34174199Srwatson#include <err.h> 35174199Srwatson#include <errno.h> 36221807Sstas#include <libprocstat.h> 37174199Srwatson#include <limits.h> 38174199Srwatson#include <stdio.h> 39174199Srwatson#include <string.h> 40174199Srwatson 41174199Srwatson#include "procstat.h" 42174199Srwatson 43174199Srwatsonvoid 44249678Strocinyprocstat_bin(struct procstat *prstat, struct kinfo_proc *kipp) 45174199Srwatson{ 46249678Strociny int osrel; 47249678Strociny static char pathname[PATH_MAX]; 48174199Srwatson 49174199Srwatson if (!hflag) 50287486Sallanjude xo_emit("{T:/%5s %-16s %8s %s}\n", "PID", "COMM", "OSREL", 51287486Sallanjude "PATH"); 52174199Srwatson 53249678Strociny if (procstat_getpathname(prstat, kipp, pathname, sizeof(pathname)) != 0) 54174199Srwatson return; 55249678Strociny if (strlen(pathname) == 0) 56174199Srwatson strcpy(pathname, "-"); 57249678Strociny if (procstat_getosrel(prstat, kipp, &osrel) != 0) 58233390Strociny return; 59233390Strociny 60287486Sallanjude xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); 61287486Sallanjude xo_emit("{:command/%-16s/%s} ", kipp->ki_comm); 62287486Sallanjude xo_emit("{:osrel/%8d/%d} ", osrel); 63287486Sallanjude xo_emit("{:pathname/%s}\n", pathname); 64174199Srwatson} 65