2 * Copyright (c) 2007-2011 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/param.h>
30 #include <sys/capsicum.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
41 #include <libprocstat.h>
50 protocol_to_string(int domain, int type, int protocol)
88 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
90 char buffer2[INET6_ADDRSTRLEN];
91 struct sockaddr_in6 *sin6;
92 struct sockaddr_in *sin;
93 struct sockaddr_un *sun;
95 switch (ss->ss_family) {
97 sun = (struct sockaddr_un *)ss;
98 if (strlen(sun->sun_path) == 0)
99 strlcpy(buffer, "-", buflen);
101 strlcpy(buffer, sun->sun_path, buflen);
105 sin = (struct sockaddr_in *)ss;
106 snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr),
107 ntohs(sin->sin_port));
111 sin6 = (struct sockaddr_in6 *)ss;
112 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
113 sizeof(buffer2)) != NULL)
114 snprintf(buffer, buflen, "%s.%d", buffer2,
115 ntohs(sin6->sin6_port));
117 strlcpy(buffer, "-", buflen);
121 strlcpy(buffer, "", buflen);
127 print_address(struct sockaddr_storage *ss)
131 addr_to_string(ss, addr, sizeof(addr));
135 static struct cap_desc {
139 /* General file I/O. */
144 { CAP_CREATE, "cr" },
145 { CAP_FEXECVE, "fe" },
147 { CAP_FTRUNCATE, "ft" },
150 { CAP_FCHDIR, "cd" },
151 { CAP_FCHFLAGS, "cf" },
152 { CAP_FCHMOD, "cm" },
153 { CAP_FCHOWN, "cn" },
156 { CAP_FPATHCONF, "fp" },
159 { CAP_FSTATFS, "sf" },
160 { CAP_FUTIMES, "fu" },
161 { CAP_LINKAT, "li" },
162 { CAP_MKDIRAT, "md" },
163 { CAP_MKFIFOAT, "mf" },
164 { CAP_MKNODAT, "mn" },
165 { CAP_RENAMEAT, "rn" },
166 { CAP_SYMLINKAT, "sl" },
167 { CAP_UNLINKAT, "un" },
169 /* Lookups - used to constrain *at() calls. */
170 { CAP_LOOKUP, "lo" },
172 /* Extended attributes. */
173 { CAP_EXTATTR_GET, "eg" },
174 { CAP_EXTATTR_SET, "es" },
175 { CAP_EXTATTR_DELETE, "ed" },
176 { CAP_EXTATTR_LIST, "el" },
178 /* Access Control Lists. */
179 { CAP_ACL_GET, "ag" },
180 { CAP_ACL_SET, "as" },
181 { CAP_ACL_DELETE, "ad" },
182 { CAP_ACL_CHECK, "ac" },
184 /* Socket operations. */
185 { CAP_ACCEPT, "at" },
187 { CAP_CONNECT, "co" },
188 { CAP_GETPEERNAME, "pn" },
189 { CAP_GETSOCKNAME, "sn" },
190 { CAP_GETSOCKOPT, "gs" },
191 { CAP_LISTEN, "ln" },
192 { CAP_PEELOFF, "pf" },
193 { CAP_SETSOCKOPT, "ss" },
194 { CAP_SHUTDOWN, "sh" },
196 /* Mandatory Access Control. */
197 { CAP_MAC_GET, "mg" },
198 { CAP_MAC_SET, "ms" },
200 /* Methods on semaphores. */
201 { CAP_SEM_GETVALUE, "sg" },
202 { CAP_SEM_POST, "sp" },
203 { CAP_SEM_WAIT, "sw" },
205 /* Event monitoring and posting. */
207 { CAP_KQUEUE_EVENT, "ke" },
208 { CAP_KQUEUE_CHANGE, "kc" },
210 /* Strange and powerful rights that should not be given lightly. */
212 { CAP_TTYHOOK, "ty" },
214 /* Process management via process descriptors. */
215 { CAP_PDGETPID, "pg" },
216 { CAP_PDWAIT, "pw" },
217 { CAP_PDKILL, "pk" },
220 * Rights that allow to use bindat(2) and connectat(2) syscalls on a
221 * directory descriptor.
223 { CAP_BINDAT, "ba" },
224 { CAP_CONNECTAT, "ca" },
226 /* Aliases and defines that combine multiple rights. */
227 { CAP_PREAD, "prd" },
228 { CAP_PWRITE, "pwr" },
230 { CAP_MMAP_R, "mmr" },
231 { CAP_MMAP_W, "mmw" },
232 { CAP_MMAP_X, "mmx" },
233 { CAP_MMAP_RW, "mrw" },
234 { CAP_MMAP_RX, "mrx" },
235 { CAP_MMAP_WX, "mwx" },
236 { CAP_MMAP_RWX, "mma" },
241 { CAP_SOCK_CLIENT, "scl" },
242 { CAP_SOCK_SERVER, "ssr" },
244 static const u_int cap_desc_count = sizeof(cap_desc) /
248 width_capability(cap_rights_t *rightsp)
250 u_int count, i, width;
254 for (i = 0; i < cap_desc_count; i++) {
255 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
256 width += strlen(cap_desc[i].cd_desc);
266 print_capability(cap_rights_t *rightsp, u_int capwidth)
268 u_int count, i, width;
272 for (i = width_capability(rightsp); i < capwidth; i++) {
278 for (i = 0; i < cap_desc_count; i++) {
279 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
280 printf("%s%s", count ? "," : "", cap_desc[i].cd_desc);
281 width += strlen(cap_desc[i].cd_desc);
290 procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
292 struct sockstat sock;
293 struct filestat_list *head;
294 struct filestat *fst;
297 u_int capwidth, width;
301 * To print the header in capability mode, we need to know the width
302 * of the widest capability string. Even if we get no processes
303 * back, we will print the header, so we defer aborting due to a lack
304 * of processes until after the header logic.
307 head = procstat_getfiles(procstat, kipp, 0);
308 if (head != NULL && Cflag) {
309 STAILQ_FOREACH(fst, head, next) {
310 width = width_capability(&fst->fs_cap_rights);
311 if (width > capwidth)
314 if (capwidth < strlen("CAPABILITIES"))
315 capwidth = strlen("CAPABILITIES");
320 printf("%5s %-16s %4s %1s %-9s %-*s "
321 "%-3s %-12s\n", "PID", "COMM", "FD", "T",
322 "FLAGS", capwidth, "CAPABILITIES", "PRO",
325 printf("%5s %-16s %4s %1s %1s %-9s "
326 "%3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T",
327 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
332 STAILQ_FOREACH(fst, head, next) {
333 printf("%5d ", kipp->ki_pid);
334 printf("%-16s ", kipp->ki_comm);
335 if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
337 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
339 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
341 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
343 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
345 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
348 printf("%5d ", fst->fs_fd);
350 switch (fst->fs_type) {
351 case PS_FST_TYPE_VNODE:
355 case PS_FST_TYPE_SOCKET:
359 case PS_FST_TYPE_PIPE:
363 case PS_FST_TYPE_FIFO:
367 case PS_FST_TYPE_KQUEUE:
371 case PS_FST_TYPE_CRYPTO:
375 case PS_FST_TYPE_MQUEUE:
379 case PS_FST_TYPE_SHM:
383 case PS_FST_TYPE_PTS:
387 case PS_FST_TYPE_SEM:
391 case PS_FST_TYPE_NONE:
392 case PS_FST_TYPE_UNKNOWN:
400 if (fst->fs_type == PS_FST_TYPE_VNODE) {
401 error = procstat_get_vnode_info(procstat, fst,
403 switch (vn.vn_type) {
404 case PS_FST_VTYPE_VREG:
408 case PS_FST_VTYPE_VDIR:
412 case PS_FST_VTYPE_VBLK:
416 case PS_FST_VTYPE_VCHR:
420 case PS_FST_VTYPE_VLNK:
424 case PS_FST_VTYPE_VSOCK:
428 case PS_FST_VTYPE_VFIFO:
432 case PS_FST_VTYPE_VBAD:
436 case PS_FST_VTYPE_VNON:
437 case PS_FST_VTYPE_UNKNOWN:
445 printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-");
446 printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-");
447 printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-");
448 printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-");
449 printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-");
450 printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-");
451 printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-");
452 printf("%s", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-");
454 if (fst->fs_ref_count > -1)
455 printf("%3d ", fst->fs_ref_count);
458 if (fst->fs_offset > -1)
459 printf("%7jd ", (intmax_t)fst->fs_offset);
464 print_capability(&fst->fs_cap_rights, capwidth);
467 switch (fst->fs_type) {
468 case PS_FST_TYPE_SOCKET:
469 error = procstat_get_socket_info(procstat, fst, &sock, NULL);
473 protocol_to_string(sock.dom_family,
474 sock.type, sock.proto));
476 * While generally we like to print two addresses,
477 * local and peer, for sockets, it turns out to be
478 * more useful to print the first non-nul address for
479 * local sockets, as typically they aren't bound and
480 * connected, and the path strings can get long.
482 if (sock.dom_family == AF_LOCAL) {
483 struct sockaddr_un *sun =
484 (struct sockaddr_un *)&sock.sa_local;
486 if (sun->sun_path[0] != 0)
487 print_address(&sock.sa_local);
489 print_address(&sock.sa_peer);
491 print_address(&sock.sa_local);
493 print_address(&sock.sa_peer);
498 printf("%-3s ", "-");
499 printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-");
504 procstat_freefiles(procstat, head);