xref: /openbsd/bin/ps/nlist.c (revision 898184e3)
1 /*	$OpenBSD: nlist.c,v 1.18 2011/12/11 00:16:49 nicm Exp $	*/
2 /*	$NetBSD: nlist.c,v 1.11 1995/03/21 09:08:03 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1990, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/proc.h>
36 #include <sys/resource.h>
37 #include <sys/sysctl.h>
38 
39 #include <err.h>
40 #include <errno.h>
41 #include <kvm.h>
42 #include <nlist.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "ps.h"
48 
49 struct	nlist psnl[] = {
50 	{"_fscale"},
51 #define	X_FSCALE	0
52 	{"_ccpu"},
53 #define	X_CCPU		1
54 	{"_physmem"},
55 #define	X_PHYSMEM	2
56 	{"_maxslp"},
57 #define X_MAXSLP	3
58 	{NULL}
59 };
60 
61 fixpt_t	ccpu;				/* kernel _ccpu variable */
62 int	nlistread;			/* if nlist already read. */
63 u_int	mempages;			/* number of pages of phys. memory */
64 int	fscale;				/* kernel _fscale variable */
65 int	maxslp;
66 
67 extern kvm_t *kd;
68 
69 #define kread(x, v) \
70 	kvm_read(kd, psnl[x].n_value, &v, sizeof v) != sizeof(v)
71 
72 int
73 donlist(void)
74 {
75 	int64_t physmem;
76 	int rval, mib[2];
77 	size_t siz;
78 
79 	rval = 0;
80 	nlistread = 1;
81 
82 	if (kd != NULL && !kvm_sysctl_only) {
83 		if (kvm_nlist(kd, psnl)) {
84 			nlisterr(psnl);
85 			eval = 1;
86 			return (1);
87 		}
88 		if (kread(X_FSCALE, fscale)) {
89 			warnx("fscale: %s", kvm_geterr(kd));
90 			eval = rval = 1;
91 		}
92 		if (kread(X_PHYSMEM, mempages)) {
93 			warnx("physmem: %s", kvm_geterr(kd));
94 			eval = rval = 1;
95 		}
96 		if (kread(X_CCPU, ccpu)) {
97 			warnx("ccpu: %s", kvm_geterr(kd));
98 			eval = rval = 1;
99 		}
100 		if (kread(X_MAXSLP, maxslp)) {
101 			warnx("maxslp: %s", kvm_geterr(kd));
102 			eval = rval = 1;
103 		}
104 	} else {
105 		siz = sizeof (fscale);
106 		mib[0] = CTL_KERN;
107 		mib[1] = KERN_FSCALE;
108 		if (sysctl(mib, 2, &fscale, &siz, NULL, 0) < 0) {
109 			warnx("fscale: failed to get kern.fscale");
110 			eval = rval = 1;
111 		}
112 		siz = sizeof (physmem);
113 		mib[0] = CTL_HW;
114 		mib[1] = HW_PHYSMEM64;
115 		if (sysctl(mib, 2, &physmem, &siz, NULL, 0) < 0) {
116 			warnx("physmem: failed to get hw.physmem");
117 			eval = rval = 1;
118 		}
119 		/* translate bytes into page count */
120 		mempages = physmem / getpagesize();
121 		siz = sizeof (ccpu);
122 		mib[0] = CTL_KERN;
123 		mib[1] = KERN_CCPU;
124 		if (sysctl(mib, 2, &ccpu, &siz, NULL, 0) < 0) {
125 			warnx("ccpu: failed to get kern.ccpu");
126 			eval = rval = 1;
127 		}
128 		siz = sizeof (maxslp);
129 		mib[0] = CTL_VM;
130 		mib[1] = VM_MAXSLP;
131 		if (sysctl(mib, 2, &maxslp, &siz, NULL, 0) < 0) {
132 			warnx("maxslp: failed to get vm.maxslp");
133 			eval = rval = 1;
134 		}
135 	}
136 	return (rval);
137 }
138 
139 void
140 nlisterr(struct nlist nl[])
141 {
142 	int i;
143 
144 	(void)fprintf(stderr, "ps: nlist: can't find following symbols:");
145 	for (i = 0; nl[i].n_name != NULL; i++)
146 		if (nl[i].n_value == 0)
147 			(void)fprintf(stderr, " %s", nl[i].n_name);
148 	(void)fprintf(stderr, "\n");
149 }
150