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[] =
45 "$FreeBSD: src/usr.bin/fstat/fstat.c,v 1.21.2.7 2001/11/21 10:49:37 dwmalone Exp $";
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 <sys/mount.h>
71 #include <nfs/nfsproto.h>
72 #include <nfs/rpcv2.h>
74 #include <nfs/nfsnode.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
81 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_pcb.h>
112 struct nlist nl[] = {
117 int fsflg, /* show files on same filesystem as file(s) argument */
118 pflg, /* show files open by a particular pid */
119 uflg; /* show files open by a particular (effective) user */
120 int checkfile; /* true if restricting to particular files or filesystems */
121 int nflg; /* (numerical) display f.s. and rdev as dev_t */
122 int vflg; /* display errors in locating kernel data objects etc... */
123 int mflg; /* include memory-mapped files */
126 struct file **ofiles; /* buffer of pointers to file structures */
128 #define ALLOC_OFILES(d) \
129 if ((d) > maxfiles) { \
131 ofiles = malloc((d) * sizeof(struct file *)); \
132 if (ofiles == NULL) { \
140 void dofiles __P((struct kinfo_proc *kp));
141 void dommap __P((struct kinfo_proc *kp));
142 void vtrans __P((struct vnode *vp, int i, int flag));
143 int ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
144 int nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
145 char *getmnton __P((struct mount *m));
146 void pipetrans __P((struct pipe *pi, int i, int flag));
147 void socktrans __P((struct socket *sock, int i));
148 void getinetproto __P((int number));
149 int getfname __P((char *filename));
150 void usage __P((void));
158 register struct passwd *passwd;
159 struct kinfo_proc *p, *plast;
162 char buf[_POSIX2_LINE_MAX];
166 what = KERN_PROC_ALL;
167 nlistf = memf = NULL;
168 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
188 if (!isdigit(*optarg)) {
189 warnx("-p requires a process id");
192 what = KERN_PROC_PID;
198 if (!(passwd = getpwnam(optarg)))
199 errx(1, "%s: unknown uid", optarg);
200 what = KERN_PROC_UID;
201 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));
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)
255 for (plast = &p[cnt]; p < plast; ++p) {
256 if (p->kp_proc.p_stat == SZOMB)
268 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
291 * print open files attributed to this process
295 struct kinfo_proc *kp;
299 struct filedesc0 filed0;
300 #define filed filed0.fd_fd
301 struct proc *p = &kp->kp_proc;
302 struct eproc *ep = &kp->kp_eproc;
304 Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
310 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
311 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
312 (void *)p->p_fd, Pid);
316 * root directory vnode, if one
319 vtrans(filed.fd_rdir, RDIR, FREAD);
321 * current working directory vnode
323 vtrans(filed.fd_cdir, CDIR, FREAD);
325 * ktrace vnode, if one
328 vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
333 vtrans(p->p_textvp, TEXT, FREAD);
337 #define FPSIZE (sizeof (struct file *))
338 ALLOC_OFILES(filed.fd_lastfile+1);
339 if (filed.fd_nfiles > NDFILE) {
340 if (!KVM_READ(filed.fd_ofiles, ofiles,
341 (filed.fd_lastfile+1) * FPSIZE)) {
343 "can't read file structures at %p for pid %d\n",
344 (void *)filed.fd_ofiles, Pid);
348 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
349 for (i = 0; i <= filed.fd_lastfile; i++) {
350 if (ofiles[i] == NULL)
352 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
353 dprintf(stderr, "can't read file %d at %p for pid %d\n",
354 i, (void *)ofiles[i], Pid);
357 if (file.f_type == DTYPE_VNODE)
358 vtrans((struct vnode *)file.f_data, i, file.f_flag);
359 else if (file.f_type == DTYPE_SOCKET) {
361 socktrans((struct socket *)file.f_data, i);
364 else if (file.f_type == DTYPE_PIPE) {
366 pipetrans((struct pipe *)file.f_data, i,
371 else if (file.f_type == DTYPE_FIFO) {
373 vtrans((struct vnode *)file.f_data, i,
379 "unknown file type %d for file %d of pid %d\n",
380 file.f_type, i, Pid);
387 struct kinfo_proc *kp;
389 struct proc *p = &kp->kp_proc;
390 struct vmspace vmspace;
392 struct vm_map_entry entry;
393 vm_map_entry_t entryp;
394 struct vm_object object;
398 if (!KVM_READ(p->p_vmspace, &vmspace, sizeof(vmspace))) {
399 dprintf(stderr, "can't read vmspace at %p for pid %d\n",
400 (void *)p->p_vmspace, Pid);
404 map = &vmspace.vm_map;
406 for (entryp = map->header.next; entryp != &p->p_vmspace->vm_map.header;
407 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 char *badtype = NULL, *filename, *getmnton();
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))
472 if (!ufs_filestat(&vn, &fst))
476 if (!nfs_filestat(&vn, &fst))
481 if (!msdosfs_filestat(&vn, &fst))
486 if (!isofs_filestat(&vn, &fst))
491 static char unknown[10];
492 sprintf(badtype = unknown, "?(%x)", vn.v_tag);
502 for (d = devs; d != NULL; d = d->next)
503 if (d->fsid == fst.fsid) {
505 if (d->ino == fst.fileid) {
510 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
515 (void)printf(" - - %10s -\n", badtype);
519 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
521 (void)printf(" %-8s", getmnton(vn.v_mount));
523 (void)sprintf(mode, "%o", fst.mode);
525 strmode(fst.mode, mode);
526 (void)printf(" %6ld %10s", fst.fileid, mode);
532 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
533 S_IFCHR : S_IFBLK)) == NULL))
534 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
536 printf(" %6s", name);
540 printf(" %6lu", fst.size);
548 if (filename && !fsflg)
549 printf(" %s", filename);
554 ufs_filestat(vp, fsp)
556 struct filestat *fsp;
560 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
561 dprintf(stderr, "can't read inode at %p for pid %d\n",
562 (void *)VTOI(vp), Pid);
566 * The st_dev from stat(2) is a udev_t. These kernel structures
567 * contain dev_t structures. We need to convert to udev to make
570 fsp->fsid = dev2udev(inode.i_dev);
571 fsp->fileid = (long)inode.i_number;
572 fsp->mode = (mode_t)inode.i_mode;
573 fsp->size = (u_long)inode.i_size;
574 fsp->rdev = inode.i_rdev;
580 nfs_filestat(vp, fsp)
582 struct filestat *fsp;
584 struct nfsnode nfsnode;
585 register mode_t mode;
587 if (!KVM_READ(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) {
633 static struct mount mount;
637 char mntonname[MNAMELEN];
639 register struct mtab *mt;
641 for (mt = mhead; mt != NULL; mt = mt->next)
643 return (mt->mntonname);
644 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
645 warnx("can't read mount table at %p", (void *)m);
648 if ((mt = malloc(sizeof (struct mtab))) == NULL)
651 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
654 return (mt->mntonname);
658 pipetrans(pi, i, flag)
669 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
670 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
674 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
675 printf(" %6d", (int)pip.pipe_buffer.cnt);
694 static char *stypename[] = {
704 struct protosw proto;
709 char dname[32], *strcpy();
714 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
715 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
719 /* fill in protosw entry */
720 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
721 dprintf(stderr, "can't read protosw at %p",
722 (void *)so.so_proto);
727 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
728 dprintf(stderr, "can't read domain at %p\n",
729 (void *)proto.pr_domain);
733 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
734 sizeof(dname) - 1)) < 0) {
735 dprintf(stderr, "can't read domain name at %p\n",
736 (void *)dom.dom_name);
742 if ((u_short)so.so_type > STYPEMAX)
743 printf("* %s ?%d", dname, so.so_type);
745 printf("* %s %s", dname, stypename[so.so_type]);
748 * protocol specific formatting
750 * Try to find interesting things to print. For tcp, the interesting
751 * thing is the address of the tcpcb, for udp and others, just the
752 * inpcb (socket pcb). For unix domain, its the address of the socket
753 * pcb and the address of the connected pcb (if connected). Otherwise
754 * just print the protocol number and address of the socket itself.
755 * The idea is not to duplicate netstat, but to make available enough
756 * information for further analysis.
758 switch(dom.dom_family) {
761 getinetproto(proto.pr_protocol);
762 if (proto.pr_protocol == IPPROTO_TCP ) {
764 if (kvm_read(kd, (u_long)so.so_pcb,
765 (char *)&inpcb, sizeof(struct inpcb))
766 != sizeof(struct inpcb)) {
768 "can't read inpcb at %p\n",
772 printf(" %lx", (u_long)inpcb.inp_ppcb);
776 printf(" %lx", (u_long)so.so_pcb);
779 /* print address of pcb and connected pcb */
781 printf(" %lx", (u_long)so.so_pcb);
782 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
783 sizeof(struct unpcb)) != sizeof(struct unpcb)){
784 dprintf(stderr, "can't read unpcb at %p\n",
788 if (unpcb.unp_conn) {
789 char shoconn[4], *cp;
792 if (!(so.so_state & SS_CANTRCVMORE))
795 if (!(so.so_state & SS_CANTSENDMORE))
798 printf(" %s %lx", shoconn,
799 (u_long)unpcb.unp_conn);
804 /* print protocol number and socket address */
805 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
815 * Read the specinfo structure in the kernel (as pointed to by a dev_t)
816 * in order to work out the associated udev_t
824 if (KVM_READ(dev, &si, sizeof si)) {
827 dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
834 * print name of protocol number
841 register struct protoent *pe;
844 setprotoent(++isopen);
845 if ((pe = getprotobynumber(number)) != NULL)
846 printf(" %s", pe->p_name);
848 printf(" %d", number);
858 if (stat(filename, &statbuf)) {
859 warn("%s", filename);
862 if ((cur = malloc(sizeof(DEVS))) == NULL)
867 cur->ino = statbuf.st_ino;
868 cur->fsid = statbuf.st_dev;
869 cur->name = filename;
876 (void)fprintf(stderr,
877 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");