2 * Copyright (c) 1980, 1991, 1993, 1994
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
33 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)pstat.c 8.16 (Berkeley) 5/9/95
35 * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $
36 * $DragonFly: src/usr.sbin/pstat/pstat.c,v 1.8 2004/10/08 09:53:02 dillon Exp $
39 #define _KERNEL_STRUCTURES
41 #include <sys/param.h>
43 #include <sys/vnode.h>
44 #include <sys/ucred.h>
46 #include <vfs/ufs/quota.h>
47 #include <vfs/ufs/inode.h>
48 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <vfs/union/union.h>
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
56 #include <nfs/nfsnode.h>
57 #include <sys/ioctl.h>
58 #include <sys/ioctl_compat.h> /* XXX NTTYDISC is too well hidden */
61 #include <sys/blist.h>
64 #include <sys/sysctl.h>
77 #define NLMANDATORYBEG 0
79 { "_mountlist" }, /* address of head of mount list. */
86 #define NLMANDATORYEND FNL_MAXFILE /* names up to here are mandatory */
87 #define SCONS NLMANDATORYEND + 1
89 #define SPTY NLMANDATORYEND + 2
91 #define SNPTY NLMANDATORYEND + 3
97 #define SCCONS (SNPTY+1)
99 #define NSCCONS (SNPTY+2)
101 #define SIO (SNPTY+3)
103 #define NSIO (SNPTY+4)
107 #define NRC (SNPTY+6)
111 #define NCY (SNPTY+8)
115 #define NSI (SNPTY+10)
134 { MNT_RDONLY, "rdonly" },
135 { MNT_SYNCHRONOUS, "sync" },
136 { MNT_NOEXEC, "noexec" },
137 { MNT_NOSUID, "nosuid" },
138 { MNT_NODEV, "nodev" },
139 { MNT_UNION, "union" },
140 { MNT_ASYNC, "async" },
141 { MNT_SUIDDIR, "suiddir" },
142 { MNT_SOFTDEP, "softdep" },
143 { MNT_NOSYMFOLLOW, "nosymfollow" },
144 { MNT_NOATIME, "noatime" },
145 { MNT_NOCLUSTERR, "noclusterread" },
146 { MNT_NOCLUSTERW, "noclusterwrite" },
147 { MNT_EXRDONLY, "exrdonly" },
148 { MNT_EXPORTED, "exported" },
149 { MNT_DEFEXPORTED, "defexported" },
150 { MNT_EXPORTANON, "exportanon" },
151 { MNT_EXKERB, "exkerb" },
152 { MNT_EXPUBLIC, "public" },
153 { MNT_LOCAL, "local" },
154 { MNT_QUOTA, "quota" },
155 { MNT_ROOTFS, "rootfs" },
156 { MNT_USER, "user" },
157 { MNT_IGNORE, "ignore" },
158 { MNT_UPDATE, "update" },
159 { MNT_DELEXPORT, "delexport" },
160 { MNT_RELOAD, "reload" },
161 { MNT_FORCE, "force" },
166 #define SVAR(var) __STRING(var) /* to force expansion */
167 #define KGET(idx, var) \
168 KGET1(idx, &var, sizeof(var), SVAR(var))
169 #define KGET1(idx, p, s, msg) \
170 KGET2(nl[idx].n_value, p, s, msg)
171 #define KGET2(addr, p, s, msg) \
172 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
173 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
174 #define KGETN(idx, var) \
175 KGET1N(idx, &var, sizeof(var), SVAR(var))
176 #define KGET1N(idx, p, s, msg) \
177 KGET2N(nl[idx].n_value, p, s, msg)
178 #define KGET2N(addr, p, s, msg) \
179 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
180 #define KGETRET(addr, p, s, msg) \
181 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
182 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
187 int getfiles(char **, int *);
189 getmnt(struct mount *);
194 void mount_print(struct mount *);
195 void nfs_header(void);
196 int nfs_print(struct vnode *);
199 void ttyprt(struct tty *, int);
200 void ttytype(struct tty *, char *, int, int, int);
201 void ufs_header(void);
202 int ufs_print(struct vnode *);
203 void union_header(void);
204 int union_print(struct vnode *);
205 static void usage(void);
206 void vnode_header(void);
207 void vnode_print(struct vnode *, struct vnode *);
208 void vnodemode(void);
211 main(int argc, char **argv)
213 int ch, i, quit, ret;
214 int fileflag, ttyflag, vnodeflag;
215 char buf[_POSIX2_LINE_MAX],*opts;
217 fileflag = swapflag = ttyflag = vnodeflag = 0;
219 /* We will behave like good old swapinfo if thus invoked */
220 opts = strrchr(argv[0],'/');
225 if (!strcmp(opts,"swapinfo")) {
228 usagestr = "swapinfo [-k] [-M core] [-N system]";
230 opts = "TM:N:fiknstv";
231 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
234 while ((ch = getopt(argc, argv, opts)) != -1)
240 putenv("BLOCKSIZE=1K");
261 case 'i': /* Backward compatibility. */
262 errx(1, "vnode mode not supported");
274 * Discard setgid privileges if not the running kernel so that bad
275 * guys can't print interesting stuff from kernel memory.
277 if (nlistf != NULL || memf != NULL)
278 (void)setgid(getgid());
280 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
281 errx(1, "kvm_openfiles: %s", buf);
282 if ((ret = kvm_nlist(kd, nl)) != 0) {
284 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
285 for (i = NLMANDATORYBEG, quit = 0; i <= NLMANDATORYEND; i++)
286 if (!nl[i].n_value) {
288 warnx("undefined symbol: %s", nl[i].n_name);
293 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
295 if (fileflag || totalflag)
301 if (swapflag || totalflag)
309 fprintf(stderr, "usage: %s\n", usagestr);
314 struct vnode *avnode;
321 struct e_vnode *e_vnodebase, *endvnode, *evp;
323 struct mount *maddr, *mp;
326 e_vnodebase = loadvnodes(&numvnodes);
328 (void)printf("%7d vnodes\n", numvnodes);
331 endvnode = e_vnodebase + numvnodes;
332 (void)printf("%d active vnodes\n", numvnodes);
335 #define ST mp->mnt_stat
337 for (evp = e_vnodebase; evp < endvnode; evp++) {
339 if (vp->v_mount != maddr) {
343 if ((mp = getmnt(vp->v_mount)) == NULL)
348 if (!strcmp(ST.f_fstypename, "ufs") ||
349 !strcmp(ST.f_fstypename, "mfs"))
351 else if (!strcmp(ST.f_fstypename, "nfs"))
353 else if (!strcmp(ST.f_fstypename, "union"))
357 vnode_print(evp->avnode, vp);
358 if (!strcmp(ST.f_fstypename, "ufs") ||
359 !strcmp(ST.f_fstypename, "mfs"))
361 else if (!strcmp(ST.f_fstypename, "nfs"))
363 else if (!strcmp(ST.f_fstypename, "union"))
373 (void)printf("ADDR TYP VFLAG USE HOLD");
377 vnode_print(struct vnode *avnode, struct vnode *vp)
379 char *type, flags[32];
386 switch (vp->v_type) {
442 if (flag & VONWORKLST)
447 if (flag & VINACTIVE)
449 if (flag & VRECLAIMED)
456 (void)printf("%8lx %s %5s %4d %4d",
457 (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
463 (void)printf(" FILEID IFLAG RDEV|SZ");
467 ufs_print(struct vnode *vp)
470 struct inode inode, *ip = &inode;
471 char flagbuf[16], *flags = flagbuf;
475 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
477 if (flag & IN_ACCESS)
479 if (flag & IN_CHANGE)
481 if (flag & IN_UPDATE)
483 if (flag & IN_MODIFIED)
485 if (flag & IN_RENAME)
487 if (flag & IN_SHLOCK)
489 if (flag & IN_EXLOCK)
491 if (flag & IN_HASHED)
493 if (flag & IN_LAZYMOD)
499 (void)printf(" %6d %5s", ip->i_number, flagbuf);
500 type = ip->i_mode & S_IFMT;
501 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
502 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
503 (void)printf(" %2d,%-2d",
504 major(ip->i_rdev), minor(ip->i_rdev));
506 (void)printf(" %7s", name);
508 (void)printf(" %7qd", ip->i_size);
515 (void)printf(" FILEID NFLAG RDEV|SZ");
519 nfs_print(struct vnode *vp)
521 struct nfsnode nfsnode, *np = &nfsnode;
522 char flagbuf[16], *flags = flagbuf;
527 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
529 if (flag & NFLUSHWANT)
531 if (flag & NFLUSHINPROG)
533 if (flag & NMODIFIED)
535 if (flag & NWRITEERR)
537 if (flag & NQNFSNONCACHE)
539 if (flag & NQNFSWRITE)
541 if (flag & NQNFSEVICTED)
557 #define VT np->n_vattr
558 (void)printf(" %6ld %5s", VT.va_fileid, flagbuf);
559 type = VT.va_mode & S_IFMT;
560 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
561 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
562 (void)printf(" %2d,%-2d",
563 major(VT.va_rdev), minor(VT.va_rdev));
565 (void)printf(" %7s", name);
567 (void)printf(" %7qd", np->n_size);
574 (void)printf(" UPPER LOWER");
578 union_print(struct vnode *vp)
580 struct union_node unode, *up = &unode;
582 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
584 (void)printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
585 (u_long)(void *)up->un_lowervp);
590 * Given a pointer to a mount structure in kernel space,
591 * read it in and return a usable pointer to it.
594 getmnt(struct mount *maddr)
603 for (mt = mhead; mt != NULL; mt = mt->next)
604 if (maddr == mt->maddr)
606 if ((mt = malloc(sizeof(struct mtab))) == NULL)
608 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
616 mount_print(struct mount *mp)
621 #define ST mp->mnt_stat
622 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
623 ST.f_mntfromname, ST.f_mntonname);
624 if ((flags = mp->mnt_flag)) {
626 const char *sep = " (";
628 for (i = 0; mnt_flags[i].m_flag; i++) {
629 if (flags & mnt_flags[i].m_flag) {
630 (void)printf("%s%s", sep, mnt_flags[i].m_name);
631 flags &= ~mnt_flags[i].m_flag;
636 (void)printf("%sunknown_flags:%x", sep, flags);
644 loadvnodes(int *avnodes)
648 struct e_vnode *vnodebase;
654 return (kinfo_vnodes(avnodes));
658 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
659 err(1, "sysctl: KERN_VNODE");
660 if ((vnodebase = malloc(copysize)) == NULL)
662 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
663 err(1, "sysctl: KERN_VNODE");
664 if (copysize % sizeof(struct e_vnode))
665 errx(1, "vnode size mismatch");
666 *avnodes = copysize / sizeof(struct e_vnode);
672 * simulate what a running kernel does in in kinfo_vnode
675 kinfo_vnodes(int *avnodes)
677 struct mntlist mountlist;
678 struct mount *mp, mount, *mp_next;
679 struct vnode *vp, vnode, *vp_next;
680 char *vbuf, *evbuf, *bp;
683 #define VPTRSZ sizeof(struct vnode *)
684 #define VNODESZ sizeof(struct vnode)
686 KGET(V_NUMV, numvnodes);
687 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
690 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
691 KGET(V_MOUNTLIST, mountlist);
692 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
693 KGET2(mp, &mount, sizeof(mount), "mount entry");
694 mp_next = TAILQ_NEXT(&mount, mnt_list);
695 for (vp = TAILQ_FIRST(&mount.mnt_nvnodelist);
696 vp != NULL; vp = vp_next) {
697 KGET2(vp, &vnode, sizeof(vnode), "vnode");
698 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
699 if ((bp + VPTRSZ + VNODESZ) > evbuf)
700 /* XXX - should realloc */
701 errx(1, "no more room for vnodes");
702 memmove(bp, &vp, VPTRSZ);
704 memmove(bp, &vnode, VNODESZ);
708 if (mp == TAILQ_LAST(&mountlist, mntlist))
712 return ((struct e_vnode *)vbuf);
716 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
723 struct tty ttyb[1000];
729 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
731 len /= sizeof(ttyb[0]);
732 for (i = 0; i < len; i++) {
736 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
738 if (nl[SCONS].n_type != 0) {
739 (void)printf("1 console\n");
744 if (nl[NSCCONS].n_type != 0)
745 ttytype(tty, "vty", SCCONS, NSCCONS, 0);
746 if (nl[NSIO].n_type != 0)
747 ttytype(tty, "sio", SIO, NSIO, 0);
748 if (nl[NRC].n_type != 0)
749 ttytype(tty, "rc", RC, NRC, 0);
750 if (nl[NCY].n_type != 0)
751 ttytype(tty, "cy", CY, NCY, 0);
752 if (nl[NSI].n_type != 0)
753 ttytype(tty, "si", SI, NSI, 1);
755 if (nl[SNPTY].n_type != 0)
756 ttytype(tty, "pty", SPTY, SNPTY, 0);
760 ttytype(struct tty *tty, char *name, int type, int number, int indir)
764 struct tty **ttyaddr;
769 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
770 if (ntty > ttyspace) {
772 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
777 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs");
779 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs");
782 for (tp = tty; tp < &tty[ntty]; tp++)
783 ttyprt(tp, tp - tty);
796 { TS_CONNECTED, 'c'},
805 { TS_SO_OLOWAT, 'A'},
807 #ifdef TS_SO_OCOMPLETE
808 { TS_SO_OCOMPLETE, 'a'},
813 { TS_CAR_OFLOW, 'm'},
816 { TS_CTS_OFLOW, 'o'},
819 { TS_DSR_OFLOW, 'd'},
828 #ifdef TS_CAN_BYPASS_L_RINT
829 { TS_CAN_BYPASS_L_RINT, 'l'},
841 ttyprt(struct tty *tp, int line)
845 char *name, state[20];
847 if (usenumflag || tp->t_dev == 0 ||
848 (name = devname(tp->t_dev, S_IFCHR)) == NULL)
849 (void)printf("%7d ", line);
851 (void)printf("%7s ", name);
852 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
853 (void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
854 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
856 for (i = j = 0; ttystates[i].flag; i++)
857 if (tp->t_state&ttystates[i].flag)
858 state[j++] = ttystates[i].val;
862 (void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
864 if (tp->t_pgrp != NULL)
865 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
866 (void)printf("%6d ", pgid);
867 switch (tp->t_line) {
869 (void)printf("term\n");
872 (void)printf("ntty\n");
875 (void)printf("slip\n");
878 (void)printf("ppp\n");
881 (void)printf("%d\n", tp->t_line);
891 char *buf, flagbuf[16], *fbp;
892 int len, maxfile, nfile;
893 static char *dtypes[] = { "???", "inode", "socket" };
895 KGET(FNL_MAXFILE, maxfile);
897 KGET(FNL_NFILE, nfile);
898 (void)printf("%3d/%3d files\n", nfile, maxfile);
901 if (getfiles(&buf, &len) == -1)
904 * Getfiles returns in malloc'd memory a pointer to the first file
905 * structure, and then an array of file structs (whose addresses are
906 * derivable from the previous entry).
908 addr = ((struct filelist *)buf)->lh_first;
909 fp = (struct file *)(buf + sizeof(struct filelist));
910 nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
912 (void)printf("%d/%d open files\n", nfile, maxfile);
913 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
914 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
915 if ((unsigned)fp->f_type > DTYPE_SOCKET)
917 (void)printf("%8lx ", (u_long)(void *)addr);
918 (void)printf("%-8.8s", dtypes[fp->f_type]);
920 if (fp->f_flag & FREAD)
922 if (fp->f_flag & FWRITE)
924 if (fp->f_flag & FAPPEND)
926 #ifdef FSHLOCK /* currently gone */
927 if (fp->f_flag & FSHLOCK)
929 if (fp->f_flag & FEXLOCK)
932 if (fp->f_flag & FASYNC)
935 (void)printf("%6s %3d", flagbuf, fp->f_count);
936 (void)printf(" %3d", fp->f_msgcount);
937 (void)printf(" %8lx", (u_long)(void *)fp->f_data);
938 if (fp->f_offset < 0)
939 (void)printf(" %qx\n", fp->f_offset);
941 (void)printf(" %qd\n", fp->f_offset);
947 getfiles(char **abuf, int *alen)
955 * Add emulation of KINFO_FILE here.
958 errx(1, "files on dead kernel, not implemented");
962 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
963 warn("sysctl: KERN_FILE");
966 if ((buf = malloc(len)) == NULL)
968 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
969 warn("sysctl: KERN_FILE");
978 * swapmode is based on a program called swapinfo written
979 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
984 struct kvm_swap kswap[16];
987 int pagesize = getpagesize();
995 sizeof(kswap)/sizeof(kswap[0]),
996 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
999 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize))
1001 header = getbsize(&hlen, &blocksize);
1002 if (totalflag == 0) {
1003 (void)printf("%-15s %*s %8s %8s %8s %s\n",
1004 "Device", hlen, header,
1005 "Used", "Avail", "Capacity", "Type");
1007 for (i = 0; i < n; ++i) {
1010 kswap[i].ksw_devname,
1012 CONVERT(kswap[i].ksw_total)
1015 "%8d %8d %5.0f%% %s\n",
1016 CONVERT(kswap[i].ksw_used),
1017 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
1018 (double)kswap[i].ksw_used * 100.0 /
1019 (double)kswap[i].ksw_total,
1020 (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
1021 "Sequential" : "Interleaved"
1027 blocksize = 1024 * 1024;
1030 "%dM/%dM swap space\n",
1031 CONVERT(kswap[n].ksw_used),
1032 CONVERT(kswap[n].ksw_total)
1036 "%-15s %*d %8d %8d %5.0f%%\n",
1039 CONVERT(kswap[n].ksw_total),
1040 CONVERT(kswap[n].ksw_used),
1041 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
1042 (double)kswap[n].ksw_used * 100.0 /
1043 (double)kswap[n].ksw_total