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.19 2006/09/11 20:24:57 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);
140 void dommap(struct kinfo_proc *kp);
141 void vtrans(struct vnode *vp, struct namecache *ncp, 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 namecache *ncp);
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;
159 char buf[_POSIX2_LINE_MAX];
163 what = KERN_PROC_ALL;
164 nlistf = memf = NULL;
165 while ((ch = getopt(argc, argv, "fmnp:u:vwN:M:")) != -1)
185 if (!isdigit(*optarg)) {
186 warnx("-p requires a process id");
189 what = KERN_PROC_PID;
195 if (!(passwd = getpwnam(optarg)))
196 errx(1, "%s: unknown uid", optarg);
197 what = KERN_PROC_UID;
198 arg = passwd->pw_uid;
212 if (*(argv += optind)) {
213 for (; *argv; ++argv) {
217 if (!checkfile) /* file(s) specified, but none accessable */
221 ALLOC_OFILES(256); /* reserve space for file pointers */
223 if (fsflg && !checkfile) {
224 /* -f with no files means use wd */
225 if (getfname(".") == 0)
231 * Discard setgid privileges if not the running kernel so that bad
232 * guys can't print interesting stuff from kernel memory.
234 if (nlistf != NULL || memf != NULL)
237 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
240 if (kvm_nlist(kd, nl) != 0)
241 errx(1, "no namelist: %s", kvm_geterr(kd));
243 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
244 errx(1, "%s", kvm_geterr(kd));
246 printf("USER %-*.*s %*.*s FD DEV %*.*s MODE SZ|DV R/W",
247 wflg_cmd, wflg_cmd, "CMD",
248 pid_width, pid_width, "PID",
249 ino_width, ino_width, "INUM");
251 printf("USER %-*.*s %*.*s FD %-*.*s %*.*s MODE SZ|DV R/W",
252 wflg_cmd, wflg_cmd, "CMD",
253 pid_width, pid_width, "PID",
254 wflg_mnt, wflg_mnt, "PATH",
255 ino_width, ino_width, "INUM");
256 if (checkfile && fsflg == 0)
261 for (plast = &p[cnt]; p < plast; ++p) {
262 if (p->kp_proc.p_stat == SZOMB)
275 printf("%-8.8s %-*s %*d", Uname, wflg_cmd, Comm, pid_width, Pid); \
298 * print open files attributed to this process
301 dofiles(struct kinfo_proc *kp)
305 struct filedesc filed;
306 struct ktrace_node ktrace_node;
307 struct proc *p = &kp->kp_proc;
308 struct eproc *ep = &kp->kp_eproc;
310 Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
312 Comm = kp->kp_thread.td_comm;
313 make_printable(Comm, strlen(Comm));
317 if (!kread(p->p_fd, &filed, sizeof (filed))) {
318 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
319 (void *)p->p_fd, Pid);
323 * root directory vnode, if one
326 vtrans(filed.fd_rdir, filed.fd_nrdir, RDIR, FREAD);
328 * current working directory vnode
330 vtrans(filed.fd_cdir, filed.fd_ncdir, CDIR, FREAD);
332 * ktrace vnode, if one
334 if (p->p_tracenode) {
335 if (kread(p->p_tracenode, &ktrace_node, sizeof (ktrace_node)))
336 vtrans(ktrace_node.kn_vp, NULL, TRACE, FREAD|FWRITE);
342 vtrans(p->p_textvp, NULL, TEXT, FREAD);
346 ALLOC_OFILES(filed.fd_lastfile+1);
347 if (!kread(filed.fd_files, ofiles,
348 (filed.fd_lastfile+1) * sizeof(struct fdnode))) {
350 "can't read file structures at %p for pid %d\n",
351 (void *)filed.fd_files, Pid);
354 for (i = 0; i <= filed.fd_lastfile; i++) {
355 if (ofiles[i].fp == NULL)
357 if (!kread(ofiles[i].fp, &file, sizeof (struct file))) {
358 dprintf(stderr, "can't read file %d at %p for pid %d\n",
359 i, (void *)ofiles[i].fp, Pid);
362 if (file.f_type == DTYPE_VNODE) {
363 vtrans((struct vnode *)file.f_data, file.f_ncp, i,
365 } else if (file.f_type == DTYPE_SOCKET) {
367 socktrans((struct socket *)file.f_data, i);
370 else if (file.f_type == DTYPE_PIPE) {
372 pipetrans((struct pipe *)file.f_data, i,
377 else if (file.f_type == DTYPE_FIFO) {
379 vtrans((struct vnode *)file.f_data, file.f_ncp,
385 "unknown file type %d for file %d of pid %d\n",
386 file.f_type, i, Pid);
392 dommap(struct kinfo_proc *kp)
394 struct proc *p = &kp->kp_proc;
395 struct vmspace vmspace;
397 struct vm_map_entry entry;
398 vm_map_entry_t entryp;
399 struct vm_object object;
403 if (!kread(p->p_vmspace, &vmspace, sizeof(vmspace))) {
404 dprintf(stderr, "can't read vmspace at %p for pid %d\n",
405 (void *)p->p_vmspace, Pid);
409 map = &vmspace.vm_map;
411 for (entryp = map->header.next; entryp != &p->p_vmspace->vm_map.header;
412 entryp = entry.next) {
413 if (!kread(entryp, &entry, sizeof(entry))) {
415 "can't read vm_map_entry at %p for pid %d\n",
416 (void *)entryp, Pid);
420 if (entry.maptype == VM_MAPTYPE_SUBMAP)
423 if ((objp = entry.object.vm_object) == NULL)
426 for (; objp; objp = object.backing_object) {
427 if (!kread(objp, &object, sizeof(object))) {
429 "can't read vm_object at %p for pid %d\n",
435 prot = entry.protection;
436 fflags = (prot & VM_PROT_READ ? FREAD : 0) |
437 (prot & VM_PROT_WRITE ? FWRITE : 0);
439 switch (object.type) {
441 vtrans((struct vnode *)object.handle, NULL,
451 vtrans(struct vnode *vp, struct namecache *ncp, int i, int flag)
455 char rw[3], mode[15];
456 const char *badtype = NULL, *filename;
458 filename = badtype = NULL;
459 if (!kread(vp, &vn, sizeof (struct vnode))) {
460 dprintf(stderr, "can't read vnode at %p for pid %d\n",
464 if (vn.v_type == VNON || vn.v_tag == VT_NON)
466 else if (vn.v_type == VBAD)
471 if (!ufs_filestat(&vn, &fst))
475 if (!ufs_filestat(&vn, &fst))
479 if (!nfs_filestat(&vn, &fst))
484 if (!msdosfs_filestat(&vn, &fst))
489 if (!isofs_filestat(&vn, &fst))
494 static char unknown[10];
495 sprintf(unknown, "?(%x)", vn.v_tag);
506 for (d = devs; d != NULL; d = d->next)
507 if (d->fsid == fst.fsid) {
509 if (d->ino == (ino_t)fst.fileid) {
514 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
519 (void)printf(" - - %10s -\n", badtype);
523 (void)printf(" %3d,%-9d ", major(fst.fsid), minor(fst.fsid));
525 (void)printf(" %-*s", wflg_mnt, getmnton(vn.v_mount, &vn.v_namecache, ncp));
527 (void)sprintf(mode, "%o", fst.mode);
529 strmode(fst.mode, mode);
530 (void)printf(" %*ld %10s", ino_width, fst.fileid, mode);
536 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
537 S_IFCHR : S_IFBLK)) == NULL))
538 printf(" %3d,%-4d", major(fst.rdev), minor(fst.rdev));
540 printf(" %8s", name);
544 printf(" %8llu", fst.size);
552 if (filename && !fsflg)
553 printf(" %s", filename);
558 ufs_filestat(struct vnode *vp, struct filestat *fsp)
562 if (!kread(VTOI(vp), &inode, sizeof (inode))) {
563 dprintf(stderr, "can't read inode at %p for pid %d\n",
564 (void *)VTOI(vp), Pid);
568 * The st_dev from stat(2) is a udev_t. These kernel structures
569 * contain dev_t structures. We need to convert to udev to make
572 fsp->fsid = dev2udev(inode.i_dev);
573 fsp->fileid = (long)inode.i_number;
574 fsp->mode = (mode_t)inode.i_mode;
575 fsp->size = inode.i_size;
576 fsp->rdev = inode.i_rdev;
582 nfs_filestat(struct vnode *vp, struct filestat *fsp)
584 struct nfsnode nfsnode;
587 if (!kread(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
588 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
589 (void *)VTONFS(vp), Pid);
592 fsp->fsid = nfsnode.n_vattr.va_fsid;
593 fsp->fileid = nfsnode.n_vattr.va_fileid;
594 fsp->size = nfsnode.n_size;
595 fsp->rdev = nfsnode.n_vattr.va_rdev;
596 mode = (mode_t)nfsnode.n_vattr.va_mode;
597 switch (vp->v_type) {
630 getmnton(struct mount *m, struct namecache_list *ncplist, struct namecache *ncp)
632 static struct mount mount_l;
636 char mntonname[MNAMELEN];
639 struct namecache ncp_copy;
640 static char path[1024];
644 * If no ncp is passed try to find one via ncplist.
647 ncp = ncplist->tqh_first;
651 * If we have an ncp, traceback the path. This is a kvm pointer.
654 i = sizeof(path) - 1;
657 if (!kread(ncp, &ncp_copy, sizeof(ncp_copy))) {
658 warnx("can't read ncp at %p", ncp);
661 if (ncp_copy.nc_flag & NCF_MOUNTPT) {
662 ncp = ncp_copy.nc_parent;
665 if (i <= ncp_copy.nc_nlen)
667 i -= ncp_copy.nc_nlen;
668 if (!kread(ncp_copy.nc_name, path + i, ncp_copy.nc_nlen)) {
669 warnx("can't read ncp %p path component at %p", ncp, ncp_copy.nc_name);
672 make_printable(path + i, ncp_copy.nc_nlen);
674 ncp = ncp_copy.nc_parent;
676 if (i == sizeof(path) - 1)
682 * If all else fails print out the mount point path
684 for (mt = mhead; mt != NULL; mt = mt->next) {
686 return (mt->mntonname);
688 if (!kread(m, &mount_l, sizeof(struct mount))) {
689 warnx("can't read mount table at %p", (void *)m);
692 if ((mt = malloc(sizeof (struct mtab))) == NULL)
695 bcopy(&mount_l.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
698 return (mt->mntonname);
702 pipetrans(struct pipe *pi, int i, int flag)
710 if (!kread(pi, &pip, sizeof(struct pipe))) {
711 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
715 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
716 printf(" %6d", (int)pip.pipe_buffer.cnt);
731 socktrans(struct socket *sock, int i)
733 static const char *stypename[] = {
743 struct protosw proto;
753 if (!kread(sock, &so, sizeof(struct socket))) {
754 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
758 /* fill in protosw entry */
759 if (!kread(so.so_proto, &proto, sizeof(struct protosw))) {
760 dprintf(stderr, "can't read protosw at %p",
761 (void *)so.so_proto);
766 if (!kread(proto.pr_domain, &dom, sizeof(struct domain))) {
767 dprintf(stderr, "can't read domain at %p\n",
768 (const void *)proto.pr_domain);
772 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
773 sizeof(dname) - 1)) < 0) {
774 dprintf(stderr, "can't read domain name at %p\n",
775 (void *)dom.dom_name);
781 if ((u_short)so.so_type > STYPEMAX)
782 printf("* %s ?%d", dname, so.so_type);
784 printf("* %s %s", dname, stypename[so.so_type]);
787 * protocol specific formatting
789 * Try to find interesting things to print. For tcp, the interesting
790 * thing is the address of the tcpcb, for udp and others, just the
791 * inpcb (socket pcb). For unix domain, its the address of the socket
792 * pcb and the address of the connected pcb (if connected). Otherwise
793 * just print the protocol number and address of the socket itself.
794 * The idea is not to duplicate netstat, but to make available enough
795 * information for further analysis.
797 switch(dom.dom_family) {
800 getinetproto(proto.pr_protocol);
801 if (proto.pr_protocol == IPPROTO_TCP ) {
803 if (kvm_read(kd, (u_long)so.so_pcb,
804 (char *)&inpcb, sizeof(struct inpcb))
805 != sizeof(struct inpcb)) {
807 "can't read inpcb at %p\n",
811 printf(" %lx", (u_long)inpcb.inp_ppcb);
815 printf(" %lx", (u_long)so.so_pcb);
818 /* print address of pcb and connected pcb */
820 printf(" %lx", (u_long)so.so_pcb);
821 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
822 sizeof(struct unpcb)) != sizeof(struct unpcb)){
823 dprintf(stderr, "can't read unpcb at %p\n",
827 if (unpcb.unp_conn) {
828 char shoconn[4], *cp;
831 if (!(so.so_state & SS_CANTRCVMORE))
834 if (!(so.so_state & SS_CANTSENDMORE))
837 printf(" %s %lx", shoconn,
838 (u_long)unpcb.unp_conn);
843 /* print protocol number and socket address */
844 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
854 * Read the cdev structure in the kernel (as pointed to by a dev_t)
855 * in order to work out the associated udev_t
862 if (kread(dev, &si, sizeof si)) {
865 dprintf(stderr, "can't convert dev_t %p to a udev_t\n", dev);
872 * print name of protocol number
875 getinetproto(int number)
881 setprotoent(++isopen);
882 if ((pe = getprotobynumber(number)) != NULL)
883 printf(" %s", pe->p_name);
885 printf(" %d", number);
889 getfname(const char *filename)
894 if (stat(filename, &statbuf)) {
895 warn("%s", filename);
898 if ((cur = malloc(sizeof(DEVS))) == NULL)
903 cur->ino = statbuf.st_ino;
904 cur->fsid = statbuf.st_dev;
905 cur->name = filename;
912 (void)fprintf(stderr,
913 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
919 make_printable(char *buf, int len)
930 kread(const void *kaddr, void *uaddr, size_t nbytes)
932 if (nbytes > 0x10000000)
935 if (kvm_read(kd, (u_long)kaddr, (char *)uaddr, nbytes) == (ssize_t)nbytes)