2007-12-02 23:31:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2007 Robert N. M. Watson
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "procstat.h"
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
protocol_to_string(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (domain) {
|
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6:
|
|
|
|
switch (protocol) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
return ("TCP");
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
return ("UDP");
|
|
|
|
case IPPROTO_ICMP:
|
2007-12-10 20:55:43 +00:00
|
|
|
return ("ICM");
|
2007-12-02 23:31:45 +00:00
|
|
|
case IPPROTO_RAW:
|
|
|
|
return ("RAW");
|
|
|
|
case IPPROTO_SCTP:
|
2007-12-10 20:55:43 +00:00
|
|
|
return ("SCT");
|
|
|
|
case IPPROTO_DIVERT:
|
|
|
|
return ("IPD");
|
2007-12-02 23:31:45 +00:00
|
|
|
default:
|
2007-12-10 20:55:43 +00:00
|
|
|
return ("IP?");
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case AF_LOCAL:
|
|
|
|
switch (type) {
|
|
|
|
case SOCK_STREAM:
|
2007-12-10 20:55:43 +00:00
|
|
|
return ("UDS");
|
2007-12-02 23:31:45 +00:00
|
|
|
case SOCK_DGRAM:
|
2007-12-10 20:55:43 +00:00
|
|
|
return ("UDD");
|
2007-12-02 23:31:45 +00:00
|
|
|
default:
|
2007-12-10 20:55:43 +00:00
|
|
|
return ("UD?");
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
|
|
|
default:
|
2007-12-10 20:55:43 +00:00
|
|
|
return ("?");
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
|
|
|
|
{
|
|
|
|
char buffer2[INET6_ADDRSTRLEN];
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
struct sockaddr_un *sun;
|
|
|
|
|
|
|
|
switch (ss->ss_family) {
|
|
|
|
case AF_LOCAL:
|
|
|
|
sun = (struct sockaddr_un *)ss;
|
|
|
|
if (strlen(sun->sun_path) == 0)
|
|
|
|
strlcpy(buffer, "-", buflen);
|
|
|
|
else
|
|
|
|
strlcpy(buffer, sun->sun_path, buflen);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
sin = (struct sockaddr_in *)ss;
|
|
|
|
snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr),
|
|
|
|
ntohs(sin->sin_port));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
sin6 = (struct sockaddr_in6 *)ss;
|
|
|
|
if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
|
|
|
|
sizeof(buffer2)) != NULL)
|
|
|
|
snprintf(buffer, buflen, "%s.%d", buffer2,
|
|
|
|
ntohs(sin6->sin6_port));
|
|
|
|
else
|
|
|
|
strlcpy(buffer, "-", sizeof(buffer));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
strlcpy(buffer, "", buflen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_address(struct sockaddr_storage *ss)
|
|
|
|
{
|
|
|
|
char addr[PATH_MAX];
|
|
|
|
|
|
|
|
addr_to_string(ss, addr, sizeof(addr));
|
2007-12-10 20:55:43 +00:00
|
|
|
printf("%s", addr);
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
procstat_files(pid_t pid, struct kinfo_proc *kipp)
|
|
|
|
{
|
|
|
|
struct kinfo_file *freep, *kif;
|
2008-02-08 11:03:05 +00:00
|
|
|
int error, name[4];
|
|
|
|
unsigned int i;
|
2007-12-02 23:31:45 +00:00
|
|
|
const char *str;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!hflag)
|
2007-12-10 20:55:43 +00:00
|
|
|
printf("%5s %-16s %3s %1s %1s %-8s %3s %7s %-3s %-12s\n",
|
|
|
|
"PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET",
|
|
|
|
"PRO", "NAME");
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
name[0] = CTL_KERN;
|
|
|
|
name[1] = KERN_PROC;
|
|
|
|
name[2] = KERN_PROC_FILEDESC;
|
|
|
|
name[3] = pid;
|
|
|
|
|
|
|
|
error = sysctl(name, 4, NULL, &len, NULL, 0);
|
|
|
|
if (error < 0 && errno != ESRCH && errno != EPERM) {
|
|
|
|
warn("sysctl: kern.proc.filedesc: %d", pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (error < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
freep = kif = malloc(len);
|
|
|
|
if (kif == NULL)
|
|
|
|
err(-1, "malloc");
|
|
|
|
|
|
|
|
if (sysctl(name, 4, kif, &len, NULL, 0) < 0) {
|
|
|
|
warn("sysctl: kern.proc.filedesc %d", pid);
|
|
|
|
free(freep);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < len / sizeof(*kif); i++, kif++) {
|
|
|
|
if (kif->kf_structsize != sizeof(*kif))
|
|
|
|
errx(-1, "kinfo_file mismatch");
|
|
|
|
printf("%5d ", pid);
|
2007-12-10 20:55:43 +00:00
|
|
|
printf("%-16s ", kipp->ki_comm);
|
2007-12-02 23:31:45 +00:00
|
|
|
printf("%3d ", kif->kf_fd);
|
|
|
|
switch (kif->kf_type) {
|
|
|
|
case KF_TYPE_VNODE:
|
|
|
|
str = "v";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_TYPE_SOCKET:
|
|
|
|
str = "s";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_TYPE_PIPE:
|
|
|
|
str = "p";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_TYPE_FIFO:
|
|
|
|
str = "f";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_TYPE_KQUEUE:
|
|
|
|
str = "k";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_TYPE_CRYPTO:
|
|
|
|
str = "c";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_TYPE_MQUEUE:
|
|
|
|
str = "m";
|
|
|
|
break;
|
|
|
|
|
2008-01-20 19:57:33 +00:00
|
|
|
case KF_TYPE_SHM:
|
|
|
|
str = "h";
|
|
|
|
break;
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
case KF_TYPE_NONE:
|
|
|
|
case KF_TYPE_UNKNOWN:
|
|
|
|
default:
|
|
|
|
str = "?";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%1s ", str);
|
|
|
|
str = "-";
|
|
|
|
if (kif->kf_type == KF_TYPE_VNODE) {
|
|
|
|
switch (kif->kf_vnode_type) {
|
|
|
|
case KF_VTYPE_VREG:
|
|
|
|
str = "r";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VDIR:
|
|
|
|
str = "d";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VBLK:
|
|
|
|
str = "b";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VCHR:
|
|
|
|
str = "c";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VLNK:
|
|
|
|
str = "l";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VSOCK:
|
|
|
|
str = "s";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VFIFO:
|
|
|
|
str = "f";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VBAD:
|
|
|
|
str = "x";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_VTYPE_VNON:
|
|
|
|
case KF_VTYPE_UNKNOWN:
|
|
|
|
default:
|
|
|
|
str = "?";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%1s ", str);
|
|
|
|
printf("%s", kif->kf_flags & KF_FLAG_READ ? "r" : "-");
|
|
|
|
printf("%s", kif->kf_flags & KF_FLAG_WRITE ? "w" : "-");
|
|
|
|
printf("%s", kif->kf_flags & KF_FLAG_APPEND ? "a" : "-");
|
|
|
|
printf("%s", kif->kf_flags & KF_FLAG_ASYNC ? "s" : "-");
|
|
|
|
printf("%s", kif->kf_flags & KF_FLAG_FSYNC ? "f" : "-");
|
|
|
|
printf("%s", kif->kf_flags & KF_FLAG_NONBLOCK ? "n" : "-");
|
|
|
|
printf("%s", kif->kf_flags & KF_FLAG_DIRECT ? "d" : "-");
|
|
|
|
printf("%s ", kif->kf_flags & KF_FLAG_HASLOCK ? "l" : "-");
|
|
|
|
printf("%3d ", kif->kf_ref_count);
|
|
|
|
printf("%7jd ", (intmax_t)kif->kf_offset);
|
|
|
|
|
|
|
|
switch (kif->kf_type) {
|
|
|
|
case KF_TYPE_VNODE:
|
|
|
|
case KF_TYPE_FIFO:
|
2007-12-10 20:55:43 +00:00
|
|
|
printf("%-3s ", "-");
|
|
|
|
printf("%-18s", kif->kf_path);
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_TYPE_SOCKET:
|
2007-12-10 20:55:43 +00:00
|
|
|
printf("%-3s ",
|
2007-12-02 23:31:45 +00:00
|
|
|
protocol_to_string(kif->kf_sock_domain,
|
|
|
|
kif->kf_sock_type, kif->kf_sock_protocol));
|
|
|
|
/*
|
|
|
|
* While generally we like to print two addresses,
|
|
|
|
* local and peer, for sockets, it turns out to be
|
|
|
|
* more useful to print the first non-nul address for
|
|
|
|
* local sockets, as typically they aren't bound and
|
|
|
|
* connected, and the path strings can get long.
|
|
|
|
*/
|
|
|
|
if (kif->kf_sock_domain == AF_LOCAL) {
|
|
|
|
struct sockaddr_un *sun =
|
|
|
|
(struct sockaddr_un *)&kif->kf_sa_local;
|
|
|
|
|
|
|
|
if (sun->sun_path[0] != 0)
|
|
|
|
print_address(&kif->kf_sa_local);
|
|
|
|
else
|
|
|
|
print_address(&kif->kf_sa_peer);
|
|
|
|
} else {
|
|
|
|
print_address(&kif->kf_sa_local);
|
|
|
|
printf(" ");
|
|
|
|
print_address(&kif->kf_sa_peer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-12-10 20:55:43 +00:00
|
|
|
printf("%-3s ", "-");
|
|
|
|
printf("%-18s", "-");
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
free(freep);
|
|
|
|
}
|