2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95";
44 static const char rcsid[] =
45 "$Id: fstat.c,v 1.15 1998/05/16 21:35:37 markm Exp $";
48 #include <sys/param.h>
53 #include <sys/vnode.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
59 #include <sys/unpcb.h>
60 #include <sys/sysctl.h>
61 #include <sys/filedesc.h>
62 #include <sys/queue.h>
66 #include <ufs/ufs/quota.h>
67 #include <ufs/ufs/inode.h>
69 #include <sys/mount.h>
70 #include <nfs/nfsproto.h>
71 #include <nfs/rpcv2.h>
73 #include <nfs/nfsnode.h>
75 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79 #include <netinet/in_pcb.h>
116 struct nlist nl[] = {
121 int fsflg, /* show files on same filesystem as file(s) argument */
122 pflg, /* show files open by a particular pid */
123 uflg; /* show files open by a particular (effective) user */
124 int checkfile; /* true if restricting to particular files or filesystems */
125 int nflg; /* (numerical) display f.s. and rdev as dev_t */
126 int vflg; /* display errors in locating kernel data objects etc... */
128 #define dprintf if (vflg) fprintf
130 struct file **ofiles; /* buffer of pointers to file structures */
132 #define ALLOC_OFILES(d) \
133 if ((d) > maxfiles) { \
135 ofiles = malloc((d) * sizeof(struct file *)); \
136 if (ofiles == NULL) { \
143 * a kvm_read that returns true if everything is read
145 #define KVM_READ(kaddr, paddr, len) \
146 (kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
150 void dofiles __P((struct kinfo_proc *kp));
151 void vtrans __P((struct vnode *vp, int i, int flag));
152 int ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
153 int nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
154 char *getmnton __P((struct mount *m));
155 void pipetrans __P((struct pipe *pi, int i, int flag));
156 void socktrans __P((struct socket *sock, int i));
157 void getinetproto __P((int number));
158 int getfname __P((char *filename));
159 void usage __P((void));
167 register struct passwd *passwd;
168 struct kinfo_proc *p, *plast;
171 char buf[_POSIX2_LINE_MAX];
175 what = KERN_PROC_ALL;
176 nlistf = memf = NULL;
177 while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
194 if (!isdigit(*optarg)) {
195 warnx("-p requires a process id");
198 what = KERN_PROC_PID;
204 if (!(passwd = getpwnam(optarg)))
205 errx(1, "%s: unknown uid", optarg);
206 what = KERN_PROC_UID;
207 arg = passwd->pw_uid;
217 if (*(argv += optind)) {
218 for (; *argv; ++argv) {
222 if (!checkfile) /* file(s) specified, but none accessable */
226 ALLOC_OFILES(256); /* reserve space for file pointers */
228 if (fsflg && !checkfile) {
229 /* -f with no files means use wd */
230 if (getfname(".") == 0)
236 * Discard setgid privileges if not the running kernel so that bad
237 * guys can't print interesting stuff from kernel memory.
239 if (nlistf != NULL || memf != NULL)
242 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
245 if (kvm_nlist(kd, nl) != 0)
246 errx(1, "no namelist: %s", kvm_geterr(kd));
248 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
249 errx(1, "%s", kvm_geterr(kd));
252 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
255 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
256 if (checkfile && fsflg == 0)
261 for (plast = &p[cnt]; p < plast; ++p) {
262 if (p->kp_proc.p_stat == SZOMB)
272 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
292 * print open files attributed to this process
296 struct kinfo_proc *kp;
300 struct filedesc0 filed0;
301 #define filed filed0.fd_fd
302 struct proc *p = &kp->kp_proc;
303 struct eproc *ep = &kp->kp_eproc;
305 Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
311 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
312 dprintf(stderr, "can't read filedesc at %p for pid %d\n",
313 (void *)p->p_fd, Pid);
317 * root directory vnode, if one
320 vtrans(filed.fd_rdir, RDIR, FREAD);
322 * current working directory vnode
324 vtrans(filed.fd_cdir, CDIR, FREAD);
326 * ktrace vnode, if one
329 vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
334 vtrans(p->p_textvp, TEXT, FREAD);
338 #define FPSIZE (sizeof (struct file *))
339 ALLOC_OFILES(filed.fd_lastfile+1);
340 if (filed.fd_nfiles > NDFILE) {
341 if (!KVM_READ(filed.fd_ofiles, ofiles,
342 (filed.fd_lastfile+1) * FPSIZE)) {
344 "can't read file structures at %p for pid %d\n",
345 (void *)filed.fd_ofiles, Pid);
349 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
350 for (i = 0; i <= filed.fd_lastfile; i++) {
351 if (ofiles[i] == NULL)
353 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
354 dprintf(stderr, "can't read file %d at %p for pid %d\n",
355 i, (void *)ofiles[i], Pid);
358 if (file.f_type == DTYPE_VNODE)
359 vtrans((struct vnode *)file.f_data, i, file.f_flag);
360 else if (file.f_type == DTYPE_SOCKET) {
362 socktrans((struct socket *)file.f_data, i);
365 else if (file.f_type == DTYPE_PIPE) {
367 pipetrans((struct pipe *)file.f_data, i,
373 "unknown file type %d for file %d of pid %d\n",
374 file.f_type, i, Pid);
387 char rw[3], mode[15];
388 char *badtype = NULL, *filename, *getmnton();
390 filename = badtype = NULL;
391 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
392 dprintf(stderr, "can't read vnode at %p for pid %d\n",
396 if (vn.v_type == VNON || vn.v_tag == VT_NON)
398 else if (vn.v_type == VBAD)
403 if (!ufs_filestat(&vn, &fst))
407 if (!ufs_filestat(&vn, &fst))
411 if (!nfs_filestat(&vn, &fst))
415 static char unknown[10];
416 sprintf(badtype = unknown, "?(%x)", vn.v_tag);
426 for (d = devs; d != NULL; d = d->next)
427 if (d->fsid == fst.fsid) {
429 if (d->ino == fst.fileid) {
434 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
439 (void)printf(" - - %10s -\n", badtype);
443 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
445 (void)printf(" %-8s", getmnton(vn.v_mount));
447 (void)sprintf(mode, "%o", fst.mode);
449 strmode(fst.mode, mode);
450 (void)printf(" %6ld %10s", fst.fileid, mode);
456 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
457 S_IFCHR : S_IFBLK)) == NULL))
458 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
460 printf(" %6s", name);
464 printf(" %6lu", fst.size);
472 if (filename && !fsflg)
473 printf(" %s", filename);
478 ufs_filestat(vp, fsp)
480 struct filestat *fsp;
484 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
485 dprintf(stderr, "can't read inode at %p for pid %d\n",
486 (void *)VTOI(vp), Pid);
489 fsp->fsid = inode.i_dev & 0xffff;
490 fsp->fileid = (long)inode.i_number;
491 fsp->mode = (mode_t)inode.i_mode;
492 fsp->size = (u_long)inode.i_size;
493 fsp->rdev = inode.i_rdev;
499 nfs_filestat(vp, fsp)
501 struct filestat *fsp;
503 struct nfsnode nfsnode;
504 register mode_t mode;
506 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
507 dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
508 (void *)VTONFS(vp), Pid);
511 fsp->fsid = nfsnode.n_vattr.va_fsid;
512 fsp->fileid = nfsnode.n_vattr.va_fileid;
513 fsp->size = nfsnode.n_size;
514 fsp->rdev = nfsnode.n_vattr.va_rdev;
515 mode = (mode_t)nfsnode.n_vattr.va_mode;
516 switch (vp->v_type) {
549 static struct mount mount;
553 char mntonname[MNAMELEN];
555 register struct mtab *mt;
557 for (mt = mhead; mt != NULL; mt = mt->next)
559 return (mt->mntonname);
560 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
561 warnx("can't read mount table at %p", (void *)m);
564 if ((mt = malloc(sizeof (struct mtab))) == NULL)
567 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
570 return (mt->mntonname);
574 pipetrans(pi, i, flag)
585 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
586 dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
590 printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
591 printf(" %6d", (int)pip.pipe_buffer.cnt);
610 static char *stypename[] = {
620 struct protosw proto;
625 char dname[32], *strcpy();
630 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
631 dprintf(stderr, "can't read sock at %p\n", (void *)sock);
635 /* fill in protosw entry */
636 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
637 dprintf(stderr, "can't read protosw at %p",
638 (void *)so.so_proto);
643 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
644 dprintf(stderr, "can't read domain at %p\n",
645 (void *)proto.pr_domain);
649 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
650 sizeof(dname) - 1)) < 0) {
651 dprintf(stderr, "can't read domain name at %p\n",
652 (void *)dom.dom_name);
658 if ((u_short)so.so_type > STYPEMAX)
659 printf("* %s ?%d", dname, so.so_type);
661 printf("* %s %s", dname, stypename[so.so_type]);
664 * protocol specific formatting
666 * Try to find interesting things to print. For tcp, the interesting
667 * thing is the address of the tcpcb, for udp and others, just the
668 * inpcb (socket pcb). For unix domain, its the address of the socket
669 * pcb and the address of the connected pcb (if connected). Otherwise
670 * just print the protocol number and address of the socket itself.
671 * The idea is not to duplicate netstat, but to make available enough
672 * information for further analysis.
674 switch(dom.dom_family) {
676 getinetproto(proto.pr_protocol);
677 if (proto.pr_protocol == IPPROTO_TCP ) {
679 if (kvm_read(kd, (u_long)so.so_pcb,
680 (char *)&inpcb, sizeof(struct inpcb))
681 != sizeof(struct inpcb)) {
683 "can't read inpcb at %p\n",
687 printf(" %x", (int)inpcb.inp_ppcb);
691 printf(" %x", (int)so.so_pcb);
694 /* print address of pcb and connected pcb */
696 printf(" %x", (int)so.so_pcb);
697 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
698 sizeof(struct unpcb)) != sizeof(struct unpcb)){
699 dprintf(stderr, "can't read unpcb at %p\n",
703 if (unpcb.unp_conn) {
704 char shoconn[4], *cp;
707 if (!(so.so_state & SS_CANTRCVMORE))
710 if (!(so.so_state & SS_CANTSENDMORE))
713 printf(" %s %x", shoconn,
714 (int)unpcb.unp_conn);
719 /* print protocol number and socket address */
720 printf(" %d %x", proto.pr_protocol, (int)sock);
730 * print name of protocol number
758 printf(" %d", number);
771 if (stat(filename, &statbuf)) {
772 warn("%s", filename);
775 if ((cur = malloc(sizeof(DEVS))) == NULL)
780 cur->ino = statbuf.st_ino;
781 cur->fsid = statbuf.st_dev & 0xffff;
782 cur->name = filename;
789 (void)fprintf(stderr,
790 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");