1 /*- 2 * Copyright (c) 2007-2011 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/capability.h> 31 #include <sys/socket.h> 32 #include <sys/sysctl.h> 33 #include <sys/un.h> 34 #include <sys/user.h> 35 36 #include <netinet/in.h> 37 38 #include <arpa/inet.h> 39 40 #include <err.h> 41 #include <libprocstat.h> 42 #include <inttypes.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "procstat.h" 48 49 static const char * 50 protocol_to_string(int domain, int type, int protocol) 51 { 52 53 switch (domain) { 54 case AF_INET: 55 case AF_INET6: 56 switch (protocol) { 57 case IPPROTO_TCP: 58 return ("TCP"); 59 case IPPROTO_UDP: 60 return ("UDP"); 61 case IPPROTO_ICMP: 62 return ("ICM"); 63 case IPPROTO_RAW: 64 return ("RAW"); 65 case IPPROTO_SCTP: 66 return ("SCT"); 67 case IPPROTO_DIVERT: 68 return ("IPD"); 69 default: 70 return ("IP?"); 71 } 72 73 case AF_LOCAL: 74 switch (type) { 75 case SOCK_STREAM: 76 return ("UDS"); 77 case SOCK_DGRAM: 78 return ("UDD"); 79 default: 80 return ("UD?"); 81 } 82 default: 83 return ("?"); 84 } 85 } 86 87 static void 88 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen) 89 { 90 char buffer2[INET6_ADDRSTRLEN]; 91 struct sockaddr_in6 *sin6; 92 struct sockaddr_in *sin; 93 struct sockaddr_un *sun; 94 95 switch (ss->ss_family) { 96 case AF_LOCAL: 97 sun = (struct sockaddr_un *)ss; 98 if (strlen(sun->sun_path) == 0) 99 strlcpy(buffer, "-", buflen); 100 else 101 strlcpy(buffer, sun->sun_path, buflen); 102 break; 103 104 case AF_INET: 105 sin = (struct sockaddr_in *)ss; 106 snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr), 107 ntohs(sin->sin_port)); 108 break; 109 110 case AF_INET6: 111 sin6 = (struct sockaddr_in6 *)ss; 112 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2, 113 sizeof(buffer2)) != NULL) 114 snprintf(buffer, buflen, "%s.%d", buffer2, 115 ntohs(sin6->sin6_port)); 116 else 117 strlcpy(buffer, "-", sizeof(buffer)); 118 break; 119 120 default: 121 strlcpy(buffer, "", buflen); 122 break; 123 } 124 } 125 126 static void 127 print_address(struct sockaddr_storage *ss) 128 { 129 char addr[PATH_MAX]; 130 131 addr_to_string(ss, addr, sizeof(addr)); 132 printf("%s", addr); 133 } 134 135 static struct cap_desc { 136 cap_rights_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_MMAP, "mm" }, 143 { CAP_MAPEXEC, "me" }, 144 { CAP_FEXECVE, "fe" }, 145 { CAP_FSYNC, "fy" }, 146 { CAP_FTRUNCATE, "ft" }, 147 { CAP_SEEK, "se" }, 148 149 /* VFS methods. */ 150 { CAP_FCHFLAGS, "cf" }, 151 { CAP_FCHDIR, "cd" }, 152 { CAP_FCHMOD, "cm" }, 153 { CAP_FCHOWN, "cn" }, 154 { CAP_FCNTL, "fc" }, 155 { CAP_FPATHCONF, "fp" }, 156 { CAP_FLOCK, "fl" }, 157 { CAP_FSCK, "fk" }, 158 { CAP_FSTAT, "fs" }, 159 { CAP_FSTATFS, "sf" }, 160 { CAP_FUTIMES, "fu" }, 161 { CAP_CREATE, "cr" }, 162 { CAP_DELETE, "de" }, 163 { CAP_MKDIR, "md" }, 164 { CAP_RMDIR, "rm" }, 165 { CAP_MKFIFO, "mf" }, 166 167 /* Lookups - used to constraint *at() calls. */ 168 { CAP_LOOKUP, "lo" }, 169 170 /* Extended attributes. */ 171 { CAP_EXTATTR_GET, "eg" }, 172 { CAP_EXTATTR_SET, "es" }, 173 { CAP_EXTATTR_DELETE, "ed" }, 174 { CAP_EXTATTR_LIST, "el" }, 175 176 /* Access Control Lists. */ 177 { CAP_ACL_GET, "ag" }, 178 { CAP_ACL_SET, "as" }, 179 { CAP_ACL_DELETE, "ad" }, 180 { CAP_ACL_CHECK, "ac" }, 181 182 /* Socket operations. */ 183 { CAP_ACCEPT, "at" }, 184 { CAP_BIND, "bd" }, 185 { CAP_CONNECT, "co" }, 186 { CAP_GETPEERNAME, "pn" }, 187 { CAP_GETSOCKNAME, "sn" }, 188 { CAP_GETSOCKOPT, "gs" }, 189 { CAP_LISTEN, "ln" }, 190 { CAP_PEELOFF, "pf" }, 191 { CAP_SETSOCKOPT, "ss" }, 192 { CAP_SHUTDOWN, "sh" }, 193 194 /* Mandatory Access Control. */ 195 { CAP_MAC_GET, "mg" }, 196 { CAP_MAC_SET, "ms" }, 197 198 /* Methods on semaphores. */ 199 { CAP_SEM_GETVALUE, "sg" }, 200 { CAP_SEM_POST, "sp" }, 201 { CAP_SEM_WAIT, "sw" }, 202 203 /* Event monitoring and posting. */ 204 { CAP_POLL_EVENT, "po" }, 205 { CAP_POST_EVENT, "ev" }, 206 207 /* Strange and powerful rights that should not be given lightly. */ 208 { CAP_IOCTL, "io" }, 209 { CAP_TTYHOOK, "ty" }, 210 211 #ifdef NOTYET 212 { CAP_PDGETPID, "pg" }, 213 { CAP_PDWAIT4, "pw" }, 214 { CAP_PDKILL, "pk" }, 215 #endif 216 }; 217 static const u_int cap_desc_count = sizeof(cap_desc) / 218 sizeof(cap_desc[0]); 219 220 static u_int 221 width_capability(cap_rights_t rights) 222 { 223 u_int count, i, width; 224 225 count = 0; 226 width = 0; 227 for (i = 0; i < cap_desc_count; i++) { 228 if (rights & cap_desc[i].cd_right) { 229 width += strlen(cap_desc[i].cd_desc); 230 if (count) 231 width++; 232 count++; 233 } 234 } 235 return (width); 236 } 237 238 static void 239 print_capability(cap_rights_t rights, u_int capwidth) 240 { 241 u_int count, i, width; 242 243 count = 0; 244 width = 0; 245 for (i = width_capability(rights); i < capwidth; i++) { 246 if (rights || i != 0) 247 printf(" "); 248 else 249 printf("-"); 250 } 251 for (i = 0; i < cap_desc_count; i++) { 252 if (rights & cap_desc[i].cd_right) { 253 printf("%s%s", count ? "," : "", cap_desc[i].cd_desc); 254 width += strlen(cap_desc[i].cd_desc); 255 if (count) 256 width++; 257 count++; 258 } 259 } 260 } 261 262 void 263 procstat_files(struct procstat *procstat, struct kinfo_proc *kipp) 264 { 265 struct sockstat sock; 266 struct filestat_list *head; 267 struct filestat *fst; 268 const char *str; 269 struct vnstat vn; 270 u_int capwidth, width; 271 int error; 272 273 /* 274 * To print the header in capability mode, we need to know the width 275 * of the widest capability string. Even if we get no processes 276 * back, we will print the header, so we defer aborting due to a lack 277 * of processes until after the header logic. 278 */ 279 capwidth = 0; 280 head = procstat_getfiles(procstat, kipp, 0); 281 if (head != NULL && Cflag) { 282 STAILQ_FOREACH(fst, head, next) { 283 width = width_capability(fst->fs_cap_rights); 284 if (width > capwidth) 285 capwidth = width; 286 } 287 if (capwidth < strlen("CAPABILITIES")) 288 capwidth = strlen("CAPABILITIES"); 289 } 290 291 if (!hflag) { 292 if (Cflag) 293 printf("%5s %-16s %4s %1s %-9s %-*s " 294 "%-3s %-12s\n", "PID", "COMM", "FD", "T", 295 "FLAGS", capwidth, "CAPABILITIES", "PRO", 296 "NAME"); 297 else 298 printf("%5s %-16s %4s %1s %1s %-9s " 299 "%3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T", 300 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME"); 301 } 302 303 if (head == NULL) 304 return; 305 STAILQ_FOREACH(fst, head, next) { 306 printf("%5d ", kipp->ki_pid); 307 printf("%-16s ", kipp->ki_comm); 308 if (fst->fs_uflags & PS_FST_UFLAG_CTTY) 309 printf("ctty "); 310 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR) 311 printf(" cwd "); 312 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL) 313 printf("jail "); 314 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR) 315 printf("root "); 316 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT) 317 printf("text "); 318 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE) 319 printf("trace "); 320 else 321 printf("%4d ", fst->fs_fd); 322 323 switch (fst->fs_type) { 324 case PS_FST_TYPE_VNODE: 325 str = "v"; 326 break; 327 328 case PS_FST_TYPE_SOCKET: 329 str = "s"; 330 break; 331 332 case PS_FST_TYPE_PIPE: 333 str = "p"; 334 break; 335 336 case PS_FST_TYPE_FIFO: 337 str = "f"; 338 break; 339 340 case PS_FST_TYPE_KQUEUE: 341 str = "k"; 342 break; 343 344 case PS_FST_TYPE_CRYPTO: 345 str = "c"; 346 break; 347 348 case PS_FST_TYPE_MQUEUE: 349 str = "m"; 350 break; 351 352 case PS_FST_TYPE_SHM: 353 str = "h"; 354 break; 355 356 case PS_FST_TYPE_PTS: 357 str = "t"; 358 break; 359 360 case PS_FST_TYPE_SEM: 361 str = "e"; 362 break; 363 364 case PS_FST_TYPE_NONE: 365 case PS_FST_TYPE_UNKNOWN: 366 default: 367 str = "?"; 368 break; 369 } 370 printf("%1s ", str); 371 if (!Cflag) { 372 str = "-"; 373 if (fst->fs_type == PS_FST_TYPE_VNODE) { 374 error = procstat_get_vnode_info(procstat, fst, 375 &vn, NULL); 376 switch (vn.vn_type) { 377 case PS_FST_VTYPE_VREG: 378 str = "r"; 379 break; 380 381 case PS_FST_VTYPE_VDIR: 382 str = "d"; 383 break; 384 385 case PS_FST_VTYPE_VBLK: 386 str = "b"; 387 break; 388 389 case PS_FST_VTYPE_VCHR: 390 str = "c"; 391 break; 392 393 case PS_FST_VTYPE_VLNK: 394 str = "l"; 395 break; 396 397 case PS_FST_VTYPE_VSOCK: 398 str = "s"; 399 break; 400 401 case PS_FST_VTYPE_VFIFO: 402 str = "f"; 403 break; 404 405 case PS_FST_VTYPE_VBAD: 406 str = "x"; 407 break; 408 409 case PS_FST_VTYPE_VNON: 410 case PS_FST_VTYPE_UNKNOWN: 411 default: 412 str = "?"; 413 break; 414 } 415 } 416 printf("%1s ", str); 417 } 418 printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-"); 419 printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-"); 420 printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-"); 421 printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-"); 422 printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-"); 423 printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-"); 424 printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-"); 425 printf("%s", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-"); 426 printf("%s ", fst->fs_fflags & PS_FST_FFLAG_CAPABILITY ? 427 "c" : "-"); 428 if (!Cflag) { 429 if (fst->fs_ref_count > -1) 430 printf("%3d ", fst->fs_ref_count); 431 else 432 printf("%3c ", '-'); 433 if (fst->fs_offset > -1) 434 printf("%7jd ", (intmax_t)fst->fs_offset); 435 else 436 printf("%7c ", '-'); 437 } 438 if (Cflag) { 439 print_capability(fst->fs_cap_rights, capwidth); 440 printf(" "); 441 } 442 switch (fst->fs_type) { 443 case PS_FST_TYPE_VNODE: 444 case PS_FST_TYPE_FIFO: 445 case PS_FST_TYPE_PTS: 446 printf("%-3s ", "-"); 447 printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-"); 448 break; 449 450 case PS_FST_TYPE_SOCKET: 451 error = procstat_get_socket_info(procstat, fst, &sock, NULL); 452 if (error != 0) 453 break; 454 printf("%-3s ", 455 protocol_to_string(sock.dom_family, 456 sock.type, sock.proto)); 457 /* 458 * While generally we like to print two addresses, 459 * local and peer, for sockets, it turns out to be 460 * more useful to print the first non-nul address for 461 * local sockets, as typically they aren't bound and 462 * connected, and the path strings can get long. 463 */ 464 if (sock.dom_family == AF_LOCAL) { 465 struct sockaddr_un *sun = 466 (struct sockaddr_un *)&sock.sa_local; 467 468 if (sun->sun_path[0] != 0) 469 print_address(&sock.sa_local); 470 else 471 print_address(&sock.sa_peer); 472 } else { 473 print_address(&sock.sa_local); 474 printf(" "); 475 print_address(&sock.sa_peer); 476 } 477 break; 478 479 default: 480 printf("%-18s", "-"); 481 } 482 483 printf("\n"); 484 } 485 } 486