2 * Copyright (c) 2007 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
35 #include <netinet/in.h>
37 #include <arpa/inet.h>
48 protocol_to_string(int domain, int type, int protocol)
86 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
88 char buffer2[INET6_ADDRSTRLEN];
89 struct sockaddr_in6 *sin6;
90 struct sockaddr_in *sin;
91 struct sockaddr_un *sun;
93 switch (ss->ss_family) {
95 sun = (struct sockaddr_un *)ss;
96 if (strlen(sun->sun_path) == 0)
97 strlcpy(buffer, "-", buflen);
99 strlcpy(buffer, sun->sun_path, buflen);
103 sin = (struct sockaddr_in *)ss;
104 snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr),
105 ntohs(sin->sin_port));
109 sin6 = (struct sockaddr_in6 *)ss;
110 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
111 sizeof(buffer2)) != NULL)
112 snprintf(buffer, buflen, "%s.%d", buffer2,
113 ntohs(sin6->sin6_port));
115 strlcpy(buffer, "-", sizeof(buffer));
119 strlcpy(buffer, "", buflen);
125 print_address(struct sockaddr_storage *ss)
129 addr_to_string(ss, addr, sizeof(addr));
134 procstat_files(pid_t pid, struct kinfo_proc *kipp)
136 struct kinfo_file *freep, *kif;
143 printf("%5s %-16s %4s %1s %1s %-8s %3s %7s %-3s %-12s\n",
144 "PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET",
149 name[2] = KERN_PROC_FILEDESC;
152 error = sysctl(name, 4, NULL, &len, NULL, 0);
153 if (error < 0 && errno != ESRCH && errno != EPERM) {
154 warn("sysctl: kern.proc.filedesc: %d", pid);
160 freep = kif = malloc(len);
164 if (sysctl(name, 4, kif, &len, NULL, 0) < 0) {
165 warn("sysctl: kern.proc.filedesc %d", pid);
170 for (i = 0; i < len / sizeof(*kif); i++, kif++) {
171 if (kif->kf_structsize != sizeof(*kif))
172 errx(-1, "kinfo_file mismatch");
174 printf("%-16s ", kipp->ki_comm);
175 switch (kif->kf_fd) {
180 case KF_FD_TYPE_ROOT:
184 case KF_FD_TYPE_JAIL:
189 printf("%4d ", kif->kf_fd);
192 switch (kif->kf_type) {
226 case KF_TYPE_UNKNOWN:
233 if (kif->kf_type == KF_TYPE_VNODE) {
234 switch (kif->kf_vnode_type) {
268 case KF_VTYPE_UNKNOWN:
275 printf("%s", kif->kf_flags & KF_FLAG_READ ? "r" : "-");
276 printf("%s", kif->kf_flags & KF_FLAG_WRITE ? "w" : "-");
277 printf("%s", kif->kf_flags & KF_FLAG_APPEND ? "a" : "-");
278 printf("%s", kif->kf_flags & KF_FLAG_ASYNC ? "s" : "-");
279 printf("%s", kif->kf_flags & KF_FLAG_FSYNC ? "f" : "-");
280 printf("%s", kif->kf_flags & KF_FLAG_NONBLOCK ? "n" : "-");
281 printf("%s", kif->kf_flags & KF_FLAG_DIRECT ? "d" : "-");
282 printf("%s ", kif->kf_flags & KF_FLAG_HASLOCK ? "l" : "-");
283 if (kif->kf_ref_count > -1)
284 printf("%3d ", kif->kf_ref_count);
287 if (kif->kf_offset > -1)
288 printf("%7jd ", (intmax_t)kif->kf_offset);
292 switch (kif->kf_type) {
295 printf("%-3s ", "-");
296 printf("%-18s", kif->kf_path);
301 protocol_to_string(kif->kf_sock_domain,
302 kif->kf_sock_type, kif->kf_sock_protocol));
304 * While generally we like to print two addresses,
305 * local and peer, for sockets, it turns out to be
306 * more useful to print the first non-nul address for
307 * local sockets, as typically they aren't bound and
308 * connected, and the path strings can get long.
310 if (kif->kf_sock_domain == AF_LOCAL) {
311 struct sockaddr_un *sun =
312 (struct sockaddr_un *)&kif->kf_sa_local;
314 if (sun->sun_path[0] != 0)
315 print_address(&kif->kf_sa_local);
317 print_address(&kif->kf_sa_peer);
319 print_address(&kif->kf_sa_local);
321 print_address(&kif->kf_sa_peer);
326 printf("%-3s ", "-");
327 printf("%-18s", "-");