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.5 2003/11/03 19:31:41 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);
215 int ch, i, quit, ret;
216 int fileflag, ttyflag, vnodeflag;
217 char buf[_POSIX2_LINE_MAX],*opts;
219 fileflag = swapflag = ttyflag = vnodeflag = 0;
221 /* We will behave like good old swapinfo if thus invoked */
222 opts = strrchr(argv[0],'/');
227 if (!strcmp(opts,"swapinfo")) {
230 usagestr = "swapinfo [-k] [-M core] [-N system]";
232 opts = "TM:N:fiknstv";
233 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
236 while ((ch = getopt(argc, argv, opts)) != -1)
242 putenv("BLOCKSIZE=1K");
263 case 'i': /* Backward compatibility. */
264 errx(1, "vnode mode not supported");
276 * Discard setgid privileges if not the running kernel so that bad
277 * guys can't print interesting stuff from kernel memory.
279 if (nlistf != NULL || memf != NULL)
280 (void)setgid(getgid());
282 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
283 errx(1, "kvm_openfiles: %s", buf);
284 if ((ret = kvm_nlist(kd, nl)) != 0) {
286 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
287 for (i = NLMANDATORYBEG, quit = 0; i <= NLMANDATORYEND; i++)
288 if (!nl[i].n_value) {
290 warnx("undefined symbol: %s", nl[i].n_name);
295 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
297 if (fileflag || totalflag)
303 if (swapflag || totalflag)
311 fprintf(stderr, "usage: %s\n", usagestr);
316 struct vnode *avnode;
323 struct e_vnode *e_vnodebase, *endvnode, *evp;
325 struct mount *maddr, *mp;
328 e_vnodebase = loadvnodes(&numvnodes);
330 (void)printf("%7d vnodes\n", numvnodes);
333 endvnode = e_vnodebase + numvnodes;
334 (void)printf("%d active vnodes\n", numvnodes);
337 #define ST mp->mnt_stat
339 for (evp = e_vnodebase; evp < endvnode; evp++) {
341 if (vp->v_mount != maddr) {
345 if ((mp = getmnt(vp->v_mount)) == NULL)
350 if (!strcmp(ST.f_fstypename, "ufs") ||
351 !strcmp(ST.f_fstypename, "mfs"))
353 else if (!strcmp(ST.f_fstypename, "nfs"))
355 else if (!strcmp(ST.f_fstypename, "union"))
359 vnode_print(evp->avnode, vp);
360 if (!strcmp(ST.f_fstypename, "ufs") ||
361 !strcmp(ST.f_fstypename, "mfs"))
363 else if (!strcmp(ST.f_fstypename, "nfs"))
365 else if (!strcmp(ST.f_fstypename, "union"))
375 (void)printf("ADDR TYP VFLAG USE HOLD");
379 vnode_print(avnode, vp)
380 struct vnode *avnode;
383 char *type, flags[16];
390 switch (vp->v_type) {
442 if (flag & VONWORKLST)
450 (void)printf("%8lx %s %5s %4d %4d",
451 (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
457 (void)printf(" FILEID IFLAG RDEV|SZ");
465 struct inode inode, *ip = &inode;
466 char flagbuf[16], *flags = flagbuf;
470 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
472 if (flag & IN_ACCESS)
474 if (flag & IN_CHANGE)
476 if (flag & IN_UPDATE)
478 if (flag & IN_MODIFIED)
480 if (flag & IN_RENAME)
482 if (flag & IN_SHLOCK)
484 if (flag & IN_EXLOCK)
486 if (flag & IN_HASHED)
488 if (flag & IN_LAZYMOD)
494 (void)printf(" %6d %5s", ip->i_number, flagbuf);
495 type = ip->i_mode & S_IFMT;
496 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
497 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
498 (void)printf(" %2d,%-2d",
499 major(ip->i_rdev), minor(ip->i_rdev));
501 (void)printf(" %7s", name);
503 (void)printf(" %7qd", ip->i_size);
510 (void)printf(" FILEID NFLAG RDEV|SZ");
517 struct nfsnode nfsnode, *np = &nfsnode;
518 char flagbuf[16], *flags = flagbuf;
523 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
525 if (flag & NFLUSHWANT)
527 if (flag & NFLUSHINPROG)
529 if (flag & NMODIFIED)
531 if (flag & NWRITEERR)
533 if (flag & NQNFSNONCACHE)
535 if (flag & NQNFSWRITE)
537 if (flag & NQNFSEVICTED)
553 #define VT np->n_vattr
554 (void)printf(" %6ld %5s", VT.va_fileid, flagbuf);
555 type = VT.va_mode & S_IFMT;
556 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
557 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
558 (void)printf(" %2d,%-2d",
559 major(VT.va_rdev), minor(VT.va_rdev));
561 (void)printf(" %7s", name);
563 (void)printf(" %7qd", np->n_size);
570 (void)printf(" UPPER LOWER");
577 struct union_node unode, *up = &unode;
579 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
581 (void)printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
582 (u_long)(void *)up->un_lowervp);
587 * Given a pointer to a mount structure in kernel space,
588 * read it in and return a usable pointer to it.
601 for (mt = mhead; mt != NULL; mt = mt->next)
602 if (maddr == mt->maddr)
604 if ((mt = malloc(sizeof(struct mtab))) == NULL)
606 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
620 #define ST mp->mnt_stat
621 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
622 ST.f_mntfromname, ST.f_mntonname);
623 if ((flags = mp->mnt_flag)) {
625 const char *sep = " (";
627 for (i = 0; mnt_flags[i].m_flag; i++) {
628 if (flags & mnt_flags[i].m_flag) {
629 (void)printf("%s%s", sep, mnt_flags[i].m_name);
630 flags &= ~mnt_flags[i].m_flag;
635 (void)printf("%sunknown_flags:%x", sep, flags);
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(avnodes)
678 struct mntlist mountlist;
679 struct mount *mp, mount, *mp_next;
680 struct vnode *vp, vnode, *vp_next;
681 char *vbuf, *evbuf, *bp;
684 #define VPTRSZ sizeof(struct vnode *)
685 #define VNODESZ sizeof(struct vnode)
687 KGET(V_NUMV, numvnodes);
688 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
691 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
692 KGET(V_MOUNTLIST, mountlist);
693 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
694 KGET2(mp, &mount, sizeof(mount), "mount entry");
695 mp_next = TAILQ_NEXT(&mount, mnt_list);
696 for (vp = TAILQ_FIRST(&mount.mnt_nvnodelist);
697 vp != NULL; vp = vp_next) {
698 KGET2(vp, &vnode, sizeof(vnode), "vnode");
699 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
700 if ((bp + VPTRSZ + VNODESZ) > evbuf)
701 /* XXX - should realloc */
702 errx(1, "no more room for vnodes");
703 memmove(bp, &vp, VPTRSZ);
705 memmove(bp, &vnode, VNODESZ);
709 if (mp == TAILQ_LAST(&mountlist, mntlist))
713 return ((struct e_vnode *)vbuf);
717 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
724 struct tty ttyb[1000];
730 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
732 len /= sizeof(ttyb[0]);
733 for (i = 0; i < len; i++) {
737 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
739 if (nl[SCONS].n_type != 0) {
740 (void)printf("1 console\n");
745 if (nl[NSCCONS].n_type != 0)
746 ttytype(tty, "vty", SCCONS, NSCCONS, 0);
747 if (nl[NSIO].n_type != 0)
748 ttytype(tty, "sio", SIO, NSIO, 0);
749 if (nl[NRC].n_type != 0)
750 ttytype(tty, "rc", RC, NRC, 0);
751 if (nl[NCY].n_type != 0)
752 ttytype(tty, "cy", CY, NCY, 0);
753 if (nl[NSI].n_type != 0)
754 ttytype(tty, "si", SI, NSI, 1);
756 if (nl[SNPTY].n_type != 0)
757 ttytype(tty, "pty", SPTY, SNPTY, 0);
761 ttytype(tty, name, type, number, indir)
764 int type, number, indir;
768 struct tty **ttyaddr;
773 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
774 if (ntty > ttyspace) {
776 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
781 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs");
783 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs");
786 for (tp = tty; tp < &tty[ntty]; tp++)
787 ttyprt(tp, tp - tty);
800 { TS_CONNECTED, 'c'},
809 { TS_SO_OLOWAT, 'A'},
811 #ifdef TS_SO_OCOMPLETE
812 { TS_SO_OCOMPLETE, 'a'},
817 { TS_CAR_OFLOW, 'm'},
820 { TS_CTS_OFLOW, 'o'},
823 { TS_DSR_OFLOW, 'd'},
832 #ifdef TS_CAN_BYPASS_L_RINT
833 { TS_CAN_BYPASS_L_RINT, 'l'},
851 char *name, state[20];
853 if (usenumflag || tp->t_dev == 0 ||
854 (name = devname(tp->t_dev, S_IFCHR)) == NULL)
855 (void)printf("%7d ", line);
857 (void)printf("%7s ", name);
858 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
859 (void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
860 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
862 for (i = j = 0; ttystates[i].flag; i++)
863 if (tp->t_state&ttystates[i].flag)
864 state[j++] = ttystates[i].val;
868 (void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
870 if (tp->t_pgrp != NULL)
871 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
872 (void)printf("%6d ", pgid);
873 switch (tp->t_line) {
875 (void)printf("term\n");
878 (void)printf("ntty\n");
881 (void)printf("slip\n");
884 (void)printf("ppp\n");
887 (void)printf("%d\n", tp->t_line);
897 char *buf, flagbuf[16], *fbp;
898 int len, maxfile, nfile;
899 static char *dtypes[] = { "???", "inode", "socket" };
901 KGET(FNL_MAXFILE, maxfile);
903 KGET(FNL_NFILE, nfile);
904 (void)printf("%3d/%3d files\n", nfile, maxfile);
907 if (getfiles(&buf, &len) == -1)
910 * Getfiles returns in malloc'd memory a pointer to the first file
911 * structure, and then an array of file structs (whose addresses are
912 * derivable from the previous entry).
914 addr = ((struct filelist *)buf)->lh_first;
915 fp = (struct file *)(buf + sizeof(struct filelist));
916 nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
918 (void)printf("%d/%d open files\n", nfile, maxfile);
919 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
920 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
921 if ((unsigned)fp->f_type > DTYPE_SOCKET)
923 (void)printf("%8lx ", (u_long)(void *)addr);
924 (void)printf("%-8.8s", dtypes[fp->f_type]);
926 if (fp->f_flag & FREAD)
928 if (fp->f_flag & FWRITE)
930 if (fp->f_flag & FAPPEND)
932 #ifdef FSHLOCK /* currently gone */
933 if (fp->f_flag & FSHLOCK)
935 if (fp->f_flag & FEXLOCK)
938 if (fp->f_flag & FASYNC)
941 (void)printf("%6s %3d", flagbuf, fp->f_count);
942 (void)printf(" %3d", fp->f_msgcount);
943 (void)printf(" %8lx", (u_long)(void *)fp->f_data);
944 if (fp->f_offset < 0)
945 (void)printf(" %qx\n", fp->f_offset);
947 (void)printf(" %qd\n", fp->f_offset);
963 * Add emulation of KINFO_FILE here.
966 errx(1, "files on dead kernel, not implemented");
970 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
971 warn("sysctl: KERN_FILE");
974 if ((buf = malloc(len)) == NULL)
976 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
977 warn("sysctl: KERN_FILE");
986 * swapmode is based on a program called swapinfo written
987 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
992 struct kvm_swap kswap[16];
995 int pagesize = getpagesize();
1000 n = kvm_getswapinfo(
1003 sizeof(kswap)/sizeof(kswap[0]),
1004 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
1007 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize))
1009 header = getbsize(&hlen, &blocksize);
1010 if (totalflag == 0) {
1011 (void)printf("%-15s %*s %8s %8s %8s %s\n",
1012 "Device", hlen, header,
1013 "Used", "Avail", "Capacity", "Type");
1015 for (i = 0; i < n; ++i) {
1018 kswap[i].ksw_devname,
1020 CONVERT(kswap[i].ksw_total)
1023 "%8d %8d %5.0f%% %s\n",
1024 CONVERT(kswap[i].ksw_used),
1025 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
1026 (double)kswap[i].ksw_used * 100.0 /
1027 (double)kswap[i].ksw_total,
1028 (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
1029 "Sequential" : "Interleaved"
1035 blocksize = 1024 * 1024;
1038 "%dM/%dM swap space\n",
1039 CONVERT(kswap[n].ksw_used),
1040 CONVERT(kswap[n].ksw_total)
1044 "%-15s %*d %8d %8d %5.0f%%\n",
1047 CONVERT(kswap[n].ksw_total),
1048 CONVERT(kswap[n].ksw_used),
1049 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
1050 (double)kswap[n].ksw_used * 100.0 /
1051 (double)kswap[n].ksw_total