xref: /titanic_52/usr/src/cmd/ptools/pfiles/pfiles.c (revision 686c0471389a2f92c1e43b38e1b97771bee482ab)
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 	/*
358 	 * Pr may be NULL when pfiles is inspecting itself, but in that case
359 	 * we already know the data model of the two processes must match.
360 	 */
361 	if ((Pr != NULL) && (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32)) {
362 		copyflock(flock_target, *flock_native);
363 		ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target);
364 		copyflock(*flock_native, flock_target);
365 		return (ret);
366 	}
367 #endif /* _LP64 */
368 	ret = pr_fcntl(Pr, fd, F_GETLK, flock_native);
369 	return (ret);
370 }
371 
372 /* examine open file with fcntl() */
373 static void
374 dofcntl(struct ps_prochandle *Pr, int fd, int mandatory, int isdoor)
375 {
376 	struct flock flock;
377 	int fileflags;
378 	int fdflags;
379 
380 	fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0);
381 	fdflags = pr_fcntl(Pr, fd, F_GETFD, 0);
382 
383 	if (fileflags != -1 || fdflags != -1) {
384 		(void) printf("      ");
385 		if (fileflags != -1)
386 			show_fileflags(fileflags);
387 		if (fdflags != -1 && (fdflags & FD_CLOEXEC))
388 			(void) printf(" FD_CLOEXEC");
389 		if (isdoor)
390 			show_door(Pr, fd);
391 		(void) fputc('\n', stdout);
392 	} else if (isdoor) {
393 		(void) printf("    ");
394 		show_door(Pr, fd);
395 		(void) fputc('\n', stdout);
396 	}
397 
398 	flock.l_type = F_WRLCK;
399 	flock.l_whence = 0;
400 	flock.l_start = 0;
401 	flock.l_len = 0;
402 	flock.l_sysid = 0;
403 	flock.l_pid = 0;
404 	if (getflock(Pr, fd, &flock) != -1) {
405 		if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) {
406 			unsigned long sysid = flock.l_sysid;
407 
408 			(void) printf("      %s %s lock set by",
409 			    mandatory ? "mandatory" : "advisory",
410 			    flock.l_type == F_RDLCK? "read" : "write");
411 			if (sysid)
412 				(void) printf(" system 0x%lX", sysid);
413 			if (flock.l_pid)
414 				(void) printf(" process %d", (int)flock.l_pid);
415 			(void) fputc('\n', stdout);
416 		}
417 	}
418 }
419 
420 #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
421 			O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
422 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
423 
424 static void
425 show_fileflags(int flags)
426 {
427 	char buffer[136];
428 	char *str = buffer;
429 
430 	switch (flags & O_ACCMODE) {
431 	case O_RDONLY:
432 		(void) strcpy(str, "O_RDONLY");
433 		break;
434 	case O_WRONLY:
435 		(void) strcpy(str, "O_WRONLY");
436 		break;
437 	case O_RDWR:
438 		(void) strcpy(str, "O_RDWR");
439 		break;
440 	case O_SEARCH:
441 		(void) strcpy(str, "O_SEARCH");
442 		break;
443 	case O_EXEC:
444 		(void) strcpy(str, "O_EXEC");
445 		break;
446 	default:
447 		(void) sprintf(str, "0x%x", flags & O_ACCMODE);
448 		break;
449 	}
450 
451 	if (flags & O_NDELAY)
452 		(void) strcat(str, "|O_NDELAY");
453 	if (flags & O_NONBLOCK)
454 		(void) strcat(str, "|O_NONBLOCK");
455 	if (flags & O_APPEND)
456 		(void) strcat(str, "|O_APPEND");
457 	if (flags & O_SYNC)
458 		(void) strcat(str, "|O_SYNC");
459 	if (flags & O_DSYNC)
460 		(void) strcat(str, "|O_DSYNC");
461 	if (flags & O_RSYNC)
462 		(void) strcat(str, "|O_RSYNC");
463 	if (flags & O_CREAT)
464 		(void) strcat(str, "|O_CREAT");
465 	if (flags & O_TRUNC)
466 		(void) strcat(str, "|O_TRUNC");
467 	if (flags & O_EXCL)
468 		(void) strcat(str, "|O_EXCL");
469 	if (flags & O_NOCTTY)
470 		(void) strcat(str, "|O_NOCTTY");
471 	if (flags & O_LARGEFILE)
472 		(void) strcat(str, "|O_LARGEFILE");
473 	if (flags & O_XATTR)
474 		(void) strcat(str, "|O_XATTR");
475 	if (flags & ~(ALL_O_FLAGS))
476 		(void) sprintf(str + strlen(str), "|0x%x",
477 		    flags & ~(ALL_O_FLAGS));
478 
479 	(void) printf("%s", str);
480 }
481 
482 /* show process on the other end of a door, socket or fifo */
483 static void
484 show_peer_process(pid_t ppid)
485 {
486 	psinfo_t psinfo;
487 
488 	if (proc_get_psinfo(ppid, &psinfo) == 0)
489 		(void) printf(" %s[%d]", psinfo.pr_fname, (int)ppid);
490 	else
491 		(void) printf(" pid %d", (int)ppid);
492 }
493 
494 /* show door info */
495 static void
496 show_door(struct ps_prochandle *Pr, int fd)
497 {
498 	door_info_t door_info;
499 
500 	if (pr_door_info(Pr, fd, &door_info) != 0)
501 		return;
502 
503 	(void) printf("  door to");
504 	show_peer_process(door_info.di_target);
505 }
506 
507 /*
508  * Print out the socket address pointed to by `sa'.  `len' is only
509  * needed for AF_UNIX sockets.
510  */
511 static void
512 show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
513 {
514 	struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa;
515 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa;
516 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
517 	char  abuf[INET6_ADDRSTRLEN];
518 	const char *p;
519 
520 	switch (sa->sa_family) {
521 	default:
522 		return;
523 	case AF_INET:
524 		(void) printf("\t%s: AF_INET %s  port: %u\n", str,
525 		    inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)),
526 		    ntohs(so_in->sin_port));
527 		return;
528 	case AF_INET6:
529 		(void) printf("\t%s: AF_INET6 %s  port: %u\n", str,
530 		    inet_ntop(AF_INET6, &so_in6->sin6_addr,
531 		    abuf, sizeof (abuf)),
532 		    ntohs(so_in->sin_port));
533 		return;
534 	case AF_UNIX:
535 		if (len >= sizeof (so_un->sun_family)) {
536 			/* Null terminate */
537 			len -= sizeof (so_un->sun_family);
538 			so_un->sun_path[len] = '\0';
539 			(void) printf("\t%s: AF_UNIX %s\n",
540 			    str, so_un->sun_path);
541 		}
542 		return;
543 	case AF_IMPLINK:	p = "AF_IMPLINK";	break;
544 	case AF_PUP:		p = "AF_PUP";		break;
545 	case AF_CHAOS:		p = "AF_CHAOS";		break;
546 	case AF_NS:		p = "AF_NS";		break;
547 	case AF_NBS:		p = "AF_NBS";		break;
548 	case AF_ECMA:		p = "AF_ECMA";		break;
549 	case AF_DATAKIT:	p = "AF_DATAKIT";	break;
550 	case AF_CCITT:		p = "AF_CCITT";		break;
551 	case AF_SNA:		p = "AF_SNA";		break;
552 	case AF_DECnet:		p = "AF_DECnet";	break;
553 	case AF_DLI:		p = "AF_DLI";		break;
554 	case AF_LAT:		p = "AF_LAT";		break;
555 	case AF_HYLINK:		p = "AF_HYLINK";	break;
556 	case AF_APPLETALK:	p = "AF_APPLETALK";	break;
557 	case AF_NIT:		p = "AF_NIT";		break;
558 	case AF_802:		p = "AF_802";		break;
559 	case AF_OSI:		p = "AF_OSI";		break;
560 	case AF_X25:		p = "AF_X25";		break;
561 	case AF_OSINET:		p = "AF_OSINET";	break;
562 	case AF_GOSIP:		p = "AF_GOSIP";		break;
563 	case AF_IPX:		p = "AF_IPX";		break;
564 	case AF_ROUTE:		p = "AF_ROUTE";		break;
565 	case AF_LINK:		p = "AF_LINK";		break;
566 	}
567 
568 	(void) printf("\t%s: %s\n", str, p);
569 }
570 
571 /*
572  * Print out the process information for the other end of local sockets
573  * and fifos
574  */
575 static void
576 show_ucred(const char *str, ucred_t *cred)
577 {
578 	pid_t upid = ucred_getpid(cred);
579 	zoneid_t uzid = ucred_getzoneid(cred);
580 	char zonename[ZONENAME_MAX];
581 
582 	if ((upid != -1) || (uzid != -1)) {
583 		(void) printf("\t%s:", str);
584 		if (upid != -1) {
585 			show_peer_process(upid);
586 		}
587 		if (uzid != -1) {
588 			if (getzonenamebyid(uzid, zonename, sizeof (zonename))
589 			    != -1) {
590 				(void) printf(" zone: %s[%d]", zonename,
591 				    (int)uzid);
592 			} else {
593 				(void) printf(" zoneid: %d", (int)uzid);
594 			}
595 		}
596 		(void) printf("\n");
597 	}
598 }
599 
600 static void
601 show_socktype(uint_t type)
602 {
603 	static const char *types[] = {
604 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
605 	};
606 
607 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
608 		(void) printf("\tSOCK_%s\n", types[type]);
609 	else
610 		(void) printf("\tunknown socket type %u\n", type);
611 }
612 
613 #define	BUFSIZE	200
614 static void
615 show_sockopts(struct ps_prochandle *Pr, int fd)
616 {
617 	int val, vlen;
618 	char buf[BUFSIZE];
619 	char buf1[32];
620 	char ipaddr[INET_ADDRSTRLEN];
621 	int i;
622 	in_addr_t nexthop_val;
623 	struct boolopt {
624 		int		level;
625 		int		opt;
626 		const char	*name;
627 	};
628 	static struct boolopt boolopts[] = {
629 	    { SOL_SOCKET, SO_DEBUG,		"SO_DEBUG,"	},
630 	    { SOL_SOCKET, SO_REUSEADDR,		"SO_REUSEADDR,"	},
631 	    { SOL_SOCKET, SO_KEEPALIVE,		"SO_KEEPALIVE,"	},
632 	    { SOL_SOCKET, SO_DONTROUTE,		"SO_DONTROUTE,"	},
633 	    { SOL_SOCKET, SO_BROADCAST,		"SO_BROADCAST,"	},
634 	    { SOL_SOCKET, SO_OOBINLINE,		"SO_OOBINLINE,"	},
635 	    { SOL_SOCKET, SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"},
636 	    { SOL_SOCKET, SO_ALLZONES,		"SO_ALLZONES,"	},
637 	    { SOL_SOCKET, SO_MAC_EXEMPT,	"SO_MAC_EXEMPT," },
638 	    { SOL_SOCKET, SO_MAC_IMPLICIT,	"SO_MAC_IMPLICIT," },
639 	    { SOL_SOCKET, SO_EXCLBIND,		"SO_EXCLBIND," },
640 	    { SOL_SOCKET, SO_VRRP,		"SO_VRRP," },
641 	    { IPPROTO_UDP, UDP_NAT_T_ENDPOINT,	"UDP_NAT_T_ENDPOINT," },
642 	};
643 	struct linger l;
644 
645 	buf[0] = '!';		/* sentinel value, never printed */
646 	buf[1] = '\0';
647 
648 	for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) {
649 		vlen = sizeof (val);
650 		if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt,
651 		    &val, &vlen) == 0 && val != 0)
652 			(void) strlcat(buf, boolopts[i].name, sizeof (buf));
653 	}
654 
655 	vlen = sizeof (l);
656 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 &&
657 	    l.l_onoff != 0) {
658 		(void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),",
659 		    l.l_linger);
660 		(void) strlcat(buf, buf1, sizeof (buf));
661 	}
662 
663 	vlen = sizeof (val);
664 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) {
665 		(void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val);
666 		(void) strlcat(buf, buf1, sizeof (buf));
667 	}
668 	vlen = sizeof (val);
669 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) {
670 		(void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val);
671 		(void) strlcat(buf, buf1, sizeof (buf));
672 	}
673 	vlen = sizeof (nexthop_val);
674 	if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val,
675 	    &vlen) == 0) {
676 		if (vlen > 0) {
677 			(void) inet_ntop(AF_INET, (void *) &nexthop_val,
678 			    ipaddr, sizeof (ipaddr));
679 			(void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),",
680 			    ipaddr);
681 			(void) strlcat(buf, buf1, sizeof (buf));
682 		}
683 	}
684 
685 	buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */
686 	if (buf[1] != '\0')
687 		(void) printf("\t%s\n", buf+1);
688 }
689 
690 #define	MAXNALLOC	32
691 static void
692 show_sockfilters(struct ps_prochandle *Pr, int fd)
693 {
694 	struct fil_info *fi;
695 	int i = 0, nalloc = 2, len = nalloc * sizeof (*fi);
696 	boolean_t printhdr = B_TRUE;
697 
698 	fi = calloc(nalloc, sizeof (*fi));
699 	if (fi == NULL) {
700 		perror("calloc");
701 		return;
702 	}
703 	/* CONSTCOND */
704 	while (1) {
705 		if (pr_getsockopt(Pr, fd, SOL_FILTER, FIL_LIST, fi, &len) != 0)
706 			break;
707 		/* No filters */
708 		if (len == 0)
709 			break;
710 		/* Make sure buffer was large enough */
711 		if (fi->fi_pos >= nalloc) {
712 			struct fil_info *new;
713 
714 			nalloc = fi->fi_pos + 1;
715 			if (nalloc > MAXNALLOC)
716 				break;
717 			len = nalloc * sizeof (*fi);
718 			new = realloc(fi, nalloc * sizeof (*fi));
719 			if (new == NULL) {
720 				perror("realloc");
721 				break;
722 			}
723 			fi = new;
724 			continue;
725 		}
726 
727 		for (i = 0; (i + 1) * sizeof (*fi) <= len; i++) {
728 			if (fi[i].fi_flags & FILF_BYPASS)
729 				continue;
730 			if (printhdr) {
731 				(void) printf("\tfilters: ");
732 				printhdr = B_FALSE;
733 			}
734 			(void) printf("%s", fi[i].fi_name);
735 			if (fi[i].fi_flags != 0) {
736 				(void) printf("(");
737 				if (fi[i].fi_flags & FILF_AUTO)
738 					(void) printf("auto,");
739 				if (fi[i].fi_flags & FILF_PROG)
740 					(void) printf("prog,");
741 				(void) printf("\b)");
742 			}
743 			if (fi[i].fi_pos == 0) /* last one */
744 				break;
745 			(void) printf(",");
746 		}
747 		if (!printhdr)
748 			(void) printf("\n");
749 		break;
750 	}
751 	free(fi);
752 }
753 
754 /* print peer credentials for sockets and named pipes */
755 static void
756 dopeerucred(struct ps_prochandle *Pr, int fd)
757 {
758 	ucred_t *peercred = NULL;	/* allocated by getpeerucred */
759 
760 	if (pr_getpeerucred(Pr, fd, &peercred) == 0) {
761 		show_ucred("peer", peercred);
762 		ucred_free(peercred);
763 	}
764 }
765 
766 /* the file is a socket */
767 static void
768 dosocket(struct ps_prochandle *Pr, int fd)
769 {
770 	/* A buffer large enough for PATH_MAX size AF_UNIX address */
771 	long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1)
772 	    / sizeof (long)];
773 	struct sockaddr *sa = (struct sockaddr *)buf;
774 	socklen_t len;
775 	int type, tlen;
776 
777 	tlen = sizeof (type);
778 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0)
779 		show_socktype((uint_t)type);
780 
781 	show_sockopts(Pr, fd);
782 	show_sockfilters(Pr, fd);
783 
784 	len = sizeof (buf);
785 	if (pr_getsockname(Pr, fd, sa, &len) == 0)
786 		show_sockaddr("sockname", sa, len);
787 
788 	len = sizeof (buf);
789 	if (pr_getpeername(Pr, fd, sa, &len) == 0)
790 		show_sockaddr("peername", sa, len);
791 
792 	dopeerucred(Pr, fd);
793 }
794 
795 /* the file is a fifo (aka "named pipe") */
796 static void
797 dofifo(struct ps_prochandle *Pr, int fd)
798 {
799 	dopeerucred(Pr, fd);
800 }
801 
802 /* the file is a TLI endpoint */
803 static void
804 dotli(struct ps_prochandle *Pr, int fd)
805 {
806 	struct strcmd strcmd;
807 
808 	strcmd.sc_len = STRCMDBUFSIZE;
809 	strcmd.sc_timeout = 5;
810 
811 	strcmd.sc_cmd = TI_GETMYNAME;
812 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
813 		show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0);
814 
815 	strcmd.sc_cmd = TI_GETPEERNAME;
816 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
817 		show_sockaddr("peername", (void *)&strcmd.sc_buf, 0);
818 }
819