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