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