1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <unistd.h> 32 #include <fcntl.h> 33 #include <ctype.h> 34 #include <string.h> 35 #include <signal.h> 36 #include <errno.h> 37 #include <dirent.h> 38 #include <limits.h> 39 #include <door.h> 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <sys/stat.h> 43 #include <sys/mman.h> 44 #include <sys/mkdev.h> 45 #include <sys/un.h> 46 #include <netdb.h> 47 #include <libproc.h> 48 #include <netinet/in.h> 49 #include <arpa/inet.h> 50 #include <netdb.h> 51 52 #define copyflock(dst, src) \ 53 (dst).l_type = (src).l_type; \ 54 (dst).l_whence = (src).l_whence; \ 55 (dst).l_start = (src).l_start; \ 56 (dst).l_len = (src).l_len; \ 57 (dst).l_sysid = (src).l_sysid; \ 58 (dst).l_pid = (src).l_pid; 59 60 static char *command; 61 static volatile int interrupt; 62 static int Fflag; 63 static boolean_t nflag = B_FALSE; 64 65 static void intr(int); 66 static void dofcntl(struct ps_prochandle *, int, int, int); 67 static void dosocket(struct ps_prochandle *, int); 68 static void show_files(struct ps_prochandle *); 69 static void show_fileflags(int); 70 static void show_door(struct ps_prochandle *, int); 71 static int getflock(struct ps_prochandle *, int, struct flock *); 72 73 int 74 main(int argc, char **argv) 75 { 76 int retc = 0; 77 int opt; 78 int errflg = 0; 79 struct ps_prochandle *Pr; 80 81 if ((command = strrchr(argv[0], '/')) != NULL) 82 command++; 83 else 84 command = argv[0]; 85 86 /* options */ 87 while ((opt = getopt(argc, argv, "Fn")) != EOF) { 88 switch (opt) { 89 case 'F': /* force grabbing (no O_EXCL) */ 90 Fflag = PGRAB_FORCE; 91 break; 92 case 'n': 93 nflag = B_TRUE; 94 break; 95 default: 96 errflg = 1; 97 break; 98 } 99 } 100 101 argc -= optind; 102 argv += optind; 103 104 if (errflg || argc <= 0) { 105 (void) fprintf(stderr, "usage:\t%s [-F] pid ...\n", 106 command); 107 (void) fprintf(stderr, 108 " (report open files of each process)\n"); 109 (void) fprintf(stderr, 110 " -F: force grabbing of the target process\n"); 111 exit(2); 112 } 113 114 /* catch signals from terminal */ 115 if (sigset(SIGHUP, SIG_IGN) == SIG_DFL) 116 (void) sigset(SIGHUP, intr); 117 if (sigset(SIGINT, SIG_IGN) == SIG_DFL) 118 (void) sigset(SIGINT, intr); 119 if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL) 120 (void) sigset(SIGQUIT, intr); 121 (void) sigset(SIGPIPE, intr); 122 (void) sigset(SIGTERM, intr); 123 124 (void) proc_initstdio(); 125 126 127 while (--argc >= 0 && !interrupt) { 128 char *arg; 129 psinfo_t psinfo; 130 pid_t pid; 131 int gret; 132 133 (void) proc_flushstdio(); 134 135 /* get the specified pid and the psinfo struct */ 136 if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS, 137 &psinfo, &gret)) == -1) { 138 (void) fprintf(stderr, "%s: cannot examine %s: %s\n", 139 command, arg, Pgrab_error(gret)); 140 retc++; 141 } else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) { 142 if (Pcreate_agent(Pr) == 0) { 143 proc_unctrl_psinfo(&psinfo); 144 (void) printf("%d:\t%.70s\n", 145 (int)pid, psinfo.pr_psargs); 146 show_files(Pr); 147 Pdestroy_agent(Pr); 148 } else { 149 (void) fprintf(stderr, 150 "%s: cannot control process %d\n", 151 command, (int)pid); 152 retc++; 153 } 154 Prelease(Pr, 0); 155 Pr = NULL; 156 } else { 157 switch (gret) { 158 case G_SYS: 159 case G_SELF: 160 proc_unctrl_psinfo(&psinfo); 161 (void) printf("%d:\t%.70s\n", (int)pid, 162 psinfo.pr_psargs); 163 if (gret == G_SYS) 164 (void) printf(" [system process]\n"); 165 else 166 show_files(NULL); 167 break; 168 default: 169 (void) fprintf(stderr, "%s: %s: %d\n", 170 command, Pgrab_error(gret), (int)pid); 171 retc++; 172 break; 173 } 174 } 175 176 177 } 178 179 (void) proc_finistdio(); 180 181 if (interrupt && retc == 0) 182 retc++; 183 return (retc); 184 } 185 186 /* ARGSUSED */ 187 static void 188 intr(int sig) 189 { 190 interrupt = 1; 191 } 192 193 /* ------ begin specific code ------ */ 194 195 static void 196 show_files(struct ps_prochandle *Pr) 197 { 198 DIR *dirp; 199 struct dirent *dentp; 200 char pname[100]; 201 char fname[PATH_MAX]; 202 struct stat64 statb; 203 struct rlimit rlim; 204 pid_t pid; 205 int fd; 206 char *s; 207 int ret; 208 209 if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) { 210 ulong_t nfd = rlim.rlim_cur; 211 if (nfd == RLIM_INFINITY) 212 (void) printf( 213 " Current rlimit: unlimited file descriptors\n"); 214 else 215 (void) printf( 216 " Current rlimit: %lu file descriptors\n", nfd); 217 } 218 219 /* in case we are doing this to ourself */ 220 pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid; 221 222 (void) sprintf(pname, "/proc/%d/fd", (int)pid); 223 if ((dirp = opendir(pname)) == NULL) { 224 (void) fprintf(stderr, "%s: cannot open directory %s\n", 225 command, pname); 226 return; 227 } 228 229 /* for each open file --- */ 230 while ((dentp = readdir(dirp)) != NULL && !interrupt) { 231 char unknown[12]; 232 dev_t rdev; 233 234 /* skip '.' and '..' */ 235 if (!isdigit(dentp->d_name[0])) 236 continue; 237 238 fd = atoi(dentp->d_name); 239 if (pr_fstat64(Pr, fd, &statb) == -1) { 240 s = unknown; 241 (void) sprintf(s, "%4d", fd); 242 perror(s); 243 continue; 244 } 245 246 rdev = NODEV; 247 switch (statb.st_mode & S_IFMT) { 248 case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break; 249 case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break; 250 case S_IFIFO: s = "S_IFIFO"; break; 251 case S_IFDIR: s = "S_IFDIR"; break; 252 case S_IFREG: s = "S_IFREG"; break; 253 case S_IFLNK: s = "S_IFLNK"; break; 254 case S_IFSOCK: s = "S_IFSOCK"; break; 255 case S_IFDOOR: s = "S_IFDOOR"; break; 256 case S_IFPORT: s = "S_IFPORT"; break; 257 default: 258 s = unknown; 259 (void) sprintf(s, "0x%.4x ", 260 (int)statb.st_mode & S_IFMT); 261 break; 262 } 263 264 (void) printf("%4d: %s mode:0%.3o", 265 fd, 266 s, 267 (int)statb.st_mode & ~S_IFMT); 268 269 if (major(statb.st_dev) != (major_t)NODEV && 270 minor(statb.st_dev) != (minor_t)NODEV) 271 (void) printf(" dev:%lu,%lu", 272 (ulong_t)major(statb.st_dev), 273 (ulong_t)minor(statb.st_dev)); 274 else 275 (void) printf(" dev:0x%.8lX", (long)statb.st_dev); 276 277 if ((statb.st_mode & S_IFMT) == S_IFPORT) { 278 (void) printf(" uid:%d gid:%d", 279 (int)statb.st_uid, 280 (int)statb.st_gid); 281 (void) printf(" size:%lld\n", 282 (longlong_t)statb.st_size); 283 continue; 284 } 285 286 (void) printf(" ino:%llu uid:%d gid:%d", 287 (u_longlong_t)statb.st_ino, 288 (int)statb.st_uid, 289 (int)statb.st_gid); 290 291 if (rdev == NODEV) 292 (void) printf(" size:%lld\n", 293 (longlong_t)statb.st_size); 294 else if (major(rdev) != (major_t)NODEV && 295 minor(rdev) != (minor_t)NODEV) 296 (void) printf(" rdev:%lu,%lu\n", 297 (ulong_t)major(rdev), 298 (ulong_t)minor(rdev)); 299 else 300 (void) printf(" rdev:0x%.8lX\n", (long)rdev); 301 302 if (!nflag) { 303 dofcntl(Pr, fd, 304 (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP)) 305 == (S_IFREG|S_ENFMT), 306 (statb.st_mode & S_IFMT) == S_IFDOOR); 307 308 if ((statb.st_mode & S_IFMT) == S_IFSOCK) 309 dosocket(Pr, fd); 310 311 (void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd); 312 313 if ((ret = readlink(pname, fname, PATH_MAX - 1)) > 0) { 314 fname[ret] = '\0'; 315 (void) printf(" %s\n", fname); 316 } 317 } 318 } 319 320 (void) closedir(dirp); 321 } 322 323 324 static int 325 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native) 326 { 327 int ret; 328 #ifdef _LP64 329 struct flock64_32 flock_target; 330 331 if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) { 332 copyflock(flock_target, *flock_native); 333 ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target); 334 copyflock(*flock_native, flock_target); 335 return (ret); 336 } 337 #endif /* _LP64 */ 338 ret = pr_fcntl(Pr, fd, F_GETLK, flock_native); 339 return (ret); 340 } 341 342 /* examine open file with fcntl() */ 343 static void 344 dofcntl(struct ps_prochandle *Pr, int fd, int manditory, int isdoor) 345 { 346 struct flock flock; 347 int fileflags; 348 int fdflags; 349 350 fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0); 351 fdflags = pr_fcntl(Pr, fd, F_GETFD, 0); 352 353 if (fileflags != -1 || fdflags != -1) { 354 (void) printf(" "); 355 if (fileflags != -1) 356 show_fileflags(fileflags); 357 if (fdflags != -1 && (fdflags & FD_CLOEXEC)) 358 (void) printf(" FD_CLOEXEC"); 359 if (isdoor) 360 show_door(Pr, fd); 361 (void) fputc('\n', stdout); 362 } else if (isdoor) { 363 (void) printf(" "); 364 show_door(Pr, fd); 365 (void) fputc('\n', stdout); 366 } 367 368 flock.l_type = F_WRLCK; 369 flock.l_whence = 0; 370 flock.l_start = 0; 371 flock.l_len = 0; 372 flock.l_sysid = 0; 373 flock.l_pid = 0; 374 if (getflock(Pr, fd, &flock) != -1) { 375 if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) { 376 unsigned long sysid = flock.l_sysid; 377 378 (void) printf(" %s %s lock set by", 379 manditory? "manditory" : "advisory", 380 flock.l_type == F_RDLCK? "read" : "write"); 381 if (sysid) 382 (void) printf(" system 0x%lX", sysid); 383 if (flock.l_pid) 384 (void) printf(" process %d", (int)flock.l_pid); 385 (void) fputc('\n', stdout); 386 } 387 } 388 } 389 390 #ifdef O_PRIV 391 #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 392 O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 393 O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 394 #else 395 #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 396 O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 397 O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 398 #endif 399 400 static void 401 show_fileflags(int flags) 402 { 403 char buffer[136]; 404 char *str = buffer; 405 406 switch (flags & O_ACCMODE) { 407 case O_RDONLY: 408 (void) strcpy(str, "O_RDONLY"); 409 break; 410 case O_WRONLY: 411 (void) strcpy(str, "O_WRONLY"); 412 break; 413 case O_RDWR: 414 (void) strcpy(str, "O_RDWR"); 415 break; 416 default: 417 (void) sprintf(str, "0x%x", flags & O_ACCMODE); 418 break; 419 } 420 421 if (flags & O_NDELAY) 422 (void) strcat(str, "|O_NDELAY"); 423 if (flags & O_NONBLOCK) 424 (void) strcat(str, "|O_NONBLOCK"); 425 if (flags & O_APPEND) 426 (void) strcat(str, "|O_APPEND"); 427 #ifdef O_PRIV 428 if (flags & O_PRIV) 429 (void) strcat(str, "|O_PRIV"); 430 #endif 431 if (flags & O_SYNC) 432 (void) strcat(str, "|O_SYNC"); 433 if (flags & O_DSYNC) 434 (void) strcat(str, "|O_DSYNC"); 435 if (flags & O_RSYNC) 436 (void) strcat(str, "|O_RSYNC"); 437 if (flags & O_CREAT) 438 (void) strcat(str, "|O_CREAT"); 439 if (flags & O_TRUNC) 440 (void) strcat(str, "|O_TRUNC"); 441 if (flags & O_EXCL) 442 (void) strcat(str, "|O_EXCL"); 443 if (flags & O_NOCTTY) 444 (void) strcat(str, "|O_NOCTTY"); 445 if (flags & O_LARGEFILE) 446 (void) strcat(str, "|O_LARGEFILE"); 447 if (flags & O_XATTR) 448 (void) strcat(str, "|O_XATTR"); 449 if (flags & ~(ALL_O_FLAGS)) 450 (void) sprintf(str + strlen(str), "|0x%x", 451 flags & ~(ALL_O_FLAGS)); 452 453 (void) printf("%s", str); 454 } 455 456 /* show door info */ 457 static void 458 show_door(struct ps_prochandle *Pr, int fd) 459 { 460 door_info_t door_info; 461 psinfo_t psinfo; 462 463 if (pr_door_info(Pr, fd, &door_info) != 0) 464 return; 465 466 if (proc_get_psinfo(door_info.di_target, &psinfo) != 0) 467 psinfo.pr_fname[0] = '\0'; 468 469 (void) printf(" door to "); 470 if (psinfo.pr_fname[0] != '\0') 471 (void) printf("%s[%d]", psinfo.pr_fname, 472 (int)door_info.di_target); 473 else 474 (void) printf("pid %d", (int)door_info.di_target); 475 } 476 477 static void 478 show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len) 479 { 480 /* LINTED pointer assignment */ 481 struct sockaddr_in *so_in = (struct sockaddr_in *)sa; 482 /* LINTED pointer assignment */ 483 struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)sa; 484 struct sockaddr_un *so_un = (struct sockaddr_un *)sa; 485 char abuf[INET6_ADDRSTRLEN]; 486 const char *p; 487 488 switch (sa->sa_family) { 489 default: 490 return; 491 case AF_INET: 492 (void) printf("\t%s: AF_INET %s port: %u\n", 493 str, 494 inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)), 495 ntohs(so_in->sin_port)); 496 return; 497 case AF_INET6: 498 (void) printf("\t%s: AF_INET6 %s port: %u\n", 499 str, 500 inet_ntop(AF_INET6, &so_in6->sin6_addr, 501 abuf, sizeof (abuf)), 502 ntohs(so_in->sin_port)); 503 return; 504 case AF_UNIX: 505 if (len >= sizeof (so_un->sun_family)) { 506 /* Null terminate */ 507 len -= sizeof (so_un->sun_family); 508 so_un->sun_path[len] = NULL; 509 (void) printf("\t%s: AF_UNIX %s\n", 510 str, so_un->sun_path); 511 } 512 return; 513 case AF_IMPLINK: p = "AF_IMPLINK"; break; 514 case AF_PUP: p = "AF_PUP"; break; 515 case AF_CHAOS: p = "AF_CHAOS"; break; 516 case AF_NS: p = "AF_NS"; break; 517 case AF_NBS: p = "AF_NBS"; break; 518 case AF_ECMA: p = "AF_ECMA"; break; 519 case AF_DATAKIT: p = "AF_DATAKIT"; break; 520 case AF_CCITT: p = "AF_CCITT"; break; 521 case AF_SNA: p = "AF_SNA"; break; 522 case AF_DECnet: p = "AF_DECnet"; break; 523 case AF_DLI: p = "AF_DLI"; break; 524 case AF_LAT: p = "AF_LAT"; break; 525 case AF_HYLINK: p = "AF_HYLINK"; break; 526 case AF_APPLETALK: p = "AF_APPLETALK"; break; 527 case AF_NIT: p = "AF_NIT"; break; 528 case AF_802: p = "AF_802"; break; 529 case AF_OSI: p = "AF_OSI"; break; 530 case AF_X25: p = "AF_X25"; break; 531 case AF_OSINET: p = "AF_OSINET"; break; 532 case AF_GOSIP: p = "AF_GOSIP"; break; 533 case AF_IPX: p = "AF_IPX"; break; 534 case AF_ROUTE: p = "AF_ROUTE"; break; 535 case AF_LINK: p = "AF_LINK"; break; 536 } 537 538 (void) printf("\t%s: %s\n", str, p); 539 } 540 541 static void 542 show_socktype(uint_t type) 543 { 544 static const char *types[] = { 545 NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET" 546 }; 547 548 if (type < sizeof (types) / sizeof (*types) && types[type] != NULL) 549 (void) printf("\tSOCK_%s\n", types[type]); 550 else 551 (void) printf("\tunknown socket type %u\n", type); 552 } 553 554 #define BUFSIZE 200 555 static void 556 show_sockopts(struct ps_prochandle *Pr, int fd) 557 { 558 int val, vlen; 559 char buf[BUFSIZE]; 560 char buf1[32]; 561 char ipaddr[INET_ADDRSTRLEN]; 562 int i; 563 in_addr_t nexthop_val; 564 struct boolopt { 565 int opt; 566 const char *name; 567 }; 568 static struct boolopt boolopts[] = { 569 { SO_DEBUG, "SO_DEBUG," }, 570 { SO_REUSEADDR, "SO_REUSEADDR," }, 571 { SO_KEEPALIVE, "SO_KEEPALIVE," }, 572 { SO_DONTROUTE, "SO_DONTROUTE," }, 573 { SO_BROADCAST, "SO_BROADCAST," }, 574 { SO_OOBINLINE, "SO_OOBINLINE," }, 575 { SO_DGRAM_ERRIND, "SO_DGRAM_ERRIND,"}, 576 { SO_ALLZONES, "SO_ALLZONES," }, 577 { SO_EXCLBIND, "SO_EXCLBIND," }, 578 }; 579 struct linger l; 580 581 buf[0] = '!'; /* sentinel value, never printed */ 582 buf[1] = '\0'; 583 584 for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) { 585 vlen = sizeof (val); 586 if (pr_getsockopt(Pr, fd, SOL_SOCKET, boolopts[i].opt, &val, 587 &vlen) == 0 && val != 0) 588 (void) strlcat(buf, boolopts[i].name, sizeof (buf)); 589 } 590 591 vlen = sizeof (l); 592 if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 && 593 l.l_onoff != 0) { 594 (void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),", 595 l.l_linger); 596 (void) strlcat(buf, buf1, sizeof (buf)); 597 } 598 599 vlen = sizeof (val); 600 if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) { 601 (void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val); 602 (void) strlcat(buf, buf1, sizeof (buf)); 603 } 604 vlen = sizeof (val); 605 if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) { 606 (void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val); 607 (void) strlcat(buf, buf1, sizeof (buf)); 608 } 609 vlen = sizeof (nexthop_val); 610 if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val, 611 &vlen) == 0) { 612 if (vlen > 0) { 613 (void) inet_ntop(AF_INET, (void *) &nexthop_val, 614 ipaddr, sizeof (ipaddr)); 615 (void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),", 616 ipaddr); 617 (void) strlcat(buf, buf1, sizeof (buf)); 618 } 619 } 620 621 buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */ 622 if (buf[1] != '\0') 623 (void) printf("\t%s\n", buf+1); 624 } 625 626 /* the file is a socket */ 627 static void 628 dosocket(struct ps_prochandle *Pr, int fd) 629 { 630 /* A buffer large enough for PATH_MAX size AF_UNIX address */ 631 long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1) 632 / sizeof (long)]; 633 struct sockaddr *sa = (struct sockaddr *)buf; 634 socklen_t len; 635 int type, tlen; 636 637 tlen = sizeof (type); 638 if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) 639 == 0) 640 show_socktype((uint_t)type); 641 642 show_sockopts(Pr, fd); 643 644 len = sizeof (buf); 645 if (pr_getsockname(Pr, fd, sa, &len) == 0) 646 show_sockaddr("sockname", sa, len); 647 648 len = sizeof (buf); 649 if (pr_getpeername(Pr, fd, sa, &len) == 0) 650 show_sockaddr("peername", sa, len); 651 } 652