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";
44 static const char rcsid[] =
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 <ufs/ufs/quota.h>
68 #include <ufs/ufs/inode.h>
69 #include <fs/devfs/devfs.h>
70 #include <sys/mount.h>
72 #include <nfs/nfsproto.h>
73 #include <nfs/rpcv2.h>
74 #include <nfsclient/nfs.h>
75 #include <nfsclient/nfsnode.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_object.h>
82 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/in_pcb.h>
113 struct nlist nl[] = {
118 int fsflg, /* show files on same filesystem as file(s) argument */
119 pflg, /* show files open by a particular pid */
120 uflg; /* show files open by a particular (effective) user */
121 int checkfile; /* true if restricting to particular files or filesystems */
122 int nflg; /* (numerical) display f.s. and rdev as dev_t */
123 int vflg; /* display errors in locating kernel data objects etc... */
124 int mflg; /* include memory-mapped files */
127 struct file **ofiles; /* buffer of pointers to file structures */
129 #define ALLOC_OFILES(d) \
130 if ((d) > maxfiles) { \
132 ofiles = malloc((d) * sizeof(struct file *)); \
133 if (ofiles == NULL) { \
141 void dofiles(struct kinfo_proc *kp);
142 void dommap(struct kinfo_proc *kp);
143 void vtrans(struct vnode *vp, int i, int flag);
144 int ufs_filestat(struct vnode *vp, struct filestat *fsp);
145 int nfs_filestat(struct vnode *vp, struct filestat *fsp);
146 int devfs_filestat(struct vnode *vp, struct filestat *fsp);
147 char *getmnton(struct mount *m);
148 void pipetrans(struct pipe *pi, int i, int flag);
149 void socktrans(struct socket *sock, int i);
150 void getinetproto(int number);
151 int getfname(const char *filename);
160 struct passwd *passwd;
161 struct kinfo_proc *p, *plast;
164 char buf[_POSIX2_LINE_MAX];
168 what = KERN_PROC_ALL;
169 nlistf = memf = NULL;
170 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
190 if (!isdigit(*optarg)) {
191 warnx("-p requires a process id");
194 what = KERN_PROC_PID;
200 if (!(passwd = getpwnam(optarg)))
201 errx(1, "%s: unknown uid", optarg);
202 what = KERN_PROC_UID;
203 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)
242 if (kvm_nlist(kd, nl) != 0)
243 errx(1, "no namelist: %s", kvm_geterr(kd));
245 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
246 errx(1, "%s", kvm_geterr(kd));
249 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
252 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
253 if (checkfile && fsflg == 0)
258 for (plast = &p[cnt]; p < plast; ++p) {
259 if (p->ki_stat == SZOMB)
268 const char *Uname, *Comm;
271 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
294 * print open files attributed to this process
298 struct kinfo_proc *kp;
302 struct filedesc0 filed0;
303 #define filed filed0.fd_fd
305 Uname = user_from_uid(kp->ki_uid, 0);
309 if (kp->ki_fd == NULL)
311 if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) {
312 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
313 (void *)kp->ki_fd, Pid);
317 * root directory vnode, if one
320 vtrans(filed.fd_rdir, RDIR, FREAD);
322 * current working directory vnode
324 vtrans(filed.fd_cdir, CDIR, FREAD);
326 * ktrace vnode, if one
329 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
334 vtrans(kp->ki_textvp, TEXT, FREAD);
338 #define FPSIZE (sizeof (struct file *))
339 ALLOC_OFILES(filed.fd_lastfile+1);
340 if (filed.fd_nfiles > NDFILE) {
341 if (!KVM_READ(filed.fd_ofiles, ofiles,
342 (filed.fd_lastfile+1) * FPSIZE)) {
344 "can't read file structures at %p for pid %d\n",
345 (void *)filed.fd_ofiles, Pid);
349 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
350 for (i = 0; i <= filed.fd_lastfile; i++) {
351 if (ofiles[i] == NULL)
353 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
354 dprintf(stderr, "can't read file %d at %p for pid %d\n",
355 i, (void *)ofiles[i], Pid);
358 if (file.f_type == DTYPE_VNODE)
359 vtrans((struct vnode *)file.f_data, i, file.f_flag);
360 else if (file.f_type == DTYPE_SOCKET) {
362 socktrans((struct socket *)file.f_data, i);
365 else if (file.f_type == DTYPE_PIPE) {
367 pipetrans((struct pipe *)file.f_data, i,
372 else if (file.f_type == DTYPE_FIFO) {
374 vtrans((struct vnode *)file.f_data, i,
380 "unknown file type %d for file %d of pid %d\n",
381 file.f_type, i, Pid);
388 struct kinfo_proc *kp;
391 struct vmspace vmspace;
392 struct vm_map_entry entry;
393 vm_map_entry_t entryp;
394 struct vm_object object;
398 if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
400 "can't read vmspace at %p for pid %d\n",
401 (void *)kp->ki_vmspace, Pid);
404 map = &vmspace.vm_map;
406 for (entryp = map->header.next;
407 entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
408 if (!KVM_READ(entryp, &entry, sizeof(entry))) {
410 "can't read vm_map_entry at %p for pid %d\n",
411 (void *)entryp, Pid);
415 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
418 if ((objp = entry.object.vm_object) == NULL)
421 for (; objp; objp = object.backing_object) {
422 if (!KVM_READ(objp, &object, sizeof(object))) {
424 "can't read vm_object at %p for pid %d\n",
430 prot = entry.protection;
431 fflags = (prot & VM_PROT_READ ? FREAD : 0) |
432 (prot & VM_PROT_WRITE ? FWRITE : 0);
434 switch (object.type) {
436 vtrans((struct vnode *)object.handle, MMAP, fflags);
452 char rw[3], mode[15];
453 const char *badtype, *filename;
455 filename = badtype = NULL;
456 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
457 dprintf(stderr, "can't read vnode at %p for pid %d\n",
461 if (vn.v_type == VNON || vn.v_tag == VT_NON)
463 else if (vn.v_type == VBAD)
468 if (!ufs_filestat(&vn, &fst))
473 if (!devfs_filestat(&vn, &fst))
478 if (!nfs_filestat(&vn, &fst))
483 if (!msdosfs_filestat(&vn, &fst))
488 if (!isofs_filestat(&vn, &fst))
493 static char unknown[10];
494 sprintf(unknown, "?(%x)", vn.v_tag);
505 for (d = devs; d != NULL; d = d->next)
506 if (d->fsid == fst.fsid) {
508 if (d->ino == fst.fileid) {
513 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
518 (void)printf(" - - %10s -\n", badtype);
522 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
524 (void)printf(" %-8s", getmnton(vn.v_mount));
526 (void)sprintf(mode, "%o", fst.mode);
528 strmode(fst.mode, mode);
529 (void)printf(" %6ld %10s", fst.fileid, mode);
535 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
536 S_IFCHR : S_IFBLK)) == NULL))
537 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
539 printf(" %6s", name);
543 printf(" %6lu", fst.size);
551 if (filename && !fsflg)
552 printf(" %s", filename);
557 ufs_filestat(vp, fsp)
559 struct filestat *fsp;
563 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
564 dprintf(stderr, "can't read inode at %p for pid %d\n",
565 (void *)VTOI(vp), Pid);
569 * The st_dev from stat(2) is a udev_t. These kernel structures
570 * contain dev_t structures. We need to convert to udev to make
573 fsp->fsid = dev2udev(inode.i_dev);
574 fsp->fileid = (long)inode.i_number;
575 fsp->mode = (mode_t)inode.i_mode;
576 fsp->size = (u_long)inode.i_size;
577 fsp->rdev = inode.i_rdev;
583 devfs_filestat(vp, fsp)
585 struct filestat *fsp;
587 struct devfs_dirent devfs_dirent;
591 if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
592 dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
593 (void *)vp->v_data, Pid);
596 if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
597 dprintf(stderr, "can't read mount at %p for pid %d\n",
598 (void *)vp->v_mount, Pid);
601 if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
602 dprintf(stderr, "can't read vnode at %p for pid %d\n",
603 (void *)devfs_dirent.de_vnode, Pid);
606 fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
607 fsp->fileid = devfs_dirent.de_inode;
608 fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
610 fsp->rdev = dev2udev((dev_t)vnode.v_rdev);
616 nfs_filestat(vp, fsp)
618 struct filestat *fsp;
620 struct nfsnode nfsnode;
623 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
624 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
625 (void *)VTONFS(vp), Pid);
628 fsp->fsid = nfsnode.n_vattr.va_fsid;
629 fsp->fileid = nfsnode.n_vattr.va_fileid;
630 fsp->size = nfsnode.n_size;
631 fsp->rdev = nfsnode.n_vattr.va_rdev;
632 mode = (mode_t)nfsnode.n_vattr.va_mode;
633 switch (vp->v_type) {
669 static struct mount mount;
673 char mntonname[MNAMELEN];
677 for (mt = mhead; mt != NULL; mt = mt->next)
679 return (mt->mntonname);
680 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
681 warnx("can't read mount table at %p", (void *)m);
684 if ((mt = malloc(sizeof (struct mtab))) == NULL)
687 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
690 return (mt->mntonname);
694 pipetrans(pi, i, flag)
705 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
706 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
710 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
711 printf(" %6d", (int)pip.pipe_buffer.cnt);
730 static const char *stypename[] = {
740 struct protosw proto;
750 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
751 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
755 /* fill in protosw entry */
756 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
757 dprintf(stderr, "can't read protosw at %p",
758 (void *)so.so_proto);
763 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
764 dprintf(stderr, "can't read domain at %p\n",
765 (void *)proto.pr_domain);
769 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
770 sizeof(dname) - 1)) < 0) {
771 dprintf(stderr, "can't read domain name at %p\n",
772 (void *)dom.dom_name);
778 if ((u_short)so.so_type > STYPEMAX)
779 printf("* %s ?%d", dname, so.so_type);
781 printf("* %s %s", dname, stypename[so.so_type]);
784 * protocol specific formatting
786 * Try to find interesting things to print. For tcp, the interesting
787 * thing is the address of the tcpcb, for udp and others, just the
788 * inpcb (socket pcb). For unix domain, its the address of the socket
789 * pcb and the address of the connected pcb (if connected). Otherwise
790 * just print the protocol number and address of the socket itself.
791 * The idea is not to duplicate netstat, but to make available enough
792 * information for further analysis.
794 switch(dom.dom_family) {
797 getinetproto(proto.pr_protocol);
798 if (proto.pr_protocol == IPPROTO_TCP ) {
800 if (kvm_read(kd, (u_long)so.so_pcb,
801 (char *)&inpcb, sizeof(struct inpcb))
802 != sizeof(struct inpcb)) {
804 "can't read inpcb at %p\n",
808 printf(" %lx", (u_long)inpcb.inp_ppcb);
812 printf(" %lx", (u_long)so.so_pcb);
815 /* print address of pcb and connected pcb */
817 printf(" %lx", (u_long)so.so_pcb);
818 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
819 sizeof(struct unpcb)) != sizeof(struct unpcb)){
820 dprintf(stderr, "can't read unpcb at %p\n",
824 if (unpcb.unp_conn) {
825 char shoconn[4], *cp;
828 if (!(so.so_state & SS_CANTRCVMORE))
831 if (!(so.so_state & SS_CANTSENDMORE))
834 printf(" %s %lx", shoconn,
835 (u_long)unpcb.unp_conn);
840 /* print protocol number and socket address */
841 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
851 * Read the specinfo structure in the kernel (as pointed to by a dev_t)
852 * in order to work out the associated udev_t
860 if (KVM_READ(dev, &si, sizeof si)) {
863 dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
870 * print name of protocol number
880 setprotoent(++isopen);
881 if ((pe = getprotobynumber(number)) != NULL)
882 printf(" %s", pe->p_name);
884 printf(" %d", number);
889 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");