pstat.c (fdaad796ed2738847aaf2bdc2bfbbfe427e353e1) pstat.c (d9961cfdb97c3a6095df8fee9c9ef9f0b490fa7a)
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

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
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

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
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
36"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
41static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
42static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
43#endif
44static const char rcsid[] =
45 "$Id$";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/vnode.h>
47#include <sys/ucred.h>
48#define KERNEL
49#include <sys/file.h>

--- 107 unchanged lines hidden (view full) ---

157};
158
159int usenumflag;
160int totalflag;
161char *nlistf = NULL;
162char *memf = NULL;
163kvm_t *kd;
164
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/time.h>
50#include <sys/vnode.h>
51#include <sys/ucred.h>
52#define KERNEL
53#include <sys/file.h>

--- 107 unchanged lines hidden (view full) ---

161};
162
163int usenumflag;
164int totalflag;
165char *nlistf = NULL;
166char *memf = NULL;
167kvm_t *kd;
168
165char *usage;
169char *usagestr;
166
167struct {
168 int m_flag;
169 const char *m_name;
170} mnt_flags[] = {
171 { MNT_RDONLY, "rdonly" },
172 { MNT_SYNCHRONOUS, "sync" },
173 { MNT_NOEXEC, "noexec" },

--- 52 unchanged lines hidden (view full) ---

226void swapmode __P((void));
227void ttymode __P((void));
228void ttyprt __P((struct tty *, int));
229void ttytype __P((struct tty *, char *, int, int, int));
230void ufs_header __P((void));
231int ufs_print __P((struct vnode *));
232void union_header __P((void));
233int union_print __P((struct vnode *));
170
171struct {
172 int m_flag;
173 const char *m_name;
174} mnt_flags[] = {
175 { MNT_RDONLY, "rdonly" },
176 { MNT_SYNCHRONOUS, "sync" },
177 { MNT_NOEXEC, "noexec" },

--- 52 unchanged lines hidden (view full) ---

230void swapmode __P((void));
231void ttymode __P((void));
232void ttyprt __P((struct tty *, int));
233void ttytype __P((struct tty *, char *, int, int, int));
234void ufs_header __P((void));
235int ufs_print __P((struct vnode *));
236void union_header __P((void));
237int union_print __P((struct vnode *));
238static void usage __P((void));
234void vnode_header __P((void));
235void vnode_print __P((struct vnode *, struct vnode *));
236void vnodemode __P((void));
237
238int
239main(argc, argv)
240 int argc;
241 char *argv[];
242{
239void vnode_header __P((void));
240void vnode_print __P((struct vnode *, struct vnode *));
241void vnodemode __P((void));
242
243int
244main(argc, argv)
245 int argc;
246 char *argv[];
247{
243 extern char *optarg;
244 extern int optind;
245 int ch, i, quit, ret;
246 int fileflag, swapflag, ttyflag, vnodeflag;
247 char buf[_POSIX2_LINE_MAX],*opts;
248
249 fileflag = swapflag = ttyflag = vnodeflag = 0;
250
251 /* We will behave like good old swapinfo if thus invoked */
252 opts = strrchr(argv[0],'/');
253 if (opts)
254 opts++;
255 else
256 opts = argv[0];
257 if (!strcmp(opts,"swapinfo")) {
258 swapflag = 1;
259 opts = "kM:N:";
248 int ch, i, quit, ret;
249 int fileflag, swapflag, ttyflag, vnodeflag;
250 char buf[_POSIX2_LINE_MAX],*opts;
251
252 fileflag = swapflag = ttyflag = vnodeflag = 0;
253
254 /* We will behave like good old swapinfo if thus invoked */
255 opts = strrchr(argv[0],'/');
256 if (opts)
257 opts++;
258 else
259 opts = argv[0];
260 if (!strcmp(opts,"swapinfo")) {
261 swapflag = 1;
262 opts = "kM:N:";
260 usage = "usage: swapinfo [-k] [-M core] [-N system]\n";
263 usagestr = "swapinfo [-k] [-M core] [-N system]";
261 } else {
262 opts = "TM:N:fiknstv";
264 } else {
265 opts = "TM:N:fiknstv";
263 usage = "usage: pstat [-Tfknstv] [-M core] [-N system]\n";
266 usagestr = "pstat [-Tfknstv] [-M core] [-N system]";
264 }
265
266 while ((ch = getopt(argc, argv, opts)) != -1)
267 switch (ch) {
268 case 'f':
269 fileflag = 1;
270 break;
271 case 'k':

--- 17 unchanged lines hidden (view full) ---

289 case 't':
290 ttyflag = 1;
291 break;
292 case 'v':
293 case 'i': /* Backward compatibility. */
294 vnodeflag = 1;
295 break;
296 default:
267 }
268
269 while ((ch = getopt(argc, argv, opts)) != -1)
270 switch (ch) {
271 case 'f':
272 fileflag = 1;
273 break;
274 case 'k':

--- 17 unchanged lines hidden (view full) ---

292 case 't':
293 ttyflag = 1;
294 break;
295 case 'v':
296 case 'i': /* Backward compatibility. */
297 vnodeflag = 1;
298 break;
299 default:
297 (void)fprintf(stderr, usage);
298 exit(1);
300 usage();
299 }
300 argc -= optind;
301 argv += optind;
302
303 /*
304 * Discard setgid privileges if not the running kernel so that bad
305 * guys can't print interesting stuff from kernel memory.
306 */
307 if (nlistf != NULL || memf != NULL)
308 (void)setgid(getgid());
309
310 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
311 errx(1, "kvm_openfiles: %s", buf);
312 if ((ret = kvm_nlist(kd, nl)) != 0) {
313 if (ret == -1)
314 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
315 for (i = quit = 0; i <= NLMANDATORY; i++)
316 if (!nl[i].n_value) {
317 quit = 1;
301 }
302 argc -= optind;
303 argv += optind;
304
305 /*
306 * Discard setgid privileges if not the running kernel so that bad
307 * guys can't print interesting stuff from kernel memory.
308 */
309 if (nlistf != NULL || memf != NULL)
310 (void)setgid(getgid());
311
312 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
313 errx(1, "kvm_openfiles: %s", buf);
314 if ((ret = kvm_nlist(kd, nl)) != 0) {
315 if (ret == -1)
316 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
317 for (i = quit = 0; i <= NLMANDATORY; i++)
318 if (!nl[i].n_value) {
319 quit = 1;
318 warnx("undefined symbol: %s\n", nl[i].n_name);
320 warnx("undefined symbol: %s", nl[i].n_name);
319 }
320 if (quit)
321 exit(1);
322 }
321 }
322 if (quit)
323 exit(1);
324 }
323 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) {
324 (void)fprintf(stderr, usage);
325 exit(1);
326 }
325 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
326 usage();
327 if (fileflag || totalflag)
328 filemode();
329 if (vnodeflag || totalflag)
330 vnodemode();
331 if (ttyflag)
332 ttymode();
333 if (swapflag || totalflag)
334 swapmode();
335 exit (0);
336}
337
327 if (fileflag || totalflag)
328 filemode();
329 if (vnodeflag || totalflag)
330 vnodemode();
331 if (ttyflag)
332 ttymode();
333 if (swapflag || totalflag)
334 swapmode();
335 exit (0);
336}
337
338static void
339usage()
340{
341 fprintf(stderr, "usage: %s\n", usagestr);
342 exit (1);
343}
344
338struct e_vnode {
339 struct vnode *avnode;
340 struct vnode vnode;
341};
342
343void
344vnodemode()
345{

--- 252 unchanged lines hidden (view full) ---

598 struct mount mount;
599 } *mhead = NULL;
600 struct mtab *mt;
601
602 for (mt = mhead; mt != NULL; mt = mt->next)
603 if (maddr == mt->maddr)
604 return (&mt->mount);
605 if ((mt = malloc(sizeof(struct mtab))) == NULL)
345struct e_vnode {
346 struct vnode *avnode;
347 struct vnode vnode;
348};
349
350void
351vnodemode()
352{

--- 252 unchanged lines hidden (view full) ---

605 struct mount mount;
606 } *mhead = NULL;
607 struct mtab *mt;
608
609 for (mt = mhead; mt != NULL; mt = mt->next)
610 if (maddr == mt->maddr)
611 return (&mt->mount);
612 if ((mt = malloc(sizeof(struct mtab))) == NULL)
606 err(1, NULL);
613 errx(1, "malloc");
607 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
608 mt->maddr = maddr;
609 mt->next = mhead;
610 mhead = mt;
611 return (&mt->mount);
612}
613
614void
615mount_print(mp)
616 struct mount *mp;
617{
618 int flags;
619 const char *type;
620
621#define ST mp->mnt_stat
622 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
623 ST.f_mntfromname, ST.f_mntonname);
614 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
615 mt->maddr = maddr;
616 mt->next = mhead;
617 mhead = mt;
618 return (&mt->mount);
619}
620
621void
622mount_print(mp)
623 struct mount *mp;
624{
625 int flags;
626 const char *type;
627
628#define ST mp->mnt_stat
629 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
630 ST.f_mntfromname, ST.f_mntonname);
624 if (flags = mp->mnt_flag) {
631 if ((flags = mp->mnt_flag)) {
625 int i;
626 const char *sep = " (";
627
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;
632 sep = ",";

--- 21 unchanged lines hidden (view full) ---

654 */
655 return (kinfo_vnodes(avnodes));
656 }
657 mib[0] = CTL_KERN;
658 mib[1] = KERN_VNODE;
659 if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
660 err(1, "sysctl: KERN_VNODE");
661 if ((vnodebase = malloc(copysize)) == NULL)
632 int i;
633 const char *sep = " (";
634
635 for (i = 0; mnt_flags[i].m_flag; i++) {
636 if (flags & mnt_flags[i].m_flag) {
637 (void)printf("%s%s", sep, mnt_flags[i].m_name);
638 flags &= ~mnt_flags[i].m_flag;
639 sep = ",";

--- 21 unchanged lines hidden (view full) ---

661 */
662 return (kinfo_vnodes(avnodes));
663 }
664 mib[0] = CTL_KERN;
665 mib[1] = KERN_VNODE;
666 if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
667 err(1, "sysctl: KERN_VNODE");
668 if ((vnodebase = malloc(copysize)) == NULL)
662 err(1, NULL);
669 errx(1, "malloc");
663 if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
664 err(1, "sysctl: KERN_VNODE");
665 if (copysize % sizeof(struct e_vnode))
666 errx(1, "vnode size mismatch");
667 *avnodes = copysize / sizeof(struct e_vnode);
668
669 return (vnodebase);
670}

--- 11 unchanged lines hidden (view full) ---

682 char *vbuf, *evbuf, *bp;
683 int num, numvnodes;
684
685#define VPTRSZ sizeof(struct vnode *)
686#define VNODESZ sizeof(struct vnode)
687
688 KGET(V_NUMV, numvnodes);
689 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
670 if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
671 err(1, "sysctl: KERN_VNODE");
672 if (copysize % sizeof(struct e_vnode))
673 errx(1, "vnode size mismatch");
674 *avnodes = copysize / sizeof(struct e_vnode);
675
676 return (vnodebase);
677}

--- 11 unchanged lines hidden (view full) ---

689 char *vbuf, *evbuf, *bp;
690 int num, numvnodes;
691
692#define VPTRSZ sizeof(struct vnode *)
693#define VNODESZ sizeof(struct vnode)
694
695 KGET(V_NUMV, numvnodes);
696 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
690 err(1, NULL);
697 errx(1, "malloc");
691 bp = vbuf;
692 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
693 KGET(V_MOUNTLIST, mountlist);
694 for (num = 0, mp = mountlist.cqh_first; ; mp = mp_next) {
695 KGET2(mp, &mount, sizeof(mount), "mount entry");
696 mp_next = mount.mnt_list.cqe_next;
697 for (vp = mount.mnt_vnodelist.lh_first;
698 vp != NULL; vp = vp_next) {

--- 19 unchanged lines hidden (view full) ---

718int ttyspace = 128;
719
720void
721ttymode()
722{
723 struct tty *tty;
724
725 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
698 bp = vbuf;
699 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
700 KGET(V_MOUNTLIST, mountlist);
701 for (num = 0, mp = mountlist.cqh_first; ; mp = mp_next) {
702 KGET2(mp, &mount, sizeof(mount), "mount entry");
703 mp_next = mount.mnt_list.cqe_next;
704 for (vp = mount.mnt_vnodelist.lh_first;
705 vp != NULL; vp = vp_next) {

--- 19 unchanged lines hidden (view full) ---

725int ttyspace = 128;
726
727void
728ttymode()
729{
730 struct tty *tty;
731
732 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
726 err(1, NULL);
733 errx(1, "malloc");
727#if !defined(hp300) && !defined(mips)
728 if (nl[SCONS].n_type != 0) {
729 (void)printf("1 console\n");
730 KGET(SCONS, *tty);
731 (void)printf(hdr);
732 ttyprt(&tty[0], 0);
733 }
734#endif

--- 59 unchanged lines hidden (view full) ---

794
795 if (tty == NULL)
796 return;
797 KGET(number, ntty);
798 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
799 if (ntty > ttyspace) {
800 ttyspace = ntty;
801 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
734#if !defined(hp300) && !defined(mips)
735 if (nl[SCONS].n_type != 0) {
736 (void)printf("1 console\n");
737 KGET(SCONS, *tty);
738 (void)printf(hdr);
739 ttyprt(&tty[0], 0);
740 }
741#endif

--- 59 unchanged lines hidden (view full) ---

801
802 if (tty == NULL)
803 return;
804 KGET(number, ntty);
805 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
806 if (ntty > ttyspace) {
807 ttyspace = ntty;
808 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
802 err(1, NULL);
809 errx(1, "realloc");
803 }
804 if (indir) {
805 KGET(type, ttyaddr);
806 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs");
807 } else {
808 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs");
809 }
810 (void)printf(hdr);

--- 174 unchanged lines hidden (view full) ---

985 int mib[2];
986 char *buf;
987
988 /*
989 * XXX
990 * Add emulation of KINFO_FILE here.
991 */
992 if (memf != NULL)
810 }
811 if (indir) {
812 KGET(type, ttyaddr);
813 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs");
814 } else {
815 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs");
816 }
817 (void)printf(hdr);

--- 174 unchanged lines hidden (view full) ---

992 int mib[2];
993 char *buf;
994
995 /*
996 * XXX
997 * Add emulation of KINFO_FILE here.
998 */
999 if (memf != NULL)
993 errx(1, "files on dead kernel, not implemented\n");
1000 errx(1, "files on dead kernel, not implemented");
994
995 mib[0] = CTL_KERN;
996 mib[1] = KERN_FILE;
997 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
998 warn("sysctl: KERN_FILE");
999 return (-1);
1000 }
1001 if ((buf = malloc(len)) == NULL)
1001
1002 mib[0] = CTL_KERN;
1003 mib[1] = KERN_FILE;
1004 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
1005 warn("sysctl: KERN_FILE");
1006 return (-1);
1007 }
1008 if ((buf = malloc(len)) == NULL)
1002 err(1, NULL);
1009 errx(1, "malloc");
1003 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
1004 warn("sysctl: KERN_FILE");
1005 return (-1);
1006 }
1007 *abuf = buf;
1008 *alen = len;
1009 return (0);
1010}

--- 16 unchanged lines hidden (view full) ---

1027 u_long ptr;
1028
1029 KGET(VM_NSWAP, nswap);
1030 KGET(VM_NSWDEV, nswdev);
1031 KGET(VM_DMMAX, dmmax);
1032 KGET1(VM_SWAPLIST, &swaplist, sizeof swaplist, "swaplist");
1033 if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
1034 (perdev = malloc(nswdev * sizeof(*perdev))) == NULL)
1010 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
1011 warn("sysctl: KERN_FILE");
1012 return (-1);
1013 }
1014 *abuf = buf;
1015 *alen = len;
1016 return (0);
1017}

--- 16 unchanged lines hidden (view full) ---

1034 u_long ptr;
1035
1036 KGET(VM_NSWAP, nswap);
1037 KGET(VM_NSWDEV, nswdev);
1038 KGET(VM_DMMAX, dmmax);
1039 KGET1(VM_SWAPLIST, &swaplist, sizeof swaplist, "swaplist");
1040 if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
1041 (perdev = malloc(nswdev * sizeof(*perdev))) == NULL)
1035 err(1, "malloc");
1042 errx(1, "malloc");
1036 KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt");
1037 KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt");
1038
1039 /* Count up swap space. */
1040 nfree = 0;
1041 memset(perdev, 0, nswdev * sizeof(*perdev));
1042 swapptr = swaplist.rlh_list;
1043 while (swapptr) {

--- 94 unchanged lines hidden ---
1043 KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt");
1044 KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt");
1045
1046 /* Count up swap space. */
1047 nfree = 0;
1048 memset(perdev, 0, nswdev * sizeof(*perdev));
1049 swapptr = swaplist.rlh_list;
1050 while (swapptr) {

--- 94 unchanged lines hidden ---