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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 32 __FBSDID("$FreeBSD$"); 33 34 #ifdef lint 35 static const char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 36 #endif 37 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1980, 1992, 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/sysctl.h> 47 #include <sys/queue.h> 48 49 #include <err.h> 50 #include <limits.h> 51 #include <locale.h> 52 #include <nlist.h> 53 #include <paths.h> 54 #include <signal.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 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 unsigned int delay = 5000000; /* in microseconds */ 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 struct cmdentry { 83 SLIST_ENTRY(cmdentry) link; 84 char *cmd; /* Command name */ 85 char *argv; /* Arguments vector for a command */ 86 }; 87 SLIST_HEAD(, cmdentry) commands; 88 89 static void 90 parse_cmd_args (int argc, char **argv) 91 { 92 int in_command = 0; 93 struct cmdentry *cmd = NULL; 94 double t; 95 96 while (argc) { 97 if (argv[0][0] == '-') { 98 if (in_command) 99 SLIST_INSERT_HEAD(&commands, cmd, link); 100 101 if (memcmp(argv[0], "--", 3) == 0) { 102 in_command = 0; /*-- ends a command explicitly*/ 103 argc --, argv ++; 104 continue; 105 } 106 cmd = calloc(1, sizeof(struct cmdentry)); 107 if (cmd == NULL) 108 errx(1, "memory allocating failure"); 109 cmd->cmd = strdup(&argv[0][1]); 110 if (cmd->cmd == NULL) 111 errx(1, "memory allocating failure"); 112 in_command = 1; 113 } 114 else if (!in_command) { 115 t = strtod(argv[0], NULL) * 1000000.0; 116 if (t > 0 && t < (double)UINT_MAX) 117 delay = (unsigned int)t; 118 } 119 else if (cmd != NULL) { 120 cmd->argv = strdup(argv[0]); 121 if (cmd->argv == NULL) 122 errx(1, "memory allocating failure"); 123 in_command = 0; 124 SLIST_INSERT_HEAD(&commands, cmd, link); 125 } 126 else 127 errx(1, "invalid arguments list"); 128 129 argc--, argv++; 130 } 131 if (in_command && cmd != NULL) 132 SLIST_INSERT_HEAD(&commands, cmd, link); 133 134 } 135 136 int 137 main(int argc, char **argv) 138 { 139 char errbuf[_POSIX2_LINE_MAX], dummy; 140 size_t size; 141 double t; 142 struct cmdentry *cmd = NULL; 143 144 (void) setlocale(LC_ALL, ""); 145 146 SLIST_INIT(&commands); 147 argc--, argv++; 148 if (argc > 0) { 149 if (argv[0][0] == '-') { 150 struct cmdtab *p; 151 152 p = lookup(&argv[0][1]); 153 if (p == (struct cmdtab *)-1) 154 errx(1, "%s: ambiguous request", &argv[0][1]); 155 if (p == (struct cmdtab *)0) 156 errx(1, "%s: unknown request", &argv[0][1]); 157 curcmd = p; 158 argc--, argv++; 159 } 160 parse_cmd_args (argc, argv); 161 162 } 163 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 164 if (kd != NULL) { 165 /* 166 * Try to actually read something, we may be in a jail, and 167 * have /dev/null opened as /dev/mem. 168 */ 169 if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 || 170 kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) != 171 sizeof(dummy)) { 172 kvm_close(kd); 173 kd = NULL; 174 } 175 } 176 if (kd == NULL) { 177 /* 178 * Maybe we are lacking permissions? Retry, this time with bogus 179 * devices. We can now use sysctl only. 180 */ 181 use_kvm = 0; 182 kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null", 183 O_RDONLY, errbuf); 184 if (kd == NULL) { 185 error("%s", errbuf); 186 exit(1); 187 } 188 } 189 signal(SIGHUP, die); 190 signal(SIGINT, die); 191 signal(SIGQUIT, die); 192 signal(SIGTERM, die); 193 194 /* 195 * Initialize display. Load average appears in a one line 196 * window of its own. Current command's display appears in 197 * an overlapping sub-window of stdscr configured by the display 198 * routines to minimize update work by curses. 199 */ 200 initscr(); 201 CMDLINE = LINES - 1; 202 wnd = (*curcmd->c_open)(); 203 if (wnd == NULL) { 204 warnx("couldn't initialize display"); 205 die(0); 206 } 207 wload = newwin(1, 0, 1, 20); 208 if (wload == NULL) { 209 warnx("couldn't set up load average window"); 210 die(0); 211 } 212 gethostname(hostname, sizeof (hostname)); 213 size = sizeof(clkinfo); 214 if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0) 215 || size != sizeof(clkinfo)) { 216 error("kern.clockrate"); 217 die(0); 218 } 219 hertz = clkinfo.stathz; 220 (*curcmd->c_init)(); 221 curcmd->c_flags |= CF_INIT; 222 labels(); 223 224 if (curcmd->c_cmd != NULL) 225 SLIST_FOREACH (cmd, &commands, link) 226 if (!curcmd->c_cmd(cmd->cmd, cmd->argv)) 227 warnx("command is not understood"); 228 229 dellave = 0.0; 230 display(); 231 noecho(); 232 crmode(); 233 keyboard(); 234 /*NOTREACHED*/ 235 236 return EXIT_SUCCESS; 237 } 238 239 void 240 labels(void) 241 { 242 if (curcmd->c_flags & CF_LOADAV) { 243 mvaddstr(0, 20, 244 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 245 mvaddstr(1, 5, "Load Average"); 246 } 247 (*curcmd->c_label)(); 248 #ifdef notdef 249 mvprintw(21, 25, "CPU usage on %s", hostname); 250 #endif 251 refresh(); 252 } 253 254 void 255 display(void) 256 { 257 int i, j; 258 259 /* Get the load average over the last minute. */ 260 (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); 261 (*curcmd->c_fetch)(); 262 if (curcmd->c_flags & CF_LOADAV) { 263 j = 5.0*avenrun[0] + 0.5; 264 dellave -= avenrun[0]; 265 if (dellave >= 0.0) 266 c = '<'; 267 else { 268 c = '>'; 269 dellave = -dellave; 270 } 271 if (dellave < 0.1) 272 c = '|'; 273 dellave = avenrun[0]; 274 wmove(wload, 0, 0); wclrtoeol(wload); 275 for (i = (j > 50) ? 50 : j; i > 0; i--) 276 waddch(wload, c); 277 if (j > 50) 278 wprintw(wload, " %4.1f", avenrun[0]); 279 } 280 (*curcmd->c_refresh)(); 281 if (curcmd->c_flags & CF_LOADAV) 282 wrefresh(wload); 283 wrefresh(wnd); 284 move(CMDLINE, col); 285 refresh(); 286 } 287 288 void 289 load(void) 290 { 291 292 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 293 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 294 avenrun[0], avenrun[1], avenrun[2]); 295 clrtoeol(); 296 } 297 298 void 299 die(int signo __unused) 300 { 301 move(CMDLINE, 0); 302 clrtoeol(); 303 refresh(); 304 endwin(); 305 exit(0); 306 } 307 308 #include <stdarg.h> 309 310 void 311 error(const char *fmt, ...) 312 { 313 va_list ap; 314 char buf[255]; 315 int oy, ox; 316 317 va_start(ap, fmt); 318 if (wnd) { 319 getyx(stdscr, oy, ox); 320 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 321 clrtoeol(); 322 standout(); 323 mvaddstr(CMDLINE, 0, buf); 324 standend(); 325 move(oy, ox); 326 refresh(); 327 } else { 328 (void) vfprintf(stderr, fmt, ap); 329 fprintf(stderr, "\n"); 330 } 331 va_end(ap); 332 } 333 334 void 335 nlisterr(struct nlist n_list[]) 336 { 337 int i, n; 338 339 n = 0; 340 clear(); 341 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 342 for (i = 0; 343 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++) 344 if (n_list[i].n_value == 0) 345 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name); 346 move(CMDLINE, 0); 347 clrtoeol(); 348 refresh(); 349 endwin(); 350 exit(1); 351 } 352