procstat_args.c revision 200420
11553Srgrimes/*- 21553Srgrimes * Copyright (c) 2007 Robert N. M. Watson 31553Srgrimes * All rights reserved. 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 131553Srgrimes * 141553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241553Srgrimes * SUCH DAMAGE. 251553Srgrimes * 261553Srgrimes * $FreeBSD: head/usr.bin/procstat/procstat_args.c 200420 2009-12-11 23:35:38Z delphij $ 271553Srgrimes */ 281553Srgrimes 291553Srgrimes#include <sys/param.h> 301553Srgrimes#include <sys/sysctl.h> 311553Srgrimes#include <sys/user.h> 321553Srgrimes 331553Srgrimes#include <err.h> 341553Srgrimes#include <errno.h> 351553Srgrimes#include <limits.h> 361553Srgrimes#include <stdio.h> 371553Srgrimes#include <string.h> 381553Srgrimes 391553Srgrimes#include "procstat.h" 401553Srgrimes 411553Srgrimesstatic char args[ARG_MAX]; 421553Srgrimes 431553Srgrimesvoid 441553Srgrimesprocstat_args(pid_t pid, struct kinfo_proc *kipp) 451553Srgrimes{ 461553Srgrimes int error, name[4]; 471553Srgrimes size_t len; 481553Srgrimes char *cp; 491553Srgrimes 501553Srgrimes if (!hflag) 511553Srgrimes printf("%5s %-16s %-53s\n", "PID", "COMM", "ARGS"); 521553Srgrimes 531553Srgrimes name[0] = CTL_KERN; 541553Srgrimes name[1] = KERN_PROC; 551553Srgrimes name[2] = KERN_PROC_ARGS; 561553Srgrimes name[3] = pid; 571553Srgrimes len = sizeof(args); 581553Srgrimes error = sysctl(name, 4, args, &len, NULL, 0); 591553Srgrimes if (error < 0 && errno != ESRCH) { 601553Srgrimes warn("sysctl: kern.proc.args: %d", pid); 611553Srgrimes return; 621553Srgrimes } 631553Srgrimes if (error < 0) 641553Srgrimes return; 651553Srgrimes if (len == 0 || strlen(args) == 0) { 661553Srgrimes strcpy(args, "-"); 671553Srgrimes len = strlen(args) + 1; 6815032Ssef } 6915032Ssef 701553Srgrimes printf("%5d ", pid); 711553Srgrimes printf("%-16s ", kipp->ki_comm); 721553Srgrimes for (cp = args; cp < args + len; cp += strlen(cp) + 1) 731553Srgrimes printf("%s%s", cp != args ? " " : "", cp); 741553Srgrimes printf("\n"); 751553Srgrimes} 761553Srgrimes