xref: /onnv-gate/usr/src/cmd/ptools/pfiles/pfiles.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <unistd.h>
32*0Sstevel@tonic-gate #include <fcntl.h>
33*0Sstevel@tonic-gate #include <ctype.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <signal.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <dirent.h>
38*0Sstevel@tonic-gate #include <limits.h>
39*0Sstevel@tonic-gate #include <door.h>
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <sys/stat.h>
42*0Sstevel@tonic-gate #include <sys/mman.h>
43*0Sstevel@tonic-gate #include <sys/mkdev.h>
44*0Sstevel@tonic-gate #include <sys/un.h>
45*0Sstevel@tonic-gate #include <netdb.h>
46*0Sstevel@tonic-gate #include <libproc.h>
47*0Sstevel@tonic-gate #include <arpa/inet.h>
48*0Sstevel@tonic-gate #include <netdb.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static char *command;
51*0Sstevel@tonic-gate static volatile int interrupt;
52*0Sstevel@tonic-gate static int Fflag;
53*0Sstevel@tonic-gate static boolean_t nflag = B_FALSE;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static	void	intr(int);
56*0Sstevel@tonic-gate static	void	dofcntl(struct ps_prochandle *, int, int, int);
57*0Sstevel@tonic-gate static	void	dosocket(struct ps_prochandle *, int);
58*0Sstevel@tonic-gate static	void	show_files(struct ps_prochandle *);
59*0Sstevel@tonic-gate static	void	show_fileflags(int);
60*0Sstevel@tonic-gate static	void	show_door(struct ps_prochandle *, int);
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate int
63*0Sstevel@tonic-gate main(int argc, char **argv)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	int retc = 0;
66*0Sstevel@tonic-gate 	int opt;
67*0Sstevel@tonic-gate 	int errflg = 0;
68*0Sstevel@tonic-gate 	struct ps_prochandle *Pr;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
71*0Sstevel@tonic-gate 		command++;
72*0Sstevel@tonic-gate 	else
73*0Sstevel@tonic-gate 		command = argv[0];
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	/* options */
76*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "Fn")) != EOF) {
77*0Sstevel@tonic-gate 		switch (opt) {
78*0Sstevel@tonic-gate 		case 'F':		/* force grabbing (no O_EXCL) */
79*0Sstevel@tonic-gate 			Fflag = PGRAB_FORCE;
80*0Sstevel@tonic-gate 			break;
81*0Sstevel@tonic-gate 		case 'n':
82*0Sstevel@tonic-gate 			nflag = B_TRUE;
83*0Sstevel@tonic-gate 			break;
84*0Sstevel@tonic-gate 		default:
85*0Sstevel@tonic-gate 			errflg = 1;
86*0Sstevel@tonic-gate 			break;
87*0Sstevel@tonic-gate 		}
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	argc -= optind;
91*0Sstevel@tonic-gate 	argv += optind;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if (errflg || argc <= 0) {
94*0Sstevel@tonic-gate 		(void) fprintf(stderr, "usage:\t%s [-F] pid ...\n",
95*0Sstevel@tonic-gate 			command);
96*0Sstevel@tonic-gate 		(void) fprintf(stderr,
97*0Sstevel@tonic-gate 			"  (report open files of each process)\n");
98*0Sstevel@tonic-gate 		(void) fprintf(stderr,
99*0Sstevel@tonic-gate 			"  -F: force grabbing of the target process\n");
100*0Sstevel@tonic-gate 		exit(2);
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/* catch signals from terminal */
104*0Sstevel@tonic-gate 	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
105*0Sstevel@tonic-gate 		(void) sigset(SIGHUP, intr);
106*0Sstevel@tonic-gate 	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
107*0Sstevel@tonic-gate 		(void) sigset(SIGINT, intr);
108*0Sstevel@tonic-gate 	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
109*0Sstevel@tonic-gate 		(void) sigset(SIGQUIT, intr);
110*0Sstevel@tonic-gate 	(void) sigset(SIGPIPE, intr);
111*0Sstevel@tonic-gate 	(void) sigset(SIGTERM, intr);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	(void) proc_initstdio();
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	while (--argc >= 0 && !interrupt) {
117*0Sstevel@tonic-gate 		char *arg;
118*0Sstevel@tonic-gate 		psinfo_t psinfo;
119*0Sstevel@tonic-gate 		pid_t pid;
120*0Sstevel@tonic-gate 		int gret;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		(void) proc_flushstdio();
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 		/* get the specified pid and the psinfo struct */
125*0Sstevel@tonic-gate 		if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS,
126*0Sstevel@tonic-gate 		    &psinfo, &gret)) == -1) {
127*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
128*0Sstevel@tonic-gate 				command, arg, Pgrab_error(gret));
129*0Sstevel@tonic-gate 			retc++;
130*0Sstevel@tonic-gate 		} else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) {
131*0Sstevel@tonic-gate 			if (Pcreate_agent(Pr) == 0) {
132*0Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
133*0Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n",
134*0Sstevel@tonic-gate 					(int)pid, psinfo.pr_psargs);
135*0Sstevel@tonic-gate 				show_files(Pr);
136*0Sstevel@tonic-gate 				Pdestroy_agent(Pr);
137*0Sstevel@tonic-gate 			} else {
138*0Sstevel@tonic-gate 				(void) fprintf(stderr,
139*0Sstevel@tonic-gate 					"%s: cannot control process %d\n",
140*0Sstevel@tonic-gate 					command, (int)pid);
141*0Sstevel@tonic-gate 				retc++;
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 			Prelease(Pr, 0);
144*0Sstevel@tonic-gate 			Pr = NULL;
145*0Sstevel@tonic-gate 		} else {
146*0Sstevel@tonic-gate 			switch (gret) {
147*0Sstevel@tonic-gate 			case G_SYS:
148*0Sstevel@tonic-gate 			case G_SELF:
149*0Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
150*0Sstevel@tonic-gate 				(void) printf("%d:\t%.70s\n", (int)pid,
151*0Sstevel@tonic-gate 					psinfo.pr_psargs);
152*0Sstevel@tonic-gate 				if (gret == G_SYS)
153*0Sstevel@tonic-gate 					(void) printf("  [system process]\n");
154*0Sstevel@tonic-gate 				else
155*0Sstevel@tonic-gate 					show_files(NULL);
156*0Sstevel@tonic-gate 				break;
157*0Sstevel@tonic-gate 			default:
158*0Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: %s: %d\n",
159*0Sstevel@tonic-gate 					command, Pgrab_error(gret), (int)pid);
160*0Sstevel@tonic-gate 				retc++;
161*0Sstevel@tonic-gate 				break;
162*0Sstevel@tonic-gate 			}
163*0Sstevel@tonic-gate 		}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	(void) proc_finistdio();
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if (interrupt && retc == 0)
171*0Sstevel@tonic-gate 		retc++;
172*0Sstevel@tonic-gate 	return (retc);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate /* ARGSUSED */
176*0Sstevel@tonic-gate static void
177*0Sstevel@tonic-gate intr(int sig)
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	interrupt = 1;
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate /* ------ begin specific code ------ */
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate static void
185*0Sstevel@tonic-gate show_files(struct ps_prochandle *Pr)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	DIR *dirp;
188*0Sstevel@tonic-gate 	struct dirent *dentp;
189*0Sstevel@tonic-gate 	char pname[100];
190*0Sstevel@tonic-gate 	char fname[PATH_MAX];
191*0Sstevel@tonic-gate 	struct stat64 statb;
192*0Sstevel@tonic-gate 	struct rlimit rlim;
193*0Sstevel@tonic-gate 	pid_t pid;
194*0Sstevel@tonic-gate 	int fd;
195*0Sstevel@tonic-gate 	char *s;
196*0Sstevel@tonic-gate 	int ret;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) {
199*0Sstevel@tonic-gate 		ulong_t nfd = rlim.rlim_cur;
200*0Sstevel@tonic-gate 		if (nfd == RLIM_INFINITY)
201*0Sstevel@tonic-gate 			(void) printf(
202*0Sstevel@tonic-gate 			    "  Current rlimit: unlimited file descriptors\n");
203*0Sstevel@tonic-gate 		else
204*0Sstevel@tonic-gate 			(void) printf(
205*0Sstevel@tonic-gate 			    "  Current rlimit: %lu file descriptors\n", nfd);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	/* in case we are doing this to ourself */
209*0Sstevel@tonic-gate 	pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	(void) sprintf(pname, "/proc/%d/fd", (int)pid);
212*0Sstevel@tonic-gate 	if ((dirp = opendir(pname)) == NULL) {
213*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot open directory %s\n",
214*0Sstevel@tonic-gate 		    command, pname);
215*0Sstevel@tonic-gate 		return;
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/* for each open file --- */
219*0Sstevel@tonic-gate 	while ((dentp = readdir(dirp)) != NULL && !interrupt) {
220*0Sstevel@tonic-gate 		char unknown[12];
221*0Sstevel@tonic-gate 		dev_t rdev;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		/* skip '.' and '..' */
224*0Sstevel@tonic-gate 		if (!isdigit(dentp->d_name[0]))
225*0Sstevel@tonic-gate 			continue;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 		fd = atoi(dentp->d_name);
228*0Sstevel@tonic-gate 		if (pr_fstat64(Pr, fd, &statb) == -1) {
229*0Sstevel@tonic-gate 			s = unknown;
230*0Sstevel@tonic-gate 			(void) sprintf(s, "%4d", fd);
231*0Sstevel@tonic-gate 			perror(s);
232*0Sstevel@tonic-gate 			continue;
233*0Sstevel@tonic-gate 		}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		rdev = NODEV;
236*0Sstevel@tonic-gate 		switch (statb.st_mode & S_IFMT) {
237*0Sstevel@tonic-gate 		case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break;
238*0Sstevel@tonic-gate 		case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break;
239*0Sstevel@tonic-gate 		case S_IFIFO: s = "S_IFIFO"; break;
240*0Sstevel@tonic-gate 		case S_IFDIR: s = "S_IFDIR"; break;
241*0Sstevel@tonic-gate 		case S_IFREG: s = "S_IFREG"; break;
242*0Sstevel@tonic-gate 		case S_IFLNK: s = "S_IFLNK"; break;
243*0Sstevel@tonic-gate 		case S_IFSOCK: s = "S_IFSOCK"; break;
244*0Sstevel@tonic-gate 		case S_IFDOOR: s = "S_IFDOOR"; break;
245*0Sstevel@tonic-gate 		case S_IFPORT: s = "S_IFPORT"; break;
246*0Sstevel@tonic-gate 		default:
247*0Sstevel@tonic-gate 			s = unknown;
248*0Sstevel@tonic-gate 			(void) sprintf(s, "0x%.4x ",
249*0Sstevel@tonic-gate 				(int)statb.st_mode & S_IFMT);
250*0Sstevel@tonic-gate 			break;
251*0Sstevel@tonic-gate 		}
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 		(void) printf("%4d: %s mode:0%.3o",
254*0Sstevel@tonic-gate 			fd,
255*0Sstevel@tonic-gate 			s,
256*0Sstevel@tonic-gate 			(int)statb.st_mode & ~S_IFMT);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 		if (major(statb.st_dev) != (major_t)NODEV &&
259*0Sstevel@tonic-gate 		    minor(statb.st_dev) != (minor_t)NODEV)
260*0Sstevel@tonic-gate 			(void) printf(" dev:%lu,%lu",
261*0Sstevel@tonic-gate 				(ulong_t)major(statb.st_dev),
262*0Sstevel@tonic-gate 				(ulong_t)minor(statb.st_dev));
263*0Sstevel@tonic-gate 		else
264*0Sstevel@tonic-gate 			(void) printf(" dev:0x%.8lX", (long)statb.st_dev);
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 		if ((statb.st_mode & S_IFMT) == S_IFPORT) {
267*0Sstevel@tonic-gate 			(void) printf(" uid:%d gid:%d",
268*0Sstevel@tonic-gate 			    (int)statb.st_uid,
269*0Sstevel@tonic-gate 			    (int)statb.st_gid);
270*0Sstevel@tonic-gate 			(void) printf(" size:%lld\n",
271*0Sstevel@tonic-gate 			    (longlong_t)statb.st_size);
272*0Sstevel@tonic-gate 			continue;
273*0Sstevel@tonic-gate 		}
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 		(void) printf(" ino:%llu uid:%d gid:%d",
276*0Sstevel@tonic-gate 			(u_longlong_t)statb.st_ino,
277*0Sstevel@tonic-gate 			(int)statb.st_uid,
278*0Sstevel@tonic-gate 			(int)statb.st_gid);
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 		if (rdev == NODEV)
281*0Sstevel@tonic-gate 			(void) printf(" size:%lld\n",
282*0Sstevel@tonic-gate 				(longlong_t)statb.st_size);
283*0Sstevel@tonic-gate 		else if (major(rdev) != (major_t)NODEV &&
284*0Sstevel@tonic-gate 		    minor(rdev) != (minor_t)NODEV)
285*0Sstevel@tonic-gate 			(void) printf(" rdev:%lu,%lu\n",
286*0Sstevel@tonic-gate 				(ulong_t)major(rdev),
287*0Sstevel@tonic-gate 				(ulong_t)minor(rdev));
288*0Sstevel@tonic-gate 		else
289*0Sstevel@tonic-gate 			(void) printf(" rdev:0x%.8lX\n", (long)rdev);
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 		if (!nflag) {
292*0Sstevel@tonic-gate 			dofcntl(Pr, fd,
293*0Sstevel@tonic-gate 				(statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP))
294*0Sstevel@tonic-gate 				== (S_IFREG|S_ENFMT),
295*0Sstevel@tonic-gate 				(statb.st_mode & S_IFMT) == S_IFDOOR);
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 			if ((statb.st_mode & S_IFMT) == S_IFSOCK)
298*0Sstevel@tonic-gate 				dosocket(Pr, fd);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 			(void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd);
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 			if ((ret = readlink(pname, fname, PATH_MAX - 1)) > 0) {
303*0Sstevel@tonic-gate 				fname[ret] = '\0';
304*0Sstevel@tonic-gate 				(void) printf("      %s\n", fname);
305*0Sstevel@tonic-gate 			}
306*0Sstevel@tonic-gate 		}
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	(void) closedir(dirp);
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate /* examine open file with fcntl() */
313*0Sstevel@tonic-gate static void
314*0Sstevel@tonic-gate dofcntl(struct ps_prochandle *Pr, int fd, int manditory, int isdoor)
315*0Sstevel@tonic-gate {
316*0Sstevel@tonic-gate 	struct flock flock;
317*0Sstevel@tonic-gate 	int fileflags;
318*0Sstevel@tonic-gate 	int fdflags;
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0);
321*0Sstevel@tonic-gate 	fdflags = pr_fcntl(Pr, fd, F_GETFD, 0);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	if (fileflags != -1 || fdflags != -1) {
324*0Sstevel@tonic-gate 		(void) printf("      ");
325*0Sstevel@tonic-gate 		if (fileflags != -1)
326*0Sstevel@tonic-gate 			show_fileflags(fileflags);
327*0Sstevel@tonic-gate 		if (fdflags != -1 && (fdflags & FD_CLOEXEC))
328*0Sstevel@tonic-gate 			(void) printf(" FD_CLOEXEC");
329*0Sstevel@tonic-gate 		if (isdoor)
330*0Sstevel@tonic-gate 			show_door(Pr, fd);
331*0Sstevel@tonic-gate 		(void) fputc('\n', stdout);
332*0Sstevel@tonic-gate 	} else if (isdoor) {
333*0Sstevel@tonic-gate 		(void) printf("    ");
334*0Sstevel@tonic-gate 		show_door(Pr, fd);
335*0Sstevel@tonic-gate 		(void) fputc('\n', stdout);
336*0Sstevel@tonic-gate 	}
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
339*0Sstevel@tonic-gate 	flock.l_whence = 0;
340*0Sstevel@tonic-gate 	flock.l_start = 0;
341*0Sstevel@tonic-gate 	flock.l_len = 0;
342*0Sstevel@tonic-gate 	flock.l_sysid = 0;
343*0Sstevel@tonic-gate 	flock.l_pid = 0;
344*0Sstevel@tonic-gate 	if (pr_fcntl(Pr, fd, F_GETLK, &flock) != -1) {
345*0Sstevel@tonic-gate 		if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) {
346*0Sstevel@tonic-gate 			unsigned long sysid = flock.l_sysid;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 			(void) printf("      %s %s lock set by",
349*0Sstevel@tonic-gate 				manditory? "manditory" : "advisory",
350*0Sstevel@tonic-gate 				flock.l_type == F_RDLCK? "read" : "write");
351*0Sstevel@tonic-gate 			if (sysid)
352*0Sstevel@tonic-gate 				(void) printf(" system 0x%lX", sysid);
353*0Sstevel@tonic-gate 			if (flock.l_pid)
354*0Sstevel@tonic-gate 				(void) printf(" process %d", (int)flock.l_pid);
355*0Sstevel@tonic-gate 			(void) fputc('\n', stdout);
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate }
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate #ifdef O_PRIV
361*0Sstevel@tonic-gate #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
362*0Sstevel@tonic-gate 			O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
363*0Sstevel@tonic-gate 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
364*0Sstevel@tonic-gate #else
365*0Sstevel@tonic-gate #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
366*0Sstevel@tonic-gate 			O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
367*0Sstevel@tonic-gate 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
368*0Sstevel@tonic-gate #endif
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate static void
371*0Sstevel@tonic-gate show_fileflags(int flags)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	char buffer[136];
374*0Sstevel@tonic-gate 	char *str = buffer;
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	switch (flags & O_ACCMODE) {
377*0Sstevel@tonic-gate 	case O_RDONLY:
378*0Sstevel@tonic-gate 		(void) strcpy(str, "O_RDONLY");
379*0Sstevel@tonic-gate 		break;
380*0Sstevel@tonic-gate 	case O_WRONLY:
381*0Sstevel@tonic-gate 		(void) strcpy(str, "O_WRONLY");
382*0Sstevel@tonic-gate 		break;
383*0Sstevel@tonic-gate 	case O_RDWR:
384*0Sstevel@tonic-gate 		(void) strcpy(str, "O_RDWR");
385*0Sstevel@tonic-gate 		break;
386*0Sstevel@tonic-gate 	default:
387*0Sstevel@tonic-gate 		(void) sprintf(str, "0x%x", flags & O_ACCMODE);
388*0Sstevel@tonic-gate 		break;
389*0Sstevel@tonic-gate 	}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	if (flags & O_NDELAY)
392*0Sstevel@tonic-gate 		(void) strcat(str, "|O_NDELAY");
393*0Sstevel@tonic-gate 	if (flags & O_NONBLOCK)
394*0Sstevel@tonic-gate 		(void) strcat(str, "|O_NONBLOCK");
395*0Sstevel@tonic-gate 	if (flags & O_APPEND)
396*0Sstevel@tonic-gate 		(void) strcat(str, "|O_APPEND");
397*0Sstevel@tonic-gate #ifdef O_PRIV
398*0Sstevel@tonic-gate 	if (flags & O_PRIV)
399*0Sstevel@tonic-gate 		(void) strcat(str, "|O_PRIV");
400*0Sstevel@tonic-gate #endif
401*0Sstevel@tonic-gate 	if (flags & O_SYNC)
402*0Sstevel@tonic-gate 		(void) strcat(str, "|O_SYNC");
403*0Sstevel@tonic-gate 	if (flags & O_DSYNC)
404*0Sstevel@tonic-gate 		(void) strcat(str, "|O_DSYNC");
405*0Sstevel@tonic-gate 	if (flags & O_RSYNC)
406*0Sstevel@tonic-gate 		(void) strcat(str, "|O_RSYNC");
407*0Sstevel@tonic-gate 	if (flags & O_CREAT)
408*0Sstevel@tonic-gate 		(void) strcat(str, "|O_CREAT");
409*0Sstevel@tonic-gate 	if (flags & O_TRUNC)
410*0Sstevel@tonic-gate 		(void) strcat(str, "|O_TRUNC");
411*0Sstevel@tonic-gate 	if (flags & O_EXCL)
412*0Sstevel@tonic-gate 		(void) strcat(str, "|O_EXCL");
413*0Sstevel@tonic-gate 	if (flags & O_NOCTTY)
414*0Sstevel@tonic-gate 		(void) strcat(str, "|O_NOCTTY");
415*0Sstevel@tonic-gate 	if (flags & O_LARGEFILE)
416*0Sstevel@tonic-gate 		(void) strcat(str, "|O_LARGEFILE");
417*0Sstevel@tonic-gate 	if (flags & O_XATTR)
418*0Sstevel@tonic-gate 		(void) strcat(str, "|O_XATTR");
419*0Sstevel@tonic-gate 	if (flags & ~(ALL_O_FLAGS))
420*0Sstevel@tonic-gate 		(void) sprintf(str + strlen(str), "|0x%x",
421*0Sstevel@tonic-gate 			flags & ~(ALL_O_FLAGS));
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	(void) printf("%s", str);
424*0Sstevel@tonic-gate }
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate /* show door info */
427*0Sstevel@tonic-gate static void
428*0Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd)
429*0Sstevel@tonic-gate {
430*0Sstevel@tonic-gate 	door_info_t door_info;
431*0Sstevel@tonic-gate 	psinfo_t psinfo;
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	if (pr_door_info(Pr, fd, &door_info) != 0)
434*0Sstevel@tonic-gate 		return;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (proc_get_psinfo(door_info.di_target, &psinfo) != 0)
437*0Sstevel@tonic-gate 		psinfo.pr_fname[0] = '\0';
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	(void) printf("  door to ");
440*0Sstevel@tonic-gate 	if (psinfo.pr_fname[0] != '\0')
441*0Sstevel@tonic-gate 		(void) printf("%s[%d]", psinfo.pr_fname,
442*0Sstevel@tonic-gate 			(int)door_info.di_target);
443*0Sstevel@tonic-gate 	else
444*0Sstevel@tonic-gate 		(void) printf("pid %d", (int)door_info.di_target);
445*0Sstevel@tonic-gate }
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate static void
448*0Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
449*0Sstevel@tonic-gate {
450*0Sstevel@tonic-gate 	/* LINTED pointer assignment */
451*0Sstevel@tonic-gate 	struct sockaddr_in *so_in = (struct sockaddr_in *)sa;
452*0Sstevel@tonic-gate 	/* LINTED pointer assignment */
453*0Sstevel@tonic-gate 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)sa;
454*0Sstevel@tonic-gate 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
455*0Sstevel@tonic-gate 	char  abuf[INET6_ADDRSTRLEN];
456*0Sstevel@tonic-gate 	const char *p;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	switch (sa->sa_family) {
459*0Sstevel@tonic-gate 	default:
460*0Sstevel@tonic-gate 		return;
461*0Sstevel@tonic-gate 	case AF_INET:
462*0Sstevel@tonic-gate 	case AF_NCA:
463*0Sstevel@tonic-gate 		p = (sa->sa_family == AF_INET) ? "AF_INET" : "AF_NCA";
464*0Sstevel@tonic-gate 		(void) printf("\t%s: %s %s  port: %u\n",
465*0Sstevel@tonic-gate 		    str,
466*0Sstevel@tonic-gate 		    p,
467*0Sstevel@tonic-gate 		    inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)),
468*0Sstevel@tonic-gate 		    ntohs(so_in->sin_port));
469*0Sstevel@tonic-gate 		return;
470*0Sstevel@tonic-gate 	case AF_INET6:
471*0Sstevel@tonic-gate 		(void) printf("\t%s: AF_INET6 %s  port: %u\n",
472*0Sstevel@tonic-gate 		    str,
473*0Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &so_in6->sin6_addr,
474*0Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
475*0Sstevel@tonic-gate 		    ntohs(so_in->sin_port));
476*0Sstevel@tonic-gate 		return;
477*0Sstevel@tonic-gate 	case AF_UNIX:
478*0Sstevel@tonic-gate 		if (len >= sizeof (so_un->sun_family)) {
479*0Sstevel@tonic-gate 			/* Null terminate */
480*0Sstevel@tonic-gate 			len -= sizeof (so_un->sun_family);
481*0Sstevel@tonic-gate 			so_un->sun_path[len] = NULL;
482*0Sstevel@tonic-gate 			(void) printf("\t%s: AF_UNIX %s\n",
483*0Sstevel@tonic-gate 				str, so_un->sun_path);
484*0Sstevel@tonic-gate 		}
485*0Sstevel@tonic-gate 		return;
486*0Sstevel@tonic-gate 	case AF_IMPLINK:	p = "AF_IMPLINK";	break;
487*0Sstevel@tonic-gate 	case AF_PUP:		p = "AF_PUP";		break;
488*0Sstevel@tonic-gate 	case AF_CHAOS:		p = "AF_CHAOS";		break;
489*0Sstevel@tonic-gate 	case AF_NS:		p = "AF_NS";		break;
490*0Sstevel@tonic-gate 	case AF_NBS:		p = "AF_NBS";		break;
491*0Sstevel@tonic-gate 	case AF_ECMA:		p = "AF_ECMA";		break;
492*0Sstevel@tonic-gate 	case AF_DATAKIT:	p = "AF_DATAKIT";	break;
493*0Sstevel@tonic-gate 	case AF_CCITT:		p = "AF_CCITT";		break;
494*0Sstevel@tonic-gate 	case AF_SNA:		p = "AF_SNA";		break;
495*0Sstevel@tonic-gate 	case AF_DECnet:		p = "AF_DECnet";	break;
496*0Sstevel@tonic-gate 	case AF_DLI:		p = "AF_DLI";		break;
497*0Sstevel@tonic-gate 	case AF_LAT:		p = "AF_LAT";		break;
498*0Sstevel@tonic-gate 	case AF_HYLINK:		p = "AF_HYLINK";	break;
499*0Sstevel@tonic-gate 	case AF_APPLETALK:	p = "AF_APPLETALK";	break;
500*0Sstevel@tonic-gate 	case AF_NIT:		p = "AF_NIT";		break;
501*0Sstevel@tonic-gate 	case AF_802:		p = "AF_802";		break;
502*0Sstevel@tonic-gate 	case AF_OSI:		p = "AF_OSI";		break;
503*0Sstevel@tonic-gate 	case AF_X25:		p = "AF_X25";		break;
504*0Sstevel@tonic-gate 	case AF_OSINET:		p = "AF_OSINET";	break;
505*0Sstevel@tonic-gate 	case AF_GOSIP:		p = "AF_GOSIP";		break;
506*0Sstevel@tonic-gate 	case AF_IPX:		p = "AF_IPX";		break;
507*0Sstevel@tonic-gate 	case AF_ROUTE:		p = "AF_ROUTE";		break;
508*0Sstevel@tonic-gate 	case AF_LINK:		p = "AF_LINK";		break;
509*0Sstevel@tonic-gate 	}
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	(void) printf("\t%s: %s\n", str, p);
512*0Sstevel@tonic-gate }
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate static void
515*0Sstevel@tonic-gate show_socktype(uint_t type)
516*0Sstevel@tonic-gate {
517*0Sstevel@tonic-gate 	static const char *types[] = {
518*0Sstevel@tonic-gate 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
519*0Sstevel@tonic-gate 	};
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
522*0Sstevel@tonic-gate 		(void) printf("\tSOCK_%s\n", types[type]);
523*0Sstevel@tonic-gate 	else
524*0Sstevel@tonic-gate 		(void) printf("\tunknown socket type %u\n", type);
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate static void
528*0Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd)
529*0Sstevel@tonic-gate {
530*0Sstevel@tonic-gate 	int val, vlen;
531*0Sstevel@tonic-gate 	char buf[64];
532*0Sstevel@tonic-gate 	char buf1[32];
533*0Sstevel@tonic-gate 	int i;
534*0Sstevel@tonic-gate 	struct boolopt {
535*0Sstevel@tonic-gate 		int		opt;
536*0Sstevel@tonic-gate 		const char	*name;
537*0Sstevel@tonic-gate 	};
538*0Sstevel@tonic-gate 	static struct boolopt boolopts[] = {
539*0Sstevel@tonic-gate 	    { SO_DEBUG,		"SO_DEBUG,"	},
540*0Sstevel@tonic-gate 	    { SO_REUSEADDR,	"SO_REUSEADDR,"	},
541*0Sstevel@tonic-gate 	    { SO_KEEPALIVE,	"SO_KEEPALIVE,"	},
542*0Sstevel@tonic-gate 	    { SO_DONTROUTE,	"SO_DONTROUTE,"	},
543*0Sstevel@tonic-gate 	    { SO_BROADCAST,	"SO_BROADCAST,"	},
544*0Sstevel@tonic-gate 	    { SO_OOBINLINE,	"SO_OOBINLINE,"	},
545*0Sstevel@tonic-gate 	    { SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"}
546*0Sstevel@tonic-gate 	};
547*0Sstevel@tonic-gate 	struct linger l;
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	buf[0] = ',';
550*0Sstevel@tonic-gate 	buf[1] = '\0';
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) {
553*0Sstevel@tonic-gate 		vlen = sizeof (val);
554*0Sstevel@tonic-gate 		if (pr_getsockopt(Pr, fd, SOL_SOCKET, boolopts[i].opt, &val,
555*0Sstevel@tonic-gate 		    &vlen) == 0 && val != 0)
556*0Sstevel@tonic-gate 			(void) strlcat(buf, boolopts[i].name, sizeof (buf));
557*0Sstevel@tonic-gate 	}
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	vlen = sizeof (l);
560*0Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 &&
561*0Sstevel@tonic-gate 	    l.l_onoff != 0) {
562*0Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),",
563*0Sstevel@tonic-gate 		    l.l_linger);
564*0Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	vlen = sizeof (val);
568*0Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) {
569*0Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val);
570*0Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
571*0Sstevel@tonic-gate 	}
572*0Sstevel@tonic-gate 	vlen = sizeof (val);
573*0Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) {
574*0Sstevel@tonic-gate 		(void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val);
575*0Sstevel@tonic-gate 		(void) strlcat(buf, buf1, sizeof (buf));
576*0Sstevel@tonic-gate 	}
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	buf[strlen(buf) - 1] = '\0';
579*0Sstevel@tonic-gate 	if (buf[1] != '\0')
580*0Sstevel@tonic-gate 		(void) printf("\t%s\n", buf+1);
581*0Sstevel@tonic-gate }
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate /* the file is a socket */
584*0Sstevel@tonic-gate static void
585*0Sstevel@tonic-gate dosocket(struct ps_prochandle *Pr, int fd)
586*0Sstevel@tonic-gate {
587*0Sstevel@tonic-gate 	/* A buffer large enough for PATH_MAX size AF_UNIX address */
588*0Sstevel@tonic-gate 	long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1)
589*0Sstevel@tonic-gate 		/ sizeof (long)];
590*0Sstevel@tonic-gate 	struct sockaddr *sa = (struct sockaddr *)buf;
591*0Sstevel@tonic-gate 	socklen_t len;
592*0Sstevel@tonic-gate 	int type, tlen;
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	tlen = sizeof (type);
595*0Sstevel@tonic-gate 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen)
596*0Sstevel@tonic-gate 	    == 0)
597*0Sstevel@tonic-gate 		show_socktype((uint_t)type);
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	show_sockopts(Pr, fd);
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	len = sizeof (buf);
602*0Sstevel@tonic-gate 	if (pr_getsockname(Pr, fd, sa, &len) == 0)
603*0Sstevel@tonic-gate 		show_sockaddr("sockname", sa, len);
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	len = sizeof (buf);
606*0Sstevel@tonic-gate 	if (pr_getpeername(Pr, fd, sa, &len) == 0)
607*0Sstevel@tonic-gate 		show_sockaddr("peername", sa, len);
608*0Sstevel@tonic-gate }
609