1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2007-2011 Robert N. M. Watson 5 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/capsicum.h> 35 #include <sys/socket.h> 36 #include <sys/sysctl.h> 37 #include <sys/un.h> 38 #include <sys/user.h> 39 40 #include <netinet/in.h> 41 42 #include <arpa/inet.h> 43 44 #include <err.h> 45 #include <libprocstat.h> 46 #include <inttypes.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "procstat.h" 52 53 static const char * 54 protocol_to_string(int domain, int type, int protocol) 55 { 56 57 switch (domain) { 58 case AF_INET: 59 case AF_INET6: 60 switch (protocol) { 61 case IPPROTO_TCP: 62 return ("TCP"); 63 case IPPROTO_UDP: 64 return ("UDP"); 65 case IPPROTO_ICMP: 66 return ("ICM"); 67 case IPPROTO_RAW: 68 return ("RAW"); 69 case IPPROTO_SCTP: 70 return ("SCT"); 71 default: 72 return ("IP?"); 73 } 74 75 case AF_LOCAL: 76 switch (type) { 77 case SOCK_STREAM: 78 return ("UDS"); 79 case SOCK_DGRAM: 80 return ("UDD"); 81 default: 82 return ("UD?"); 83 } 84 case AF_DIVERT: 85 return ("IPD"); 86 break; 87 default: 88 return ("?"); 89 } 90 } 91 92 static void 93 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen) 94 { 95 char buffer2[INET6_ADDRSTRLEN]; 96 struct sockaddr_in6 *sin6; 97 struct sockaddr_in *sin; 98 struct sockaddr_un *sun; 99 100 switch (ss->ss_family) { 101 case AF_LOCAL: 102 sun = (struct sockaddr_un *)ss; 103 if (strlen(sun->sun_path) == 0) 104 strlcpy(buffer, "-", buflen); 105 else 106 strlcpy(buffer, sun->sun_path, buflen); 107 break; 108 109 case AF_INET: 110 sin = (struct sockaddr_in *)ss; 111 if (sin->sin_addr.s_addr == INADDR_ANY) 112 snprintf(buffer, buflen, "%s:%d", "*", 113 ntohs(sin->sin_port)); 114 else if (inet_ntop(AF_INET, &sin->sin_addr, buffer2, 115 sizeof(buffer2)) != NULL) 116 snprintf(buffer, buflen, "%s:%d", buffer2, 117 ntohs(sin->sin_port)); 118 break; 119 120 case AF_INET6: 121 sin6 = (struct sockaddr_in6 *)ss; 122 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2, 123 sizeof(buffer2)) != NULL) 124 snprintf(buffer, buflen, "%s.%d", buffer2, 125 ntohs(sin6->sin6_port)); 126 else 127 strlcpy(buffer, "-", buflen); 128 break; 129 130 default: 131 strlcpy(buffer, "", buflen); 132 break; 133 } 134 } 135 136 static struct cap_desc { 137 uint64_t cd_right; 138 const char *cd_desc; 139 } cap_desc[] = { 140 /* General file I/O. */ 141 { CAP_READ, "rd" }, 142 { CAP_WRITE, "wr" }, 143 { CAP_SEEK, "se" }, 144 { CAP_MMAP, "mm" }, 145 { CAP_CREATE, "cr" }, 146 { CAP_FEXECVE, "fe" }, 147 { CAP_FSYNC, "fy" }, 148 { CAP_FTRUNCATE, "ft" }, 149 150 /* VFS methods. */ 151 { CAP_FCHDIR, "cd" }, 152 { CAP_FCHFLAGS, "cf" }, 153 { CAP_FCHMOD, "cm" }, 154 { CAP_FCHOWN, "cn" }, 155 { CAP_FCNTL, "fc" }, 156 { CAP_FLOCK, "fl" }, 157 { CAP_FPATHCONF, "fp" }, 158 { CAP_FSCK, "fk" }, 159 { CAP_FSTAT, "fs" }, 160 { CAP_FSTATFS, "sf" }, 161 { CAP_FUTIMES, "fu" }, 162 { CAP_LINKAT_SOURCE, "ls" }, 163 { CAP_LINKAT_TARGET, "lt" }, 164 { CAP_MKDIRAT, "md" }, 165 { CAP_MKFIFOAT, "mf" }, 166 { CAP_MKNODAT, "mn" }, 167 { CAP_RENAMEAT_SOURCE, "rs" }, 168 { CAP_RENAMEAT_TARGET, "rt" }, 169 { CAP_SYMLINKAT, "sl" }, 170 { CAP_UNLINKAT, "un" }, 171 172 /* Lookups - used to constrain *at() calls. */ 173 { CAP_LOOKUP, "lo" }, 174 175 /* Extended attributes. */ 176 { CAP_EXTATTR_GET, "eg" }, 177 { CAP_EXTATTR_SET, "es" }, 178 { CAP_EXTATTR_DELETE, "ed" }, 179 { CAP_EXTATTR_LIST, "el" }, 180 181 /* Access Control Lists. */ 182 { CAP_ACL_GET, "ag" }, 183 { CAP_ACL_SET, "as" }, 184 { CAP_ACL_DELETE, "ad" }, 185 { CAP_ACL_CHECK, "ac" }, 186 187 /* Socket operations. */ 188 { CAP_ACCEPT, "at" }, 189 { CAP_BIND, "bd" }, 190 { CAP_CONNECT, "co" }, 191 { CAP_GETPEERNAME, "pn" }, 192 { CAP_GETSOCKNAME, "sn" }, 193 { CAP_GETSOCKOPT, "gs" }, 194 { CAP_LISTEN, "ln" }, 195 { CAP_PEELOFF, "pf" }, 196 { CAP_SETSOCKOPT, "ss" }, 197 { CAP_SHUTDOWN, "sh" }, 198 199 /* Mandatory Access Control. */ 200 { CAP_MAC_GET, "mg" }, 201 { CAP_MAC_SET, "ms" }, 202 203 /* Methods on semaphores. */ 204 { CAP_SEM_GETVALUE, "sg" }, 205 { CAP_SEM_POST, "sp" }, 206 { CAP_SEM_WAIT, "sw" }, 207 208 /* Event monitoring and posting. */ 209 { CAP_EVENT, "ev" }, 210 { CAP_KQUEUE_EVENT, "ke" }, 211 { CAP_KQUEUE_CHANGE, "kc" }, 212 213 /* Strange and powerful rights that should not be given lightly. */ 214 { CAP_IOCTL, "io" }, 215 { CAP_TTYHOOK, "ty" }, 216 217 /* Process management via process descriptors. */ 218 { CAP_PDGETPID, "pg" }, 219 { CAP_PDWAIT, "pw" }, 220 { CAP_PDKILL, "pk" }, 221 222 /* 223 * Rights that allow to use bindat(2) and connectat(2) syscalls on a 224 * directory descriptor. 225 */ 226 { CAP_BINDAT, "ba" }, 227 { CAP_CONNECTAT, "ca" }, 228 229 /* Aliases and defines that combine multiple rights. */ 230 { CAP_PREAD, "prd" }, 231 { CAP_PWRITE, "pwr" }, 232 233 { CAP_MMAP_R, "mmr" }, 234 { CAP_MMAP_W, "mmw" }, 235 { CAP_MMAP_X, "mmx" }, 236 { CAP_MMAP_RW, "mrw" }, 237 { CAP_MMAP_RX, "mrx" }, 238 { CAP_MMAP_WX, "mwx" }, 239 { CAP_MMAP_RWX, "mma" }, 240 241 { CAP_RECV, "re" }, 242 { CAP_SEND, "sd" }, 243 244 { CAP_SOCK_CLIENT, "scl" }, 245 { CAP_SOCK_SERVER, "ssr" }, 246 }; 247 static const u_int cap_desc_count = nitems(cap_desc); 248 249 static u_int 250 width_capability(cap_rights_t *rightsp) 251 { 252 u_int count, i, width; 253 254 count = 0; 255 width = 0; 256 for (i = 0; i < cap_desc_count; i++) { 257 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) { 258 width += strlen(cap_desc[i].cd_desc); 259 if (count) 260 width++; 261 count++; 262 } 263 } 264 return (width); 265 } 266 267 static void 268 print_capability(cap_rights_t *rightsp, u_int capwidth) 269 { 270 u_int count, i; 271 272 count = 0; 273 for (i = width_capability(rightsp); i < capwidth; i++) { 274 if (i != 0) 275 xo_emit(" "); 276 else 277 xo_emit("-"); 278 } 279 xo_open_list("capabilities"); 280 for (i = 0; i < cap_desc_count; i++) { 281 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) { 282 xo_emit("{D:/%s}{l:capabilities/%s}", count ? "," : "", 283 cap_desc[i].cd_desc); 284 count++; 285 } 286 } 287 xo_close_list("capabilities"); 288 } 289 290 void 291 procstat_files(struct procstat *procstat, struct kinfo_proc *kipp) 292 { 293 struct sockstat sock; 294 struct filestat_list *head; 295 struct filestat *fst; 296 const char *str; 297 struct vnstat vn; 298 u_int capwidth, width; 299 int error; 300 char src_addr[PATH_MAX]; 301 char dst_addr[PATH_MAX]; 302 303 /* 304 * To print the header in capability mode, we need to know the width 305 * of the widest capability string. Even if we get no processes 306 * back, we will print the header, so we defer aborting due to a lack 307 * of processes until after the header logic. 308 */ 309 capwidth = 0; 310 head = procstat_getfiles(procstat, kipp, 0); 311 if (head != NULL && 312 (procstat_opts & PS_OPT_CAPABILITIES) != 0) { 313 STAILQ_FOREACH(fst, head, next) { 314 width = width_capability(&fst->fs_cap_rights); 315 if (width > capwidth) 316 capwidth = width; 317 } 318 if (capwidth < strlen("CAPABILITIES")) 319 capwidth = strlen("CAPABILITIES"); 320 } 321 322 if ((procstat_opts & PS_OPT_NOHEADER) == 0) { 323 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0) 324 xo_emit("{T:/%5s %-16s %5s %1s %-8s %-*s " 325 "%-3s %-12s}\n", "PID", "COMM", "FD", "T", 326 "FLAGS", capwidth, "CAPABILITIES", "PRO", 327 "NAME"); 328 else 329 xo_emit("{T:/%5s %-16s %5s %1s %1s %-8s " 330 "%3s %7s %-3s %-12s}\n", "PID", "COMM", "FD", "T", 331 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME"); 332 } 333 334 if (head == NULL) 335 return; 336 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 337 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 338 xo_open_list("files"); 339 STAILQ_FOREACH(fst, head, next) { 340 xo_open_instance("files"); 341 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 342 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 343 if (fst->fs_uflags & PS_FST_UFLAG_CTTY) 344 xo_emit("{P: }{:fd/%s} ", "ctty"); 345 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR) 346 xo_emit("{P: }{:fd/%s} ", "cwd"); 347 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL) 348 xo_emit("{P: }{:fd/%s} ", "jail"); 349 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR) 350 xo_emit("{P: }{:fd/%s} ", "root"); 351 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT) 352 xo_emit("{P: }{:fd/%s} ", "text"); 353 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE) 354 xo_emit("{:fd/%s} ", "trace"); 355 else 356 xo_emit("{:fd/%5d} ", fst->fs_fd); 357 358 switch (fst->fs_type) { 359 case PS_FST_TYPE_VNODE: 360 str = "v"; 361 xo_emit("{eq:fd_type/vnode}"); 362 break; 363 364 case PS_FST_TYPE_SOCKET: 365 str = "s"; 366 xo_emit("{eq:fd_type/socket}"); 367 break; 368 369 case PS_FST_TYPE_PIPE: 370 str = "p"; 371 xo_emit("{eq:fd_type/pipe}"); 372 break; 373 374 case PS_FST_TYPE_FIFO: 375 str = "f"; 376 xo_emit("{eq:fd_type/fifo}"); 377 break; 378 379 case PS_FST_TYPE_KQUEUE: 380 str = "k"; 381 xo_emit("{eq:fd_type/kqueue}"); 382 break; 383 384 case PS_FST_TYPE_MQUEUE: 385 str = "m"; 386 xo_emit("{eq:fd_type/mqueue}"); 387 break; 388 389 case PS_FST_TYPE_SHM: 390 str = "h"; 391 xo_emit("{eq:fd_type/shm}"); 392 break; 393 394 case PS_FST_TYPE_PTS: 395 str = "t"; 396 xo_emit("{eq:fd_type/pts}"); 397 break; 398 399 case PS_FST_TYPE_SEM: 400 str = "e"; 401 xo_emit("{eq:fd_type/sem}"); 402 break; 403 404 case PS_FST_TYPE_PROCDESC: 405 str = "P"; 406 xo_emit("{eq:fd_type/procdesc}"); 407 break; 408 409 case PS_FST_TYPE_DEV: 410 str = "D"; 411 xo_emit("{eq:fd_type/dev}"); 412 break; 413 414 case PS_FST_TYPE_EVENTFD: 415 str = "E"; 416 xo_emit("{eq:fd_type/eventfd}"); 417 break; 418 419 case PS_FST_TYPE_NONE: 420 str = "?"; 421 xo_emit("{eq:fd_type/none}"); 422 break; 423 424 case PS_FST_TYPE_UNKNOWN: 425 default: 426 str = "?"; 427 xo_emit("{eq:fd_type/unknown}"); 428 break; 429 } 430 xo_emit("{d:fd_type/%1s/%s} ", str); 431 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) { 432 str = "-"; 433 if (fst->fs_type == PS_FST_TYPE_VNODE) { 434 error = procstat_get_vnode_info(procstat, fst, 435 &vn, NULL); 436 switch (vn.vn_type) { 437 case PS_FST_VTYPE_VREG: 438 str = "r"; 439 xo_emit("{eq:vode_type/regular}"); 440 break; 441 442 case PS_FST_VTYPE_VDIR: 443 str = "d"; 444 xo_emit("{eq:vode_type/directory}"); 445 break; 446 447 case PS_FST_VTYPE_VBLK: 448 str = "b"; 449 xo_emit("{eq:vode_type/block}"); 450 break; 451 452 case PS_FST_VTYPE_VCHR: 453 str = "c"; 454 xo_emit("{eq:vode_type/character}"); 455 break; 456 457 case PS_FST_VTYPE_VLNK: 458 str = "l"; 459 xo_emit("{eq:vode_type/link}"); 460 break; 461 462 case PS_FST_VTYPE_VSOCK: 463 str = "s"; 464 xo_emit("{eq:vode_type/socket}"); 465 break; 466 467 case PS_FST_VTYPE_VFIFO: 468 str = "f"; 469 xo_emit("{eq:vode_type/fifo}"); 470 break; 471 472 case PS_FST_VTYPE_VBAD: 473 str = "x"; 474 xo_emit("{eq:vode_type/revoked_device}"); 475 break; 476 477 case PS_FST_VTYPE_VNON: 478 str = "?"; 479 xo_emit("{eq:vode_type/non}"); 480 break; 481 482 case PS_FST_VTYPE_UNKNOWN: 483 default: 484 str = "?"; 485 xo_emit("{eq:vode_type/unknown}"); 486 break; 487 } 488 } 489 xo_emit("{d:vnode_type/%1s/%s} ", str); 490 } 491 492 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_READ ? 493 "r" : "-"); 494 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_WRITE ? 495 "w" : "-"); 496 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_APPEND ? 497 "a" : "-"); 498 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? 499 "s" : "-"); 500 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_SYNC ? 501 "f" : "-"); 502 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? 503 "n" : "-"); 504 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? 505 "d" : "-"); 506 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? 507 "l" : "-"); 508 xo_emit(" "); 509 xo_open_list("fd_flags"); 510 if (fst->fs_fflags & PS_FST_FFLAG_READ) 511 xo_emit("{elq:fd_flags/read}"); 512 if (fst->fs_fflags & PS_FST_FFLAG_WRITE) 513 xo_emit("{elq:fd_flags/write}"); 514 if (fst->fs_fflags & PS_FST_FFLAG_APPEND) 515 xo_emit("{elq:fd_flags/append}"); 516 if (fst->fs_fflags & PS_FST_FFLAG_ASYNC) 517 xo_emit("{elq:fd_flags/async}"); 518 if (fst->fs_fflags & PS_FST_FFLAG_SYNC) 519 xo_emit("{elq:fd_flags/fsync}"); 520 if (fst->fs_fflags & PS_FST_FFLAG_NONBLOCK) 521 xo_emit("{elq:fd_flags/nonblocking}"); 522 if (fst->fs_fflags & PS_FST_FFLAG_DIRECT) 523 xo_emit("{elq:fd_flags/direct_io}"); 524 if (fst->fs_fflags & PS_FST_FFLAG_HASLOCK) 525 xo_emit("{elq:fd_flags/lock_held}"); 526 xo_close_list("fd_flags"); 527 528 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) { 529 if (fst->fs_ref_count > -1) 530 xo_emit("{:ref_count/%3d/%d} ", 531 fst->fs_ref_count); 532 else 533 xo_emit("{q:ref_count/%3c/%c} ", '-'); 534 if (fst->fs_offset > -1) 535 xo_emit("{:offset/%7jd/%jd} ", 536 (intmax_t)fst->fs_offset); 537 else 538 xo_emit("{q:offset/%7c/%c} ", '-'); 539 } 540 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0) { 541 print_capability(&fst->fs_cap_rights, capwidth); 542 xo_emit(" "); 543 } 544 switch (fst->fs_type) { 545 case PS_FST_TYPE_SOCKET: 546 error = procstat_get_socket_info(procstat, fst, &sock, 547 NULL); 548 if (error != 0) 549 break; 550 xo_emit("{:protocol/%-3s/%s} ", 551 protocol_to_string(sock.dom_family, 552 sock.type, sock.proto)); 553 if (sock.proto == IPPROTO_TCP || 554 sock.proto == IPPROTO_SCTP || 555 sock.type == SOCK_STREAM) { 556 xo_emit("{:sendq/%u} ", sock.sendq); 557 xo_emit("{:recvq/%u} ", sock.recvq); 558 } 559 /* 560 * While generally we like to print two addresses, 561 * local and peer, for sockets, it turns out to be 562 * more useful to print the first non-nul address for 563 * local sockets, as typically they aren't bound and 564 * connected, and the path strings can get long. 565 */ 566 if (sock.dom_family == AF_LOCAL) { 567 struct sockaddr_un *sun = 568 (struct sockaddr_un *)&sock.sa_local; 569 570 if (sun->sun_path[0] != 0) 571 addr_to_string(&sock.sa_local, 572 src_addr, sizeof(src_addr)); 573 else 574 addr_to_string(&sock.sa_peer, 575 src_addr, sizeof(src_addr)); 576 xo_emit("{:path/%s}", src_addr); 577 } else { 578 addr_to_string(&sock.sa_local, src_addr, 579 sizeof(src_addr)); 580 addr_to_string(&sock.sa_peer, dst_addr, 581 sizeof(dst_addr)); 582 xo_emit("{:path/%s %s}", src_addr, dst_addr); 583 } 584 break; 585 586 default: 587 xo_emit("{:protocol/%-3s/%s} ", "-"); 588 xo_emit("{:path/%-18s/%s}", fst->fs_path != NULL ? 589 fst->fs_path : "-"); 590 } 591 592 xo_emit("\n"); 593 xo_close_instance("files"); 594 } 595 xo_close_list("files"); 596 procstat_freefiles(procstat, head); 597 } 598