xref: /openbsd/bin/ps/nlist.c (revision 404b540a)
1 /*	$OpenBSD: nlist.c,v 1.16 2008/02/10 16:56:13 kettenis 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 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)nlist.c	8.4 (Berkeley) 4/2/94";
36 #else
37 static char rcsid[] = "$OpenBSD: nlist.c,v 1.16 2008/02/10 16:56:13 kettenis Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/proc.h>
44 #include <sys/resource.h>
45 #include <sys/sysctl.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <kvm.h>
50 #include <nlist.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include "ps.h"
56 
57 struct	nlist psnl[] = {
58 	{"_fscale"},
59 #define	X_FSCALE	0
60 	{"_ccpu"},
61 #define	X_CCPU		1
62 	{"_physmem"},
63 #define	X_PHYSMEM	2
64 	{"_maxslp"},
65 #define X_MAXSLP	3
66 	{NULL}
67 };
68 
69 fixpt_t	ccpu;				/* kernel _ccpu variable */
70 int	nlistread;			/* if nlist already read. */
71 u_int	mempages;			/* number of pages of phys. memory */
72 int	fscale;				/* kernel _fscale variable */
73 int	maxslp;
74 
75 extern kvm_t *kd;
76 extern int kvm_sysctl_only;
77 
78 #define kread(x, v) \
79 	kvm_read(kd, psnl[x].n_value, &v, sizeof v) != sizeof(v)
80 
81 int
82 donlist(void)
83 {
84 	int64_t physmem;
85 	int rval, mib[2];
86 	size_t siz;
87 
88 	rval = 0;
89 	nlistread = 1;
90 
91 	if (kd != NULL && !kvm_sysctl_only) {
92 		if (kvm_nlist(kd, psnl)) {
93 			nlisterr(psnl);
94 			eval = 1;
95 			return (1);
96 		}
97 		if (kread(X_FSCALE, fscale)) {
98 			warnx("fscale: %s", kvm_geterr(kd));
99 			eval = rval = 1;
100 		}
101 		if (kread(X_PHYSMEM, mempages)) {
102 			warnx("physmem: %s", kvm_geterr(kd));
103 			eval = rval = 1;
104 		}
105 		if (kread(X_CCPU, ccpu)) {
106 			warnx("ccpu: %s", kvm_geterr(kd));
107 			eval = rval = 1;
108 		}
109 		if (kread(X_MAXSLP, maxslp)) {
110 			warnx("maxslp: %s", kvm_geterr(kd));
111 			eval = rval = 1;
112 		}
113 	} else {
114 		siz = sizeof (fscale);
115 		mib[0] = CTL_KERN;
116 		mib[1] = KERN_FSCALE;
117 		if (sysctl(mib, 2, &fscale, &siz, NULL, 0) < 0) {
118 			warnx("fscale: failed to get kern.fscale");
119 			eval = rval = 1;
120 		}
121 		siz = sizeof (physmem);
122 		mib[0] = CTL_HW;
123 		mib[1] = HW_PHYSMEM64;
124 		if (sysctl(mib, 2, &physmem, &siz, NULL, 0) < 0) {
125 			warnx("physmem: failed to get hw.physmem");
126 			eval = rval = 1;
127 		}
128 		/* translate bytes into page count */
129 		mempages = physmem / getpagesize();
130 		siz = sizeof (ccpu);
131 		mib[0] = CTL_KERN;
132 		mib[1] = KERN_CCPU;
133 		if (sysctl(mib, 2, &ccpu, &siz, NULL, 0) < 0) {
134 			warnx("ccpu: failed to get kern.ccpu");
135 			eval = rval = 1;
136 		}
137 		siz = sizeof (maxslp);
138 		mib[0] = CTL_VM;
139 		mib[1] = VM_MAXSLP;
140 		if (sysctl(mib, 2, &maxslp, &siz, NULL, 0) < 0) {
141 			warnx("maxslp: failed to get vm.maxslp");
142 			eval = rval = 1;
143 		}
144 	}
145 	return (rval);
146 }
147 
148 void
149 nlisterr(struct nlist nl[])
150 {
151 	int i;
152 
153 	(void)fprintf(stderr, "ps: nlist: can't find following symbols:");
154 	for (i = 0; nl[i].n_name != NULL; i++)
155 		if (nl[i].n_value == 0)
156 			(void)fprintf(stderr, " %s", nl[i].n_name);
157 	(void)fprintf(stderr, "\n");
158 }
159