1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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, width; 271 272 count = 0; 273 width = 0; 274 for (i = width_capability(rightsp); i < capwidth; i++) { 275 if (i != 0) 276 xo_emit(" "); 277 else 278 xo_emit("-"); 279 } 280 xo_open_list("capabilities"); 281 for (i = 0; i < cap_desc_count; i++) { 282 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) { 283 xo_emit("{D:/%s}{l:capabilities/%s}", count ? "," : "", 284 cap_desc[i].cd_desc); 285 width += strlen(cap_desc[i].cd_desc); 286 if (count) 287 width++; 288 count++; 289 } 290 } 291 xo_close_list("capabilities"); 292 } 293 294 void 295 procstat_files(struct procstat *procstat, struct kinfo_proc *kipp) 296 { 297 struct sockstat sock; 298 struct filestat_list *head; 299 struct filestat *fst; 300 const char *str; 301 struct vnstat vn; 302 u_int capwidth, width; 303 int error; 304 char src_addr[PATH_MAX]; 305 char dst_addr[PATH_MAX]; 306 307 /* 308 * To print the header in capability mode, we need to know the width 309 * of the widest capability string. Even if we get no processes 310 * back, we will print the header, so we defer aborting due to a lack 311 * of processes until after the header logic. 312 */ 313 capwidth = 0; 314 head = procstat_getfiles(procstat, kipp, 0); 315 if (head != NULL && 316 (procstat_opts & PS_OPT_CAPABILITIES) != 0) { 317 STAILQ_FOREACH(fst, head, next) { 318 width = width_capability(&fst->fs_cap_rights); 319 if (width > capwidth) 320 capwidth = width; 321 } 322 if (capwidth < strlen("CAPABILITIES")) 323 capwidth = strlen("CAPABILITIES"); 324 } 325 326 if ((procstat_opts & PS_OPT_NOHEADER) == 0) { 327 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0) 328 xo_emit("{T:/%5s %-16s %5s %1s %-8s %-*s " 329 "%-3s %-12s}\n", "PID", "COMM", "FD", "T", 330 "FLAGS", capwidth, "CAPABILITIES", "PRO", 331 "NAME"); 332 else 333 xo_emit("{T:/%5s %-16s %5s %1s %1s %-8s " 334 "%3s %7s %-3s %-12s}\n", "PID", "COMM", "FD", "T", 335 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME"); 336 } 337 338 if (head == NULL) 339 return; 340 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 341 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 342 xo_open_list("files"); 343 STAILQ_FOREACH(fst, head, next) { 344 xo_open_instance("files"); 345 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 346 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 347 if (fst->fs_uflags & PS_FST_UFLAG_CTTY) 348 xo_emit("{P: }{:fd/%s} ", "ctty"); 349 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR) 350 xo_emit("{P: }{:fd/%s} ", "cwd"); 351 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL) 352 xo_emit("{P: }{:fd/%s} ", "jail"); 353 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR) 354 xo_emit("{P: }{:fd/%s} ", "root"); 355 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT) 356 xo_emit("{P: }{:fd/%s} ", "text"); 357 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE) 358 xo_emit("{:fd/%s} ", "trace"); 359 else 360 xo_emit("{:fd/%5d} ", fst->fs_fd); 361 362 switch (fst->fs_type) { 363 case PS_FST_TYPE_VNODE: 364 str = "v"; 365 xo_emit("{eq:fd_type/vnode}"); 366 break; 367 368 case PS_FST_TYPE_SOCKET: 369 str = "s"; 370 xo_emit("{eq:fd_type/socket}"); 371 break; 372 373 case PS_FST_TYPE_PIPE: 374 str = "p"; 375 xo_emit("{eq:fd_type/pipe}"); 376 break; 377 378 case PS_FST_TYPE_FIFO: 379 str = "f"; 380 xo_emit("{eq:fd_type/fifo}"); 381 break; 382 383 case PS_FST_TYPE_KQUEUE: 384 str = "k"; 385 xo_emit("{eq:fd_type/kqueue}"); 386 break; 387 388 case PS_FST_TYPE_MQUEUE: 389 str = "m"; 390 xo_emit("{eq:fd_type/mqueue}"); 391 break; 392 393 case PS_FST_TYPE_SHM: 394 str = "h"; 395 xo_emit("{eq:fd_type/shm}"); 396 break; 397 398 case PS_FST_TYPE_PTS: 399 str = "t"; 400 xo_emit("{eq:fd_type/pts}"); 401 break; 402 403 case PS_FST_TYPE_SEM: 404 str = "e"; 405 xo_emit("{eq:fd_type/sem}"); 406 break; 407 408 case PS_FST_TYPE_PROCDESC: 409 str = "P"; 410 xo_emit("{eq:fd_type/procdesc}"); 411 break; 412 413 case PS_FST_TYPE_DEV: 414 str = "D"; 415 xo_emit("{eq:fd_type/dev}"); 416 break; 417 418 case PS_FST_TYPE_EVENTFD: 419 str = "E"; 420 xo_emit("{eq:fd_type/eventfd}"); 421 break; 422 423 case PS_FST_TYPE_NONE: 424 str = "?"; 425 xo_emit("{eq:fd_type/none}"); 426 break; 427 428 case PS_FST_TYPE_UNKNOWN: 429 default: 430 str = "?"; 431 xo_emit("{eq:fd_type/unknown}"); 432 break; 433 } 434 xo_emit("{d:fd_type/%1s/%s} ", str); 435 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) { 436 str = "-"; 437 if (fst->fs_type == PS_FST_TYPE_VNODE) { 438 error = procstat_get_vnode_info(procstat, fst, 439 &vn, NULL); 440 switch (vn.vn_type) { 441 case PS_FST_VTYPE_VREG: 442 str = "r"; 443 xo_emit("{eq:vode_type/regular}"); 444 break; 445 446 case PS_FST_VTYPE_VDIR: 447 str = "d"; 448 xo_emit("{eq:vode_type/directory}"); 449 break; 450 451 case PS_FST_VTYPE_VBLK: 452 str = "b"; 453 xo_emit("{eq:vode_type/block}"); 454 break; 455 456 case PS_FST_VTYPE_VCHR: 457 str = "c"; 458 xo_emit("{eq:vode_type/character}"); 459 break; 460 461 case PS_FST_VTYPE_VLNK: 462 str = "l"; 463 xo_emit("{eq:vode_type/link}"); 464 break; 465 466 case PS_FST_VTYPE_VSOCK: 467 str = "s"; 468 xo_emit("{eq:vode_type/socket}"); 469 break; 470 471 case PS_FST_VTYPE_VFIFO: 472 str = "f"; 473 xo_emit("{eq:vode_type/fifo}"); 474 break; 475 476 case PS_FST_VTYPE_VBAD: 477 str = "x"; 478 xo_emit("{eq:vode_type/revoked_device}"); 479 break; 480 481 case PS_FST_VTYPE_VNON: 482 str = "?"; 483 xo_emit("{eq:vode_type/non}"); 484 break; 485 486 case PS_FST_VTYPE_UNKNOWN: 487 default: 488 str = "?"; 489 xo_emit("{eq:vode_type/unknown}"); 490 break; 491 } 492 } 493 xo_emit("{d:vnode_type/%1s/%s} ", str); 494 } 495 496 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_READ ? 497 "r" : "-"); 498 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_WRITE ? 499 "w" : "-"); 500 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_APPEND ? 501 "a" : "-"); 502 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? 503 "s" : "-"); 504 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_SYNC ? 505 "f" : "-"); 506 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? 507 "n" : "-"); 508 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? 509 "d" : "-"); 510 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? 511 "l" : "-"); 512 xo_emit(" "); 513 xo_open_list("fd_flags"); 514 if (fst->fs_fflags & PS_FST_FFLAG_READ) 515 xo_emit("{elq:fd_flags/read}"); 516 if (fst->fs_fflags & PS_FST_FFLAG_WRITE) 517 xo_emit("{elq:fd_flags/write}"); 518 if (fst->fs_fflags & PS_FST_FFLAG_APPEND) 519 xo_emit("{elq:fd_flags/append}"); 520 if (fst->fs_fflags & PS_FST_FFLAG_ASYNC) 521 xo_emit("{elq:fd_flags/async}"); 522 if (fst->fs_fflags & PS_FST_FFLAG_SYNC) 523 xo_emit("{elq:fd_flags/fsync}"); 524 if (fst->fs_fflags & PS_FST_FFLAG_NONBLOCK) 525 xo_emit("{elq:fd_flags/nonblocking}"); 526 if (fst->fs_fflags & PS_FST_FFLAG_DIRECT) 527 xo_emit("{elq:fd_flags/direct_io}"); 528 if (fst->fs_fflags & PS_FST_FFLAG_HASLOCK) 529 xo_emit("{elq:fd_flags/lock_held}"); 530 xo_close_list("fd_flags"); 531 532 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) { 533 if (fst->fs_ref_count > -1) 534 xo_emit("{:ref_count/%3d/%d} ", 535 fst->fs_ref_count); 536 else 537 xo_emit("{q:ref_count/%3c/%c} ", '-'); 538 if (fst->fs_offset > -1) 539 xo_emit("{:offset/%7jd/%jd} ", 540 (intmax_t)fst->fs_offset); 541 else 542 xo_emit("{q:offset/%7c/%c} ", '-'); 543 } 544 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0) { 545 print_capability(&fst->fs_cap_rights, capwidth); 546 xo_emit(" "); 547 } 548 switch (fst->fs_type) { 549 case PS_FST_TYPE_SOCKET: 550 error = procstat_get_socket_info(procstat, fst, &sock, 551 NULL); 552 if (error != 0) 553 break; 554 xo_emit("{:protocol/%-3s/%s} ", 555 protocol_to_string(sock.dom_family, 556 sock.type, sock.proto)); 557 if (sock.proto == IPPROTO_TCP || 558 sock.proto == IPPROTO_SCTP || 559 sock.type == SOCK_STREAM) { 560 xo_emit("{:sendq/%u} ", sock.sendq); 561 xo_emit("{:recvq/%u} ", sock.recvq); 562 } 563 /* 564 * While generally we like to print two addresses, 565 * local and peer, for sockets, it turns out to be 566 * more useful to print the first non-nul address for 567 * local sockets, as typically they aren't bound and 568 * connected, and the path strings can get long. 569 */ 570 if (sock.dom_family == AF_LOCAL) { 571 struct sockaddr_un *sun = 572 (struct sockaddr_un *)&sock.sa_local; 573 574 if (sun->sun_path[0] != 0) 575 addr_to_string(&sock.sa_local, 576 src_addr, sizeof(src_addr)); 577 else 578 addr_to_string(&sock.sa_peer, 579 src_addr, sizeof(src_addr)); 580 xo_emit("{:path/%s}", src_addr); 581 } else { 582 addr_to_string(&sock.sa_local, src_addr, 583 sizeof(src_addr)); 584 addr_to_string(&sock.sa_peer, dst_addr, 585 sizeof(dst_addr)); 586 xo_emit("{:path/%s %s}", src_addr, dst_addr); 587 } 588 break; 589 590 default: 591 xo_emit("{:protocol/%-3s/%s} ", "-"); 592 xo_emit("{:path/%-18s/%s}", fst->fs_path != NULL ? 593 fst->fs_path : "-"); 594 } 595 596 xo_emit("\n"); 597 xo_close_instance("files"); 598 } 599 xo_close_list("files"); 600 procstat_freefiles(procstat, head); 601 } 602