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