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