xref: /original-bsd/usr.bin/fstat/fstat.c (revision d54be081)
1 /*-
2  * Copyright (c) 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)fstat.c	5.32 (Berkeley) 06/17/91";
16 #endif /* not lint */
17 
18 /*
19  *  fstat
20  */
21 #include <sys/param.h>
22 #include <sys/time.h>
23 #include <sys/proc.h>
24 #include <sys/user.h>
25 #ifdef SPPWAIT
26 #define NEWVM
27 #endif
28 #ifndef NEWVM
29 #include <machine/pte.h>
30 #include <sys/vmmac.h>
31 #endif
32 #include <sys/stat.h>
33 #include <sys/vnode.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include <sys/domain.h>
37 #include <sys/protosw.h>
38 #include <sys/unpcb.h>
39 #include <sys/kinfo.h>
40 #include <sys/filedesc.h>
41 #define	KERNEL
42 #define NFS
43 #include <sys/file.h>
44 #include <sys/mount.h>
45 #include <ufs/quota.h>
46 #include <ufs/inode.h>
47 #include <nfs/nfsv2.h>
48 #include <nfs/nfs.h>
49 #include <nfs/nfsnode.h>
50 #undef KERNEL
51 
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/in_pcb.h>
57 
58 #include <errno.h>
59 #include <nlist.h>
60 #include <kvm.h>
61 #include <pwd.h>
62 #include <stdio.h>
63 #include <paths.h>
64 #include <ctype.h>
65 #include <stdlib.h>
66 #include <string.h>
67 
68 #define	TEXT	-1
69 #define	CDIR	-2
70 #define	RDIR	-3
71 #define	TRACE	-4
72 
73 typedef struct devs {
74 	struct	devs *next;
75 	long	fsid;
76 	ino_t	ino;
77 	char	*name;
78 } DEVS;
79 DEVS *devs;
80 
81 struct  filestat {
82 	long	fsid;
83 	long	fileid;
84 	mode_t	mode;
85 	u_long	size;
86 	dev_t	rdev;
87 };
88 
89 #ifdef notdef
90 struct nlist nl[] = {
91 	{ "" },
92 };
93 #endif
94 
95 int 	fsflg,	/* show files on same filesystem as file(s) argument */
96 	pflg,	/* show files open by a particular pid */
97 	uflg;	/* show files open by a particular (effective) user */
98 int 	checkfile; /* true if restricting to particular files or filesystems */
99 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
100 int	vflg;	/* display errors in locating kernel data objects etc... */
101 
102 #define dprintf	if (vflg) fprintf
103 
104 struct file **ofiles;	/* buffer of pointers to file structures */
105 int maxfiles;
106 #define ALLOC_OFILES(d)	\
107 	if ((d) > maxfiles) { \
108 		free(ofiles); \
109 		ofiles = malloc((d) * sizeof(struct file *)); \
110 		if (ofiles == NULL) { \
111 			fprintf(stderr, "fstat: %s\n", strerror(errno)); \
112 			exit(1); \
113 		} \
114 		maxfiles = (d); \
115 	}
116 
117 /*
118  * a kvm_read that returns true if everything is read
119  */
120 #define KVM_READ(kaddr, paddr, len) (kvm_read((kaddr), (paddr), (len)) == (len))
121 
122 void dofiles(), getinetproto(), socktrans(), nfs_filestat(), ufs_filestat();
123 void usage(), vtrans();
124 
125 main(argc, argv)
126 	int argc;
127 	char **argv;
128 {
129 	extern char *optarg;
130 	extern int optind;
131 	register struct passwd *passwd;
132 	struct proc *p;
133 	int arg, ch, what;
134 	char *namelist = NULL, *memfile = NULL;
135 
136 	arg = 0;
137 	what = KINFO_PROC_ALL;
138 	while ((ch = getopt(argc, argv, "fnp:u:vNM")) != EOF)
139 		switch((char)ch) {
140 		case 'f':
141 			fsflg = 1;
142 			break;
143 		case 'n':
144 			nflg = 1;
145 			break;
146 		case 'p':
147 			if (pflg++)
148 				usage();
149 			if (!isdigit(*optarg)) {
150 				fprintf(stderr,
151 				    "fstat: -p requires a process id\n");
152 				usage();
153 			}
154 			what = KINFO_PROC_PID;
155 			arg = atoi(optarg);
156 			break;
157 		case 'u':
158 			if (uflg++)
159 				usage();
160 			if (!(passwd = getpwnam(optarg))) {
161 				fprintf(stderr, "%s: unknown uid\n",
162 				    optarg);
163 				exit(1);
164 			}
165 			what = KINFO_PROC_UID;
166 			arg = passwd->pw_uid;
167 			break;
168 		case 'v':
169 			vflg = 1;
170 			break;
171 		case 'N':
172 			namelist = optarg;
173 			break;
174 		case 'M':
175 			memfile = optarg;
176 			break;
177 		case '?':
178 		default:
179 			usage();
180 		}
181 
182 	if (*(argv += optind)) {
183 		for (; *argv; ++argv) {
184 			if (getfname(*argv))
185 				checkfile = 1;
186 		}
187 		if (!checkfile)	/* file(s) specified, but none accessable */
188 			exit(1);
189 	}
190 
191 	ALLOC_OFILES(256);	/* reserve space for file pointers */
192 
193 	if (fsflg && !checkfile) {
194 		/* -f with no files means use wd */
195 		if (getfname(".") == 0)
196 			exit(1);
197 		checkfile = 1;
198 	}
199 
200 	if (kvm_openfiles(namelist, memfile, NULL) == -1) {
201 		fprintf(stderr, "fstat: %s\n", kvm_geterr());
202 		exit(1);
203 	}
204 #ifdef notdef
205 	if (kvm_nlist(nl) != 0) {
206 		fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr());
207 		exit(1);
208 	}
209 #endif
210 	if (kvm_getprocs(what, arg) == -1) {
211 		fprintf(stderr, "fstat: %s\n", kvm_geterr());
212 		exit(1);
213 	}
214 	if (nflg)
215 		printf("%s",
216 "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV");
217 	else
218 		printf("%s",
219 "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV");
220 	if (checkfile && fsflg == 0)
221 		printf(" NAME\n");
222 	else
223 		putchar('\n');
224 
225 	while ((p = kvm_nextproc()) != NULL) {
226 		if (p->p_stat == SZOMB)
227 			continue;
228 		dofiles(p);
229 	}
230 	exit(0);
231 }
232 
233 char	*Uname, *Comm;
234 int	Pid;
235 
236 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
237 	switch(i) { \
238 	case TEXT: \
239 		printf(" text"); \
240 		break; \
241 	case CDIR: \
242 		printf("   wd"); \
243 		break; \
244 	case RDIR: \
245 		printf(" root"); \
246 		break; \
247 	case TRACE: \
248 		printf("   tr"); \
249 		break; \
250 	default: \
251 		printf(" %4d", i); \
252 		break; \
253 	}
254 
255 /*
256  * print open files attributed to this process
257  */
258 void
259 dofiles(p)
260 	struct proc *p;
261 {
262 	int i, last;
263 	struct file file;
264 #ifdef NEWVM
265 	struct filedesc0 filed0;
266 #define	filed	filed0.fd_fd
267 	struct eproc *ep;
268 #else
269 	struct filedesc filed;
270 #endif
271 
272 	extern char *user_from_uid();
273 #ifndef NEWVM
274 	struct vnode *xvptr;
275 #endif
276 
277 #ifdef NEWVM
278 	ep = kvm_geteproc(p);
279 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
280 #else
281 	Uname = user_from_uid(p->p_uid, 0);
282 #endif
283 	Pid = p->p_pid;
284 	Comm = p->p_comm;
285 
286 	if (p->p_fd == NULL)
287 		return;
288 #ifdef NEWVM
289 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
290 		dprintf(stderr, "can't read filedesc at %x for pid %d\n",
291 			p->p_fd, Pid);
292 		return;
293 	}
294 #else
295 	if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
296 		dprintf(stderr, "can't read filedesc at %x for pid %d\n",
297 			p->p_fd, Pid);
298 		return;
299 	}
300 #endif
301 	/*
302 	 * root directory vnode, if one
303 	 */
304 	if (filed.fd_rdir)
305 		vtrans(filed.fd_rdir, RDIR);
306 #ifndef NEWVM
307 	/*
308 	 * text vnode
309 	 */
310 	if (p->p_textp &&
311 	    KVM_READ(&(p->p_textp->x_vptr), &xvptr, sizeof (struct vnode *)) &&
312 	    xvptr != NULL)
313 		vtrans(xvptr, TEXT);
314 #endif
315 	/*
316 	 * current working directory vnode
317 	 */
318 	vtrans(filed.fd_cdir, CDIR);
319 	/*
320 	 * ktrace vnode, if one
321 	 */
322 	if (p->p_tracep)
323 		vtrans(p->p_tracep, TRACE);
324 	/*
325 	 * open files
326 	 */
327 #define FPSIZE	(sizeof (struct file *))
328 	ALLOC_OFILES(filed.fd_lastfile);
329 #ifdef NEWVM
330 	if (filed.fd_nfiles > NDFILE) {
331 		if (!KVM_READ(filed.fd_ofiles, ofiles,
332 		    filed.fd_lastfile * FPSIZE)) {
333 			dprintf(stderr,
334 			    "can't read file structures at %x for pid %d\n",
335 			    filed.fd_ofiles, Pid);
336 			return;
337 		}
338 	} else
339 		bcopy(filed0.fd_dfiles, ofiles, filed.fd_lastfile * FPSIZE);
340 #else
341 	bcopy(filed.fd_ofile, ofiles, MIN(filed.fd_lastfile, NDFILE) * FPSIZE);
342 	last = filed.fd_lastfile;
343 	if ((last > NDFILE) && !KVM_READ(filed.fd_moreofiles, &ofiles[NDFILE],
344 	    (last - NDFILE) * FPSIZE)) {
345 		dprintf(stderr, "can't read rest of files at %x for pid %d\n",
346 			filed.fd_moreofiles, Pid);
347 		return;
348 	}
349 #endif
350 	for (i = 0; i <= filed.fd_lastfile; i++) {
351 		if (ofiles[i] == NULL)
352 			continue;
353 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
354 			dprintf(stderr, "can't read file %d at %x for pid %d\n",
355 				i, ofiles[i], Pid);
356 			continue;
357 		}
358 		if (file.f_type == DTYPE_VNODE)
359 			vtrans((struct vnode *)file.f_data, i);
360 		else if (file.f_type == DTYPE_SOCKET && checkfile == 0)
361 			socktrans((struct socket *)file.f_data, i);
362 		else {
363 			dprintf(stderr,
364 				"unknown file type %d for file %d of pid %d\n",
365 				file.f_type, i, Pid);
366 		}
367 	}
368 }
369 
370 void
371 vtrans(vp, i)
372 	struct vnode *vp;
373 	int i;
374 {
375 	extern char *devname();
376 	struct vnode vn;
377 	struct filestat fst;
378 	char mode[15];
379 	char *badtype, *filename, *getmnton();
380 
381 	filename = badtype = NULL;
382 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
383 		dprintf(stderr, "can't read vnode at %x for pid %d\n",
384 			vp, Pid);
385 		return;
386 	}
387 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
388 		badtype = "none";
389 	else if (vn.v_type == VBAD)
390 		badtype = "bad";
391 	else
392 		switch (vn.v_tag) {
393 		case VT_UFS:
394 			ufs_filestat(&vn, &fst);
395 			break;
396 		case VT_MFS:
397 			ufs_filestat(&vn, &fst);
398 			break;
399 		case VT_NFS:
400 			nfs_filestat(&vn, &fst);
401 			break;
402 		default: {
403 			static char unknown[10];
404 			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
405 			break;;
406 		}
407 	}
408 	if (checkfile) {
409 		int fsmatch = 0;
410 		register DEVS *d;
411 
412 		if (badtype)
413 			return;
414 		for (d = devs; d != NULL; d = d->next)
415 			if (d->fsid == fst.fsid) {
416 				fsmatch = 1;
417 				if (d->ino == fst.fileid) {
418 					filename = d->name;
419 					break;
420 				}
421 			}
422 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
423 			return;
424 	}
425 	PREFIX(i);
426 	if (badtype) {
427 		(void)printf(" -         -  %10s    -\n", badtype);
428 		return;
429 	}
430 	if (nflg)
431 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
432 	else
433 		(void)printf(" %-8s", getmnton(vn.v_mount));
434 	if (nflg)
435 		(void)sprintf(mode, "%o", fst.mode);
436 	else
437 		strmode(fst.mode, mode);
438 	(void)printf(" %6d %10s", fst.fileid, mode);
439 	switch (vn.v_type) {
440 	case VBLK:
441 	case VCHR: {
442 		char *name;
443 
444 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
445 		    S_IFCHR : S_IFBLK)) == NULL))
446 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
447 		else
448 			printf(" %6s", name);
449 		break;
450 	}
451 	default:
452 		printf(" %6d", fst.size);
453 	}
454 	if (filename && !fsflg)
455 		printf(" %s", filename);
456 
457 	putchar('\n');
458 }
459 
460 void
461 ufs_filestat(vp, fsp)
462 	struct vnode *vp;
463 	struct filestat *fsp;
464 {
465 	struct inode *ip = VTOI(vp);
466 
467 	fsp->fsid = ip->i_dev & 0xffff;
468 	fsp->fileid = (long)ip->i_number;
469 	fsp->mode = (mode_t)ip->i_mode;
470 	fsp->size = (u_long)ip->i_size;
471 	fsp->rdev = ip->i_rdev;
472 }
473 
474 void
475 nfs_filestat(vp, fsp)
476 	struct vnode *vp;
477 	struct filestat *fsp;
478 {
479 	register struct nfsnode *np = VTONFS(vp);
480 	register mode_t mode;
481 
482 	fsp->fsid = np->n_vattr.va_fsid;
483 	fsp->fileid = np->n_vattr.va_fileid;
484 	fsp->size = np->n_size;
485 	fsp->rdev = np->n_vattr.va_rdev;
486 	mode = (mode_t)np->n_vattr.va_mode;
487 	switch (vp->v_type) {
488 	case VREG:
489 		mode |= S_IFREG;
490 		break;
491 	case VDIR:
492 		mode |= S_IFDIR;
493 		break;
494 	case VBLK:
495 		mode |= S_IFBLK;
496 		break;
497 	case VCHR:
498 		mode |= S_IFCHR;
499 		break;
500 	case VLNK:
501 		mode |= S_IFLNK;
502 		break;
503 	case VSOCK:
504 		mode |= S_IFSOCK;
505 		break;
506 	case VFIFO:
507 		mode |= S_IFIFO;
508 		break;
509 	};
510 	fsp->mode = mode;
511 }
512 
513 
514 char *
515 getmnton(m)
516 	struct mount *m;
517 {
518 	static struct mount mount;
519 	static struct mtab {
520 		struct mtab *next;
521 		struct mount *m;
522 		char mntonname[MNAMELEN];
523 	} *mhead = NULL;
524 	register struct mtab *mt;
525 
526 	for (mt = mhead; mt != NULL; mt = mt->next)
527 		if (m == mt->m)
528 			return (mt->mntonname);
529 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
530 		fprintf(stderr, "can't read mount table at %x\n", m);
531 		return (NULL);
532 	}
533 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
534 		fprintf(stderr, "fstat: %s\n", strerror(errno));
535 		exit(1);
536 	}
537 	mt->m = m;
538 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
539 	mt->next = mhead;
540 	mhead = mt;
541 	return (mt->mntonname);
542 }
543 
544 void
545 socktrans(sock, i)
546 	struct socket *sock;
547 	int i;
548 {
549 	static char *stypename[] = {
550 		"unused",	/* 0 */
551 		"stream", 	/* 1 */
552 		"dgram",	/* 2 */
553 		"raw",		/* 3 */
554 		"rdm",		/* 4 */
555 		"seqpak"	/* 5 */
556 	};
557 #define	STYPEMAX 5
558 	struct socket	so;
559 	struct protosw	proto;
560 	struct domain	dom;
561 	struct inpcb	inpcb;
562 	struct unpcb	unpcb;
563 	int len;
564 	char dname[32], *strcpy();
565 
566 	PREFIX(i);
567 
568 	/* fill in socket */
569 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
570 		dprintf(stderr, "can't read sock at %x\n", sock);
571 		goto bad;
572 	}
573 
574 	/* fill in protosw entry */
575 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
576 		dprintf(stderr, "can't read protosw at %x", so.so_proto);
577 		goto bad;
578 	}
579 
580 	/* fill in domain */
581 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
582 		dprintf(stderr, "can't read domain at %x\n", proto.pr_domain);
583 		goto bad;
584 	}
585 
586 	if ((len =
587 	    kvm_read(dom.dom_name, dname, sizeof(dname) - 1)) < 0) {
588 		dprintf(stderr, "can't read domain name at %x\n",
589 			dom.dom_name);
590 		dname[0] = '\0';
591 	}
592 	else
593 		dname[len] = '\0';
594 
595 	if ((u_short)so.so_type > STYPEMAX)
596 		printf("* %s ?%d", dname, so.so_type);
597 	else
598 		printf("* %s %s", dname, stypename[so.so_type]);
599 
600 	/*
601 	 * protocol specific formatting
602 	 *
603 	 * Try to find interesting things to print.  For tcp, the interesting
604 	 * thing is the address of the tcpcb, for udp and others, just the
605 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
606 	 * pcb and the address of the connected pcb (if connected).  Otherwise
607 	 * just print the protocol number and address of the socket itself.
608 	 * The idea is not to duplicate netstat, but to make available enough
609 	 * information for further analysis.
610 	 */
611 	switch(dom.dom_family) {
612 	case AF_INET:
613 		getinetproto(proto.pr_protocol);
614 		if (proto.pr_protocol == IPPROTO_TCP ) {
615 			if (so.so_pcb) {
616 				if (kvm_read(so.so_pcb, &inpcb,
617 				    sizeof(struct inpcb))
618 				    != sizeof(struct inpcb)) {
619 					dprintf(stderr,
620 					    "can't read inpcb at %x\n",
621 					    so.so_pcb);
622 					goto bad;
623 				}
624 				printf(" %x", (int)inpcb.inp_ppcb);
625 			}
626 		}
627 		else if (so.so_pcb)
628 			printf(" %x", (int)so.so_pcb);
629 		break;
630 	case AF_UNIX:
631 		/* print address of pcb and connected pcb */
632 		if (so.so_pcb) {
633 			printf(" %x", (int)so.so_pcb);
634 			if (kvm_read(so.so_pcb, &unpcb,
635 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
636 				dprintf(stderr, "can't read unpcb at %x\n",
637 				    so.so_pcb);
638 				goto bad;
639 			}
640 			if (unpcb.unp_conn) {
641 				char shoconn[4], *cp;
642 
643 				cp = shoconn;
644 				if (!(so.so_state & SS_CANTRCVMORE))
645 					*cp++ = '<';
646 				*cp++ = '-';
647 				if (!(so.so_state & SS_CANTSENDMORE))
648 					*cp++ = '>';
649 				*cp = '\0';
650 				printf(" %s %x", shoconn,
651 				    (int)unpcb.unp_conn);
652 			}
653 		}
654 		break;
655 	default:
656 		/* print protocol number and socket address */
657 		printf(" %d %x", proto.pr_protocol, (int)sock);
658 	}
659 	printf("\n");
660 	return;
661 bad:
662 	printf("* error\n");
663 }
664 
665 /*
666  * getinetproto --
667  *	print name of protocol number
668  */
669 void
670 getinetproto(number)
671 	int number;
672 {
673 	char *cp;
674 
675 	switch(number) {
676 	case IPPROTO_IP:
677 		cp = "ip"; break;
678 	case IPPROTO_ICMP:
679 		cp ="icmp"; break;
680 	case IPPROTO_GGP:
681 		cp ="ggp"; break;
682 	case IPPROTO_TCP:
683 		cp ="tcp"; break;
684 	case IPPROTO_EGP:
685 		cp ="egp"; break;
686 	case IPPROTO_PUP:
687 		cp ="pup"; break;
688 	case IPPROTO_UDP:
689 		cp ="udp"; break;
690 	case IPPROTO_IDP:
691 		cp ="idp"; break;
692 	case IPPROTO_RAW:
693 		cp ="raw"; break;
694 	default:
695 		printf(" %d", number);
696 		return;
697 	}
698 	printf(" %s", cp);
699 }
700 
701 getfname(filename)
702 	char *filename;
703 {
704 	struct stat statbuf;
705 	DEVS *cur;
706 
707 	if (stat(filename, &statbuf)) {
708 		fprintf(stderr, "fstat: %s: %s\n", strerror(errno),
709 		    filename);
710 		return(0);
711 	}
712 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
713 		fprintf(stderr, "fstat: %s\n", strerror(errno));
714 		exit(1);
715 	}
716 	cur->next = devs;
717 	devs = cur;
718 
719 	cur->ino = statbuf.st_ino;
720 	cur->fsid = statbuf.st_dev & 0xffff;
721 	cur->name = filename;
722 	return(1);
723 }
724 
725 void
726 usage()
727 {
728 	(void)fprintf(stderr,
729  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
730 	exit(1);
731 }
732