1 /*- 2 * Copyright (c) 1980, 1992, 1993 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 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1980, 1992, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/time.h> 50 #include <sys/sysctl.h> 51 52 #include <err.h> 53 #include <locale.h> 54 #include <nlist.h> 55 #include <paths.h> 56 #include <signal.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <unistd.h> 60 #include "systat.h" 61 #include "extern.h" 62 63 static int dellave; 64 65 kvm_t *kd; 66 sig_t sigtstpdfl; 67 double avenrun[3]; 68 int col; 69 int naptime = 5; 70 int verbose = 1; /* to report kvm read errs */ 71 struct clockinfo clkinfo; 72 double hertz; 73 char c; 74 char *namp; 75 char hostname[MAXHOSTNAMELEN]; 76 WINDOW *wnd; 77 int CMDLINE; 78 int use_kvm = 1; 79 80 static WINDOW *wload; /* one line window for load average */ 81 82 int 83 main(argc, argv) 84 int argc; 85 char **argv; 86 { 87 char errbuf[80], dummy; 88 size_t size; 89 int err; 90 91 (void) setlocale(LC_TIME, ""); 92 93 argc--, argv++; 94 while (argc > 0) { 95 if (argv[0][0] == '-') { 96 struct cmdtab *p; 97 98 p = lookup(&argv[0][1]); 99 if (p == (struct cmdtab *)-1) 100 errx(1, "%s: ambiguous request", &argv[0][1]); 101 if (p == (struct cmdtab *)0) 102 errx(1, "%s: unknown request", &argv[0][1]); 103 curcmd = p; 104 } else { 105 naptime = atoi(argv[0]); 106 if (naptime <= 0) 107 naptime = 5; 108 } 109 argc--, argv++; 110 } 111 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 112 if (kd != NULL) { 113 /* 114 * Try to actually read something, we may be in a jail, and 115 * have /dev/null opened as /dev/mem. 116 */ 117 if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 || 118 kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) != 119 sizeof(dummy)) { 120 kvm_close(kd); 121 kd = NULL; 122 } 123 } 124 if (kd == NULL) { 125 /* 126 * Maybe we are lacking permissions? Retry, this time with bogus 127 * devices. We can now use sysctl only. 128 */ 129 use_kvm = 0; 130 kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null", O_RDONLY, errbuf); 131 if (kd == NULL) { 132 error("%s", errbuf); 133 exit(1); 134 } 135 } 136 signal(SIGINT, die); 137 signal(SIGQUIT, die); 138 signal(SIGTERM, die); 139 140 /* 141 * Initialize display. Load average appears in a one line 142 * window of its own. Current command's display appears in 143 * an overlapping sub-window of stdscr configured by the display 144 * routines to minimize update work by curses. 145 */ 146 initscr(); 147 CMDLINE = LINES - 1; 148 wnd = (*curcmd->c_open)(); 149 if (wnd == NULL) { 150 warnx("couldn't initialize display"); 151 die(0); 152 } 153 wload = newwin(1, 0, 3, 20); 154 if (wload == NULL) { 155 warnx("couldn't set up load average window"); 156 die(0); 157 } 158 gethostname(hostname, sizeof (hostname)); 159 size = sizeof(clkinfo); 160 err = sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0); 161 if (err != 0 || size != sizeof(clkinfo)) { 162 error("kern.clockrate"); 163 die(0); 164 } 165 hertz = clkinfo.stathz; 166 (*curcmd->c_init)(); 167 curcmd->c_flags |= CF_INIT; 168 labels(); 169 170 dellave = 0.0; 171 172 signal(SIGALRM, display); 173 display(0); 174 noecho(); 175 crmode(); 176 keyboard(); 177 /*NOTREACHED*/ 178 179 return EXIT_SUCCESS; 180 } 181 182 void 183 labels() 184 { 185 if (curcmd->c_flags & CF_LOADAV) { 186 mvaddstr(2, 20, 187 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 188 mvaddstr(3, 5, "Load Average"); 189 } 190 (*curcmd->c_label)(); 191 #ifdef notdef 192 mvprintw(21, 25, "CPU usage on %s", hostname); 193 #endif 194 refresh(); 195 } 196 197 void 198 display(signo) 199 int signo; 200 { 201 register int i, j; 202 203 /* Get the load average over the last minute. */ 204 (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); 205 (*curcmd->c_fetch)(); 206 if (curcmd->c_flags & CF_LOADAV) { 207 j = 5.0*avenrun[0] + 0.5; 208 dellave -= avenrun[0]; 209 if (dellave >= 0.0) 210 c = '<'; 211 else { 212 c = '>'; 213 dellave = -dellave; 214 } 215 if (dellave < 0.1) 216 c = '|'; 217 dellave = avenrun[0]; 218 wmove(wload, 0, 0); wclrtoeol(wload); 219 for (i = (j > 50) ? 50 : j; i > 0; i--) 220 waddch(wload, c); 221 if (j > 50) 222 wprintw(wload, " %4.1f", avenrun[0]); 223 } 224 (*curcmd->c_refresh)(); 225 if (curcmd->c_flags & CF_LOADAV) 226 wrefresh(wload); 227 wrefresh(wnd); 228 move(CMDLINE, col); 229 refresh(); 230 alarm(naptime); 231 } 232 233 void 234 load() 235 { 236 237 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 238 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 239 avenrun[0], avenrun[1], avenrun[2]); 240 clrtoeol(); 241 } 242 243 void 244 die(signo) 245 int signo; 246 { 247 move(CMDLINE, 0); 248 clrtoeol(); 249 refresh(); 250 endwin(); 251 exit(0); 252 } 253 254 #if __STDC__ 255 #include <stdarg.h> 256 #else 257 #include <varargs.h> 258 #endif 259 260 #if __STDC__ 261 void 262 error(const char *fmt, ...) 263 #else 264 void 265 error(fmt, va_alist) 266 char *fmt; 267 va_dcl 268 #endif 269 { 270 va_list ap; 271 char buf[255]; 272 int oy, ox; 273 #if __STDC__ 274 va_start(ap, fmt); 275 #else 276 va_start(ap); 277 #endif 278 279 if (wnd) { 280 getyx(stdscr, oy, ox); 281 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 282 clrtoeol(); 283 standout(); 284 mvaddstr(CMDLINE, 0, buf); 285 standend(); 286 move(oy, ox); 287 refresh(); 288 } else { 289 (void) vfprintf(stderr, fmt, ap); 290 fprintf(stderr, "\n"); 291 } 292 va_end(ap); 293 } 294 295 void 296 nlisterr(namelist) 297 struct nlist namelist[]; 298 { 299 int i, n; 300 301 n = 0; 302 clear(); 303 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 304 for (i = 0; 305 namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++) 306 if (namelist[i].n_value == 0) 307 mvprintw(2 + ++n, 10, "%s", namelist[i].n_name); 308 move(CMDLINE, 0); 309 clrtoeol(); 310 refresh(); 311 endwin(); 312 exit(1); 313 } 314