procstat_args.c revision 174199
154134Swpaul/*-
254134Swpaul * Copyright (c) 2007 Robert N. M. Watson
354134Swpaul * All rights reserved.
454134Swpaul *
554134Swpaul * Redistribution and use in source and binary forms, with or without
654134Swpaul * modification, are permitted provided that the following conditions
754134Swpaul * are met:
854134Swpaul * 1. Redistributions of source code must retain the above copyright
954134Swpaul *    notice, this list of conditions and the following disclaimer.
1054134Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1154134Swpaul *    notice, this list of conditions and the following disclaimer in the
1254134Swpaul *    documentation and/or other materials provided with the distribution.
1354134Swpaul *
1454134Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1554134Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1654134Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1754134Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1854134Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1954134Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2054134Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2154134Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2254134Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2354134Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2454134Swpaul * SUCH DAMAGE.
2554134Swpaul *
2654134Swpaul * $FreeBSD: head/usr.bin/procstat/procstat_args.c 174199 2007-12-02 23:31:45Z rwatson $
2754134Swpaul */
2854134Swpaul
2954134Swpaul#include <sys/types.h>
3054134Swpaul#include <sys/sysctl.h>
3154134Swpaul
3254134Swpaul#include <err.h>
33119418Sobrien#include <errno.h>
34119418Sobrien#include <limits.h>
35119418Sobrien#include <stdio.h>
3654134Swpaul#include <stdlib.h>
3754134Swpaul#include <string.h>
38183505Smarius
39183505Smarius#include "procstat.h"
4054134Swpaul
4154134Swpaulstatic char args[ARG_MAX];
4254134Swpaul
4354134Swpaulvoid
4454134Swpaulprocstat_args(pid_t pid, struct kinfo_proc *kipp)
4554134Swpaul{
4654134Swpaul	int error, name[4];
4754134Swpaul	size_t len;
4874914Sjhb	char *cp;
4954134Swpaul
5067365Sjhb	if (!hflag)
5154134Swpaul		printf("%5s %-70s\n", "PID", "ARGS");
5254134Swpaul
5354134Swpaul	name[0] = CTL_KERN;
5454134Swpaul	name[1] = KERN_PROC;
5554134Swpaul	name[2] = KERN_PROC_ARGS;
5654134Swpaul	name[3] = pid;
5754134Swpaul	len = sizeof(args);
5854134Swpaul	error = sysctl(name, 4, args, &len, NULL, 0);
59109514Sobrien	if (error < 0 && errno != ESRCH) {
6054134Swpaul		warn("sysctl: kern.proc.args: %d", pid);
6154134Swpaul		return;
6254134Swpaul	}
6354134Swpaul	if (error < 0)
64151435Simp		return;
6554134Swpaul	if (len == 0 || strlen(args) == 0) {
6654134Swpaul		strcpy(args, "-");
6754134Swpaul		len = strlen(args) + 1;
68105135Salfred	}
69105135Salfred
7054134Swpaul	printf("%5d ", pid);
7154134Swpaul	for (cp = args; cp < args + len; cp += strlen(cp) + 1)
7254134Swpaul		printf("%s%s", cp != args ? " " : "", cp);
7354134Swpaul	printf("\n");
7454134Swpaul}
7595722Sphk