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