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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
53 #include <sys/vnode.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
59 #include <sys/unpcb.h>
60 #include <sys/sysctl.h>
62 #include <sys/filedesc.h>
63 #include <sys/queue.h>
70 #include <sys/mount.h>
71 #include <ufs/ufs/quota.h>
72 #include <ufs/ufs/inode.h>
73 #include <fs/devfs/devfs.h>
74 #include <fs/devfs/devfs_int.h>
76 #include <nfs/nfsproto.h>
77 #include <nfsclient/nfs.h>
78 #include <nfsclient/nfsnode.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
85 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
118 struct nlist nl[] = {
123 int fsflg, /* show files on same filesystem as file(s) argument */
124 pflg, /* show files open by a particular pid */
125 uflg; /* show files open by a particular (effective) user */
126 int checkfile; /* true if restricting to particular files or filesystems */
127 int nflg; /* (numerical) display f.s. and rdev as dev_t */
128 int vflg; /* display errors in locating kernel data objects etc... */
129 int mflg; /* include memory-mapped files */
132 struct file **ofiles; /* buffer of pointers to file structures */
134 #define ALLOC_OFILES(d) \
135 if ((d) > maxfiles) { \
137 ofiles = malloc((d) * sizeof(struct file *)); \
138 if (ofiles == NULL) { \
147 static void fstat_kvm(int, int);
148 static void fstat_sysctl(int, int);
149 void dofiles(struct kinfo_proc *kp);
150 void dommap(struct kinfo_proc *kp);
151 void vtrans(struct vnode *vp, int i, int flag);
152 int ufs_filestat(struct vnode *vp, struct filestat *fsp);
153 int nfs_filestat(struct vnode *vp, struct filestat *fsp);
154 int devfs_filestat(struct vnode *vp, struct filestat *fsp);
155 char *getmnton(struct mount *m);
156 void pipetrans(struct pipe *pi, int i, int flag);
157 void socktrans(struct socket *sock, int i);
158 void ptstrans(struct tty *tp, int i, int flag);
159 void shmtrans(struct shmfd *shmp, int i, int flag);
160 void getinetproto(int number);
161 int getfname(const char *filename);
163 char *kdevtoname(struct cdev *dev);
166 main(int argc, char **argv)
168 struct passwd *passwd;
172 what = KERN_PROC_PROC;
173 nlistf = memf = NULL;
174 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
194 if (!isdigit(*optarg)) {
195 warnx("-p requires a process id");
198 what = KERN_PROC_PID;
204 if (!(passwd = getpwnam(optarg)))
205 errx(1, "%s: unknown uid", optarg);
206 what = KERN_PROC_UID;
207 arg = passwd->pw_uid;
217 if (*(argv += optind)) {
218 for (; *argv; ++argv) {
222 if (!checkfile) /* file(s) specified, but none accessable */
226 if (fsflg && !checkfile) {
227 /* -f with no files means use wd */
228 if (getfname(".") == 0)
234 fstat_kvm(what, arg);
236 fstat_sysctl(what, arg);
246 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
249 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
250 if (checkfile && fsflg == 0)
257 fstat_kvm(int what, int arg)
259 struct kinfo_proc *p, *plast;
260 char buf[_POSIX2_LINE_MAX];
263 ALLOC_OFILES(256); /* reserve space for file pointers */
266 * Discard setgid privileges if not the running kernel so that bad
267 * guys can't print interesting stuff from kernel memory.
269 if (nlistf != NULL || memf != NULL)
272 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
276 if (kvm_nlist(kd, nl) != 0)
277 errx(1, "no namelist: %s", kvm_geterr(kd));
279 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
280 errx(1, "%s", kvm_geterr(kd));
282 for (plast = &p[cnt]; p < plast; ++p) {
283 if (p->ki_stat == SZOMB)
292 fstat_sysctl(int what, int arg)
295 /* not yet implemented */
296 fstat_kvm(what, arg);
299 const char *Uname, *Comm;
302 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
328 * print open files attributed to this process
331 dofiles(struct kinfo_proc *kp)
335 struct filedesc filed;
337 Uname = user_from_uid(kp->ki_uid, 0);
341 if (kp->ki_fd == NULL)
343 if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) {
344 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
345 (void *)kp->ki_fd, Pid);
349 * root directory vnode, if one
352 vtrans(filed.fd_rdir, RDIR, FREAD);
354 * current working directory vnode
357 vtrans(filed.fd_cdir, CDIR, FREAD);
362 vtrans(filed.fd_jdir, JDIR, FREAD);
364 * ktrace vnode, if one
367 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
372 vtrans(kp->ki_textvp, TEXT, FREAD);
376 #define FPSIZE (sizeof (struct file *))
377 #define MAX_LASTFILE (0x1000000)
379 /* Sanity check on filed.fd_lastfile */
380 if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE)
383 ALLOC_OFILES(filed.fd_lastfile+1);
384 if (!KVM_READ(filed.fd_ofiles, ofiles,
385 (filed.fd_lastfile+1) * FPSIZE)) {
387 "can't read file structures at %p for pid %d\n",
388 (void *)filed.fd_ofiles, Pid);
391 for (i = 0; i <= filed.fd_lastfile; i++) {
392 if (ofiles[i] == NULL)
394 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
395 dprintf(stderr, "can't read file %d at %p for pid %d\n",
396 i, (void *)ofiles[i], Pid);
399 if (file.f_type == DTYPE_VNODE)
400 vtrans(file.f_vnode, i, file.f_flag);
401 else if (file.f_type == DTYPE_SOCKET) {
403 socktrans(file.f_data, i);
406 else if (file.f_type == DTYPE_PIPE) {
408 pipetrans(file.f_data, i, file.f_flag);
412 else if (file.f_type == DTYPE_FIFO) {
414 vtrans(file.f_vnode, i, file.f_flag);
418 else if (file.f_type == DTYPE_PTS) {
420 ptstrans(file.f_data, i, file.f_flag);
424 else if (file.f_type == DTYPE_SHM) {
426 shmtrans(file.f_data, i, file.f_flag);
431 "unknown file type %d for file %d of pid %d\n",
432 file.f_type, i, Pid);
438 dommap(struct kinfo_proc *kp)
441 struct vmspace vmspace;
442 struct vm_map_entry entry;
443 vm_map_entry_t entryp;
444 struct vm_object object;
448 if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
450 "can't read vmspace at %p for pid %d\n",
451 (void *)kp->ki_vmspace, Pid);
454 map = &vmspace.vm_map;
456 for (entryp = map->header.next;
457 entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
458 if (!KVM_READ(entryp, &entry, sizeof(entry))) {
460 "can't read vm_map_entry at %p for pid %d\n",
461 (void *)entryp, Pid);
465 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
468 if ((objp = entry.object.vm_object) == NULL)
471 for (; objp; objp = object.backing_object) {
472 if (!KVM_READ(objp, &object, sizeof(object))) {
474 "can't read vm_object at %p for pid %d\n",
480 prot = entry.protection;
481 fflags = (prot & VM_PROT_READ ? FREAD : 0) |
482 (prot & VM_PROT_WRITE ? FWRITE : 0);
484 switch (object.type) {
486 vtrans((struct vnode *)object.handle, MMAP, fflags);
495 kdevtoname(struct cdev *dev)
499 if (!KVM_READ(dev, &si, sizeof si))
501 return (strdup(si.__si_namebuf));
505 vtrans(struct vnode *vp, int i, int flag)
509 char rw[3], mode[15], tagstr[12], *tagptr;
510 const char *badtype, *filename;
512 filename = badtype = NULL;
513 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
514 dprintf(stderr, "can't read vnode at %p for pid %d\n",
518 if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
519 !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
520 dprintf(stderr, "can't read v_tag at %p for pid %d\n",
524 tagstr[sizeof(tagstr) - 1] = '\0';
525 if (vn.v_type == VNON)
527 else if (vn.v_type == VBAD)
530 if (!strcmp("ufs", tagstr)) {
531 if (!ufs_filestat(&vn, &fst))
533 } else if (!strcmp("devfs", tagstr)) {
534 if (!devfs_filestat(&vn, &fst))
536 } else if (!strcmp("nfs", tagstr)) {
537 if (!nfs_filestat(&vn, &fst))
539 } else if (!strcmp("msdosfs", tagstr)) {
540 if (!msdosfs_filestat(&vn, &fst))
542 } else if (!strcmp("isofs", tagstr)) {
543 if (!isofs_filestat(&vn, &fst))
546 } else if (!strcmp("zfs", tagstr)) {
547 if (!zfs_filestat(&vn, &fst))
551 static char unknown[32];
552 snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
562 for (d = devs; d != NULL; d = d->next)
563 if (d->fsid == fst.fsid) {
565 if (d->ino == fst.fileid) {
570 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
575 (void)printf(" - - %10s -\n", badtype);
579 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
581 (void)printf(" %-8s", getmnton(vn.v_mount));
583 (void)sprintf(mode, "%o", fst.mode);
585 strmode(fst.mode, mode);
586 (void)printf(" %6ld %10s", fst.fileid, mode);
592 name = kdevtoname(vn.v_rdev);
594 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
596 printf(" %6s", name);
602 printf(" %6lu", fst.size);
610 if (filename && !fsflg)
611 printf(" %s", filename);
616 ufs_filestat(struct vnode *vp, struct filestat *fsp)
620 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
621 dprintf(stderr, "can't read inode at %p for pid %d\n",
622 (void *)VTOI(vp), Pid);
626 * The st_dev from stat(2) is a dev_t. These kernel structures
627 * contain cdev pointers. We need to convert to dev_t to make
630 fsp->fsid = dev2udev(inode.i_dev);
631 fsp->fileid = (long)inode.i_number;
632 fsp->mode = (mode_t)inode.i_mode;
633 fsp->size = (u_long)inode.i_size;
634 #if should_be_but_is_hard
635 /* XXX - need to load i_ump and i_din[12] from kernel memory */
636 if (inode.i_ump->um_fstype == UFS1)
637 fsp->rdev = inode.i_din1->di_rdev;
639 fsp->rdev = inode.i_din2->di_rdev;
648 devfs_filestat(struct vnode *vp, struct filestat *fsp)
650 struct devfs_dirent devfs_dirent;
654 if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
655 dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
656 (void *)vp->v_data, Pid);
659 if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
660 dprintf(stderr, "can't read mount at %p for pid %d\n",
661 (void *)vp->v_mount, Pid);
664 if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
665 dprintf(stderr, "can't read vnode at %p for pid %d\n",
666 (void *)devfs_dirent.de_vnode, Pid);
669 fsp->fsid = (long)(uint32_t)mount.mnt_stat.f_fsid.val[0];
670 fsp->fileid = devfs_dirent.de_inode;
671 fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
673 fsp->rdev = dev2udev(vnode.v_rdev);
679 nfs_filestat(struct vnode *vp, struct filestat *fsp)
681 struct nfsnode nfsnode;
684 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
685 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
686 (void *)VTONFS(vp), Pid);
689 fsp->fsid = nfsnode.n_vattr.va_fsid;
690 fsp->fileid = nfsnode.n_vattr.va_fileid;
691 fsp->size = nfsnode.n_size;
692 fsp->rdev = nfsnode.n_vattr.va_rdev;
693 mode = (mode_t)nfsnode.n_vattr.va_mode;
694 switch (vp->v_type) {
728 getmnton(struct mount *m)
730 static struct mount mount;
734 char mntonname[MNAMELEN];
738 for (mt = mhead; mt != NULL; mt = mt->next)
740 return (mt->mntonname);
741 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
742 warnx("can't read mount table at %p", (void *)m);
745 if ((mt = malloc(sizeof (struct mtab))) == NULL)
748 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
751 return (mt->mntonname);
755 pipetrans(struct pipe *pi, int i, int flag)
763 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
764 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
768 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
769 printf(" %6d", (int)pip.pipe_buffer.cnt);
784 socktrans(struct socket *sock, int i)
786 static const char *stypename[] = {
796 struct protosw proto;
806 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
807 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
811 /* fill in protosw entry */
812 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
813 dprintf(stderr, "can't read protosw at %p",
814 (void *)so.so_proto);
819 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
820 dprintf(stderr, "can't read domain at %p\n",
821 (void *)proto.pr_domain);
825 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
826 sizeof(dname) - 1)) < 0) {
827 dprintf(stderr, "can't read domain name at %p\n",
828 (void *)dom.dom_name);
834 if ((u_short)so.so_type > STYPEMAX)
835 printf("* %s ?%d", dname, so.so_type);
837 printf("* %s %s", dname, stypename[so.so_type]);
840 * protocol specific formatting
842 * Try to find interesting things to print. For tcp, the interesting
843 * thing is the address of the tcpcb, for udp and others, just the
844 * inpcb (socket pcb). For unix domain, its the address of the socket
845 * pcb and the address of the connected pcb (if connected). Otherwise
846 * just print the protocol number and address of the socket itself.
847 * The idea is not to duplicate netstat, but to make available enough
848 * information for further analysis.
850 switch(dom.dom_family) {
853 getinetproto(proto.pr_protocol);
854 if (proto.pr_protocol == IPPROTO_TCP ) {
856 if (kvm_read(kd, (u_long)so.so_pcb,
857 (char *)&inpcb, sizeof(struct inpcb))
858 != sizeof(struct inpcb)) {
860 "can't read inpcb at %p\n",
864 printf(" %lx", (u_long)inpcb.inp_ppcb);
868 printf(" %lx", (u_long)so.so_pcb);
871 /* print address of pcb and connected pcb */
873 printf(" %lx", (u_long)so.so_pcb);
874 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
875 sizeof(struct unpcb)) != sizeof(struct unpcb)){
876 dprintf(stderr, "can't read unpcb at %p\n",
880 if (unpcb.unp_conn) {
881 char shoconn[4], *cp;
884 if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE))
887 if (!(so.so_snd.sb_state & SBS_CANTSENDMORE))
890 printf(" %s %lx", shoconn,
891 (u_long)unpcb.unp_conn);
896 /* print protocol number and socket address */
897 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
906 ptstrans(struct tty *tp, int i, int flag)
915 /* Obtain struct tty. */
916 if (!KVM_READ(tp, &tty, sizeof(struct tty))) {
917 dprintf(stderr, "can't read tty at %p\n", (void *)tp);
921 /* Figure out the device name. */
922 name = kdevtoname(tty.t_dev);
924 dprintf(stderr, "can't determine tty name at %p\n", (void *)tp);
934 printf("* pseudo-terminal master ");
936 rdev = dev2udev(tty.t_dev);
937 printf("%10d,%-2d", major(rdev), minor(rdev));
939 printf("%10s", name);
941 printf(" %2s\n", rw);
951 shmtrans(struct shmfd *shmp, int i, int flag)
954 char name[MAXPATHLEN];
961 if (!KVM_READ(shmp, &shm, sizeof(struct shmfd))) {
962 dprintf(stderr, "can't read shm at %p\n", shmp);
966 if (shm.shm_path != NULL) {
967 for (j = 0; j < sizeof(name) - 1; j++) {
968 if (!KVM_READ(shm.shm_path + j, name + j, 1))
983 shm.shm_mode |= S_IFREG;
986 (void)snprintf(mode, sizeof(mode), "%o", shm.shm_mode);
988 printf(" %-15s", name[0] != '\0' ? name : "-");
989 strmode(shm.shm_mode, mode);
991 printf(" %10s %6zu", mode, shm.shm_size);
992 printf(" %2s\n", rw);
1000 * Read the cdev structure in the kernel in order to work out the
1004 dev2udev(struct cdev *dev)
1006 struct cdev_priv priv;
1008 if (KVM_READ(cdev2priv(dev), &priv, sizeof priv)) {
1009 return ((dev_t)priv.cdp_inode);
1011 dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
1018 * print name of protocol number
1021 getinetproto(int number)
1024 struct protoent *pe;
1027 setprotoent(++isopen);
1028 if ((pe = getprotobynumber(number)) != NULL)
1029 printf(" %s", pe->p_name);
1031 printf(" %d", number);
1035 getfname(const char *filename)
1037 struct stat statbuf;
1040 if (stat(filename, &statbuf)) {
1041 warn("%s", filename);
1044 if ((cur = malloc(sizeof(DEVS))) == NULL)
1049 cur->ino = statbuf.st_ino;
1050 cur->fsid = statbuf.st_dev;
1051 cur->name = filename;
1057 getvnodedata(struct vnode *vp)
1059 return (vp->v_data);
1063 getvnodemount(struct vnode *vp)
1065 return (vp->v_mount);
1072 (void)fprintf(stderr,
1073 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");