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.6 2004/01/02 18:19:49 eirikn 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[16];
386 switch (vp->v_type) {
438 if (flag & VONWORKLST)
446 (void)printf("%8lx %s %5s %4d %4d",
447 (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
453 (void)printf(" FILEID IFLAG RDEV|SZ");
457 ufs_print(struct vnode *vp)
460 struct inode inode, *ip = &inode;
461 char flagbuf[16], *flags = flagbuf;
465 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
467 if (flag & IN_ACCESS)
469 if (flag & IN_CHANGE)
471 if (flag & IN_UPDATE)
473 if (flag & IN_MODIFIED)
475 if (flag & IN_RENAME)
477 if (flag & IN_SHLOCK)
479 if (flag & IN_EXLOCK)
481 if (flag & IN_HASHED)
483 if (flag & IN_LAZYMOD)
489 (void)printf(" %6d %5s", ip->i_number, flagbuf);
490 type = ip->i_mode & S_IFMT;
491 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
492 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
493 (void)printf(" %2d,%-2d",
494 major(ip->i_rdev), minor(ip->i_rdev));
496 (void)printf(" %7s", name);
498 (void)printf(" %7qd", ip->i_size);
505 (void)printf(" FILEID NFLAG RDEV|SZ");
509 nfs_print(struct vnode *vp)
511 struct nfsnode nfsnode, *np = &nfsnode;
512 char flagbuf[16], *flags = flagbuf;
517 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
519 if (flag & NFLUSHWANT)
521 if (flag & NFLUSHINPROG)
523 if (flag & NMODIFIED)
525 if (flag & NWRITEERR)
527 if (flag & NQNFSNONCACHE)
529 if (flag & NQNFSWRITE)
531 if (flag & NQNFSEVICTED)
547 #define VT np->n_vattr
548 (void)printf(" %6ld %5s", VT.va_fileid, flagbuf);
549 type = VT.va_mode & S_IFMT;
550 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
551 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
552 (void)printf(" %2d,%-2d",
553 major(VT.va_rdev), minor(VT.va_rdev));
555 (void)printf(" %7s", name);
557 (void)printf(" %7qd", np->n_size);
564 (void)printf(" UPPER LOWER");
568 union_print(struct vnode *vp)
570 struct union_node unode, *up = &unode;
572 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
574 (void)printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
575 (u_long)(void *)up->un_lowervp);
580 * Given a pointer to a mount structure in kernel space,
581 * read it in and return a usable pointer to it.
584 getmnt(struct mount *maddr)
593 for (mt = mhead; mt != NULL; mt = mt->next)
594 if (maddr == mt->maddr)
596 if ((mt = malloc(sizeof(struct mtab))) == NULL)
598 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
606 mount_print(struct mount *mp)
611 #define ST mp->mnt_stat
612 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
613 ST.f_mntfromname, ST.f_mntonname);
614 if ((flags = mp->mnt_flag)) {
616 const char *sep = " (";
618 for (i = 0; mnt_flags[i].m_flag; i++) {
619 if (flags & mnt_flags[i].m_flag) {
620 (void)printf("%s%s", sep, mnt_flags[i].m_name);
621 flags &= ~mnt_flags[i].m_flag;
626 (void)printf("%sunknown_flags:%x", sep, flags);
634 loadvnodes(int *avnodes)
638 struct e_vnode *vnodebase;
644 return (kinfo_vnodes(avnodes));
648 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
649 err(1, "sysctl: KERN_VNODE");
650 if ((vnodebase = malloc(copysize)) == NULL)
652 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
653 err(1, "sysctl: KERN_VNODE");
654 if (copysize % sizeof(struct e_vnode))
655 errx(1, "vnode size mismatch");
656 *avnodes = copysize / sizeof(struct e_vnode);
662 * simulate what a running kernel does in in kinfo_vnode
665 kinfo_vnodes(int *avnodes)
667 struct mntlist mountlist;
668 struct mount *mp, mount, *mp_next;
669 struct vnode *vp, vnode, *vp_next;
670 char *vbuf, *evbuf, *bp;
673 #define VPTRSZ sizeof(struct vnode *)
674 #define VNODESZ sizeof(struct vnode)
676 KGET(V_NUMV, numvnodes);
677 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
680 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
681 KGET(V_MOUNTLIST, mountlist);
682 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
683 KGET2(mp, &mount, sizeof(mount), "mount entry");
684 mp_next = TAILQ_NEXT(&mount, mnt_list);
685 for (vp = TAILQ_FIRST(&mount.mnt_nvnodelist);
686 vp != NULL; vp = vp_next) {
687 KGET2(vp, &vnode, sizeof(vnode), "vnode");
688 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
689 if ((bp + VPTRSZ + VNODESZ) > evbuf)
690 /* XXX - should realloc */
691 errx(1, "no more room for vnodes");
692 memmove(bp, &vp, VPTRSZ);
694 memmove(bp, &vnode, VNODESZ);
698 if (mp == TAILQ_LAST(&mountlist, mntlist))
702 return ((struct e_vnode *)vbuf);
706 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
713 struct tty ttyb[1000];
719 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
721 len /= sizeof(ttyb[0]);
722 for (i = 0; i < len; i++) {
726 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
728 if (nl[SCONS].n_type != 0) {
729 (void)printf("1 console\n");
734 if (nl[NSCCONS].n_type != 0)
735 ttytype(tty, "vty", SCCONS, NSCCONS, 0);
736 if (nl[NSIO].n_type != 0)
737 ttytype(tty, "sio", SIO, NSIO, 0);
738 if (nl[NRC].n_type != 0)
739 ttytype(tty, "rc", RC, NRC, 0);
740 if (nl[NCY].n_type != 0)
741 ttytype(tty, "cy", CY, NCY, 0);
742 if (nl[NSI].n_type != 0)
743 ttytype(tty, "si", SI, NSI, 1);
745 if (nl[SNPTY].n_type != 0)
746 ttytype(tty, "pty", SPTY, SNPTY, 0);
750 ttytype(struct tty *tty, char *name, int type, int number, int indir)
754 struct tty **ttyaddr;
759 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
760 if (ntty > ttyspace) {
762 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
767 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs");
769 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs");
772 for (tp = tty; tp < &tty[ntty]; tp++)
773 ttyprt(tp, tp - tty);
786 { TS_CONNECTED, 'c'},
795 { TS_SO_OLOWAT, 'A'},
797 #ifdef TS_SO_OCOMPLETE
798 { TS_SO_OCOMPLETE, 'a'},
803 { TS_CAR_OFLOW, 'm'},
806 { TS_CTS_OFLOW, 'o'},
809 { TS_DSR_OFLOW, 'd'},
818 #ifdef TS_CAN_BYPASS_L_RINT
819 { TS_CAN_BYPASS_L_RINT, 'l'},
831 ttyprt(struct tty *tp, int line)
835 char *name, state[20];
837 if (usenumflag || tp->t_dev == 0 ||
838 (name = devname(tp->t_dev, S_IFCHR)) == NULL)
839 (void)printf("%7d ", line);
841 (void)printf("%7s ", name);
842 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
843 (void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
844 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
846 for (i = j = 0; ttystates[i].flag; i++)
847 if (tp->t_state&ttystates[i].flag)
848 state[j++] = ttystates[i].val;
852 (void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
854 if (tp->t_pgrp != NULL)
855 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
856 (void)printf("%6d ", pgid);
857 switch (tp->t_line) {
859 (void)printf("term\n");
862 (void)printf("ntty\n");
865 (void)printf("slip\n");
868 (void)printf("ppp\n");
871 (void)printf("%d\n", tp->t_line);
881 char *buf, flagbuf[16], *fbp;
882 int len, maxfile, nfile;
883 static char *dtypes[] = { "???", "inode", "socket" };
885 KGET(FNL_MAXFILE, maxfile);
887 KGET(FNL_NFILE, nfile);
888 (void)printf("%3d/%3d files\n", nfile, maxfile);
891 if (getfiles(&buf, &len) == -1)
894 * Getfiles returns in malloc'd memory a pointer to the first file
895 * structure, and then an array of file structs (whose addresses are
896 * derivable from the previous entry).
898 addr = ((struct filelist *)buf)->lh_first;
899 fp = (struct file *)(buf + sizeof(struct filelist));
900 nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
902 (void)printf("%d/%d open files\n", nfile, maxfile);
903 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
904 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
905 if ((unsigned)fp->f_type > DTYPE_SOCKET)
907 (void)printf("%8lx ", (u_long)(void *)addr);
908 (void)printf("%-8.8s", dtypes[fp->f_type]);
910 if (fp->f_flag & FREAD)
912 if (fp->f_flag & FWRITE)
914 if (fp->f_flag & FAPPEND)
916 #ifdef FSHLOCK /* currently gone */
917 if (fp->f_flag & FSHLOCK)
919 if (fp->f_flag & FEXLOCK)
922 if (fp->f_flag & FASYNC)
925 (void)printf("%6s %3d", flagbuf, fp->f_count);
926 (void)printf(" %3d", fp->f_msgcount);
927 (void)printf(" %8lx", (u_long)(void *)fp->f_data);
928 if (fp->f_offset < 0)
929 (void)printf(" %qx\n", fp->f_offset);
931 (void)printf(" %qd\n", fp->f_offset);
937 getfiles(char **abuf, int *alen)
945 * Add emulation of KINFO_FILE here.
948 errx(1, "files on dead kernel, not implemented");
952 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
953 warn("sysctl: KERN_FILE");
956 if ((buf = malloc(len)) == NULL)
958 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
959 warn("sysctl: KERN_FILE");
968 * swapmode is based on a program called swapinfo written
969 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
974 struct kvm_swap kswap[16];
977 int pagesize = getpagesize();
985 sizeof(kswap)/sizeof(kswap[0]),
986 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
989 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize))
991 header = getbsize(&hlen, &blocksize);
992 if (totalflag == 0) {
993 (void)printf("%-15s %*s %8s %8s %8s %s\n",
994 "Device", hlen, header,
995 "Used", "Avail", "Capacity", "Type");
997 for (i = 0; i < n; ++i) {
1000 kswap[i].ksw_devname,
1002 CONVERT(kswap[i].ksw_total)
1005 "%8d %8d %5.0f%% %s\n",
1006 CONVERT(kswap[i].ksw_used),
1007 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
1008 (double)kswap[i].ksw_used * 100.0 /
1009 (double)kswap[i].ksw_total,
1010 (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
1011 "Sequential" : "Interleaved"
1017 blocksize = 1024 * 1024;
1020 "%dM/%dM swap space\n",
1021 CONVERT(kswap[n].ksw_used),
1022 CONVERT(kswap[n].ksw_total)
1026 "%-15s %*d %8d %8d %5.0f%%\n",
1029 CONVERT(kswap[n].ksw_total),
1030 CONVERT(kswap[n].ksw_used),
1031 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
1032 (double)kswap[n].ksw_used * 100.0 /
1033 (double)kswap[n].ksw_total