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>
54 #include <sys/vnode.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
60 #include <sys/unpcb.h>
61 #include <sys/sysctl.h>
62 #include <sys/filedesc.h>
63 #include <sys/queue.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/inode.h>
70 #include <sys/mount.h>
72 #include <nfs/nfsproto.h>
73 #include <nfs/rpcv2.h>
75 #include <nfs/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 __P((struct kinfo_proc *kp));
142 void dommap __P((struct kinfo_proc *kp));
143 void vtrans __P((struct vnode *vp, int i, int flag));
144 int ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
145 int nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
146 char *getmnton __P((struct mount *m));
147 void pipetrans __P((struct pipe *pi, int i, int flag));
148 void socktrans __P((struct socket *sock, int i));
149 void getinetproto __P((int number));
150 int getfname __P((char *filename));
151 void usage __P((void));
159 register struct passwd *passwd;
160 struct kinfo_proc *p, *plast;
163 char buf[_POSIX2_LINE_MAX];
167 what = KERN_PROC_ALL;
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 ALLOC_OFILES(256); /* reserve space for file pointers */
223 if (fsflg && !checkfile) {
224 /* -f with no files means use wd */
225 if (getfname(".") == 0)
231 * Discard setgid privileges if not the running kernel so that bad
232 * guys can't print interesting stuff from kernel memory.
234 if (nlistf != NULL || memf != NULL)
237 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
240 if (kvm_nlist(kd, nl) != 0)
241 errx(1, "no namelist: %s", kvm_geterr(kd));
243 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
244 errx(1, "%s", kvm_geterr(kd));
247 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
250 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
251 if (checkfile && fsflg == 0)
256 for (plast = &p[cnt]; p < plast; ++p) {
257 if (p->ki_stat == SZOMB)
269 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
292 * print open files attributed to this process
296 struct kinfo_proc *kp;
300 struct filedesc0 filed0;
301 #define filed filed0.fd_fd
303 Uname = user_from_uid(kp->ki_uid, 0);
307 if (kp->ki_fd == NULL)
309 if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) {
310 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
311 (void *)kp->ki_fd, Pid);
315 * root directory vnode, if one
318 vtrans(filed.fd_rdir, RDIR, FREAD);
320 * current working directory vnode
322 vtrans(filed.fd_cdir, CDIR, FREAD);
324 * ktrace vnode, if one
327 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
332 vtrans(kp->ki_textvp, TEXT, FREAD);
336 #define FPSIZE (sizeof (struct file *))
337 ALLOC_OFILES(filed.fd_lastfile+1);
338 if (filed.fd_nfiles > NDFILE) {
339 if (!KVM_READ(filed.fd_ofiles, ofiles,
340 (filed.fd_lastfile+1) * FPSIZE)) {
342 "can't read file structures at %p for pid %d\n",
343 (void *)filed.fd_ofiles, Pid);
347 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
348 for (i = 0; i <= filed.fd_lastfile; i++) {
349 if (ofiles[i] == NULL)
351 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
352 dprintf(stderr, "can't read file %d at %p for pid %d\n",
353 i, (void *)ofiles[i], Pid);
356 if (file.f_type == DTYPE_VNODE)
357 vtrans((struct vnode *)file.f_data, i, file.f_flag);
358 else if (file.f_type == DTYPE_SOCKET) {
360 socktrans((struct socket *)file.f_data, i);
363 else if (file.f_type == DTYPE_PIPE) {
365 pipetrans((struct pipe *)file.f_data, i,
370 else if (file.f_type == DTYPE_FIFO) {
372 vtrans((struct vnode *)file.f_data, i,
378 "unknown file type %d for file %d of pid %d\n",
379 file.f_type, i, Pid);
386 struct kinfo_proc *kp;
389 struct vmspace vmspace;
390 struct vm_map_entry entry;
391 vm_map_entry_t entryp;
392 struct vm_object object;
396 if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
398 "can't read vmspace at %p for pid %d\n",
399 (void *)kp->ki_vmspace, Pid);
402 map = &vmspace.vm_map;
404 for (entryp = map->header.next;
405 entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
406 if (!KVM_READ(entryp, &entry, sizeof(entry))) {
408 "can't read vm_map_entry at %p for pid %d\n",
409 (void *)entryp, Pid);
413 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
416 if ((objp = entry.object.vm_object) == NULL)
419 for (; objp; objp = object.backing_object) {
420 if (!KVM_READ(objp, &object, sizeof(object))) {
422 "can't read vm_object at %p for pid %d\n",
428 prot = entry.protection;
429 fflags = (prot & VM_PROT_READ ? FREAD : 0) |
430 (prot & VM_PROT_WRITE ? FWRITE : 0);
432 switch (object.type) {
434 vtrans((struct vnode *)object.handle, MMAP, fflags);
450 char rw[3], mode[15];
451 char *badtype = NULL, *filename, *getmnton();
453 filename = badtype = NULL;
454 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
455 dprintf(stderr, "can't read vnode at %p for pid %d\n",
459 if (vn.v_type == VNON || vn.v_tag == VT_NON)
461 else if (vn.v_type == VBAD)
466 if (!ufs_filestat(&vn, &fst))
470 if (!nfs_filestat(&vn, &fst))
475 if (!msdosfs_filestat(&vn, &fst))
480 if (!isofs_filestat(&vn, &fst))
485 static char unknown[10];
486 sprintf(badtype = unknown, "?(%x)", vn.v_tag);
496 for (d = devs; d != NULL; d = d->next)
497 if (d->fsid == fst.fsid) {
499 if (d->ino == fst.fileid) {
504 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
509 (void)printf(" - - %10s -\n", badtype);
513 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
515 (void)printf(" %-8s", getmnton(vn.v_mount));
517 (void)sprintf(mode, "%o", fst.mode);
519 strmode(fst.mode, mode);
520 (void)printf(" %6ld %10s", fst.fileid, mode);
526 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
527 S_IFCHR : S_IFBLK)) == NULL))
528 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
530 printf(" %6s", name);
534 printf(" %6lu", fst.size);
542 if (filename && !fsflg)
543 printf(" %s", filename);
548 ufs_filestat(vp, fsp)
550 struct filestat *fsp;
554 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
555 dprintf(stderr, "can't read inode at %p for pid %d\n",
556 (void *)VTOI(vp), Pid);
560 * The st_dev from stat(2) is a udev_t. These kernel structures
561 * contain dev_t structures. We need to convert to udev to make
564 fsp->fsid = dev2udev(inode.i_dev) & 0xffff;
565 fsp->fileid = (long)inode.i_number;
566 fsp->mode = (mode_t)inode.i_mode;
567 fsp->size = (u_long)inode.i_size;
568 fsp->rdev = inode.i_rdev;
574 nfs_filestat(vp, fsp)
576 struct filestat *fsp;
578 struct nfsnode nfsnode;
579 register mode_t mode;
581 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
582 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
583 (void *)VTONFS(vp), Pid);
586 fsp->fsid = nfsnode.n_vattr.va_fsid;
587 fsp->fileid = nfsnode.n_vattr.va_fileid;
588 fsp->size = nfsnode.n_size;
589 fsp->rdev = nfsnode.n_vattr.va_rdev;
590 mode = (mode_t)nfsnode.n_vattr.va_mode;
591 switch (vp->v_type) {
627 static struct mount mount;
631 char mntonname[MNAMELEN];
633 register struct mtab *mt;
635 for (mt = mhead; mt != NULL; mt = mt->next)
637 return (mt->mntonname);
638 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
639 warnx("can't read mount table at %p", (void *)m);
642 if ((mt = malloc(sizeof (struct mtab))) == NULL)
645 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
648 return (mt->mntonname);
652 pipetrans(pi, i, flag)
663 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
664 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
668 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
669 printf(" %6d", (int)pip.pipe_buffer.cnt);
688 static char *stypename[] = {
698 struct protosw proto;
703 char dname[32], *strcpy();
708 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
709 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
713 /* fill in protosw entry */
714 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
715 dprintf(stderr, "can't read protosw at %p",
716 (void *)so.so_proto);
721 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
722 dprintf(stderr, "can't read domain at %p\n",
723 (void *)proto.pr_domain);
727 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
728 sizeof(dname) - 1)) < 0) {
729 dprintf(stderr, "can't read domain name at %p\n",
730 (void *)dom.dom_name);
736 if ((u_short)so.so_type > STYPEMAX)
737 printf("* %s ?%d", dname, so.so_type);
739 printf("* %s %s", dname, stypename[so.so_type]);
742 * protocol specific formatting
744 * Try to find interesting things to print. For tcp, the interesting
745 * thing is the address of the tcpcb, for udp and others, just the
746 * inpcb (socket pcb). For unix domain, its the address of the socket
747 * pcb and the address of the connected pcb (if connected). Otherwise
748 * just print the protocol number and address of the socket itself.
749 * The idea is not to duplicate netstat, but to make available enough
750 * information for further analysis.
752 switch(dom.dom_family) {
755 getinetproto(proto.pr_protocol);
756 if (proto.pr_protocol == IPPROTO_TCP ) {
758 if (kvm_read(kd, (u_long)so.so_pcb,
759 (char *)&inpcb, sizeof(struct inpcb))
760 != sizeof(struct inpcb)) {
762 "can't read inpcb at %p\n",
766 printf(" %lx", (u_long)inpcb.inp_ppcb);
770 printf(" %lx", (u_long)so.so_pcb);
773 /* print address of pcb and connected pcb */
775 printf(" %lx", (u_long)so.so_pcb);
776 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
777 sizeof(struct unpcb)) != sizeof(struct unpcb)){
778 dprintf(stderr, "can't read unpcb at %p\n",
782 if (unpcb.unp_conn) {
783 char shoconn[4], *cp;
786 if (!(so.so_state & SS_CANTRCVMORE))
789 if (!(so.so_state & SS_CANTSENDMORE))
792 printf(" %s %lx", shoconn,
793 (u_long)unpcb.unp_conn);
798 /* print protocol number and socket address */
799 printf(" %d %lx", proto.pr_protocol, (u_long)sock);
809 * Read the specinfo structure in the kernel (as pointed to by a dev_t)
810 * in order to work out the associated udev_t
818 if (KVM_READ(dev, &si, sizeof si)) {
821 dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
828 * print name of protocol number
835 register struct protoent *pe;
838 setprotoent(++isopen);
839 if ((pe = getprotobynumber(number)) != NULL)
840 printf(" %s", pe->p_name);
842 printf(" %d", number);
852 if (stat(filename, &statbuf)) {
853 warn("%s", filename);
856 if ((cur = malloc(sizeof(DEVS))) == NULL)
861 cur->ino = statbuf.st_ino;
862 cur->fsid = statbuf.st_dev & 0xffff;
863 cur->name = filename;
870 (void)fprintf(stderr,
871 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");