xref: /titanic_50/usr/src/cmd/ptools/pfiles/pfiles.c (revision ca9327a6de44d69ddab3668cc1e143ce781387a3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52caf0dcdSrshoaib  * Common Development and Distribution License (the "License").
62caf0dcdSrshoaib  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212caf0dcdSrshoaib 
227c478bd9Sstevel@tonic-gate /*
23*ca9327a6Smeem  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <dirent.h>
377c478bd9Sstevel@tonic-gate #include <limits.h>
387c478bd9Sstevel@tonic-gate #include <door.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
4043d18f1cSpriyanka #include <sys/socket.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
43*ca9327a6Smeem #include <sys/stropts.h>
44*ca9327a6Smeem #include <sys/timod.h>
457c478bd9Sstevel@tonic-gate #include <sys/un.h>
467c478bd9Sstevel@tonic-gate #include <libproc.h>
4743d18f1cSpriyanka #include <netinet/in.h>
48437220cdSdanmcd #include <netinet/udp.h>
497c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
507c478bd9Sstevel@tonic-gate 
51150d2c52Svb160487 #define	copyflock(dst, src) \
52150d2c52Svb160487 	(dst).l_type = (src).l_type;		\
53150d2c52Svb160487 	(dst).l_whence = (src).l_whence;	\
54150d2c52Svb160487 	(dst).l_start = (src).l_start;		\
55150d2c52Svb160487 	(dst).l_len = (src).l_len;		\
56150d2c52Svb160487 	(dst).l_sysid = (src).l_sysid;		\
57150d2c52Svb160487 	(dst).l_pid = (src).l_pid;
58150d2c52Svb160487 
597c478bd9Sstevel@tonic-gate static char *command;
607c478bd9Sstevel@tonic-gate static volatile int interrupt;
617c478bd9Sstevel@tonic-gate static int Fflag;
627c478bd9Sstevel@tonic-gate static boolean_t nflag = B_FALSE;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static	void	intr(int);
657c478bd9Sstevel@tonic-gate static	void	dofcntl(struct ps_prochandle *, int, int, int);
667c478bd9Sstevel@tonic-gate static	void	dosocket(struct ps_prochandle *, int);
67*ca9327a6Smeem static	void	dotli(struct ps_prochandle *, int);
687c478bd9Sstevel@tonic-gate static	void	show_files(struct ps_prochandle *);
697c478bd9Sstevel@tonic-gate static	void	show_fileflags(int);
707c478bd9Sstevel@tonic-gate static	void	show_door(struct ps_prochandle *, int);
71150d2c52Svb160487 static	int	getflock(struct ps_prochandle *, int, struct flock *);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate int
747c478bd9Sstevel@tonic-gate main(int argc, char **argv)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	int retc = 0;
777c478bd9Sstevel@tonic-gate 	int opt;
787c478bd9Sstevel@tonic-gate 	int errflg = 0;
797c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
827c478bd9Sstevel@tonic-gate 		command++;
837c478bd9Sstevel@tonic-gate 	else
847c478bd9Sstevel@tonic-gate 		command = argv[0];
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/* options */
877c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "Fn")) != EOF) {
887c478bd9Sstevel@tonic-gate 		switch (opt) {
897c478bd9Sstevel@tonic-gate 		case 'F':		/* force grabbing (no O_EXCL) */
907c478bd9Sstevel@tonic-gate 			Fflag = PGRAB_FORCE;
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 		case 'n':
937c478bd9Sstevel@tonic-gate 			nflag = B_TRUE;
947c478bd9Sstevel@tonic-gate 			break;
957c478bd9Sstevel@tonic-gate 		default:
967c478bd9Sstevel@tonic-gate 			errflg = 1;
977c478bd9Sstevel@tonic-gate 			break;
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	argc -= optind;
1027c478bd9Sstevel@tonic-gate 	argv += optind;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (errflg || argc <= 0) {
1057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "usage:\t%s [-F] pid ...\n",
1067c478bd9Sstevel@tonic-gate 		    command);
1077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1087c478bd9Sstevel@tonic-gate 		    "  (report open files of each process)\n");
1097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1107c478bd9Sstevel@tonic-gate 		    "  -F: force grabbing of the target process\n");
1117c478bd9Sstevel@tonic-gate 		exit(2);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* catch signals from terminal */
1157c478bd9Sstevel@tonic-gate 	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
1167c478bd9Sstevel@tonic-gate 		(void) sigset(SIGHUP, intr);
1177c478bd9Sstevel@tonic-gate 	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
1187c478bd9Sstevel@tonic-gate 		(void) sigset(SIGINT, intr);
1197c478bd9Sstevel@tonic-gate 	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
1207c478bd9Sstevel@tonic-gate 		(void) sigset(SIGQUIT, intr);
1217c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, intr);
1227c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, intr);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) proc_initstdio();
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	while (--argc >= 0 && !interrupt) {
1287c478bd9Sstevel@tonic-gate 		char *arg;
1297c478bd9Sstevel@tonic-gate 		psinfo_t psinfo;
1307c478bd9Sstevel@tonic-gate 		pid_t pid;
1317c478bd9Sstevel@tonic-gate 		int gret;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		(void) proc_flushstdio();
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		/* get the specified pid and the psinfo struct */
1367c478bd9Sstevel@tonic-gate 		if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS,
1377c478bd9Sstevel@tonic-gate 		    &psinfo, &gret)) == -1) {
1387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1397c478bd9Sstevel@tonic-gate 			    command, arg, Pgrab_error(gret));
1407c478bd9Sstevel@tonic-gate 			retc++;
1417c478bd9Sstevel@tonic-gate 		} else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) {
1427c478bd9Sstevel@tonic-gate 			if (Pcreate_agent(Pr) == 0) {
1437c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
1447c478bd9Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n",
1457c478bd9Sstevel@tonic-gate 				    (int)pid, psinfo.pr_psargs);
1467c478bd9Sstevel@tonic-gate 				show_files(Pr);
1477c478bd9Sstevel@tonic-gate 				Pdestroy_agent(Pr);
1487c478bd9Sstevel@tonic-gate 			} else {
1497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1507c478bd9Sstevel@tonic-gate 				    "%s: cannot control process %d\n",
1517c478bd9Sstevel@tonic-gate 				    command, (int)pid);
1527c478bd9Sstevel@tonic-gate 				retc++;
1537c478bd9Sstevel@tonic-gate 			}
1547c478bd9Sstevel@tonic-gate 			Prelease(Pr, 0);
1557c478bd9Sstevel@tonic-gate 			Pr = NULL;
1567c478bd9Sstevel@tonic-gate 		} else {
1577c478bd9Sstevel@tonic-gate 			switch (gret) {
1587c478bd9Sstevel@tonic-gate 			case G_SYS:
1597c478bd9Sstevel@tonic-gate 			case G_SELF:
1607c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
1617c478bd9Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n", (int)pid,
1627c478bd9Sstevel@tonic-gate 				    psinfo.pr_psargs);
1637c478bd9Sstevel@tonic-gate 				if (gret == G_SYS)
1647c478bd9Sstevel@tonic-gate 					(void) printf("  [system process]\n");
1657c478bd9Sstevel@tonic-gate 				else
1667c478bd9Sstevel@tonic-gate 					show_files(NULL);
1677c478bd9Sstevel@tonic-gate 				break;
1687c478bd9Sstevel@tonic-gate 			default:
1697c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: %s: %d\n",
1707c478bd9Sstevel@tonic-gate 				    command, Pgrab_error(gret), (int)pid);
1717c478bd9Sstevel@tonic-gate 				retc++;
1727c478bd9Sstevel@tonic-gate 				break;
1737c478bd9Sstevel@tonic-gate 			}
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	(void) proc_finistdio();
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (interrupt && retc == 0)
1807c478bd9Sstevel@tonic-gate 		retc++;
1817c478bd9Sstevel@tonic-gate 	return (retc);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /* ARGSUSED */
1857c478bd9Sstevel@tonic-gate static void
1867c478bd9Sstevel@tonic-gate intr(int sig)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	interrupt = 1;
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /* ------ begin specific code ------ */
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static void
1947c478bd9Sstevel@tonic-gate show_files(struct ps_prochandle *Pr)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	DIR *dirp;
1977c478bd9Sstevel@tonic-gate 	struct dirent *dentp;
198*ca9327a6Smeem 	const char *dev;
1997c478bd9Sstevel@tonic-gate 	char pname[100];
2007c478bd9Sstevel@tonic-gate 	char fname[PATH_MAX];
2017c478bd9Sstevel@tonic-gate 	struct stat64 statb;
2027c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
2037c478bd9Sstevel@tonic-gate 	pid_t pid;
2047c478bd9Sstevel@tonic-gate 	int fd;
2057c478bd9Sstevel@tonic-gate 	char *s;
2067c478bd9Sstevel@tonic-gate 	int ret;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) {
2097c478bd9Sstevel@tonic-gate 		ulong_t nfd = rlim.rlim_cur;
2107c478bd9Sstevel@tonic-gate 		if (nfd == RLIM_INFINITY)
2117c478bd9Sstevel@tonic-gate 			(void) printf(
2127c478bd9Sstevel@tonic-gate 			    "  Current rlimit: unlimited file descriptors\n");
2137c478bd9Sstevel@tonic-gate 		else
2147c478bd9Sstevel@tonic-gate 			(void) printf(
2157c478bd9Sstevel@tonic-gate 			    "  Current rlimit: %lu file descriptors\n", nfd);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/* in case we are doing this to ourself */
2197c478bd9Sstevel@tonic-gate 	pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	(void) sprintf(pname, "/proc/%d/fd", (int)pid);
2227c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(pname)) == NULL) {
2237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot open directory %s\n",
2247c478bd9Sstevel@tonic-gate 		    command, pname);
2257c478bd9Sstevel@tonic-gate 		return;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/* for each open file --- */
2297c478bd9Sstevel@tonic-gate 	while ((dentp = readdir(dirp)) != NULL && !interrupt) {
2307c478bd9Sstevel@tonic-gate 		char unknown[12];
2317c478bd9Sstevel@tonic-gate 		dev_t rdev;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		/* skip '.' and '..' */
2347c478bd9Sstevel@tonic-gate 		if (!isdigit(dentp->d_name[0]))
2357c478bd9Sstevel@tonic-gate 			continue;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		fd = atoi(dentp->d_name);
2387c478bd9Sstevel@tonic-gate 		if (pr_fstat64(Pr, fd, &statb) == -1) {
2397c478bd9Sstevel@tonic-gate 			s = unknown;
2407c478bd9Sstevel@tonic-gate 			(void) sprintf(s, "%4d", fd);
2417c478bd9Sstevel@tonic-gate 			perror(s);
2427c478bd9Sstevel@tonic-gate 			continue;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		rdev = NODEV;
2467c478bd9Sstevel@tonic-gate 		switch (statb.st_mode & S_IFMT) {
2477c478bd9Sstevel@tonic-gate 		case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break;
2487c478bd9Sstevel@tonic-gate 		case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break;
2497c478bd9Sstevel@tonic-gate 		case S_IFIFO: s = "S_IFIFO"; break;
2507c478bd9Sstevel@tonic-gate 		case S_IFDIR: s = "S_IFDIR"; break;
2517c478bd9Sstevel@tonic-gate 		case S_IFREG: s = "S_IFREG"; break;
2527c478bd9Sstevel@tonic-gate 		case S_IFLNK: s = "S_IFLNK"; break;
2537c478bd9Sstevel@tonic-gate 		case S_IFSOCK: s = "S_IFSOCK"; break;
2547c478bd9Sstevel@tonic-gate 		case S_IFDOOR: s = "S_IFDOOR"; break;
2557c478bd9Sstevel@tonic-gate 		case S_IFPORT: s = "S_IFPORT"; break;
2567c478bd9Sstevel@tonic-gate 		default:
2577c478bd9Sstevel@tonic-gate 			s = unknown;
2587c478bd9Sstevel@tonic-gate 			(void) sprintf(s, "0x%.4x ",
2597c478bd9Sstevel@tonic-gate 			    (int)statb.st_mode & S_IFMT);
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 
263437220cdSdanmcd 		(void) printf("%4d: %s mode:0%.3o", fd, s,
2647c478bd9Sstevel@tonic-gate 		    (int)statb.st_mode & ~S_IFMT);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		if (major(statb.st_dev) != (major_t)NODEV &&
2677c478bd9Sstevel@tonic-gate 		    minor(statb.st_dev) != (minor_t)NODEV)
2687c478bd9Sstevel@tonic-gate 			(void) printf(" dev:%lu,%lu",
2697c478bd9Sstevel@tonic-gate 			    (ulong_t)major(statb.st_dev),
2707c478bd9Sstevel@tonic-gate 			    (ulong_t)minor(statb.st_dev));
2717c478bd9Sstevel@tonic-gate 		else
2727c478bd9Sstevel@tonic-gate 			(void) printf(" dev:0x%.8lX", (long)statb.st_dev);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		if ((statb.st_mode & S_IFMT) == S_IFPORT) {
2757c478bd9Sstevel@tonic-gate 			(void) printf(" uid:%d gid:%d",
2767c478bd9Sstevel@tonic-gate 			    (int)statb.st_uid,
2777c478bd9Sstevel@tonic-gate 			    (int)statb.st_gid);
2787c478bd9Sstevel@tonic-gate 			(void) printf(" size:%lld\n",
2797c478bd9Sstevel@tonic-gate 			    (longlong_t)statb.st_size);
2807c478bd9Sstevel@tonic-gate 			continue;
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		(void) printf(" ino:%llu uid:%d gid:%d",
2847c478bd9Sstevel@tonic-gate 		    (u_longlong_t)statb.st_ino,
285437220cdSdanmcd 		    (int)statb.st_uid, (int)statb.st_gid);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		if (rdev == NODEV)
2887c478bd9Sstevel@tonic-gate 			(void) printf(" size:%lld\n",
2897c478bd9Sstevel@tonic-gate 			    (longlong_t)statb.st_size);
2907c478bd9Sstevel@tonic-gate 		else if (major(rdev) != (major_t)NODEV &&
2917c478bd9Sstevel@tonic-gate 		    minor(rdev) != (minor_t)NODEV)
2927c478bd9Sstevel@tonic-gate 			(void) printf(" rdev:%lu,%lu\n",
293437220cdSdanmcd 			    (ulong_t)major(rdev), (ulong_t)minor(rdev));
2947c478bd9Sstevel@tonic-gate 		else
2957c478bd9Sstevel@tonic-gate 			(void) printf(" rdev:0x%.8lX\n", (long)rdev);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		if (!nflag) {
2987c478bd9Sstevel@tonic-gate 			dofcntl(Pr, fd,
2997c478bd9Sstevel@tonic-gate 			    (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP))
3007c478bd9Sstevel@tonic-gate 			    == (S_IFREG|S_ENFMT),
3017c478bd9Sstevel@tonic-gate 			    (statb.st_mode & S_IFMT) == S_IFDOOR);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			if ((statb.st_mode & S_IFMT) == S_IFSOCK)
3047c478bd9Sstevel@tonic-gate 				dosocket(Pr, fd);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 			(void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd);
3077c478bd9Sstevel@tonic-gate 
308*ca9327a6Smeem 			if ((ret = readlink(pname, fname, PATH_MAX - 1)) <= 0)
309*ca9327a6Smeem 				continue;
310*ca9327a6Smeem 
3117c478bd9Sstevel@tonic-gate 			fname[ret] = '\0';
312*ca9327a6Smeem 
313*ca9327a6Smeem 			if ((statb.st_mode & S_IFMT) == S_IFCHR &&
314*ca9327a6Smeem 			    (dev = strrchr(fname, ':')) != NULL) {
315*ca9327a6Smeem 				/*
316*ca9327a6Smeem 				 * There's no elegant way to determine if a
317*ca9327a6Smeem 				 * character device supports TLI, so we lame
318*ca9327a6Smeem 				 * out and just check a hardcoded list of
319*ca9327a6Smeem 				 * known TLI devices.
320*ca9327a6Smeem 				 */
321*ca9327a6Smeem 				int i;
322*ca9327a6Smeem 				const char *tlidevs[] =
323*ca9327a6Smeem 				    { "tcp", "tcp6", "udp", "udp6", NULL };
324*ca9327a6Smeem 
325*ca9327a6Smeem 				dev++; /* skip past the `:' */
326*ca9327a6Smeem 				for (i = 0; tlidevs[i] != NULL; i++) {
327*ca9327a6Smeem 					if (strcmp(dev, tlidevs[i]) == 0) {
328*ca9327a6Smeem 						dotli(Pr, fd);
329*ca9327a6Smeem 						break;
330*ca9327a6Smeem 					}
331*ca9327a6Smeem 				}
332*ca9327a6Smeem 			}
3337c478bd9Sstevel@tonic-gate 			(void) printf("      %s\n", fname);
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
339150d2c52Svb160487 
340150d2c52Svb160487 static int
341150d2c52Svb160487 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native)
342150d2c52Svb160487 {
343150d2c52Svb160487 	int ret;
344150d2c52Svb160487 #ifdef _LP64
345150d2c52Svb160487 	struct flock64_32 flock_target;
346150d2c52Svb160487 
347150d2c52Svb160487 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) {
348150d2c52Svb160487 		copyflock(flock_target, *flock_native);
349150d2c52Svb160487 		ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target);
350150d2c52Svb160487 		copyflock(*flock_native, flock_target);
351150d2c52Svb160487 		return (ret);
352150d2c52Svb160487 	}
353150d2c52Svb160487 #endif /* _LP64 */
354150d2c52Svb160487 	ret = pr_fcntl(Pr, fd, F_GETLK, flock_native);
355150d2c52Svb160487 	return (ret);
356150d2c52Svb160487 }
357150d2c52Svb160487 
3587c478bd9Sstevel@tonic-gate /* examine open file with fcntl() */
3597c478bd9Sstevel@tonic-gate static void
360437220cdSdanmcd dofcntl(struct ps_prochandle *Pr, int fd, int mandatory, int isdoor)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	struct flock flock;
3637c478bd9Sstevel@tonic-gate 	int fileflags;
3647c478bd9Sstevel@tonic-gate 	int fdflags;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0);
3677c478bd9Sstevel@tonic-gate 	fdflags = pr_fcntl(Pr, fd, F_GETFD, 0);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (fileflags != -1 || fdflags != -1) {
3707c478bd9Sstevel@tonic-gate 		(void) printf("      ");
3717c478bd9Sstevel@tonic-gate 		if (fileflags != -1)
3727c478bd9Sstevel@tonic-gate 			show_fileflags(fileflags);
3737c478bd9Sstevel@tonic-gate 		if (fdflags != -1 && (fdflags & FD_CLOEXEC))
3747c478bd9Sstevel@tonic-gate 			(void) printf(" FD_CLOEXEC");
3757c478bd9Sstevel@tonic-gate 		if (isdoor)
3767c478bd9Sstevel@tonic-gate 			show_door(Pr, fd);
3777c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
3787c478bd9Sstevel@tonic-gate 	} else if (isdoor) {
3797c478bd9Sstevel@tonic-gate 		(void) printf("    ");
3807c478bd9Sstevel@tonic-gate 		show_door(Pr, fd);
3817c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
3857c478bd9Sstevel@tonic-gate 	flock.l_whence = 0;
3867c478bd9Sstevel@tonic-gate 	flock.l_start = 0;
3877c478bd9Sstevel@tonic-gate 	flock.l_len = 0;
3887c478bd9Sstevel@tonic-gate 	flock.l_sysid = 0;
3897c478bd9Sstevel@tonic-gate 	flock.l_pid = 0;
390150d2c52Svb160487 	if (getflock(Pr, fd, &flock) != -1) {
3917c478bd9Sstevel@tonic-gate 		if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) {
3927c478bd9Sstevel@tonic-gate 			unsigned long sysid = flock.l_sysid;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 			(void) printf("      %s %s lock set by",
395437220cdSdanmcd 			    mandatory ? "mandatory" : "advisory",
3967c478bd9Sstevel@tonic-gate 			    flock.l_type == F_RDLCK? "read" : "write");
3977c478bd9Sstevel@tonic-gate 			if (sysid)
3987c478bd9Sstevel@tonic-gate 				(void) printf(" system 0x%lX", sysid);
3997c478bd9Sstevel@tonic-gate 			if (flock.l_pid)
4007c478bd9Sstevel@tonic-gate 				(void) printf(" process %d", (int)flock.l_pid);
4017c478bd9Sstevel@tonic-gate 			(void) fputc('\n', stdout);
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate #ifdef O_PRIV
4077c478bd9Sstevel@tonic-gate #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
4087c478bd9Sstevel@tonic-gate 			O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
4097c478bd9Sstevel@tonic-gate 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
4107c478bd9Sstevel@tonic-gate #else
4117c478bd9Sstevel@tonic-gate #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
4127c478bd9Sstevel@tonic-gate 			O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
4137c478bd9Sstevel@tonic-gate 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
4147c478bd9Sstevel@tonic-gate #endif
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate static void
4177c478bd9Sstevel@tonic-gate show_fileflags(int flags)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	char buffer[136];
4207c478bd9Sstevel@tonic-gate 	char *str = buffer;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	switch (flags & O_ACCMODE) {
4237c478bd9Sstevel@tonic-gate 	case O_RDONLY:
4247c478bd9Sstevel@tonic-gate 		(void) strcpy(str, "O_RDONLY");
4257c478bd9Sstevel@tonic-gate 		break;
4267c478bd9Sstevel@tonic-gate 	case O_WRONLY:
4277c478bd9Sstevel@tonic-gate 		(void) strcpy(str, "O_WRONLY");
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 	case O_RDWR:
4307c478bd9Sstevel@tonic-gate 		(void) strcpy(str, "O_RDWR");
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	default:
4337c478bd9Sstevel@tonic-gate 		(void) sprintf(str, "0x%x", flags & O_ACCMODE);
4347c478bd9Sstevel@tonic-gate 		break;
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (flags & O_NDELAY)
4387c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_NDELAY");
4397c478bd9Sstevel@tonic-gate 	if (flags & O_NONBLOCK)
4407c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_NONBLOCK");
4417c478bd9Sstevel@tonic-gate 	if (flags & O_APPEND)
4427c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_APPEND");
4437c478bd9Sstevel@tonic-gate #ifdef O_PRIV
4447c478bd9Sstevel@tonic-gate 	if (flags & O_PRIV)
4457c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_PRIV");
4467c478bd9Sstevel@tonic-gate #endif
4477c478bd9Sstevel@tonic-gate 	if (flags & O_SYNC)
4487c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_SYNC");
4497c478bd9Sstevel@tonic-gate 	if (flags & O_DSYNC)
4507c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_DSYNC");
4517c478bd9Sstevel@tonic-gate 	if (flags & O_RSYNC)
4527c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_RSYNC");
4537c478bd9Sstevel@tonic-gate 	if (flags & O_CREAT)
4547c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_CREAT");
4557c478bd9Sstevel@tonic-gate 	if (flags & O_TRUNC)
4567c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_TRUNC");
4577c478bd9Sstevel@tonic-gate 	if (flags & O_EXCL)
4587c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_EXCL");
4597c478bd9Sstevel@tonic-gate 	if (flags & O_NOCTTY)
4607c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_NOCTTY");
4617c478bd9Sstevel@tonic-gate 	if (flags & O_LARGEFILE)
4627c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_LARGEFILE");
4637c478bd9Sstevel@tonic-gate 	if (flags & O_XATTR)
4647c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_XATTR");
4657c478bd9Sstevel@tonic-gate 	if (flags & ~(ALL_O_FLAGS))
4667c478bd9Sstevel@tonic-gate 		(void) sprintf(str + strlen(str), "|0x%x",
4677c478bd9Sstevel@tonic-gate 		    flags & ~(ALL_O_FLAGS));
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	(void) printf("%s", str);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /* show door info */
4737c478bd9Sstevel@tonic-gate static void
4747c478bd9Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	door_info_t door_info;
4777c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (pr_door_info(Pr, fd, &door_info) != 0)
4807c478bd9Sstevel@tonic-gate 		return;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (proc_get_psinfo(door_info.di_target, &psinfo) != 0)
4837c478bd9Sstevel@tonic-gate 		psinfo.pr_fname[0] = '\0';
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	(void) printf("  door to ");
4867c478bd9Sstevel@tonic-gate 	if (psinfo.pr_fname[0] != '\0')
4877c478bd9Sstevel@tonic-gate 		(void) printf("%s[%d]", psinfo.pr_fname,
4887c478bd9Sstevel@tonic-gate 		    (int)door_info.di_target);
4897c478bd9Sstevel@tonic-gate 	else
4907c478bd9Sstevel@tonic-gate 		(void) printf("pid %d", (int)door_info.di_target);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
493*ca9327a6Smeem /*
494*ca9327a6Smeem  * Print out the socket address pointed to by `sa'.  `len' is only
495*ca9327a6Smeem  * needed for AF_UNIX sockets.
496*ca9327a6Smeem  */
4977c478bd9Sstevel@tonic-gate static void
4987c478bd9Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
4997c478bd9Sstevel@tonic-gate {
500*ca9327a6Smeem 	struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa;
501*ca9327a6Smeem 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa;
5027c478bd9Sstevel@tonic-gate 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
5037c478bd9Sstevel@tonic-gate 	char  abuf[INET6_ADDRSTRLEN];
5047c478bd9Sstevel@tonic-gate 	const char *p;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
5077c478bd9Sstevel@tonic-gate 	default:
5087c478bd9Sstevel@tonic-gate 		return;
5097c478bd9Sstevel@tonic-gate 	case AF_INET:
510*ca9327a6Smeem 		(void) printf("\t%s: AF_INET %s  port: %u\n", str,
5117c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)),
5127c478bd9Sstevel@tonic-gate 		    ntohs(so_in->sin_port));
5137c478bd9Sstevel@tonic-gate 		return;
5147c478bd9Sstevel@tonic-gate 	case AF_INET6:
515*ca9327a6Smeem 		(void) printf("\t%s: AF_INET6 %s  port: %u\n", str,
5167c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &so_in6->sin6_addr,
5177c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
5187c478bd9Sstevel@tonic-gate 		    ntohs(so_in->sin_port));
5197c478bd9Sstevel@tonic-gate 		return;
5207c478bd9Sstevel@tonic-gate 	case AF_UNIX:
5217c478bd9Sstevel@tonic-gate 		if (len >= sizeof (so_un->sun_family)) {
5227c478bd9Sstevel@tonic-gate 			/* Null terminate */
5237c478bd9Sstevel@tonic-gate 			len -= sizeof (so_un->sun_family);
524*ca9327a6Smeem 			so_un->sun_path[len] = '\0';
5257c478bd9Sstevel@tonic-gate 			(void) printf("\t%s: AF_UNIX %s\n",
5267c478bd9Sstevel@tonic-gate 			    str, so_un->sun_path);
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		return;
5297c478bd9Sstevel@tonic-gate 	case AF_IMPLINK:	p = "AF_IMPLINK";	break;
5307c478bd9Sstevel@tonic-gate 	case AF_PUP:		p = "AF_PUP";		break;
5317c478bd9Sstevel@tonic-gate 	case AF_CHAOS:		p = "AF_CHAOS";		break;
5327c478bd9Sstevel@tonic-gate 	case AF_NS:		p = "AF_NS";		break;
5337c478bd9Sstevel@tonic-gate 	case AF_NBS:		p = "AF_NBS";		break;
5347c478bd9Sstevel@tonic-gate 	case AF_ECMA:		p = "AF_ECMA";		break;
5357c478bd9Sstevel@tonic-gate 	case AF_DATAKIT:	p = "AF_DATAKIT";	break;
5367c478bd9Sstevel@tonic-gate 	case AF_CCITT:		p = "AF_CCITT";		break;
5377c478bd9Sstevel@tonic-gate 	case AF_SNA:		p = "AF_SNA";		break;
5387c478bd9Sstevel@tonic-gate 	case AF_DECnet:		p = "AF_DECnet";	break;
5397c478bd9Sstevel@tonic-gate 	case AF_DLI:		p = "AF_DLI";		break;
5407c478bd9Sstevel@tonic-gate 	case AF_LAT:		p = "AF_LAT";		break;
5417c478bd9Sstevel@tonic-gate 	case AF_HYLINK:		p = "AF_HYLINK";	break;
5427c478bd9Sstevel@tonic-gate 	case AF_APPLETALK:	p = "AF_APPLETALK";	break;
5437c478bd9Sstevel@tonic-gate 	case AF_NIT:		p = "AF_NIT";		break;
5447c478bd9Sstevel@tonic-gate 	case AF_802:		p = "AF_802";		break;
5457c478bd9Sstevel@tonic-gate 	case AF_OSI:		p = "AF_OSI";		break;
5467c478bd9Sstevel@tonic-gate 	case AF_X25:		p = "AF_X25";		break;
5477c478bd9Sstevel@tonic-gate 	case AF_OSINET:		p = "AF_OSINET";	break;
5487c478bd9Sstevel@tonic-gate 	case AF_GOSIP:		p = "AF_GOSIP";		break;
5497c478bd9Sstevel@tonic-gate 	case AF_IPX:		p = "AF_IPX";		break;
5507c478bd9Sstevel@tonic-gate 	case AF_ROUTE:		p = "AF_ROUTE";		break;
5517c478bd9Sstevel@tonic-gate 	case AF_LINK:		p = "AF_LINK";		break;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	(void) printf("\t%s: %s\n", str, p);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate static void
5587c478bd9Sstevel@tonic-gate show_socktype(uint_t type)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	static const char *types[] = {
5617c478bd9Sstevel@tonic-gate 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
5627c478bd9Sstevel@tonic-gate 	};
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
5657c478bd9Sstevel@tonic-gate 		(void) printf("\tSOCK_%s\n", types[type]);
5667c478bd9Sstevel@tonic-gate 	else
5677c478bd9Sstevel@tonic-gate 		(void) printf("\tunknown socket type %u\n", type);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
57043d18f1cSpriyanka #define	BUFSIZE	200
5717c478bd9Sstevel@tonic-gate static void
5727c478bd9Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	int val, vlen;
57543d18f1cSpriyanka 	char buf[BUFSIZE];
5767c478bd9Sstevel@tonic-gate 	char buf1[32];
57743d18f1cSpriyanka 	char ipaddr[INET_ADDRSTRLEN];
5787c478bd9Sstevel@tonic-gate 	int i;
57943d18f1cSpriyanka 	in_addr_t nexthop_val;
5807c478bd9Sstevel@tonic-gate 	struct boolopt {
581437220cdSdanmcd 		int		level;
5827c478bd9Sstevel@tonic-gate 		int		opt;
5837c478bd9Sstevel@tonic-gate 		const char	*name;
5847c478bd9Sstevel@tonic-gate 	};
5857c478bd9Sstevel@tonic-gate 	static struct boolopt boolopts[] = {
586437220cdSdanmcd 	    { SOL_SOCKET, SO_DEBUG,		"SO_DEBUG,"	},
587437220cdSdanmcd 	    { SOL_SOCKET, SO_REUSEADDR,		"SO_REUSEADDR,"	},
588437220cdSdanmcd 	    { SOL_SOCKET, SO_KEEPALIVE,		"SO_KEEPALIVE,"	},
589437220cdSdanmcd 	    { SOL_SOCKET, SO_DONTROUTE,		"SO_DONTROUTE,"	},
590437220cdSdanmcd 	    { SOL_SOCKET, SO_BROADCAST,		"SO_BROADCAST,"	},
591437220cdSdanmcd 	    { SOL_SOCKET, SO_OOBINLINE,		"SO_OOBINLINE,"	},
592437220cdSdanmcd 	    { SOL_SOCKET, SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"},
593437220cdSdanmcd 	    { SOL_SOCKET, SO_ALLZONES,		"SO_ALLZONES,"	},
594437220cdSdanmcd 	    { SOL_SOCKET, SO_EXCLBIND,		"SO_EXCLBIND," },
595437220cdSdanmcd 	    { IPPROTO_UDP, UDP_NAT_T_ENDPOINT,	"UDP_NAT_T_ENDPOINT," },
5967c478bd9Sstevel@tonic-gate 	};
5977c478bd9Sstevel@tonic-gate 	struct linger l;
5987c478bd9Sstevel@tonic-gate 
5995d0bc3edSsommerfe 	buf[0] = '!';		/* sentinel value, never printed */
6007c478bd9Sstevel@tonic-gate 	buf[1] = '\0';
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) {
6037c478bd9Sstevel@tonic-gate 		vlen = sizeof (val);
604437220cdSdanmcd 		if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt,
605437220cdSdanmcd 		    &val, &vlen) == 0 && val != 0)
6067c478bd9Sstevel@tonic-gate 			(void) strlcat(buf, boolopts[i].name, sizeof (buf));
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	vlen = sizeof (l);
6107c478bd9Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 &&
6117c478bd9Sstevel@tonic-gate 	    l.l_onoff != 0) {
6127c478bd9Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),",
6137c478bd9Sstevel@tonic-gate 		    l.l_linger);
6147c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	vlen = sizeof (val);
6187c478bd9Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) {
6197c478bd9Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val);
6207c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	vlen = sizeof (val);
6237c478bd9Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) {
6247c478bd9Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val);
6257c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
6267c478bd9Sstevel@tonic-gate 	}
62743d18f1cSpriyanka 	vlen = sizeof (nexthop_val);
62843d18f1cSpriyanka 	if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val,
62943d18f1cSpriyanka 	    &vlen) == 0) {
63029e362daSpriyanka 		if (vlen > 0) {
63129e362daSpriyanka 			(void) inet_ntop(AF_INET, (void *) &nexthop_val,
63229e362daSpriyanka 			    ipaddr, sizeof (ipaddr));
63343d18f1cSpriyanka 			(void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),",
63443d18f1cSpriyanka 			    ipaddr);
63543d18f1cSpriyanka 			(void) strlcat(buf, buf1, sizeof (buf));
63643d18f1cSpriyanka 		}
63729e362daSpriyanka 	}
6387c478bd9Sstevel@tonic-gate 
6395d0bc3edSsommerfe 	buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */
6407c478bd9Sstevel@tonic-gate 	if (buf[1] != '\0')
6417c478bd9Sstevel@tonic-gate 		(void) printf("\t%s\n", buf+1);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /* the file is a socket */
6457c478bd9Sstevel@tonic-gate static void
6467c478bd9Sstevel@tonic-gate dosocket(struct ps_prochandle *Pr, int fd)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	/* A buffer large enough for PATH_MAX size AF_UNIX address */
6497c478bd9Sstevel@tonic-gate 	long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1)
6507c478bd9Sstevel@tonic-gate 	    / sizeof (long)];
6517c478bd9Sstevel@tonic-gate 	struct sockaddr *sa = (struct sockaddr *)buf;
6527c478bd9Sstevel@tonic-gate 	socklen_t len;
6537c478bd9Sstevel@tonic-gate 	int type, tlen;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	tlen = sizeof (type);
656*ca9327a6Smeem 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0)
6577c478bd9Sstevel@tonic-gate 		show_socktype((uint_t)type);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	show_sockopts(Pr, fd);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
6627c478bd9Sstevel@tonic-gate 	if (pr_getsockname(Pr, fd, sa, &len) == 0)
6637c478bd9Sstevel@tonic-gate 		show_sockaddr("sockname", sa, len);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
6667c478bd9Sstevel@tonic-gate 	if (pr_getpeername(Pr, fd, sa, &len) == 0)
6677c478bd9Sstevel@tonic-gate 		show_sockaddr("peername", sa, len);
6687c478bd9Sstevel@tonic-gate }
669*ca9327a6Smeem 
670*ca9327a6Smeem /* the file is a TLI endpoint */
671*ca9327a6Smeem static void
672*ca9327a6Smeem dotli(struct ps_prochandle *Pr, int fd)
673*ca9327a6Smeem {
674*ca9327a6Smeem 	struct strcmd strcmd;
675*ca9327a6Smeem 
676*ca9327a6Smeem 	strcmd.sc_len = STRCMDBUFSIZE;
677*ca9327a6Smeem 	strcmd.sc_timeout = 5;
678*ca9327a6Smeem 
679*ca9327a6Smeem 	strcmd.sc_cmd = TI_GETMYNAME;
680*ca9327a6Smeem 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
681*ca9327a6Smeem 		show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0);
682*ca9327a6Smeem 
683*ca9327a6Smeem 	strcmd.sc_cmd = TI_GETPEERNAME;
684*ca9327a6Smeem 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
685*ca9327a6Smeem 		show_sockaddr("peername", (void *)&strcmd.sc_buf, 0);
686*ca9327a6Smeem }
687