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>
61 #include <sys/filedesc.h>
62 #include <sys/queue.h>
67 #include <sys/mount.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/inode.h>
70 #include <fs/devfs/devfs.h>
71 #include <fs/devfs/devfs_int.h>
73 #include <nfs/nfsproto.h>
74 #include <nfs/rpcv2.h>
75 #include <nfsclient/nfs.h>
76 #include <nfsclient/nfsnode.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_object.h>
83 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
115 struct nlist nl[] = {
120 int fsflg, /* show files on same filesystem as file(s) argument */
121 pflg, /* show files open by a particular pid */
122 uflg; /* show files open by a particular (effective) user */
123 int checkfile; /* true if restricting to particular files or filesystems */
124 int nflg; /* (numerical) display f.s. and rdev as dev_t */
125 int vflg; /* display errors in locating kernel data objects etc... */
126 int mflg; /* include memory-mapped files */
129 struct file **ofiles; /* buffer of pointers to file structures */
131 #define ALLOC_OFILES(d) \
132 if ((d) > maxfiles) { \
134 ofiles = malloc((d) * sizeof(struct file *)); \
135 if (ofiles == NULL) { \
144 static void fstat_kvm(int, int);
145 static void fstat_sysctl(int, int);
146 void dofiles(struct kinfo_proc *kp);
147 void dommap(struct kinfo_proc *kp);
148 void vtrans(struct vnode *vp, int i, int flag);
149 int ufs_filestat(struct vnode *vp, struct filestat *fsp);
150 int nfs_filestat(struct vnode *vp, struct filestat *fsp);
151 int devfs_filestat(struct vnode *vp, struct filestat *fsp);
152 char *getmnton(struct mount *m);
153 void pipetrans(struct pipe *pi, int i, int flag);
154 void socktrans(struct socket *sock, int i);
155 void getinetproto(int number);
156 int getfname(const char *filename);
158 char *kdevtoname(struct cdev *dev);
161 main(int argc, char **argv)
163 struct passwd *passwd;
167 what = KERN_PROC_PROC;
168 nlistf = memf = NULL;
169 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
189 if (!isdigit(*optarg)) {
190 warnx("-p requires a process id");
193 what = KERN_PROC_PID;
199 if (!(passwd = getpwnam(optarg)))
200 errx(1, "%s: unknown uid", optarg);
201 what = KERN_PROC_UID;
202 arg = passwd->pw_uid;
212 if (*(argv += optind)) {
213 for (; *argv; ++argv) {
217 if (!checkfile) /* file(s) specified, but none accessable */
221 if (fsflg && !checkfile) {
222 /* -f with no files means use wd */
223 if (getfname(".") == 0)
229 fstat_kvm(what, arg);
231 fstat_sysctl(what, arg);
241 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
244 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
245 if (checkfile && fsflg == 0)
252 fstat_kvm(int what, int arg)
254 struct kinfo_proc *p, *plast;
255 char buf[_POSIX2_LINE_MAX];
258 ALLOC_OFILES(256); /* reserve space for file pointers */
261 * Discard setgid privileges if not the running kernel so that bad
262 * guys can't print interesting stuff from kernel memory.
264 if (nlistf != NULL || memf != NULL)
267 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
271 if (kvm_nlist(kd, nl) != 0)
272 errx(1, "no namelist: %s", kvm_geterr(kd));
274 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
275 errx(1, "%s", kvm_geterr(kd));
277 for (plast = &p[cnt]; p < plast; ++p) {
278 if (p->ki_stat == SZOMB)
287 fstat_sysctl(int what, int arg)
290 /* not yet implemented */
291 fstat_kvm(what, arg);
294 const char *Uname, *Comm;
297 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
323 * print open files attributed to this process
326 dofiles(struct kinfo_proc *kp)
330 struct filedesc filed;
332 Uname = user_from_uid(kp->ki_uid, 0);
336 if (kp->ki_fd == NULL)
338 if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) {
339 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
340 (void *)kp->ki_fd, Pid);
344 * root directory vnode, if one
347 vtrans(filed.fd_rdir, RDIR, FREAD);
349 * current working directory vnode
352 vtrans(filed.fd_cdir, CDIR, FREAD);
357 vtrans(filed.fd_jdir, JDIR, FREAD);
359 * ktrace vnode, if one
362 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
367 vtrans(kp->ki_textvp, TEXT, FREAD);
371 #define FPSIZE (sizeof (struct file *))
372 #define MAX_LASTFILE (0x1000000)
374 /* Sanity check on filed.fd_lastfile */
375 if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE)
378 ALLOC_OFILES(filed.fd_lastfile+1);
379 if (!KVM_READ(filed.fd_ofiles, ofiles,
380 (filed.fd_lastfile+1) * FPSIZE)) {
382 "can't read file structures at %p for pid %d\n",
383 (void *)filed.fd_ofiles, Pid);
386 for (i = 0; i <= filed.fd_lastfile; i++) {
387 if (ofiles[i] == NULL)
389 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
390 dprintf(stderr, "can't read file %d at %p for pid %d\n",
391 i, (void *)ofiles[i], Pid);
394 if (file.f_type == DTYPE_VNODE)
395 vtrans(file.f_vnode, i, file.f_flag);
396 else if (file.f_type == DTYPE_SOCKET) {
398 socktrans(file.f_data, i);
401 else if (file.f_type == DTYPE_PIPE) {
403 pipetrans(file.f_data, i, file.f_flag);
407 else if (file.f_type == DTYPE_FIFO) {
409 vtrans(file.f_vnode, i, file.f_flag);
414 "unknown file type %d for file %d of pid %d\n",
415 file.f_type, i, Pid);
421 dommap(struct kinfo_proc *kp)
424 struct vmspace vmspace;
425 struct vm_map_entry entry;
426 vm_map_entry_t entryp;
427 struct vm_object object;
431 if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
433 "can't read vmspace at %p for pid %d\n",
434 (void *)kp->ki_vmspace, Pid);
437 map = &vmspace.vm_map;
439 for (entryp = map->header.next;
440 entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
441 if (!KVM_READ(entryp, &entry, sizeof(entry))) {
443 "can't read vm_map_entry at %p for pid %d\n",
444 (void *)entryp, Pid);
448 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
451 if ((objp = entry.object.vm_object) == NULL)
454 for (; objp; objp = object.backing_object) {
455 if (!KVM_READ(objp, &object, sizeof(object))) {
457 "can't read vm_object at %p for pid %d\n",
463 prot = entry.protection;
464 fflags = (prot & VM_PROT_READ ? FREAD : 0) |
465 (prot & VM_PROT_WRITE ? FWRITE : 0);
467 switch (object.type) {
469 vtrans((struct vnode *)object.handle, MMAP, fflags);
478 kdevtoname(struct cdev *dev)
482 if (!KVM_READ(dev, &si, sizeof si))
484 return (strdup(si.__si_namebuf));
488 vtrans(struct vnode *vp, int i, int flag)
492 char rw[3], mode[15], tagstr[12], *tagptr;
493 const char *badtype, *filename;
495 filename = badtype = NULL;
496 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
497 dprintf(stderr, "can't read vnode at %p for pid %d\n",
501 if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
502 !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
503 dprintf(stderr, "can't read v_tag at %p for pid %d\n",
507 tagstr[sizeof(tagstr) - 1] = '\0';
508 if (vn.v_type == VNON)
510 else if (vn.v_type == VBAD)
513 if (!strcmp("ufs", tagstr)) {
514 if (!ufs_filestat(&vn, &fst))
516 } else if (!strcmp("devfs", tagstr)) {
517 if (!devfs_filestat(&vn, &fst))
519 } else if (!strcmp("nfs", tagstr)) {
520 if (!nfs_filestat(&vn, &fst))
522 } else if (!strcmp("msdosfs", tagstr)) {
523 if (!msdosfs_filestat(&vn, &fst))
525 } else if (!strcmp("isofs", tagstr)) {
526 if (!isofs_filestat(&vn, &fst))
529 static char unknown[32];
530 snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
540 for (d = devs; d != NULL; d = d->next)
541 if (d->fsid == fst.fsid) {
543 if (d->ino == fst.fileid) {
548 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
553 (void)printf(" - - %10s -\n", badtype);
557 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
559 (void)printf(" %-8s", getmnton(vn.v_mount));
561 (void)sprintf(mode, "%o", fst.mode);
563 strmode(fst.mode, mode);
564 (void)printf(" %6ld %10s", fst.fileid, mode);
570 name = kdevtoname(vn.v_rdev);
572 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
574 printf(" %6s", name);
580 printf(" %6lu", fst.size);
588 if (filename && !fsflg)
589 printf(" %s", filename);
594 ufs_filestat(struct vnode *vp, struct filestat *fsp)
598 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
599 dprintf(stderr, "can't read inode at %p for pid %d\n",
600 (void *)VTOI(vp), Pid);
604 * The st_dev from stat(2) is a dev_t. These kernel structures
605 * contain cdev pointers. We need to convert to dev_t to make
608 fsp->fsid = dev2udev(inode.i_dev);
609 fsp->fileid = (long)inode.i_number;
610 fsp->mode = (mode_t)inode.i_mode;
611 fsp->size = (u_long)inode.i_size;
612 #if should_be_but_is_hard
613 /* XXX - need to load i_ump and i_din[12] from kernel memory */
614 if (inode.i_ump->um_fstype == UFS1)
615 fsp->rdev = inode.i_din1->di_rdev;
617 fsp->rdev = inode.i_din2->di_rdev;
626 devfs_filestat(struct vnode *vp, struct filestat *fsp)
628 struct devfs_dirent devfs_dirent;
632 if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
633 dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
634 (void *)vp->v_data, Pid);
637 if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
638 dprintf(stderr, "can't read mount at %p for pid %d\n",
639 (void *)vp->v_mount, Pid);
642 if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
643 dprintf(stderr, "can't read vnode at %p for pid %d\n",
644 (void *)devfs_dirent.de_vnode, Pid);
647 fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
648 fsp->fileid = devfs_dirent.de_inode;
649 fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
651 fsp->rdev = dev2udev(vnode.v_rdev);
657 nfs_filestat(struct vnode *vp, struct filestat *fsp)
659 struct nfsnode nfsnode;
662 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
663 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
664 (void *)VTONFS(vp), Pid);
667 fsp->fsid = nfsnode.n_vattr.va_fsid;
668 fsp->fileid = nfsnode.n_vattr.va_fileid;
669 fsp->size = nfsnode.n_size;
670 fsp->rdev = nfsnode.n_vattr.va_rdev;
671 mode = (mode_t)nfsnode.n_vattr.va_mode;
672 switch (vp->v_type) {
706 getmnton(struct mount *m)
708 static struct mount mount;
712 char mntonname[MNAMELEN];
716 for (mt = mhead; mt != NULL; mt = mt->next)
718 return (mt->mntonname);
719 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
720 warnx("can't read mount table at %p", (void *)m);
723 if ((mt = malloc(sizeof (struct mtab))) == NULL)
726 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
729 return (mt->mntonname);
733 pipetrans(struct pipe *pi, int i, int flag)
741 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
742 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
746 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
747 printf(" %6d", (int)pip.pipe_buffer.cnt);
762 socktrans(struct socket *sock, int i)
764 static const char *stypename[] = {
774 struct protosw proto;
784 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
785 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
789 /* fill in protosw entry */
790 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
791 dprintf(stderr, "can't read protosw at %p",
792 (void *)so.so_proto);
797 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
798 dprintf(stderr, "can't read domain at %p\n",
799 (void *)proto.pr_domain);
803 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
804 sizeof(dname) - 1)) < 0) {
805 dprintf(stderr, "can't read domain name at %p\n",
806 (void *)dom.dom_name);
812 if ((u_short)so.so_type > STYPEMAX)
813 printf("* %s ?%d", dname, so.so_type);
815 printf("* %s %s", dname, stypename[so.so_type]);
818 * protocol specific formatting
820 * Try to find interesting things to print. For tcp, the interesting
821 * thing is the address of the tcpcb, for udp and others, just the
822 * inpcb (socket pcb). For unix domain, its the address of the socket
823 * pcb and the address of the connected pcb (if connected). Otherwise
824 * just print the protocol number and address of the socket itself.
825 * The idea is not to duplicate netstat, but to make available enough
826 * information for further analysis.
828 switch(dom.dom_family) {
831 getinetproto(proto.pr_protocol);
832 if (proto.pr_protocol == IPPROTO_TCP ) {
834 if (kvm_read(kd, (u_long)so.so_pcb,
835 (char *)&inpcb, sizeof(struct inpcb))
836 != sizeof(struct inpcb)) {
838 "can't read inpcb at %p\n",
842 printf(" %lx", (u_long)inpcb.inp_ppcb);
846 printf(" %lx", (u_long)so.so_pcb);
849 /* print address of pcb and connected pcb */
851 printf(" %lx", (u_long)so.so_pcb);
852 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
853 sizeof(struct unpcb)) != sizeof(struct unpcb)){
854 dprintf(stderr, "can't read unpcb at %p\n",
858 if (unpcb.unp_conn) {
859 char shoconn[4], *cp;
862 if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE))
865 if (!(so.so_snd.sb_state & SBS_CANTSENDMORE))
868 printf(" %s %lx", shoconn,
869 (u_long)unpcb.unp_conn);
874 /* print protocol number and socket address */
875 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
885 * Read the cdev structure in the kernel in order to work out the
889 dev2udev(struct cdev *dev)
891 struct cdev_priv priv;
894 if (KVM_READ(dev, &si, sizeof si) &&
895 KVM_READ(si.si_priv, &priv, sizeof priv)) {
896 return ((dev_t)priv.cdp_inode);
898 dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
905 * print name of protocol number
908 getinetproto(int number)
914 setprotoent(++isopen);
915 if ((pe = getprotobynumber(number)) != NULL)
916 printf(" %s", pe->p_name);
918 printf(" %d", number);
922 getfname(const char *filename)
927 if (stat(filename, &statbuf)) {
928 warn("%s", filename);
931 if ((cur = malloc(sizeof(DEVS))) == NULL)
936 cur->ino = statbuf.st_ino;
937 cur->fsid = statbuf.st_dev;
938 cur->name = filename;
945 (void)fprintf(stderr,
946 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");