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