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