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