fstat.c revision 132477
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 3527272Scharnierstatic const 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 4127272Scharnier#if 0 4223693Speterstatic char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; 4327272Scharnier#endif 441590Srgrimes#endif /* not lint */ 4599112Sobrien#include <sys/cdefs.h> 4699112Sobrien__FBSDID("$FreeBSD: head/usr.bin/fstat/fstat.c 132477 2004-07-21 03:07:50Z silby $"); 471590Srgrimes 481590Srgrimes#include <sys/param.h> 491590Srgrimes#include <sys/time.h> 501590Srgrimes#include <sys/proc.h> 511590Srgrimes#include <sys/user.h> 521590Srgrimes#include <sys/stat.h> 531590Srgrimes#include <sys/vnode.h> 541590Srgrimes#include <sys/socket.h> 551590Srgrimes#include <sys/socketvar.h> 561590Srgrimes#include <sys/domain.h> 571590Srgrimes#include <sys/protosw.h> 5836110Smarkm#include <sys/un.h> 591590Srgrimes#include <sys/unpcb.h> 601590Srgrimes#include <sys/sysctl.h> 611590Srgrimes#include <sys/filedesc.h> 627726Sdg#include <sys/queue.h> 63132477Ssilby#define _KERNEL 6417808Speter#include <sys/pipe.h> 6553133Sgreen#include <sys/conf.h> 661590Srgrimes#include <sys/file.h> 67101872Sbmilekic#include <sys/mount.h> 681590Srgrimes#include <ufs/ufs/quota.h> 691590Srgrimes#include <ufs/ufs/inode.h> 7088051Sgreen#include <fs/devfs/devfs.h> 7155206Speter#undef _KERNEL 729336Sdfr#include <nfs/nfsproto.h> 731590Srgrimes#include <nfs/rpcv2.h> 7483653Speter#include <nfsclient/nfs.h> 7583653Speter#include <nfsclient/nfsnode.h> 761590Srgrimes 7758125Sgreen 7859029Sgreen#include <vm/vm.h> 7959029Sgreen#include <vm/vm_map.h> 8059029Sgreen#include <vm/vm_object.h> 8159029Sgreen 821590Srgrimes#include <net/route.h> 831590Srgrimes#include <netinet/in.h> 841590Srgrimes#include <netinet/in_systm.h> 851590Srgrimes#include <netinet/ip.h> 861590Srgrimes#include <netinet/in_pcb.h> 871590Srgrimes 881590Srgrimes#include <ctype.h> 8927272Scharnier#include <err.h> 9018570Sbde#include <fcntl.h> 911590Srgrimes#include <kvm.h> 9223693Speter#include <limits.h> 931590Srgrimes#include <nlist.h> 941590Srgrimes#include <paths.h> 951590Srgrimes#include <pwd.h> 961590Srgrimes#include <stdio.h> 971590Srgrimes#include <stdlib.h> 981590Srgrimes#include <string.h> 9923693Speter#include <unistd.h> 10048463Sru#include <netdb.h> 1011590Srgrimes 10258125Sgreen#include "fstat.h" 10358125Sgreen 1041590Srgrimes#define TEXT -1 1051590Srgrimes#define CDIR -2 1061590Srgrimes#define RDIR -3 1071590Srgrimes#define TRACE -4 10859029Sgreen#define MMAP -5 1091590Srgrimes 1101590SrgrimesDEVS *devs; 1111590Srgrimes 1121590Srgrimes#ifdef notdef 1131590Srgrimesstruct nlist nl[] = { 1141590Srgrimes { "" }, 1151590Srgrimes}; 1161590Srgrimes#endif 1171590Srgrimes 1181590Srgrimesint fsflg, /* show files on same filesystem as file(s) argument */ 1191590Srgrimes pflg, /* show files open by a particular pid */ 1201590Srgrimes uflg; /* show files open by a particular (effective) user */ 1211590Srgrimesint checkfile; /* true if restricting to particular files or filesystems */ 1221590Srgrimesint nflg; /* (numerical) display f.s. and rdev as dev_t */ 1231590Srgrimesint vflg; /* display errors in locating kernel data objects etc... */ 12459029Sgreenint mflg; /* include memory-mapped files */ 1251590Srgrimes 1261590Srgrimes 1271590Srgrimesstruct file **ofiles; /* buffer of pointers to file structures */ 1281590Srgrimesint maxfiles; 1291590Srgrimes#define ALLOC_OFILES(d) \ 1301590Srgrimes if ((d) > maxfiles) { \ 1311590Srgrimes free(ofiles); \ 1321590Srgrimes ofiles = malloc((d) * sizeof(struct file *)); \ 1331590Srgrimes if (ofiles == NULL) { \ 13427272Scharnier err(1, NULL); \ 1351590Srgrimes } \ 1361590Srgrimes maxfiles = (d); \ 1371590Srgrimes } 1381590Srgrimes 13997946Sdeschar *memf, *nlistf; 1401590Srgrimeskvm_t *kd; 1411590Srgrimes 14297946Sdesstatic void fstat_kvm(int, int); 14397946Sdesstatic void fstat_sysctl(int, int); 14492920Simpvoid dofiles(struct kinfo_proc *kp); 14592920Simpvoid dommap(struct kinfo_proc *kp); 14692920Simpvoid vtrans(struct vnode *vp, int i, int flag); 14792920Simpint ufs_filestat(struct vnode *vp, struct filestat *fsp); 14892920Simpint nfs_filestat(struct vnode *vp, struct filestat *fsp); 14992920Simpint devfs_filestat(struct vnode *vp, struct filestat *fsp); 15092920Simpchar *getmnton(struct mount *m); 15192920Simpvoid pipetrans(struct pipe *pi, int i, int flag); 15292920Simpvoid socktrans(struct socket *sock, int i); 15392920Simpvoid getinetproto(int number); 15493427Sdwmaloneint getfname(const char *filename); 15592920Simpvoid usage(void); 1561590Srgrimes 15717808Speter 15817808Speterint 159131293Sdwmalonemain(int argc, char **argv) 1601590Srgrimes{ 16193427Sdwmalone struct passwd *passwd; 1621590Srgrimes int arg, ch, what; 1631590Srgrimes 1641590Srgrimes arg = 0; 1651590Srgrimes what = KERN_PROC_ALL; 1661590Srgrimes nlistf = memf = NULL; 16759029Sgreen while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1) 1681590Srgrimes switch((char)ch) { 1691590Srgrimes case 'f': 1701590Srgrimes fsflg = 1; 1711590Srgrimes break; 1721590Srgrimes case 'M': 1731590Srgrimes memf = optarg; 1741590Srgrimes break; 1751590Srgrimes case 'N': 1761590Srgrimes nlistf = optarg; 1771590Srgrimes break; 17859029Sgreen case 'm': 17959029Sgreen mflg = 1; 18059029Sgreen break; 1811590Srgrimes case 'n': 1821590Srgrimes nflg = 1; 1831590Srgrimes break; 1841590Srgrimes case 'p': 1851590Srgrimes if (pflg++) 1861590Srgrimes usage(); 1871590Srgrimes if (!isdigit(*optarg)) { 18827311Scharnier warnx("-p requires a process id"); 1891590Srgrimes usage(); 1901590Srgrimes } 1911590Srgrimes what = KERN_PROC_PID; 1921590Srgrimes arg = atoi(optarg); 1931590Srgrimes break; 1941590Srgrimes case 'u': 1951590Srgrimes if (uflg++) 1961590Srgrimes usage(); 19727272Scharnier if (!(passwd = getpwnam(optarg))) 19827272Scharnier errx(1, "%s: unknown uid", optarg); 1991590Srgrimes what = KERN_PROC_UID; 2001590Srgrimes arg = passwd->pw_uid; 2011590Srgrimes break; 2021590Srgrimes case 'v': 2031590Srgrimes vflg = 1; 2041590Srgrimes break; 2051590Srgrimes case '?': 2061590Srgrimes default: 2071590Srgrimes usage(); 2081590Srgrimes } 2091590Srgrimes 2101590Srgrimes if (*(argv += optind)) { 2111590Srgrimes for (; *argv; ++argv) { 2121590Srgrimes if (getfname(*argv)) 2131590Srgrimes checkfile = 1; 2141590Srgrimes } 2151590Srgrimes if (!checkfile) /* file(s) specified, but none accessable */ 2161590Srgrimes exit(1); 2171590Srgrimes } 2181590Srgrimes 2198874Srgrimes if (fsflg && !checkfile) { 2201590Srgrimes /* -f with no files means use wd */ 2211590Srgrimes if (getfname(".") == 0) 2221590Srgrimes exit(1); 2231590Srgrimes checkfile = 1; 2241590Srgrimes } 2251590Srgrimes 22697946Sdes if (memf != NULL) 22797946Sdes fstat_kvm(what, arg); 22897946Sdes else 22997946Sdes fstat_sysctl(what, arg); 23097946Sdes exit(0); 23197946Sdes} 23297946Sdes 23397946Sdesstatic void 23497946Sdesprint_header(void) 23597946Sdes{ 23697946Sdes 23797946Sdes if (nflg) 23897946Sdes printf("%s", 23997946Sdes"USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 24097946Sdes else 24197946Sdes printf("%s", 24297946Sdes"USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 24397946Sdes if (checkfile && fsflg == 0) 24497946Sdes printf(" NAME\n"); 24597946Sdes else 24697946Sdes putchar('\n'); 24797946Sdes} 24897946Sdes 24997946Sdesstatic void 25097946Sdesfstat_kvm(int what, int arg) 25197946Sdes{ 25297946Sdes struct kinfo_proc *p, *plast; 25397946Sdes char buf[_POSIX2_LINE_MAX]; 25497946Sdes int cnt; 25597946Sdes 25697946Sdes ALLOC_OFILES(256); /* reserve space for file pointers */ 25797946Sdes 2581590Srgrimes /* 2591590Srgrimes * Discard setgid privileges if not the running kernel so that bad 2601590Srgrimes * guys can't print interesting stuff from kernel memory. 2611590Srgrimes */ 2621590Srgrimes if (nlistf != NULL || memf != NULL) 2631590Srgrimes setgid(getgid()); 2641590Srgrimes 26527272Scharnier if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 26627272Scharnier errx(1, "%s", buf); 26782664Sru setgid(getgid()); 2681590Srgrimes#ifdef notdef 26927272Scharnier if (kvm_nlist(kd, nl) != 0) 27027272Scharnier errx(1, "no namelist: %s", kvm_geterr(kd)); 2711590Srgrimes#endif 27227272Scharnier if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) 27327272Scharnier errx(1, "%s", kvm_geterr(kd)); 274116717Smaxim print_header(); 2751590Srgrimes for (plast = &p[cnt]; p < plast; ++p) { 27669896Smckusick if (p->ki_stat == SZOMB) 2771590Srgrimes continue; 2781590Srgrimes dofiles(p); 27959029Sgreen if (mflg) 28059029Sgreen dommap(p); 2811590Srgrimes } 2821590Srgrimes} 2831590Srgrimes 28497946Sdesstatic void 28597946Sdesfstat_sysctl(int what, int arg) 28697946Sdes{ 28797946Sdes 28897946Sdes /* not yet implemented */ 28997946Sdes fstat_kvm(what, arg); 29097946Sdes} 29197946Sdes 29293427Sdwmaloneconst char *Uname, *Comm; 2931590Srgrimesint Pid; 2941590Srgrimes 2951590Srgrimes#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 2961590Srgrimes switch(i) { \ 2971590Srgrimes case TEXT: \ 2981590Srgrimes printf(" text"); \ 2991590Srgrimes break; \ 3001590Srgrimes case CDIR: \ 3011590Srgrimes printf(" wd"); \ 3021590Srgrimes break; \ 3031590Srgrimes case RDIR: \ 3041590Srgrimes printf(" root"); \ 3051590Srgrimes break; \ 3061590Srgrimes case TRACE: \ 3071590Srgrimes printf(" tr"); \ 3081590Srgrimes break; \ 30959029Sgreen case MMAP: \ 31059029Sgreen printf(" mmap"); \ 31159029Sgreen break; \ 3121590Srgrimes default: \ 3131590Srgrimes printf(" %4d", i); \ 3141590Srgrimes break; \ 3151590Srgrimes } 3161590Srgrimes 3171590Srgrimes/* 3181590Srgrimes * print open files attributed to this process 3191590Srgrimes */ 3201590Srgrimesvoid 321131293Sdwmalonedofiles(struct kinfo_proc *kp) 3221590Srgrimes{ 32327272Scharnier int i; 3241590Srgrimes struct file file; 3251590Srgrimes struct filedesc0 filed0; 3261590Srgrimes#define filed filed0.fd_fd 3271590Srgrimes 32869896Smckusick Uname = user_from_uid(kp->ki_uid, 0); 32969896Smckusick Pid = kp->ki_pid; 33069896Smckusick Comm = kp->ki_comm; 3311590Srgrimes 33269896Smckusick if (kp->ki_fd == NULL) 3331590Srgrimes return; 33469896Smckusick if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) { 33537453Sbde dprintf(stderr, "can't read filedesc at %p for pid %d\n", 33669896Smckusick (void *)kp->ki_fd, Pid); 3371590Srgrimes return; 3381590Srgrimes } 3391590Srgrimes /* 3401590Srgrimes * root directory vnode, if one 3411590Srgrimes */ 3421590Srgrimes if (filed.fd_rdir) 3431590Srgrimes vtrans(filed.fd_rdir, RDIR, FREAD); 3441590Srgrimes /* 3451590Srgrimes * current working directory vnode 3461590Srgrimes */ 3471590Srgrimes vtrans(filed.fd_cdir, CDIR, FREAD); 3481590Srgrimes /* 3491590Srgrimes * ktrace vnode, if one 3501590Srgrimes */ 35169896Smckusick if (kp->ki_tracep) 35269896Smckusick vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE); 3531590Srgrimes /* 35417813Speter * text vnode, if one 35517813Speter */ 35669896Smckusick if (kp->ki_textvp) 35769896Smckusick vtrans(kp->ki_textvp, TEXT, FREAD); 35817813Speter /* 3591590Srgrimes * open files 3601590Srgrimes */ 3611590Srgrimes#define FPSIZE (sizeof (struct file *)) 3621590Srgrimes ALLOC_OFILES(filed.fd_lastfile+1); 3631590Srgrimes if (filed.fd_nfiles > NDFILE) { 3641590Srgrimes if (!KVM_READ(filed.fd_ofiles, ofiles, 3651590Srgrimes (filed.fd_lastfile+1) * FPSIZE)) { 3661590Srgrimes dprintf(stderr, 36737453Sbde "can't read file structures at %p for pid %d\n", 36837453Sbde (void *)filed.fd_ofiles, Pid); 3691590Srgrimes return; 3701590Srgrimes } 3711590Srgrimes } else 3721590Srgrimes bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 3731590Srgrimes for (i = 0; i <= filed.fd_lastfile; i++) { 3741590Srgrimes if (ofiles[i] == NULL) 3751590Srgrimes continue; 3761590Srgrimes if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 37737453Sbde dprintf(stderr, "can't read file %d at %p for pid %d\n", 37837453Sbde i, (void *)ofiles[i], Pid); 3791590Srgrimes continue; 3801590Srgrimes } 3811590Srgrimes if (file.f_type == DTYPE_VNODE) 382109153Sdillon vtrans(file.f_data, i, file.f_flag); 3831590Srgrimes else if (file.f_type == DTYPE_SOCKET) { 3841590Srgrimes if (checkfile == 0) 385109153Sdillon socktrans(file.f_data, i); 3861590Srgrimes } 38717808Speter#ifdef DTYPE_PIPE 38817808Speter else if (file.f_type == DTYPE_PIPE) { 38917808Speter if (checkfile == 0) 390109153Sdillon pipetrans(file.f_data, i, file.f_flag); 39117808Speter } 39217808Speter#endif 39378401Sroam#ifdef DTYPE_FIFO 39478401Sroam else if (file.f_type == DTYPE_FIFO) { 39578401Sroam if (checkfile == 0) 396109153Sdillon vtrans(file.f_data, i, file.f_flag); 39778401Sroam } 39878401Sroam#endif 3991590Srgrimes else { 4008874Srgrimes dprintf(stderr, 40178401Sroam "unknown file type %d for file %d of pid %d\n", 40278401Sroam file.f_type, i, Pid); 4031590Srgrimes } 4041590Srgrimes } 4051590Srgrimes} 4061590Srgrimes 4071590Srgrimesvoid 408131293Sdwmalonedommap(struct kinfo_proc *kp) 40959029Sgreen{ 41069896Smckusick vm_map_t map; 41159029Sgreen struct vmspace vmspace; 41259029Sgreen struct vm_map_entry entry; 41359029Sgreen vm_map_entry_t entryp; 41459029Sgreen struct vm_object object; 41559029Sgreen vm_object_t objp; 41659029Sgreen int prot, fflags; 41759029Sgreen 41869896Smckusick if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) { 41969896Smckusick dprintf(stderr, 42069896Smckusick "can't read vmspace at %p for pid %d\n", 42169896Smckusick (void *)kp->ki_vmspace, Pid); 42259029Sgreen return; 42359029Sgreen } 42459029Sgreen map = &vmspace.vm_map; 42559029Sgreen 42672527Siedowse for (entryp = map->header.next; 42772527Siedowse entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) { 42859029Sgreen if (!KVM_READ(entryp, &entry, sizeof(entry))) { 42959029Sgreen dprintf(stderr, 43059029Sgreen "can't read vm_map_entry at %p for pid %d\n", 43159029Sgreen (void *)entryp, Pid); 43259029Sgreen return; 43359029Sgreen } 43459029Sgreen 43559029Sgreen if (entry.eflags & MAP_ENTRY_IS_SUB_MAP) 43659029Sgreen continue; 43759029Sgreen 43859029Sgreen if ((objp = entry.object.vm_object) == NULL) 43959029Sgreen continue; 44059029Sgreen 44159029Sgreen for (; objp; objp = object.backing_object) { 44259029Sgreen if (!KVM_READ(objp, &object, sizeof(object))) { 44359029Sgreen dprintf(stderr, 44459029Sgreen "can't read vm_object at %p for pid %d\n", 44559029Sgreen (void *)objp, Pid); 44659029Sgreen return; 44759029Sgreen } 44859029Sgreen } 44959029Sgreen 45059029Sgreen prot = entry.protection; 45159029Sgreen fflags = (prot & VM_PROT_READ ? FREAD : 0) | 45259029Sgreen (prot & VM_PROT_WRITE ? FWRITE : 0); 45359029Sgreen 45459029Sgreen switch (object.type) { 45559029Sgreen case OBJT_VNODE: 45659029Sgreen vtrans((struct vnode *)object.handle, MMAP, fflags); 45759029Sgreen break; 45859029Sgreen default: 45959029Sgreen break; 46059029Sgreen } 46159029Sgreen } 46259029Sgreen} 46359029Sgreen 46459029Sgreenvoid 465131293Sdwmalonevtrans(struct vnode *vp, int i, int flag) 4661590Srgrimes{ 4671590Srgrimes struct vnode vn; 4681590Srgrimes struct filestat fst; 469103325Snjl char rw[3], mode[15], tagstr[12], *tagptr; 47093427Sdwmalone const char *badtype, *filename; 4711590Srgrimes 4721590Srgrimes filename = badtype = NULL; 4731590Srgrimes if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 47437453Sbde dprintf(stderr, "can't read vnode at %p for pid %d\n", 47537453Sbde (void *)vp, Pid); 4761590Srgrimes return; 4771590Srgrimes } 478103325Snjl if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) || 479103325Snjl !KVM_READ(tagptr, tagstr, sizeof tagstr)) { 480103325Snjl dprintf(stderr, "can't read v_tag at %p for pid %d\n", 481103325Snjl (void *)vp, Pid); 482103325Snjl return; 483103325Snjl } 484103325Snjl tagstr[sizeof(tagstr) - 1] = '\0'; 485103325Snjl if (vn.v_type == VNON) 4861590Srgrimes badtype = "none"; 4871590Srgrimes else if (vn.v_type == VBAD) 4881590Srgrimes badtype = "bad"; 489103325Snjl else { 490103325Snjl if (!strcmp("ufs", tagstr)) { 4911590Srgrimes if (!ufs_filestat(&vn, &fst)) 4921590Srgrimes badtype = "error"; 493103325Snjl } else if (!strcmp("devfs", tagstr)) { 49488051Sgreen if (!devfs_filestat(&vn, &fst)) 49588051Sgreen badtype = "error"; 496103325Snjl } else if (!strcmp("nfs", tagstr)) { 4971590Srgrimes if (!nfs_filestat(&vn, &fst)) 4981590Srgrimes badtype = "error"; 499103325Snjl } else if (!strcmp("msdosfs", tagstr)) { 50058125Sgreen if (!msdosfs_filestat(&vn, &fst)) 50158125Sgreen badtype = "error"; 502103325Snjl } else if (!strcmp("isofs", tagstr)) { 50358125Sgreen if (!isofs_filestat(&vn, &fst)) 50458125Sgreen badtype = "error"; 505103325Snjl } else { 506103325Snjl static char unknown[32]; 507103325Snjl snprintf(unknown, sizeof unknown, "?(%s)", tagstr); 50893427Sdwmalone badtype = unknown; 5091590Srgrimes } 5101590Srgrimes } 5111590Srgrimes if (checkfile) { 5121590Srgrimes int fsmatch = 0; 51393427Sdwmalone DEVS *d; 5141590Srgrimes 5151590Srgrimes if (badtype) 5161590Srgrimes return; 5171590Srgrimes for (d = devs; d != NULL; d = d->next) 5181590Srgrimes if (d->fsid == fst.fsid) { 5191590Srgrimes fsmatch = 1; 5201590Srgrimes if (d->ino == fst.fileid) { 5211590Srgrimes filename = d->name; 5221590Srgrimes break; 5231590Srgrimes } 5241590Srgrimes } 5251590Srgrimes if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 5261590Srgrimes return; 5271590Srgrimes } 5281590Srgrimes PREFIX(i); 5291590Srgrimes if (badtype) { 5301590Srgrimes (void)printf(" - - %10s -\n", badtype); 5311590Srgrimes return; 5321590Srgrimes } 5331590Srgrimes if (nflg) 5341590Srgrimes (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 5351590Srgrimes else 5361590Srgrimes (void)printf(" %-8s", getmnton(vn.v_mount)); 5371590Srgrimes if (nflg) 5381590Srgrimes (void)sprintf(mode, "%o", fst.mode); 5391590Srgrimes else 5401590Srgrimes strmode(fst.mode, mode); 54137453Sbde (void)printf(" %6ld %10s", fst.fileid, mode); 5421590Srgrimes switch (vn.v_type) { 5431590Srgrimes case VBLK: 5441590Srgrimes case VCHR: { 5451590Srgrimes char *name; 5461590Srgrimes 5478874Srgrimes if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 5481590Srgrimes S_IFCHR : S_IFBLK)) == NULL)) 5491590Srgrimes printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 5501590Srgrimes else 5511590Srgrimes printf(" %6s", name); 5521590Srgrimes break; 5531590Srgrimes } 5541590Srgrimes default: 55528948Salex printf(" %6lu", fst.size); 5561590Srgrimes } 5571590Srgrimes rw[0] = '\0'; 5581590Srgrimes if (flag & FREAD) 5591590Srgrimes strcat(rw, "r"); 5601590Srgrimes if (flag & FWRITE) 5611590Srgrimes strcat(rw, "w"); 5621590Srgrimes printf(" %2s", rw); 5631590Srgrimes if (filename && !fsflg) 5641590Srgrimes printf(" %s", filename); 5651590Srgrimes putchar('\n'); 5661590Srgrimes} 5671590Srgrimes 5681590Srgrimesint 569131293Sdwmaloneufs_filestat(struct vnode *vp, struct filestat *fsp) 5701590Srgrimes{ 5711590Srgrimes struct inode inode; 5721590Srgrimes 5731590Srgrimes if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 57437453Sbde dprintf(stderr, "can't read inode at %p for pid %d\n", 57537453Sbde (void *)VTOI(vp), Pid); 5761590Srgrimes return 0; 5771590Srgrimes } 57853133Sgreen /* 579130640Sphk * The st_dev from stat(2) is a dev_t. These kernel structures 580130640Sphk * contain cdev pointers. We need to convert to dev_t to make 58153133Sgreen * comparisons 58253133Sgreen */ 58386100Sdwmalone fsp->fsid = dev2udev(inode.i_dev); 5841590Srgrimes fsp->fileid = (long)inode.i_number; 5851590Srgrimes fsp->mode = (mode_t)inode.i_mode; 5861590Srgrimes fsp->size = (u_long)inode.i_size; 58798542Smckusick#if should_be_but_is_hard 588116780Sjmg /* XXX - need to load i_ump and i_din[12] from kernel memory */ 589116780Sjmg if (inode.i_ump->um_fstype == UFS1) 590116780Sjmg fsp->rdev = inode.i_din1->di_rdev; 591116780Sjmg else 592116780Sjmg fsp->rdev = inode.i_din2->di_rdev; 59398542Smckusick#else 59498542Smckusick fsp->rdev = 0; 59598542Smckusick#endif 5961590Srgrimes 5971590Srgrimes return 1; 5981590Srgrimes} 5991590Srgrimes 6001590Srgrimesint 601131293Sdwmalonedevfs_filestat(struct vnode *vp, struct filestat *fsp) 60288051Sgreen{ 60388051Sgreen struct devfs_dirent devfs_dirent; 60488051Sgreen struct mount mount; 60588051Sgreen struct vnode vnode; 60688051Sgreen 60788051Sgreen if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) { 60888051Sgreen dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n", 60988051Sgreen (void *)vp->v_data, Pid); 61088051Sgreen return 0; 61188051Sgreen } 61288051Sgreen if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) { 61388051Sgreen dprintf(stderr, "can't read mount at %p for pid %d\n", 61488051Sgreen (void *)vp->v_mount, Pid); 61588051Sgreen return 0; 61688051Sgreen } 61788051Sgreen if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) { 61888051Sgreen dprintf(stderr, "can't read vnode at %p for pid %d\n", 61988051Sgreen (void *)devfs_dirent.de_vnode, Pid); 62088051Sgreen return 0; 62188051Sgreen } 62288051Sgreen fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0]; 62388051Sgreen fsp->fileid = devfs_dirent.de_inode; 62488051Sgreen fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; 62588051Sgreen fsp->size = 0; 626116556Sjmg fsp->rdev = dev2udev(vnode.v_rdev); 62788051Sgreen 62888051Sgreen return 1; 62988051Sgreen} 63088051Sgreen 63188051Sgreenint 632131293Sdwmalonenfs_filestat(struct vnode *vp, struct filestat *fsp) 6331590Srgrimes{ 6341590Srgrimes struct nfsnode nfsnode; 63593427Sdwmalone mode_t mode; 6361590Srgrimes 6371590Srgrimes if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 63837453Sbde dprintf(stderr, "can't read nfsnode at %p for pid %d\n", 63937453Sbde (void *)VTONFS(vp), Pid); 6401590Srgrimes return 0; 6411590Srgrimes } 6421590Srgrimes fsp->fsid = nfsnode.n_vattr.va_fsid; 6431590Srgrimes fsp->fileid = nfsnode.n_vattr.va_fileid; 6441590Srgrimes fsp->size = nfsnode.n_size; 6451590Srgrimes fsp->rdev = nfsnode.n_vattr.va_rdev; 6461590Srgrimes mode = (mode_t)nfsnode.n_vattr.va_mode; 6471590Srgrimes switch (vp->v_type) { 6481590Srgrimes case VREG: 6491590Srgrimes mode |= S_IFREG; 6501590Srgrimes break; 6511590Srgrimes case VDIR: 6521590Srgrimes mode |= S_IFDIR; 6531590Srgrimes break; 6541590Srgrimes case VBLK: 6551590Srgrimes mode |= S_IFBLK; 6561590Srgrimes break; 6571590Srgrimes case VCHR: 6581590Srgrimes mode |= S_IFCHR; 6591590Srgrimes break; 6601590Srgrimes case VLNK: 6611590Srgrimes mode |= S_IFLNK; 6621590Srgrimes break; 6631590Srgrimes case VSOCK: 6641590Srgrimes mode |= S_IFSOCK; 6651590Srgrimes break; 6661590Srgrimes case VFIFO: 6671590Srgrimes mode |= S_IFIFO; 6681590Srgrimes break; 66948463Sru case VNON: 67048463Sru case VBAD: 67148463Sru return 0; 6721590Srgrimes }; 6731590Srgrimes fsp->mode = mode; 6741590Srgrimes 6751590Srgrimes return 1; 6761590Srgrimes} 6771590Srgrimes 6781590Srgrimes 6791590Srgrimeschar * 680131293Sdwmalonegetmnton(struct mount *m) 6811590Srgrimes{ 6821590Srgrimes static struct mount mount; 6831590Srgrimes static struct mtab { 6841590Srgrimes struct mtab *next; 6851590Srgrimes struct mount *m; 6861590Srgrimes char mntonname[MNAMELEN]; 6871590Srgrimes } *mhead = NULL; 68893427Sdwmalone struct mtab *mt; 6891590Srgrimes 6901590Srgrimes for (mt = mhead; mt != NULL; mt = mt->next) 6911590Srgrimes if (m == mt->m) 6921590Srgrimes return (mt->mntonname); 6931590Srgrimes if (!KVM_READ(m, &mount, sizeof(struct mount))) { 69437453Sbde warnx("can't read mount table at %p", (void *)m); 6951590Srgrimes return (NULL); 6961590Srgrimes } 69727272Scharnier if ((mt = malloc(sizeof (struct mtab))) == NULL) 69827272Scharnier err(1, NULL); 6991590Srgrimes mt->m = m; 7001590Srgrimes bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 7011590Srgrimes mt->next = mhead; 7021590Srgrimes mhead = mt; 7031590Srgrimes return (mt->mntonname); 7041590Srgrimes} 7051590Srgrimes 7061590Srgrimesvoid 707131293Sdwmalonepipetrans(struct pipe *pi, int i, int flag) 70817808Speter{ 70917808Speter struct pipe pip; 71017808Speter char rw[3]; 71117808Speter 71217808Speter PREFIX(i); 71317808Speter 71417808Speter /* fill in socket */ 71517808Speter if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { 71637453Sbde dprintf(stderr, "can't read pipe at %p\n", (void *)pi); 71717808Speter goto bad; 71817808Speter } 71917808Speter 72080355Smjacob printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer); 72117808Speter printf(" %6d", (int)pip.pipe_buffer.cnt); 72217808Speter rw[0] = '\0'; 72317808Speter if (flag & FREAD) 72417808Speter strcat(rw, "r"); 72517808Speter if (flag & FWRITE) 72617808Speter strcat(rw, "w"); 72717808Speter printf(" %2s", rw); 72817808Speter putchar('\n'); 72917808Speter return; 73017808Speter 73117808Speterbad: 73217808Speter printf("* error\n"); 73317808Speter} 73417808Speter 73517808Spetervoid 736131293Sdwmalonesocktrans(struct socket *sock, int i) 7371590Srgrimes{ 73893427Sdwmalone static const char *stypename[] = { 7391590Srgrimes "unused", /* 0 */ 7401590Srgrimes "stream", /* 1 */ 7411590Srgrimes "dgram", /* 2 */ 7421590Srgrimes "raw", /* 3 */ 7431590Srgrimes "rdm", /* 4 */ 7441590Srgrimes "seqpak" /* 5 */ 7451590Srgrimes }; 7461590Srgrimes#define STYPEMAX 5 7471590Srgrimes struct socket so; 7481590Srgrimes struct protosw proto; 7491590Srgrimes struct domain dom; 7501590Srgrimes struct inpcb inpcb; 7511590Srgrimes struct unpcb unpcb; 7521590Srgrimes int len; 75393427Sdwmalone char dname[32]; 7541590Srgrimes 7551590Srgrimes PREFIX(i); 7561590Srgrimes 7571590Srgrimes /* fill in socket */ 7581590Srgrimes if (!KVM_READ(sock, &so, sizeof(struct socket))) { 75937453Sbde dprintf(stderr, "can't read sock at %p\n", (void *)sock); 7601590Srgrimes goto bad; 7611590Srgrimes } 7621590Srgrimes 7631590Srgrimes /* fill in protosw entry */ 7641590Srgrimes if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 76537453Sbde dprintf(stderr, "can't read protosw at %p", 76637453Sbde (void *)so.so_proto); 7671590Srgrimes goto bad; 7681590Srgrimes } 7691590Srgrimes 7701590Srgrimes /* fill in domain */ 7711590Srgrimes if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 77237453Sbde dprintf(stderr, "can't read domain at %p\n", 77337453Sbde (void *)proto.pr_domain); 7741590Srgrimes goto bad; 7751590Srgrimes } 7761590Srgrimes 7771590Srgrimes if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 7781590Srgrimes sizeof(dname) - 1)) < 0) { 77937453Sbde dprintf(stderr, "can't read domain name at %p\n", 78037453Sbde (void *)dom.dom_name); 7811590Srgrimes dname[0] = '\0'; 7821590Srgrimes } 7831590Srgrimes else 7841590Srgrimes dname[len] = '\0'; 7851590Srgrimes 7861590Srgrimes if ((u_short)so.so_type > STYPEMAX) 7871590Srgrimes printf("* %s ?%d", dname, so.so_type); 7881590Srgrimes else 7891590Srgrimes printf("* %s %s", dname, stypename[so.so_type]); 7901590Srgrimes 7918874Srgrimes /* 7921590Srgrimes * protocol specific formatting 7931590Srgrimes * 7941590Srgrimes * Try to find interesting things to print. For tcp, the interesting 7951590Srgrimes * thing is the address of the tcpcb, for udp and others, just the 7961590Srgrimes * inpcb (socket pcb). For unix domain, its the address of the socket 7971590Srgrimes * pcb and the address of the connected pcb (if connected). Otherwise 7981590Srgrimes * just print the protocol number and address of the socket itself. 7991590Srgrimes * The idea is not to duplicate netstat, but to make available enough 8001590Srgrimes * information for further analysis. 8011590Srgrimes */ 8021590Srgrimes switch(dom.dom_family) { 8031590Srgrimes case AF_INET: 80457345Sshin case AF_INET6: 8051590Srgrimes getinetproto(proto.pr_protocol); 8061590Srgrimes if (proto.pr_protocol == IPPROTO_TCP ) { 8071590Srgrimes if (so.so_pcb) { 8081590Srgrimes if (kvm_read(kd, (u_long)so.so_pcb, 8091590Srgrimes (char *)&inpcb, sizeof(struct inpcb)) 8101590Srgrimes != sizeof(struct inpcb)) { 8118874Srgrimes dprintf(stderr, 81237453Sbde "can't read inpcb at %p\n", 81337453Sbde (void *)so.so_pcb); 8141590Srgrimes goto bad; 8151590Srgrimes } 81680355Smjacob printf(" %lx", (u_long)inpcb.inp_ppcb); 8171590Srgrimes } 8181590Srgrimes } 8191590Srgrimes else if (so.so_pcb) 82080355Smjacob printf(" %lx", (u_long)so.so_pcb); 8211590Srgrimes break; 8221590Srgrimes case AF_UNIX: 8231590Srgrimes /* print address of pcb and connected pcb */ 8241590Srgrimes if (so.so_pcb) { 82580355Smjacob printf(" %lx", (u_long)so.so_pcb); 8261590Srgrimes if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 8271590Srgrimes sizeof(struct unpcb)) != sizeof(struct unpcb)){ 82837453Sbde dprintf(stderr, "can't read unpcb at %p\n", 82937453Sbde (void *)so.so_pcb); 8301590Srgrimes goto bad; 8311590Srgrimes } 8321590Srgrimes if (unpcb.unp_conn) { 8331590Srgrimes char shoconn[4], *cp; 8341590Srgrimes 8351590Srgrimes cp = shoconn; 836130489Srwatson if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE)) 8371590Srgrimes *cp++ = '<'; 8381590Srgrimes *cp++ = '-'; 839130489Srwatson if (!(so.so_snd.sb_state & SBS_CANTSENDMORE)) 8401590Srgrimes *cp++ = '>'; 8411590Srgrimes *cp = '\0'; 84280355Smjacob printf(" %s %lx", shoconn, 84380355Smjacob (u_long)unpcb.unp_conn); 8441590Srgrimes } 8451590Srgrimes } 8461590Srgrimes break; 8471590Srgrimes default: 8481590Srgrimes /* print protocol number and socket address */ 84980355Smjacob printf(" %d %lx", proto.pr_protocol, (u_long)sock); 8501590Srgrimes } 8511590Srgrimes printf("\n"); 8521590Srgrimes return; 8531590Srgrimesbad: 8541590Srgrimes printf("* error\n"); 8551590Srgrimes} 8561590Srgrimes 85753133Sgreen 8581590Srgrimes/* 859130640Sphk * Read the cdev structure in the kernel in order to work out the 860130640Sphk * associated dev_t 86153133Sgreen */ 862130640Sphkdev_t 863131293Sdwmalonedev2udev(struct cdev *dev) 86453133Sgreen{ 865104043Sphk struct cdev si; 86653133Sgreen 86753133Sgreen if (KVM_READ(dev, &si, sizeof si)) { 86853133Sgreen return si.si_udev; 86953133Sgreen } else { 870131293Sdwmalone dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev); 87153133Sgreen return -1; 87253133Sgreen } 87353133Sgreen} 87453133Sgreen 87553133Sgreen/* 8761590Srgrimes * getinetproto -- 8771590Srgrimes * print name of protocol number 8781590Srgrimes */ 8791590Srgrimesvoid 880131293Sdwmalonegetinetproto(int number) 8811590Srgrimes{ 88248463Sru static int isopen; 88393427Sdwmalone struct protoent *pe; 8841590Srgrimes 88548463Sru if (!isopen) 88648463Sru setprotoent(++isopen); 88748463Sru if ((pe = getprotobynumber(number)) != NULL) 88848463Sru printf(" %s", pe->p_name); 88948463Sru else 8901590Srgrimes printf(" %d", number); 8911590Srgrimes} 8921590Srgrimes 89317808Speterint 894131293Sdwmalonegetfname(const char *filename) 8951590Srgrimes{ 8961590Srgrimes struct stat statbuf; 8971590Srgrimes DEVS *cur; 8981590Srgrimes 8991590Srgrimes if (stat(filename, &statbuf)) { 90027272Scharnier warn("%s", filename); 9011590Srgrimes return(0); 9021590Srgrimes } 90327272Scharnier if ((cur = malloc(sizeof(DEVS))) == NULL) 90427272Scharnier err(1, NULL); 9051590Srgrimes cur->next = devs; 9061590Srgrimes devs = cur; 9071590Srgrimes 9081590Srgrimes cur->ino = statbuf.st_ino; 90986100Sdwmalone cur->fsid = statbuf.st_dev; 9101590Srgrimes cur->name = filename; 9111590Srgrimes return(1); 9121590Srgrimes} 9131590Srgrimes 9141590Srgrimesvoid 915131293Sdwmaloneusage(void) 9161590Srgrimes{ 9171590Srgrimes (void)fprintf(stderr, 91859029Sgreen "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 9191590Srgrimes exit(1); 9201590Srgrimes} 921