procstat_bin.c revision 174199
1174199Srwatson/*-
2174199Srwatson * Copyright (c) 2007 Robert N. M. Watson
3174199Srwatson * All rights reserved.
4174199Srwatson *
5174199Srwatson * Redistribution and use in source and binary forms, with or without
6174199Srwatson * modification, are permitted provided that the following conditions
7174199Srwatson * are met:
8174199Srwatson * 1. Redistributions of source code must retain the above copyright
9174199Srwatson *    notice, this list of conditions and the following disclaimer.
10174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11174199Srwatson *    notice, this list of conditions and the following disclaimer in the
12174199Srwatson *    documentation and/or other materials provided with the distribution.
13174199Srwatson *
14174199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15174199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16174199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17174199Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18174199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19174199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20174199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21174199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22174199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23174199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24174199Srwatson * SUCH DAMAGE.
25174199Srwatson *
26174199Srwatson * $FreeBSD: head/usr.bin/procstat/procstat_bin.c 174199 2007-12-02 23:31:45Z rwatson $
27174199Srwatson */
28174199Srwatson
29174199Srwatson#include <sys/types.h>
30174199Srwatson#include <sys/sysctl.h>
31174199Srwatson
32174199Srwatson#include <err.h>
33174199Srwatson#include <errno.h>
34174199Srwatson#include <limits.h>
35174199Srwatson#include <stdio.h>
36174199Srwatson#include <string.h>
37174199Srwatson
38174199Srwatson#include "procstat.h"
39174199Srwatson
40174199Srwatsonvoid
41174199Srwatsonprocstat_bin(pid_t pid, struct kinfo_proc *kipp)
42174199Srwatson{
43174199Srwatson	char pathname[PATH_MAX];
44174199Srwatson	int error, name[4];
45174199Srwatson	size_t len;
46174199Srwatson
47174199Srwatson	if (!hflag)
48174199Srwatson		printf("%5s %-70s\n", "PID", "PATH");
49174199Srwatson
50174199Srwatson	name[0] = CTL_KERN;
51174199Srwatson	name[1] = KERN_PROC;
52174199Srwatson	name[2] = KERN_PROC_PATHNAME;
53174199Srwatson	name[3] = pid;
54174199Srwatson
55174199Srwatson	len = sizeof(pathname);
56174199Srwatson	error = sysctl(name, 4, pathname, &len, NULL, 0);
57174199Srwatson	if (error < 0 && errno != ESRCH) {
58174199Srwatson		warn("sysctl: kern.proc.pathname: %d", pid);
59174199Srwatson		return;
60174199Srwatson	}
61174199Srwatson	if (error < 0)
62174199Srwatson		return;
63174199Srwatson	if (len == 0 || strlen(pathname) == 0)
64174199Srwatson		strcpy(pathname, "-");
65174199Srwatson
66174199Srwatson	printf("%5d ", pid);
67174199Srwatson	printf("%s\n", pathname);
68174199Srwatson}
69