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 $
39 #include <sys/param.h>
42 #include <sys/vnode.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
48 #include <sys/unpcb.h>
49 #include <sys/sysctl.h>
50 #include <sys/filedesc.h>
51 #include <sys/queue.h>
55 #include <sys/ktrace.h>
56 #include <vfs/ufs/quota.h>
57 #include <vfs/ufs/inode.h>
58 #include <sys/mount.h>
59 #include <sys/namecache.h>
60 #include <nfs/nfsproto.h>
61 #include <nfs/rpcv2.h>
63 #include <nfs/nfsnode.h>
64 #include <sys/devfs.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/in_pcb.h>
100 static void make_printable(char *buf, int len);
103 struct nlist nl[] = {
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 */
121 struct fdnode *ofiles; /* buffer of pointers to file structures */
124 #define ALLOC_OFILES(d) \
125 if ((d) > maxfiles) { \
127 ofiles = malloc((d) * sizeof(struct fdnode)); \
128 if (ofiles == NULL) { \
136 void dofiles(struct kinfo_proc *kp, struct proc *p);
137 void dommap(struct proc *p);
138 void vtrans(struct vnode *vp, struct nchandle *ncr, int i, int flag);
139 int ufs_filestat(struct vnode *vp, struct filestat *fsp);
140 int nfs_filestat(struct vnode *vp, struct filestat *fsp);
141 int devfs_filestat(struct vnode *vp, struct filestat *fsp);
142 char *getmnton(struct mount *m, struct namecache_list *ncplist, struct nchandle *ncr);
143 void pipetrans(struct pipe *pi, int i, int flag);
144 void socktrans(struct socket *sock, int i);
145 void getinetproto(int number);
146 int getfname(const char *filename);
151 main(int argc, char **argv)
153 struct passwd *passwd;
154 struct kinfo_proc *p, *plast;
158 char buf[_POSIX2_LINE_MAX];
162 what = KERN_PROC_ALL;
163 nlistf = memf = NULL;
164 while ((ch = getopt(argc, argv, "fmnp:u:vwN:M:")) != -1)
184 if (!isdigit(*optarg)) {
185 warnx("-p requires a process id");
188 what = KERN_PROC_PID;
194 if (!(passwd = getpwnam(optarg)))
195 errx(1, "%s: unknown uid", optarg);
196 what = KERN_PROC_UID;
197 arg = passwd->pw_uid;
211 if (*(argv += optind)) {
212 for (; *argv; ++argv) {
216 if (!checkfile) /* file(s) specified, but none accessable */
220 ALLOC_OFILES(256); /* reserve space for file pointers */
222 if (fsflg && !checkfile) {
223 /* -f with no files means use wd */
224 if (getfname(".") == 0)
230 * Discard setgid privileges if not the running kernel so that bad
231 * guys can't print interesting stuff from kernel memory.
233 if (nlistf != NULL || memf != NULL)
236 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
239 if (kvm_nlist(kd, nl) != 0)
240 errx(1, "no namelist: %s", kvm_geterr(kd));
242 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
243 errx(1, "%s", kvm_geterr(kd));
245 printf("USER %-*.*s %*.*s FD DEV %*.*s MODE SZ|DV R/W",
246 wflg_cmd, wflg_cmd, "CMD",
247 pid_width, pid_width, "PID",
248 ino_width, ino_width, "INUM");
250 printf("USER %-*.*s %*.*s FD %-*.*s %*.*s MODE SZ|DV R/W",
251 wflg_cmd, wflg_cmd, "CMD",
252 pid_width, pid_width, "PID",
253 wflg_mnt, wflg_mnt, "PATH",
254 ino_width, ino_width, "INUM");
255 if (checkfile && fsflg == 0)
260 for (plast = &p[cnt]; p < plast; ++p) {
261 if (p->kp_stat == SZOMB)
263 if (!kread((void *)p->kp_paddr, &proc, sizeof(proc))) {
264 dprintf(stderr, "can't read proc at %p for pid %d\n",
265 (void *)p->kp_paddr, Pid);
280 printf("%-8.8s %-*s %*d", Uname, wflg_cmd, Comm, pid_width, Pid); \
303 * print open files attributed to this process
306 dofiles(struct kinfo_proc *kp, struct proc *p)
310 struct filedesc filed;
311 struct ktrace_node ktrace_node;
313 Uname = user_from_uid(kp->kp_uid, 0);
316 make_printable(Comm, strlen(Comm));
320 if (!kread(p->p_fd, &filed, sizeof (filed))) {
321 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
322 (void *)p->p_fd, Pid);
326 * root directory vnode, if one
329 vtrans(filed.fd_rdir, &filed.fd_nrdir, RDIR, FREAD);
331 * current working directory vnode
333 vtrans(filed.fd_cdir, &filed.fd_ncdir, CDIR, FREAD);
335 * ktrace vnode, if one
337 if (p->p_tracenode) {
338 if (kread(p->p_tracenode, &ktrace_node, sizeof (ktrace_node)))
339 vtrans(ktrace_node.kn_vp, NULL, TRACE, FREAD|FWRITE);
345 vtrans(p->p_textvp, NULL, TEXT, FREAD);
349 ALLOC_OFILES(filed.fd_lastfile+1);
350 if (!kread(filed.fd_files, ofiles,
351 (filed.fd_lastfile+1) * sizeof(struct fdnode))) {
353 "can't read file structures at %p for pid %d\n",
354 (void *)filed.fd_files, Pid);
357 for (i = 0; i <= filed.fd_lastfile; i++) {
358 if (ofiles[i].fp == NULL)
360 if (!kread(ofiles[i].fp, &file, sizeof (struct file))) {
361 dprintf(stderr, "can't read file %d at %p for pid %d\n",
362 i, (void *)ofiles[i].fp, Pid);
365 if (file.f_type == DTYPE_VNODE) {
366 vtrans((struct vnode *)file.f_data, &file.f_nchandle, i,
368 } else if (file.f_type == DTYPE_SOCKET) {
370 socktrans((struct socket *)file.f_data, i);
373 else if (file.f_type == DTYPE_PIPE) {
375 pipetrans((struct pipe *)file.f_data, i,
380 else if (file.f_type == DTYPE_FIFO) {
382 vtrans((struct vnode *)file.f_data, &file.f_nchandle,
388 "unknown file type %d for file %d of pid %d\n",
389 file.f_type, i, Pid);
395 dommap(struct proc *p)
397 struct vmspace vmspace;
399 struct vm_map_entry entry;
400 vm_map_entry_t entryp;
401 struct vm_object object;
405 if (!kread(p->p_vmspace, &vmspace, sizeof(vmspace))) {
406 dprintf(stderr, "can't read vmspace at %p for pid %d\n",
407 (void *)p->p_vmspace, Pid);
411 map = &vmspace.vm_map;
413 for (entryp = map->header.next; entryp != &p->p_vmspace->vm_map.header;
414 entryp = entry.next) {
415 if (!kread(entryp, &entry, sizeof(entry))) {
417 "can't read vm_map_entry at %p for pid %d\n",
418 (void *)entryp, Pid);
422 if (entry.maptype == VM_MAPTYPE_SUBMAP)
425 if ((objp = entry.object.vm_object) == NULL)
428 for (; objp; objp = object.backing_object) {
429 if (!kread(objp, &object, sizeof(object))) {
431 "can't read vm_object at %p for pid %d\n",
437 prot = entry.protection;
438 fflags = (prot & VM_PROT_READ ? FREAD : 0) |
439 (prot & VM_PROT_WRITE ? FWRITE : 0);
441 switch (object.type) {
443 vtrans((struct vnode *)object.handle, NULL,
453 vtrans(struct vnode *vp, struct nchandle *ncr, int i, int flag)
457 char rw[3], mode[15];
458 const char *badtype = NULL, *filename;
460 filename = badtype = NULL;
461 if (!kread(vp, &vn, sizeof (struct vnode))) {
462 dprintf(stderr, "can't read vnode at %p for pid %d\n",
466 if (vn.v_type == VNON || vn.v_tag == VT_NON)
468 else if (vn.v_type == VBAD)
473 if (!ufs_filestat(&vn, &fst))
477 if (!ufs_filestat(&vn, &fst))
481 if (!nfs_filestat(&vn, &fst))
486 if (!msdosfs_filestat(&vn, &fst))
491 if (!isofs_filestat(&vn, &fst))
496 if (!devfs_filestat(&vn, &fst))
501 static char unknown[10];
502 sprintf(unknown, "?(%x)", vn.v_tag);
513 for (d = devs; d != NULL; d = d->next)
514 if (d->fsid == fst.fsid) {
516 if (d->ino == (ino_t)fst.fileid) {
521 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
526 (void)printf(" %-*s %10s -\n",
528 getmnton(vn.v_mount, &vn.v_namecache, ncr),
533 (void)printf(" %3d,%-9d ", major(fst.fsid), minor(fst.fsid));
535 (void)printf(" %-*s", wflg_mnt, getmnton(vn.v_mount, &vn.v_namecache, ncr));
537 (void)sprintf(mode, "%o", fst.mode);
539 strmode(fst.mode, mode);
540 (void)printf(" %*ld %10s", ino_width, fst.fileid, mode);
546 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
547 S_IFCHR : S_IFBLK)) == NULL))
548 printf(" %3d,%-4d", major(fst.rdev), minor(fst.rdev));
550 printf(" %8s", name);
554 printf(" %8llu", fst.size);
562 if (filename && !fsflg)
563 printf(" %s", filename);
568 ufs_filestat(struct vnode *vp, struct filestat *fsp)
572 if (!kread(VTOI(vp), &inode, sizeof (inode))) {
573 dprintf(stderr, "can't read inode at %p for pid %d\n",
574 (void *)VTOI(vp), Pid);
578 * The st_dev from stat(2) is a udev_t. These kernel structures
579 * contain dev_t structures. We need to convert to udev to make
582 fsp->fsid = dev2udev(inode.i_dev);
583 fsp->fileid = (long)inode.i_number;
584 fsp->mode = (mode_t)inode.i_mode;
585 fsp->size = inode.i_size;
586 fsp->rdev = inode.i_rdev;
592 nfs_filestat(struct vnode *vp, struct filestat *fsp)
594 struct nfsnode nfsnode;
597 if (!kread(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
598 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
599 (void *)VTONFS(vp), Pid);
602 fsp->fsid = nfsnode.n_vattr.va_fsid;
603 fsp->fileid = nfsnode.n_vattr.va_fileid;
604 fsp->size = nfsnode.n_size;
605 fsp->rdev = makeudev(nfsnode.n_vattr.va_rmajor,
606 nfsnode.n_vattr.va_rminor);
607 mode = (mode_t)nfsnode.n_vattr.va_mode;
608 switch (vp->v_type) {
643 devfs_filestat(struct vnode *vp, struct filestat *fsp)
645 struct devfs_node devfs_node;
647 if (!kread(vp->v_data, &devfs_node, sizeof (devfs_node))) {
648 dprintf(stderr, "can't read devfs_node at %p for pid %d\n",
649 (void *)vp->v_data, Pid);
652 fsp->fsid = fsp->rdev = dev2udev(vp->v_rdev);
653 fsp->fileid = devfs_node.d_dir.d_ino;
654 fsp->mode = (devfs_node.mode & ~S_IFMT) | S_IFCHR;
661 getmnton(struct mount *m, struct namecache_list *ncplist, struct nchandle *ncr)
663 static struct mount mount_l;
667 char mntonname[MNAMELEN];
670 struct namecache *ncp;
671 struct namecache ncp_copy;
672 static char path[1024];
676 * If no ncp is passed try to find one via ncplist. Make sure
677 * we are using the correct mount pointer or the matching code
678 * will not know how to transition mount points properly.
680 if (ncr == NULL || ncr->ncp == NULL) {
681 ncp = ncplist->tqh_first;
689 * If we have an ncp, traceback the path. This is a kvm pointer.
692 if (!kread(m, &mount_l, sizeof(struct mount))) {
693 warnx("can't read mount table at %p", (void *)m);
696 i = sizeof(path) - 1;
700 * If this is the root of the mount then traverse
701 * to the parent mount.
703 if (ncp == mount_l.mnt_ncmountpt.ncp) {
704 ncp = mount_l.mnt_ncmounton.ncp;
707 m = mount_l.mnt_ncmounton.mount;
708 if (!kread(m, &mount_l, sizeof(struct mount))) {
709 warnx("can't read mount table at %p", (void *)m);
715 * Ok, pull out the ncp and extract the name
717 if (!kread(ncp, &ncp_copy, sizeof(ncp_copy))) {
718 warnx("can't read ncp at %p", ncp);
721 if (i <= ncp_copy.nc_nlen)
723 i -= ncp_copy.nc_nlen;
724 if (!kread(ncp_copy.nc_name, path + i, ncp_copy.nc_nlen)) {
725 warnx("can't read ncp %p path component at %p", ncp, ncp_copy.nc_name);
728 make_printable(path + i, ncp_copy.nc_nlen);
730 ncp = ncp_copy.nc_parent;
732 if (i == sizeof(path) - 1)
738 * If all else fails print out the mount point path
740 for (mt = mhead; mt != NULL; mt = mt->next) {
742 return (mt->mntonname);
744 if (!kread(m, &mount_l, sizeof(struct mount))) {
745 warnx("can't read mount table at %p", (void *)m);
748 if ((mt = malloc(sizeof (struct mtab))) == NULL)
751 bcopy(&mount_l.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
754 return (mt->mntonname);
758 pipetrans(struct pipe *pi, int i, int flag)
766 if (!kread(pi, &pip, sizeof(struct pipe))) {
767 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
771 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
772 printf(" %6d", (int)(pip.pipe_buffer.windex - pip.pipe_buffer.rindex));
787 socktrans(struct socket *sock, int i)
789 static const char *stypename[] = {
799 struct protosw proto;
809 if (!kread(sock, &so, sizeof(struct socket))) {
810 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
814 /* fill in protosw entry */
815 if (!kread(so.so_proto, &proto, sizeof(struct protosw))) {
816 dprintf(stderr, "can't read protosw at %p",
817 (void *)so.so_proto);
822 if (!kread(proto.pr_domain, &dom, sizeof(struct domain))) {
823 dprintf(stderr, "can't read domain at %p\n",
824 (const void *)proto.pr_domain);
828 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
829 sizeof(dname) - 1)) < 0) {
830 dprintf(stderr, "can't read domain name at %p\n",
831 (void *)dom.dom_name);
837 if ((u_short)so.so_type > STYPEMAX)
838 printf("* %s ?%d", dname, so.so_type);
840 printf("* %s %s", dname, stypename[so.so_type]);
843 * protocol specific formatting
845 * Try to find interesting things to print. For tcp, the interesting
846 * thing is the address of the tcpcb, for udp and others, just the
847 * inpcb (socket pcb). For unix domain, its the address of the socket
848 * pcb and the address of the connected pcb (if connected). Otherwise
849 * just print the protocol number and address of the socket itself.
850 * The idea is not to duplicate netstat, but to make available enough
851 * information for further analysis.
853 switch(dom.dom_family) {
856 getinetproto(proto.pr_protocol);
857 if (proto.pr_protocol == IPPROTO_TCP ) {
859 if (kvm_read(kd, (u_long)so.so_pcb,
860 (char *)&inpcb, sizeof(struct inpcb))
861 != sizeof(struct inpcb)) {
863 "can't read inpcb at %p\n",
867 printf(" %lx", (u_long)inpcb.inp_ppcb);
871 printf(" %lx", (u_long)so.so_pcb);
874 /* print address of pcb and connected pcb */
876 printf(" %lx", (u_long)so.so_pcb);
877 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
878 sizeof(struct unpcb)) != sizeof(struct unpcb)){
879 dprintf(stderr, "can't read unpcb at %p\n",
883 if (unpcb.unp_conn) {
884 char shoconn[4], *cp;
887 if (!(so.so_state & SS_CANTRCVMORE))
890 if (!(so.so_state & SS_CANTSENDMORE))
893 printf(" %s %lx", shoconn,
894 (u_long)unpcb.unp_conn);
899 /* print protocol number and socket address */
900 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
910 * Read the cdev structure in the kernel (as pointed to by a dev_t)
911 * in order to work out the associated udev_t
918 if (kread(dev, &si, sizeof si)) {
919 if ((si.si_umajor & 0xffffff00) ||
920 (si.si_uminor & 0x0000ff00)) {
923 return((si.si_umajor << 8) | si.si_uminor);
925 dprintf(stderr, "can't convert dev_t %p to a udev_t\n", dev);
931 makeudev(int x, int y)
933 if ((x & 0xffffff00) || (y & 0x0000ff00))
935 return ((x << 8) | y);
940 * print name of protocol number
943 getinetproto(int number)
949 setprotoent(++isopen);
950 if ((pe = getprotobynumber(number)) != NULL)
951 printf(" %s", pe->p_name);
953 printf(" %d", number);
957 getfname(const char *filename)
962 if (stat(filename, &statbuf)) {
963 warn("%s", filename);
966 if ((cur = malloc(sizeof(DEVS))) == NULL)
971 cur->ino = statbuf.st_ino;
972 cur->fsid = statbuf.st_dev;
973 cur->name = filename;
980 (void)fprintf(stderr,
981 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
987 make_printable(char *buf, int len)
998 kread(const void *kaddr, void *uaddr, size_t nbytes)
1000 if (nbytes > 0x10000000)
1003 if (kvm_read(kd, (u_long)kaddr, (char *)uaddr, nbytes) == (ssize_t)nbytes)