10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51548Srshoaib * Common Development and Distribution License (the "License"). 61548Srshoaib * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211548Srshoaib 220Sstevel@tonic-gate /* 2310934Ssommerfeld@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <stdio.h> 280Sstevel@tonic-gate #include <stdlib.h> 290Sstevel@tonic-gate #include <unistd.h> 300Sstevel@tonic-gate #include <fcntl.h> 310Sstevel@tonic-gate #include <ctype.h> 320Sstevel@tonic-gate #include <string.h> 330Sstevel@tonic-gate #include <signal.h> 340Sstevel@tonic-gate #include <dirent.h> 350Sstevel@tonic-gate #include <limits.h> 360Sstevel@tonic-gate #include <door.h> 370Sstevel@tonic-gate #include <sys/types.h> 381095Spriyanka #include <sys/socket.h> 390Sstevel@tonic-gate #include <sys/stat.h> 400Sstevel@tonic-gate #include <sys/mkdev.h> 416583Smeem #include <sys/stropts.h> 426583Smeem #include <sys/timod.h> 430Sstevel@tonic-gate #include <sys/un.h> 440Sstevel@tonic-gate #include <libproc.h> 451095Spriyanka #include <netinet/in.h> 464987Sdanmcd #include <netinet/udp.h> 470Sstevel@tonic-gate #include <arpa/inet.h> 480Sstevel@tonic-gate 493507Svb160487 #define copyflock(dst, src) \ 503507Svb160487 (dst).l_type = (src).l_type; \ 513507Svb160487 (dst).l_whence = (src).l_whence; \ 523507Svb160487 (dst).l_start = (src).l_start; \ 533507Svb160487 (dst).l_len = (src).l_len; \ 543507Svb160487 (dst).l_sysid = (src).l_sysid; \ 553507Svb160487 (dst).l_pid = (src).l_pid; 563507Svb160487 570Sstevel@tonic-gate static char *command; 580Sstevel@tonic-gate static volatile int interrupt; 590Sstevel@tonic-gate static int Fflag; 600Sstevel@tonic-gate static boolean_t nflag = B_FALSE; 610Sstevel@tonic-gate 620Sstevel@tonic-gate static void intr(int); 630Sstevel@tonic-gate static void dofcntl(struct ps_prochandle *, int, int, int); 640Sstevel@tonic-gate static void dosocket(struct ps_prochandle *, int); 656583Smeem static void dotli(struct ps_prochandle *, int); 660Sstevel@tonic-gate static void show_files(struct ps_prochandle *); 670Sstevel@tonic-gate static void show_fileflags(int); 680Sstevel@tonic-gate static void show_door(struct ps_prochandle *, int); 693507Svb160487 static int getflock(struct ps_prochandle *, int, struct flock *); 700Sstevel@tonic-gate 710Sstevel@tonic-gate int 720Sstevel@tonic-gate main(int argc, char **argv) 730Sstevel@tonic-gate { 740Sstevel@tonic-gate int retc = 0; 750Sstevel@tonic-gate int opt; 760Sstevel@tonic-gate int errflg = 0; 770Sstevel@tonic-gate struct ps_prochandle *Pr; 780Sstevel@tonic-gate 790Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL) 800Sstevel@tonic-gate command++; 810Sstevel@tonic-gate else 820Sstevel@tonic-gate command = argv[0]; 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* options */ 850Sstevel@tonic-gate while ((opt = getopt(argc, argv, "Fn")) != EOF) { 860Sstevel@tonic-gate switch (opt) { 870Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */ 880Sstevel@tonic-gate Fflag = PGRAB_FORCE; 890Sstevel@tonic-gate break; 900Sstevel@tonic-gate case 'n': 910Sstevel@tonic-gate nflag = B_TRUE; 920Sstevel@tonic-gate break; 930Sstevel@tonic-gate default: 940Sstevel@tonic-gate errflg = 1; 950Sstevel@tonic-gate break; 960Sstevel@tonic-gate } 970Sstevel@tonic-gate } 980Sstevel@tonic-gate 990Sstevel@tonic-gate argc -= optind; 1000Sstevel@tonic-gate argv += optind; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate if (errflg || argc <= 0) { 1030Sstevel@tonic-gate (void) fprintf(stderr, "usage:\t%s [-F] pid ...\n", 1044987Sdanmcd command); 1050Sstevel@tonic-gate (void) fprintf(stderr, 1064987Sdanmcd " (report open files of each process)\n"); 1070Sstevel@tonic-gate (void) fprintf(stderr, 1084987Sdanmcd " -F: force grabbing of the target process\n"); 1090Sstevel@tonic-gate exit(2); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* catch signals from terminal */ 1130Sstevel@tonic-gate if (sigset(SIGHUP, SIG_IGN) == SIG_DFL) 1140Sstevel@tonic-gate (void) sigset(SIGHUP, intr); 1150Sstevel@tonic-gate if (sigset(SIGINT, SIG_IGN) == SIG_DFL) 1160Sstevel@tonic-gate (void) sigset(SIGINT, intr); 1170Sstevel@tonic-gate if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL) 1180Sstevel@tonic-gate (void) sigset(SIGQUIT, intr); 1190Sstevel@tonic-gate (void) sigset(SIGPIPE, intr); 1200Sstevel@tonic-gate (void) sigset(SIGTERM, intr); 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate (void) proc_initstdio(); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate while (--argc >= 0 && !interrupt) { 1260Sstevel@tonic-gate char *arg; 1270Sstevel@tonic-gate psinfo_t psinfo; 1280Sstevel@tonic-gate pid_t pid; 1290Sstevel@tonic-gate int gret; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate (void) proc_flushstdio(); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* get the specified pid and the psinfo struct */ 1340Sstevel@tonic-gate if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS, 1350Sstevel@tonic-gate &psinfo, &gret)) == -1) { 1360Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n", 1374987Sdanmcd command, arg, Pgrab_error(gret)); 1380Sstevel@tonic-gate retc++; 1390Sstevel@tonic-gate } else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) { 1400Sstevel@tonic-gate if (Pcreate_agent(Pr) == 0) { 1410Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 1420Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", 1434987Sdanmcd (int)pid, psinfo.pr_psargs); 1440Sstevel@tonic-gate show_files(Pr); 1450Sstevel@tonic-gate Pdestroy_agent(Pr); 1460Sstevel@tonic-gate } else { 1470Sstevel@tonic-gate (void) fprintf(stderr, 1484987Sdanmcd "%s: cannot control process %d\n", 1494987Sdanmcd command, (int)pid); 1500Sstevel@tonic-gate retc++; 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate Prelease(Pr, 0); 1530Sstevel@tonic-gate Pr = NULL; 1540Sstevel@tonic-gate } else { 1550Sstevel@tonic-gate switch (gret) { 1560Sstevel@tonic-gate case G_SYS: 1570Sstevel@tonic-gate case G_SELF: 1580Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 1590Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", (int)pid, 1604987Sdanmcd psinfo.pr_psargs); 1610Sstevel@tonic-gate if (gret == G_SYS) 1620Sstevel@tonic-gate (void) printf(" [system process]\n"); 1630Sstevel@tonic-gate else 1640Sstevel@tonic-gate show_files(NULL); 1650Sstevel@tonic-gate break; 1660Sstevel@tonic-gate default: 1670Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %d\n", 1684987Sdanmcd command, Pgrab_error(gret), (int)pid); 1690Sstevel@tonic-gate retc++; 1700Sstevel@tonic-gate break; 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate (void) proc_finistdio(); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate if (interrupt && retc == 0) 1780Sstevel@tonic-gate retc++; 1790Sstevel@tonic-gate return (retc); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* ARGSUSED */ 1830Sstevel@tonic-gate static void 1840Sstevel@tonic-gate intr(int sig) 1850Sstevel@tonic-gate { 1860Sstevel@tonic-gate interrupt = 1; 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* ------ begin specific code ------ */ 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate static void 1920Sstevel@tonic-gate show_files(struct ps_prochandle *Pr) 1930Sstevel@tonic-gate { 1940Sstevel@tonic-gate DIR *dirp; 1950Sstevel@tonic-gate struct dirent *dentp; 1966583Smeem const char *dev; 1970Sstevel@tonic-gate char pname[100]; 1980Sstevel@tonic-gate char fname[PATH_MAX]; 1990Sstevel@tonic-gate struct stat64 statb; 2000Sstevel@tonic-gate struct rlimit rlim; 2010Sstevel@tonic-gate pid_t pid; 2020Sstevel@tonic-gate int fd; 2030Sstevel@tonic-gate char *s; 2040Sstevel@tonic-gate int ret; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) { 2070Sstevel@tonic-gate ulong_t nfd = rlim.rlim_cur; 2080Sstevel@tonic-gate if (nfd == RLIM_INFINITY) 2090Sstevel@tonic-gate (void) printf( 2100Sstevel@tonic-gate " Current rlimit: unlimited file descriptors\n"); 2110Sstevel@tonic-gate else 2120Sstevel@tonic-gate (void) printf( 2130Sstevel@tonic-gate " Current rlimit: %lu file descriptors\n", nfd); 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* in case we are doing this to ourself */ 2170Sstevel@tonic-gate pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate (void) sprintf(pname, "/proc/%d/fd", (int)pid); 2200Sstevel@tonic-gate if ((dirp = opendir(pname)) == NULL) { 2210Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open directory %s\n", 2220Sstevel@tonic-gate command, pname); 2230Sstevel@tonic-gate return; 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* for each open file --- */ 2270Sstevel@tonic-gate while ((dentp = readdir(dirp)) != NULL && !interrupt) { 2280Sstevel@tonic-gate char unknown[12]; 2290Sstevel@tonic-gate dev_t rdev; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* skip '.' and '..' */ 2320Sstevel@tonic-gate if (!isdigit(dentp->d_name[0])) 2330Sstevel@tonic-gate continue; 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate fd = atoi(dentp->d_name); 2360Sstevel@tonic-gate if (pr_fstat64(Pr, fd, &statb) == -1) { 2370Sstevel@tonic-gate s = unknown; 2380Sstevel@tonic-gate (void) sprintf(s, "%4d", fd); 2390Sstevel@tonic-gate perror(s); 2400Sstevel@tonic-gate continue; 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate rdev = NODEV; 2440Sstevel@tonic-gate switch (statb.st_mode & S_IFMT) { 2450Sstevel@tonic-gate case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break; 2460Sstevel@tonic-gate case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break; 2470Sstevel@tonic-gate case S_IFIFO: s = "S_IFIFO"; break; 2480Sstevel@tonic-gate case S_IFDIR: s = "S_IFDIR"; break; 2490Sstevel@tonic-gate case S_IFREG: s = "S_IFREG"; break; 2500Sstevel@tonic-gate case S_IFLNK: s = "S_IFLNK"; break; 2510Sstevel@tonic-gate case S_IFSOCK: s = "S_IFSOCK"; break; 2520Sstevel@tonic-gate case S_IFDOOR: s = "S_IFDOOR"; break; 2530Sstevel@tonic-gate case S_IFPORT: s = "S_IFPORT"; break; 2540Sstevel@tonic-gate default: 2550Sstevel@tonic-gate s = unknown; 2560Sstevel@tonic-gate (void) sprintf(s, "0x%.4x ", 2574987Sdanmcd (int)statb.st_mode & S_IFMT); 2580Sstevel@tonic-gate break; 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2614987Sdanmcd (void) printf("%4d: %s mode:0%.3o", fd, s, 2624987Sdanmcd (int)statb.st_mode & ~S_IFMT); 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate if (major(statb.st_dev) != (major_t)NODEV && 2650Sstevel@tonic-gate minor(statb.st_dev) != (minor_t)NODEV) 2660Sstevel@tonic-gate (void) printf(" dev:%lu,%lu", 2674987Sdanmcd (ulong_t)major(statb.st_dev), 2684987Sdanmcd (ulong_t)minor(statb.st_dev)); 2690Sstevel@tonic-gate else 2700Sstevel@tonic-gate (void) printf(" dev:0x%.8lX", (long)statb.st_dev); 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFPORT) { 2730Sstevel@tonic-gate (void) printf(" uid:%d gid:%d", 2740Sstevel@tonic-gate (int)statb.st_uid, 2750Sstevel@tonic-gate (int)statb.st_gid); 2760Sstevel@tonic-gate (void) printf(" size:%lld\n", 2770Sstevel@tonic-gate (longlong_t)statb.st_size); 2780Sstevel@tonic-gate continue; 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate (void) printf(" ino:%llu uid:%d gid:%d", 2824987Sdanmcd (u_longlong_t)statb.st_ino, 2834987Sdanmcd (int)statb.st_uid, (int)statb.st_gid); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate if (rdev == NODEV) 2860Sstevel@tonic-gate (void) printf(" size:%lld\n", 2874987Sdanmcd (longlong_t)statb.st_size); 2880Sstevel@tonic-gate else if (major(rdev) != (major_t)NODEV && 2890Sstevel@tonic-gate minor(rdev) != (minor_t)NODEV) 2900Sstevel@tonic-gate (void) printf(" rdev:%lu,%lu\n", 2914987Sdanmcd (ulong_t)major(rdev), (ulong_t)minor(rdev)); 2920Sstevel@tonic-gate else 2930Sstevel@tonic-gate (void) printf(" rdev:0x%.8lX\n", (long)rdev); 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate if (!nflag) { 296*11281SDan.Mick@Sun.COM off_t offset; 297*11281SDan.Mick@Sun.COM 2980Sstevel@tonic-gate dofcntl(Pr, fd, 2994987Sdanmcd (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP)) 3004987Sdanmcd == (S_IFREG|S_ENFMT), 3014987Sdanmcd (statb.st_mode & S_IFMT) == S_IFDOOR); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFSOCK) 3040Sstevel@tonic-gate dosocket(Pr, fd); 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate (void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd); 3070Sstevel@tonic-gate 3086583Smeem if ((ret = readlink(pname, fname, PATH_MAX - 1)) <= 0) 3096583Smeem continue; 3106583Smeem 3116583Smeem fname[ret] = '\0'; 3126583Smeem 3136583Smeem if ((statb.st_mode & S_IFMT) == S_IFCHR && 3146583Smeem (dev = strrchr(fname, ':')) != NULL) { 3156583Smeem /* 3166583Smeem * There's no elegant way to determine if a 3176583Smeem * character device supports TLI, so we lame 3186583Smeem * out and just check a hardcoded list of 3196583Smeem * known TLI devices. 3206583Smeem */ 3216583Smeem int i; 3226583Smeem const char *tlidevs[] = 3236583Smeem { "tcp", "tcp6", "udp", "udp6", NULL }; 3246583Smeem 3256583Smeem dev++; /* skip past the `:' */ 3266583Smeem for (i = 0; tlidevs[i] != NULL; i++) { 3276583Smeem if (strcmp(dev, tlidevs[i]) == 0) { 3286583Smeem dotli(Pr, fd); 3296583Smeem break; 3306583Smeem } 3316583Smeem } 3320Sstevel@tonic-gate } 3336583Smeem (void) printf(" %s\n", fname); 334*11281SDan.Mick@Sun.COM 335*11281SDan.Mick@Sun.COM offset = pr_lseek(Pr, fd, 0, SEEK_CUR); 336*11281SDan.Mick@Sun.COM if (offset != -1) { 337*11281SDan.Mick@Sun.COM (void) printf(" offset:%ld\n", offset); 338*11281SDan.Mick@Sun.COM } 339*11281SDan.Mick@Sun.COM 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate (void) closedir(dirp); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate 3453507Svb160487 3463507Svb160487 static int 3473507Svb160487 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native) 3483507Svb160487 { 3493507Svb160487 int ret; 3503507Svb160487 #ifdef _LP64 3513507Svb160487 struct flock64_32 flock_target; 3523507Svb160487 3533507Svb160487 if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) { 3543507Svb160487 copyflock(flock_target, *flock_native); 3553507Svb160487 ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target); 3563507Svb160487 copyflock(*flock_native, flock_target); 3573507Svb160487 return (ret); 3583507Svb160487 } 3593507Svb160487 #endif /* _LP64 */ 3603507Svb160487 ret = pr_fcntl(Pr, fd, F_GETLK, flock_native); 3613507Svb160487 return (ret); 3623507Svb160487 } 3633507Svb160487 3640Sstevel@tonic-gate /* examine open file with fcntl() */ 3650Sstevel@tonic-gate static void 3664987Sdanmcd dofcntl(struct ps_prochandle *Pr, int fd, int mandatory, int isdoor) 3670Sstevel@tonic-gate { 3680Sstevel@tonic-gate struct flock flock; 3690Sstevel@tonic-gate int fileflags; 3700Sstevel@tonic-gate int fdflags; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0); 3730Sstevel@tonic-gate fdflags = pr_fcntl(Pr, fd, F_GETFD, 0); 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate if (fileflags != -1 || fdflags != -1) { 3760Sstevel@tonic-gate (void) printf(" "); 3770Sstevel@tonic-gate if (fileflags != -1) 3780Sstevel@tonic-gate show_fileflags(fileflags); 3790Sstevel@tonic-gate if (fdflags != -1 && (fdflags & FD_CLOEXEC)) 3800Sstevel@tonic-gate (void) printf(" FD_CLOEXEC"); 3810Sstevel@tonic-gate if (isdoor) 3820Sstevel@tonic-gate show_door(Pr, fd); 3830Sstevel@tonic-gate (void) fputc('\n', stdout); 3840Sstevel@tonic-gate } else if (isdoor) { 3850Sstevel@tonic-gate (void) printf(" "); 3860Sstevel@tonic-gate show_door(Pr, fd); 3870Sstevel@tonic-gate (void) fputc('\n', stdout); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate flock.l_type = F_WRLCK; 3910Sstevel@tonic-gate flock.l_whence = 0; 3920Sstevel@tonic-gate flock.l_start = 0; 3930Sstevel@tonic-gate flock.l_len = 0; 3940Sstevel@tonic-gate flock.l_sysid = 0; 3950Sstevel@tonic-gate flock.l_pid = 0; 3963507Svb160487 if (getflock(Pr, fd, &flock) != -1) { 3970Sstevel@tonic-gate if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) { 3980Sstevel@tonic-gate unsigned long sysid = flock.l_sysid; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate (void) printf(" %s %s lock set by", 4014987Sdanmcd mandatory ? "mandatory" : "advisory", 4024987Sdanmcd flock.l_type == F_RDLCK? "read" : "write"); 4030Sstevel@tonic-gate if (sysid) 4040Sstevel@tonic-gate (void) printf(" system 0x%lX", sysid); 4050Sstevel@tonic-gate if (flock.l_pid) 4060Sstevel@tonic-gate (void) printf(" process %d", (int)flock.l_pid); 4070Sstevel@tonic-gate (void) fputc('\n', stdout); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate #ifdef O_PRIV 4130Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 4140Sstevel@tonic-gate O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 4150Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 4160Sstevel@tonic-gate #else 4170Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 4180Sstevel@tonic-gate O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 4190Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 4200Sstevel@tonic-gate #endif 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate static void 4230Sstevel@tonic-gate show_fileflags(int flags) 4240Sstevel@tonic-gate { 4250Sstevel@tonic-gate char buffer[136]; 4260Sstevel@tonic-gate char *str = buffer; 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate switch (flags & O_ACCMODE) { 4290Sstevel@tonic-gate case O_RDONLY: 4300Sstevel@tonic-gate (void) strcpy(str, "O_RDONLY"); 4310Sstevel@tonic-gate break; 4320Sstevel@tonic-gate case O_WRONLY: 4330Sstevel@tonic-gate (void) strcpy(str, "O_WRONLY"); 4340Sstevel@tonic-gate break; 4350Sstevel@tonic-gate case O_RDWR: 4360Sstevel@tonic-gate (void) strcpy(str, "O_RDWR"); 4370Sstevel@tonic-gate break; 4380Sstevel@tonic-gate default: 4390Sstevel@tonic-gate (void) sprintf(str, "0x%x", flags & O_ACCMODE); 4400Sstevel@tonic-gate break; 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate if (flags & O_NDELAY) 4440Sstevel@tonic-gate (void) strcat(str, "|O_NDELAY"); 4450Sstevel@tonic-gate if (flags & O_NONBLOCK) 4460Sstevel@tonic-gate (void) strcat(str, "|O_NONBLOCK"); 4470Sstevel@tonic-gate if (flags & O_APPEND) 4480Sstevel@tonic-gate (void) strcat(str, "|O_APPEND"); 4490Sstevel@tonic-gate #ifdef O_PRIV 4500Sstevel@tonic-gate if (flags & O_PRIV) 4510Sstevel@tonic-gate (void) strcat(str, "|O_PRIV"); 4520Sstevel@tonic-gate #endif 4530Sstevel@tonic-gate if (flags & O_SYNC) 4540Sstevel@tonic-gate (void) strcat(str, "|O_SYNC"); 4550Sstevel@tonic-gate if (flags & O_DSYNC) 4560Sstevel@tonic-gate (void) strcat(str, "|O_DSYNC"); 4570Sstevel@tonic-gate if (flags & O_RSYNC) 4580Sstevel@tonic-gate (void) strcat(str, "|O_RSYNC"); 4590Sstevel@tonic-gate if (flags & O_CREAT) 4600Sstevel@tonic-gate (void) strcat(str, "|O_CREAT"); 4610Sstevel@tonic-gate if (flags & O_TRUNC) 4620Sstevel@tonic-gate (void) strcat(str, "|O_TRUNC"); 4630Sstevel@tonic-gate if (flags & O_EXCL) 4640Sstevel@tonic-gate (void) strcat(str, "|O_EXCL"); 4650Sstevel@tonic-gate if (flags & O_NOCTTY) 4660Sstevel@tonic-gate (void) strcat(str, "|O_NOCTTY"); 4670Sstevel@tonic-gate if (flags & O_LARGEFILE) 4680Sstevel@tonic-gate (void) strcat(str, "|O_LARGEFILE"); 4690Sstevel@tonic-gate if (flags & O_XATTR) 4700Sstevel@tonic-gate (void) strcat(str, "|O_XATTR"); 4710Sstevel@tonic-gate if (flags & ~(ALL_O_FLAGS)) 4720Sstevel@tonic-gate (void) sprintf(str + strlen(str), "|0x%x", 4734987Sdanmcd flags & ~(ALL_O_FLAGS)); 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate (void) printf("%s", str); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* show door info */ 4790Sstevel@tonic-gate static void 4800Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd) 4810Sstevel@tonic-gate { 4820Sstevel@tonic-gate door_info_t door_info; 4830Sstevel@tonic-gate psinfo_t psinfo; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate if (pr_door_info(Pr, fd, &door_info) != 0) 4860Sstevel@tonic-gate return; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate if (proc_get_psinfo(door_info.di_target, &psinfo) != 0) 4890Sstevel@tonic-gate psinfo.pr_fname[0] = '\0'; 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate (void) printf(" door to "); 4920Sstevel@tonic-gate if (psinfo.pr_fname[0] != '\0') 4930Sstevel@tonic-gate (void) printf("%s[%d]", psinfo.pr_fname, 4944987Sdanmcd (int)door_info.di_target); 4950Sstevel@tonic-gate else 4960Sstevel@tonic-gate (void) printf("pid %d", (int)door_info.di_target); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4996583Smeem /* 5006583Smeem * Print out the socket address pointed to by `sa'. `len' is only 5016583Smeem * needed for AF_UNIX sockets. 5026583Smeem */ 5030Sstevel@tonic-gate static void 5040Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len) 5050Sstevel@tonic-gate { 5066583Smeem struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa; 5076583Smeem struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa; 5080Sstevel@tonic-gate struct sockaddr_un *so_un = (struct sockaddr_un *)sa; 5090Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 5100Sstevel@tonic-gate const char *p; 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate switch (sa->sa_family) { 5130Sstevel@tonic-gate default: 5140Sstevel@tonic-gate return; 5150Sstevel@tonic-gate case AF_INET: 5166583Smeem (void) printf("\t%s: AF_INET %s port: %u\n", str, 5170Sstevel@tonic-gate inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)), 5180Sstevel@tonic-gate ntohs(so_in->sin_port)); 5190Sstevel@tonic-gate return; 5200Sstevel@tonic-gate case AF_INET6: 5216583Smeem (void) printf("\t%s: AF_INET6 %s port: %u\n", str, 5220Sstevel@tonic-gate inet_ntop(AF_INET6, &so_in6->sin6_addr, 5230Sstevel@tonic-gate abuf, sizeof (abuf)), 5240Sstevel@tonic-gate ntohs(so_in->sin_port)); 5250Sstevel@tonic-gate return; 5260Sstevel@tonic-gate case AF_UNIX: 5270Sstevel@tonic-gate if (len >= sizeof (so_un->sun_family)) { 5280Sstevel@tonic-gate /* Null terminate */ 5290Sstevel@tonic-gate len -= sizeof (so_un->sun_family); 5306583Smeem so_un->sun_path[len] = '\0'; 5310Sstevel@tonic-gate (void) printf("\t%s: AF_UNIX %s\n", 5324987Sdanmcd str, so_un->sun_path); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate return; 5350Sstevel@tonic-gate case AF_IMPLINK: p = "AF_IMPLINK"; break; 5360Sstevel@tonic-gate case AF_PUP: p = "AF_PUP"; break; 5370Sstevel@tonic-gate case AF_CHAOS: p = "AF_CHAOS"; break; 5380Sstevel@tonic-gate case AF_NS: p = "AF_NS"; break; 5390Sstevel@tonic-gate case AF_NBS: p = "AF_NBS"; break; 5400Sstevel@tonic-gate case AF_ECMA: p = "AF_ECMA"; break; 5410Sstevel@tonic-gate case AF_DATAKIT: p = "AF_DATAKIT"; break; 5420Sstevel@tonic-gate case AF_CCITT: p = "AF_CCITT"; break; 5430Sstevel@tonic-gate case AF_SNA: p = "AF_SNA"; break; 5440Sstevel@tonic-gate case AF_DECnet: p = "AF_DECnet"; break; 5450Sstevel@tonic-gate case AF_DLI: p = "AF_DLI"; break; 5460Sstevel@tonic-gate case AF_LAT: p = "AF_LAT"; break; 5470Sstevel@tonic-gate case AF_HYLINK: p = "AF_HYLINK"; break; 5480Sstevel@tonic-gate case AF_APPLETALK: p = "AF_APPLETALK"; break; 5490Sstevel@tonic-gate case AF_NIT: p = "AF_NIT"; break; 5500Sstevel@tonic-gate case AF_802: p = "AF_802"; break; 5510Sstevel@tonic-gate case AF_OSI: p = "AF_OSI"; break; 5520Sstevel@tonic-gate case AF_X25: p = "AF_X25"; break; 5530Sstevel@tonic-gate case AF_OSINET: p = "AF_OSINET"; break; 5540Sstevel@tonic-gate case AF_GOSIP: p = "AF_GOSIP"; break; 5550Sstevel@tonic-gate case AF_IPX: p = "AF_IPX"; break; 5560Sstevel@tonic-gate case AF_ROUTE: p = "AF_ROUTE"; break; 5570Sstevel@tonic-gate case AF_LINK: p = "AF_LINK"; break; 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate (void) printf("\t%s: %s\n", str, p); 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate static void 5640Sstevel@tonic-gate show_socktype(uint_t type) 5650Sstevel@tonic-gate { 5660Sstevel@tonic-gate static const char *types[] = { 5670Sstevel@tonic-gate NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET" 5680Sstevel@tonic-gate }; 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate if (type < sizeof (types) / sizeof (*types) && types[type] != NULL) 5710Sstevel@tonic-gate (void) printf("\tSOCK_%s\n", types[type]); 5720Sstevel@tonic-gate else 5730Sstevel@tonic-gate (void) printf("\tunknown socket type %u\n", type); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5761095Spriyanka #define BUFSIZE 200 5770Sstevel@tonic-gate static void 5780Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd) 5790Sstevel@tonic-gate { 5800Sstevel@tonic-gate int val, vlen; 5811095Spriyanka char buf[BUFSIZE]; 5820Sstevel@tonic-gate char buf1[32]; 5831095Spriyanka char ipaddr[INET_ADDRSTRLEN]; 5840Sstevel@tonic-gate int i; 5851095Spriyanka in_addr_t nexthop_val; 5860Sstevel@tonic-gate struct boolopt { 5874987Sdanmcd int level; 5880Sstevel@tonic-gate int opt; 5890Sstevel@tonic-gate const char *name; 5900Sstevel@tonic-gate }; 5910Sstevel@tonic-gate static struct boolopt boolopts[] = { 5924987Sdanmcd { SOL_SOCKET, SO_DEBUG, "SO_DEBUG," }, 5934987Sdanmcd { SOL_SOCKET, SO_REUSEADDR, "SO_REUSEADDR," }, 5944987Sdanmcd { SOL_SOCKET, SO_KEEPALIVE, "SO_KEEPALIVE," }, 5954987Sdanmcd { SOL_SOCKET, SO_DONTROUTE, "SO_DONTROUTE," }, 5964987Sdanmcd { SOL_SOCKET, SO_BROADCAST, "SO_BROADCAST," }, 5974987Sdanmcd { SOL_SOCKET, SO_OOBINLINE, "SO_OOBINLINE," }, 5984987Sdanmcd { SOL_SOCKET, SO_DGRAM_ERRIND, "SO_DGRAM_ERRIND,"}, 5994987Sdanmcd { SOL_SOCKET, SO_ALLZONES, "SO_ALLZONES," }, 60010934Ssommerfeld@sun.com { SOL_SOCKET, SO_MAC_EXEMPT, "SO_MAC_EXEMPT," }, 60110934Ssommerfeld@sun.com { SOL_SOCKET, SO_MAC_IMPLICIT, "SO_MAC_IMPLICIT," }, 6024987Sdanmcd { SOL_SOCKET, SO_EXCLBIND, "SO_EXCLBIND," }, 60311076SCathy.Zhou@Sun.COM { SOL_SOCKET, SO_VRRP, "SO_VRRP," }, 6044987Sdanmcd { IPPROTO_UDP, UDP_NAT_T_ENDPOINT, "UDP_NAT_T_ENDPOINT," }, 6050Sstevel@tonic-gate }; 6060Sstevel@tonic-gate struct linger l; 6070Sstevel@tonic-gate 6082263Ssommerfe buf[0] = '!'; /* sentinel value, never printed */ 6090Sstevel@tonic-gate buf[1] = '\0'; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) { 6120Sstevel@tonic-gate vlen = sizeof (val); 6134987Sdanmcd if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt, 6144987Sdanmcd &val, &vlen) == 0 && val != 0) 6150Sstevel@tonic-gate (void) strlcat(buf, boolopts[i].name, sizeof (buf)); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate vlen = sizeof (l); 6190Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 && 6200Sstevel@tonic-gate l.l_onoff != 0) { 6210Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),", 6220Sstevel@tonic-gate l.l_linger); 6230Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate vlen = sizeof (val); 6270Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) { 6280Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val); 6290Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate vlen = sizeof (val); 6320Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) { 6330Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val); 6340Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 6350Sstevel@tonic-gate } 6361095Spriyanka vlen = sizeof (nexthop_val); 6371095Spriyanka if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val, 6381095Spriyanka &vlen) == 0) { 6391628Spriyanka if (vlen > 0) { 6401628Spriyanka (void) inet_ntop(AF_INET, (void *) &nexthop_val, 6411628Spriyanka ipaddr, sizeof (ipaddr)); 6421628Spriyanka (void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),", 6431628Spriyanka ipaddr); 6441628Spriyanka (void) strlcat(buf, buf1, sizeof (buf)); 6451628Spriyanka } 6461095Spriyanka } 6470Sstevel@tonic-gate 6482263Ssommerfe buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */ 6490Sstevel@tonic-gate if (buf[1] != '\0') 6500Sstevel@tonic-gate (void) printf("\t%s\n", buf+1); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* the file is a socket */ 6540Sstevel@tonic-gate static void 6550Sstevel@tonic-gate dosocket(struct ps_prochandle *Pr, int fd) 6560Sstevel@tonic-gate { 6570Sstevel@tonic-gate /* A buffer large enough for PATH_MAX size AF_UNIX address */ 6580Sstevel@tonic-gate long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1) 6594987Sdanmcd / sizeof (long)]; 6600Sstevel@tonic-gate struct sockaddr *sa = (struct sockaddr *)buf; 6610Sstevel@tonic-gate socklen_t len; 6620Sstevel@tonic-gate int type, tlen; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate tlen = sizeof (type); 6656583Smeem if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0) 6660Sstevel@tonic-gate show_socktype((uint_t)type); 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate show_sockopts(Pr, fd); 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate len = sizeof (buf); 6710Sstevel@tonic-gate if (pr_getsockname(Pr, fd, sa, &len) == 0) 6720Sstevel@tonic-gate show_sockaddr("sockname", sa, len); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate len = sizeof (buf); 6750Sstevel@tonic-gate if (pr_getpeername(Pr, fd, sa, &len) == 0) 6760Sstevel@tonic-gate show_sockaddr("peername", sa, len); 6770Sstevel@tonic-gate } 6786583Smeem 6796583Smeem /* the file is a TLI endpoint */ 6806583Smeem static void 6816583Smeem dotli(struct ps_prochandle *Pr, int fd) 6826583Smeem { 6836583Smeem struct strcmd strcmd; 6846583Smeem 6856583Smeem strcmd.sc_len = STRCMDBUFSIZE; 6866583Smeem strcmd.sc_timeout = 5; 6876583Smeem 6886583Smeem strcmd.sc_cmd = TI_GETMYNAME; 6896583Smeem if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0) 6906583Smeem show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0); 6916583Smeem 6926583Smeem strcmd.sc_cmd = TI_GETPEERNAME; 6936583Smeem if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0) 6946583Smeem show_sockaddr("peername", (void *)&strcmd.sc_buf, 0); 6956583Smeem } 696