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 /* 23*6583Smeem * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <unistd.h> 320Sstevel@tonic-gate #include <fcntl.h> 330Sstevel@tonic-gate #include <ctype.h> 340Sstevel@tonic-gate #include <string.h> 350Sstevel@tonic-gate #include <signal.h> 360Sstevel@tonic-gate #include <dirent.h> 370Sstevel@tonic-gate #include <limits.h> 380Sstevel@tonic-gate #include <door.h> 390Sstevel@tonic-gate #include <sys/types.h> 401095Spriyanka #include <sys/socket.h> 410Sstevel@tonic-gate #include <sys/stat.h> 420Sstevel@tonic-gate #include <sys/mkdev.h> 43*6583Smeem #include <sys/stropts.h> 44*6583Smeem #include <sys/timod.h> 450Sstevel@tonic-gate #include <sys/un.h> 460Sstevel@tonic-gate #include <libproc.h> 471095Spriyanka #include <netinet/in.h> 484987Sdanmcd #include <netinet/udp.h> 490Sstevel@tonic-gate #include <arpa/inet.h> 500Sstevel@tonic-gate 513507Svb160487 #define copyflock(dst, src) \ 523507Svb160487 (dst).l_type = (src).l_type; \ 533507Svb160487 (dst).l_whence = (src).l_whence; \ 543507Svb160487 (dst).l_start = (src).l_start; \ 553507Svb160487 (dst).l_len = (src).l_len; \ 563507Svb160487 (dst).l_sysid = (src).l_sysid; \ 573507Svb160487 (dst).l_pid = (src).l_pid; 583507Svb160487 590Sstevel@tonic-gate static char *command; 600Sstevel@tonic-gate static volatile int interrupt; 610Sstevel@tonic-gate static int Fflag; 620Sstevel@tonic-gate static boolean_t nflag = B_FALSE; 630Sstevel@tonic-gate 640Sstevel@tonic-gate static void intr(int); 650Sstevel@tonic-gate static void dofcntl(struct ps_prochandle *, int, int, int); 660Sstevel@tonic-gate static void dosocket(struct ps_prochandle *, int); 67*6583Smeem static void dotli(struct ps_prochandle *, int); 680Sstevel@tonic-gate static void show_files(struct ps_prochandle *); 690Sstevel@tonic-gate static void show_fileflags(int); 700Sstevel@tonic-gate static void show_door(struct ps_prochandle *, int); 713507Svb160487 static int getflock(struct ps_prochandle *, int, struct flock *); 720Sstevel@tonic-gate 730Sstevel@tonic-gate int 740Sstevel@tonic-gate main(int argc, char **argv) 750Sstevel@tonic-gate { 760Sstevel@tonic-gate int retc = 0; 770Sstevel@tonic-gate int opt; 780Sstevel@tonic-gate int errflg = 0; 790Sstevel@tonic-gate struct ps_prochandle *Pr; 800Sstevel@tonic-gate 810Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL) 820Sstevel@tonic-gate command++; 830Sstevel@tonic-gate else 840Sstevel@tonic-gate command = argv[0]; 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* options */ 870Sstevel@tonic-gate while ((opt = getopt(argc, argv, "Fn")) != EOF) { 880Sstevel@tonic-gate switch (opt) { 890Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */ 900Sstevel@tonic-gate Fflag = PGRAB_FORCE; 910Sstevel@tonic-gate break; 920Sstevel@tonic-gate case 'n': 930Sstevel@tonic-gate nflag = B_TRUE; 940Sstevel@tonic-gate break; 950Sstevel@tonic-gate default: 960Sstevel@tonic-gate errflg = 1; 970Sstevel@tonic-gate break; 980Sstevel@tonic-gate } 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate argc -= optind; 1020Sstevel@tonic-gate argv += optind; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate if (errflg || argc <= 0) { 1050Sstevel@tonic-gate (void) fprintf(stderr, "usage:\t%s [-F] pid ...\n", 1064987Sdanmcd command); 1070Sstevel@tonic-gate (void) fprintf(stderr, 1084987Sdanmcd " (report open files of each process)\n"); 1090Sstevel@tonic-gate (void) fprintf(stderr, 1104987Sdanmcd " -F: force grabbing of the target process\n"); 1110Sstevel@tonic-gate exit(2); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* catch signals from terminal */ 1150Sstevel@tonic-gate if (sigset(SIGHUP, SIG_IGN) == SIG_DFL) 1160Sstevel@tonic-gate (void) sigset(SIGHUP, intr); 1170Sstevel@tonic-gate if (sigset(SIGINT, SIG_IGN) == SIG_DFL) 1180Sstevel@tonic-gate (void) sigset(SIGINT, intr); 1190Sstevel@tonic-gate if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL) 1200Sstevel@tonic-gate (void) sigset(SIGQUIT, intr); 1210Sstevel@tonic-gate (void) sigset(SIGPIPE, intr); 1220Sstevel@tonic-gate (void) sigset(SIGTERM, intr); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate (void) proc_initstdio(); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate while (--argc >= 0 && !interrupt) { 1280Sstevel@tonic-gate char *arg; 1290Sstevel@tonic-gate psinfo_t psinfo; 1300Sstevel@tonic-gate pid_t pid; 1310Sstevel@tonic-gate int gret; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate (void) proc_flushstdio(); 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* get the specified pid and the psinfo struct */ 1360Sstevel@tonic-gate if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS, 1370Sstevel@tonic-gate &psinfo, &gret)) == -1) { 1380Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n", 1394987Sdanmcd command, arg, Pgrab_error(gret)); 1400Sstevel@tonic-gate retc++; 1410Sstevel@tonic-gate } else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) { 1420Sstevel@tonic-gate if (Pcreate_agent(Pr) == 0) { 1430Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 1440Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", 1454987Sdanmcd (int)pid, psinfo.pr_psargs); 1460Sstevel@tonic-gate show_files(Pr); 1470Sstevel@tonic-gate Pdestroy_agent(Pr); 1480Sstevel@tonic-gate } else { 1490Sstevel@tonic-gate (void) fprintf(stderr, 1504987Sdanmcd "%s: cannot control process %d\n", 1514987Sdanmcd command, (int)pid); 1520Sstevel@tonic-gate retc++; 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate Prelease(Pr, 0); 1550Sstevel@tonic-gate Pr = NULL; 1560Sstevel@tonic-gate } else { 1570Sstevel@tonic-gate switch (gret) { 1580Sstevel@tonic-gate case G_SYS: 1590Sstevel@tonic-gate case G_SELF: 1600Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 1610Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", (int)pid, 1624987Sdanmcd psinfo.pr_psargs); 1630Sstevel@tonic-gate if (gret == G_SYS) 1640Sstevel@tonic-gate (void) printf(" [system process]\n"); 1650Sstevel@tonic-gate else 1660Sstevel@tonic-gate show_files(NULL); 1670Sstevel@tonic-gate break; 1680Sstevel@tonic-gate default: 1690Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %d\n", 1704987Sdanmcd command, Pgrab_error(gret), (int)pid); 1710Sstevel@tonic-gate retc++; 1720Sstevel@tonic-gate break; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate (void) proc_finistdio(); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate if (interrupt && retc == 0) 1800Sstevel@tonic-gate retc++; 1810Sstevel@tonic-gate return (retc); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* ARGSUSED */ 1850Sstevel@tonic-gate static void 1860Sstevel@tonic-gate intr(int sig) 1870Sstevel@tonic-gate { 1880Sstevel@tonic-gate interrupt = 1; 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate /* ------ begin specific code ------ */ 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate static void 1940Sstevel@tonic-gate show_files(struct ps_prochandle *Pr) 1950Sstevel@tonic-gate { 1960Sstevel@tonic-gate DIR *dirp; 1970Sstevel@tonic-gate struct dirent *dentp; 198*6583Smeem const char *dev; 1990Sstevel@tonic-gate char pname[100]; 2000Sstevel@tonic-gate char fname[PATH_MAX]; 2010Sstevel@tonic-gate struct stat64 statb; 2020Sstevel@tonic-gate struct rlimit rlim; 2030Sstevel@tonic-gate pid_t pid; 2040Sstevel@tonic-gate int fd; 2050Sstevel@tonic-gate char *s; 2060Sstevel@tonic-gate int ret; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) { 2090Sstevel@tonic-gate ulong_t nfd = rlim.rlim_cur; 2100Sstevel@tonic-gate if (nfd == RLIM_INFINITY) 2110Sstevel@tonic-gate (void) printf( 2120Sstevel@tonic-gate " Current rlimit: unlimited file descriptors\n"); 2130Sstevel@tonic-gate else 2140Sstevel@tonic-gate (void) printf( 2150Sstevel@tonic-gate " Current rlimit: %lu file descriptors\n", nfd); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /* in case we are doing this to ourself */ 2190Sstevel@tonic-gate pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid; 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate (void) sprintf(pname, "/proc/%d/fd", (int)pid); 2220Sstevel@tonic-gate if ((dirp = opendir(pname)) == NULL) { 2230Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open directory %s\n", 2240Sstevel@tonic-gate command, pname); 2250Sstevel@tonic-gate return; 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* for each open file --- */ 2290Sstevel@tonic-gate while ((dentp = readdir(dirp)) != NULL && !interrupt) { 2300Sstevel@tonic-gate char unknown[12]; 2310Sstevel@tonic-gate dev_t rdev; 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate /* skip '.' and '..' */ 2340Sstevel@tonic-gate if (!isdigit(dentp->d_name[0])) 2350Sstevel@tonic-gate continue; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate fd = atoi(dentp->d_name); 2380Sstevel@tonic-gate if (pr_fstat64(Pr, fd, &statb) == -1) { 2390Sstevel@tonic-gate s = unknown; 2400Sstevel@tonic-gate (void) sprintf(s, "%4d", fd); 2410Sstevel@tonic-gate perror(s); 2420Sstevel@tonic-gate continue; 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate rdev = NODEV; 2460Sstevel@tonic-gate switch (statb.st_mode & S_IFMT) { 2470Sstevel@tonic-gate case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break; 2480Sstevel@tonic-gate case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break; 2490Sstevel@tonic-gate case S_IFIFO: s = "S_IFIFO"; break; 2500Sstevel@tonic-gate case S_IFDIR: s = "S_IFDIR"; break; 2510Sstevel@tonic-gate case S_IFREG: s = "S_IFREG"; break; 2520Sstevel@tonic-gate case S_IFLNK: s = "S_IFLNK"; break; 2530Sstevel@tonic-gate case S_IFSOCK: s = "S_IFSOCK"; break; 2540Sstevel@tonic-gate case S_IFDOOR: s = "S_IFDOOR"; break; 2550Sstevel@tonic-gate case S_IFPORT: s = "S_IFPORT"; break; 2560Sstevel@tonic-gate default: 2570Sstevel@tonic-gate s = unknown; 2580Sstevel@tonic-gate (void) sprintf(s, "0x%.4x ", 2594987Sdanmcd (int)statb.st_mode & S_IFMT); 2600Sstevel@tonic-gate break; 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2634987Sdanmcd (void) printf("%4d: %s mode:0%.3o", fd, s, 2644987Sdanmcd (int)statb.st_mode & ~S_IFMT); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (major(statb.st_dev) != (major_t)NODEV && 2670Sstevel@tonic-gate minor(statb.st_dev) != (minor_t)NODEV) 2680Sstevel@tonic-gate (void) printf(" dev:%lu,%lu", 2694987Sdanmcd (ulong_t)major(statb.st_dev), 2704987Sdanmcd (ulong_t)minor(statb.st_dev)); 2710Sstevel@tonic-gate else 2720Sstevel@tonic-gate (void) printf(" dev:0x%.8lX", (long)statb.st_dev); 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFPORT) { 2750Sstevel@tonic-gate (void) printf(" uid:%d gid:%d", 2760Sstevel@tonic-gate (int)statb.st_uid, 2770Sstevel@tonic-gate (int)statb.st_gid); 2780Sstevel@tonic-gate (void) printf(" size:%lld\n", 2790Sstevel@tonic-gate (longlong_t)statb.st_size); 2800Sstevel@tonic-gate continue; 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate (void) printf(" ino:%llu uid:%d gid:%d", 2844987Sdanmcd (u_longlong_t)statb.st_ino, 2854987Sdanmcd (int)statb.st_uid, (int)statb.st_gid); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate if (rdev == NODEV) 2880Sstevel@tonic-gate (void) printf(" size:%lld\n", 2894987Sdanmcd (longlong_t)statb.st_size); 2900Sstevel@tonic-gate else if (major(rdev) != (major_t)NODEV && 2910Sstevel@tonic-gate minor(rdev) != (minor_t)NODEV) 2920Sstevel@tonic-gate (void) printf(" rdev:%lu,%lu\n", 2934987Sdanmcd (ulong_t)major(rdev), (ulong_t)minor(rdev)); 2940Sstevel@tonic-gate else 2950Sstevel@tonic-gate (void) printf(" rdev:0x%.8lX\n", (long)rdev); 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate if (!nflag) { 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 308*6583Smeem if ((ret = readlink(pname, fname, PATH_MAX - 1)) <= 0) 309*6583Smeem continue; 310*6583Smeem 311*6583Smeem fname[ret] = '\0'; 312*6583Smeem 313*6583Smeem if ((statb.st_mode & S_IFMT) == S_IFCHR && 314*6583Smeem (dev = strrchr(fname, ':')) != NULL) { 315*6583Smeem /* 316*6583Smeem * There's no elegant way to determine if a 317*6583Smeem * character device supports TLI, so we lame 318*6583Smeem * out and just check a hardcoded list of 319*6583Smeem * known TLI devices. 320*6583Smeem */ 321*6583Smeem int i; 322*6583Smeem const char *tlidevs[] = 323*6583Smeem { "tcp", "tcp6", "udp", "udp6", NULL }; 324*6583Smeem 325*6583Smeem dev++; /* skip past the `:' */ 326*6583Smeem for (i = 0; tlidevs[i] != NULL; i++) { 327*6583Smeem if (strcmp(dev, tlidevs[i]) == 0) { 328*6583Smeem dotli(Pr, fd); 329*6583Smeem break; 330*6583Smeem } 331*6583Smeem } 3320Sstevel@tonic-gate } 333*6583Smeem (void) printf(" %s\n", fname); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate (void) closedir(dirp); 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate 3393507Svb160487 3403507Svb160487 static int 3413507Svb160487 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native) 3423507Svb160487 { 3433507Svb160487 int ret; 3443507Svb160487 #ifdef _LP64 3453507Svb160487 struct flock64_32 flock_target; 3463507Svb160487 3473507Svb160487 if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) { 3483507Svb160487 copyflock(flock_target, *flock_native); 3493507Svb160487 ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target); 3503507Svb160487 copyflock(*flock_native, flock_target); 3513507Svb160487 return (ret); 3523507Svb160487 } 3533507Svb160487 #endif /* _LP64 */ 3543507Svb160487 ret = pr_fcntl(Pr, fd, F_GETLK, flock_native); 3553507Svb160487 return (ret); 3563507Svb160487 } 3573507Svb160487 3580Sstevel@tonic-gate /* examine open file with fcntl() */ 3590Sstevel@tonic-gate static void 3604987Sdanmcd dofcntl(struct ps_prochandle *Pr, int fd, int mandatory, int isdoor) 3610Sstevel@tonic-gate { 3620Sstevel@tonic-gate struct flock flock; 3630Sstevel@tonic-gate int fileflags; 3640Sstevel@tonic-gate int fdflags; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0); 3670Sstevel@tonic-gate fdflags = pr_fcntl(Pr, fd, F_GETFD, 0); 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate if (fileflags != -1 || fdflags != -1) { 3700Sstevel@tonic-gate (void) printf(" "); 3710Sstevel@tonic-gate if (fileflags != -1) 3720Sstevel@tonic-gate show_fileflags(fileflags); 3730Sstevel@tonic-gate if (fdflags != -1 && (fdflags & FD_CLOEXEC)) 3740Sstevel@tonic-gate (void) printf(" FD_CLOEXEC"); 3750Sstevel@tonic-gate if (isdoor) 3760Sstevel@tonic-gate show_door(Pr, fd); 3770Sstevel@tonic-gate (void) fputc('\n', stdout); 3780Sstevel@tonic-gate } else if (isdoor) { 3790Sstevel@tonic-gate (void) printf(" "); 3800Sstevel@tonic-gate show_door(Pr, fd); 3810Sstevel@tonic-gate (void) fputc('\n', stdout); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate flock.l_type = F_WRLCK; 3850Sstevel@tonic-gate flock.l_whence = 0; 3860Sstevel@tonic-gate flock.l_start = 0; 3870Sstevel@tonic-gate flock.l_len = 0; 3880Sstevel@tonic-gate flock.l_sysid = 0; 3890Sstevel@tonic-gate flock.l_pid = 0; 3903507Svb160487 if (getflock(Pr, fd, &flock) != -1) { 3910Sstevel@tonic-gate if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) { 3920Sstevel@tonic-gate unsigned long sysid = flock.l_sysid; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate (void) printf(" %s %s lock set by", 3954987Sdanmcd mandatory ? "mandatory" : "advisory", 3964987Sdanmcd flock.l_type == F_RDLCK? "read" : "write"); 3970Sstevel@tonic-gate if (sysid) 3980Sstevel@tonic-gate (void) printf(" system 0x%lX", sysid); 3990Sstevel@tonic-gate if (flock.l_pid) 4000Sstevel@tonic-gate (void) printf(" process %d", (int)flock.l_pid); 4010Sstevel@tonic-gate (void) fputc('\n', stdout); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate #ifdef O_PRIV 4070Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 4080Sstevel@tonic-gate O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 4090Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 4100Sstevel@tonic-gate #else 4110Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 4120Sstevel@tonic-gate O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 4130Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 4140Sstevel@tonic-gate #endif 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate static void 4170Sstevel@tonic-gate show_fileflags(int flags) 4180Sstevel@tonic-gate { 4190Sstevel@tonic-gate char buffer[136]; 4200Sstevel@tonic-gate char *str = buffer; 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate switch (flags & O_ACCMODE) { 4230Sstevel@tonic-gate case O_RDONLY: 4240Sstevel@tonic-gate (void) strcpy(str, "O_RDONLY"); 4250Sstevel@tonic-gate break; 4260Sstevel@tonic-gate case O_WRONLY: 4270Sstevel@tonic-gate (void) strcpy(str, "O_WRONLY"); 4280Sstevel@tonic-gate break; 4290Sstevel@tonic-gate case O_RDWR: 4300Sstevel@tonic-gate (void) strcpy(str, "O_RDWR"); 4310Sstevel@tonic-gate break; 4320Sstevel@tonic-gate default: 4330Sstevel@tonic-gate (void) sprintf(str, "0x%x", flags & O_ACCMODE); 4340Sstevel@tonic-gate break; 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate if (flags & O_NDELAY) 4380Sstevel@tonic-gate (void) strcat(str, "|O_NDELAY"); 4390Sstevel@tonic-gate if (flags & O_NONBLOCK) 4400Sstevel@tonic-gate (void) strcat(str, "|O_NONBLOCK"); 4410Sstevel@tonic-gate if (flags & O_APPEND) 4420Sstevel@tonic-gate (void) strcat(str, "|O_APPEND"); 4430Sstevel@tonic-gate #ifdef O_PRIV 4440Sstevel@tonic-gate if (flags & O_PRIV) 4450Sstevel@tonic-gate (void) strcat(str, "|O_PRIV"); 4460Sstevel@tonic-gate #endif 4470Sstevel@tonic-gate if (flags & O_SYNC) 4480Sstevel@tonic-gate (void) strcat(str, "|O_SYNC"); 4490Sstevel@tonic-gate if (flags & O_DSYNC) 4500Sstevel@tonic-gate (void) strcat(str, "|O_DSYNC"); 4510Sstevel@tonic-gate if (flags & O_RSYNC) 4520Sstevel@tonic-gate (void) strcat(str, "|O_RSYNC"); 4530Sstevel@tonic-gate if (flags & O_CREAT) 4540Sstevel@tonic-gate (void) strcat(str, "|O_CREAT"); 4550Sstevel@tonic-gate if (flags & O_TRUNC) 4560Sstevel@tonic-gate (void) strcat(str, "|O_TRUNC"); 4570Sstevel@tonic-gate if (flags & O_EXCL) 4580Sstevel@tonic-gate (void) strcat(str, "|O_EXCL"); 4590Sstevel@tonic-gate if (flags & O_NOCTTY) 4600Sstevel@tonic-gate (void) strcat(str, "|O_NOCTTY"); 4610Sstevel@tonic-gate if (flags & O_LARGEFILE) 4620Sstevel@tonic-gate (void) strcat(str, "|O_LARGEFILE"); 4630Sstevel@tonic-gate if (flags & O_XATTR) 4640Sstevel@tonic-gate (void) strcat(str, "|O_XATTR"); 4650Sstevel@tonic-gate if (flags & ~(ALL_O_FLAGS)) 4660Sstevel@tonic-gate (void) sprintf(str + strlen(str), "|0x%x", 4674987Sdanmcd flags & ~(ALL_O_FLAGS)); 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate (void) printf("%s", str); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* show door info */ 4730Sstevel@tonic-gate static void 4740Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd) 4750Sstevel@tonic-gate { 4760Sstevel@tonic-gate door_info_t door_info; 4770Sstevel@tonic-gate psinfo_t psinfo; 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate if (pr_door_info(Pr, fd, &door_info) != 0) 4800Sstevel@tonic-gate return; 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate if (proc_get_psinfo(door_info.di_target, &psinfo) != 0) 4830Sstevel@tonic-gate psinfo.pr_fname[0] = '\0'; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate (void) printf(" door to "); 4860Sstevel@tonic-gate if (psinfo.pr_fname[0] != '\0') 4870Sstevel@tonic-gate (void) printf("%s[%d]", psinfo.pr_fname, 4884987Sdanmcd (int)door_info.di_target); 4890Sstevel@tonic-gate else 4900Sstevel@tonic-gate (void) printf("pid %d", (int)door_info.di_target); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate 493*6583Smeem /* 494*6583Smeem * Print out the socket address pointed to by `sa'. `len' is only 495*6583Smeem * needed for AF_UNIX sockets. 496*6583Smeem */ 4970Sstevel@tonic-gate static void 4980Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len) 4990Sstevel@tonic-gate { 500*6583Smeem struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa; 501*6583Smeem struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa; 5020Sstevel@tonic-gate struct sockaddr_un *so_un = (struct sockaddr_un *)sa; 5030Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 5040Sstevel@tonic-gate const char *p; 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate switch (sa->sa_family) { 5070Sstevel@tonic-gate default: 5080Sstevel@tonic-gate return; 5090Sstevel@tonic-gate case AF_INET: 510*6583Smeem (void) printf("\t%s: AF_INET %s port: %u\n", str, 5110Sstevel@tonic-gate inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)), 5120Sstevel@tonic-gate ntohs(so_in->sin_port)); 5130Sstevel@tonic-gate return; 5140Sstevel@tonic-gate case AF_INET6: 515*6583Smeem (void) printf("\t%s: AF_INET6 %s port: %u\n", str, 5160Sstevel@tonic-gate inet_ntop(AF_INET6, &so_in6->sin6_addr, 5170Sstevel@tonic-gate abuf, sizeof (abuf)), 5180Sstevel@tonic-gate ntohs(so_in->sin_port)); 5190Sstevel@tonic-gate return; 5200Sstevel@tonic-gate case AF_UNIX: 5210Sstevel@tonic-gate if (len >= sizeof (so_un->sun_family)) { 5220Sstevel@tonic-gate /* Null terminate */ 5230Sstevel@tonic-gate len -= sizeof (so_un->sun_family); 524*6583Smeem so_un->sun_path[len] = '\0'; 5250Sstevel@tonic-gate (void) printf("\t%s: AF_UNIX %s\n", 5264987Sdanmcd str, so_un->sun_path); 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate return; 5290Sstevel@tonic-gate case AF_IMPLINK: p = "AF_IMPLINK"; break; 5300Sstevel@tonic-gate case AF_PUP: p = "AF_PUP"; break; 5310Sstevel@tonic-gate case AF_CHAOS: p = "AF_CHAOS"; break; 5320Sstevel@tonic-gate case AF_NS: p = "AF_NS"; break; 5330Sstevel@tonic-gate case AF_NBS: p = "AF_NBS"; break; 5340Sstevel@tonic-gate case AF_ECMA: p = "AF_ECMA"; break; 5350Sstevel@tonic-gate case AF_DATAKIT: p = "AF_DATAKIT"; break; 5360Sstevel@tonic-gate case AF_CCITT: p = "AF_CCITT"; break; 5370Sstevel@tonic-gate case AF_SNA: p = "AF_SNA"; break; 5380Sstevel@tonic-gate case AF_DECnet: p = "AF_DECnet"; break; 5390Sstevel@tonic-gate case AF_DLI: p = "AF_DLI"; break; 5400Sstevel@tonic-gate case AF_LAT: p = "AF_LAT"; break; 5410Sstevel@tonic-gate case AF_HYLINK: p = "AF_HYLINK"; break; 5420Sstevel@tonic-gate case AF_APPLETALK: p = "AF_APPLETALK"; break; 5430Sstevel@tonic-gate case AF_NIT: p = "AF_NIT"; break; 5440Sstevel@tonic-gate case AF_802: p = "AF_802"; break; 5450Sstevel@tonic-gate case AF_OSI: p = "AF_OSI"; break; 5460Sstevel@tonic-gate case AF_X25: p = "AF_X25"; break; 5470Sstevel@tonic-gate case AF_OSINET: p = "AF_OSINET"; break; 5480Sstevel@tonic-gate case AF_GOSIP: p = "AF_GOSIP"; break; 5490Sstevel@tonic-gate case AF_IPX: p = "AF_IPX"; break; 5500Sstevel@tonic-gate case AF_ROUTE: p = "AF_ROUTE"; break; 5510Sstevel@tonic-gate case AF_LINK: p = "AF_LINK"; break; 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate (void) printf("\t%s: %s\n", str, p); 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate static void 5580Sstevel@tonic-gate show_socktype(uint_t type) 5590Sstevel@tonic-gate { 5600Sstevel@tonic-gate static const char *types[] = { 5610Sstevel@tonic-gate NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET" 5620Sstevel@tonic-gate }; 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate if (type < sizeof (types) / sizeof (*types) && types[type] != NULL) 5650Sstevel@tonic-gate (void) printf("\tSOCK_%s\n", types[type]); 5660Sstevel@tonic-gate else 5670Sstevel@tonic-gate (void) printf("\tunknown socket type %u\n", type); 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate 5701095Spriyanka #define BUFSIZE 200 5710Sstevel@tonic-gate static void 5720Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd) 5730Sstevel@tonic-gate { 5740Sstevel@tonic-gate int val, vlen; 5751095Spriyanka char buf[BUFSIZE]; 5760Sstevel@tonic-gate char buf1[32]; 5771095Spriyanka char ipaddr[INET_ADDRSTRLEN]; 5780Sstevel@tonic-gate int i; 5791095Spriyanka in_addr_t nexthop_val; 5800Sstevel@tonic-gate struct boolopt { 5814987Sdanmcd int level; 5820Sstevel@tonic-gate int opt; 5830Sstevel@tonic-gate const char *name; 5840Sstevel@tonic-gate }; 5850Sstevel@tonic-gate static struct boolopt boolopts[] = { 5864987Sdanmcd { SOL_SOCKET, SO_DEBUG, "SO_DEBUG," }, 5874987Sdanmcd { SOL_SOCKET, SO_REUSEADDR, "SO_REUSEADDR," }, 5884987Sdanmcd { SOL_SOCKET, SO_KEEPALIVE, "SO_KEEPALIVE," }, 5894987Sdanmcd { SOL_SOCKET, SO_DONTROUTE, "SO_DONTROUTE," }, 5904987Sdanmcd { SOL_SOCKET, SO_BROADCAST, "SO_BROADCAST," }, 5914987Sdanmcd { SOL_SOCKET, SO_OOBINLINE, "SO_OOBINLINE," }, 5924987Sdanmcd { SOL_SOCKET, SO_DGRAM_ERRIND, "SO_DGRAM_ERRIND,"}, 5934987Sdanmcd { SOL_SOCKET, SO_ALLZONES, "SO_ALLZONES," }, 5944987Sdanmcd { SOL_SOCKET, SO_EXCLBIND, "SO_EXCLBIND," }, 5954987Sdanmcd { IPPROTO_UDP, UDP_NAT_T_ENDPOINT, "UDP_NAT_T_ENDPOINT," }, 5960Sstevel@tonic-gate }; 5970Sstevel@tonic-gate struct linger l; 5980Sstevel@tonic-gate 5992263Ssommerfe buf[0] = '!'; /* sentinel value, never printed */ 6000Sstevel@tonic-gate buf[1] = '\0'; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) { 6030Sstevel@tonic-gate vlen = sizeof (val); 6044987Sdanmcd if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt, 6054987Sdanmcd &val, &vlen) == 0 && val != 0) 6060Sstevel@tonic-gate (void) strlcat(buf, boolopts[i].name, sizeof (buf)); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate vlen = sizeof (l); 6100Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 && 6110Sstevel@tonic-gate l.l_onoff != 0) { 6120Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),", 6130Sstevel@tonic-gate l.l_linger); 6140Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate vlen = sizeof (val); 6180Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) { 6190Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val); 6200Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate vlen = sizeof (val); 6230Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) { 6240Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val); 6250Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 6260Sstevel@tonic-gate } 6271095Spriyanka vlen = sizeof (nexthop_val); 6281095Spriyanka if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val, 6291095Spriyanka &vlen) == 0) { 6301628Spriyanka if (vlen > 0) { 6311628Spriyanka (void) inet_ntop(AF_INET, (void *) &nexthop_val, 6321628Spriyanka ipaddr, sizeof (ipaddr)); 6331628Spriyanka (void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),", 6341628Spriyanka ipaddr); 6351628Spriyanka (void) strlcat(buf, buf1, sizeof (buf)); 6361628Spriyanka } 6371095Spriyanka } 6380Sstevel@tonic-gate 6392263Ssommerfe buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */ 6400Sstevel@tonic-gate if (buf[1] != '\0') 6410Sstevel@tonic-gate (void) printf("\t%s\n", buf+1); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* the file is a socket */ 6450Sstevel@tonic-gate static void 6460Sstevel@tonic-gate dosocket(struct ps_prochandle *Pr, int fd) 6470Sstevel@tonic-gate { 6480Sstevel@tonic-gate /* A buffer large enough for PATH_MAX size AF_UNIX address */ 6490Sstevel@tonic-gate long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1) 6504987Sdanmcd / sizeof (long)]; 6510Sstevel@tonic-gate struct sockaddr *sa = (struct sockaddr *)buf; 6520Sstevel@tonic-gate socklen_t len; 6530Sstevel@tonic-gate int type, tlen; 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate tlen = sizeof (type); 656*6583Smeem if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0) 6570Sstevel@tonic-gate show_socktype((uint_t)type); 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate show_sockopts(Pr, fd); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate len = sizeof (buf); 6620Sstevel@tonic-gate if (pr_getsockname(Pr, fd, sa, &len) == 0) 6630Sstevel@tonic-gate show_sockaddr("sockname", sa, len); 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate len = sizeof (buf); 6660Sstevel@tonic-gate if (pr_getpeername(Pr, fd, sa, &len) == 0) 6670Sstevel@tonic-gate show_sockaddr("peername", sa, len); 6680Sstevel@tonic-gate } 669*6583Smeem 670*6583Smeem /* the file is a TLI endpoint */ 671*6583Smeem static void 672*6583Smeem dotli(struct ps_prochandle *Pr, int fd) 673*6583Smeem { 674*6583Smeem struct strcmd strcmd; 675*6583Smeem 676*6583Smeem strcmd.sc_len = STRCMDBUFSIZE; 677*6583Smeem strcmd.sc_timeout = 5; 678*6583Smeem 679*6583Smeem strcmd.sc_cmd = TI_GETMYNAME; 680*6583Smeem if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0) 681*6583Smeem show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0); 682*6583Smeem 683*6583Smeem strcmd.sc_cmd = TI_GETPEERNAME; 684*6583Smeem if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0) 685*6583Smeem show_sockaddr("peername", (void *)&strcmd.sc_buf, 0); 686*6583Smeem } 687