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