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