xref: /titanic_41/usr/src/cmd/ptools/pfiles/pfiles.c (revision dfc0fed81813380fb526f1a003d3be17ab26ce3b)
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 /*
233e95bd4aSAnders Persson  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
2434bdffbfSGarrett D'Amore  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
26*dfc0fed8SRobert Mustacchi /*
27*dfc0fed8SRobert Mustacchi  * Copyright (c) 2013 Joyent, Inc.  All Rights reserved.
28*dfc0fed8SRobert Mustacchi  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <dirent.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
397c478bd9Sstevel@tonic-gate #include <door.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
4143d18f1cSpriyanka #include <sys/socket.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
44ca9327a6Smeem #include <sys/stropts.h>
45ca9327a6Smeem #include <sys/timod.h>
467c478bd9Sstevel@tonic-gate #include <sys/un.h>
477c478bd9Sstevel@tonic-gate #include <libproc.h>
4843d18f1cSpriyanka #include <netinet/in.h>
49437220cdSdanmcd #include <netinet/udp.h>
507c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
51a154f012SAlexander Stetsenko #include <ucred.h>
52a154f012SAlexander Stetsenko #include <zone.h>
537c478bd9Sstevel@tonic-gate 
54150d2c52Svb160487 #define	copyflock(dst, src) \
55150d2c52Svb160487 	(dst).l_type = (src).l_type;		\
56150d2c52Svb160487 	(dst).l_whence = (src).l_whence;	\
57150d2c52Svb160487 	(dst).l_start = (src).l_start;		\
58150d2c52Svb160487 	(dst).l_len = (src).l_len;		\
59150d2c52Svb160487 	(dst).l_sysid = (src).l_sysid;		\
60150d2c52Svb160487 	(dst).l_pid = (src).l_pid;
61150d2c52Svb160487 
627c478bd9Sstevel@tonic-gate static char *command;
637c478bd9Sstevel@tonic-gate static volatile int interrupt;
647c478bd9Sstevel@tonic-gate static int Fflag;
657c478bd9Sstevel@tonic-gate static boolean_t nflag = B_FALSE;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static	void	intr(int);
6834bdffbfSGarrett D'Amore static	void	dofcntl(struct ps_prochandle *, prfdinfo_t *, int, int);
697c478bd9Sstevel@tonic-gate static	void	dosocket(struct ps_prochandle *, int);
70a154f012SAlexander Stetsenko static	void	dofifo(struct ps_prochandle *, int);
71ca9327a6Smeem static	void	dotli(struct ps_prochandle *, int);
727c478bd9Sstevel@tonic-gate static	void	show_files(struct ps_prochandle *);
737c478bd9Sstevel@tonic-gate static	void	show_fileflags(int);
747c478bd9Sstevel@tonic-gate static	void	show_door(struct ps_prochandle *, int);
75150d2c52Svb160487 static	int	getflock(struct ps_prochandle *, int, struct flock *);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)787c478bd9Sstevel@tonic-gate main(int argc, char **argv)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	int retc = 0;
817c478bd9Sstevel@tonic-gate 	int opt;
827c478bd9Sstevel@tonic-gate 	int errflg = 0;
837c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
867c478bd9Sstevel@tonic-gate 		command++;
877c478bd9Sstevel@tonic-gate 	else
887c478bd9Sstevel@tonic-gate 		command = argv[0];
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/* options */
917c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "Fn")) != EOF) {
927c478bd9Sstevel@tonic-gate 		switch (opt) {
937c478bd9Sstevel@tonic-gate 		case 'F':		/* force grabbing (no O_EXCL) */
947c478bd9Sstevel@tonic-gate 			Fflag = PGRAB_FORCE;
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case 'n':
977c478bd9Sstevel@tonic-gate 			nflag = B_TRUE;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		default:
1007c478bd9Sstevel@tonic-gate 			errflg = 1;
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	argc -= optind;
1067c478bd9Sstevel@tonic-gate 	argv += optind;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (errflg || argc <= 0) {
10934bdffbfSGarrett D'Amore 		(void) fprintf(stderr, "usage:\t%s [-F] { pid | core } ...\n",
1107c478bd9Sstevel@tonic-gate 		    command);
1117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1127c478bd9Sstevel@tonic-gate 		    "  (report open files of each process)\n");
1137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1147c478bd9Sstevel@tonic-gate 		    "  -F: force grabbing of the target process\n");
1157c478bd9Sstevel@tonic-gate 		exit(2);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* catch signals from terminal */
1197c478bd9Sstevel@tonic-gate 	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
1207c478bd9Sstevel@tonic-gate 		(void) sigset(SIGHUP, intr);
1217c478bd9Sstevel@tonic-gate 	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
1227c478bd9Sstevel@tonic-gate 		(void) sigset(SIGINT, intr);
1237c478bd9Sstevel@tonic-gate 	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
1247c478bd9Sstevel@tonic-gate 		(void) sigset(SIGQUIT, intr);
1257c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, intr);
1267c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, intr);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) proc_initstdio();
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	while (--argc >= 0 && !interrupt) {
1327c478bd9Sstevel@tonic-gate 		char *arg;
1337c478bd9Sstevel@tonic-gate 		psinfo_t psinfo;
1347c478bd9Sstevel@tonic-gate 		pid_t pid;
1357c478bd9Sstevel@tonic-gate 		int gret;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		(void) proc_flushstdio();
1387c478bd9Sstevel@tonic-gate 
13934bdffbfSGarrett D'Amore 		arg = *argv++;
14034bdffbfSGarrett D'Amore 
1417c478bd9Sstevel@tonic-gate 		/* get the specified pid and the psinfo struct */
14234bdffbfSGarrett D'Amore 		if ((pid = proc_arg_psinfo(arg, PR_ARG_PIDS,
1437c478bd9Sstevel@tonic-gate 		    &psinfo, &gret)) == -1) {
14434bdffbfSGarrett D'Amore 
14534bdffbfSGarrett D'Amore 			if ((Pr = proc_arg_xgrab(arg, NULL, PR_ARG_CORES,
14634bdffbfSGarrett D'Amore 			    Fflag, &gret, NULL)) == NULL) {
14734bdffbfSGarrett D'Amore 				(void) fprintf(stderr,
14834bdffbfSGarrett D'Amore 				    "%s: cannot examine %s: %s\n",
1497c478bd9Sstevel@tonic-gate 				    command, arg, Pgrab_error(gret));
1507c478bd9Sstevel@tonic-gate 				retc++;
15134bdffbfSGarrett D'Amore 				continue;
15234bdffbfSGarrett D'Amore 			}
15334bdffbfSGarrett D'Amore 			if (proc_arg_psinfo(arg, PR_ARG_ANY, &psinfo,
15434bdffbfSGarrett D'Amore 			    &gret) < 0) {
15534bdffbfSGarrett D'Amore 				(void) fprintf(stderr,
15634bdffbfSGarrett D'Amore 				    "%s: cannot examine %s: %s\n",
15734bdffbfSGarrett D'Amore 				    command, arg, Pgrab_error(gret));
15834bdffbfSGarrett D'Amore 				retc++;
15934bdffbfSGarrett D'Amore 				Prelease(Pr, 0);
16034bdffbfSGarrett D'Amore 				continue;
16134bdffbfSGarrett D'Amore 			}
16234bdffbfSGarrett D'Amore 			(void) printf("core '%s' of %d:\t%.70s\n",
16334bdffbfSGarrett D'Amore 			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
16434bdffbfSGarrett D'Amore 
16534bdffbfSGarrett D'Amore 			show_files(Pr);
16634bdffbfSGarrett D'Amore 			Prelease(Pr, 0);
16734bdffbfSGarrett D'Amore 
1687c478bd9Sstevel@tonic-gate 		} else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) {
1697c478bd9Sstevel@tonic-gate 			if (Pcreate_agent(Pr) == 0) {
1707c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
1717c478bd9Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n",
1727c478bd9Sstevel@tonic-gate 				    (int)pid, psinfo.pr_psargs);
1737c478bd9Sstevel@tonic-gate 				show_files(Pr);
1747c478bd9Sstevel@tonic-gate 				Pdestroy_agent(Pr);
1757c478bd9Sstevel@tonic-gate 			} else {
1767c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1777c478bd9Sstevel@tonic-gate 				    "%s: cannot control process %d\n",
1787c478bd9Sstevel@tonic-gate 				    command, (int)pid);
1797c478bd9Sstevel@tonic-gate 				retc++;
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 			Prelease(Pr, 0);
1827c478bd9Sstevel@tonic-gate 			Pr = NULL;
1837c478bd9Sstevel@tonic-gate 		} else {
1847c478bd9Sstevel@tonic-gate 			switch (gret) {
1857c478bd9Sstevel@tonic-gate 			case G_SYS:
1867c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
1877c478bd9Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n", (int)pid,
1887c478bd9Sstevel@tonic-gate 				    psinfo.pr_psargs);
1897c478bd9Sstevel@tonic-gate 				(void) printf("  [system process]\n");
1907c478bd9Sstevel@tonic-gate 				break;
1917c478bd9Sstevel@tonic-gate 			default:
1927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: %s: %d\n",
1937c478bd9Sstevel@tonic-gate 				    command, Pgrab_error(gret), (int)pid);
1947c478bd9Sstevel@tonic-gate 				retc++;
1957c478bd9Sstevel@tonic-gate 				break;
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	(void) proc_finistdio();
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (interrupt && retc == 0)
2037c478bd9Sstevel@tonic-gate 		retc++;
2047c478bd9Sstevel@tonic-gate 	return (retc);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /* ARGSUSED */
2087c478bd9Sstevel@tonic-gate static void
intr(int sig)2097c478bd9Sstevel@tonic-gate intr(int sig)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	interrupt = 1;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /* ------ begin specific code ------ */
2157c478bd9Sstevel@tonic-gate 
21634bdffbfSGarrett D'Amore static int
show_file(void * data,prfdinfo_t * info)21734bdffbfSGarrett D'Amore show_file(void *data, prfdinfo_t *info)
21834bdffbfSGarrett D'Amore {
21934bdffbfSGarrett D'Amore 	struct ps_prochandle *Pr = data;
22034bdffbfSGarrett D'Amore 	char unknown[12];
22134bdffbfSGarrett D'Amore 	char *s;
22234bdffbfSGarrett D'Amore 	mode_t mode;
22334bdffbfSGarrett D'Amore 
22434bdffbfSGarrett D'Amore 	if (interrupt)
22534bdffbfSGarrett D'Amore 		return (1);
22634bdffbfSGarrett D'Amore 
22734bdffbfSGarrett D'Amore 	mode = info->pr_mode;
22834bdffbfSGarrett D'Amore 
22934bdffbfSGarrett D'Amore 	switch (mode & S_IFMT) {
23034bdffbfSGarrett D'Amore 	case S_IFCHR: s = "S_IFCHR"; break;
23134bdffbfSGarrett D'Amore 	case S_IFBLK: s = "S_IFBLK"; break;
23234bdffbfSGarrett D'Amore 	case S_IFIFO: s = "S_IFIFO"; break;
23334bdffbfSGarrett D'Amore 	case S_IFDIR: s = "S_IFDIR"; break;
23434bdffbfSGarrett D'Amore 	case S_IFREG: s = "S_IFREG"; break;
23534bdffbfSGarrett D'Amore 	case S_IFLNK: s = "S_IFLNK"; break;
23634bdffbfSGarrett D'Amore 	case S_IFSOCK: s = "S_IFSOCK"; break;
23734bdffbfSGarrett D'Amore 	case S_IFDOOR: s = "S_IFDOOR"; break;
23834bdffbfSGarrett D'Amore 	case S_IFPORT: s = "S_IFPORT"; break;
23934bdffbfSGarrett D'Amore 	default:
24034bdffbfSGarrett D'Amore 		s = unknown;
24134bdffbfSGarrett D'Amore 		(void) sprintf(s, "0x%.4x ", (int)mode & S_IFMT);
24234bdffbfSGarrett D'Amore 		break;
24334bdffbfSGarrett D'Amore 	}
24434bdffbfSGarrett D'Amore 
24534bdffbfSGarrett D'Amore 	(void) printf("%4d: %s mode:0%.3o", info->pr_fd, s,
24634bdffbfSGarrett D'Amore 	    (int)mode & ~S_IFMT);
24734bdffbfSGarrett D'Amore 
24834bdffbfSGarrett D'Amore 	(void) printf(" dev:%u,%u",
24934bdffbfSGarrett D'Amore 	    (unsigned)info->pr_major, (unsigned)info->pr_minor);
25034bdffbfSGarrett D'Amore 
25134bdffbfSGarrett D'Amore 	if ((mode & S_IFMT) == S_IFPORT) {
25234bdffbfSGarrett D'Amore 		(void) printf(" uid:%d gid:%d",
25334bdffbfSGarrett D'Amore 		    (int)info->pr_uid, (int)info->pr_gid);
25434bdffbfSGarrett D'Amore 		(void) printf(" size:%lld\n", (longlong_t)info->pr_size);
25534bdffbfSGarrett D'Amore 		return (0);
25634bdffbfSGarrett D'Amore 	}
25734bdffbfSGarrett D'Amore 
25834bdffbfSGarrett D'Amore 	(void) printf(" ino:%llu uid:%d gid:%d",
25934bdffbfSGarrett D'Amore 	    (u_longlong_t)info->pr_ino, (int)info->pr_uid, (int)info->pr_gid);
26034bdffbfSGarrett D'Amore 
26134bdffbfSGarrett D'Amore 	if ((info->pr_rmajor == (major_t)NODEV) &&
26234bdffbfSGarrett D'Amore 	    (info->pr_rminor == (minor_t)NODEV))
26334bdffbfSGarrett D'Amore 		(void) printf(" size:%lld\n", (longlong_t)info->pr_size);
26434bdffbfSGarrett D'Amore 	else
26534bdffbfSGarrett D'Amore 		(void) printf(" rdev:%u,%u\n",
26634bdffbfSGarrett D'Amore 		    (unsigned)info->pr_rmajor, (unsigned)info->pr_rminor);
26734bdffbfSGarrett D'Amore 
26834bdffbfSGarrett D'Amore 	if (!nflag) {
26934bdffbfSGarrett D'Amore 		dofcntl(Pr, info,
27034bdffbfSGarrett D'Amore 		    (mode & (S_IFMT|S_ENFMT|S_IXGRP)) == (S_IFREG|S_ENFMT),
27134bdffbfSGarrett D'Amore 		    (mode & S_IFMT) == S_IFDOOR);
27234bdffbfSGarrett D'Amore 
27334bdffbfSGarrett D'Amore 		if (Pstate(Pr) != PS_DEAD) {
27434bdffbfSGarrett D'Amore 			char *dev;
27534bdffbfSGarrett D'Amore 
27634bdffbfSGarrett D'Amore 			if ((mode & S_IFMT) == S_IFSOCK)
27734bdffbfSGarrett D'Amore 				dosocket(Pr, info->pr_fd);
27834bdffbfSGarrett D'Amore 			else if ((mode & S_IFMT) == S_IFIFO)
27934bdffbfSGarrett D'Amore 				dofifo(Pr, info->pr_fd);
28034bdffbfSGarrett D'Amore 
28134bdffbfSGarrett D'Amore 			if ((mode & S_IFMT) == S_IFCHR &&
28234bdffbfSGarrett D'Amore 			    (dev = strrchr(info->pr_path, ':')) != NULL) {
28334bdffbfSGarrett D'Amore 				/*
28434bdffbfSGarrett D'Amore 				 * There's no elegant way to determine
28534bdffbfSGarrett D'Amore 				 * if a character device supports TLI,
28634bdffbfSGarrett D'Amore 				 * so we lame out and just check a
28734bdffbfSGarrett D'Amore 				 * hardcoded list of known TLI devices.
28834bdffbfSGarrett D'Amore 				 */
28934bdffbfSGarrett D'Amore 				int i;
29034bdffbfSGarrett D'Amore 				const char *tlidevs[] = {
29134bdffbfSGarrett D'Amore 				    "tcp", "tcp6", "udp", "udp6", NULL
29234bdffbfSGarrett D'Amore 				};
29334bdffbfSGarrett D'Amore 
29434bdffbfSGarrett D'Amore 				dev++; /* skip past the `:' */
29534bdffbfSGarrett D'Amore 				for (i = 0; tlidevs[i] != NULL; i++) {
29634bdffbfSGarrett D'Amore 					if (strcmp(dev, tlidevs[i]) == 0) {
29734bdffbfSGarrett D'Amore 						dotli(Pr, info->pr_fd);
29834bdffbfSGarrett D'Amore 						break;
29934bdffbfSGarrett D'Amore 					}
30034bdffbfSGarrett D'Amore 				}
30134bdffbfSGarrett D'Amore 			}
30234bdffbfSGarrett D'Amore 		}
30334bdffbfSGarrett D'Amore 
30434bdffbfSGarrett D'Amore 		if (info->pr_path[0] != '\0')
30534bdffbfSGarrett D'Amore 			(void) printf("      %s\n", info->pr_path);
30634bdffbfSGarrett D'Amore 
30734bdffbfSGarrett D'Amore 		if (info->pr_offset != -1) {
30834bdffbfSGarrett D'Amore 			(void) printf("      offset:%lld\n",
30934bdffbfSGarrett D'Amore 			    (long long)info->pr_offset);
31034bdffbfSGarrett D'Amore 		}
31134bdffbfSGarrett D'Amore 	}
31234bdffbfSGarrett D'Amore 	return (0);
31334bdffbfSGarrett D'Amore }
31434bdffbfSGarrett D'Amore 
3157c478bd9Sstevel@tonic-gate static void
show_files(struct ps_prochandle * Pr)3167c478bd9Sstevel@tonic-gate show_files(struct ps_prochandle *Pr)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) {
3217c478bd9Sstevel@tonic-gate 		ulong_t nfd = rlim.rlim_cur;
3227c478bd9Sstevel@tonic-gate 		if (nfd == RLIM_INFINITY)
3237c478bd9Sstevel@tonic-gate 			(void) printf(
3247c478bd9Sstevel@tonic-gate 			    "  Current rlimit: unlimited file descriptors\n");
3257c478bd9Sstevel@tonic-gate 		else
3267c478bd9Sstevel@tonic-gate 			(void) printf(
3277c478bd9Sstevel@tonic-gate 			    "  Current rlimit: %lu file descriptors\n", nfd);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
33034bdffbfSGarrett D'Amore 	(void) Pfdinfo_iter(Pr, show_file, Pr);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
333150d2c52Svb160487 
334150d2c52Svb160487 static int
getflock(struct ps_prochandle * Pr,int fd,struct flock * flock_native)335150d2c52Svb160487 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native)
336150d2c52Svb160487 {
337150d2c52Svb160487 	int ret;
338150d2c52Svb160487 #ifdef _LP64
339150d2c52Svb160487 	struct flock64_32 flock_target;
340150d2c52Svb160487 
341*dfc0fed8SRobert Mustacchi 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) {
342150d2c52Svb160487 		copyflock(flock_target, *flock_native);
343150d2c52Svb160487 		ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target);
344150d2c52Svb160487 		copyflock(*flock_native, flock_target);
345150d2c52Svb160487 		return (ret);
346150d2c52Svb160487 	}
347150d2c52Svb160487 #endif /* _LP64 */
348150d2c52Svb160487 	ret = pr_fcntl(Pr, fd, F_GETLK, flock_native);
349150d2c52Svb160487 	return (ret);
350150d2c52Svb160487 }
351150d2c52Svb160487 
3527c478bd9Sstevel@tonic-gate /* examine open file with fcntl() */
3537c478bd9Sstevel@tonic-gate static void
dofcntl(struct ps_prochandle * Pr,prfdinfo_t * info,int mandatory,int isdoor)35434bdffbfSGarrett D'Amore dofcntl(struct ps_prochandle *Pr, prfdinfo_t *info, int mandatory, int isdoor)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	struct flock flock;
3577c478bd9Sstevel@tonic-gate 	int fileflags;
3587c478bd9Sstevel@tonic-gate 	int fdflags;
35934bdffbfSGarrett D'Amore 	int fd;
3607c478bd9Sstevel@tonic-gate 
36134bdffbfSGarrett D'Amore 	fd = info->pr_fd;
36234bdffbfSGarrett D'Amore 
36334bdffbfSGarrett D'Amore 	fileflags = info->pr_fileflags;
36434bdffbfSGarrett D'Amore 	fdflags = info->pr_fdflags;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (fileflags != -1 || fdflags != -1) {
3677c478bd9Sstevel@tonic-gate 		(void) printf("      ");
3687c478bd9Sstevel@tonic-gate 		if (fileflags != -1)
3697c478bd9Sstevel@tonic-gate 			show_fileflags(fileflags);
3707c478bd9Sstevel@tonic-gate 		if (fdflags != -1 && (fdflags & FD_CLOEXEC))
3717c478bd9Sstevel@tonic-gate 			(void) printf(" FD_CLOEXEC");
37234bdffbfSGarrett D'Amore 		if (isdoor && (Pstate(Pr) != PS_DEAD))
3737c478bd9Sstevel@tonic-gate 			show_door(Pr, fd);
3747c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
37534bdffbfSGarrett D'Amore 	} else if (isdoor && (Pstate(Pr) != PS_DEAD)) {
3767c478bd9Sstevel@tonic-gate 		(void) printf("    ");
3777c478bd9Sstevel@tonic-gate 		show_door(Pr, fd);
3787c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
3827c478bd9Sstevel@tonic-gate 	flock.l_whence = 0;
3837c478bd9Sstevel@tonic-gate 	flock.l_start = 0;
3847c478bd9Sstevel@tonic-gate 	flock.l_len = 0;
3857c478bd9Sstevel@tonic-gate 	flock.l_sysid = 0;
3867c478bd9Sstevel@tonic-gate 	flock.l_pid = 0;
38734bdffbfSGarrett D'Amore 	if ((Pstate(Pr) != PS_DEAD) && (getflock(Pr, fd, &flock) != -1)) {
3887c478bd9Sstevel@tonic-gate 		if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) {
3897c478bd9Sstevel@tonic-gate 			unsigned long sysid = flock.l_sysid;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 			(void) printf("      %s %s lock set by",
392437220cdSdanmcd 			    mandatory ? "mandatory" : "advisory",
3937c478bd9Sstevel@tonic-gate 			    flock.l_type == F_RDLCK? "read" : "write");
3947c478bd9Sstevel@tonic-gate 			if (sysid)
3957c478bd9Sstevel@tonic-gate 				(void) printf(" system 0x%lX", sysid);
3967c478bd9Sstevel@tonic-gate 			if (flock.l_pid)
3977c478bd9Sstevel@tonic-gate 				(void) printf(" process %d", (int)flock.l_pid);
3987c478bd9Sstevel@tonic-gate 			(void) fputc('\n', stdout);
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
4047c478bd9Sstevel@tonic-gate 			O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
4057c478bd9Sstevel@tonic-gate 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static void
show_fileflags(int flags)4087c478bd9Sstevel@tonic-gate show_fileflags(int flags)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	char buffer[136];
4117c478bd9Sstevel@tonic-gate 	char *str = buffer;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	switch (flags & O_ACCMODE) {
4147c478bd9Sstevel@tonic-gate 	case O_RDONLY:
4157c478bd9Sstevel@tonic-gate 		(void) strcpy(str, "O_RDONLY");
4167c478bd9Sstevel@tonic-gate 		break;
4177c478bd9Sstevel@tonic-gate 	case O_WRONLY:
4187c478bd9Sstevel@tonic-gate 		(void) strcpy(str, "O_WRONLY");
4197c478bd9Sstevel@tonic-gate 		break;
4207c478bd9Sstevel@tonic-gate 	case O_RDWR:
4217c478bd9Sstevel@tonic-gate 		(void) strcpy(str, "O_RDWR");
4227c478bd9Sstevel@tonic-gate 		break;
423794f0adbSRoger A. Faulkner 	case O_SEARCH:
424794f0adbSRoger A. Faulkner 		(void) strcpy(str, "O_SEARCH");
425794f0adbSRoger A. Faulkner 		break;
426794f0adbSRoger A. Faulkner 	case O_EXEC:
427794f0adbSRoger A. Faulkner 		(void) strcpy(str, "O_EXEC");
428794f0adbSRoger A. Faulkner 		break;
4297c478bd9Sstevel@tonic-gate 	default:
4307c478bd9Sstevel@tonic-gate 		(void) sprintf(str, "0x%x", flags & O_ACCMODE);
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (flags & O_NDELAY)
4357c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_NDELAY");
4367c478bd9Sstevel@tonic-gate 	if (flags & O_NONBLOCK)
4377c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_NONBLOCK");
4387c478bd9Sstevel@tonic-gate 	if (flags & O_APPEND)
4397c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_APPEND");
4407c478bd9Sstevel@tonic-gate 	if (flags & O_SYNC)
4417c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_SYNC");
4427c478bd9Sstevel@tonic-gate 	if (flags & O_DSYNC)
4437c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_DSYNC");
4447c478bd9Sstevel@tonic-gate 	if (flags & O_RSYNC)
4457c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_RSYNC");
4467c478bd9Sstevel@tonic-gate 	if (flags & O_CREAT)
4477c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_CREAT");
4487c478bd9Sstevel@tonic-gate 	if (flags & O_TRUNC)
4497c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_TRUNC");
4507c478bd9Sstevel@tonic-gate 	if (flags & O_EXCL)
4517c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_EXCL");
4527c478bd9Sstevel@tonic-gate 	if (flags & O_NOCTTY)
4537c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_NOCTTY");
4547c478bd9Sstevel@tonic-gate 	if (flags & O_LARGEFILE)
4557c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_LARGEFILE");
4567c478bd9Sstevel@tonic-gate 	if (flags & O_XATTR)
4577c478bd9Sstevel@tonic-gate 		(void) strcat(str, "|O_XATTR");
4587c478bd9Sstevel@tonic-gate 	if (flags & ~(ALL_O_FLAGS))
4597c478bd9Sstevel@tonic-gate 		(void) sprintf(str + strlen(str), "|0x%x",
4607c478bd9Sstevel@tonic-gate 		    flags & ~(ALL_O_FLAGS));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	(void) printf("%s", str);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
465a154f012SAlexander Stetsenko /* show process on the other end of a door, socket or fifo */
466a154f012SAlexander Stetsenko static void
show_peer_process(pid_t ppid)467a154f012SAlexander Stetsenko show_peer_process(pid_t ppid)
468a154f012SAlexander Stetsenko {
469a154f012SAlexander Stetsenko 	psinfo_t psinfo;
470a154f012SAlexander Stetsenko 
471a154f012SAlexander Stetsenko 	if (proc_get_psinfo(ppid, &psinfo) == 0)
472a154f012SAlexander Stetsenko 		(void) printf(" %s[%d]", psinfo.pr_fname, (int)ppid);
473a154f012SAlexander Stetsenko 	else
474a154f012SAlexander Stetsenko 		(void) printf(" pid %d", (int)ppid);
475a154f012SAlexander Stetsenko }
476a154f012SAlexander Stetsenko 
4777c478bd9Sstevel@tonic-gate /* show door info */
4787c478bd9Sstevel@tonic-gate static void
show_door(struct ps_prochandle * Pr,int fd)4797c478bd9Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	door_info_t door_info;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (pr_door_info(Pr, fd, &door_info) != 0)
4847c478bd9Sstevel@tonic-gate 		return;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	(void) printf("  door to");
487a154f012SAlexander Stetsenko 	show_peer_process(door_info.di_target);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
490ca9327a6Smeem /*
491ca9327a6Smeem  * Print out the socket address pointed to by `sa'.  `len' is only
492ca9327a6Smeem  * needed for AF_UNIX sockets.
493ca9327a6Smeem  */
4947c478bd9Sstevel@tonic-gate static void
show_sockaddr(const char * str,struct sockaddr * sa,socklen_t len)4957c478bd9Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
4967c478bd9Sstevel@tonic-gate {
497ca9327a6Smeem 	struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa;
498ca9327a6Smeem 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa;
4997c478bd9Sstevel@tonic-gate 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
5007c478bd9Sstevel@tonic-gate 	char  abuf[INET6_ADDRSTRLEN];
5017c478bd9Sstevel@tonic-gate 	const char *p;
5027c478bd9Sstevel@tonic-gate 
503*dfc0fed8SRobert Mustacchi 	if (len == 0)
504*dfc0fed8SRobert Mustacchi 		return;
505*dfc0fed8SRobert Mustacchi 
5067c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
5077c478bd9Sstevel@tonic-gate 	default:
5087c478bd9Sstevel@tonic-gate 		return;
5097c478bd9Sstevel@tonic-gate 	case AF_INET:
510ca9327a6Smeem 		(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:
515ca9327a6Smeem 		(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);
524ca9327a6Smeem 			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 
557a154f012SAlexander Stetsenko /*
558a154f012SAlexander Stetsenko  * Print out the process information for the other end of local sockets
559a154f012SAlexander Stetsenko  * and fifos
560a154f012SAlexander Stetsenko  */
561a154f012SAlexander Stetsenko static void
show_ucred(const char * str,ucred_t * cred)562a154f012SAlexander Stetsenko show_ucred(const char *str, ucred_t *cred)
563a154f012SAlexander Stetsenko {
564a154f012SAlexander Stetsenko 	pid_t upid = ucred_getpid(cred);
565a154f012SAlexander Stetsenko 	zoneid_t uzid = ucred_getzoneid(cred);
566a154f012SAlexander Stetsenko 	char zonename[ZONENAME_MAX];
567a154f012SAlexander Stetsenko 
568a154f012SAlexander Stetsenko 	if ((upid != -1) || (uzid != -1)) {
569a154f012SAlexander Stetsenko 		(void) printf("\t%s:", str);
570a154f012SAlexander Stetsenko 		if (upid != -1) {
571a154f012SAlexander Stetsenko 			show_peer_process(upid);
572a154f012SAlexander Stetsenko 		}
573a154f012SAlexander Stetsenko 		if (uzid != -1) {
574a154f012SAlexander Stetsenko 			if (getzonenamebyid(uzid, zonename, sizeof (zonename))
575a154f012SAlexander Stetsenko 			    != -1) {
576a154f012SAlexander Stetsenko 				(void) printf(" zone: %s[%d]", zonename,
577a154f012SAlexander Stetsenko 				    (int)uzid);
578a154f012SAlexander Stetsenko 			} else {
579a154f012SAlexander Stetsenko 				(void) printf(" zoneid: %d", (int)uzid);
580a154f012SAlexander Stetsenko 			}
581a154f012SAlexander Stetsenko 		}
582a154f012SAlexander Stetsenko 		(void) printf("\n");
583a154f012SAlexander Stetsenko 	}
584a154f012SAlexander Stetsenko }
585a154f012SAlexander Stetsenko 
5867c478bd9Sstevel@tonic-gate static void
show_socktype(uint_t type)5877c478bd9Sstevel@tonic-gate show_socktype(uint_t type)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	static const char *types[] = {
5907c478bd9Sstevel@tonic-gate 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
5917c478bd9Sstevel@tonic-gate 	};
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
5947c478bd9Sstevel@tonic-gate 		(void) printf("\tSOCK_%s\n", types[type]);
5957c478bd9Sstevel@tonic-gate 	else
5967c478bd9Sstevel@tonic-gate 		(void) printf("\tunknown socket type %u\n", type);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
59943d18f1cSpriyanka #define	BUFSIZE	200
6007c478bd9Sstevel@tonic-gate static void
show_sockopts(struct ps_prochandle * Pr,int fd)6017c478bd9Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	int val, vlen;
60443d18f1cSpriyanka 	char buf[BUFSIZE];
6057c478bd9Sstevel@tonic-gate 	char buf1[32];
60643d18f1cSpriyanka 	char ipaddr[INET_ADDRSTRLEN];
6077c478bd9Sstevel@tonic-gate 	int i;
60843d18f1cSpriyanka 	in_addr_t nexthop_val;
6097c478bd9Sstevel@tonic-gate 	struct boolopt {
610437220cdSdanmcd 		int		level;
6117c478bd9Sstevel@tonic-gate 		int		opt;
6127c478bd9Sstevel@tonic-gate 		const char	*name;
6137c478bd9Sstevel@tonic-gate 	};
6147c478bd9Sstevel@tonic-gate 	static struct boolopt boolopts[] = {
615437220cdSdanmcd 	    { SOL_SOCKET, SO_DEBUG,		"SO_DEBUG,"	},
616437220cdSdanmcd 	    { SOL_SOCKET, SO_REUSEADDR,		"SO_REUSEADDR,"	},
617437220cdSdanmcd 	    { SOL_SOCKET, SO_KEEPALIVE,		"SO_KEEPALIVE,"	},
618437220cdSdanmcd 	    { SOL_SOCKET, SO_DONTROUTE,		"SO_DONTROUTE,"	},
619437220cdSdanmcd 	    { SOL_SOCKET, SO_BROADCAST,		"SO_BROADCAST,"	},
620437220cdSdanmcd 	    { SOL_SOCKET, SO_OOBINLINE,		"SO_OOBINLINE,"	},
621437220cdSdanmcd 	    { SOL_SOCKET, SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"},
622437220cdSdanmcd 	    { SOL_SOCKET, SO_ALLZONES,		"SO_ALLZONES,"	},
6235d3b8cb7SBill Sommerfeld 	    { SOL_SOCKET, SO_MAC_EXEMPT,	"SO_MAC_EXEMPT," },
6245d3b8cb7SBill Sommerfeld 	    { SOL_SOCKET, SO_MAC_IMPLICIT,	"SO_MAC_IMPLICIT," },
625437220cdSdanmcd 	    { SOL_SOCKET, SO_EXCLBIND,		"SO_EXCLBIND," },
6261cb875aeSCathy Zhou 	    { SOL_SOCKET, SO_VRRP,		"SO_VRRP," },
627437220cdSdanmcd 	    { IPPROTO_UDP, UDP_NAT_T_ENDPOINT,	"UDP_NAT_T_ENDPOINT," },
6287c478bd9Sstevel@tonic-gate 	};
6297c478bd9Sstevel@tonic-gate 	struct linger l;
6307c478bd9Sstevel@tonic-gate 
6315d0bc3edSsommerfe 	buf[0] = '!';		/* sentinel value, never printed */
6327c478bd9Sstevel@tonic-gate 	buf[1] = '\0';
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) {
6357c478bd9Sstevel@tonic-gate 		vlen = sizeof (val);
636437220cdSdanmcd 		if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt,
637437220cdSdanmcd 		    &val, &vlen) == 0 && val != 0)
6387c478bd9Sstevel@tonic-gate 			(void) strlcat(buf, boolopts[i].name, sizeof (buf));
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	vlen = sizeof (l);
6427c478bd9Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 &&
6437c478bd9Sstevel@tonic-gate 	    l.l_onoff != 0) {
6447c478bd9Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),",
6457c478bd9Sstevel@tonic-gate 		    l.l_linger);
6467c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	vlen = sizeof (val);
6507c478bd9Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) {
6517c478bd9Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val);
6527c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	vlen = sizeof (val);
6557c478bd9Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) {
6567c478bd9Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val);
6577c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
6587c478bd9Sstevel@tonic-gate 	}
65943d18f1cSpriyanka 	vlen = sizeof (nexthop_val);
66043d18f1cSpriyanka 	if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val,
66143d18f1cSpriyanka 	    &vlen) == 0) {
66229e362daSpriyanka 		if (vlen > 0) {
66329e362daSpriyanka 			(void) inet_ntop(AF_INET, (void *) &nexthop_val,
66429e362daSpriyanka 			    ipaddr, sizeof (ipaddr));
66543d18f1cSpriyanka 			(void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),",
66643d18f1cSpriyanka 			    ipaddr);
66743d18f1cSpriyanka 			(void) strlcat(buf, buf1, sizeof (buf));
66843d18f1cSpriyanka 		}
66929e362daSpriyanka 	}
6707c478bd9Sstevel@tonic-gate 
6715d0bc3edSsommerfe 	buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */
6727c478bd9Sstevel@tonic-gate 	if (buf[1] != '\0')
6737c478bd9Sstevel@tonic-gate 		(void) printf("\t%s\n", buf+1);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6763e95bd4aSAnders Persson #define	MAXNALLOC	32
6773e95bd4aSAnders Persson static void
show_sockfilters(struct ps_prochandle * Pr,int fd)6783e95bd4aSAnders Persson show_sockfilters(struct ps_prochandle *Pr, int fd)
6793e95bd4aSAnders Persson {
6803e95bd4aSAnders Persson 	struct fil_info *fi;
6813e95bd4aSAnders Persson 	int i = 0, nalloc = 2, len = nalloc * sizeof (*fi);
6823e95bd4aSAnders Persson 	boolean_t printhdr = B_TRUE;
6833e95bd4aSAnders Persson 
6843e95bd4aSAnders Persson 	fi = calloc(nalloc, sizeof (*fi));
6853e95bd4aSAnders Persson 	if (fi == NULL) {
6863e95bd4aSAnders Persson 		perror("calloc");
6873e95bd4aSAnders Persson 		return;
6883e95bd4aSAnders Persson 	}
6893e95bd4aSAnders Persson 	/* CONSTCOND */
6903e95bd4aSAnders Persson 	while (1) {
6913e95bd4aSAnders Persson 		if (pr_getsockopt(Pr, fd, SOL_FILTER, FIL_LIST, fi, &len) != 0)
6923e95bd4aSAnders Persson 			break;
6933e95bd4aSAnders Persson 		/* No filters */
6943e95bd4aSAnders Persson 		if (len == 0)
6953e95bd4aSAnders Persson 			break;
6963e95bd4aSAnders Persson 		/* Make sure buffer was large enough */
6973e95bd4aSAnders Persson 		if (fi->fi_pos >= nalloc) {
6983e95bd4aSAnders Persson 			struct fil_info *new;
6993e95bd4aSAnders Persson 
7003e95bd4aSAnders Persson 			nalloc = fi->fi_pos + 1;
7013e95bd4aSAnders Persson 			if (nalloc > MAXNALLOC)
7023e95bd4aSAnders Persson 				break;
7033e95bd4aSAnders Persson 			len = nalloc * sizeof (*fi);
7043e95bd4aSAnders Persson 			new = realloc(fi, nalloc * sizeof (*fi));
7053e95bd4aSAnders Persson 			if (new == NULL) {
7063e95bd4aSAnders Persson 				perror("realloc");
7073e95bd4aSAnders Persson 				break;
7083e95bd4aSAnders Persson 			}
7093e95bd4aSAnders Persson 			fi = new;
7103e95bd4aSAnders Persson 			continue;
7113e95bd4aSAnders Persson 		}
7123e95bd4aSAnders Persson 
7133e95bd4aSAnders Persson 		for (i = 0; (i + 1) * sizeof (*fi) <= len; i++) {
7143e95bd4aSAnders Persson 			if (fi[i].fi_flags & FILF_BYPASS)
7153e95bd4aSAnders Persson 				continue;
7163e95bd4aSAnders Persson 			if (printhdr) {
7173e95bd4aSAnders Persson 				(void) printf("\tfilters: ");
7183e95bd4aSAnders Persson 				printhdr = B_FALSE;
7193e95bd4aSAnders Persson 			}
7203e95bd4aSAnders Persson 			(void) printf("%s", fi[i].fi_name);
7213e95bd4aSAnders Persson 			if (fi[i].fi_flags != 0) {
7223e95bd4aSAnders Persson 				(void) printf("(");
7233e95bd4aSAnders Persson 				if (fi[i].fi_flags & FILF_AUTO)
7243e95bd4aSAnders Persson 					(void) printf("auto,");
7253e95bd4aSAnders Persson 				if (fi[i].fi_flags & FILF_PROG)
7263e95bd4aSAnders Persson 					(void) printf("prog,");
7273e95bd4aSAnders Persson 				(void) printf("\b)");
7283e95bd4aSAnders Persson 			}
7293e95bd4aSAnders Persson 			if (fi[i].fi_pos == 0) /* last one */
7303e95bd4aSAnders Persson 				break;
7313e95bd4aSAnders Persson 			(void) printf(",");
7323e95bd4aSAnders Persson 		}
7333e95bd4aSAnders Persson 		if (!printhdr)
7343e95bd4aSAnders Persson 			(void) printf("\n");
7353e95bd4aSAnders Persson 		break;
7363e95bd4aSAnders Persson 	}
7373e95bd4aSAnders Persson 	free(fi);
7383e95bd4aSAnders Persson }
7393e95bd4aSAnders Persson 
740a154f012SAlexander Stetsenko /* print peer credentials for sockets and named pipes */
741a154f012SAlexander Stetsenko static void
dopeerucred(struct ps_prochandle * Pr,int fd)742a154f012SAlexander Stetsenko dopeerucred(struct ps_prochandle *Pr, int fd)
743a154f012SAlexander Stetsenko {
744a154f012SAlexander Stetsenko 	ucred_t *peercred = NULL;	/* allocated by getpeerucred */
745a154f012SAlexander Stetsenko 
746a154f012SAlexander Stetsenko 	if (pr_getpeerucred(Pr, fd, &peercred) == 0) {
747a154f012SAlexander Stetsenko 		show_ucred("peer", peercred);
748a154f012SAlexander Stetsenko 		ucred_free(peercred);
749a154f012SAlexander Stetsenko 	}
750a154f012SAlexander Stetsenko }
751a154f012SAlexander Stetsenko 
7527c478bd9Sstevel@tonic-gate /* the file is a socket */
7537c478bd9Sstevel@tonic-gate static void
dosocket(struct ps_prochandle * Pr,int fd)7547c478bd9Sstevel@tonic-gate dosocket(struct ps_prochandle *Pr, int fd)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	/* A buffer large enough for PATH_MAX size AF_UNIX address */
7577c478bd9Sstevel@tonic-gate 	long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1)
7587c478bd9Sstevel@tonic-gate 	    / sizeof (long)];
7597c478bd9Sstevel@tonic-gate 	struct sockaddr *sa = (struct sockaddr *)buf;
7607c478bd9Sstevel@tonic-gate 	socklen_t len;
7617c478bd9Sstevel@tonic-gate 	int type, tlen;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	tlen = sizeof (type);
764ca9327a6Smeem 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0)
7657c478bd9Sstevel@tonic-gate 		show_socktype((uint_t)type);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	show_sockopts(Pr, fd);
7683e95bd4aSAnders Persson 	show_sockfilters(Pr, fd);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
7717c478bd9Sstevel@tonic-gate 	if (pr_getsockname(Pr, fd, sa, &len) == 0)
7727c478bd9Sstevel@tonic-gate 		show_sockaddr("sockname", sa, len);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
7757c478bd9Sstevel@tonic-gate 	if (pr_getpeername(Pr, fd, sa, &len) == 0)
7767c478bd9Sstevel@tonic-gate 		show_sockaddr("peername", sa, len);
777a154f012SAlexander Stetsenko 
778a154f012SAlexander Stetsenko 	dopeerucred(Pr, fd);
779a154f012SAlexander Stetsenko }
780a154f012SAlexander Stetsenko 
781a154f012SAlexander Stetsenko /* the file is a fifo (aka "named pipe") */
782a154f012SAlexander Stetsenko static void
dofifo(struct ps_prochandle * Pr,int fd)783a154f012SAlexander Stetsenko dofifo(struct ps_prochandle *Pr, int fd)
784a154f012SAlexander Stetsenko {
785a154f012SAlexander Stetsenko 	dopeerucred(Pr, fd);
7867c478bd9Sstevel@tonic-gate }
787ca9327a6Smeem 
788ca9327a6Smeem /* the file is a TLI endpoint */
789ca9327a6Smeem static void
dotli(struct ps_prochandle * Pr,int fd)790ca9327a6Smeem dotli(struct ps_prochandle *Pr, int fd)
791ca9327a6Smeem {
792ca9327a6Smeem 	struct strcmd strcmd;
793ca9327a6Smeem 
794ca9327a6Smeem 	strcmd.sc_len = STRCMDBUFSIZE;
795ca9327a6Smeem 	strcmd.sc_timeout = 5;
796ca9327a6Smeem 
797ca9327a6Smeem 	strcmd.sc_cmd = TI_GETMYNAME;
798ca9327a6Smeem 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
799ca9327a6Smeem 		show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0);
800ca9327a6Smeem 
801ca9327a6Smeem 	strcmd.sc_cmd = TI_GETPEERNAME;
802ca9327a6Smeem 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
803ca9327a6Smeem 		show_sockaddr("peername", (void *)&strcmd.sc_buf, 0);
804ca9327a6Smeem }
805