xref: /titanic_44/usr/src/cmd/ptools/pfiles/pfiles.c (revision 7ff178cd8db129d385d3177eb20744d3b6efc59b)
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 #ifdef O_PRIV
412 #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
413 			O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
414 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
415 #else
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 #endif
420 
421 static void
422 show_fileflags(int flags)
423 {
424 	char buffer[136];
425 	char *str = buffer;
426 
427 	switch (flags & O_ACCMODE) {
428 	case O_RDONLY:
429 		(void) strcpy(str, "O_RDONLY");
430 		break;
431 	case O_WRONLY:
432 		(void) strcpy(str, "O_WRONLY");
433 		break;
434 	case O_RDWR:
435 		(void) strcpy(str, "O_RDWR");
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 #ifdef O_PRIV
449 	if (flags & O_PRIV)
450 		(void) strcat(str, "|O_PRIV");
451 #endif
452 	if (flags & O_SYNC)
453 		(void) strcat(str, "|O_SYNC");
454 	if (flags & O_DSYNC)
455 		(void) strcat(str, "|O_DSYNC");
456 	if (flags & O_RSYNC)
457 		(void) strcat(str, "|O_RSYNC");
458 	if (flags & O_CREAT)
459 		(void) strcat(str, "|O_CREAT");
460 	if (flags & O_TRUNC)
461 		(void) strcat(str, "|O_TRUNC");
462 	if (flags & O_EXCL)
463 		(void) strcat(str, "|O_EXCL");
464 	if (flags & O_NOCTTY)
465 		(void) strcat(str, "|O_NOCTTY");
466 	if (flags & O_LARGEFILE)
467 		(void) strcat(str, "|O_LARGEFILE");
468 	if (flags & O_XATTR)
469 		(void) strcat(str, "|O_XATTR");
470 	if (flags & ~(ALL_O_FLAGS))
471 		(void) sprintf(str + strlen(str), "|0x%x",
472 		    flags & ~(ALL_O_FLAGS));
473 
474 	(void) printf("%s", str);
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 	psinfo_t psinfo;
483 
484 	if (pr_door_info(Pr, fd, &door_info) != 0)
485 		return;
486 
487 	if (proc_get_psinfo(door_info.di_target, &psinfo) != 0)
488 		psinfo.pr_fname[0] = '\0';
489 
490 	(void) printf("  door to ");
491 	if (psinfo.pr_fname[0] != '\0')
492 		(void) printf("%s[%d]", psinfo.pr_fname,
493 		    (int)door_info.di_target);
494 	else
495 		(void) printf("pid %d", (int)door_info.di_target);
496 }
497 
498 /*
499  * Print out the socket address pointed to by `sa'.  `len' is only
500  * needed for AF_UNIX sockets.
501  */
502 static void
503 show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
504 {
505 	struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa;
506 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa;
507 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
508 	char  abuf[INET6_ADDRSTRLEN];
509 	const char *p;
510 
511 	switch (sa->sa_family) {
512 	default:
513 		return;
514 	case AF_INET:
515 		(void) printf("\t%s: AF_INET %s  port: %u\n", str,
516 		    inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)),
517 		    ntohs(so_in->sin_port));
518 		return;
519 	case AF_INET6:
520 		(void) printf("\t%s: AF_INET6 %s  port: %u\n", str,
521 		    inet_ntop(AF_INET6, &so_in6->sin6_addr,
522 		    abuf, sizeof (abuf)),
523 		    ntohs(so_in->sin_port));
524 		return;
525 	case AF_UNIX:
526 		if (len >= sizeof (so_un->sun_family)) {
527 			/* Null terminate */
528 			len -= sizeof (so_un->sun_family);
529 			so_un->sun_path[len] = '\0';
530 			(void) printf("\t%s: AF_UNIX %s\n",
531 			    str, so_un->sun_path);
532 		}
533 		return;
534 	case AF_IMPLINK:	p = "AF_IMPLINK";	break;
535 	case AF_PUP:		p = "AF_PUP";		break;
536 	case AF_CHAOS:		p = "AF_CHAOS";		break;
537 	case AF_NS:		p = "AF_NS";		break;
538 	case AF_NBS:		p = "AF_NBS";		break;
539 	case AF_ECMA:		p = "AF_ECMA";		break;
540 	case AF_DATAKIT:	p = "AF_DATAKIT";	break;
541 	case AF_CCITT:		p = "AF_CCITT";		break;
542 	case AF_SNA:		p = "AF_SNA";		break;
543 	case AF_DECnet:		p = "AF_DECnet";	break;
544 	case AF_DLI:		p = "AF_DLI";		break;
545 	case AF_LAT:		p = "AF_LAT";		break;
546 	case AF_HYLINK:		p = "AF_HYLINK";	break;
547 	case AF_APPLETALK:	p = "AF_APPLETALK";	break;
548 	case AF_NIT:		p = "AF_NIT";		break;
549 	case AF_802:		p = "AF_802";		break;
550 	case AF_OSI:		p = "AF_OSI";		break;
551 	case AF_X25:		p = "AF_X25";		break;
552 	case AF_OSINET:		p = "AF_OSINET";	break;
553 	case AF_GOSIP:		p = "AF_GOSIP";		break;
554 	case AF_IPX:		p = "AF_IPX";		break;
555 	case AF_ROUTE:		p = "AF_ROUTE";		break;
556 	case AF_LINK:		p = "AF_LINK";		break;
557 	}
558 
559 	(void) printf("\t%s: %s\n", str, p);
560 }
561 
562 static void
563 show_socktype(uint_t type)
564 {
565 	static const char *types[] = {
566 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
567 	};
568 
569 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
570 		(void) printf("\tSOCK_%s\n", types[type]);
571 	else
572 		(void) printf("\tunknown socket type %u\n", type);
573 }
574 
575 #define	BUFSIZE	200
576 static void
577 show_sockopts(struct ps_prochandle *Pr, int fd)
578 {
579 	int val, vlen;
580 	char buf[BUFSIZE];
581 	char buf1[32];
582 	char ipaddr[INET_ADDRSTRLEN];
583 	int i;
584 	in_addr_t nexthop_val;
585 	struct boolopt {
586 		int		level;
587 		int		opt;
588 		const char	*name;
589 	};
590 	static struct boolopt boolopts[] = {
591 	    { SOL_SOCKET, SO_DEBUG,		"SO_DEBUG,"	},
592 	    { SOL_SOCKET, SO_REUSEADDR,		"SO_REUSEADDR,"	},
593 	    { SOL_SOCKET, SO_KEEPALIVE,		"SO_KEEPALIVE,"	},
594 	    { SOL_SOCKET, SO_DONTROUTE,		"SO_DONTROUTE,"	},
595 	    { SOL_SOCKET, SO_BROADCAST,		"SO_BROADCAST,"	},
596 	    { SOL_SOCKET, SO_OOBINLINE,		"SO_OOBINLINE,"	},
597 	    { SOL_SOCKET, SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"},
598 	    { SOL_SOCKET, SO_ALLZONES,		"SO_ALLZONES,"	},
599 	    { SOL_SOCKET, SO_MAC_EXEMPT,	"SO_MAC_EXEMPT," },
600 	    { SOL_SOCKET, SO_MAC_IMPLICIT,	"SO_MAC_IMPLICIT," },
601 	    { SOL_SOCKET, SO_EXCLBIND,		"SO_EXCLBIND," },
602 	    { SOL_SOCKET, SO_VRRP,		"SO_VRRP," },
603 	    { IPPROTO_UDP, UDP_NAT_T_ENDPOINT,	"UDP_NAT_T_ENDPOINT," },
604 	};
605 	struct linger l;
606 
607 	buf[0] = '!';		/* sentinel value, never printed */
608 	buf[1] = '\0';
609 
610 	for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) {
611 		vlen = sizeof (val);
612 		if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt,
613 		    &val, &vlen) == 0 && val != 0)
614 			(void) strlcat(buf, boolopts[i].name, sizeof (buf));
615 	}
616 
617 	vlen = sizeof (l);
618 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 &&
619 	    l.l_onoff != 0) {
620 		(void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),",
621 		    l.l_linger);
622 		(void) strlcat(buf, buf1, sizeof (buf));
623 	}
624 
625 	vlen = sizeof (val);
626 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) {
627 		(void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val);
628 		(void) strlcat(buf, buf1, sizeof (buf));
629 	}
630 	vlen = sizeof (val);
631 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) {
632 		(void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val);
633 		(void) strlcat(buf, buf1, sizeof (buf));
634 	}
635 	vlen = sizeof (nexthop_val);
636 	if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val,
637 	    &vlen) == 0) {
638 		if (vlen > 0) {
639 			(void) inet_ntop(AF_INET, (void *) &nexthop_val,
640 			    ipaddr, sizeof (ipaddr));
641 			(void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),",
642 			    ipaddr);
643 			(void) strlcat(buf, buf1, sizeof (buf));
644 		}
645 	}
646 
647 	buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */
648 	if (buf[1] != '\0')
649 		(void) printf("\t%s\n", buf+1);
650 }
651 
652 #define	MAXNALLOC	32
653 static void
654 show_sockfilters(struct ps_prochandle *Pr, int fd)
655 {
656 	struct fil_info *fi;
657 	int i = 0, nalloc = 2, len = nalloc * sizeof (*fi);
658 	boolean_t printhdr = B_TRUE;
659 
660 	fi = calloc(nalloc, sizeof (*fi));
661 	if (fi == NULL) {
662 		perror("calloc");
663 		return;
664 	}
665 	/* CONSTCOND */
666 	while (1) {
667 		if (pr_getsockopt(Pr, fd, SOL_FILTER, FIL_LIST, fi, &len) != 0)
668 			break;
669 		/* No filters */
670 		if (len == 0)
671 			break;
672 		/* Make sure buffer was large enough */
673 		if (fi->fi_pos >= nalloc) {
674 			struct fil_info *new;
675 
676 			nalloc = fi->fi_pos + 1;
677 			if (nalloc > MAXNALLOC)
678 				break;
679 			len = nalloc * sizeof (*fi);
680 			new = realloc(fi, nalloc * sizeof (*fi));
681 			if (new == NULL) {
682 				perror("realloc");
683 				break;
684 			}
685 			fi = new;
686 			continue;
687 		}
688 
689 		for (i = 0; (i + 1) * sizeof (*fi) <= len; i++) {
690 			if (fi[i].fi_flags & FILF_BYPASS)
691 				continue;
692 			if (printhdr) {
693 				(void) printf("\tfilters: ");
694 				printhdr = B_FALSE;
695 			}
696 			(void) printf("%s", fi[i].fi_name);
697 			if (fi[i].fi_flags != 0) {
698 				(void) printf("(");
699 				if (fi[i].fi_flags & FILF_AUTO)
700 					(void) printf("auto,");
701 				if (fi[i].fi_flags & FILF_PROG)
702 					(void) printf("prog,");
703 				(void) printf("\b)");
704 			}
705 			if (fi[i].fi_pos == 0) /* last one */
706 				break;
707 			(void) printf(",");
708 		}
709 		if (!printhdr)
710 			(void) printf("\n");
711 		break;
712 	}
713 	free(fi);
714 }
715 
716 /* the file is a socket */
717 static void
718 dosocket(struct ps_prochandle *Pr, int fd)
719 {
720 	/* A buffer large enough for PATH_MAX size AF_UNIX address */
721 	long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1)
722 	    / sizeof (long)];
723 	struct sockaddr *sa = (struct sockaddr *)buf;
724 	socklen_t len;
725 	int type, tlen;
726 
727 	tlen = sizeof (type);
728 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0)
729 		show_socktype((uint_t)type);
730 
731 	show_sockopts(Pr, fd);
732 	show_sockfilters(Pr, fd);
733 
734 	len = sizeof (buf);
735 	if (pr_getsockname(Pr, fd, sa, &len) == 0)
736 		show_sockaddr("sockname", sa, len);
737 
738 	len = sizeof (buf);
739 	if (pr_getpeername(Pr, fd, sa, &len) == 0)
740 		show_sockaddr("peername", sa, len);
741 }
742 
743 /* the file is a TLI endpoint */
744 static void
745 dotli(struct ps_prochandle *Pr, int fd)
746 {
747 	struct strcmd strcmd;
748 
749 	strcmd.sc_len = STRCMDBUFSIZE;
750 	strcmd.sc_timeout = 5;
751 
752 	strcmd.sc_cmd = TI_GETMYNAME;
753 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
754 		show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0);
755 
756 	strcmd.sc_cmd = TI_GETPEERNAME;
757 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
758 		show_sockaddr("peername", (void *)&strcmd.sc_buf, 0);
759 }
760