2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.25 2007/05/09 04:33:51 dillon Exp $
39 #define _KERNEL_STRUCTURES
41 #include <sys/param.h>
45 #include <sys/vnode.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
51 #include <sys/unpcb.h>
52 #include <sys/sysctl.h>
53 #include <sys/filedesc.h>
54 #include <sys/queue.h>
58 #include <sys/ktrace.h>
59 #include <vfs/ufs/quota.h>
60 #include <vfs/ufs/inode.h>
61 #include <sys/mount.h>
62 #include <sys/namecache.h>
63 #include <nfs/nfsproto.h>
64 #include <nfs/rpcv2.h>
66 #include <nfs/nfsnode.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
73 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/in_pcb.h>
103 static void make_printable(char *buf
, int len
);
106 struct nlist nl
[] = {
111 int fsflg
, /* show files on same filesystem as file(s) argument */
112 pflg
, /* show files open by a particular pid */
113 uflg
; /* show files open by a particular (effective) user */
114 int checkfile
; /* true if restricting to particular files or filesystems */
115 int nflg
; /* (numerical) display f.s. and rdev as dev_t */
116 int vflg
; /* display errors in locating kernel data objects etc... */
117 int mflg
; /* include memory-mapped files */
124 struct fdnode
*ofiles
; /* buffer of pointers to file structures */
127 #define ALLOC_OFILES(d) \
128 if ((d) > maxfiles) { \
130 ofiles = malloc((d) * sizeof(struct fdnode)); \
131 if (ofiles == NULL) { \
139 void dofiles(struct kinfo_proc
*kp
, struct proc
*p
);
140 void dommap(struct proc
*p
);
141 void vtrans(struct vnode
*vp
, struct nchandle
*ncr
, int i
, int flag
);
142 int ufs_filestat(struct vnode
*vp
, struct filestat
*fsp
);
143 int nfs_filestat(struct vnode
*vp
, struct filestat
*fsp
);
144 char *getmnton(struct mount
*m
, struct namecache_list
*ncplist
, struct nchandle
*ncr
);
145 void pipetrans(struct pipe
*pi
, int i
, int flag
);
146 void socktrans(struct socket
*sock
, int i
);
147 void getinetproto(int number
);
148 int getfname(const char *filename
);
153 main(int argc
, char **argv
)
155 struct passwd
*passwd
;
156 struct kinfo_proc
*p
, *plast
;
160 char buf
[_POSIX2_LINE_MAX
];
164 what
= KERN_PROC_ALL
;
165 nlistf
= memf
= NULL
;
166 while ((ch
= getopt(argc
, argv
, "fmnp:u:vwN:M:")) != -1)
186 if (!isdigit(*optarg
)) {
187 warnx("-p requires a process id");
190 what
= KERN_PROC_PID
;
196 if (!(passwd
= getpwnam(optarg
)))
197 errx(1, "%s: unknown uid", optarg
);
198 what
= KERN_PROC_UID
;
199 arg
= passwd
->pw_uid
;
213 if (*(argv
+= optind
)) {
214 for (; *argv
; ++argv
) {
218 if (!checkfile
) /* file(s) specified, but none accessable */
222 ALLOC_OFILES(256); /* reserve space for file pointers */
224 if (fsflg
&& !checkfile
) {
225 /* -f with no files means use wd */
226 if (getfname(".") == 0)
232 * Discard setgid privileges if not the running kernel so that bad
233 * guys can't print interesting stuff from kernel memory.
235 if (nlistf
!= NULL
|| memf
!= NULL
)
238 if ((kd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
)) == NULL
)
241 if (kvm_nlist(kd
, nl
) != 0)
242 errx(1, "no namelist: %s", kvm_geterr(kd
));
244 if ((p
= kvm_getprocs(kd
, what
, arg
, &cnt
)) == NULL
)
245 errx(1, "%s", kvm_geterr(kd
));
247 printf("USER %-*.*s %*.*s FD DEV %*.*s MODE SZ|DV R/W",
248 wflg_cmd
, wflg_cmd
, "CMD",
249 pid_width
, pid_width
, "PID",
250 ino_width
, ino_width
, "INUM");
252 printf("USER %-*.*s %*.*s FD %-*.*s %*.*s MODE SZ|DV R/W",
253 wflg_cmd
, wflg_cmd
, "CMD",
254 pid_width
, pid_width
, "PID",
255 wflg_mnt
, wflg_mnt
, "PATH",
256 ino_width
, ino_width
, "INUM");
257 if (checkfile
&& fsflg
== 0)
262 for (plast
= &p
[cnt
]; p
< plast
; ++p
) {
263 if (p
->kp_stat
== SZOMB
)
265 if (!kread((void *)p
->kp_paddr
, &proc
, sizeof(proc
))) {
266 dprintf(stderr
, "can't read proc at %p for pid %d\n",
267 (void *)p
->kp_paddr
, Pid
);
281 printf("%-8.8s %-*s %*d", Uname, wflg_cmd, Comm, pid_width, Pid); \
304 * print open files attributed to this process
307 dofiles(struct kinfo_proc
*kp
, struct proc
*p
)
311 struct filedesc filed
;
312 struct ktrace_node ktrace_node
;
314 Uname
= user_from_uid(kp
->kp_uid
, 0);
317 make_printable(Comm
, strlen(Comm
));
321 if (!kread(p
->p_fd
, &filed
, sizeof (filed
))) {
322 dprintf(stderr
, "can't read filedesc at %p for pid %d\n",
323 (void *)p
->p_fd
, Pid
);
327 * root directory vnode, if one
330 vtrans(filed
.fd_rdir
, &filed
.fd_nrdir
, RDIR
, FREAD
);
332 * current working directory vnode
334 vtrans(filed
.fd_cdir
, &filed
.fd_ncdir
, CDIR
, FREAD
);
336 * ktrace vnode, if one
338 if (p
->p_tracenode
) {
339 if (kread(p
->p_tracenode
, &ktrace_node
, sizeof (ktrace_node
)))
340 vtrans(ktrace_node
.kn_vp
, NULL
, TRACE
, FREAD
|FWRITE
);
346 vtrans(p
->p_textvp
, NULL
, TEXT
, FREAD
);
350 ALLOC_OFILES(filed
.fd_lastfile
+1);
351 if (!kread(filed
.fd_files
, ofiles
,
352 (filed
.fd_lastfile
+1) * sizeof(struct fdnode
))) {
354 "can't read file structures at %p for pid %d\n",
355 (void *)filed
.fd_files
, Pid
);
358 for (i
= 0; i
<= filed
.fd_lastfile
; i
++) {
359 if (ofiles
[i
].fp
== NULL
)
361 if (!kread(ofiles
[i
].fp
, &file
, sizeof (struct file
))) {
362 dprintf(stderr
, "can't read file %d at %p for pid %d\n",
363 i
, (void *)ofiles
[i
].fp
, Pid
);
366 if (file
.f_type
== DTYPE_VNODE
) {
367 vtrans((struct vnode
*)file
.f_data
, &file
.f_nchandle
, i
,
369 } else if (file
.f_type
== DTYPE_SOCKET
) {
371 socktrans((struct socket
*)file
.f_data
, i
);
374 else if (file
.f_type
== DTYPE_PIPE
) {
376 pipetrans((struct pipe
*)file
.f_data
, i
,
381 else if (file
.f_type
== DTYPE_FIFO
) {
383 vtrans((struct vnode
*)file
.f_data
, &file
.f_nchandle
,
389 "unknown file type %d for file %d of pid %d\n",
390 file
.f_type
, i
, Pid
);
396 dommap(struct proc
*p
)
398 struct vmspace vmspace
;
400 struct vm_map_entry entry
;
401 vm_map_entry_t entryp
;
402 struct vm_object object
;
406 if (!kread(p
->p_vmspace
, &vmspace
, sizeof(vmspace
))) {
407 dprintf(stderr
, "can't read vmspace at %p for pid %d\n",
408 (void *)p
->p_vmspace
, Pid
);
412 map
= &vmspace
.vm_map
;
414 for (entryp
= map
->header
.next
; entryp
!= &p
->p_vmspace
->vm_map
.header
;
415 entryp
= entry
.next
) {
416 if (!kread(entryp
, &entry
, sizeof(entry
))) {
418 "can't read vm_map_entry at %p for pid %d\n",
419 (void *)entryp
, Pid
);
423 if (entry
.maptype
== VM_MAPTYPE_SUBMAP
)
426 if ((objp
= entry
.object
.vm_object
) == NULL
)
429 for (; objp
; objp
= object
.backing_object
) {
430 if (!kread(objp
, &object
, sizeof(object
))) {
432 "can't read vm_object at %p for pid %d\n",
438 prot
= entry
.protection
;
439 fflags
= (prot
& VM_PROT_READ
? FREAD
: 0) |
440 (prot
& VM_PROT_WRITE
? FWRITE
: 0);
442 switch (object
.type
) {
444 vtrans((struct vnode
*)object
.handle
, NULL
,
454 vtrans(struct vnode
*vp
, struct nchandle
*ncr
, int i
, int flag
)
458 char rw
[3], mode
[15];
459 const char *badtype
= NULL
, *filename
;
461 filename
= badtype
= NULL
;
462 if (!kread(vp
, &vn
, sizeof (struct vnode
))) {
463 dprintf(stderr
, "can't read vnode at %p for pid %d\n",
467 if (vn
.v_type
== VNON
|| vn
.v_tag
== VT_NON
)
469 else if (vn
.v_type
== VBAD
)
474 if (!ufs_filestat(&vn
, &fst
))
478 if (!ufs_filestat(&vn
, &fst
))
482 if (!nfs_filestat(&vn
, &fst
))
487 if (!msdosfs_filestat(&vn
, &fst
))
492 if (!isofs_filestat(&vn
, &fst
))
497 static char unknown
[10];
498 sprintf(unknown
, "?(%x)", vn
.v_tag
);
509 for (d
= devs
; d
!= NULL
; d
= d
->next
)
510 if (d
->fsid
== fst
.fsid
) {
512 if (d
->ino
== (ino_t
)fst
.fileid
) {
517 if (fsmatch
== 0 || (filename
== NULL
&& fsflg
== 0))
522 (void)printf(" - - %10s -\n", badtype
);
526 (void)printf(" %3d,%-9d ", major(fst
.fsid
), minor(fst
.fsid
));
528 (void)printf(" %-*s", wflg_mnt
, getmnton(vn
.v_mount
, &vn
.v_namecache
, ncr
));
530 (void)sprintf(mode
, "%o", fst
.mode
);
532 strmode(fst
.mode
, mode
);
533 (void)printf(" %*ld %10s", ino_width
, fst
.fileid
, mode
);
539 if (nflg
|| ((name
= devname(fst
.rdev
, vn
.v_type
== VCHR
?
540 S_IFCHR
: S_IFBLK
)) == NULL
))
541 printf(" %3d,%-4d", major(fst
.rdev
), minor(fst
.rdev
));
543 printf(" %8s", name
);
547 printf(" %8llu", fst
.size
);
555 if (filename
&& !fsflg
)
556 printf(" %s", filename
);
561 ufs_filestat(struct vnode
*vp
, struct filestat
*fsp
)
565 if (!kread(VTOI(vp
), &inode
, sizeof (inode
))) {
566 dprintf(stderr
, "can't read inode at %p for pid %d\n",
567 (void *)VTOI(vp
), Pid
);
571 * The st_dev from stat(2) is a udev_t. These kernel structures
572 * contain dev_t structures. We need to convert to udev to make
575 fsp
->fsid
= dev2udev(inode
.i_dev
);
576 fsp
->fileid
= (long)inode
.i_number
;
577 fsp
->mode
= (mode_t
)inode
.i_mode
;
578 fsp
->size
= inode
.i_size
;
579 fsp
->rdev
= inode
.i_rdev
;
585 nfs_filestat(struct vnode
*vp
, struct filestat
*fsp
)
587 struct nfsnode nfsnode
;
590 if (!kread(VTONFS(vp
), &nfsnode
, sizeof (nfsnode
))) {
591 dprintf(stderr
, "can't read nfsnode at %p for pid %d\n",
592 (void *)VTONFS(vp
), Pid
);
595 fsp
->fsid
= nfsnode
.n_vattr
.va_fsid
;
596 fsp
->fileid
= nfsnode
.n_vattr
.va_fileid
;
597 fsp
->size
= nfsnode
.n_size
;
598 fsp
->rdev
= makeudev(nfsnode
.n_vattr
.va_rmajor
,
599 nfsnode
.n_vattr
.va_rminor
);
600 mode
= (mode_t
)nfsnode
.n_vattr
.va_mode
;
601 switch (vp
->v_type
) {
634 getmnton(struct mount
*m
, struct namecache_list
*ncplist
, struct nchandle
*ncr
)
636 static struct mount mount_l
;
640 char mntonname
[MNAMELEN
];
643 struct namecache
*ncp
;
644 struct namecache ncp_copy
;
645 static char path
[1024];
649 * If no ncp is passed try to find one via ncplist.
651 if (ncr
== NULL
|| ncr
->ncp
== NULL
) {
652 ncp
= ncplist
->tqh_first
;
658 * If we have an ncp, traceback the path. This is a kvm pointer.
661 if (!kread(m
, &mount_l
, sizeof(struct mount
))) {
662 warnx("can't read mount table at %p", (void *)m
);
665 i
= sizeof(path
) - 1;
669 * If this is the root of the mount then traverse
670 * to the parent mount.
672 if (ncp
== mount_l
.mnt_ncmountpt
.ncp
) {
673 ncp
= mount_l
.mnt_ncmounton
.ncp
;
676 m
= mount_l
.mnt_ncmounton
.mount
;
677 if (!kread(m
, &mount_l
, sizeof(struct mount
))) {
678 warnx("can't read mount table at %p", (void *)m
);
684 * Ok, pull out the ncp and extract the name
686 if (!kread(ncp
, &ncp_copy
, sizeof(ncp_copy
))) {
687 warnx("can't read ncp at %p", ncp
);
690 if (i
<= ncp_copy
.nc_nlen
)
692 i
-= ncp_copy
.nc_nlen
;
693 if (!kread(ncp_copy
.nc_name
, path
+ i
, ncp_copy
.nc_nlen
)) {
694 warnx("can't read ncp %p path component at %p", ncp
, ncp_copy
.nc_name
);
697 make_printable(path
+ i
, ncp_copy
.nc_nlen
);
699 ncp
= ncp_copy
.nc_parent
;
701 if (i
== sizeof(path
) - 1)
707 * If all else fails print out the mount point path
709 for (mt
= mhead
; mt
!= NULL
; mt
= mt
->next
) {
711 return (mt
->mntonname
);
713 if (!kread(m
, &mount_l
, sizeof(struct mount
))) {
714 warnx("can't read mount table at %p", (void *)m
);
717 if ((mt
= malloc(sizeof (struct mtab
))) == NULL
)
720 bcopy(&mount_l
.mnt_stat
.f_mntonname
[0], &mt
->mntonname
[0], MNAMELEN
);
723 return (mt
->mntonname
);
727 pipetrans(struct pipe
*pi
, int i
, int flag
)
735 if (!kread(pi
, &pip
, sizeof(struct pipe
))) {
736 dprintf(stderr
, "can't read pipe at %p\n", (void *)pi
);
740 printf("* pipe %8lx <-> %8lx", (u_long
)pi
, (u_long
)pip
.pipe_peer
);
741 printf(" %6d", (int)pip
.pipe_buffer
.cnt
);
756 socktrans(struct socket
*sock
, int i
)
758 static const char *stypename
[] = {
768 struct protosw proto
;
778 if (!kread(sock
, &so
, sizeof(struct socket
))) {
779 dprintf(stderr
, "can't read sock at %p\n", (void *)sock
);
783 /* fill in protosw entry */
784 if (!kread(so
.so_proto
, &proto
, sizeof(struct protosw
))) {
785 dprintf(stderr
, "can't read protosw at %p",
786 (void *)so
.so_proto
);
791 if (!kread(proto
.pr_domain
, &dom
, sizeof(struct domain
))) {
792 dprintf(stderr
, "can't read domain at %p\n",
793 (const void *)proto
.pr_domain
);
797 if ((len
= kvm_read(kd
, (u_long
)dom
.dom_name
, dname
,
798 sizeof(dname
) - 1)) < 0) {
799 dprintf(stderr
, "can't read domain name at %p\n",
800 (void *)dom
.dom_name
);
806 if ((u_short
)so
.so_type
> STYPEMAX
)
807 printf("* %s ?%d", dname
, so
.so_type
);
809 printf("* %s %s", dname
, stypename
[so
.so_type
]);
812 * protocol specific formatting
814 * Try to find interesting things to print. For tcp, the interesting
815 * thing is the address of the tcpcb, for udp and others, just the
816 * inpcb (socket pcb). For unix domain, its the address of the socket
817 * pcb and the address of the connected pcb (if connected). Otherwise
818 * just print the protocol number and address of the socket itself.
819 * The idea is not to duplicate netstat, but to make available enough
820 * information for further analysis.
822 switch(dom
.dom_family
) {
825 getinetproto(proto
.pr_protocol
);
826 if (proto
.pr_protocol
== IPPROTO_TCP
) {
828 if (kvm_read(kd
, (u_long
)so
.so_pcb
,
829 (char *)&inpcb
, sizeof(struct inpcb
))
830 != sizeof(struct inpcb
)) {
832 "can't read inpcb at %p\n",
836 printf(" %lx", (u_long
)inpcb
.inp_ppcb
);
840 printf(" %lx", (u_long
)so
.so_pcb
);
843 /* print address of pcb and connected pcb */
845 printf(" %lx", (u_long
)so
.so_pcb
);
846 if (kvm_read(kd
, (u_long
)so
.so_pcb
, (char *)&unpcb
,
847 sizeof(struct unpcb
)) != sizeof(struct unpcb
)){
848 dprintf(stderr
, "can't read unpcb at %p\n",
852 if (unpcb
.unp_conn
) {
853 char shoconn
[4], *cp
;
856 if (!(so
.so_state
& SS_CANTRCVMORE
))
859 if (!(so
.so_state
& SS_CANTSENDMORE
))
862 printf(" %s %lx", shoconn
,
863 (u_long
)unpcb
.unp_conn
);
868 /* print protocol number and socket address */
869 printf(" %d %lx", proto
.pr_protocol
, (u_long
)sock
);
879 * Read the cdev structure in the kernel (as pointed to by a dev_t)
880 * in order to work out the associated udev_t
887 if (kread(dev
, &si
, sizeof si
)) {
888 if ((si
.si_umajor
& 0xffffff00) ||
889 (si
.si_uminor
& 0x0000ff00)) {
892 return((si
.si_umajor
<< 8) | si
.si_uminor
);
894 dprintf(stderr
, "can't convert dev_t %p to a udev_t\n", dev
);
900 makeudev(int x
, int y
)
902 if ((x
& 0xffffff00) || (y
& 0x0000ff00))
904 return ((x
<< 8) | y
);
909 * print name of protocol number
912 getinetproto(int number
)
918 setprotoent(++isopen
);
919 if ((pe
= getprotobynumber(number
)) != NULL
)
920 printf(" %s", pe
->p_name
);
922 printf(" %d", number
);
926 getfname(const char *filename
)
931 if (stat(filename
, &statbuf
)) {
932 warn("%s", filename
);
935 if ((cur
= malloc(sizeof(DEVS
))) == NULL
)
940 cur
->ino
= statbuf
.st_ino
;
941 cur
->fsid
= statbuf
.st_dev
;
942 cur
->name
= filename
;
949 (void)fprintf(stderr
,
950 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
956 make_printable(char *buf
, int len
)
967 kread(const void *kaddr
, void *uaddr
, size_t nbytes
)
969 if (nbytes
> 0x10000000)
972 if (kvm_read(kd
, (u_long
)kaddr
, (char *)uaddr
, nbytes
) == (ssize_t
)nbytes
)