1 /* $NetBSD: fstat.c,v 1.88 2009/04/12 06:36:12 lukem Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95";
42 __RCSID("$NetBSD: fstat.c,v 1.88 2009/04/12 06:36:12 lukem Exp $");
46 #include <sys/types.h>
47 #include <sys/param.h>
51 #include <sys/vnode.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/domain.h>
55 #include <sys/protosw.h>
56 #include <sys/unpcb.h>
57 #include <sys/sysctl.h>
58 #include <sys/filedesc.h>
62 #include <ufs/ufs/inode.h>
65 #include <sys/mount.h>
68 #include <nfs/nfsproto.h>
69 #include <nfs/rpcv2.h>
71 #include <nfs/nfsnode.h>
73 #include <msdosfs/denode.h>
74 #include <msdosfs/bpb.h>
76 #include <msdosfs/msdosfsmount.h>
79 #include <miscfs/genfs/layer.h>
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>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/in6_pcb.h>
95 #include <arpa/inet.h>
117 typedef struct devs
{
125 static int fsflg
, /* show files on same filesystem as file(s) argument */
126 pflg
, /* show files open by a particular pid */
127 uflg
; /* show files open by a particular (effective) user */
128 static int checkfile
; /* true if restricting to particular files or filesystems */
129 static int nflg
; /* (numerical) display f.s. and rdev as dev_t */
130 int vflg
; /* display errors in locating kernel data objects etc... */
132 static fdfile_t
**ofiles
; /* buffer of pointers to file structures */
133 static int fstat_maxfiles
;
134 #define ALLOC_OFILES(d) \
135 if ((d) > fstat_maxfiles) { \
136 size_t len = (d) * sizeof(fdfile_t *); \
138 ofiles = malloc(len); \
139 if (ofiles == NULL) { \
140 err(1, "malloc(%zu)", len); \
142 fstat_maxfiles = (d); \
147 static const char *const dtypes
[] = {
151 static void dofiles(struct kinfo_proc2
*);
152 static int ext2fs_filestat(struct vnode
*, struct filestat
*);
153 static int getfname(const char *);
154 static void getinetproto(int);
155 static char *getmnton(struct mount
*);
156 static const char *layer_filestat(struct vnode
*, struct filestat
*);
157 static int msdosfs_filestat(struct vnode
*, struct filestat
*);
158 static int nfs_filestat(struct vnode
*, struct filestat
*);
160 static const char *inet6_addrstr(struct in6_addr
*);
162 static void socktrans(struct socket
*, int);
163 static void misctrans(struct file
*);
164 static int ufs_filestat(struct vnode
*, struct filestat
*);
165 static void usage(void) __dead
;
166 static const char *vfilestat(struct vnode
*, struct filestat
*);
167 static void vtrans(struct vnode
*, int, int);
168 static void ftrans(fdfile_t
*, int);
169 static void ptrans(struct file
*, struct pipe
*, int);
172 main(int argc
, char **argv
)
174 struct passwd
*passwd
;
175 struct kinfo_proc2
*p
, *plast
;
178 char buf
[_POSIX2_LINE_MAX
];
180 gid_t egid
= getegid();
182 (void)setegid(getgid());
184 what
= KERN_PROC_ALL
;
185 nlistf
= memf
= NULL
;
186 while ((ch
= getopt(argc
, argv
, "fnp:u:vN:M:")) != -1)
203 if (!isdigit((unsigned char)*optarg
)) {
204 warnx("-p requires a process id");
207 what
= KERN_PROC_PID
;
213 if (!(passwd
= getpwnam(optarg
))) {
214 errx(1, "%s: unknown uid", optarg
);
216 what
= KERN_PROC_UID
;
217 arg
= passwd
->pw_uid
;
227 if (*(argv
+= optind
)) {
228 for (; *argv
; ++argv
) {
232 if (!checkfile
) /* file(s) specified, but none accessible */
236 ALLOC_OFILES(256); /* reserve space for file pointers */
238 if (fsflg
&& !checkfile
) {
239 /* -f with no files means use wd */
240 if (getfname(".") == 0)
246 * Discard setgid privileges. If not the running kernel, we toss
247 * them away totally so that bad guys can't print interesting stuff
248 * from kernel memory, otherwise switch back to kmem for the
249 * duration of the kvm_openfiles() call.
251 if (nlistf
!= NULL
|| memf
!= NULL
)
252 (void)setgid(getgid());
256 if ((kd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
)) == NULL
)
259 /* get rid of it now anyway */
260 if (nlistf
== NULL
&& memf
== NULL
)
261 (void)setgid(getgid());
263 if ((p
= kvm_getproc2(kd
, what
, arg
, sizeof *p
, &cnt
)) == NULL
) {
264 errx(1, "%s", kvm_geterr(kd
));
268 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
271 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
272 if (checkfile
&& fsflg
== 0)
273 (void)printf(" NAME\n");
277 for (plast
= &p
[cnt
]; p
< plast
; ++p
) {
278 if (p
->p_stat
== SZOMB
)
285 static const char *Uname
, *Comm
;
288 #define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
291 (void)printf(" text"); \
294 (void)printf(" wd"); \
297 (void)printf(" root"); \
300 (void)printf(" tr"); \
303 (void)printf(" %4d", i); \
308 * print open files attributed to this process
311 dofiles(struct kinfo_proc2
*p
)
314 struct filedesc filed
;
318 Uname
= user_from_uid(p
->p_uid
, 0);
322 if (p
->p_fd
== 0 || p
->p_cwdi
== 0)
324 if (!KVM_READ(p
->p_fd
, &filed
, sizeof (filed
))) {
325 warnx("can't read filedesc at %p for pid %d", (void *)(uintptr_t)p
->p_fd
, Pid
);
328 if (!KVM_READ(p
->p_cwdi
, &cwdi
, sizeof(cwdi
))) {
329 warnx("can't read cwdinfo at %p for pid %d", (void *)(uintptr_t)p
->p_cwdi
, Pid
);
332 if (!KVM_READ(filed
.fd_dt
, &dt
, sizeof(dt
))) {
333 warnx("can't read dtab at %p for pid %d", filed
.fd_dt
, Pid
);
336 if ((unsigned)filed
.fd_lastfile
>= dt
.dt_nfiles
||
337 filed
.fd_freefile
> filed
.fd_lastfile
+ 1) {
338 dprintf("filedesc corrupted at %p for pid %d", (void *)(uintptr_t)p
->p_fd
, Pid
);
342 * root directory vnode, if one
345 vtrans(cwdi
.cwdi_rdir
, RDIR
, FREAD
);
347 * current working directory vnode
349 vtrans(cwdi
.cwdi_cdir
, CDIR
, FREAD
);
352 * Disable for now, since p->p_tracep appears to point to a ktr_desc *
353 * ktrace vnode, if one
356 ftrans((struct file
*)(intptr_t)p
->p_tracep
, TRACE
);
361 #define FPSIZE (sizeof (fdfile_t *))
362 ALLOC_OFILES(filed
.fd_lastfile
+1);
363 if (!KVM_READ(&filed
.fd_dt
->dt_ff
, ofiles
,
364 (filed
.fd_lastfile
+1) * FPSIZE
)) {
365 dprintf("can't read file structures at %p for pid %d",
366 &filed
.fd_dt
->dt_ff
, Pid
);
369 for (i
= 0; i
<= filed
.fd_lastfile
; i
++) {
370 if (ofiles
[i
] == NULL
)
372 ftrans(ofiles
[i
], i
);
377 ftrans(fdfile_t
*fp
, int i
)
382 if (!KVM_READ(fp
, &fdfile
, sizeof(fdfile
))) {
383 dprintf("can't read file %d at %p for pid %d",
387 if (fdfile
.ff_file
== NULL
)
389 if (!KVM_READ(fdfile
.ff_file
, &file
, sizeof(file
))) {
390 dprintf("can't read file %d at %p for pid %d",
391 i
, fdfile
.ff_file
, Pid
);
394 switch (file
.f_type
) {
396 vtrans((struct vnode
*)file
.f_data
, i
, file
.f_flag
);
400 socktrans((struct socket
*)file
.f_data
, i
);
404 ptrans(&file
, (struct pipe
*)file
.f_data
, i
);
414 dprintf("unknown file type %d for file %d of pid %d",
415 file
.f_type
, i
, Pid
);
421 vfilestat(struct vnode
*vp
, struct filestat
*fsp
)
423 const char *badtype
= NULL
;
425 if (vp
->v_type
== VNON
|| vp
->v_tag
== VT_NON
)
427 else if (vp
->v_type
== VBAD
)
434 if (!ufs_filestat(vp
, fsp
))
438 if (!msdosfs_filestat(vp
, fsp
))
442 if (!nfs_filestat(vp
, fsp
))
446 if (!ext2fs_filestat(vp
, fsp
))
450 if (!isofs_filestat(vp
, fsp
))
454 if (!ntfs_filestat(vp
, fsp
))
458 if (!ptyfs_filestat(vp
, fsp
))
462 if (!tmpfs_filestat(vp
, fsp
))
468 badtype
= layer_filestat(vp
, fsp
);
471 static char unknown
[10];
472 (void)snprintf(unknown
, sizeof unknown
,
482 vtrans(struct vnode
*vp
, int i
, int flag
)
486 char mode
[15], rw
[3];
487 const char *badtype
, *filename
;
490 if (!KVM_READ(vp
, &vn
, sizeof(struct vnode
))) {
491 dprintf("can't read vnode at %p for pid %d", vp
, Pid
);
494 badtype
= vfilestat(&vn
, &fst
);
501 for (d
= devs
; d
!= NULL
; d
= d
->next
)
502 if (d
->fsid
== fst
.fsid
) {
504 if (d
->ino
== fst
.fileid
) {
509 if (fsmatch
== 0 || (filename
== NULL
&& fsflg
== 0))
514 (void)printf(" - - %10s -\n", badtype
);
518 (void)printf(" %2llu,%-2llu",
519 (unsigned long long)major(fst
.fsid
),
520 (unsigned long long)minor(fst
.fsid
));
522 (void)printf(" %-8s", getmnton(vn
.v_mount
));
524 (void)snprintf(mode
, sizeof mode
, "%o", fst
.mode
);
526 strmode(fst
.mode
, mode
);
527 (void)printf(" %7"PRIu64
" %*s", fst
.fileid
, nflg
? 5 : 10, mode
);
533 if (nflg
|| ((name
= devname(fst
.rdev
, vn
.v_type
== VCHR
?
534 S_IFCHR
: S_IFBLK
)) == NULL
))
535 (void)printf(" %2llu,%-2llu",
536 (unsigned long long)major(fst
.rdev
),
537 (unsigned long long)minor(fst
.rdev
));
539 (void)printf(" %6s", name
);
543 (void)printf(" %6lld", (long long)fst
.size
);
547 (void)strlcat(rw
, "r", sizeof(rw
));
549 (void)strlcat(rw
, "w", sizeof(rw
));
550 (void)printf(" %-2s", rw
);
551 if (filename
&& !fsflg
)
552 (void)printf(" %s", filename
);
557 ufs_filestat(struct vnode
*vp
, struct filestat
*fsp
)
561 struct ufs1_dinode dp1
;
562 struct ufs2_dinode dp2
;
565 if (!KVM_READ(VTOI(vp
), &inode
, sizeof (inode
))) {
566 dprintf("can't read inode at %p for pid %d", VTOI(vp
), Pid
);
570 if (!KVM_READ(inode
.i_din
.ffs1_din
, &dip
, sizeof(struct ufs1_dinode
))) {
571 dprintf("can't read dinode at %p for pid %d",
572 inode
.i_din
.ffs1_din
, Pid
);
575 if (inode
.i_size
== dip
.dp1
.di_size
)
576 fsp
->rdev
= dip
.dp1
.di_rdev
;
578 if (!KVM_READ(inode
.i_din
.ffs1_din
, &dip
,
579 sizeof(struct ufs2_dinode
))) {
580 dprintf("can't read dinode at %p for pid %d",
581 inode
.i_din
.ffs1_din
, Pid
);
584 fsp
->rdev
= dip
.dp2
.di_rdev
;
587 fsp
->fsid
= inode
.i_dev
& 0xffff;
588 fsp
->fileid
= inode
.i_number
;
589 fsp
->mode
= (mode_t
)inode
.i_mode
;
590 fsp
->size
= inode
.i_size
;
596 ext2fs_filestat(struct vnode
*vp
, struct filestat
*fsp
)
602 if (!KVM_READ(VTOI(vp
), &inode
, sizeof (inode
))) {
603 dprintf("can't read inode at %p for pid %d", VTOI(vp
), Pid
);
606 fsp
->fsid
= inode
.i_dev
& 0xffff;
607 fsp
->fileid
= inode
.i_number
;
609 if (!KVM_READ(&inode
.i_e2fs_mode
, &mode
, sizeof mode
)) {
610 dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp
),
611 &inode
.i_e2fs_mode
, Pid
);
616 if (!KVM_READ(&inode
.i_e2fs_size
, &size
, sizeof size
)) {
617 dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp
),
618 &inode
.i_e2fs_size
, Pid
);
622 fsp
->rdev
= 0; /* XXX */
627 nfs_filestat(struct vnode
*vp
, struct filestat
*fsp
)
629 struct nfsnode nfsnode
;
632 if (!KVM_READ(VTONFS(vp
), &nfsnode
, sizeof (nfsnode
))) {
633 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp
),
637 if (!KVM_READ(nfsnode
.n_vattr
, &va
, sizeof(va
))) {
638 dprintf("can't read vnode attributes at %p for pid %d",
639 nfsnode
.n_vattr
, Pid
);
642 fsp
->fsid
= va
.va_fsid
;
643 fsp
->fileid
= va
.va_fileid
;
644 fsp
->size
= nfsnode
.n_size
;
645 fsp
->rdev
= va
.va_rdev
;
646 fsp
->mode
= (mode_t
)va
.va_mode
| getftype(vp
->v_type
);
652 msdosfs_filestat(struct vnode
*vp
, struct filestat
*fsp
)
655 struct msdosfsmount mp
;
657 if (!KVM_READ(VTONFS(vp
), &de
, sizeof(de
))) {
658 dprintf("can't read denode at %p for pid %d", VTONFS(vp
),
662 if (!KVM_READ(de
.de_pmp
, &mp
, sizeof(mp
))) {
663 dprintf("can't read mount struct at %p for pid %d", de
.de_pmp
,
668 fsp
->fsid
= de
.de_dev
& 0xffff;
669 fsp
->fileid
= 0; /* XXX see msdosfs_vptofh() for more info */
670 fsp
->size
= de
.de_FileSize
;
671 fsp
->rdev
= 0; /* msdosfs doesn't support device files */
672 fsp
->mode
= (0777 & mp
.pm_mask
) | getftype(vp
->v_type
);
677 layer_filestat(struct vnode
*vp
, struct filestat
*fsp
)
679 struct layer_node layer_node
;
684 if (!KVM_READ(VTOLAYER(vp
), &layer_node
, sizeof(layer_node
))) {
685 dprintf("can't read layer_node at %p for pid %d",
689 if (!KVM_READ(vp
->v_mount
, &mount
, sizeof(struct mount
))) {
690 dprintf("can't read mount struct at %p for pid %d",
694 vp
= layer_node
.layer_lowervp
;
695 if (!KVM_READ(vp
, &vn
, sizeof(struct vnode
))) {
696 dprintf("can't read vnode at %p for pid %d", vp
, Pid
);
699 if ((badtype
= vfilestat(&vn
, fsp
)) == NULL
)
700 fsp
->fsid
= mount
.mnt_stat
.f_fsidx
.__fsid_val
[0];
705 getmnton(struct mount
*m
)
707 static struct mount mount
;
711 char mntonname
[MNAMELEN
];
715 for (mt
= mhead
; mt
!= NULL
; mt
= mt
->next
)
717 return (mt
->mntonname
);
718 if (!KVM_READ(m
, &mount
, sizeof(struct mount
))) {
719 warnx("can't read mount table at %p", m
);
722 if ((mt
= malloc(sizeof (struct mtab
))) == NULL
) {
723 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab
));
726 (void)memmove(&mt
->mntonname
[0], &mount
.mnt_stat
.f_mntonname
[0],
730 return (mt
->mntonname
);
735 inet6_addrstr(struct in6_addr
*p
)
737 struct sockaddr_in6 sin6
;
738 static char hbuf
[NI_MAXHOST
];
739 const int niflags
= NI_NUMERICHOST
;
741 (void)memset(&sin6
, 0, sizeof(sin6
));
742 sin6
.sin6_family
= AF_INET6
;
743 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
745 if (IN6_IS_ADDR_LINKLOCAL(p
) &&
746 *(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2] != 0) {
748 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
749 sin6
.sin6_addr
.s6_addr
[2] = sin6
.sin6_addr
.s6_addr
[3] = 0;
752 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
753 hbuf
, sizeof(hbuf
), NULL
, 0, niflags
))
761 socktrans(struct socket
*sock
, int i
)
763 static const char *stypename
[] = {
773 struct protosw proto
;
777 struct in6pcb in6pcb
;
783 char xaddrbuf
[NI_MAXHOST
+ 2];
789 if (!KVM_READ(sock
, &so
, sizeof(struct socket
))) {
790 dprintf("can't read sock at %p", sock
);
794 /* fill in protosw entry */
795 if (!KVM_READ(so
.so_proto
, &proto
, sizeof(struct protosw
))) {
796 dprintf("can't read protosw at %p", so
.so_proto
);
801 if (!KVM_READ(proto
.pr_domain
, &dom
, sizeof(struct domain
))) {
802 dprintf("can't read domain at %p", proto
.pr_domain
);
806 if ((len
= kvm_read(kd
, (u_long
)dom
.dom_name
, dname
,
807 sizeof(dname
) - 1)) != sizeof(dname
) -1) {
808 dprintf("can't read domain name at %p", dom
.dom_name
);
814 if ((u_short
)so
.so_type
> STYPEMAX
)
815 (void)printf("* %s ?%d", dname
, so
.so_type
);
817 (void)printf("* %s %s", dname
, stypename
[so
.so_type
]);
820 * protocol specific formatting
822 * Try to find interesting things to print. For TCP, the interesting
823 * thing is the address of the tcpcb, for UDP and others, just the
824 * inpcb (socket pcb). For UNIX domain, its the address of the socket
825 * pcb and the address of the connected pcb (if connected). Otherwise
826 * just print the protocol number and address of the socket itself.
827 * The idea is not to duplicate netstat, but to make available enough
828 * information for further analysis.
830 switch(dom
.dom_family
) {
832 getinetproto(proto
.pr_protocol
);
833 if (proto
.pr_protocol
== IPPROTO_TCP
) {
834 if (so
.so_pcb
== NULL
)
836 if (kvm_read(kd
, (u_long
)so
.so_pcb
, (char *)&inpcb
,
837 sizeof(struct inpcb
)) != sizeof(struct inpcb
)) {
838 dprintf("can't read inpcb at %p", so
.so_pcb
);
841 (void)printf(" %lx", (long)inpcb
.inp_ppcb
);
842 (void)printf(" %s:%d",
843 inpcb
.inp_laddr
.s_addr
== INADDR_ANY
? "*" :
844 inet_ntoa(inpcb
.inp_laddr
), ntohs(inpcb
.inp_lport
));
845 if (inpcb
.inp_fport
) {
846 (void)printf(" <-> %s:%d",
847 inpcb
.inp_faddr
.s_addr
== INADDR_ANY
? "*" :
848 inet_ntoa(inpcb
.inp_faddr
),
849 ntohs(inpcb
.inp_fport
));
851 } else if (proto
.pr_protocol
== IPPROTO_UDP
) {
852 if (so
.so_pcb
== NULL
)
854 if (kvm_read(kd
, (u_long
)so
.so_pcb
, (char *)&inpcb
,
855 sizeof(struct inpcb
)) != sizeof(struct inpcb
)) {
856 dprintf("can't read inpcb at %p", so
.so_pcb
);
859 (void)printf(" %lx", (long)so
.so_pcb
);
860 (void)printf(" %s:%d",
861 inpcb
.inp_laddr
.s_addr
== INADDR_ANY
? "*" :
862 inet_ntoa(inpcb
.inp_laddr
), ntohs(inpcb
.inp_lport
));
864 (void)printf(" <-> %s:%d",
865 inpcb
.inp_faddr
.s_addr
== INADDR_ANY
? "*" :
866 inet_ntoa(inpcb
.inp_faddr
),
867 ntohs(inpcb
.inp_fport
));
868 } else if (so
.so_pcb
)
869 (void)printf(" %lx", (long)so
.so_pcb
);
873 getinetproto(proto
.pr_protocol
);
874 if (proto
.pr_protocol
== IPPROTO_TCP
) {
875 if (so
.so_pcb
== NULL
)
877 if (kvm_read(kd
, (u_long
)so
.so_pcb
, (char *)&in6pcb
,
878 sizeof(struct in6pcb
)) != sizeof(struct in6pcb
)) {
879 dprintf("can't read in6pcb at %p", so
.so_pcb
);
882 (void)printf(" %lx", (long)in6pcb
.in6p_ppcb
);
883 (void)snprintf(xaddrbuf
, sizeof(xaddrbuf
), "[%s]",
884 inet6_addrstr(&in6pcb
.in6p_laddr
));
885 (void)printf(" %s:%d",
886 IN6_IS_ADDR_UNSPECIFIED(&in6pcb
.in6p_laddr
) ? "*" :
888 ntohs(in6pcb
.in6p_lport
));
889 if (in6pcb
.in6p_fport
) {
890 (void)snprintf(xaddrbuf
, sizeof(xaddrbuf
),
891 "[%s]", inet6_addrstr(&in6pcb
.in6p_faddr
));
892 (void)printf(" <-> %s:%d",
893 IN6_IS_ADDR_UNSPECIFIED(&in6pcb
.in6p_faddr
) ? "*" :
895 ntohs(in6pcb
.in6p_fport
));
897 } else if (proto
.pr_protocol
== IPPROTO_UDP
) {
898 if (so
.so_pcb
== NULL
)
900 if (kvm_read(kd
, (u_long
)so
.so_pcb
, (char *)&in6pcb
,
901 sizeof(struct in6pcb
)) != sizeof(struct in6pcb
)) {
902 dprintf("can't read inpcb at %p", so
.so_pcb
);
905 (void)printf(" %lx", (long)so
.so_pcb
);
906 (void)snprintf(xaddrbuf
, sizeof(xaddrbuf
), "[%s]",
907 inet6_addrstr(&in6pcb
.in6p_laddr
));
908 (void)printf(" %s:%d",
909 IN6_IS_ADDR_UNSPECIFIED(&in6pcb
.in6p_laddr
) ? "*" :
911 ntohs(in6pcb
.in6p_lport
));
912 if (in6pcb
.in6p_fport
) {
913 (void)snprintf(xaddrbuf
, sizeof(xaddrbuf
), "[%s]",
914 inet6_addrstr(&in6pcb
.in6p_faddr
));
915 (void)printf(" <-> %s:%d",
916 IN6_IS_ADDR_UNSPECIFIED(&in6pcb
.in6p_faddr
) ? "*" :
918 ntohs(in6pcb
.in6p_fport
));
920 } else if (so
.so_pcb
)
921 (void)printf(" %lx", (long)so
.so_pcb
);
925 /* print address of pcb and connected pcb */
927 (void)printf(" %lx", (long)so
.so_pcb
);
928 if (kvm_read(kd
, (u_long
)so
.so_pcb
, (char *)&unpcb
,
929 sizeof(struct unpcb
)) != sizeof(struct unpcb
)){
930 dprintf("can't read unpcb at %p", so
.so_pcb
);
933 if (unpcb
.unp_conn
) {
934 char shoconn
[4], *cp
;
937 if (!(so
.so_state
& SS_CANTRCVMORE
))
940 if (!(so
.so_state
& SS_CANTSENDMORE
))
943 (void)printf(" %s %lx", shoconn
,
944 (long)unpcb
.unp_conn
);
949 /* print protocol number and socket address */
950 (void)printf(" %d %lx", proto
.pr_protocol
, (long)sock
);
955 (void)printf("* error\n");
959 ptrans(struct file
*fp
, struct pipe
*cpipe
, int i
)
966 if (!KVM_READ(cpipe
, &cp
, sizeof(struct pipe
))) {
967 dprintf("can't read pipe at %p", cpipe
);
971 /* pipe descriptor is either read or write, never both */
972 (void)printf("* pipe %p %s %p %s%s%s", cpipe
,
973 (fp
->f_flag
& FWRITE
) ? "->" : "<-",
975 (fp
->f_flag
& FWRITE
) ? "w" : "r",
976 (fp
->f_flag
& FNONBLOCK
) ? "n" : "",
977 (cp
.pipe_state
& PIPE_ASYNC
) ? "a" : "");
981 (void)printf("* error\n");
985 misctrans(struct file
*file
)
988 PREFIX((int)file
->f_type
);
989 pmisc(file
, dtypes
[file
->f_type
]);
994 * print name of protocol number
997 getinetproto(int number
)
1020 case IPPROTO_ICMPV6
:
1023 (void)printf(" %d", number
);
1026 (void)printf(" %s", cp
);
1030 getfname(const char *filename
)
1032 struct stat statbuf
;
1035 if (stat(filename
, &statbuf
)) {
1036 warn("stat(%s)", filename
);
1039 if ((cur
= malloc(sizeof(DEVS
))) == NULL
) {
1040 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS
));
1045 cur
->ino
= statbuf
.st_ino
;
1046 cur
->fsid
= statbuf
.st_dev
& 0xffff;
1047 cur
->name
= filename
;
1052 getftype(enum vtype v_type
)
1089 (void)fprintf(stderr
, "Usage: %s [-fnv] [-p pid] [-u user] "
1090 "[-N system] [-M core] [file ...]\n", getprogname());