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