xref: /titanic_51/usr/src/cmd/ptools/pfiles/pfiles.c (revision b3697b90e692e3e5d859fb77d285d4c056d99eda)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <dirent.h>
34 #include <limits.h>
35 #include <door.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/mkdev.h>
40 #include <sys/stropts.h>
41 #include <sys/timod.h>
42 #include <sys/un.h>
43 #include <libproc.h>
44 #include <netinet/in.h>
45 #include <netinet/udp.h>
46 #include <arpa/inet.h>
47 #include <ucred.h>
48 #include <zone.h>
49 
50 #define	copyflock(dst, src) \
51 	(dst).l_type = (src).l_type;		\
52 	(dst).l_whence = (src).l_whence;	\
53 	(dst).l_start = (src).l_start;		\
54 	(dst).l_len = (src).l_len;		\
55 	(dst).l_sysid = (src).l_sysid;		\
56 	(dst).l_pid = (src).l_pid;
57 
58 static char *command;
59 static volatile int interrupt;
60 static int Fflag;
61 static boolean_t nflag = B_FALSE;
62 
63 static	void	intr(int);
64 static	void	dofcntl(struct ps_prochandle *, int, int, int);
65 static	void	dosocket(struct ps_prochandle *, int);
66 static	void	dofifo(struct ps_prochandle *, int);
67 static	void	dotli(struct ps_prochandle *, int);
68 static	void	show_files(struct ps_prochandle *);
69 static	void	show_fileflags(int);
70 static	void	show_door(struct ps_prochandle *, int);
71 static	int	getflock(struct ps_prochandle *, int, struct flock *);
72 
73 int
74 main(int argc, char **argv)
75 {
76 	int retc = 0;
77 	int opt;
78 	int errflg = 0;
79 	struct ps_prochandle *Pr;
80 
81 	if ((command = strrchr(argv[0], '/')) != NULL)
82 		command++;
83 	else
84 		command = argv[0];
85 
86 	/* options */
87 	while ((opt = getopt(argc, argv, "Fn")) != EOF) {
88 		switch (opt) {
89 		case 'F':		/* force grabbing (no O_EXCL) */
90 			Fflag = PGRAB_FORCE;
91 			break;
92 		case 'n':
93 			nflag = B_TRUE;
94 			break;
95 		default:
96 			errflg = 1;
97 			break;
98 		}
99 	}
100 
101 	argc -= optind;
102 	argv += optind;
103 
104 	if (errflg || argc <= 0) {
105 		(void) fprintf(stderr, "usage:\t%s [-F] pid ...\n",
106 		    command);
107 		(void) fprintf(stderr,
108 		    "  (report open files of each process)\n");
109 		(void) fprintf(stderr,
110 		    "  -F: force grabbing of the target process\n");
111 		exit(2);
112 	}
113 
114 	/* catch signals from terminal */
115 	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
116 		(void) sigset(SIGHUP, intr);
117 	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
118 		(void) sigset(SIGINT, intr);
119 	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
120 		(void) sigset(SIGQUIT, intr);
121 	(void) sigset(SIGPIPE, intr);
122 	(void) sigset(SIGTERM, intr);
123 
124 	(void) proc_initstdio();
125 
126 
127 	while (--argc >= 0 && !interrupt) {
128 		char *arg;
129 		psinfo_t psinfo;
130 		pid_t pid;
131 		int gret;
132 
133 		(void) proc_flushstdio();
134 
135 		/* get the specified pid and the psinfo struct */
136 		if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS,
137 		    &psinfo, &gret)) == -1) {
138 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
139 			    command, arg, Pgrab_error(gret));
140 			retc++;
141 		} else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) {
142 			if (Pcreate_agent(Pr) == 0) {
143 				proc_unctrl_psinfo(&psinfo);
144 				(void) printf("%d:\t%.70s\n",
145 				    (int)pid, psinfo.pr_psargs);
146 				show_files(Pr);
147 				Pdestroy_agent(Pr);
148 			} else {
149 				(void) fprintf(stderr,
150 				    "%s: cannot control process %d\n",
151 				    command, (int)pid);
152 				retc++;
153 			}
154 			Prelease(Pr, 0);
155 			Pr = NULL;
156 		} else {
157 			switch (gret) {
158 			case G_SYS:
159 			case G_SELF:
160 				proc_unctrl_psinfo(&psinfo);
161 				(void) printf("%d:\t%.70s\n", (int)pid,
162 				    psinfo.pr_psargs);
163 				if (gret == G_SYS)
164 					(void) printf("  [system process]\n");
165 				else
166 					show_files(NULL);
167 				break;
168 			default:
169 				(void) fprintf(stderr, "%s: %s: %d\n",
170 				    command, Pgrab_error(gret), (int)pid);
171 				retc++;
172 				break;
173 			}
174 		}
175 	}
176 
177 	(void) proc_finistdio();
178 
179 	if (interrupt && retc == 0)
180 		retc++;
181 	return (retc);
182 }
183 
184 /* ARGSUSED */
185 static void
186 intr(int sig)
187 {
188 	interrupt = 1;
189 }
190 
191 /* ------ begin specific code ------ */
192 
193 static void
194 show_files(struct ps_prochandle *Pr)
195 {
196 	DIR *dirp;
197 	struct dirent *dentp;
198 	const char *dev;
199 	char pname[100];
200 	char fname[PATH_MAX];
201 	struct stat64 statb;
202 	struct rlimit rlim;
203 	pid_t pid;
204 	int fd;
205 	char *s;
206 	int ret;
207 
208 	if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) {
209 		ulong_t nfd = rlim.rlim_cur;
210 		if (nfd == RLIM_INFINITY)
211 			(void) printf(
212 			    "  Current rlimit: unlimited file descriptors\n");
213 		else
214 			(void) printf(
215 			    "  Current rlimit: %lu file descriptors\n", nfd);
216 	}
217 
218 	/* in case we are doing this to ourself */
219 	pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid;
220 
221 	(void) sprintf(pname, "/proc/%d/fd", (int)pid);
222 	if ((dirp = opendir(pname)) == NULL) {
223 		(void) fprintf(stderr, "%s: cannot open directory %s\n",
224 		    command, pname);
225 		return;
226 	}
227 
228 	/* for each open file --- */
229 	while ((dentp = readdir(dirp)) != NULL && !interrupt) {
230 		char unknown[12];
231 		dev_t rdev;
232 
233 		/* skip '.' and '..' */
234 		if (!isdigit(dentp->d_name[0]))
235 			continue;
236 
237 		fd = atoi(dentp->d_name);
238 		if (pr_fstat64(Pr, fd, &statb) == -1) {
239 			s = unknown;
240 			(void) sprintf(s, "%4d", fd);
241 			perror(s);
242 			continue;
243 		}
244 
245 		rdev = NODEV;
246 		switch (statb.st_mode & S_IFMT) {
247 		case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break;
248 		case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break;
249 		case S_IFIFO: s = "S_IFIFO"; break;
250 		case S_IFDIR: s = "S_IFDIR"; break;
251 		case S_IFREG: s = "S_IFREG"; break;
252 		case S_IFLNK: s = "S_IFLNK"; break;
253 		case S_IFSOCK: s = "S_IFSOCK"; break;
254 		case S_IFDOOR: s = "S_IFDOOR"; break;
255 		case S_IFPORT: s = "S_IFPORT"; break;
256 		default:
257 			s = unknown;
258 			(void) sprintf(s, "0x%.4x ",
259 			    (int)statb.st_mode & S_IFMT);
260 			break;
261 		}
262 
263 		(void) printf("%4d: %s mode:0%.3o", fd, s,
264 		    (int)statb.st_mode & ~S_IFMT);
265 
266 		if (major(statb.st_dev) != (major_t)NODEV &&
267 		    minor(statb.st_dev) != (minor_t)NODEV)
268 			(void) printf(" dev:%lu,%lu",
269 			    (ulong_t)major(statb.st_dev),
270 			    (ulong_t)minor(statb.st_dev));
271 		else
272 			(void) printf(" dev:0x%.8lX", (long)statb.st_dev);
273 
274 		if ((statb.st_mode & S_IFMT) == S_IFPORT) {
275 			(void) printf(" uid:%d gid:%d",
276 			    (int)statb.st_uid,
277 			    (int)statb.st_gid);
278 			(void) printf(" size:%lld\n",
279 			    (longlong_t)statb.st_size);
280 			continue;
281 		}
282 
283 		(void) printf(" ino:%llu uid:%d gid:%d",
284 		    (u_longlong_t)statb.st_ino,
285 		    (int)statb.st_uid, (int)statb.st_gid);
286 
287 		if (rdev == NODEV)
288 			(void) printf(" size:%lld\n",
289 			    (longlong_t)statb.st_size);
290 		else if (major(rdev) != (major_t)NODEV &&
291 		    minor(rdev) != (minor_t)NODEV)
292 			(void) printf(" rdev:%lu,%lu\n",
293 			    (ulong_t)major(rdev), (ulong_t)minor(rdev));
294 		else
295 			(void) printf(" rdev:0x%.8lX\n", (long)rdev);
296 
297 		if (!nflag) {
298 			off_t offset;
299 
300 			dofcntl(Pr, fd,
301 			    (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP))
302 			    == (S_IFREG|S_ENFMT),
303 			    (statb.st_mode & S_IFMT) == S_IFDOOR);
304 
305 			if ((statb.st_mode & S_IFMT) == S_IFSOCK)
306 				dosocket(Pr, fd);
307 			else if ((statb.st_mode & S_IFMT) == S_IFIFO)
308 				dofifo(Pr, fd);
309 
310 			(void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd);
311 
312 			if ((ret = readlink(pname, fname, PATH_MAX - 1)) <= 0)
313 				continue;
314 
315 			fname[ret] = '\0';
316 
317 			if ((statb.st_mode & S_IFMT) == S_IFCHR &&
318 			    (dev = strrchr(fname, ':')) != NULL) {
319 				/*
320 				 * There's no elegant way to determine if a
321 				 * character device supports TLI, so we lame
322 				 * out and just check a hardcoded list of
323 				 * known TLI devices.
324 				 */
325 				int i;
326 				const char *tlidevs[] =
327 				    { "tcp", "tcp6", "udp", "udp6", NULL };
328 
329 				dev++; /* skip past the `:' */
330 				for (i = 0; tlidevs[i] != NULL; i++) {
331 					if (strcmp(dev, tlidevs[i]) == 0) {
332 						dotli(Pr, fd);
333 						break;
334 					}
335 				}
336 			}
337 			(void) printf("      %s\n", fname);
338 
339 			offset = pr_lseek(Pr, fd, 0, SEEK_CUR);
340 			if (offset != -1) {
341 				(void) printf("      offset:%ld\n", offset);
342 			}
343 
344 		}
345 	}
346 	(void) closedir(dirp);
347 }
348 
349 
350 static int
351 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native)
352 {
353 	int ret;
354 #ifdef _LP64
355 	struct flock64_32 flock_target;
356 
357 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) {
358 		copyflock(flock_target, *flock_native);
359 		ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target);
360 		copyflock(*flock_native, flock_target);
361 		return (ret);
362 	}
363 #endif /* _LP64 */
364 	ret = pr_fcntl(Pr, fd, F_GETLK, flock_native);
365 	return (ret);
366 }
367 
368 /* examine open file with fcntl() */
369 static void
370 dofcntl(struct ps_prochandle *Pr, int fd, int mandatory, int isdoor)
371 {
372 	struct flock flock;
373 	int fileflags;
374 	int fdflags;
375 
376 	fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0);
377 	fdflags = pr_fcntl(Pr, fd, F_GETFD, 0);
378 
379 	if (fileflags != -1 || fdflags != -1) {
380 		(void) printf("      ");
381 		if (fileflags != -1)
382 			show_fileflags(fileflags);
383 		if (fdflags != -1 && (fdflags & FD_CLOEXEC))
384 			(void) printf(" FD_CLOEXEC");
385 		if (isdoor)
386 			show_door(Pr, fd);
387 		(void) fputc('\n', stdout);
388 	} else if (isdoor) {
389 		(void) printf("    ");
390 		show_door(Pr, fd);
391 		(void) fputc('\n', stdout);
392 	}
393 
394 	flock.l_type = F_WRLCK;
395 	flock.l_whence = 0;
396 	flock.l_start = 0;
397 	flock.l_len = 0;
398 	flock.l_sysid = 0;
399 	flock.l_pid = 0;
400 	if (getflock(Pr, fd, &flock) != -1) {
401 		if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) {
402 			unsigned long sysid = flock.l_sysid;
403 
404 			(void) printf("      %s %s lock set by",
405 			    mandatory ? "mandatory" : "advisory",
406 			    flock.l_type == F_RDLCK? "read" : "write");
407 			if (sysid)
408 				(void) printf(" system 0x%lX", sysid);
409 			if (flock.l_pid)
410 				(void) printf(" process %d", (int)flock.l_pid);
411 			(void) fputc('\n', stdout);
412 		}
413 	}
414 }
415 
416 #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
417 			O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
418 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
419 
420 static void
421 show_fileflags(int flags)
422 {
423 	char buffer[136];
424 	char *str = buffer;
425 
426 	switch (flags & O_ACCMODE) {
427 	case O_RDONLY:
428 		(void) strcpy(str, "O_RDONLY");
429 		break;
430 	case O_WRONLY:
431 		(void) strcpy(str, "O_WRONLY");
432 		break;
433 	case O_RDWR:
434 		(void) strcpy(str, "O_RDWR");
435 		break;
436 	case O_SEARCH:
437 		(void) strcpy(str, "O_SEARCH");
438 		break;
439 	case O_EXEC:
440 		(void) strcpy(str, "O_EXEC");
441 		break;
442 	default:
443 		(void) sprintf(str, "0x%x", flags & O_ACCMODE);
444 		break;
445 	}
446 
447 	if (flags & O_NDELAY)
448 		(void) strcat(str, "|O_NDELAY");
449 	if (flags & O_NONBLOCK)
450 		(void) strcat(str, "|O_NONBLOCK");
451 	if (flags & O_APPEND)
452 		(void) strcat(str, "|O_APPEND");
453 	if (flags & O_SYNC)
454 		(void) strcat(str, "|O_SYNC");
455 	if (flags & O_DSYNC)
456 		(void) strcat(str, "|O_DSYNC");
457 	if (flags & O_RSYNC)
458 		(void) strcat(str, "|O_RSYNC");
459 	if (flags & O_CREAT)
460 		(void) strcat(str, "|O_CREAT");
461 	if (flags & O_TRUNC)
462 		(void) strcat(str, "|O_TRUNC");
463 	if (flags & O_EXCL)
464 		(void) strcat(str, "|O_EXCL");
465 	if (flags & O_NOCTTY)
466 		(void) strcat(str, "|O_NOCTTY");
467 	if (flags & O_LARGEFILE)
468 		(void) strcat(str, "|O_LARGEFILE");
469 	if (flags & O_XATTR)
470 		(void) strcat(str, "|O_XATTR");
471 	if (flags & ~(ALL_O_FLAGS))
472 		(void) sprintf(str + strlen(str), "|0x%x",
473 		    flags & ~(ALL_O_FLAGS));
474 
475 	(void) printf("%s", str);
476 }
477 
478 /* show process on the other end of a door, socket or fifo */
479 static void
480 show_peer_process(pid_t ppid)
481 {
482 	psinfo_t psinfo;
483 
484 	if (proc_get_psinfo(ppid, &psinfo) == 0)
485 		(void) printf(" %s[%d]", psinfo.pr_fname, (int)ppid);
486 	else
487 		(void) printf(" pid %d", (int)ppid);
488 }
489 
490 /* show door info */
491 static void
492 show_door(struct ps_prochandle *Pr, int fd)
493 {
494 	door_info_t door_info;
495 
496 	if (pr_door_info(Pr, fd, &door_info) != 0)
497 		return;
498 
499 	(void) printf("  door to");
500 	show_peer_process(door_info.di_target);
501 }
502 
503 /*
504  * Print out the socket address pointed to by `sa'.  `len' is only
505  * needed for AF_UNIX sockets.
506  */
507 static void
508 show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
509 {
510 	struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa;
511 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa;
512 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
513 	char  abuf[INET6_ADDRSTRLEN];
514 	const char *p;
515 
516 	switch (sa->sa_family) {
517 	default:
518 		return;
519 	case AF_INET:
520 		(void) printf("\t%s: AF_INET %s  port: %u\n", str,
521 		    inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)),
522 		    ntohs(so_in->sin_port));
523 		return;
524 	case AF_INET6:
525 		(void) printf("\t%s: AF_INET6 %s  port: %u\n", str,
526 		    inet_ntop(AF_INET6, &so_in6->sin6_addr,
527 		    abuf, sizeof (abuf)),
528 		    ntohs(so_in->sin_port));
529 		return;
530 	case AF_UNIX:
531 		if (len >= sizeof (so_un->sun_family)) {
532 			/* Null terminate */
533 			len -= sizeof (so_un->sun_family);
534 			so_un->sun_path[len] = '\0';
535 			(void) printf("\t%s: AF_UNIX %s\n",
536 			    str, so_un->sun_path);
537 		}
538 		return;
539 	case AF_IMPLINK:	p = "AF_IMPLINK";	break;
540 	case AF_PUP:		p = "AF_PUP";		break;
541 	case AF_CHAOS:		p = "AF_CHAOS";		break;
542 	case AF_NS:		p = "AF_NS";		break;
543 	case AF_NBS:		p = "AF_NBS";		break;
544 	case AF_ECMA:		p = "AF_ECMA";		break;
545 	case AF_DATAKIT:	p = "AF_DATAKIT";	break;
546 	case AF_CCITT:		p = "AF_CCITT";		break;
547 	case AF_SNA:		p = "AF_SNA";		break;
548 	case AF_DECnet:		p = "AF_DECnet";	break;
549 	case AF_DLI:		p = "AF_DLI";		break;
550 	case AF_LAT:		p = "AF_LAT";		break;
551 	case AF_HYLINK:		p = "AF_HYLINK";	break;
552 	case AF_APPLETALK:	p = "AF_APPLETALK";	break;
553 	case AF_NIT:		p = "AF_NIT";		break;
554 	case AF_802:		p = "AF_802";		break;
555 	case AF_OSI:		p = "AF_OSI";		break;
556 	case AF_X25:		p = "AF_X25";		break;
557 	case AF_OSINET:		p = "AF_OSINET";	break;
558 	case AF_GOSIP:		p = "AF_GOSIP";		break;
559 	case AF_IPX:		p = "AF_IPX";		break;
560 	case AF_ROUTE:		p = "AF_ROUTE";		break;
561 	case AF_LINK:		p = "AF_LINK";		break;
562 	}
563 
564 	(void) printf("\t%s: %s\n", str, p);
565 }
566 
567 /*
568  * Print out the process information for the other end of local sockets
569  * and fifos
570  */
571 static void
572 show_ucred(const char *str, ucred_t *cred)
573 {
574 	pid_t upid = ucred_getpid(cred);
575 	zoneid_t uzid = ucred_getzoneid(cred);
576 	char zonename[ZONENAME_MAX];
577 
578 	if ((upid != -1) || (uzid != -1)) {
579 		(void) printf("\t%s:", str);
580 		if (upid != -1) {
581 			show_peer_process(upid);
582 		}
583 		if (uzid != -1) {
584 			if (getzonenamebyid(uzid, zonename, sizeof (zonename))
585 			    != -1) {
586 				(void) printf(" zone: %s[%d]", zonename,
587 				    (int)uzid);
588 			} else {
589 				(void) printf(" zoneid: %d", (int)uzid);
590 			}
591 		}
592 		(void) printf("\n");
593 	}
594 }
595 
596 static void
597 show_socktype(uint_t type)
598 {
599 	static const char *types[] = {
600 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
601 	};
602 
603 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
604 		(void) printf("\tSOCK_%s\n", types[type]);
605 	else
606 		(void) printf("\tunknown socket type %u\n", type);
607 }
608 
609 #define	BUFSIZE	200
610 static void
611 show_sockopts(struct ps_prochandle *Pr, int fd)
612 {
613 	int val, vlen;
614 	char buf[BUFSIZE];
615 	char buf1[32];
616 	char ipaddr[INET_ADDRSTRLEN];
617 	int i;
618 	in_addr_t nexthop_val;
619 	struct boolopt {
620 		int		level;
621 		int		opt;
622 		const char	*name;
623 	};
624 	static struct boolopt boolopts[] = {
625 	    { SOL_SOCKET, SO_DEBUG,		"SO_DEBUG,"	},
626 	    { SOL_SOCKET, SO_REUSEADDR,		"SO_REUSEADDR,"	},
627 	    { SOL_SOCKET, SO_KEEPALIVE,		"SO_KEEPALIVE,"	},
628 	    { SOL_SOCKET, SO_DONTROUTE,		"SO_DONTROUTE,"	},
629 	    { SOL_SOCKET, SO_BROADCAST,		"SO_BROADCAST,"	},
630 	    { SOL_SOCKET, SO_OOBINLINE,		"SO_OOBINLINE,"	},
631 	    { SOL_SOCKET, SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"},
632 	    { SOL_SOCKET, SO_ALLZONES,		"SO_ALLZONES,"	},
633 	    { SOL_SOCKET, SO_MAC_EXEMPT,	"SO_MAC_EXEMPT," },
634 	    { SOL_SOCKET, SO_MAC_IMPLICIT,	"SO_MAC_IMPLICIT," },
635 	    { SOL_SOCKET, SO_EXCLBIND,		"SO_EXCLBIND," },
636 	    { SOL_SOCKET, SO_VRRP,		"SO_VRRP," },
637 	    { IPPROTO_UDP, UDP_NAT_T_ENDPOINT,	"UDP_NAT_T_ENDPOINT," },
638 	};
639 	struct linger l;
640 
641 	buf[0] = '!';		/* sentinel value, never printed */
642 	buf[1] = '\0';
643 
644 	for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) {
645 		vlen = sizeof (val);
646 		if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt,
647 		    &val, &vlen) == 0 && val != 0)
648 			(void) strlcat(buf, boolopts[i].name, sizeof (buf));
649 	}
650 
651 	vlen = sizeof (l);
652 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 &&
653 	    l.l_onoff != 0) {
654 		(void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),",
655 		    l.l_linger);
656 		(void) strlcat(buf, buf1, sizeof (buf));
657 	}
658 
659 	vlen = sizeof (val);
660 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) {
661 		(void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val);
662 		(void) strlcat(buf, buf1, sizeof (buf));
663 	}
664 	vlen = sizeof (val);
665 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) {
666 		(void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val);
667 		(void) strlcat(buf, buf1, sizeof (buf));
668 	}
669 	vlen = sizeof (nexthop_val);
670 	if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val,
671 	    &vlen) == 0) {
672 		if (vlen > 0) {
673 			(void) inet_ntop(AF_INET, (void *) &nexthop_val,
674 			    ipaddr, sizeof (ipaddr));
675 			(void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),",
676 			    ipaddr);
677 			(void) strlcat(buf, buf1, sizeof (buf));
678 		}
679 	}
680 
681 	buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */
682 	if (buf[1] != '\0')
683 		(void) printf("\t%s\n", buf+1);
684 }
685 
686 #define	MAXNALLOC	32
687 static void
688 show_sockfilters(struct ps_prochandle *Pr, int fd)
689 {
690 	struct fil_info *fi;
691 	int i = 0, nalloc = 2, len = nalloc * sizeof (*fi);
692 	boolean_t printhdr = B_TRUE;
693 
694 	fi = calloc(nalloc, sizeof (*fi));
695 	if (fi == NULL) {
696 		perror("calloc");
697 		return;
698 	}
699 	/* CONSTCOND */
700 	while (1) {
701 		if (pr_getsockopt(Pr, fd, SOL_FILTER, FIL_LIST, fi, &len) != 0)
702 			break;
703 		/* No filters */
704 		if (len == 0)
705 			break;
706 		/* Make sure buffer was large enough */
707 		if (fi->fi_pos >= nalloc) {
708 			struct fil_info *new;
709 
710 			nalloc = fi->fi_pos + 1;
711 			if (nalloc > MAXNALLOC)
712 				break;
713 			len = nalloc * sizeof (*fi);
714 			new = realloc(fi, nalloc * sizeof (*fi));
715 			if (new == NULL) {
716 				perror("realloc");
717 				break;
718 			}
719 			fi = new;
720 			continue;
721 		}
722 
723 		for (i = 0; (i + 1) * sizeof (*fi) <= len; i++) {
724 			if (fi[i].fi_flags & FILF_BYPASS)
725 				continue;
726 			if (printhdr) {
727 				(void) printf("\tfilters: ");
728 				printhdr = B_FALSE;
729 			}
730 			(void) printf("%s", fi[i].fi_name);
731 			if (fi[i].fi_flags != 0) {
732 				(void) printf("(");
733 				if (fi[i].fi_flags & FILF_AUTO)
734 					(void) printf("auto,");
735 				if (fi[i].fi_flags & FILF_PROG)
736 					(void) printf("prog,");
737 				(void) printf("\b)");
738 			}
739 			if (fi[i].fi_pos == 0) /* last one */
740 				break;
741 			(void) printf(",");
742 		}
743 		if (!printhdr)
744 			(void) printf("\n");
745 		break;
746 	}
747 	free(fi);
748 }
749 
750 /* print peer credentials for sockets and named pipes */
751 static void
752 dopeerucred(struct ps_prochandle *Pr, int fd)
753 {
754 	ucred_t *peercred = NULL;	/* allocated by getpeerucred */
755 
756 	if (pr_getpeerucred(Pr, fd, &peercred) == 0) {
757 		show_ucred("peer", peercred);
758 		ucred_free(peercred);
759 	}
760 }
761 
762 /* the file is a socket */
763 static void
764 dosocket(struct ps_prochandle *Pr, int fd)
765 {
766 	/* A buffer large enough for PATH_MAX size AF_UNIX address */
767 	long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1)
768 	    / sizeof (long)];
769 	struct sockaddr *sa = (struct sockaddr *)buf;
770 	socklen_t len;
771 	int type, tlen;
772 
773 	tlen = sizeof (type);
774 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0)
775 		show_socktype((uint_t)type);
776 
777 	show_sockopts(Pr, fd);
778 	show_sockfilters(Pr, fd);
779 
780 	len = sizeof (buf);
781 	if (pr_getsockname(Pr, fd, sa, &len) == 0)
782 		show_sockaddr("sockname", sa, len);
783 
784 	len = sizeof (buf);
785 	if (pr_getpeername(Pr, fd, sa, &len) == 0)
786 		show_sockaddr("peername", sa, len);
787 
788 	dopeerucred(Pr, fd);
789 }
790 
791 /* the file is a fifo (aka "named pipe") */
792 static void
793 dofifo(struct ps_prochandle *Pr, int fd)
794 {
795 	dopeerucred(Pr, fd);
796 }
797 
798 /* the file is a TLI endpoint */
799 static void
800 dotli(struct ps_prochandle *Pr, int fd)
801 {
802 	struct strcmd strcmd;
803 
804 	strcmd.sc_len = STRCMDBUFSIZE;
805 	strcmd.sc_timeout = 5;
806 
807 	strcmd.sc_cmd = TI_GETMYNAME;
808 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
809 		show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0);
810 
811 	strcmd.sc_cmd = TI_GETPEERNAME;
812 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
813 		show_sockaddr("peername", (void *)&strcmd.sc_buf, 0);
814 }
815