19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1988, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 359b50d902SRodney W. Grimes static char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 419b50d902SRodney W. Grimes static char sccsid[] = "@(#)fstat.c 8.1 (Berkeley) 6/6/93"; 429b50d902SRodney W. Grimes #endif /* not lint */ 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include <sys/param.h> 459b50d902SRodney W. Grimes #include <sys/time.h> 469b50d902SRodney W. Grimes #include <sys/proc.h> 479b50d902SRodney W. Grimes #include <sys/user.h> 489b50d902SRodney W. Grimes #include <sys/stat.h> 499b50d902SRodney W. Grimes #include <sys/vnode.h> 509b50d902SRodney W. Grimes #include <sys/socket.h> 519b50d902SRodney W. Grimes #include <sys/socketvar.h> 529b50d902SRodney W. Grimes #include <sys/domain.h> 539b50d902SRodney W. Grimes #include <sys/protosw.h> 549b50d902SRodney W. Grimes #include <sys/unpcb.h> 559b50d902SRodney W. Grimes #include <sys/sysctl.h> 569b50d902SRodney W. Grimes #include <sys/filedesc.h> 57d2bccb9fSDavid Greenman #include <sys/queue.h> 589b50d902SRodney W. Grimes #define KERNEL 599b50d902SRodney W. Grimes #include <sys/file.h> 609b50d902SRodney W. Grimes #include <ufs/ufs/quota.h> 619b50d902SRodney W. Grimes #include <ufs/ufs/inode.h> 629b50d902SRodney W. Grimes #undef KERNEL 639b50d902SRodney W. Grimes #define NFS 649b50d902SRodney W. Grimes #include <sys/mount.h> 659b50d902SRodney W. Grimes #include <nfs/nfsv2.h> 669b50d902SRodney W. Grimes #include <nfs/rpcv2.h> 679b50d902SRodney W. Grimes #include <nfs/nfs.h> 689b50d902SRodney W. Grimes #include <nfs/nfsnode.h> 699b50d902SRodney W. Grimes #undef NFS 709b50d902SRodney W. Grimes 719b50d902SRodney W. Grimes #include <net/route.h> 729b50d902SRodney W. Grimes #include <netinet/in.h> 739b50d902SRodney W. Grimes #include <netinet/in_systm.h> 749b50d902SRodney W. Grimes #include <netinet/ip.h> 759b50d902SRodney W. Grimes #include <netinet/in_pcb.h> 769b50d902SRodney W. Grimes 779b50d902SRodney W. Grimes #include <ctype.h> 789b50d902SRodney W. Grimes #include <errno.h> 799b50d902SRodney W. Grimes #include <kvm.h> 809b50d902SRodney W. Grimes #include <nlist.h> 819b50d902SRodney W. Grimes #include <paths.h> 829b50d902SRodney W. Grimes #include <pwd.h> 839b50d902SRodney W. Grimes #include <stdio.h> 849b50d902SRodney W. Grimes #include <stdlib.h> 859b50d902SRodney W. Grimes #include <string.h> 869b50d902SRodney W. Grimes 879b50d902SRodney W. Grimes #define TEXT -1 889b50d902SRodney W. Grimes #define CDIR -2 899b50d902SRodney W. Grimes #define RDIR -3 909b50d902SRodney W. Grimes #define TRACE -4 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes typedef struct devs { 939b50d902SRodney W. Grimes struct devs *next; 949b50d902SRodney W. Grimes long fsid; 959b50d902SRodney W. Grimes ino_t ino; 969b50d902SRodney W. Grimes char *name; 979b50d902SRodney W. Grimes } DEVS; 989b50d902SRodney W. Grimes DEVS *devs; 999b50d902SRodney W. Grimes 1009b50d902SRodney W. Grimes struct filestat { 1019b50d902SRodney W. Grimes long fsid; 1029b50d902SRodney W. Grimes long fileid; 1039b50d902SRodney W. Grimes mode_t mode; 1049b50d902SRodney W. Grimes u_long size; 1059b50d902SRodney W. Grimes dev_t rdev; 1069b50d902SRodney W. Grimes }; 1079b50d902SRodney W. Grimes 1089b50d902SRodney W. Grimes #ifdef notdef 1099b50d902SRodney W. Grimes struct nlist nl[] = { 1109b50d902SRodney W. Grimes { "" }, 1119b50d902SRodney W. Grimes }; 1129b50d902SRodney W. Grimes #endif 1139b50d902SRodney W. Grimes 1149b50d902SRodney W. Grimes int fsflg, /* show files on same filesystem as file(s) argument */ 1159b50d902SRodney W. Grimes pflg, /* show files open by a particular pid */ 1169b50d902SRodney W. Grimes uflg; /* show files open by a particular (effective) user */ 1179b50d902SRodney W. Grimes int checkfile; /* true if restricting to particular files or filesystems */ 1189b50d902SRodney W. Grimes int nflg; /* (numerical) display f.s. and rdev as dev_t */ 1199b50d902SRodney W. Grimes int vflg; /* display errors in locating kernel data objects etc... */ 1209b50d902SRodney W. Grimes 1219b50d902SRodney W. Grimes #define dprintf if (vflg) fprintf 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes struct file **ofiles; /* buffer of pointers to file structures */ 1249b50d902SRodney W. Grimes int maxfiles; 1259b50d902SRodney W. Grimes #define ALLOC_OFILES(d) \ 1269b50d902SRodney W. Grimes if ((d) > maxfiles) { \ 1279b50d902SRodney W. Grimes free(ofiles); \ 1289b50d902SRodney W. Grimes ofiles = malloc((d) * sizeof(struct file *)); \ 1299b50d902SRodney W. Grimes if (ofiles == NULL) { \ 1309b50d902SRodney W. Grimes fprintf(stderr, "fstat: %s\n", strerror(errno)); \ 1319b50d902SRodney W. Grimes exit(1); \ 1329b50d902SRodney W. Grimes } \ 1339b50d902SRodney W. Grimes maxfiles = (d); \ 1349b50d902SRodney W. Grimes } 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes /* 1379b50d902SRodney W. Grimes * a kvm_read that returns true if everything is read 1389b50d902SRodney W. Grimes */ 1399b50d902SRodney W. Grimes #define KVM_READ(kaddr, paddr, len) \ 1409b50d902SRodney W. Grimes (kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len)) 1419b50d902SRodney W. Grimes 1429b50d902SRodney W. Grimes kvm_t *kd; 1439b50d902SRodney W. Grimes 1449b50d902SRodney W. Grimes int ufs_filestat(), nfs_filestat(); 1459b50d902SRodney W. Grimes void dofiles(), getinetproto(), socktrans(); 1469b50d902SRodney W. Grimes void usage(), vtrans(); 1479b50d902SRodney W. Grimes 1489b50d902SRodney W. Grimes main(argc, argv) 1499b50d902SRodney W. Grimes int argc; 1509b50d902SRodney W. Grimes char **argv; 1519b50d902SRodney W. Grimes { 1529b50d902SRodney W. Grimes extern char *optarg; 1539b50d902SRodney W. Grimes extern int optind; 1549b50d902SRodney W. Grimes register struct passwd *passwd; 1559b50d902SRodney W. Grimes struct kinfo_proc *p, *plast; 1569b50d902SRodney W. Grimes int arg, ch, what; 1579b50d902SRodney W. Grimes char *memf, *nlistf; 1589b50d902SRodney W. Grimes int cnt; 1599b50d902SRodney W. Grimes 1609b50d902SRodney W. Grimes arg = 0; 1619b50d902SRodney W. Grimes what = KERN_PROC_ALL; 1629b50d902SRodney W. Grimes nlistf = memf = NULL; 1639b50d902SRodney W. Grimes while ((ch = getopt(argc, argv, "fnp:u:vNM")) != EOF) 1649b50d902SRodney W. Grimes switch((char)ch) { 1659b50d902SRodney W. Grimes case 'f': 1669b50d902SRodney W. Grimes fsflg = 1; 1679b50d902SRodney W. Grimes break; 1689b50d902SRodney W. Grimes case 'M': 1699b50d902SRodney W. Grimes memf = optarg; 1709b50d902SRodney W. Grimes break; 1719b50d902SRodney W. Grimes case 'N': 1729b50d902SRodney W. Grimes nlistf = optarg; 1739b50d902SRodney W. Grimes break; 1749b50d902SRodney W. Grimes case 'n': 1759b50d902SRodney W. Grimes nflg = 1; 1769b50d902SRodney W. Grimes break; 1779b50d902SRodney W. Grimes case 'p': 1789b50d902SRodney W. Grimes if (pflg++) 1799b50d902SRodney W. Grimes usage(); 1809b50d902SRodney W. Grimes if (!isdigit(*optarg)) { 1819b50d902SRodney W. Grimes fprintf(stderr, 1829b50d902SRodney W. Grimes "fstat: -p requires a process id\n"); 1839b50d902SRodney W. Grimes usage(); 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes what = KERN_PROC_PID; 1869b50d902SRodney W. Grimes arg = atoi(optarg); 1879b50d902SRodney W. Grimes break; 1889b50d902SRodney W. Grimes case 'u': 1899b50d902SRodney W. Grimes if (uflg++) 1909b50d902SRodney W. Grimes usage(); 1919b50d902SRodney W. Grimes if (!(passwd = getpwnam(optarg))) { 1929b50d902SRodney W. Grimes fprintf(stderr, "%s: unknown uid\n", 1939b50d902SRodney W. Grimes optarg); 1949b50d902SRodney W. Grimes exit(1); 1959b50d902SRodney W. Grimes } 1969b50d902SRodney W. Grimes what = KERN_PROC_UID; 1979b50d902SRodney W. Grimes arg = passwd->pw_uid; 1989b50d902SRodney W. Grimes break; 1999b50d902SRodney W. Grimes case 'v': 2009b50d902SRodney W. Grimes vflg = 1; 2019b50d902SRodney W. Grimes break; 2029b50d902SRodney W. Grimes case '?': 2039b50d902SRodney W. Grimes default: 2049b50d902SRodney W. Grimes usage(); 2059b50d902SRodney W. Grimes } 2069b50d902SRodney W. Grimes 2079b50d902SRodney W. Grimes if (*(argv += optind)) { 2089b50d902SRodney W. Grimes for (; *argv; ++argv) { 2099b50d902SRodney W. Grimes if (getfname(*argv)) 2109b50d902SRodney W. Grimes checkfile = 1; 2119b50d902SRodney W. Grimes } 2129b50d902SRodney W. Grimes if (!checkfile) /* file(s) specified, but none accessable */ 2139b50d902SRodney W. Grimes exit(1); 2149b50d902SRodney W. Grimes } 2159b50d902SRodney W. Grimes 2169b50d902SRodney W. Grimes ALLOC_OFILES(256); /* reserve space for file pointers */ 2179b50d902SRodney W. Grimes 2189b50d902SRodney W. Grimes if (fsflg && !checkfile) { 2199b50d902SRodney W. Grimes /* -f with no files means use wd */ 2209b50d902SRodney W. Grimes if (getfname(".") == 0) 2219b50d902SRodney W. Grimes exit(1); 2229b50d902SRodney W. Grimes checkfile = 1; 2239b50d902SRodney W. Grimes } 2249b50d902SRodney W. Grimes 2259b50d902SRodney W. Grimes /* 2269b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 2279b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 2289b50d902SRodney W. Grimes */ 2299b50d902SRodney W. Grimes if (nlistf != NULL || memf != NULL) 2309b50d902SRodney W. Grimes setgid(getgid()); 2319b50d902SRodney W. Grimes 2329b50d902SRodney W. Grimes if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, NULL)) == NULL) { 2339b50d902SRodney W. Grimes fprintf(stderr, "fstat: %s\n", kvm_geterr(kd)); 2349b50d902SRodney W. Grimes exit(1); 2359b50d902SRodney W. Grimes } 2369b50d902SRodney W. Grimes #ifdef notdef 2379b50d902SRodney W. Grimes if (kvm_nlist(kd, nl) != 0) { 2389b50d902SRodney W. Grimes fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr(kd)); 2399b50d902SRodney W. Grimes exit(1); 2409b50d902SRodney W. Grimes } 2419b50d902SRodney W. Grimes #endif 2429b50d902SRodney W. Grimes if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) { 2439b50d902SRodney W. Grimes fprintf(stderr, "fstat: %s\n", kvm_geterr(kd)); 2449b50d902SRodney W. Grimes exit(1); 2459b50d902SRodney W. Grimes } 2469b50d902SRodney W. Grimes if (nflg) 2479b50d902SRodney W. Grimes printf("%s", 2489b50d902SRodney W. Grimes "USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 2499b50d902SRodney W. Grimes else 2509b50d902SRodney W. Grimes printf("%s", 2519b50d902SRodney W. Grimes "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 2529b50d902SRodney W. Grimes if (checkfile && fsflg == 0) 2539b50d902SRodney W. Grimes printf(" NAME\n"); 2549b50d902SRodney W. Grimes else 2559b50d902SRodney W. Grimes putchar('\n'); 2569b50d902SRodney W. Grimes 2579b50d902SRodney W. Grimes for (plast = &p[cnt]; p < plast; ++p) { 2589b50d902SRodney W. Grimes if (p->kp_proc.p_stat == SZOMB) 2599b50d902SRodney W. Grimes continue; 2609b50d902SRodney W. Grimes dofiles(p); 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes exit(0); 2639b50d902SRodney W. Grimes } 2649b50d902SRodney W. Grimes 2659b50d902SRodney W. Grimes char *Uname, *Comm; 2669b50d902SRodney W. Grimes int Pid; 2679b50d902SRodney W. Grimes 2689b50d902SRodney W. Grimes #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 2699b50d902SRodney W. Grimes switch(i) { \ 2709b50d902SRodney W. Grimes case TEXT: \ 2719b50d902SRodney W. Grimes printf(" text"); \ 2729b50d902SRodney W. Grimes break; \ 2739b50d902SRodney W. Grimes case CDIR: \ 2749b50d902SRodney W. Grimes printf(" wd"); \ 2759b50d902SRodney W. Grimes break; \ 2769b50d902SRodney W. Grimes case RDIR: \ 2779b50d902SRodney W. Grimes printf(" root"); \ 2789b50d902SRodney W. Grimes break; \ 2799b50d902SRodney W. Grimes case TRACE: \ 2809b50d902SRodney W. Grimes printf(" tr"); \ 2819b50d902SRodney W. Grimes break; \ 2829b50d902SRodney W. Grimes default: \ 2839b50d902SRodney W. Grimes printf(" %4d", i); \ 2849b50d902SRodney W. Grimes break; \ 2859b50d902SRodney W. Grimes } 2869b50d902SRodney W. Grimes 2879b50d902SRodney W. Grimes /* 2889b50d902SRodney W. Grimes * print open files attributed to this process 2899b50d902SRodney W. Grimes */ 2909b50d902SRodney W. Grimes void 2919b50d902SRodney W. Grimes dofiles(kp) 2929b50d902SRodney W. Grimes struct kinfo_proc *kp; 2939b50d902SRodney W. Grimes { 2949b50d902SRodney W. Grimes int i, last; 2959b50d902SRodney W. Grimes struct file file; 2969b50d902SRodney W. Grimes struct filedesc0 filed0; 2979b50d902SRodney W. Grimes #define filed filed0.fd_fd 2989b50d902SRodney W. Grimes struct proc *p = &kp->kp_proc; 2999b50d902SRodney W. Grimes struct eproc *ep = &kp->kp_eproc; 3009b50d902SRodney W. Grimes 3019b50d902SRodney W. Grimes extern char *user_from_uid(); 3029b50d902SRodney W. Grimes 3039b50d902SRodney W. Grimes Uname = user_from_uid(ep->e_ucred.cr_uid, 0); 3049b50d902SRodney W. Grimes Pid = p->p_pid; 3059b50d902SRodney W. Grimes Comm = p->p_comm; 3069b50d902SRodney W. Grimes 3079b50d902SRodney W. Grimes if (p->p_fd == NULL) 3089b50d902SRodney W. Grimes return; 3099b50d902SRodney W. Grimes if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) { 3109b50d902SRodney W. Grimes dprintf(stderr, "can't read filedesc at %x for pid %d\n", 3119b50d902SRodney W. Grimes p->p_fd, Pid); 3129b50d902SRodney W. Grimes return; 3139b50d902SRodney W. Grimes } 3149b50d902SRodney W. Grimes /* 3159b50d902SRodney W. Grimes * root directory vnode, if one 3169b50d902SRodney W. Grimes */ 3179b50d902SRodney W. Grimes if (filed.fd_rdir) 3189b50d902SRodney W. Grimes vtrans(filed.fd_rdir, RDIR, FREAD); 3199b50d902SRodney W. Grimes /* 3209b50d902SRodney W. Grimes * current working directory vnode 3219b50d902SRodney W. Grimes */ 3229b50d902SRodney W. Grimes vtrans(filed.fd_cdir, CDIR, FREAD); 3239b50d902SRodney W. Grimes /* 3249b50d902SRodney W. Grimes * ktrace vnode, if one 3259b50d902SRodney W. Grimes */ 3269b50d902SRodney W. Grimes if (p->p_tracep) 3279b50d902SRodney W. Grimes vtrans(p->p_tracep, TRACE, FREAD|FWRITE); 3289b50d902SRodney W. Grimes /* 3299b50d902SRodney W. Grimes * open files 3309b50d902SRodney W. Grimes */ 3319b50d902SRodney W. Grimes #define FPSIZE (sizeof (struct file *)) 3329b50d902SRodney W. Grimes ALLOC_OFILES(filed.fd_lastfile+1); 3339b50d902SRodney W. Grimes if (filed.fd_nfiles > NDFILE) { 3349b50d902SRodney W. Grimes if (!KVM_READ(filed.fd_ofiles, ofiles, 3359b50d902SRodney W. Grimes (filed.fd_lastfile+1) * FPSIZE)) { 3369b50d902SRodney W. Grimes dprintf(stderr, 3379b50d902SRodney W. Grimes "can't read file structures at %x for pid %d\n", 3389b50d902SRodney W. Grimes filed.fd_ofiles, Pid); 3399b50d902SRodney W. Grimes return; 3409b50d902SRodney W. Grimes } 3419b50d902SRodney W. Grimes } else 3429b50d902SRodney W. Grimes bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 3439b50d902SRodney W. Grimes for (i = 0; i <= filed.fd_lastfile; i++) { 3449b50d902SRodney W. Grimes if (ofiles[i] == NULL) 3459b50d902SRodney W. Grimes continue; 3469b50d902SRodney W. Grimes if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 3479b50d902SRodney W. Grimes dprintf(stderr, "can't read file %d at %x for pid %d\n", 3489b50d902SRodney W. Grimes i, ofiles[i], Pid); 3499b50d902SRodney W. Grimes continue; 3509b50d902SRodney W. Grimes } 3519b50d902SRodney W. Grimes if (file.f_type == DTYPE_VNODE) 3529b50d902SRodney W. Grimes vtrans((struct vnode *)file.f_data, i, file.f_flag); 3539b50d902SRodney W. Grimes else if (file.f_type == DTYPE_SOCKET) { 3549b50d902SRodney W. Grimes if (checkfile == 0) 3559b50d902SRodney W. Grimes socktrans((struct socket *)file.f_data, i); 3569b50d902SRodney W. Grimes } 3579b50d902SRodney W. Grimes else { 3589b50d902SRodney W. Grimes dprintf(stderr, 3599b50d902SRodney W. Grimes "unknown file type %d for file %d of pid %d\n", 3609b50d902SRodney W. Grimes file.f_type, i, Pid); 3619b50d902SRodney W. Grimes } 3629b50d902SRodney W. Grimes } 3639b50d902SRodney W. Grimes } 3649b50d902SRodney W. Grimes 3659b50d902SRodney W. Grimes void 3669b50d902SRodney W. Grimes vtrans(vp, i, flag) 3679b50d902SRodney W. Grimes struct vnode *vp; 3689b50d902SRodney W. Grimes int i; 3699b50d902SRodney W. Grimes int flag; 3709b50d902SRodney W. Grimes { 3719b50d902SRodney W. Grimes struct vnode vn; 3729b50d902SRodney W. Grimes struct filestat fst; 3739b50d902SRodney W. Grimes char rw[3], mode[15]; 3749b50d902SRodney W. Grimes char *badtype = NULL, *filename, *getmnton(); 3759b50d902SRodney W. Grimes 3769b50d902SRodney W. Grimes filename = badtype = NULL; 3779b50d902SRodney W. Grimes if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 3789b50d902SRodney W. Grimes dprintf(stderr, "can't read vnode at %x for pid %d\n", 3799b50d902SRodney W. Grimes vp, Pid); 3809b50d902SRodney W. Grimes return; 3819b50d902SRodney W. Grimes } 3829b50d902SRodney W. Grimes if (vn.v_type == VNON || vn.v_tag == VT_NON) 3839b50d902SRodney W. Grimes badtype = "none"; 3849b50d902SRodney W. Grimes else if (vn.v_type == VBAD) 3859b50d902SRodney W. Grimes badtype = "bad"; 3869b50d902SRodney W. Grimes else 3879b50d902SRodney W. Grimes switch (vn.v_tag) { 3889b50d902SRodney W. Grimes case VT_UFS: 3899b50d902SRodney W. Grimes if (!ufs_filestat(&vn, &fst)) 3909b50d902SRodney W. Grimes badtype = "error"; 3919b50d902SRodney W. Grimes break; 3929b50d902SRodney W. Grimes case VT_MFS: 3939b50d902SRodney W. Grimes if (!ufs_filestat(&vn, &fst)) 3949b50d902SRodney W. Grimes badtype = "error"; 3959b50d902SRodney W. Grimes break; 3969b50d902SRodney W. Grimes case VT_NFS: 3979b50d902SRodney W. Grimes if (!nfs_filestat(&vn, &fst)) 3989b50d902SRodney W. Grimes badtype = "error"; 3999b50d902SRodney W. Grimes break; 4009b50d902SRodney W. Grimes default: { 4019b50d902SRodney W. Grimes static char unknown[10]; 4029b50d902SRodney W. Grimes sprintf(badtype = unknown, "?(%x)", vn.v_tag); 4039b50d902SRodney W. Grimes break;; 4049b50d902SRodney W. Grimes } 4059b50d902SRodney W. Grimes } 4069b50d902SRodney W. Grimes if (checkfile) { 4079b50d902SRodney W. Grimes int fsmatch = 0; 4089b50d902SRodney W. Grimes register DEVS *d; 4099b50d902SRodney W. Grimes 4109b50d902SRodney W. Grimes if (badtype) 4119b50d902SRodney W. Grimes return; 4129b50d902SRodney W. Grimes for (d = devs; d != NULL; d = d->next) 4139b50d902SRodney W. Grimes if (d->fsid == fst.fsid) { 4149b50d902SRodney W. Grimes fsmatch = 1; 4159b50d902SRodney W. Grimes if (d->ino == fst.fileid) { 4169b50d902SRodney W. Grimes filename = d->name; 4179b50d902SRodney W. Grimes break; 4189b50d902SRodney W. Grimes } 4199b50d902SRodney W. Grimes } 4209b50d902SRodney W. Grimes if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 4219b50d902SRodney W. Grimes return; 4229b50d902SRodney W. Grimes } 4239b50d902SRodney W. Grimes PREFIX(i); 4249b50d902SRodney W. Grimes if (badtype) { 4259b50d902SRodney W. Grimes (void)printf(" - - %10s -\n", badtype); 4269b50d902SRodney W. Grimes return; 4279b50d902SRodney W. Grimes } 4289b50d902SRodney W. Grimes if (nflg) 4299b50d902SRodney W. Grimes (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 4309b50d902SRodney W. Grimes else 4319b50d902SRodney W. Grimes (void)printf(" %-8s", getmnton(vn.v_mount)); 4329b50d902SRodney W. Grimes if (nflg) 4339b50d902SRodney W. Grimes (void)sprintf(mode, "%o", fst.mode); 4349b50d902SRodney W. Grimes else 4359b50d902SRodney W. Grimes strmode(fst.mode, mode); 4369b50d902SRodney W. Grimes (void)printf(" %6d %10s", fst.fileid, mode); 4379b50d902SRodney W. Grimes switch (vn.v_type) { 4389b50d902SRodney W. Grimes case VBLK: 4399b50d902SRodney W. Grimes case VCHR: { 4409b50d902SRodney W. Grimes char *name; 4419b50d902SRodney W. Grimes 4429b50d902SRodney W. Grimes if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 4439b50d902SRodney W. Grimes S_IFCHR : S_IFBLK)) == NULL)) 4449b50d902SRodney W. Grimes printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 4459b50d902SRodney W. Grimes else 4469b50d902SRodney W. Grimes printf(" %6s", name); 4479b50d902SRodney W. Grimes break; 4489b50d902SRodney W. Grimes } 4499b50d902SRodney W. Grimes default: 4509b50d902SRodney W. Grimes printf(" %6d", fst.size); 4519b50d902SRodney W. Grimes } 4529b50d902SRodney W. Grimes rw[0] = '\0'; 4539b50d902SRodney W. Grimes if (flag & FREAD) 4549b50d902SRodney W. Grimes strcat(rw, "r"); 4559b50d902SRodney W. Grimes if (flag & FWRITE) 4569b50d902SRodney W. Grimes strcat(rw, "w"); 4579b50d902SRodney W. Grimes printf(" %2s", rw); 4589b50d902SRodney W. Grimes if (filename && !fsflg) 4599b50d902SRodney W. Grimes printf(" %s", filename); 4609b50d902SRodney W. Grimes putchar('\n'); 4619b50d902SRodney W. Grimes } 4629b50d902SRodney W. Grimes 4639b50d902SRodney W. Grimes int 4649b50d902SRodney W. Grimes ufs_filestat(vp, fsp) 4659b50d902SRodney W. Grimes struct vnode *vp; 4669b50d902SRodney W. Grimes struct filestat *fsp; 4679b50d902SRodney W. Grimes { 4689b50d902SRodney W. Grimes struct inode inode; 4699b50d902SRodney W. Grimes 4709b50d902SRodney W. Grimes if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 4719b50d902SRodney W. Grimes dprintf(stderr, "can't read inode at %x for pid %d\n", 4729b50d902SRodney W. Grimes VTOI(vp), Pid); 4739b50d902SRodney W. Grimes return 0; 4749b50d902SRodney W. Grimes } 4759b50d902SRodney W. Grimes fsp->fsid = inode.i_dev & 0xffff; 4769b50d902SRodney W. Grimes fsp->fileid = (long)inode.i_number; 4779b50d902SRodney W. Grimes fsp->mode = (mode_t)inode.i_mode; 4789b50d902SRodney W. Grimes fsp->size = (u_long)inode.i_size; 4799b50d902SRodney W. Grimes fsp->rdev = inode.i_rdev; 4809b50d902SRodney W. Grimes 4819b50d902SRodney W. Grimes return 1; 4829b50d902SRodney W. Grimes } 4839b50d902SRodney W. Grimes 4849b50d902SRodney W. Grimes int 4859b50d902SRodney W. Grimes nfs_filestat(vp, fsp) 4869b50d902SRodney W. Grimes struct vnode *vp; 4879b50d902SRodney W. Grimes struct filestat *fsp; 4889b50d902SRodney W. Grimes { 4899b50d902SRodney W. Grimes struct nfsnode nfsnode; 4909b50d902SRodney W. Grimes register mode_t mode; 4919b50d902SRodney W. Grimes 4929b50d902SRodney W. Grimes if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 4939b50d902SRodney W. Grimes dprintf(stderr, "can't read nfsnode at %x for pid %d\n", 4949b50d902SRodney W. Grimes VTONFS(vp), Pid); 4959b50d902SRodney W. Grimes return 0; 4969b50d902SRodney W. Grimes } 4979b50d902SRodney W. Grimes fsp->fsid = nfsnode.n_vattr.va_fsid; 4989b50d902SRodney W. Grimes fsp->fileid = nfsnode.n_vattr.va_fileid; 4999b50d902SRodney W. Grimes fsp->size = nfsnode.n_size; 5009b50d902SRodney W. Grimes fsp->rdev = nfsnode.n_vattr.va_rdev; 5019b50d902SRodney W. Grimes mode = (mode_t)nfsnode.n_vattr.va_mode; 5029b50d902SRodney W. Grimes switch (vp->v_type) { 5039b50d902SRodney W. Grimes case VREG: 5049b50d902SRodney W. Grimes mode |= S_IFREG; 5059b50d902SRodney W. Grimes break; 5069b50d902SRodney W. Grimes case VDIR: 5079b50d902SRodney W. Grimes mode |= S_IFDIR; 5089b50d902SRodney W. Grimes break; 5099b50d902SRodney W. Grimes case VBLK: 5109b50d902SRodney W. Grimes mode |= S_IFBLK; 5119b50d902SRodney W. Grimes break; 5129b50d902SRodney W. Grimes case VCHR: 5139b50d902SRodney W. Grimes mode |= S_IFCHR; 5149b50d902SRodney W. Grimes break; 5159b50d902SRodney W. Grimes case VLNK: 5169b50d902SRodney W. Grimes mode |= S_IFLNK; 5179b50d902SRodney W. Grimes break; 5189b50d902SRodney W. Grimes case VSOCK: 5199b50d902SRodney W. Grimes mode |= S_IFSOCK; 5209b50d902SRodney W. Grimes break; 5219b50d902SRodney W. Grimes case VFIFO: 5229b50d902SRodney W. Grimes mode |= S_IFIFO; 5239b50d902SRodney W. Grimes break; 5249b50d902SRodney W. Grimes }; 5259b50d902SRodney W. Grimes fsp->mode = mode; 5269b50d902SRodney W. Grimes 5279b50d902SRodney W. Grimes return 1; 5289b50d902SRodney W. Grimes } 5299b50d902SRodney W. Grimes 5309b50d902SRodney W. Grimes 5319b50d902SRodney W. Grimes char * 5329b50d902SRodney W. Grimes getmnton(m) 5339b50d902SRodney W. Grimes struct mount *m; 5349b50d902SRodney W. Grimes { 5359b50d902SRodney W. Grimes static struct mount mount; 5369b50d902SRodney W. Grimes static struct mtab { 5379b50d902SRodney W. Grimes struct mtab *next; 5389b50d902SRodney W. Grimes struct mount *m; 5399b50d902SRodney W. Grimes char mntonname[MNAMELEN]; 5409b50d902SRodney W. Grimes } *mhead = NULL; 5419b50d902SRodney W. Grimes register struct mtab *mt; 5429b50d902SRodney W. Grimes 5439b50d902SRodney W. Grimes for (mt = mhead; mt != NULL; mt = mt->next) 5449b50d902SRodney W. Grimes if (m == mt->m) 5459b50d902SRodney W. Grimes return (mt->mntonname); 5469b50d902SRodney W. Grimes if (!KVM_READ(m, &mount, sizeof(struct mount))) { 5479b50d902SRodney W. Grimes fprintf(stderr, "can't read mount table at %x\n", m); 5489b50d902SRodney W. Grimes return (NULL); 5499b50d902SRodney W. Grimes } 5509b50d902SRodney W. Grimes if ((mt = malloc(sizeof (struct mtab))) == NULL) { 5519b50d902SRodney W. Grimes fprintf(stderr, "fstat: %s\n", strerror(errno)); 5529b50d902SRodney W. Grimes exit(1); 5539b50d902SRodney W. Grimes } 5549b50d902SRodney W. Grimes mt->m = m; 5559b50d902SRodney W. Grimes bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 5569b50d902SRodney W. Grimes mt->next = mhead; 5579b50d902SRodney W. Grimes mhead = mt; 5589b50d902SRodney W. Grimes return (mt->mntonname); 5599b50d902SRodney W. Grimes } 5609b50d902SRodney W. Grimes 5619b50d902SRodney W. Grimes void 5629b50d902SRodney W. Grimes socktrans(sock, i) 5639b50d902SRodney W. Grimes struct socket *sock; 5649b50d902SRodney W. Grimes int i; 5659b50d902SRodney W. Grimes { 5669b50d902SRodney W. Grimes static char *stypename[] = { 5679b50d902SRodney W. Grimes "unused", /* 0 */ 5689b50d902SRodney W. Grimes "stream", /* 1 */ 5699b50d902SRodney W. Grimes "dgram", /* 2 */ 5709b50d902SRodney W. Grimes "raw", /* 3 */ 5719b50d902SRodney W. Grimes "rdm", /* 4 */ 5729b50d902SRodney W. Grimes "seqpak" /* 5 */ 5739b50d902SRodney W. Grimes }; 5749b50d902SRodney W. Grimes #define STYPEMAX 5 5759b50d902SRodney W. Grimes struct socket so; 5769b50d902SRodney W. Grimes struct protosw proto; 5779b50d902SRodney W. Grimes struct domain dom; 5789b50d902SRodney W. Grimes struct inpcb inpcb; 5799b50d902SRodney W. Grimes struct unpcb unpcb; 5809b50d902SRodney W. Grimes int len; 5819b50d902SRodney W. Grimes char dname[32], *strcpy(); 5829b50d902SRodney W. Grimes 5839b50d902SRodney W. Grimes PREFIX(i); 5849b50d902SRodney W. Grimes 5859b50d902SRodney W. Grimes /* fill in socket */ 5869b50d902SRodney W. Grimes if (!KVM_READ(sock, &so, sizeof(struct socket))) { 5879b50d902SRodney W. Grimes dprintf(stderr, "can't read sock at %x\n", sock); 5889b50d902SRodney W. Grimes goto bad; 5899b50d902SRodney W. Grimes } 5909b50d902SRodney W. Grimes 5919b50d902SRodney W. Grimes /* fill in protosw entry */ 5929b50d902SRodney W. Grimes if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 5939b50d902SRodney W. Grimes dprintf(stderr, "can't read protosw at %x", so.so_proto); 5949b50d902SRodney W. Grimes goto bad; 5959b50d902SRodney W. Grimes } 5969b50d902SRodney W. Grimes 5979b50d902SRodney W. Grimes /* fill in domain */ 5989b50d902SRodney W. Grimes if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 5999b50d902SRodney W. Grimes dprintf(stderr, "can't read domain at %x\n", proto.pr_domain); 6009b50d902SRodney W. Grimes goto bad; 6019b50d902SRodney W. Grimes } 6029b50d902SRodney W. Grimes 6039b50d902SRodney W. Grimes if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 6049b50d902SRodney W. Grimes sizeof(dname) - 1)) < 0) { 6059b50d902SRodney W. Grimes dprintf(stderr, "can't read domain name at %x\n", 6069b50d902SRodney W. Grimes dom.dom_name); 6079b50d902SRodney W. Grimes dname[0] = '\0'; 6089b50d902SRodney W. Grimes } 6099b50d902SRodney W. Grimes else 6109b50d902SRodney W. Grimes dname[len] = '\0'; 6119b50d902SRodney W. Grimes 6129b50d902SRodney W. Grimes if ((u_short)so.so_type > STYPEMAX) 6139b50d902SRodney W. Grimes printf("* %s ?%d", dname, so.so_type); 6149b50d902SRodney W. Grimes else 6159b50d902SRodney W. Grimes printf("* %s %s", dname, stypename[so.so_type]); 6169b50d902SRodney W. Grimes 6179b50d902SRodney W. Grimes /* 6189b50d902SRodney W. Grimes * protocol specific formatting 6199b50d902SRodney W. Grimes * 6209b50d902SRodney W. Grimes * Try to find interesting things to print. For tcp, the interesting 6219b50d902SRodney W. Grimes * thing is the address of the tcpcb, for udp and others, just the 6229b50d902SRodney W. Grimes * inpcb (socket pcb). For unix domain, its the address of the socket 6239b50d902SRodney W. Grimes * pcb and the address of the connected pcb (if connected). Otherwise 6249b50d902SRodney W. Grimes * just print the protocol number and address of the socket itself. 6259b50d902SRodney W. Grimes * The idea is not to duplicate netstat, but to make available enough 6269b50d902SRodney W. Grimes * information for further analysis. 6279b50d902SRodney W. Grimes */ 6289b50d902SRodney W. Grimes switch(dom.dom_family) { 6299b50d902SRodney W. Grimes case AF_INET: 6309b50d902SRodney W. Grimes getinetproto(proto.pr_protocol); 6319b50d902SRodney W. Grimes if (proto.pr_protocol == IPPROTO_TCP ) { 6329b50d902SRodney W. Grimes if (so.so_pcb) { 6339b50d902SRodney W. Grimes if (kvm_read(kd, (u_long)so.so_pcb, 6349b50d902SRodney W. Grimes (char *)&inpcb, sizeof(struct inpcb)) 6359b50d902SRodney W. Grimes != sizeof(struct inpcb)) { 6369b50d902SRodney W. Grimes dprintf(stderr, 6379b50d902SRodney W. Grimes "can't read inpcb at %x\n", 6389b50d902SRodney W. Grimes so.so_pcb); 6399b50d902SRodney W. Grimes goto bad; 6409b50d902SRodney W. Grimes } 6419b50d902SRodney W. Grimes printf(" %x", (int)inpcb.inp_ppcb); 6429b50d902SRodney W. Grimes } 6439b50d902SRodney W. Grimes } 6449b50d902SRodney W. Grimes else if (so.so_pcb) 6459b50d902SRodney W. Grimes printf(" %x", (int)so.so_pcb); 6469b50d902SRodney W. Grimes break; 6479b50d902SRodney W. Grimes case AF_UNIX: 6489b50d902SRodney W. Grimes /* print address of pcb and connected pcb */ 6499b50d902SRodney W. Grimes if (so.so_pcb) { 6509b50d902SRodney W. Grimes printf(" %x", (int)so.so_pcb); 6519b50d902SRodney W. Grimes if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 6529b50d902SRodney W. Grimes sizeof(struct unpcb)) != sizeof(struct unpcb)){ 6539b50d902SRodney W. Grimes dprintf(stderr, "can't read unpcb at %x\n", 6549b50d902SRodney W. Grimes so.so_pcb); 6559b50d902SRodney W. Grimes goto bad; 6569b50d902SRodney W. Grimes } 6579b50d902SRodney W. Grimes if (unpcb.unp_conn) { 6589b50d902SRodney W. Grimes char shoconn[4], *cp; 6599b50d902SRodney W. Grimes 6609b50d902SRodney W. Grimes cp = shoconn; 6619b50d902SRodney W. Grimes if (!(so.so_state & SS_CANTRCVMORE)) 6629b50d902SRodney W. Grimes *cp++ = '<'; 6639b50d902SRodney W. Grimes *cp++ = '-'; 6649b50d902SRodney W. Grimes if (!(so.so_state & SS_CANTSENDMORE)) 6659b50d902SRodney W. Grimes *cp++ = '>'; 6669b50d902SRodney W. Grimes *cp = '\0'; 6679b50d902SRodney W. Grimes printf(" %s %x", shoconn, 6689b50d902SRodney W. Grimes (int)unpcb.unp_conn); 6699b50d902SRodney W. Grimes } 6709b50d902SRodney W. Grimes } 6719b50d902SRodney W. Grimes break; 6729b50d902SRodney W. Grimes default: 6739b50d902SRodney W. Grimes /* print protocol number and socket address */ 6749b50d902SRodney W. Grimes printf(" %d %x", proto.pr_protocol, (int)sock); 6759b50d902SRodney W. Grimes } 6769b50d902SRodney W. Grimes printf("\n"); 6779b50d902SRodney W. Grimes return; 6789b50d902SRodney W. Grimes bad: 6799b50d902SRodney W. Grimes printf("* error\n"); 6809b50d902SRodney W. Grimes } 6819b50d902SRodney W. Grimes 6829b50d902SRodney W. Grimes /* 6839b50d902SRodney W. Grimes * getinetproto -- 6849b50d902SRodney W. Grimes * print name of protocol number 6859b50d902SRodney W. Grimes */ 6869b50d902SRodney W. Grimes void 6879b50d902SRodney W. Grimes getinetproto(number) 6889b50d902SRodney W. Grimes int number; 6899b50d902SRodney W. Grimes { 6909b50d902SRodney W. Grimes char *cp; 6919b50d902SRodney W. Grimes 6929b50d902SRodney W. Grimes switch(number) { 6939b50d902SRodney W. Grimes case IPPROTO_IP: 6949b50d902SRodney W. Grimes cp = "ip"; break; 6959b50d902SRodney W. Grimes case IPPROTO_ICMP: 6969b50d902SRodney W. Grimes cp ="icmp"; break; 6979b50d902SRodney W. Grimes case IPPROTO_GGP: 6989b50d902SRodney W. Grimes cp ="ggp"; break; 6999b50d902SRodney W. Grimes case IPPROTO_TCP: 7009b50d902SRodney W. Grimes cp ="tcp"; break; 7019b50d902SRodney W. Grimes case IPPROTO_EGP: 7029b50d902SRodney W. Grimes cp ="egp"; break; 7039b50d902SRodney W. Grimes case IPPROTO_PUP: 7049b50d902SRodney W. Grimes cp ="pup"; break; 7059b50d902SRodney W. Grimes case IPPROTO_UDP: 7069b50d902SRodney W. Grimes cp ="udp"; break; 7079b50d902SRodney W. Grimes case IPPROTO_IDP: 7089b50d902SRodney W. Grimes cp ="idp"; break; 7099b50d902SRodney W. Grimes case IPPROTO_RAW: 7109b50d902SRodney W. Grimes cp ="raw"; break; 7119b50d902SRodney W. Grimes default: 7129b50d902SRodney W. Grimes printf(" %d", number); 7139b50d902SRodney W. Grimes return; 7149b50d902SRodney W. Grimes } 7159b50d902SRodney W. Grimes printf(" %s", cp); 7169b50d902SRodney W. Grimes } 7179b50d902SRodney W. Grimes 7189b50d902SRodney W. Grimes getfname(filename) 7199b50d902SRodney W. Grimes char *filename; 7209b50d902SRodney W. Grimes { 7219b50d902SRodney W. Grimes struct stat statbuf; 7229b50d902SRodney W. Grimes DEVS *cur; 7239b50d902SRodney W. Grimes 7249b50d902SRodney W. Grimes if (stat(filename, &statbuf)) { 7259b50d902SRodney W. Grimes fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno)); 7269b50d902SRodney W. Grimes return(0); 7279b50d902SRodney W. Grimes } 7289b50d902SRodney W. Grimes if ((cur = malloc(sizeof(DEVS))) == NULL) { 7299b50d902SRodney W. Grimes fprintf(stderr, "fstat: %s\n", strerror(errno)); 7309b50d902SRodney W. Grimes exit(1); 7319b50d902SRodney W. Grimes } 7329b50d902SRodney W. Grimes cur->next = devs; 7339b50d902SRodney W. Grimes devs = cur; 7349b50d902SRodney W. Grimes 7359b50d902SRodney W. Grimes cur->ino = statbuf.st_ino; 7369b50d902SRodney W. Grimes cur->fsid = statbuf.st_dev & 0xffff; 7379b50d902SRodney W. Grimes cur->name = filename; 7389b50d902SRodney W. Grimes return(1); 7399b50d902SRodney W. Grimes } 7409b50d902SRodney W. Grimes 7419b50d902SRodney W. Grimes void 7429b50d902SRodney W. Grimes usage() 7439b50d902SRodney W. Grimes { 7449b50d902SRodney W. Grimes (void)fprintf(stderr, 7459b50d902SRodney W. Grimes "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 7469b50d902SRodney W. Grimes exit(1); 7479b50d902SRodney W. Grimes } 748