fstat.c revision 17808
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1988, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 351590Srgrimesstatic char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1988, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 411590Srgrimesstatic char sccsid[] = "@(#)fstat.c 8.1 (Berkeley) 6/6/93"; 421590Srgrimes#endif /* not lint */ 431590Srgrimes 441590Srgrimes#include <sys/param.h> 451590Srgrimes#include <sys/time.h> 461590Srgrimes#include <sys/proc.h> 471590Srgrimes#include <sys/user.h> 481590Srgrimes#include <sys/stat.h> 491590Srgrimes#include <sys/vnode.h> 501590Srgrimes#include <sys/socket.h> 511590Srgrimes#include <sys/socketvar.h> 521590Srgrimes#include <sys/domain.h> 531590Srgrimes#include <sys/protosw.h> 541590Srgrimes#include <sys/unpcb.h> 551590Srgrimes#include <sys/sysctl.h> 561590Srgrimes#include <sys/filedesc.h> 577726Sdg#include <sys/queue.h> 5817808Speter#include <sys/pipe.h> 591590Srgrimes#define KERNEL 601590Srgrimes#include <sys/file.h> 611590Srgrimes#include <ufs/ufs/quota.h> 621590Srgrimes#include <ufs/ufs/inode.h> 631590Srgrimes#undef KERNEL 641590Srgrimes#define NFS 651590Srgrimes#include <sys/mount.h> 669336Sdfr#include <nfs/nfsproto.h> 671590Srgrimes#include <nfs/rpcv2.h> 681590Srgrimes#include <nfs/nfs.h> 691590Srgrimes#include <nfs/nfsnode.h> 701590Srgrimes#undef NFS 711590Srgrimes 721590Srgrimes#include <net/route.h> 731590Srgrimes#include <netinet/in.h> 741590Srgrimes#include <netinet/in_systm.h> 751590Srgrimes#include <netinet/ip.h> 761590Srgrimes#include <netinet/in_pcb.h> 771590Srgrimes 781590Srgrimes#include <ctype.h> 791590Srgrimes#include <errno.h> 801590Srgrimes#include <kvm.h> 811590Srgrimes#include <nlist.h> 821590Srgrimes#include <paths.h> 831590Srgrimes#include <pwd.h> 841590Srgrimes#include <stdio.h> 851590Srgrimes#include <stdlib.h> 861590Srgrimes#include <string.h> 871590Srgrimes 881590Srgrimes#define TEXT -1 891590Srgrimes#define CDIR -2 901590Srgrimes#define RDIR -3 911590Srgrimes#define TRACE -4 921590Srgrimes 931590Srgrimestypedef struct devs { 941590Srgrimes struct devs *next; 951590Srgrimes long fsid; 961590Srgrimes ino_t ino; 971590Srgrimes char *name; 981590Srgrimes} DEVS; 991590SrgrimesDEVS *devs; 1001590Srgrimes 1011590Srgrimesstruct filestat { 1021590Srgrimes long fsid; 1031590Srgrimes long fileid; 1041590Srgrimes mode_t mode; 1051590Srgrimes u_long size; 1061590Srgrimes dev_t rdev; 1071590Srgrimes}; 1081590Srgrimes 1091590Srgrimes#ifdef notdef 1101590Srgrimesstruct nlist nl[] = { 1111590Srgrimes { "" }, 1121590Srgrimes}; 1131590Srgrimes#endif 1141590Srgrimes 1151590Srgrimesint fsflg, /* show files on same filesystem as file(s) argument */ 1161590Srgrimes pflg, /* show files open by a particular pid */ 1171590Srgrimes uflg; /* show files open by a particular (effective) user */ 1181590Srgrimesint checkfile; /* true if restricting to particular files or filesystems */ 1191590Srgrimesint nflg; /* (numerical) display f.s. and rdev as dev_t */ 1201590Srgrimesint vflg; /* display errors in locating kernel data objects etc... */ 1211590Srgrimes 1221590Srgrimes#define dprintf if (vflg) fprintf 1231590Srgrimes 1241590Srgrimesstruct file **ofiles; /* buffer of pointers to file structures */ 1251590Srgrimesint maxfiles; 1261590Srgrimes#define ALLOC_OFILES(d) \ 1271590Srgrimes if ((d) > maxfiles) { \ 1281590Srgrimes free(ofiles); \ 1291590Srgrimes ofiles = malloc((d) * sizeof(struct file *)); \ 1301590Srgrimes if (ofiles == NULL) { \ 1311590Srgrimes fprintf(stderr, "fstat: %s\n", strerror(errno)); \ 1321590Srgrimes exit(1); \ 1331590Srgrimes } \ 1341590Srgrimes maxfiles = (d); \ 1351590Srgrimes } 1361590Srgrimes 1371590Srgrimes/* 1388874Srgrimes * a kvm_read that returns true if everything is read 1391590Srgrimes */ 1401590Srgrimes#define KVM_READ(kaddr, paddr, len) \ 1411590Srgrimes (kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len)) 1421590Srgrimes 1431590Srgrimeskvm_t *kd; 1441590Srgrimes 14517808Spetervoid dofiles __P((struct kinfo_proc *kp)); 14617808Spetervoid vtrans __P((struct vnode *vp, int i, int flag)); 14717808Speterint ufs_filestat __P((struct vnode *vp, struct filestat *fsp)); 14817808Speterint nfs_filestat __P((struct vnode *vp, struct filestat *fsp)); 14917808Speterchar *getmnton __P((struct mount *m)); 15017808Spetervoid pipetrans __P((struct pipe *pi, int i, int flag)); 15117808Spetervoid socktrans __P((struct socket *sock, int i)); 15217808Spetervoid getinetproto __P((int number)); 15317808Speterint getfname __P((char *filename)); 15417808Spetervoid usage __P((void)); 1551590Srgrimes 15617808Speter 15717808Speterint 1581590Srgrimesmain(argc, argv) 1591590Srgrimes int argc; 1601590Srgrimes char **argv; 1611590Srgrimes{ 1621590Srgrimes register struct passwd *passwd; 1631590Srgrimes struct kinfo_proc *p, *plast; 1641590Srgrimes int arg, ch, what; 1651590Srgrimes char *memf, *nlistf; 1661590Srgrimes int cnt; 1671590Srgrimes 1681590Srgrimes arg = 0; 1691590Srgrimes what = KERN_PROC_ALL; 1701590Srgrimes nlistf = memf = NULL; 1711590Srgrimes while ((ch = getopt(argc, argv, "fnp:u:vNM")) != EOF) 1721590Srgrimes switch((char)ch) { 1731590Srgrimes case 'f': 1741590Srgrimes fsflg = 1; 1751590Srgrimes break; 1761590Srgrimes case 'M': 1771590Srgrimes memf = optarg; 1781590Srgrimes break; 1791590Srgrimes case 'N': 1801590Srgrimes nlistf = optarg; 1811590Srgrimes break; 1821590Srgrimes case 'n': 1831590Srgrimes nflg = 1; 1841590Srgrimes break; 1851590Srgrimes case 'p': 1861590Srgrimes if (pflg++) 1871590Srgrimes usage(); 1881590Srgrimes if (!isdigit(*optarg)) { 1891590Srgrimes fprintf(stderr, 1901590Srgrimes "fstat: -p requires a process id\n"); 1911590Srgrimes usage(); 1921590Srgrimes } 1931590Srgrimes what = KERN_PROC_PID; 1941590Srgrimes arg = atoi(optarg); 1951590Srgrimes break; 1961590Srgrimes case 'u': 1971590Srgrimes if (uflg++) 1981590Srgrimes usage(); 1991590Srgrimes if (!(passwd = getpwnam(optarg))) { 2001590Srgrimes fprintf(stderr, "%s: unknown uid\n", 2011590Srgrimes optarg); 2021590Srgrimes exit(1); 2031590Srgrimes } 2041590Srgrimes what = KERN_PROC_UID; 2051590Srgrimes arg = passwd->pw_uid; 2061590Srgrimes break; 2071590Srgrimes case 'v': 2081590Srgrimes vflg = 1; 2091590Srgrimes break; 2101590Srgrimes case '?': 2111590Srgrimes default: 2121590Srgrimes usage(); 2131590Srgrimes } 2141590Srgrimes 2151590Srgrimes if (*(argv += optind)) { 2161590Srgrimes for (; *argv; ++argv) { 2171590Srgrimes if (getfname(*argv)) 2181590Srgrimes checkfile = 1; 2191590Srgrimes } 2201590Srgrimes if (!checkfile) /* file(s) specified, but none accessable */ 2211590Srgrimes exit(1); 2221590Srgrimes } 2231590Srgrimes 2241590Srgrimes ALLOC_OFILES(256); /* reserve space for file pointers */ 2251590Srgrimes 2268874Srgrimes if (fsflg && !checkfile) { 2271590Srgrimes /* -f with no files means use wd */ 2281590Srgrimes if (getfname(".") == 0) 2291590Srgrimes exit(1); 2301590Srgrimes checkfile = 1; 2311590Srgrimes } 2321590Srgrimes 2331590Srgrimes /* 2341590Srgrimes * Discard setgid privileges if not the running kernel so that bad 2351590Srgrimes * guys can't print interesting stuff from kernel memory. 2361590Srgrimes */ 2371590Srgrimes if (nlistf != NULL || memf != NULL) 2381590Srgrimes setgid(getgid()); 2391590Srgrimes 2401590Srgrimes if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, NULL)) == NULL) { 2411590Srgrimes fprintf(stderr, "fstat: %s\n", kvm_geterr(kd)); 2421590Srgrimes exit(1); 2431590Srgrimes } 2441590Srgrimes#ifdef notdef 2451590Srgrimes if (kvm_nlist(kd, nl) != 0) { 2461590Srgrimes fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr(kd)); 2471590Srgrimes exit(1); 2481590Srgrimes } 2491590Srgrimes#endif 2501590Srgrimes if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) { 2511590Srgrimes fprintf(stderr, "fstat: %s\n", kvm_geterr(kd)); 2521590Srgrimes exit(1); 2531590Srgrimes } 2541590Srgrimes if (nflg) 2551590Srgrimes printf("%s", 2561590Srgrimes"USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 2571590Srgrimes else 2581590Srgrimes printf("%s", 2591590Srgrimes"USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 2601590Srgrimes if (checkfile && fsflg == 0) 2611590Srgrimes printf(" NAME\n"); 2621590Srgrimes else 2631590Srgrimes putchar('\n'); 2641590Srgrimes 2651590Srgrimes for (plast = &p[cnt]; p < plast; ++p) { 2661590Srgrimes if (p->kp_proc.p_stat == SZOMB) 2671590Srgrimes continue; 2681590Srgrimes dofiles(p); 2691590Srgrimes } 2701590Srgrimes exit(0); 2711590Srgrimes} 2721590Srgrimes 2731590Srgrimeschar *Uname, *Comm; 2741590Srgrimesint Pid; 2751590Srgrimes 2761590Srgrimes#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 2771590Srgrimes switch(i) { \ 2781590Srgrimes case TEXT: \ 2791590Srgrimes printf(" text"); \ 2801590Srgrimes break; \ 2811590Srgrimes case CDIR: \ 2821590Srgrimes printf(" wd"); \ 2831590Srgrimes break; \ 2841590Srgrimes case RDIR: \ 2851590Srgrimes printf(" root"); \ 2861590Srgrimes break; \ 2871590Srgrimes case TRACE: \ 2881590Srgrimes printf(" tr"); \ 2891590Srgrimes break; \ 2901590Srgrimes default: \ 2911590Srgrimes printf(" %4d", i); \ 2921590Srgrimes break; \ 2931590Srgrimes } 2941590Srgrimes 2951590Srgrimes/* 2961590Srgrimes * print open files attributed to this process 2971590Srgrimes */ 2981590Srgrimesvoid 2991590Srgrimesdofiles(kp) 3001590Srgrimes struct kinfo_proc *kp; 3011590Srgrimes{ 3021590Srgrimes int i, last; 3031590Srgrimes struct file file; 3041590Srgrimes struct filedesc0 filed0; 3051590Srgrimes#define filed filed0.fd_fd 3061590Srgrimes struct proc *p = &kp->kp_proc; 3071590Srgrimes struct eproc *ep = &kp->kp_eproc; 3081590Srgrimes 3091590Srgrimes Uname = user_from_uid(ep->e_ucred.cr_uid, 0); 3101590Srgrimes Pid = p->p_pid; 3111590Srgrimes Comm = p->p_comm; 3121590Srgrimes 3131590Srgrimes if (p->p_fd == NULL) 3141590Srgrimes return; 3151590Srgrimes if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) { 3161590Srgrimes dprintf(stderr, "can't read filedesc at %x for pid %d\n", 3171590Srgrimes p->p_fd, Pid); 3181590Srgrimes return; 3191590Srgrimes } 3201590Srgrimes /* 3211590Srgrimes * root directory vnode, if one 3221590Srgrimes */ 3231590Srgrimes if (filed.fd_rdir) 3241590Srgrimes vtrans(filed.fd_rdir, RDIR, FREAD); 3251590Srgrimes /* 3261590Srgrimes * current working directory vnode 3271590Srgrimes */ 3281590Srgrimes vtrans(filed.fd_cdir, CDIR, FREAD); 3291590Srgrimes /* 3301590Srgrimes * ktrace vnode, if one 3311590Srgrimes */ 3321590Srgrimes if (p->p_tracep) 3331590Srgrimes vtrans(p->p_tracep, TRACE, FREAD|FWRITE); 3341590Srgrimes /* 3351590Srgrimes * open files 3361590Srgrimes */ 3371590Srgrimes#define FPSIZE (sizeof (struct file *)) 3381590Srgrimes ALLOC_OFILES(filed.fd_lastfile+1); 3391590Srgrimes if (filed.fd_nfiles > NDFILE) { 3401590Srgrimes if (!KVM_READ(filed.fd_ofiles, ofiles, 3411590Srgrimes (filed.fd_lastfile+1) * FPSIZE)) { 3421590Srgrimes dprintf(stderr, 3431590Srgrimes "can't read file structures at %x for pid %d\n", 3441590Srgrimes filed.fd_ofiles, Pid); 3451590Srgrimes return; 3461590Srgrimes } 3471590Srgrimes } else 3481590Srgrimes bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 3491590Srgrimes for (i = 0; i <= filed.fd_lastfile; i++) { 3501590Srgrimes if (ofiles[i] == NULL) 3511590Srgrimes continue; 3521590Srgrimes if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 3531590Srgrimes dprintf(stderr, "can't read file %d at %x for pid %d\n", 3541590Srgrimes i, ofiles[i], Pid); 3551590Srgrimes continue; 3561590Srgrimes } 3571590Srgrimes if (file.f_type == DTYPE_VNODE) 3581590Srgrimes vtrans((struct vnode *)file.f_data, i, file.f_flag); 3591590Srgrimes else if (file.f_type == DTYPE_SOCKET) { 3601590Srgrimes if (checkfile == 0) 3611590Srgrimes socktrans((struct socket *)file.f_data, i); 3621590Srgrimes } 36317808Speter#ifdef DTYPE_PIPE 36417808Speter else if (file.f_type == DTYPE_PIPE) { 36517808Speter if (checkfile == 0) 36617808Speter pipetrans((struct pipe *)file.f_data, i, 36717808Speter file.f_flag); 36817808Speter } 36917808Speter#endif 3701590Srgrimes else { 3718874Srgrimes dprintf(stderr, 3721590Srgrimes "unknown file type %d for file %d of pid %d\n", 3731590Srgrimes file.f_type, i, Pid); 3741590Srgrimes } 3751590Srgrimes } 3761590Srgrimes} 3771590Srgrimes 3781590Srgrimesvoid 3791590Srgrimesvtrans(vp, i, flag) 3801590Srgrimes struct vnode *vp; 3811590Srgrimes int i; 3821590Srgrimes int flag; 3831590Srgrimes{ 3841590Srgrimes struct vnode vn; 3851590Srgrimes struct filestat fst; 3861590Srgrimes char rw[3], mode[15]; 3871590Srgrimes char *badtype = NULL, *filename, *getmnton(); 3881590Srgrimes 3891590Srgrimes filename = badtype = NULL; 3901590Srgrimes if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 3911590Srgrimes dprintf(stderr, "can't read vnode at %x for pid %d\n", 3921590Srgrimes vp, Pid); 3931590Srgrimes return; 3941590Srgrimes } 3951590Srgrimes if (vn.v_type == VNON || vn.v_tag == VT_NON) 3961590Srgrimes badtype = "none"; 3971590Srgrimes else if (vn.v_type == VBAD) 3981590Srgrimes badtype = "bad"; 3991590Srgrimes else 4001590Srgrimes switch (vn.v_tag) { 4011590Srgrimes case VT_UFS: 4021590Srgrimes if (!ufs_filestat(&vn, &fst)) 4031590Srgrimes badtype = "error"; 4041590Srgrimes break; 4051590Srgrimes case VT_MFS: 4061590Srgrimes if (!ufs_filestat(&vn, &fst)) 4071590Srgrimes badtype = "error"; 4081590Srgrimes break; 4091590Srgrimes case VT_NFS: 4101590Srgrimes if (!nfs_filestat(&vn, &fst)) 4111590Srgrimes badtype = "error"; 4121590Srgrimes break; 4131590Srgrimes default: { 4141590Srgrimes static char unknown[10]; 4151590Srgrimes sprintf(badtype = unknown, "?(%x)", vn.v_tag); 4161590Srgrimes break;; 4171590Srgrimes } 4181590Srgrimes } 4191590Srgrimes if (checkfile) { 4201590Srgrimes int fsmatch = 0; 4211590Srgrimes register DEVS *d; 4221590Srgrimes 4231590Srgrimes if (badtype) 4241590Srgrimes return; 4251590Srgrimes for (d = devs; d != NULL; d = d->next) 4261590Srgrimes if (d->fsid == fst.fsid) { 4271590Srgrimes fsmatch = 1; 4281590Srgrimes if (d->ino == fst.fileid) { 4291590Srgrimes filename = d->name; 4301590Srgrimes break; 4311590Srgrimes } 4321590Srgrimes } 4331590Srgrimes if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 4341590Srgrimes return; 4351590Srgrimes } 4361590Srgrimes PREFIX(i); 4371590Srgrimes if (badtype) { 4381590Srgrimes (void)printf(" - - %10s -\n", badtype); 4391590Srgrimes return; 4401590Srgrimes } 4411590Srgrimes if (nflg) 4421590Srgrimes (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 4431590Srgrimes else 4441590Srgrimes (void)printf(" %-8s", getmnton(vn.v_mount)); 4451590Srgrimes if (nflg) 4461590Srgrimes (void)sprintf(mode, "%o", fst.mode); 4471590Srgrimes else 4481590Srgrimes strmode(fst.mode, mode); 4491590Srgrimes (void)printf(" %6d %10s", fst.fileid, mode); 4501590Srgrimes switch (vn.v_type) { 4511590Srgrimes case VBLK: 4521590Srgrimes case VCHR: { 4531590Srgrimes char *name; 4541590Srgrimes 4558874Srgrimes if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 4561590Srgrimes S_IFCHR : S_IFBLK)) == NULL)) 4571590Srgrimes printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 4581590Srgrimes else 4591590Srgrimes printf(" %6s", name); 4601590Srgrimes break; 4611590Srgrimes } 4621590Srgrimes default: 4631590Srgrimes printf(" %6d", fst.size); 4641590Srgrimes } 4651590Srgrimes rw[0] = '\0'; 4661590Srgrimes if (flag & FREAD) 4671590Srgrimes strcat(rw, "r"); 4681590Srgrimes if (flag & FWRITE) 4691590Srgrimes strcat(rw, "w"); 4701590Srgrimes printf(" %2s", rw); 4711590Srgrimes if (filename && !fsflg) 4721590Srgrimes printf(" %s", filename); 4731590Srgrimes putchar('\n'); 4741590Srgrimes} 4751590Srgrimes 4761590Srgrimesint 4771590Srgrimesufs_filestat(vp, fsp) 4781590Srgrimes struct vnode *vp; 4791590Srgrimes struct filestat *fsp; 4801590Srgrimes{ 4811590Srgrimes struct inode inode; 4821590Srgrimes 4831590Srgrimes if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 4841590Srgrimes dprintf(stderr, "can't read inode at %x for pid %d\n", 4851590Srgrimes VTOI(vp), Pid); 4861590Srgrimes return 0; 4871590Srgrimes } 4881590Srgrimes fsp->fsid = inode.i_dev & 0xffff; 4891590Srgrimes fsp->fileid = (long)inode.i_number; 4901590Srgrimes fsp->mode = (mode_t)inode.i_mode; 4911590Srgrimes fsp->size = (u_long)inode.i_size; 4921590Srgrimes fsp->rdev = inode.i_rdev; 4931590Srgrimes 4941590Srgrimes return 1; 4951590Srgrimes} 4961590Srgrimes 4971590Srgrimesint 4981590Srgrimesnfs_filestat(vp, fsp) 4991590Srgrimes struct vnode *vp; 5001590Srgrimes struct filestat *fsp; 5011590Srgrimes{ 5021590Srgrimes struct nfsnode nfsnode; 5031590Srgrimes register mode_t mode; 5041590Srgrimes 5051590Srgrimes if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 5061590Srgrimes dprintf(stderr, "can't read nfsnode at %x for pid %d\n", 5071590Srgrimes VTONFS(vp), Pid); 5081590Srgrimes return 0; 5091590Srgrimes } 5101590Srgrimes fsp->fsid = nfsnode.n_vattr.va_fsid; 5111590Srgrimes fsp->fileid = nfsnode.n_vattr.va_fileid; 5121590Srgrimes fsp->size = nfsnode.n_size; 5131590Srgrimes fsp->rdev = nfsnode.n_vattr.va_rdev; 5141590Srgrimes mode = (mode_t)nfsnode.n_vattr.va_mode; 5151590Srgrimes switch (vp->v_type) { 5161590Srgrimes case VREG: 5171590Srgrimes mode |= S_IFREG; 5181590Srgrimes break; 5191590Srgrimes case VDIR: 5201590Srgrimes mode |= S_IFDIR; 5211590Srgrimes break; 5221590Srgrimes case VBLK: 5231590Srgrimes mode |= S_IFBLK; 5241590Srgrimes break; 5251590Srgrimes case VCHR: 5261590Srgrimes mode |= S_IFCHR; 5271590Srgrimes break; 5281590Srgrimes case VLNK: 5291590Srgrimes mode |= S_IFLNK; 5301590Srgrimes break; 5311590Srgrimes case VSOCK: 5321590Srgrimes mode |= S_IFSOCK; 5331590Srgrimes break; 5341590Srgrimes case VFIFO: 5351590Srgrimes mode |= S_IFIFO; 5361590Srgrimes break; 5371590Srgrimes }; 5381590Srgrimes fsp->mode = mode; 5391590Srgrimes 5401590Srgrimes return 1; 5411590Srgrimes} 5421590Srgrimes 5431590Srgrimes 5441590Srgrimeschar * 5451590Srgrimesgetmnton(m) 5461590Srgrimes struct mount *m; 5471590Srgrimes{ 5481590Srgrimes static struct mount mount; 5491590Srgrimes static struct mtab { 5501590Srgrimes struct mtab *next; 5511590Srgrimes struct mount *m; 5521590Srgrimes char mntonname[MNAMELEN]; 5531590Srgrimes } *mhead = NULL; 5541590Srgrimes register struct mtab *mt; 5551590Srgrimes 5561590Srgrimes for (mt = mhead; mt != NULL; mt = mt->next) 5571590Srgrimes if (m == mt->m) 5581590Srgrimes return (mt->mntonname); 5591590Srgrimes if (!KVM_READ(m, &mount, sizeof(struct mount))) { 5601590Srgrimes fprintf(stderr, "can't read mount table at %x\n", m); 5611590Srgrimes return (NULL); 5621590Srgrimes } 5631590Srgrimes if ((mt = malloc(sizeof (struct mtab))) == NULL) { 5641590Srgrimes fprintf(stderr, "fstat: %s\n", strerror(errno)); 5651590Srgrimes exit(1); 5661590Srgrimes } 5671590Srgrimes mt->m = m; 5681590Srgrimes bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 5691590Srgrimes mt->next = mhead; 5701590Srgrimes mhead = mt; 5711590Srgrimes return (mt->mntonname); 5721590Srgrimes} 5731590Srgrimes 5741590Srgrimesvoid 57517808Speterpipetrans(pi, i, flag) 57617808Speter struct pipe *pi; 57717808Speter int i; 57817808Speter int flag; 57917808Speter{ 58017808Speter struct pipe pip; 58117808Speter char rw[3]; 58217808Speter 58317808Speter PREFIX(i); 58417808Speter 58517808Speter /* fill in socket */ 58617808Speter if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { 58717808Speter dprintf(stderr, "can't read pipe at %x\n", pi); 58817808Speter goto bad; 58917808Speter } 59017808Speter 59117808Speter printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer); 59217808Speter printf(" %6d", (int)pip.pipe_buffer.cnt); 59317808Speter rw[0] = '\0'; 59417808Speter if (flag & FREAD) 59517808Speter strcat(rw, "r"); 59617808Speter if (flag & FWRITE) 59717808Speter strcat(rw, "w"); 59817808Speter printf(" %2s", rw); 59917808Speter putchar('\n'); 60017808Speter return; 60117808Speter 60217808Speterbad: 60317808Speter printf("* error\n"); 60417808Speter} 60517808Speter 60617808Spetervoid 6071590Srgrimessocktrans(sock, i) 6081590Srgrimes struct socket *sock; 6091590Srgrimes int i; 6101590Srgrimes{ 6111590Srgrimes static char *stypename[] = { 6121590Srgrimes "unused", /* 0 */ 6131590Srgrimes "stream", /* 1 */ 6141590Srgrimes "dgram", /* 2 */ 6151590Srgrimes "raw", /* 3 */ 6161590Srgrimes "rdm", /* 4 */ 6171590Srgrimes "seqpak" /* 5 */ 6181590Srgrimes }; 6191590Srgrimes#define STYPEMAX 5 6201590Srgrimes struct socket so; 6211590Srgrimes struct protosw proto; 6221590Srgrimes struct domain dom; 6231590Srgrimes struct inpcb inpcb; 6241590Srgrimes struct unpcb unpcb; 6251590Srgrimes int len; 6261590Srgrimes char dname[32], *strcpy(); 6271590Srgrimes 6281590Srgrimes PREFIX(i); 6291590Srgrimes 6301590Srgrimes /* fill in socket */ 6311590Srgrimes if (!KVM_READ(sock, &so, sizeof(struct socket))) { 6321590Srgrimes dprintf(stderr, "can't read sock at %x\n", sock); 6331590Srgrimes goto bad; 6341590Srgrimes } 6351590Srgrimes 6361590Srgrimes /* fill in protosw entry */ 6371590Srgrimes if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 6381590Srgrimes dprintf(stderr, "can't read protosw at %x", so.so_proto); 6391590Srgrimes goto bad; 6401590Srgrimes } 6411590Srgrimes 6421590Srgrimes /* fill in domain */ 6431590Srgrimes if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 6441590Srgrimes dprintf(stderr, "can't read domain at %x\n", proto.pr_domain); 6451590Srgrimes goto bad; 6461590Srgrimes } 6471590Srgrimes 6481590Srgrimes if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 6491590Srgrimes sizeof(dname) - 1)) < 0) { 6501590Srgrimes dprintf(stderr, "can't read domain name at %x\n", 6511590Srgrimes dom.dom_name); 6521590Srgrimes dname[0] = '\0'; 6531590Srgrimes } 6541590Srgrimes else 6551590Srgrimes dname[len] = '\0'; 6561590Srgrimes 6571590Srgrimes if ((u_short)so.so_type > STYPEMAX) 6581590Srgrimes printf("* %s ?%d", dname, so.so_type); 6591590Srgrimes else 6601590Srgrimes printf("* %s %s", dname, stypename[so.so_type]); 6611590Srgrimes 6628874Srgrimes /* 6631590Srgrimes * protocol specific formatting 6641590Srgrimes * 6651590Srgrimes * Try to find interesting things to print. For tcp, the interesting 6661590Srgrimes * thing is the address of the tcpcb, for udp and others, just the 6671590Srgrimes * inpcb (socket pcb). For unix domain, its the address of the socket 6681590Srgrimes * pcb and the address of the connected pcb (if connected). Otherwise 6691590Srgrimes * just print the protocol number and address of the socket itself. 6701590Srgrimes * The idea is not to duplicate netstat, but to make available enough 6711590Srgrimes * information for further analysis. 6721590Srgrimes */ 6731590Srgrimes switch(dom.dom_family) { 6741590Srgrimes case AF_INET: 6751590Srgrimes getinetproto(proto.pr_protocol); 6761590Srgrimes if (proto.pr_protocol == IPPROTO_TCP ) { 6771590Srgrimes if (so.so_pcb) { 6781590Srgrimes if (kvm_read(kd, (u_long)so.so_pcb, 6791590Srgrimes (char *)&inpcb, sizeof(struct inpcb)) 6801590Srgrimes != sizeof(struct inpcb)) { 6818874Srgrimes dprintf(stderr, 6821590Srgrimes "can't read inpcb at %x\n", 6831590Srgrimes so.so_pcb); 6841590Srgrimes goto bad; 6851590Srgrimes } 6861590Srgrimes printf(" %x", (int)inpcb.inp_ppcb); 6871590Srgrimes } 6881590Srgrimes } 6891590Srgrimes else if (so.so_pcb) 6901590Srgrimes printf(" %x", (int)so.so_pcb); 6911590Srgrimes break; 6921590Srgrimes case AF_UNIX: 6931590Srgrimes /* print address of pcb and connected pcb */ 6941590Srgrimes if (so.so_pcb) { 6951590Srgrimes printf(" %x", (int)so.so_pcb); 6961590Srgrimes if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 6971590Srgrimes sizeof(struct unpcb)) != sizeof(struct unpcb)){ 6981590Srgrimes dprintf(stderr, "can't read unpcb at %x\n", 6991590Srgrimes so.so_pcb); 7001590Srgrimes goto bad; 7011590Srgrimes } 7021590Srgrimes if (unpcb.unp_conn) { 7031590Srgrimes char shoconn[4], *cp; 7041590Srgrimes 7051590Srgrimes cp = shoconn; 7061590Srgrimes if (!(so.so_state & SS_CANTRCVMORE)) 7071590Srgrimes *cp++ = '<'; 7081590Srgrimes *cp++ = '-'; 7091590Srgrimes if (!(so.so_state & SS_CANTSENDMORE)) 7101590Srgrimes *cp++ = '>'; 7111590Srgrimes *cp = '\0'; 7121590Srgrimes printf(" %s %x", shoconn, 7131590Srgrimes (int)unpcb.unp_conn); 7141590Srgrimes } 7151590Srgrimes } 7161590Srgrimes break; 7171590Srgrimes default: 7181590Srgrimes /* print protocol number and socket address */ 7191590Srgrimes printf(" %d %x", proto.pr_protocol, (int)sock); 7201590Srgrimes } 7211590Srgrimes printf("\n"); 7221590Srgrimes return; 7231590Srgrimesbad: 7241590Srgrimes printf("* error\n"); 7251590Srgrimes} 7261590Srgrimes 7271590Srgrimes/* 7281590Srgrimes * getinetproto -- 7291590Srgrimes * print name of protocol number 7301590Srgrimes */ 7311590Srgrimesvoid 7321590Srgrimesgetinetproto(number) 7331590Srgrimes int number; 7341590Srgrimes{ 7351590Srgrimes char *cp; 7361590Srgrimes 7371590Srgrimes switch(number) { 7381590Srgrimes case IPPROTO_IP: 7391590Srgrimes cp = "ip"; break; 7401590Srgrimes case IPPROTO_ICMP: 7411590Srgrimes cp ="icmp"; break; 7421590Srgrimes case IPPROTO_GGP: 7431590Srgrimes cp ="ggp"; break; 7441590Srgrimes case IPPROTO_TCP: 7451590Srgrimes cp ="tcp"; break; 7461590Srgrimes case IPPROTO_EGP: 7471590Srgrimes cp ="egp"; break; 7481590Srgrimes case IPPROTO_PUP: 7491590Srgrimes cp ="pup"; break; 7501590Srgrimes case IPPROTO_UDP: 7511590Srgrimes cp ="udp"; break; 7521590Srgrimes case IPPROTO_IDP: 7531590Srgrimes cp ="idp"; break; 7541590Srgrimes case IPPROTO_RAW: 7551590Srgrimes cp ="raw"; break; 7561590Srgrimes default: 7571590Srgrimes printf(" %d", number); 7581590Srgrimes return; 7591590Srgrimes } 7601590Srgrimes printf(" %s", cp); 7611590Srgrimes} 7621590Srgrimes 76317808Speterint 7641590Srgrimesgetfname(filename) 7651590Srgrimes char *filename; 7661590Srgrimes{ 7671590Srgrimes struct stat statbuf; 7681590Srgrimes DEVS *cur; 7691590Srgrimes 7701590Srgrimes if (stat(filename, &statbuf)) { 7711590Srgrimes fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno)); 7721590Srgrimes return(0); 7731590Srgrimes } 7741590Srgrimes if ((cur = malloc(sizeof(DEVS))) == NULL) { 7751590Srgrimes fprintf(stderr, "fstat: %s\n", strerror(errno)); 7761590Srgrimes exit(1); 7771590Srgrimes } 7781590Srgrimes cur->next = devs; 7791590Srgrimes devs = cur; 7801590Srgrimes 7811590Srgrimes cur->ino = statbuf.st_ino; 7821590Srgrimes cur->fsid = statbuf.st_dev & 0xffff; 7831590Srgrimes cur->name = filename; 7841590Srgrimes return(1); 7851590Srgrimes} 7861590Srgrimes 7871590Srgrimesvoid 7881590Srgrimesusage() 7891590Srgrimes{ 7901590Srgrimes (void)fprintf(stderr, 7911590Srgrimes "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 7921590Srgrimes exit(1); 7931590Srgrimes} 794