2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2007-2011 Robert N. M. Watson
5 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/capsicum.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
37 #include <netinet/in.h>
39 #include <arpa/inet.h>
42 #include <libprocstat.h>
51 protocol_to_string(int domain, int type, int protocol)
90 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
92 char buffer2[INET6_ADDRSTRLEN];
93 struct sockaddr_in6 *sin6;
94 struct sockaddr_in *sin;
95 struct sockaddr_un *sun;
97 switch (ss->ss_family) {
99 sun = (struct sockaddr_un *)ss;
100 if (strlen(sun->sun_path) == 0)
101 strlcpy(buffer, "-", buflen);
103 strlcpy(buffer, sun->sun_path, buflen);
107 sin = (struct sockaddr_in *)ss;
108 if (sin->sin_addr.s_addr == INADDR_ANY)
109 snprintf(buffer, buflen, "%s:%d", "*",
110 ntohs(sin->sin_port));
111 else if (inet_ntop(AF_INET, &sin->sin_addr, buffer2,
112 sizeof(buffer2)) != NULL)
113 snprintf(buffer, buflen, "%s:%d", buffer2,
114 ntohs(sin->sin_port));
118 sin6 = (struct sockaddr_in6 *)ss;
119 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
120 sizeof(buffer2)) != NULL)
121 snprintf(buffer, buflen, "%s.%d", buffer2,
122 ntohs(sin6->sin6_port));
124 strlcpy(buffer, "-", buflen);
128 strlcpy(buffer, "", buflen);
133 static struct cap_desc {
137 /* General file I/O. */
142 { CAP_CREATE, "cr" },
143 { CAP_FEXECVE, "fe" },
145 { CAP_FTRUNCATE, "ft" },
148 { CAP_FCHDIR, "cd" },
149 { CAP_FCHFLAGS, "cf" },
150 { CAP_FCHMOD, "cm" },
151 { CAP_FCHOWN, "cn" },
154 { CAP_FPATHCONF, "fp" },
157 { CAP_FSTATFS, "sf" },
158 { CAP_FUTIMES, "fu" },
159 { CAP_LINKAT_SOURCE, "ls" },
160 { CAP_LINKAT_TARGET, "lt" },
161 { CAP_MKDIRAT, "md" },
162 { CAP_MKFIFOAT, "mf" },
163 { CAP_MKNODAT, "mn" },
164 { CAP_RENAMEAT_SOURCE, "rs" },
165 { CAP_RENAMEAT_TARGET, "rt" },
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 = nitems(cap_desc);
247 width_capability(cap_rights_t *rightsp)
249 u_int count, i, width;
253 for (i = 0; i < cap_desc_count; i++) {
254 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
255 width += strlen(cap_desc[i].cd_desc);
265 print_capability(cap_rights_t *rightsp, u_int capwidth)
270 for (i = width_capability(rightsp); i < capwidth; i++) {
276 xo_open_list("capabilities");
277 for (i = 0; i < cap_desc_count; i++) {
278 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
279 xo_emit("{D:/%s}{l:capabilities/%s}", count ? "," : "",
280 cap_desc[i].cd_desc);
284 xo_close_list("capabilities");
288 procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
290 struct sockstat sock;
291 struct filestat_list *head;
292 struct filestat *fst;
295 u_int capwidth, width;
297 char src_addr[PATH_MAX];
298 char dst_addr[PATH_MAX];
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);
309 (procstat_opts & PS_OPT_CAPABILITIES) != 0) {
310 STAILQ_FOREACH(fst, head, next) {
311 width = width_capability(&fst->fs_cap_rights);
312 if (width > capwidth)
315 if (capwidth < strlen("CAPABILITIES"))
316 capwidth = strlen("CAPABILITIES");
319 if ((procstat_opts & PS_OPT_NOHEADER) == 0) {
320 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0)
321 xo_emit("{T:/%5s %-16s %5s %1s %-8s %-*s "
322 "%-3s %-12s}\n", "PID", "COMM", "FD", "T",
323 "FLAGS", capwidth, "CAPABILITIES", "PRO",
326 xo_emit("{T:/%5s %-16s %5s %1s %1s %-8s "
327 "%3s %7s %-3s %-12s}\n", "PID", "COMM", "FD", "T",
328 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
333 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
334 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
335 xo_open_list("files");
336 STAILQ_FOREACH(fst, head, next) {
337 xo_open_instance("files");
338 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
339 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
340 if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
341 xo_emit("{P: }{:fd/%s} ", "ctty");
342 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
343 xo_emit("{P: }{:fd/%s} ", "cwd");
344 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
345 xo_emit("{P: }{:fd/%s} ", "jail");
346 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
347 xo_emit("{P: }{:fd/%s} ", "root");
348 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
349 xo_emit("{P: }{:fd/%s} ", "text");
350 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
351 xo_emit("{:fd/%s} ", "trace");
353 xo_emit("{:fd/%5d} ", fst->fs_fd);
355 switch (fst->fs_type) {
356 case PS_FST_TYPE_VNODE:
358 xo_emit("{eq:fd_type/vnode}");
361 case PS_FST_TYPE_SOCKET:
363 xo_emit("{eq:fd_type/socket}");
366 case PS_FST_TYPE_PIPE:
368 xo_emit("{eq:fd_type/pipe}");
371 case PS_FST_TYPE_FIFO:
373 xo_emit("{eq:fd_type/fifo}");
376 case PS_FST_TYPE_KQUEUE:
378 xo_emit("{eq:fd_type/kqueue}");
381 case PS_FST_TYPE_MQUEUE:
383 xo_emit("{eq:fd_type/mqueue}");
386 case PS_FST_TYPE_SHM:
388 xo_emit("{eq:fd_type/shm}");
391 case PS_FST_TYPE_PTS:
393 xo_emit("{eq:fd_type/pts}");
396 case PS_FST_TYPE_SEM:
398 xo_emit("{eq:fd_type/sem}");
401 case PS_FST_TYPE_PROCDESC:
403 xo_emit("{eq:fd_type/procdesc}");
406 case PS_FST_TYPE_DEV:
408 xo_emit("{eq:fd_type/dev}");
411 case PS_FST_TYPE_EVENTFD:
413 xo_emit("{eq:fd_type/eventfd}");
416 case PS_FST_TYPE_NONE:
418 xo_emit("{eq:fd_type/none}");
421 case PS_FST_TYPE_UNKNOWN:
424 xo_emit("{eq:fd_type/unknown}");
427 xo_emit("{d:fd_type/%1s/%s} ", str);
428 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) {
430 if (fst->fs_type == PS_FST_TYPE_VNODE) {
431 error = procstat_get_vnode_info(procstat, fst,
433 switch (vn.vn_type) {
434 case PS_FST_VTYPE_VREG:
436 xo_emit("{eq:vode_type/regular}");
439 case PS_FST_VTYPE_VDIR:
441 xo_emit("{eq:vode_type/directory}");
444 case PS_FST_VTYPE_VBLK:
446 xo_emit("{eq:vode_type/block}");
449 case PS_FST_VTYPE_VCHR:
451 xo_emit("{eq:vode_type/character}");
454 case PS_FST_VTYPE_VLNK:
456 xo_emit("{eq:vode_type/link}");
459 case PS_FST_VTYPE_VSOCK:
461 xo_emit("{eq:vode_type/socket}");
464 case PS_FST_VTYPE_VFIFO:
466 xo_emit("{eq:vode_type/fifo}");
469 case PS_FST_VTYPE_VBAD:
471 xo_emit("{eq:vode_type/revoked_device}");
474 case PS_FST_VTYPE_VNON:
476 xo_emit("{eq:vode_type/non}");
479 case PS_FST_VTYPE_UNKNOWN:
482 xo_emit("{eq:vode_type/unknown}");
486 xo_emit("{d:vnode_type/%1s/%s} ", str);
489 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_READ ?
491 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_WRITE ?
493 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_APPEND ?
495 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_ASYNC ?
497 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_SYNC ?
499 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ?
501 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_DIRECT ?
503 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ?
506 xo_open_list("fd_flags");
507 if (fst->fs_fflags & PS_FST_FFLAG_READ)
508 xo_emit("{elq:fd_flags/read}");
509 if (fst->fs_fflags & PS_FST_FFLAG_WRITE)
510 xo_emit("{elq:fd_flags/write}");
511 if (fst->fs_fflags & PS_FST_FFLAG_APPEND)
512 xo_emit("{elq:fd_flags/append}");
513 if (fst->fs_fflags & PS_FST_FFLAG_ASYNC)
514 xo_emit("{elq:fd_flags/async}");
515 if (fst->fs_fflags & PS_FST_FFLAG_SYNC)
516 xo_emit("{elq:fd_flags/fsync}");
517 if (fst->fs_fflags & PS_FST_FFLAG_NONBLOCK)
518 xo_emit("{elq:fd_flags/nonblocking}");
519 if (fst->fs_fflags & PS_FST_FFLAG_DIRECT)
520 xo_emit("{elq:fd_flags/direct_io}");
521 if (fst->fs_fflags & PS_FST_FFLAG_HASLOCK)
522 xo_emit("{elq:fd_flags/lock_held}");
523 xo_close_list("fd_flags");
525 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) {
526 if (fst->fs_ref_count > -1)
527 xo_emit("{:ref_count/%3d/%d} ",
530 xo_emit("{q:ref_count/%3c/%c} ", '-');
531 if (fst->fs_offset > -1)
532 xo_emit("{:offset/%7jd/%jd} ",
533 (intmax_t)fst->fs_offset);
535 xo_emit("{q:offset/%7c/%c} ", '-');
537 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0) {
538 print_capability(&fst->fs_cap_rights, capwidth);
541 switch (fst->fs_type) {
542 case PS_FST_TYPE_SOCKET:
543 error = procstat_get_socket_info(procstat, fst, &sock,
547 xo_emit("{:protocol/%-3s/%s} ",
548 protocol_to_string(sock.dom_family,
549 sock.type, sock.proto));
550 if (sock.proto == IPPROTO_TCP ||
551 sock.proto == IPPROTO_SCTP ||
552 sock.type == SOCK_STREAM) {
553 xo_emit("{:sendq/%u} ", sock.sendq);
554 xo_emit("{:recvq/%u} ", sock.recvq);
557 * While generally we like to print two addresses,
558 * local and peer, for sockets, it turns out to be
559 * more useful to print the first non-nul address for
560 * local sockets, as typically they aren't bound and
561 * connected, and the path strings can get long.
563 if (sock.dom_family == AF_LOCAL) {
564 struct sockaddr_un *sun =
565 (struct sockaddr_un *)&sock.sa_local;
567 if (sun->sun_path[0] != 0)
568 addr_to_string(&sock.sa_local,
569 src_addr, sizeof(src_addr));
571 addr_to_string(&sock.sa_peer,
572 src_addr, sizeof(src_addr));
573 xo_emit("{:path/%s}", src_addr);
575 addr_to_string(&sock.sa_local, src_addr,
577 addr_to_string(&sock.sa_peer, dst_addr,
579 xo_emit("{:path/%s %s}", src_addr, dst_addr);
584 xo_emit("{:protocol/%-3s/%s} ", "-");
585 xo_emit("{:path/%-18s/%s}", fst->fs_path != NULL ?
590 xo_close_instance("files");
592 xo_close_list("files");
593 procstat_freefiles(procstat, head);