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