xref: /onnv-gate/usr/src/cmd/ptools/pfiles/pfiles.c (revision 10934:e209937a4f19)
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*10934Ssommerfeld@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <fcntl.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <signal.h>
340Sstevel@tonic-gate #include <dirent.h>
350Sstevel@tonic-gate #include <limits.h>
360Sstevel@tonic-gate #include <door.h>
370Sstevel@tonic-gate #include <sys/types.h>
381095Spriyanka #include <sys/socket.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <sys/mkdev.h>
416583Smeem #include <sys/stropts.h>
426583Smeem #include <sys/timod.h>
430Sstevel@tonic-gate #include <sys/un.h>
440Sstevel@tonic-gate #include <libproc.h>
451095Spriyanka #include <netinet/in.h>
464987Sdanmcd #include <netinet/udp.h>
470Sstevel@tonic-gate #include <arpa/inet.h>
480Sstevel@tonic-gate 
493507Svb160487 #define	copyflock(dst, src) \
503507Svb160487 	(dst).l_type = (src).l_type;		\
513507Svb160487 	(dst).l_whence = (src).l_whence;	\
523507Svb160487 	(dst).l_start = (src).l_start;		\
533507Svb160487 	(dst).l_len = (src).l_len;		\
543507Svb160487 	(dst).l_sysid = (src).l_sysid;		\
553507Svb160487 	(dst).l_pid = (src).l_pid;
563507Svb160487 
570Sstevel@tonic-gate static char *command;
580Sstevel@tonic-gate static volatile int interrupt;
590Sstevel@tonic-gate static int Fflag;
600Sstevel@tonic-gate static boolean_t nflag = B_FALSE;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static	void	intr(int);
630Sstevel@tonic-gate static	void	dofcntl(struct ps_prochandle *, int, int, int);
640Sstevel@tonic-gate static	void	dosocket(struct ps_prochandle *, int);
656583Smeem static	void	dotli(struct ps_prochandle *, int);
660Sstevel@tonic-gate static	void	show_files(struct ps_prochandle *);
670Sstevel@tonic-gate static	void	show_fileflags(int);
680Sstevel@tonic-gate static	void	show_door(struct ps_prochandle *, int);
693507Svb160487 static	int	getflock(struct ps_prochandle *, int, struct flock *);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate int
720Sstevel@tonic-gate main(int argc, char **argv)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	int retc = 0;
750Sstevel@tonic-gate 	int opt;
760Sstevel@tonic-gate 	int errflg = 0;
770Sstevel@tonic-gate 	struct ps_prochandle *Pr;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
800Sstevel@tonic-gate 		command++;
810Sstevel@tonic-gate 	else
820Sstevel@tonic-gate 		command = argv[0];
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/* options */
850Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "Fn")) != EOF) {
860Sstevel@tonic-gate 		switch (opt) {
870Sstevel@tonic-gate 		case 'F':		/* force grabbing (no O_EXCL) */
880Sstevel@tonic-gate 			Fflag = PGRAB_FORCE;
890Sstevel@tonic-gate 			break;
900Sstevel@tonic-gate 		case 'n':
910Sstevel@tonic-gate 			nflag = B_TRUE;
920Sstevel@tonic-gate 			break;
930Sstevel@tonic-gate 		default:
940Sstevel@tonic-gate 			errflg = 1;
950Sstevel@tonic-gate 			break;
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	argc -= optind;
1000Sstevel@tonic-gate 	argv += optind;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (errflg || argc <= 0) {
1030Sstevel@tonic-gate 		(void) fprintf(stderr, "usage:\t%s [-F] pid ...\n",
1044987Sdanmcd 		    command);
1050Sstevel@tonic-gate 		(void) fprintf(stderr,
1064987Sdanmcd 		    "  (report open files of each process)\n");
1070Sstevel@tonic-gate 		(void) fprintf(stderr,
1084987Sdanmcd 		    "  -F: force grabbing of the target process\n");
1090Sstevel@tonic-gate 		exit(2);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/* catch signals from terminal */
1130Sstevel@tonic-gate 	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
1140Sstevel@tonic-gate 		(void) sigset(SIGHUP, intr);
1150Sstevel@tonic-gate 	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
1160Sstevel@tonic-gate 		(void) sigset(SIGINT, intr);
1170Sstevel@tonic-gate 	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
1180Sstevel@tonic-gate 		(void) sigset(SIGQUIT, intr);
1190Sstevel@tonic-gate 	(void) sigset(SIGPIPE, intr);
1200Sstevel@tonic-gate 	(void) sigset(SIGTERM, intr);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	(void) proc_initstdio();
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	while (--argc >= 0 && !interrupt) {
1260Sstevel@tonic-gate 		char *arg;
1270Sstevel@tonic-gate 		psinfo_t psinfo;
1280Sstevel@tonic-gate 		pid_t pid;
1290Sstevel@tonic-gate 		int gret;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 		(void) proc_flushstdio();
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		/* get the specified pid and the psinfo struct */
1340Sstevel@tonic-gate 		if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS,
1350Sstevel@tonic-gate 		    &psinfo, &gret)) == -1) {
1360Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1374987Sdanmcd 			    command, arg, Pgrab_error(gret));
1380Sstevel@tonic-gate 			retc++;
1390Sstevel@tonic-gate 		} else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) {
1400Sstevel@tonic-gate 			if (Pcreate_agent(Pr) == 0) {
1410Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
1420Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n",
1434987Sdanmcd 				    (int)pid, psinfo.pr_psargs);
1440Sstevel@tonic-gate 				show_files(Pr);
1450Sstevel@tonic-gate 				Pdestroy_agent(Pr);
1460Sstevel@tonic-gate 			} else {
1470Sstevel@tonic-gate 				(void) fprintf(stderr,
1484987Sdanmcd 				    "%s: cannot control process %d\n",
1494987Sdanmcd 				    command, (int)pid);
1500Sstevel@tonic-gate 				retc++;
1510Sstevel@tonic-gate 			}
1520Sstevel@tonic-gate 			Prelease(Pr, 0);
1530Sstevel@tonic-gate 			Pr = NULL;
1540Sstevel@tonic-gate 		} else {
1550Sstevel@tonic-gate 			switch (gret) {
1560Sstevel@tonic-gate 			case G_SYS:
1570Sstevel@tonic-gate 			case G_SELF:
1580Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
1590Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n", (int)pid,
1604987Sdanmcd 				    psinfo.pr_psargs);
1610Sstevel@tonic-gate 				if (gret == G_SYS)
1620Sstevel@tonic-gate 					(void) printf("  [system process]\n");
1630Sstevel@tonic-gate 				else
1640Sstevel@tonic-gate 					show_files(NULL);
1650Sstevel@tonic-gate 				break;
1660Sstevel@tonic-gate 			default:
1670Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: %s: %d\n",
1684987Sdanmcd 				    command, Pgrab_error(gret), (int)pid);
1690Sstevel@tonic-gate 				retc++;
1700Sstevel@tonic-gate 				break;
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	(void) proc_finistdio();
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (interrupt && retc == 0)
1780Sstevel@tonic-gate 		retc++;
1790Sstevel@tonic-gate 	return (retc);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /* ARGSUSED */
1830Sstevel@tonic-gate static void
1840Sstevel@tonic-gate intr(int sig)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	interrupt = 1;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /* ------ begin specific code ------ */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static void
1920Sstevel@tonic-gate show_files(struct ps_prochandle *Pr)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	DIR *dirp;
1950Sstevel@tonic-gate 	struct dirent *dentp;
1966583Smeem 	const char *dev;
1970Sstevel@tonic-gate 	char pname[100];
1980Sstevel@tonic-gate 	char fname[PATH_MAX];
1990Sstevel@tonic-gate 	struct stat64 statb;
2000Sstevel@tonic-gate 	struct rlimit rlim;
2010Sstevel@tonic-gate 	pid_t pid;
2020Sstevel@tonic-gate 	int fd;
2030Sstevel@tonic-gate 	char *s;
2040Sstevel@tonic-gate 	int ret;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) {
2070Sstevel@tonic-gate 		ulong_t nfd = rlim.rlim_cur;
2080Sstevel@tonic-gate 		if (nfd == RLIM_INFINITY)
2090Sstevel@tonic-gate 			(void) printf(
2100Sstevel@tonic-gate 			    "  Current rlimit: unlimited file descriptors\n");
2110Sstevel@tonic-gate 		else
2120Sstevel@tonic-gate 			(void) printf(
2130Sstevel@tonic-gate 			    "  Current rlimit: %lu file descriptors\n", nfd);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/* in case we are doing this to ourself */
2170Sstevel@tonic-gate 	pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	(void) sprintf(pname, "/proc/%d/fd", (int)pid);
2200Sstevel@tonic-gate 	if ((dirp = opendir(pname)) == NULL) {
2210Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot open directory %s\n",
2220Sstevel@tonic-gate 		    command, pname);
2230Sstevel@tonic-gate 		return;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/* for each open file --- */
2270Sstevel@tonic-gate 	while ((dentp = readdir(dirp)) != NULL && !interrupt) {
2280Sstevel@tonic-gate 		char unknown[12];
2290Sstevel@tonic-gate 		dev_t rdev;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		/* skip '.' and '..' */
2320Sstevel@tonic-gate 		if (!isdigit(dentp->d_name[0]))
2330Sstevel@tonic-gate 			continue;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		fd = atoi(dentp->d_name);
2360Sstevel@tonic-gate 		if (pr_fstat64(Pr, fd, &statb) == -1) {
2370Sstevel@tonic-gate 			s = unknown;
2380Sstevel@tonic-gate 			(void) sprintf(s, "%4d", fd);
2390Sstevel@tonic-gate 			perror(s);
2400Sstevel@tonic-gate 			continue;
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		rdev = NODEV;
2440Sstevel@tonic-gate 		switch (statb.st_mode & S_IFMT) {
2450Sstevel@tonic-gate 		case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break;
2460Sstevel@tonic-gate 		case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break;
2470Sstevel@tonic-gate 		case S_IFIFO: s = "S_IFIFO"; break;
2480Sstevel@tonic-gate 		case S_IFDIR: s = "S_IFDIR"; break;
2490Sstevel@tonic-gate 		case S_IFREG: s = "S_IFREG"; break;
2500Sstevel@tonic-gate 		case S_IFLNK: s = "S_IFLNK"; break;
2510Sstevel@tonic-gate 		case S_IFSOCK: s = "S_IFSOCK"; break;
2520Sstevel@tonic-gate 		case S_IFDOOR: s = "S_IFDOOR"; break;
2530Sstevel@tonic-gate 		case S_IFPORT: s = "S_IFPORT"; break;
2540Sstevel@tonic-gate 		default:
2550Sstevel@tonic-gate 			s = unknown;
2560Sstevel@tonic-gate 			(void) sprintf(s, "0x%.4x ",
2574987Sdanmcd 			    (int)statb.st_mode & S_IFMT);
2580Sstevel@tonic-gate 			break;
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 
2614987Sdanmcd 		(void) printf("%4d: %s mode:0%.3o", fd, s,
2624987Sdanmcd 		    (int)statb.st_mode & ~S_IFMT);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		if (major(statb.st_dev) != (major_t)NODEV &&
2650Sstevel@tonic-gate 		    minor(statb.st_dev) != (minor_t)NODEV)
2660Sstevel@tonic-gate 			(void) printf(" dev:%lu,%lu",
2674987Sdanmcd 			    (ulong_t)major(statb.st_dev),
2684987Sdanmcd 			    (ulong_t)minor(statb.st_dev));
2690Sstevel@tonic-gate 		else
2700Sstevel@tonic-gate 			(void) printf(" dev:0x%.8lX", (long)statb.st_dev);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		if ((statb.st_mode & S_IFMT) == S_IFPORT) {
2730Sstevel@tonic-gate 			(void) printf(" uid:%d gid:%d",
2740Sstevel@tonic-gate 			    (int)statb.st_uid,
2750Sstevel@tonic-gate 			    (int)statb.st_gid);
2760Sstevel@tonic-gate 			(void) printf(" size:%lld\n",
2770Sstevel@tonic-gate 			    (longlong_t)statb.st_size);
2780Sstevel@tonic-gate 			continue;
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		(void) printf(" ino:%llu uid:%d gid:%d",
2824987Sdanmcd 		    (u_longlong_t)statb.st_ino,
2834987Sdanmcd 		    (int)statb.st_uid, (int)statb.st_gid);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		if (rdev == NODEV)
2860Sstevel@tonic-gate 			(void) printf(" size:%lld\n",
2874987Sdanmcd 			    (longlong_t)statb.st_size);
2880Sstevel@tonic-gate 		else if (major(rdev) != (major_t)NODEV &&
2890Sstevel@tonic-gate 		    minor(rdev) != (minor_t)NODEV)
2900Sstevel@tonic-gate 			(void) printf(" rdev:%lu,%lu\n",
2914987Sdanmcd 			    (ulong_t)major(rdev), (ulong_t)minor(rdev));
2920Sstevel@tonic-gate 		else
2930Sstevel@tonic-gate 			(void) printf(" rdev:0x%.8lX\n", (long)rdev);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		if (!nflag) {
2960Sstevel@tonic-gate 			dofcntl(Pr, fd,
2974987Sdanmcd 			    (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP))
2984987Sdanmcd 			    == (S_IFREG|S_ENFMT),
2994987Sdanmcd 			    (statb.st_mode & S_IFMT) == S_IFDOOR);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			if ((statb.st_mode & S_IFMT) == S_IFSOCK)
3020Sstevel@tonic-gate 				dosocket(Pr, fd);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 			(void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd);
3050Sstevel@tonic-gate 
3066583Smeem 			if ((ret = readlink(pname, fname, PATH_MAX - 1)) <= 0)
3076583Smeem 				continue;
3086583Smeem 
3096583Smeem 			fname[ret] = '\0';
3106583Smeem 
3116583Smeem 			if ((statb.st_mode & S_IFMT) == S_IFCHR &&
3126583Smeem 			    (dev = strrchr(fname, ':')) != NULL) {
3136583Smeem 				/*
3146583Smeem 				 * There's no elegant way to determine if a
3156583Smeem 				 * character device supports TLI, so we lame
3166583Smeem 				 * out and just check a hardcoded list of
3176583Smeem 				 * known TLI devices.
3186583Smeem 				 */
3196583Smeem 				int i;
3206583Smeem 				const char *tlidevs[] =
3216583Smeem 				    { "tcp", "tcp6", "udp", "udp6", NULL };
3226583Smeem 
3236583Smeem 				dev++; /* skip past the `:' */
3246583Smeem 				for (i = 0; tlidevs[i] != NULL; i++) {
3256583Smeem 					if (strcmp(dev, tlidevs[i]) == 0) {
3266583Smeem 						dotli(Pr, fd);
3276583Smeem 						break;
3286583Smeem 					}
3296583Smeem 				}
3300Sstevel@tonic-gate 			}
3316583Smeem 			(void) printf("      %s\n", fname);
3320Sstevel@tonic-gate 		}
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 	(void) closedir(dirp);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3373507Svb160487 
3383507Svb160487 static int
3393507Svb160487 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native)
3403507Svb160487 {
3413507Svb160487 	int ret;
3423507Svb160487 #ifdef _LP64
3433507Svb160487 	struct flock64_32 flock_target;
3443507Svb160487 
3453507Svb160487 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) {
3463507Svb160487 		copyflock(flock_target, *flock_native);
3473507Svb160487 		ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target);
3483507Svb160487 		copyflock(*flock_native, flock_target);
3493507Svb160487 		return (ret);
3503507Svb160487 	}
3513507Svb160487 #endif /* _LP64 */
3523507Svb160487 	ret = pr_fcntl(Pr, fd, F_GETLK, flock_native);
3533507Svb160487 	return (ret);
3543507Svb160487 }
3553507Svb160487 
3560Sstevel@tonic-gate /* examine open file with fcntl() */
3570Sstevel@tonic-gate static void
3584987Sdanmcd dofcntl(struct ps_prochandle *Pr, int fd, int mandatory, int isdoor)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	struct flock flock;
3610Sstevel@tonic-gate 	int fileflags;
3620Sstevel@tonic-gate 	int fdflags;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0);
3650Sstevel@tonic-gate 	fdflags = pr_fcntl(Pr, fd, F_GETFD, 0);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (fileflags != -1 || fdflags != -1) {
3680Sstevel@tonic-gate 		(void) printf("      ");
3690Sstevel@tonic-gate 		if (fileflags != -1)
3700Sstevel@tonic-gate 			show_fileflags(fileflags);
3710Sstevel@tonic-gate 		if (fdflags != -1 && (fdflags & FD_CLOEXEC))
3720Sstevel@tonic-gate 			(void) printf(" FD_CLOEXEC");
3730Sstevel@tonic-gate 		if (isdoor)
3740Sstevel@tonic-gate 			show_door(Pr, fd);
3750Sstevel@tonic-gate 		(void) fputc('\n', stdout);
3760Sstevel@tonic-gate 	} else if (isdoor) {
3770Sstevel@tonic-gate 		(void) printf("    ");
3780Sstevel@tonic-gate 		show_door(Pr, fd);
3790Sstevel@tonic-gate 		(void) fputc('\n', stdout);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
3830Sstevel@tonic-gate 	flock.l_whence = 0;
3840Sstevel@tonic-gate 	flock.l_start = 0;
3850Sstevel@tonic-gate 	flock.l_len = 0;
3860Sstevel@tonic-gate 	flock.l_sysid = 0;
3870Sstevel@tonic-gate 	flock.l_pid = 0;
3883507Svb160487 	if (getflock(Pr, fd, &flock) != -1) {
3890Sstevel@tonic-gate 		if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) {
3900Sstevel@tonic-gate 			unsigned long sysid = flock.l_sysid;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 			(void) printf("      %s %s lock set by",
3934987Sdanmcd 			    mandatory ? "mandatory" : "advisory",
3944987Sdanmcd 			    flock.l_type == F_RDLCK? "read" : "write");
3950Sstevel@tonic-gate 			if (sysid)
3960Sstevel@tonic-gate 				(void) printf(" system 0x%lX", sysid);
3970Sstevel@tonic-gate 			if (flock.l_pid)
3980Sstevel@tonic-gate 				(void) printf(" process %d", (int)flock.l_pid);
3990Sstevel@tonic-gate 			(void) fputc('\n', stdout);
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate #ifdef O_PRIV
4050Sstevel@tonic-gate #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
4060Sstevel@tonic-gate 			O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
4070Sstevel@tonic-gate 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
4080Sstevel@tonic-gate #else
4090Sstevel@tonic-gate #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
4100Sstevel@tonic-gate 			O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
4110Sstevel@tonic-gate 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
4120Sstevel@tonic-gate #endif
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate static void
4150Sstevel@tonic-gate show_fileflags(int flags)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	char buffer[136];
4180Sstevel@tonic-gate 	char *str = buffer;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	switch (flags & O_ACCMODE) {
4210Sstevel@tonic-gate 	case O_RDONLY:
4220Sstevel@tonic-gate 		(void) strcpy(str, "O_RDONLY");
4230Sstevel@tonic-gate 		break;
4240Sstevel@tonic-gate 	case O_WRONLY:
4250Sstevel@tonic-gate 		(void) strcpy(str, "O_WRONLY");
4260Sstevel@tonic-gate 		break;
4270Sstevel@tonic-gate 	case O_RDWR:
4280Sstevel@tonic-gate 		(void) strcpy(str, "O_RDWR");
4290Sstevel@tonic-gate 		break;
4300Sstevel@tonic-gate 	default:
4310Sstevel@tonic-gate 		(void) sprintf(str, "0x%x", flags & O_ACCMODE);
4320Sstevel@tonic-gate 		break;
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (flags & O_NDELAY)
4360Sstevel@tonic-gate 		(void) strcat(str, "|O_NDELAY");
4370Sstevel@tonic-gate 	if (flags & O_NONBLOCK)
4380Sstevel@tonic-gate 		(void) strcat(str, "|O_NONBLOCK");
4390Sstevel@tonic-gate 	if (flags & O_APPEND)
4400Sstevel@tonic-gate 		(void) strcat(str, "|O_APPEND");
4410Sstevel@tonic-gate #ifdef O_PRIV
4420Sstevel@tonic-gate 	if (flags & O_PRIV)
4430Sstevel@tonic-gate 		(void) strcat(str, "|O_PRIV");
4440Sstevel@tonic-gate #endif
4450Sstevel@tonic-gate 	if (flags & O_SYNC)
4460Sstevel@tonic-gate 		(void) strcat(str, "|O_SYNC");
4470Sstevel@tonic-gate 	if (flags & O_DSYNC)
4480Sstevel@tonic-gate 		(void) strcat(str, "|O_DSYNC");
4490Sstevel@tonic-gate 	if (flags & O_RSYNC)
4500Sstevel@tonic-gate 		(void) strcat(str, "|O_RSYNC");
4510Sstevel@tonic-gate 	if (flags & O_CREAT)
4520Sstevel@tonic-gate 		(void) strcat(str, "|O_CREAT");
4530Sstevel@tonic-gate 	if (flags & O_TRUNC)
4540Sstevel@tonic-gate 		(void) strcat(str, "|O_TRUNC");
4550Sstevel@tonic-gate 	if (flags & O_EXCL)
4560Sstevel@tonic-gate 		(void) strcat(str, "|O_EXCL");
4570Sstevel@tonic-gate 	if (flags & O_NOCTTY)
4580Sstevel@tonic-gate 		(void) strcat(str, "|O_NOCTTY");
4590Sstevel@tonic-gate 	if (flags & O_LARGEFILE)
4600Sstevel@tonic-gate 		(void) strcat(str, "|O_LARGEFILE");
4610Sstevel@tonic-gate 	if (flags & O_XATTR)
4620Sstevel@tonic-gate 		(void) strcat(str, "|O_XATTR");
4630Sstevel@tonic-gate 	if (flags & ~(ALL_O_FLAGS))
4640Sstevel@tonic-gate 		(void) sprintf(str + strlen(str), "|0x%x",
4654987Sdanmcd 		    flags & ~(ALL_O_FLAGS));
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	(void) printf("%s", str);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /* show door info */
4710Sstevel@tonic-gate static void
4720Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	door_info_t door_info;
4750Sstevel@tonic-gate 	psinfo_t psinfo;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if (pr_door_info(Pr, fd, &door_info) != 0)
4780Sstevel@tonic-gate 		return;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (proc_get_psinfo(door_info.di_target, &psinfo) != 0)
4810Sstevel@tonic-gate 		psinfo.pr_fname[0] = '\0';
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	(void) printf("  door to ");
4840Sstevel@tonic-gate 	if (psinfo.pr_fname[0] != '\0')
4850Sstevel@tonic-gate 		(void) printf("%s[%d]", psinfo.pr_fname,
4864987Sdanmcd 		    (int)door_info.di_target);
4870Sstevel@tonic-gate 	else
4880Sstevel@tonic-gate 		(void) printf("pid %d", (int)door_info.di_target);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
4916583Smeem /*
4926583Smeem  * Print out the socket address pointed to by `sa'.  `len' is only
4936583Smeem  * needed for AF_UNIX sockets.
4946583Smeem  */
4950Sstevel@tonic-gate static void
4960Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
4970Sstevel@tonic-gate {
4986583Smeem 	struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa;
4996583Smeem 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa;
5000Sstevel@tonic-gate 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
5010Sstevel@tonic-gate 	char  abuf[INET6_ADDRSTRLEN];
5020Sstevel@tonic-gate 	const char *p;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	switch (sa->sa_family) {
5050Sstevel@tonic-gate 	default:
5060Sstevel@tonic-gate 		return;
5070Sstevel@tonic-gate 	case AF_INET:
5086583Smeem 		(void) printf("\t%s: AF_INET %s  port: %u\n", str,
5090Sstevel@tonic-gate 		    inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)),
5100Sstevel@tonic-gate 		    ntohs(so_in->sin_port));
5110Sstevel@tonic-gate 		return;
5120Sstevel@tonic-gate 	case AF_INET6:
5136583Smeem 		(void) printf("\t%s: AF_INET6 %s  port: %u\n", str,
5140Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &so_in6->sin6_addr,
5150Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
5160Sstevel@tonic-gate 		    ntohs(so_in->sin_port));
5170Sstevel@tonic-gate 		return;
5180Sstevel@tonic-gate 	case AF_UNIX:
5190Sstevel@tonic-gate 		if (len >= sizeof (so_un->sun_family)) {
5200Sstevel@tonic-gate 			/* Null terminate */
5210Sstevel@tonic-gate 			len -= sizeof (so_un->sun_family);
5226583Smeem 			so_un->sun_path[len] = '\0';
5230Sstevel@tonic-gate 			(void) printf("\t%s: AF_UNIX %s\n",
5244987Sdanmcd 			    str, so_un->sun_path);
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 		return;
5270Sstevel@tonic-gate 	case AF_IMPLINK:	p = "AF_IMPLINK";	break;
5280Sstevel@tonic-gate 	case AF_PUP:		p = "AF_PUP";		break;
5290Sstevel@tonic-gate 	case AF_CHAOS:		p = "AF_CHAOS";		break;
5300Sstevel@tonic-gate 	case AF_NS:		p = "AF_NS";		break;
5310Sstevel@tonic-gate 	case AF_NBS:		p = "AF_NBS";		break;
5320Sstevel@tonic-gate 	case AF_ECMA:		p = "AF_ECMA";		break;
5330Sstevel@tonic-gate 	case AF_DATAKIT:	p = "AF_DATAKIT";	break;
5340Sstevel@tonic-gate 	case AF_CCITT:		p = "AF_CCITT";		break;
5350Sstevel@tonic-gate 	case AF_SNA:		p = "AF_SNA";		break;
5360Sstevel@tonic-gate 	case AF_DECnet:		p = "AF_DECnet";	break;
5370Sstevel@tonic-gate 	case AF_DLI:		p = "AF_DLI";		break;
5380Sstevel@tonic-gate 	case AF_LAT:		p = "AF_LAT";		break;
5390Sstevel@tonic-gate 	case AF_HYLINK:		p = "AF_HYLINK";	break;
5400Sstevel@tonic-gate 	case AF_APPLETALK:	p = "AF_APPLETALK";	break;
5410Sstevel@tonic-gate 	case AF_NIT:		p = "AF_NIT";		break;
5420Sstevel@tonic-gate 	case AF_802:		p = "AF_802";		break;
5430Sstevel@tonic-gate 	case AF_OSI:		p = "AF_OSI";		break;
5440Sstevel@tonic-gate 	case AF_X25:		p = "AF_X25";		break;
5450Sstevel@tonic-gate 	case AF_OSINET:		p = "AF_OSINET";	break;
5460Sstevel@tonic-gate 	case AF_GOSIP:		p = "AF_GOSIP";		break;
5470Sstevel@tonic-gate 	case AF_IPX:		p = "AF_IPX";		break;
5480Sstevel@tonic-gate 	case AF_ROUTE:		p = "AF_ROUTE";		break;
5490Sstevel@tonic-gate 	case AF_LINK:		p = "AF_LINK";		break;
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	(void) printf("\t%s: %s\n", str, p);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate static void
5560Sstevel@tonic-gate show_socktype(uint_t type)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	static const char *types[] = {
5590Sstevel@tonic-gate 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
5600Sstevel@tonic-gate 	};
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
5630Sstevel@tonic-gate 		(void) printf("\tSOCK_%s\n", types[type]);
5640Sstevel@tonic-gate 	else
5650Sstevel@tonic-gate 		(void) printf("\tunknown socket type %u\n", type);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5681095Spriyanka #define	BUFSIZE	200
5690Sstevel@tonic-gate static void
5700Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	int val, vlen;
5731095Spriyanka 	char buf[BUFSIZE];
5740Sstevel@tonic-gate 	char buf1[32];
5751095Spriyanka 	char ipaddr[INET_ADDRSTRLEN];
5760Sstevel@tonic-gate 	int i;
5771095Spriyanka 	in_addr_t nexthop_val;
5780Sstevel@tonic-gate 	struct boolopt {
5794987Sdanmcd 		int		level;
5800Sstevel@tonic-gate 		int		opt;
5810Sstevel@tonic-gate 		const char	*name;
5820Sstevel@tonic-gate 	};
5830Sstevel@tonic-gate 	static struct boolopt boolopts[] = {
5844987Sdanmcd 	    { SOL_SOCKET, SO_DEBUG,		"SO_DEBUG,"	},
5854987Sdanmcd 	    { SOL_SOCKET, SO_REUSEADDR,		"SO_REUSEADDR,"	},
5864987Sdanmcd 	    { SOL_SOCKET, SO_KEEPALIVE,		"SO_KEEPALIVE,"	},
5874987Sdanmcd 	    { SOL_SOCKET, SO_DONTROUTE,		"SO_DONTROUTE,"	},
5884987Sdanmcd 	    { SOL_SOCKET, SO_BROADCAST,		"SO_BROADCAST,"	},
5894987Sdanmcd 	    { SOL_SOCKET, SO_OOBINLINE,		"SO_OOBINLINE,"	},
5904987Sdanmcd 	    { SOL_SOCKET, SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"},
5914987Sdanmcd 	    { SOL_SOCKET, SO_ALLZONES,		"SO_ALLZONES,"	},
592*10934Ssommerfeld@sun.com 	    { SOL_SOCKET, SO_MAC_EXEMPT,	"SO_MAC_EXEMPT," },
593*10934Ssommerfeld@sun.com 	    { SOL_SOCKET, SO_MAC_IMPLICIT,	"SO_MAC_IMPLICIT," },
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);
6566583Smeem 	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 }
6696583Smeem 
6706583Smeem /* the file is a TLI endpoint */
6716583Smeem static void
6726583Smeem dotli(struct ps_prochandle *Pr, int fd)
6736583Smeem {
6746583Smeem 	struct strcmd strcmd;
6756583Smeem 
6766583Smeem 	strcmd.sc_len = STRCMDBUFSIZE;
6776583Smeem 	strcmd.sc_timeout = 5;
6786583Smeem 
6796583Smeem 	strcmd.sc_cmd = TI_GETMYNAME;
6806583Smeem 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
6816583Smeem 		show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0);
6826583Smeem 
6836583Smeem 	strcmd.sc_cmd = TI_GETPEERNAME;
6846583Smeem 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
6856583Smeem 		show_sockaddr("peername", (void *)&strcmd.sc_buf, 0);
6866583Smeem }
687