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