1 /* $NetBSD: main.c,v 1.45 2009/11/04 21:46:24 dsl Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\ 35 The Regents of the University of California. All rights reserved."); 36 #if 0 37 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 38 #endif 39 __RCSID("$NetBSD: main.c,v 1.45 2009/11/04 21:46:24 dsl Exp $"); 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 44 #include <ctype.h> 45 #include <err.h> 46 #include <limits.h> 47 #include <signal.h> 48 #include <stdarg.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <termios.h> 54 #include <sys/ioctl.h> 55 56 #include "systat.h" 57 #include "extern.h" 58 59 static struct nlist namelist[] = { 60 #define X_FIRST 0 61 #define X_HZ 0 62 { .n_name = "_hz" }, 63 #define X_STATHZ 1 64 { .n_name = "_stathz" }, 65 #define X_MAXSLP 2 66 { .n_name = "_maxslp" }, 67 { .n_name = NULL } 68 }; 69 static int dellave; 70 71 kvm_t *kd; 72 char *memf = NULL; 73 char *nlistf = NULL; 74 sig_t sigtstpdfl; 75 double avenrun[3]; 76 int col; 77 int naptime = 5; 78 int verbose = 1; /* to report kvm read errs */ 79 int hz, stathz, maxslp; 80 char c; 81 char *namp; 82 char hostname[MAXHOSTNAMELEN + 1]; 83 WINDOW *wnd; 84 int CMDLINE; 85 int turns = 2; /* stay how many refresh-turns in 'all' mode? */ 86 int allflag; 87 int allcounter; 88 sig_atomic_t needsredraw = 0; 89 int use_sysctl = 1; 90 91 static WINDOW *wload; /* one line window for load average */ 92 93 static void (*sv_stop_handler)(int); 94 95 static void stop(int); 96 static void usage(void); 97 int main(int, char **); 98 99 gid_t egid; /* XXX needed by initiostat() and initkre() */ 100 101 int 102 main(int argc, char **argv) 103 { 104 int ch; 105 char errbuf[_POSIX2_LINE_MAX]; 106 const char *all; 107 108 all = "all"; 109 egid = getegid(); 110 (void)setegid(getgid()); 111 112 while ((ch = getopt(argc, argv, "M:N:nw:t:")) != -1) 113 switch(ch) { 114 case 'M': 115 memf = optarg; 116 use_sysctl = 0; 117 break; 118 case 'N': 119 nlistf = optarg; 120 break; 121 case 'n': 122 nflag = !nflag; 123 break; 124 case 'w': 125 if ((naptime = atoi(optarg)) <= 0) 126 errx(1, "interval <= 0."); 127 break; 128 case 't': 129 if ((turns = atoi(optarg)) <= 0) 130 errx(1, "turns <= 0."); 131 break; 132 case '?': 133 default: 134 usage(); 135 } 136 argc -= optind; 137 argv += optind; 138 139 140 for ( ; argc > 0; argc--, argv++) { 141 struct mode *p; 142 int modefound = 0; 143 144 if (isdigit((unsigned char)argv[0][0])) { 145 naptime = atoi(argv[0]); 146 if (naptime <= 0) 147 naptime = 5; 148 continue; 149 } 150 151 for (p = modes; p->c_name ; p++) { 152 if (strstr(p->c_name, argv[0]) == p->c_name) { 153 curmode = p; 154 modefound++; 155 break; 156 } 157 158 if (strstr(all, argv[0]) == all) { 159 allcounter=0; 160 allflag=1; 161 } 162 } 163 164 165 if (!modefound && !allflag) 166 error("%s: Unknown command.", argv[0]); 167 } 168 169 /* 170 * Discard setgid privileges. If not the running kernel, we toss 171 * them away totally so that bad guys can't print interesting stuff 172 * from kernel memory, otherwise switch back to kmem for the 173 * duration of the kvm_openfiles() call. 174 */ 175 if (nlistf != NULL || memf != NULL) 176 (void)setgid(getgid()); 177 else 178 (void)setegid(egid); 179 180 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 181 if (kd == NULL) { 182 error("%s", errbuf); 183 exit(1); 184 } 185 186 /* Get rid of privs for now. */ 187 if (nlistf == NULL && memf == NULL) 188 (void)setegid(getgid()); 189 190 if (kvm_nlist(kd, namelist)) { 191 if (nlistf) 192 errx(1, "%s: no namelist", nlistf); 193 else 194 errx(1, "no namelist"); 195 } 196 197 signal(SIGINT, die); 198 signal(SIGQUIT, die); 199 signal(SIGTERM, die); 200 sv_stop_handler = signal(SIGTSTP, stop); 201 202 /* 203 * Initialize display. Load average appears in a one line 204 * window of its own. Current command's display appears in 205 * an overlapping sub-window of stdscr configured by the display 206 * routines to minimize update work by curses. 207 */ 208 if (initscr() == NULL) 209 { 210 warnx("couldn't initialize screen"); 211 exit(0); 212 } 213 214 CMDLINE = LINES - 1; 215 wnd = (*curmode->c_open)(); 216 if (wnd == NULL) { 217 warnx("couldn't initialize display"); 218 die(0); 219 } 220 wload = newwin(1, 0, 3, 20); 221 if (wload == NULL) { 222 warnx("couldn't set up load average window"); 223 die(0); 224 } 225 gethostname(hostname, sizeof (hostname)); 226 hostname[sizeof(hostname) - 1] = '\0'; 227 NREAD(X_HZ, &hz, sizeof hz); 228 NREAD(X_STATHZ, &stathz, sizeof stathz); 229 NREAD(X_MAXSLP, &maxslp, sizeof maxslp); 230 (*curmode->c_init)(); 231 curmode->c_flags |= CF_INIT; 232 labels(); 233 234 dellave = 0.0; 235 236 display(0); 237 noecho(); 238 cbreak(); 239 keyboard(); 240 /*NOTREACHED*/ 241 } 242 243 static void 244 usage(void) 245 { 246 fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] " 247 "[-t turns]\n\t\t[display] [refresh-interval]\n"); 248 exit(1); 249 } 250 251 252 void 253 labels(void) 254 { 255 if (curmode->c_flags & CF_LOADAV) { 256 mvaddstr(2, 20, 257 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 258 mvaddstr(3, 5, "Load Average"); 259 } 260 (*curmode->c_label)(); 261 #ifdef notdef 262 mvprintw(21, 25, "CPU usage on %s", hostname); 263 #endif 264 refresh(); 265 } 266 267 void 268 display(int signo) 269 { 270 static int skip; 271 int j; 272 struct mode *p; 273 int ms_delay; 274 275 if (signo == SIGALRM && skip-- > 0) 276 /* Don't display on this timeout */ 277 return; 278 279 /* Get the load average over the last minute. */ 280 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); 281 (*curmode->c_fetch)(); 282 if (curmode->c_flags & CF_LOADAV) { 283 j = 5.0*avenrun[0] + 0.5; 284 dellave -= avenrun[0]; 285 if (dellave >= 0.0) 286 c = '<'; 287 else { 288 c = '>'; 289 dellave = -dellave; 290 } 291 if (dellave < 0.1) 292 c = '|'; 293 dellave = avenrun[0]; 294 wmove(wload, 0, 0); 295 wclrtoeol(wload); 296 whline(wload, c, (j > 50) ? 50 : j); 297 if (j > 50) 298 wprintw(wload, " %4.1f", avenrun[0]); 299 } 300 (*curmode->c_refresh)(); 301 if (curmode->c_flags & CF_LOADAV) 302 wrefresh(wload); 303 wrefresh(wnd); 304 move(CMDLINE, col); 305 refresh(); 306 307 if (allflag && signo==SIGALRM) { 308 if (allcounter >= turns){ 309 p = curmode; 310 p++; 311 if (p->c_name == NULL) 312 p = modes; 313 switch_mode(p); 314 allcounter=0; 315 } else 316 allcounter++; 317 } 318 319 /* curses timeout() uses VTIME, limited to 255 1/10th secs */ 320 ms_delay = naptime * 1000; 321 if (ms_delay < 25500) { 322 timeout(ms_delay); 323 skip = 0; 324 } else { 325 skip = ms_delay / 25500; 326 timeout(ms_delay / (skip + 1)); 327 } 328 } 329 330 void 331 redraw(void) 332 { 333 resizeterm(LINES, COLS); 334 CMDLINE = LINES - 1; 335 labels(); 336 337 display(0); 338 needsredraw = 0; 339 } 340 341 static void 342 stop(int signo) 343 { 344 sigset_t set; 345 346 signal(SIGTSTP, sv_stop_handler); 347 /* unblock SIGTSTP */ 348 sigemptyset(&set); 349 sigaddset(&set, SIGTSTP); 350 sigprocmask(SIG_UNBLOCK, &set, NULL); 351 /* stop ourselves */ 352 kill(0, SIGTSTP); 353 /* must have been restarted */ 354 signal(SIGTSTP, stop); 355 needsredraw = signo; 356 } 357 358 void 359 die(int signo) 360 { 361 move(CMDLINE, 0); 362 clrtoeol(); 363 refresh(); 364 endwin(); 365 exit(0); 366 } 367 368 void 369 error(const char *fmt, ...) 370 { 371 va_list ap; 372 char buf[255]; 373 int oy, ox; 374 375 va_start(ap, fmt); 376 377 if (wnd) { 378 getyx(stdscr, oy, ox); 379 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 380 clrtoeol(); 381 standout(); 382 mvaddstr(CMDLINE, 0, buf); 383 standend(); 384 move(oy, ox); 385 refresh(); 386 } else { 387 (void) vfprintf(stderr, fmt, ap); 388 fprintf(stderr, "\n"); 389 } 390 va_end(ap); 391 } 392 393 void 394 nlisterr(struct nlist name_list[]) 395 { 396 int i, n; 397 398 n = 0; 399 clear(); 400 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 401 for (i = 0; 402 name_list[i].n_name != NULL && *name_list[i].n_name != '\0'; i++) 403 if (name_list[i].n_value == 0) 404 mvprintw(2 + ++n, 10, "%s", name_list[i].n_name); 405 move(CMDLINE, 0); 406 clrtoeol(); 407 refresh(); 408 sleep(5); 409 endwin(); 410 exit(1); 411 } 412