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