xref: /illumos-gate/usr/src/cmd/ptools/pfiles/pfiles.c (revision c3a558e7)
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 			off_t offset;
297 
298 			dofcntl(Pr, fd,
299 			    (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP))
300 			    == (S_IFREG|S_ENFMT),
301 			    (statb.st_mode & S_IFMT) == S_IFDOOR);
302 
303 			if ((statb.st_mode & S_IFMT) == S_IFSOCK)
304 				dosocket(Pr, fd);
305 
306 			(void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd);
307 
308 			if ((ret = readlink(pname, fname, PATH_MAX - 1)) <= 0)
309 				continue;
310 
311 			fname[ret] = '\0';
312 
313 			if ((statb.st_mode & S_IFMT) == S_IFCHR &&
314 			    (dev = strrchr(fname, ':')) != NULL) {
315 				/*
316 				 * There's no elegant way to determine if a
317 				 * character device supports TLI, so we lame
318 				 * out and just check a hardcoded list of
319 				 * known TLI devices.
320 				 */
321 				int i;
322 				const char *tlidevs[] =
323 				    { "tcp", "tcp6", "udp", "udp6", NULL };
324 
325 				dev++; /* skip past the `:' */
326 				for (i = 0; tlidevs[i] != NULL; i++) {
327 					if (strcmp(dev, tlidevs[i]) == 0) {
328 						dotli(Pr, fd);
329 						break;
330 					}
331 				}
332 			}
333 			(void) printf("      %s\n", fname);
334 
335 			offset = pr_lseek(Pr, fd, 0, SEEK_CUR);
336 			if (offset != -1) {
337 				(void) printf("      offset:%ld\n", offset);
338 			}
339 
340 		}
341 	}
342 	(void) closedir(dirp);
343 }
344 
345 
346 static int
347 getflock(struct ps_prochandle *Pr, int fd, struct flock *flock_native)
348 {
349 	int ret;
350 #ifdef _LP64
351 	struct flock64_32 flock_target;
352 
353 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32) {
354 		copyflock(flock_target, *flock_native);
355 		ret = pr_fcntl(Pr, fd, F_GETLK, &flock_target);
356 		copyflock(*flock_native, flock_target);
357 		return (ret);
358 	}
359 #endif /* _LP64 */
360 	ret = pr_fcntl(Pr, fd, F_GETLK, flock_native);
361 	return (ret);
362 }
363 
364 /* examine open file with fcntl() */
365 static void
366 dofcntl(struct ps_prochandle *Pr, int fd, int mandatory, int isdoor)
367 {
368 	struct flock flock;
369 	int fileflags;
370 	int fdflags;
371 
372 	fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0);
373 	fdflags = pr_fcntl(Pr, fd, F_GETFD, 0);
374 
375 	if (fileflags != -1 || fdflags != -1) {
376 		(void) printf("      ");
377 		if (fileflags != -1)
378 			show_fileflags(fileflags);
379 		if (fdflags != -1 && (fdflags & FD_CLOEXEC))
380 			(void) printf(" FD_CLOEXEC");
381 		if (isdoor)
382 			show_door(Pr, fd);
383 		(void) fputc('\n', stdout);
384 	} else if (isdoor) {
385 		(void) printf("    ");
386 		show_door(Pr, fd);
387 		(void) fputc('\n', stdout);
388 	}
389 
390 	flock.l_type = F_WRLCK;
391 	flock.l_whence = 0;
392 	flock.l_start = 0;
393 	flock.l_len = 0;
394 	flock.l_sysid = 0;
395 	flock.l_pid = 0;
396 	if (getflock(Pr, fd, &flock) != -1) {
397 		if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) {
398 			unsigned long sysid = flock.l_sysid;
399 
400 			(void) printf("      %s %s lock set by",
401 			    mandatory ? "mandatory" : "advisory",
402 			    flock.l_type == F_RDLCK? "read" : "write");
403 			if (sysid)
404 				(void) printf(" system 0x%lX", sysid);
405 			if (flock.l_pid)
406 				(void) printf(" process %d", (int)flock.l_pid);
407 			(void) fputc('\n', stdout);
408 		}
409 	}
410 }
411 
412 #ifdef O_PRIV
413 #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
414 			O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
415 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
416 #else
417 #define	ALL_O_FLAGS	O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \
418 			O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \
419 			O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE
420 #endif
421 
422 static void
423 show_fileflags(int flags)
424 {
425 	char buffer[136];
426 	char *str = buffer;
427 
428 	switch (flags & O_ACCMODE) {
429 	case O_RDONLY:
430 		(void) strcpy(str, "O_RDONLY");
431 		break;
432 	case O_WRONLY:
433 		(void) strcpy(str, "O_WRONLY");
434 		break;
435 	case O_RDWR:
436 		(void) strcpy(str, "O_RDWR");
437 		break;
438 	default:
439 		(void) sprintf(str, "0x%x", flags & O_ACCMODE);
440 		break;
441 	}
442 
443 	if (flags & O_NDELAY)
444 		(void) strcat(str, "|O_NDELAY");
445 	if (flags & O_NONBLOCK)
446 		(void) strcat(str, "|O_NONBLOCK");
447 	if (flags & O_APPEND)
448 		(void) strcat(str, "|O_APPEND");
449 #ifdef O_PRIV
450 	if (flags & O_PRIV)
451 		(void) strcat(str, "|O_PRIV");
452 #endif
453 	if (flags & O_SYNC)
454 		(void) strcat(str, "|O_SYNC");
455 	if (flags & O_DSYNC)
456 		(void) strcat(str, "|O_DSYNC");
457 	if (flags & O_RSYNC)
458 		(void) strcat(str, "|O_RSYNC");
459 	if (flags & O_CREAT)
460 		(void) strcat(str, "|O_CREAT");
461 	if (flags & O_TRUNC)
462 		(void) strcat(str, "|O_TRUNC");
463 	if (flags & O_EXCL)
464 		(void) strcat(str, "|O_EXCL");
465 	if (flags & O_NOCTTY)
466 		(void) strcat(str, "|O_NOCTTY");
467 	if (flags & O_LARGEFILE)
468 		(void) strcat(str, "|O_LARGEFILE");
469 	if (flags & O_XATTR)
470 		(void) strcat(str, "|O_XATTR");
471 	if (flags & ~(ALL_O_FLAGS))
472 		(void) sprintf(str + strlen(str), "|0x%x",
473 		    flags & ~(ALL_O_FLAGS));
474 
475 	(void) printf("%s", str);
476 }
477 
478 /* show door info */
479 static void
480 show_door(struct ps_prochandle *Pr, int fd)
481 {
482 	door_info_t door_info;
483 	psinfo_t psinfo;
484 
485 	if (pr_door_info(Pr, fd, &door_info) != 0)
486 		return;
487 
488 	if (proc_get_psinfo(door_info.di_target, &psinfo) != 0)
489 		psinfo.pr_fname[0] = '\0';
490 
491 	(void) printf("  door to ");
492 	if (psinfo.pr_fname[0] != '\0')
493 		(void) printf("%s[%d]", psinfo.pr_fname,
494 		    (int)door_info.di_target);
495 	else
496 		(void) printf("pid %d", (int)door_info.di_target);
497 }
498 
499 /*
500  * Print out the socket address pointed to by `sa'.  `len' is only
501  * needed for AF_UNIX sockets.
502  */
503 static void
504 show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len)
505 {
506 	struct sockaddr_in *so_in = (struct sockaddr_in *)(void *)sa;
507 	struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)(void *)sa;
508 	struct sockaddr_un *so_un = (struct sockaddr_un *)sa;
509 	char  abuf[INET6_ADDRSTRLEN];
510 	const char *p;
511 
512 	switch (sa->sa_family) {
513 	default:
514 		return;
515 	case AF_INET:
516 		(void) printf("\t%s: AF_INET %s  port: %u\n", str,
517 		    inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)),
518 		    ntohs(so_in->sin_port));
519 		return;
520 	case AF_INET6:
521 		(void) printf("\t%s: AF_INET6 %s  port: %u\n", str,
522 		    inet_ntop(AF_INET6, &so_in6->sin6_addr,
523 		    abuf, sizeof (abuf)),
524 		    ntohs(so_in->sin_port));
525 		return;
526 	case AF_UNIX:
527 		if (len >= sizeof (so_un->sun_family)) {
528 			/* Null terminate */
529 			len -= sizeof (so_un->sun_family);
530 			so_un->sun_path[len] = '\0';
531 			(void) printf("\t%s: AF_UNIX %s\n",
532 			    str, so_un->sun_path);
533 		}
534 		return;
535 	case AF_IMPLINK:	p = "AF_IMPLINK";	break;
536 	case AF_PUP:		p = "AF_PUP";		break;
537 	case AF_CHAOS:		p = "AF_CHAOS";		break;
538 	case AF_NS:		p = "AF_NS";		break;
539 	case AF_NBS:		p = "AF_NBS";		break;
540 	case AF_ECMA:		p = "AF_ECMA";		break;
541 	case AF_DATAKIT:	p = "AF_DATAKIT";	break;
542 	case AF_CCITT:		p = "AF_CCITT";		break;
543 	case AF_SNA:		p = "AF_SNA";		break;
544 	case AF_DECnet:		p = "AF_DECnet";	break;
545 	case AF_DLI:		p = "AF_DLI";		break;
546 	case AF_LAT:		p = "AF_LAT";		break;
547 	case AF_HYLINK:		p = "AF_HYLINK";	break;
548 	case AF_APPLETALK:	p = "AF_APPLETALK";	break;
549 	case AF_NIT:		p = "AF_NIT";		break;
550 	case AF_802:		p = "AF_802";		break;
551 	case AF_OSI:		p = "AF_OSI";		break;
552 	case AF_X25:		p = "AF_X25";		break;
553 	case AF_OSINET:		p = "AF_OSINET";	break;
554 	case AF_GOSIP:		p = "AF_GOSIP";		break;
555 	case AF_IPX:		p = "AF_IPX";		break;
556 	case AF_ROUTE:		p = "AF_ROUTE";		break;
557 	case AF_LINK:		p = "AF_LINK";		break;
558 	}
559 
560 	(void) printf("\t%s: %s\n", str, p);
561 }
562 
563 static void
564 show_socktype(uint_t type)
565 {
566 	static const char *types[] = {
567 		NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET"
568 	};
569 
570 	if (type < sizeof (types) / sizeof (*types) && types[type] != NULL)
571 		(void) printf("\tSOCK_%s\n", types[type]);
572 	else
573 		(void) printf("\tunknown socket type %u\n", type);
574 }
575 
576 #define	BUFSIZE	200
577 static void
578 show_sockopts(struct ps_prochandle *Pr, int fd)
579 {
580 	int val, vlen;
581 	char buf[BUFSIZE];
582 	char buf1[32];
583 	char ipaddr[INET_ADDRSTRLEN];
584 	int i;
585 	in_addr_t nexthop_val;
586 	struct boolopt {
587 		int		level;
588 		int		opt;
589 		const char	*name;
590 	};
591 	static struct boolopt boolopts[] = {
592 	    { SOL_SOCKET, SO_DEBUG,		"SO_DEBUG,"	},
593 	    { SOL_SOCKET, SO_REUSEADDR,		"SO_REUSEADDR,"	},
594 	    { SOL_SOCKET, SO_KEEPALIVE,		"SO_KEEPALIVE,"	},
595 	    { SOL_SOCKET, SO_DONTROUTE,		"SO_DONTROUTE,"	},
596 	    { SOL_SOCKET, SO_BROADCAST,		"SO_BROADCAST,"	},
597 	    { SOL_SOCKET, SO_OOBINLINE,		"SO_OOBINLINE,"	},
598 	    { SOL_SOCKET, SO_DGRAM_ERRIND,	"SO_DGRAM_ERRIND,"},
599 	    { SOL_SOCKET, SO_ALLZONES,		"SO_ALLZONES,"	},
600 	    { SOL_SOCKET, SO_MAC_EXEMPT,	"SO_MAC_EXEMPT," },
601 	    { SOL_SOCKET, SO_MAC_IMPLICIT,	"SO_MAC_IMPLICIT," },
602 	    { SOL_SOCKET, SO_EXCLBIND,		"SO_EXCLBIND," },
603 	    { SOL_SOCKET, SO_VRRP,		"SO_VRRP," },
604 	    { IPPROTO_UDP, UDP_NAT_T_ENDPOINT,	"UDP_NAT_T_ENDPOINT," },
605 	};
606 	struct linger l;
607 
608 	buf[0] = '!';		/* sentinel value, never printed */
609 	buf[1] = '\0';
610 
611 	for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) {
612 		vlen = sizeof (val);
613 		if (pr_getsockopt(Pr, fd, boolopts[i].level, boolopts[i].opt,
614 		    &val, &vlen) == 0 && val != 0)
615 			(void) strlcat(buf, boolopts[i].name, sizeof (buf));
616 	}
617 
618 	vlen = sizeof (l);
619 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 &&
620 	    l.l_onoff != 0) {
621 		(void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),",
622 		    l.l_linger);
623 		(void) strlcat(buf, buf1, sizeof (buf));
624 	}
625 
626 	vlen = sizeof (val);
627 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) {
628 		(void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val);
629 		(void) strlcat(buf, buf1, sizeof (buf));
630 	}
631 	vlen = sizeof (val);
632 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) {
633 		(void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val);
634 		(void) strlcat(buf, buf1, sizeof (buf));
635 	}
636 	vlen = sizeof (nexthop_val);
637 	if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val,
638 	    &vlen) == 0) {
639 		if (vlen > 0) {
640 			(void) inet_ntop(AF_INET, (void *) &nexthop_val,
641 			    ipaddr, sizeof (ipaddr));
642 			(void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),",
643 			    ipaddr);
644 			(void) strlcat(buf, buf1, sizeof (buf));
645 		}
646 	}
647 
648 	buf[strlen(buf) - 1] = '\0'; /* overwrites sentinel if no options */
649 	if (buf[1] != '\0')
650 		(void) printf("\t%s\n", buf+1);
651 }
652 
653 /* the file is a socket */
654 static void
655 dosocket(struct ps_prochandle *Pr, int fd)
656 {
657 	/* A buffer large enough for PATH_MAX size AF_UNIX address */
658 	long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1)
659 	    / sizeof (long)];
660 	struct sockaddr *sa = (struct sockaddr *)buf;
661 	socklen_t len;
662 	int type, tlen;
663 
664 	tlen = sizeof (type);
665 	if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) == 0)
666 		show_socktype((uint_t)type);
667 
668 	show_sockopts(Pr, fd);
669 
670 	len = sizeof (buf);
671 	if (pr_getsockname(Pr, fd, sa, &len) == 0)
672 		show_sockaddr("sockname", sa, len);
673 
674 	len = sizeof (buf);
675 	if (pr_getpeername(Pr, fd, sa, &len) == 0)
676 		show_sockaddr("peername", sa, len);
677 }
678 
679 /* the file is a TLI endpoint */
680 static void
681 dotli(struct ps_prochandle *Pr, int fd)
682 {
683 	struct strcmd strcmd;
684 
685 	strcmd.sc_len = STRCMDBUFSIZE;
686 	strcmd.sc_timeout = 5;
687 
688 	strcmd.sc_cmd = TI_GETMYNAME;
689 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
690 		show_sockaddr("sockname", (void *)&strcmd.sc_buf, 0);
691 
692 	strcmd.sc_cmd = TI_GETPEERNAME;
693 	if (pr_ioctl(Pr, fd, _I_CMD, &strcmd, sizeof (strcmd)) == 0)
694 		show_sockaddr("peername", (void *)&strcmd.sc_buf, 0);
695 }
696