]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/fstat/fstat.c
This commit was generated by cvs2svn to compensate for changes in r175801,
[FreeBSD/FreeBSD.git] / usr.bin / fstat / fstat.c
1 /*-
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)fstat.c     8.3 (Berkeley) 5/2/95";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include <sys/param.h>
49 #include <sys/time.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52 #include <sys/stat.h>
53 #include <sys/vnode.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
58 #include <sys/un.h>
59 #include <sys/unpcb.h>
60 #include <sys/sysctl.h>
61 #include <sys/filedesc.h>
62 #include <sys/queue.h>
63 #define _KERNEL
64 #include <sys/pipe.h>
65 #include <sys/conf.h>
66 #include <sys/file.h>
67 #include <sys/mount.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/inode.h>
70 #include <fs/devfs/devfs.h>
71 #include <fs/devfs/devfs_int.h>
72 #undef _KERNEL
73 #include <nfs/nfsproto.h>
74 #include <nfs/rpcv2.h>
75 #include <nfsclient/nfs.h>
76 #include <nfsclient/nfsnode.h>
77
78
79 #include <vm/vm.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_object.h>
82
83 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88
89 #include <ctype.h>
90 #include <err.h>
91 #include <fcntl.h>
92 #include <kvm.h>
93 #include <limits.h>
94 #include <nlist.h>
95 #include <paths.h>
96 #include <pwd.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <netdb.h>
102
103 #include "fstat.h"
104
105 #define TEXT    -1
106 #define CDIR    -2
107 #define RDIR    -3
108 #define TRACE   -4
109 #define MMAP    -5
110 #define JDIR    -6
111
112 DEVS *devs;
113
114 #ifdef notdef
115 struct nlist nl[] = {
116         { "" },
117 };
118 #endif
119
120 int     fsflg,  /* show files on same filesystem as file(s) argument */
121         pflg,   /* show files open by a particular pid */
122         uflg;   /* show files open by a particular (effective) user */
123 int     checkfile; /* true if restricting to particular files or filesystems */
124 int     nflg;   /* (numerical) display f.s. and rdev as dev_t */
125 int     vflg;   /* display errors in locating kernel data objects etc... */
126 int     mflg;   /* include memory-mapped files */
127
128
129 struct file **ofiles;   /* buffer of pointers to file structures */
130 int maxfiles;
131 #define ALLOC_OFILES(d) \
132         if ((d) > maxfiles) { \
133                 free(ofiles); \
134                 ofiles = malloc((d) * sizeof(struct file *)); \
135                 if (ofiles == NULL) { \
136                         err(1, NULL); \
137                 } \
138                 maxfiles = (d); \
139         }
140
141 char *memf, *nlistf;
142 kvm_t *kd;
143
144 static void fstat_kvm(int, int);
145 static void fstat_sysctl(int, int);
146 void dofiles(struct kinfo_proc *kp);
147 void dommap(struct kinfo_proc *kp);
148 void vtrans(struct vnode *vp, int i, int flag);
149 int  ufs_filestat(struct vnode *vp, struct filestat *fsp);
150 int  nfs_filestat(struct vnode *vp, struct filestat *fsp);
151 int  devfs_filestat(struct vnode *vp, struct filestat *fsp);
152 char *getmnton(struct mount *m);
153 void pipetrans(struct pipe *pi, int i, int flag);
154 void socktrans(struct socket *sock, int i);
155 void getinetproto(int number);
156 int  getfname(const char *filename);
157 void usage(void);
158 char *kdevtoname(struct cdev *dev);
159
160 int
161 main(int argc, char **argv)
162 {
163         struct passwd *passwd;
164         int arg, ch, what;
165
166         arg = 0;
167         what = KERN_PROC_PROC;
168         nlistf = memf = NULL;
169         while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
170                 switch((char)ch) {
171                 case 'f':
172                         fsflg = 1;
173                         break;
174                 case 'M':
175                         memf = optarg;
176                         break;
177                 case 'N':
178                         nlistf = optarg;
179                         break;
180                 case 'm':
181                         mflg = 1;
182                         break;
183                 case 'n':
184                         nflg = 1;
185                         break;
186                 case 'p':
187                         if (pflg++)
188                                 usage();
189                         if (!isdigit(*optarg)) {
190                                 warnx("-p requires a process id");
191                                 usage();
192                         }
193                         what = KERN_PROC_PID;
194                         arg = atoi(optarg);
195                         break;
196                 case 'u':
197                         if (uflg++)
198                                 usage();
199                         if (!(passwd = getpwnam(optarg)))
200                                 errx(1, "%s: unknown uid", optarg);
201                         what = KERN_PROC_UID;
202                         arg = passwd->pw_uid;
203                         break;
204                 case 'v':
205                         vflg = 1;
206                         break;
207                 case '?':
208                 default:
209                         usage();
210                 }
211
212         if (*(argv += optind)) {
213                 for (; *argv; ++argv) {
214                         if (getfname(*argv))
215                                 checkfile = 1;
216                 }
217                 if (!checkfile) /* file(s) specified, but none accessable */
218                         exit(1);
219         }
220
221         if (fsflg && !checkfile) {
222                 /* -f with no files means use wd */
223                 if (getfname(".") == 0)
224                         exit(1);
225                 checkfile = 1;
226         }
227
228         if (memf != NULL)
229                 fstat_kvm(what, arg);
230         else
231                 fstat_sysctl(what, arg);
232         exit(0);
233 }
234
235 static void
236 print_header(void)
237 {
238
239         if (nflg)
240                 printf("%s",
241 "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
242         else
243                 printf("%s",
244 "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
245         if (checkfile && fsflg == 0)
246                 printf(" NAME\n");
247         else
248                 putchar('\n');
249 }
250
251 static void
252 fstat_kvm(int what, int arg)
253 {
254         struct kinfo_proc *p, *plast;
255         char buf[_POSIX2_LINE_MAX];
256         int cnt;
257
258         ALLOC_OFILES(256);      /* reserve space for file pointers */
259
260         /*
261          * Discard setgid privileges if not the running kernel so that bad
262          * guys can't print interesting stuff from kernel memory.
263          */
264         if (nlistf != NULL || memf != NULL)
265                 setgid(getgid());
266
267         if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
268                 errx(1, "%s", buf);
269         setgid(getgid());
270 #ifdef notdef
271         if (kvm_nlist(kd, nl) != 0)
272                 errx(1, "no namelist: %s", kvm_geterr(kd));
273 #endif
274         if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
275                 errx(1, "%s", kvm_geterr(kd));
276         print_header();
277         for (plast = &p[cnt]; p < plast; ++p) {
278                 if (p->ki_stat == SZOMB)
279                         continue;
280                 dofiles(p);
281                 if (mflg)
282                         dommap(p);
283         }
284 }
285
286 static void
287 fstat_sysctl(int what, int arg)
288 {
289
290         /* not yet implemented */
291         fstat_kvm(what, arg);
292 }
293
294 const char      *Uname, *Comm;
295 int     Pid;
296
297 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
298         switch(i) { \
299         case TEXT: \
300                 printf(" text"); \
301                 break; \
302         case CDIR: \
303                 printf("   wd"); \
304                 break; \
305         case RDIR: \
306                 printf(" root"); \
307                 break; \
308         case TRACE: \
309                 printf("   tr"); \
310                 break; \
311         case MMAP: \
312                 printf(" mmap"); \
313                 break; \
314         case JDIR: \
315                 printf(" jail"); \
316                 break; \
317         default: \
318                 printf(" %4d", i); \
319                 break; \
320         }
321
322 /*
323  * print open files attributed to this process
324  */
325 void
326 dofiles(struct kinfo_proc *kp)
327 {
328         int i;
329         struct file file;
330         struct filedesc filed;
331
332         Uname = user_from_uid(kp->ki_uid, 0);
333         Pid = kp->ki_pid;
334         Comm = kp->ki_comm;
335
336         if (kp->ki_fd == NULL)
337                 return;
338         if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) {
339                 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
340                     (void *)kp->ki_fd, Pid);
341                 return;
342         }
343         /*
344          * root directory vnode, if one
345          */
346         if (filed.fd_rdir)
347                 vtrans(filed.fd_rdir, RDIR, FREAD);
348         /*
349          * current working directory vnode
350          */
351         vtrans(filed.fd_cdir, CDIR, FREAD);
352         /*
353          * jail root, if any.
354          */
355         if (filed.fd_jdir)
356                 vtrans(filed.fd_jdir, JDIR, FREAD);
357         /*
358          * ktrace vnode, if one
359          */
360         if (kp->ki_tracep)
361                 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
362         /*
363          * text vnode, if one
364          */
365         if (kp->ki_textvp)
366                 vtrans(kp->ki_textvp, TEXT, FREAD);
367         /*
368          * open files
369          */
370 #define FPSIZE  (sizeof (struct file *))
371 #define MAX_LASTFILE    (0x1000000)
372
373         /* Sanity check on filed.fd_lastfile */
374         if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE)
375                 return;
376
377         ALLOC_OFILES(filed.fd_lastfile+1);
378         if (!KVM_READ(filed.fd_ofiles, ofiles,
379             (filed.fd_lastfile+1) * FPSIZE)) {
380                 dprintf(stderr,
381                     "can't read file structures at %p for pid %d\n",
382                     (void *)filed.fd_ofiles, Pid);
383                 return;
384         }
385         for (i = 0; i <= filed.fd_lastfile; i++) {
386                 if (ofiles[i] == NULL)
387                         continue;
388                 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
389                         dprintf(stderr, "can't read file %d at %p for pid %d\n",
390                             i, (void *)ofiles[i], Pid);
391                         continue;
392                 }
393                 if (file.f_type == DTYPE_VNODE)
394                         vtrans(file.f_vnode, i, file.f_flag);
395                 else if (file.f_type == DTYPE_SOCKET) {
396                         if (checkfile == 0)
397                                 socktrans(file.f_data, i);
398                 }
399 #ifdef DTYPE_PIPE
400                 else if (file.f_type == DTYPE_PIPE) {
401                         if (checkfile == 0)
402                                 pipetrans(file.f_data, i, file.f_flag);
403                 }
404 #endif
405 #ifdef DTYPE_FIFO
406                 else if (file.f_type == DTYPE_FIFO) {
407                         if (checkfile == 0)
408                                 vtrans(file.f_vnode, i, file.f_flag);
409                 }
410 #endif
411                 else {
412                         dprintf(stderr,
413                             "unknown file type %d for file %d of pid %d\n",
414                             file.f_type, i, Pid);
415                 }
416         }
417 }
418
419 void
420 dommap(struct kinfo_proc *kp)
421 {
422         vm_map_t map;
423         struct vmspace vmspace;
424         struct vm_map_entry entry;
425         vm_map_entry_t entryp;
426         struct vm_object object;
427         vm_object_t objp;
428         int prot, fflags;
429
430         if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
431                 dprintf(stderr,
432                     "can't read vmspace at %p for pid %d\n",
433                     (void *)kp->ki_vmspace, Pid);
434                 return;
435         }
436         map = &vmspace.vm_map;
437
438         for (entryp = map->header.next;
439             entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
440                 if (!KVM_READ(entryp, &entry, sizeof(entry))) {
441                         dprintf(stderr,
442                             "can't read vm_map_entry at %p for pid %d\n",
443                             (void *)entryp, Pid);
444                         return;
445                 }
446
447                 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
448                         continue;
449
450                 if ((objp = entry.object.vm_object) == NULL)
451                         continue;
452
453                 for (; objp; objp = object.backing_object) {
454                         if (!KVM_READ(objp, &object, sizeof(object))) {
455                                 dprintf(stderr,
456                                     "can't read vm_object at %p for pid %d\n",
457                                     (void *)objp, Pid);
458                                 return;
459                         }
460                 }
461
462                 prot = entry.protection;
463                 fflags = (prot & VM_PROT_READ ? FREAD : 0) |
464                     (prot & VM_PROT_WRITE ? FWRITE : 0);
465
466                 switch (object.type) {
467                 case OBJT_VNODE:
468                         vtrans((struct vnode *)object.handle, MMAP, fflags);
469                         break;
470                 default:
471                         break;
472                 }
473         }
474 }
475
476 char *
477 kdevtoname(struct cdev *dev)
478 {
479         struct cdev si;
480
481         if (!KVM_READ(dev, &si, sizeof si))
482                 return (NULL);
483         return (strdup(si.__si_namebuf));
484 }
485
486 void
487 vtrans(struct vnode *vp, int i, int flag)
488 {
489         struct vnode vn;
490         struct filestat fst;
491         char rw[3], mode[15], tagstr[12], *tagptr;
492         const char *badtype, *filename;
493
494         filename = badtype = NULL;
495         if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
496                 dprintf(stderr, "can't read vnode at %p for pid %d\n",
497                     (void *)vp, Pid);
498                 return;
499         }
500         if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
501             !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
502                 dprintf(stderr, "can't read v_tag at %p for pid %d\n",
503                     (void *)vp, Pid);
504                 return;
505         }
506         tagstr[sizeof(tagstr) - 1] = '\0';
507         if (vn.v_type == VNON)
508                 badtype = "none";
509         else if (vn.v_type == VBAD)
510                 badtype = "bad";
511         else {
512                 if (!strcmp("ufs", tagstr)) {
513                         if (!ufs_filestat(&vn, &fst))
514                                 badtype = "error";
515                 } else if (!strcmp("devfs", tagstr)) {
516                         if (!devfs_filestat(&vn, &fst))
517                                 badtype = "error";
518                 } else if (!strcmp("nfs", tagstr)) {
519                         if (!nfs_filestat(&vn, &fst))
520                                 badtype = "error";
521                 } else if (!strcmp("msdosfs", tagstr)) {
522                         if (!msdosfs_filestat(&vn, &fst))
523                                 badtype = "error";
524                 } else if (!strcmp("isofs", tagstr)) {
525                         if (!isofs_filestat(&vn, &fst))
526                                 badtype = "error";
527 #ifdef ZFS
528                 } else if (!strcmp("zfs", tagstr)) {
529                         if (!zfs_filestat(&vn, &fst))
530                                 badtype = "error";
531 #endif
532                 } else {
533                         static char unknown[32];
534                         snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
535                         badtype = unknown;
536                 }
537         }
538         if (checkfile) {
539                 int fsmatch = 0;
540                 DEVS *d;
541
542                 if (badtype)
543                         return;
544                 for (d = devs; d != NULL; d = d->next)
545                         if (d->fsid == fst.fsid) {
546                                 fsmatch = 1;
547                                 if (d->ino == fst.fileid) {
548                                         filename = d->name;
549                                         break;
550                                 }
551                         }
552                 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
553                         return;
554         }
555         PREFIX(i);
556         if (badtype) {
557                 (void)printf(" -         -  %10s    -\n", badtype);
558                 return;
559         }
560         if (nflg)
561                 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
562         else
563                 (void)printf(" %-8s", getmnton(vn.v_mount));
564         if (nflg)
565                 (void)sprintf(mode, "%o", fst.mode);
566         else
567                 strmode(fst.mode, mode);
568         (void)printf(" %6ld %10s", fst.fileid, mode);
569         switch (vn.v_type) {
570         case VBLK:
571         case VCHR: {
572                 char *name;
573
574                 name = kdevtoname(vn.v_rdev);
575                 if (nflg || !name)
576                         printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
577                 else {
578                         printf(" %6s", name);
579                         free(name);
580                 }
581                 break;
582         }
583         default:
584                 printf(" %6lu", fst.size);
585         }
586         rw[0] = '\0';
587         if (flag & FREAD)
588                 strcat(rw, "r");
589         if (flag & FWRITE)
590                 strcat(rw, "w");
591         printf(" %2s", rw);
592         if (filename && !fsflg)
593                 printf("  %s", filename);
594         putchar('\n');
595 }
596
597 int
598 ufs_filestat(struct vnode *vp, struct filestat *fsp)
599 {
600         struct inode inode;
601
602         if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
603                 dprintf(stderr, "can't read inode at %p for pid %d\n",
604                     (void *)VTOI(vp), Pid);
605                 return 0;
606         }
607         /*
608          * The st_dev from stat(2) is a dev_t. These kernel structures
609          * contain cdev pointers. We need to convert to dev_t to make
610          * comparisons
611          */
612         fsp->fsid = dev2udev(inode.i_dev);
613         fsp->fileid = (long)inode.i_number;
614         fsp->mode = (mode_t)inode.i_mode;
615         fsp->size = (u_long)inode.i_size;
616 #if should_be_but_is_hard
617         /* XXX - need to load i_ump and i_din[12] from kernel memory */
618         if (inode.i_ump->um_fstype == UFS1)
619                 fsp->rdev = inode.i_din1->di_rdev;
620         else
621                 fsp->rdev = inode.i_din2->di_rdev;
622 #else
623         fsp->rdev = 0;
624 #endif
625
626         return 1;
627 }
628
629 int
630 devfs_filestat(struct vnode *vp, struct filestat *fsp)
631 {
632         struct devfs_dirent devfs_dirent;
633         struct mount mount;
634         struct vnode vnode;
635
636         if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
637                 dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
638                     (void *)vp->v_data, Pid);
639                 return 0;
640         }
641         if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
642                 dprintf(stderr, "can't read mount at %p for pid %d\n",
643                     (void *)vp->v_mount, Pid);
644                 return 0;
645         }
646         if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
647                 dprintf(stderr, "can't read vnode at %p for pid %d\n",
648                     (void *)devfs_dirent.de_vnode, Pid);
649                 return 0;
650         }
651         fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
652         fsp->fileid = devfs_dirent.de_inode;
653         fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
654         fsp->size = 0;
655         fsp->rdev = dev2udev(vnode.v_rdev);
656
657         return 1;
658 }
659
660 int
661 nfs_filestat(struct vnode *vp, struct filestat *fsp)
662 {
663         struct nfsnode nfsnode;
664         mode_t mode;
665
666         if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
667                 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
668                     (void *)VTONFS(vp), Pid);
669                 return 0;
670         }
671         fsp->fsid = nfsnode.n_vattr.va_fsid;
672         fsp->fileid = nfsnode.n_vattr.va_fileid;
673         fsp->size = nfsnode.n_size;
674         fsp->rdev = nfsnode.n_vattr.va_rdev;
675         mode = (mode_t)nfsnode.n_vattr.va_mode;
676         switch (vp->v_type) {
677         case VREG:
678                 mode |= S_IFREG;
679                 break;
680         case VDIR:
681                 mode |= S_IFDIR;
682                 break;
683         case VBLK:
684                 mode |= S_IFBLK;
685                 break;
686         case VCHR:
687                 mode |= S_IFCHR;
688                 break;
689         case VLNK:
690                 mode |= S_IFLNK;
691                 break;
692         case VSOCK:
693                 mode |= S_IFSOCK;
694                 break;
695         case VFIFO:
696                 mode |= S_IFIFO;
697                 break;
698         case VNON:
699         case VBAD:
700         case VMARKER:
701                 return 0;
702         };
703         fsp->mode = mode;
704
705         return 1;
706 }
707
708
709 char *
710 getmnton(struct mount *m)
711 {
712         static struct mount mount;
713         static struct mtab {
714                 struct mtab *next;
715                 struct mount *m;
716                 char mntonname[MNAMELEN];
717         } *mhead = NULL;
718         struct mtab *mt;
719
720         for (mt = mhead; mt != NULL; mt = mt->next)
721                 if (m == mt->m)
722                         return (mt->mntonname);
723         if (!KVM_READ(m, &mount, sizeof(struct mount))) {
724                 warnx("can't read mount table at %p", (void *)m);
725                 return (NULL);
726         }
727         if ((mt = malloc(sizeof (struct mtab))) == NULL)
728                 err(1, NULL);
729         mt->m = m;
730         bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
731         mt->next = mhead;
732         mhead = mt;
733         return (mt->mntonname);
734 }
735
736 void
737 pipetrans(struct pipe *pi, int i, int flag)
738 {
739         struct pipe pip;
740         char rw[3];
741
742         PREFIX(i);
743
744         /* fill in socket */
745         if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
746                 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
747                 goto bad;
748         }
749
750         printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
751         printf(" %6d", (int)pip.pipe_buffer.cnt);
752         rw[0] = '\0';
753         if (flag & FREAD)
754                 strcat(rw, "r");
755         if (flag & FWRITE)
756                 strcat(rw, "w");
757         printf(" %2s", rw);
758         putchar('\n');
759         return;
760
761 bad:
762         printf("* error\n");
763 }
764
765 void
766 socktrans(struct socket *sock, int i)
767 {
768         static const char *stypename[] = {
769                 "unused",       /* 0 */
770                 "stream",       /* 1 */
771                 "dgram",        /* 2 */
772                 "raw",          /* 3 */
773                 "rdm",          /* 4 */
774                 "seqpak"        /* 5 */
775         };
776 #define STYPEMAX 5
777         struct socket   so;
778         struct protosw  proto;
779         struct domain   dom;
780         struct inpcb    inpcb;
781         struct unpcb    unpcb;
782         int len;
783         char dname[32];
784
785         PREFIX(i);
786
787         /* fill in socket */
788         if (!KVM_READ(sock, &so, sizeof(struct socket))) {
789                 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
790                 goto bad;
791         }
792
793         /* fill in protosw entry */
794         if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
795                 dprintf(stderr, "can't read protosw at %p",
796                     (void *)so.so_proto);
797                 goto bad;
798         }
799
800         /* fill in domain */
801         if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
802                 dprintf(stderr, "can't read domain at %p\n",
803                     (void *)proto.pr_domain);
804                 goto bad;
805         }
806
807         if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
808             sizeof(dname) - 1)) < 0) {
809                 dprintf(stderr, "can't read domain name at %p\n",
810                     (void *)dom.dom_name);
811                 dname[0] = '\0';
812         }
813         else
814                 dname[len] = '\0';
815
816         if ((u_short)so.so_type > STYPEMAX)
817                 printf("* %s ?%d", dname, so.so_type);
818         else
819                 printf("* %s %s", dname, stypename[so.so_type]);
820
821         /*
822          * protocol specific formatting
823          *
824          * Try to find interesting things to print.  For tcp, the interesting
825          * thing is the address of the tcpcb, for udp and others, just the
826          * inpcb (socket pcb).  For unix domain, its the address of the socket
827          * pcb and the address of the connected pcb (if connected).  Otherwise
828          * just print the protocol number and address of the socket itself.
829          * The idea is not to duplicate netstat, but to make available enough
830          * information for further analysis.
831          */
832         switch(dom.dom_family) {
833         case AF_INET:
834         case AF_INET6:
835                 getinetproto(proto.pr_protocol);
836                 if (proto.pr_protocol == IPPROTO_TCP ) {
837                         if (so.so_pcb) {
838                                 if (kvm_read(kd, (u_long)so.so_pcb,
839                                     (char *)&inpcb, sizeof(struct inpcb))
840                                     != sizeof(struct inpcb)) {
841                                         dprintf(stderr,
842                                             "can't read inpcb at %p\n",
843                                             (void *)so.so_pcb);
844                                         goto bad;
845                                 }
846                                 printf(" %lx", (u_long)inpcb.inp_ppcb);
847                         }
848                 }
849                 else if (so.so_pcb)
850                         printf(" %lx", (u_long)so.so_pcb);
851                 break;
852         case AF_UNIX:
853                 /* print address of pcb and connected pcb */
854                 if (so.so_pcb) {
855                         printf(" %lx", (u_long)so.so_pcb);
856                         if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
857                             sizeof(struct unpcb)) != sizeof(struct unpcb)){
858                                 dprintf(stderr, "can't read unpcb at %p\n",
859                                     (void *)so.so_pcb);
860                                 goto bad;
861                         }
862                         if (unpcb.unp_conn) {
863                                 char shoconn[4], *cp;
864
865                                 cp = shoconn;
866                                 if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE))
867                                         *cp++ = '<';
868                                 *cp++ = '-';
869                                 if (!(so.so_snd.sb_state & SBS_CANTSENDMORE))
870                                         *cp++ = '>';
871                                 *cp = '\0';
872                                 printf(" %s %lx", shoconn,
873                                     (u_long)unpcb.unp_conn);
874                         }
875                 }
876                 break;
877         default:
878                 /* print protocol number and socket address */
879                 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
880         }
881         printf("\n");
882         return;
883 bad:
884         printf("* error\n");
885 }
886
887
888 /*
889  * Read the cdev structure in the kernel in order to work out the
890  * associated dev_t
891  */
892 dev_t
893 dev2udev(struct cdev *dev)
894 {
895         struct cdev_priv priv;
896         struct cdev si;
897
898         if (KVM_READ(dev, &si, sizeof si) &&
899             KVM_READ(si.si_priv, &priv, sizeof priv)) {
900                 return ((dev_t)priv.cdp_inode);
901         } else {
902                 dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
903                 return -1;
904         }
905 }
906
907 /*
908  * getinetproto --
909  *      print name of protocol number
910  */
911 void
912 getinetproto(int number)
913 {
914         static int isopen;
915         struct protoent *pe;
916
917         if (!isopen)
918                 setprotoent(++isopen);
919         if ((pe = getprotobynumber(number)) != NULL)
920                 printf(" %s", pe->p_name);
921         else
922                 printf(" %d", number);
923 }
924
925 int
926 getfname(const char *filename)
927 {
928         struct stat statbuf;
929         DEVS *cur;
930
931         if (stat(filename, &statbuf)) {
932                 warn("%s", filename);
933                 return(0);
934         }
935         if ((cur = malloc(sizeof(DEVS))) == NULL)
936                 err(1, NULL);
937         cur->next = devs;
938         devs = cur;
939
940         cur->ino = statbuf.st_ino;
941         cur->fsid = statbuf.st_dev;
942         cur->name = filename;
943         return(1);
944 }
945
946 #ifdef ZFS
947 void *
948 getvnodedata(struct vnode *vp)
949 {
950         return (vp->v_data);
951 }
952
953 struct mount *
954 getvnodemount(struct vnode *vp)
955 {
956         return (vp->v_mount);
957 }
958 #endif
959
960 void
961 usage(void)
962 {
963         (void)fprintf(stderr,
964  "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");
965         exit(1);
966 }