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