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