1 /* $NetBSD: main.c,v 1.48 2014/06/03 22:22:41 joerg 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.48 2014/06/03 22:22:41 joerg Exp $"); 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/sysctl.h> 44 #include <sys/ioctl.h> 45 46 #include <ctype.h> 47 #include <err.h> 48 #include <errno.h> 49 #include <limits.h> 50 #include <signal.h> 51 #include <stdarg.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 #include <termios.h> 57 58 #include "systat.h" 59 #include "extern.h" 60 61 static int dellave; 62 63 kvm_t *kd; 64 char *memf = NULL; 65 char *nlistf = NULL; 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 int hz, stathz, maxslp; 72 char c; 73 char *namp; 74 char hostname[MAXHOSTNAMELEN + 1]; 75 WINDOW *wnd; 76 int CMDLINE; 77 int turns = 2; /* stay how many refresh-turns in 'all' mode? */ 78 int allflag; 79 int allcounter; 80 sig_atomic_t needsredraw = 0; 81 82 static WINDOW *wload; /* one line window for load average */ 83 84 static void (*sv_stop_handler)(int); 85 86 static void stop(int); 87 __dead static void usage(void); 88 89 gid_t egid; /* XXX needed by initiostat() and initkre() */ 90 91 int 92 main(int argc, char **argv) 93 { 94 int ch; 95 char errbuf[_POSIX2_LINE_MAX]; 96 const char *all; 97 struct clockinfo clk; 98 size_t len; 99 100 all = "all"; 101 egid = getegid(); 102 (void)setegid(getgid()); 103 104 while ((ch = getopt(argc, argv, "M:N:nw:t:")) != -1) 105 switch(ch) { 106 case 'M': 107 memf = optarg; 108 break; 109 case 'N': 110 nlistf = optarg; 111 break; 112 case 'n': 113 nflag = !nflag; 114 break; 115 case 'w': 116 if ((naptime = atoi(optarg)) <= 0) 117 errx(1, "interval <= 0."); 118 break; 119 case 't': 120 if ((turns = atoi(optarg)) <= 0) 121 errx(1, "turns <= 0."); 122 break; 123 case '?': 124 default: 125 usage(); 126 } 127 argc -= optind; 128 argv += optind; 129 130 131 for ( ; argc > 0; argc--, argv++) { 132 struct mode *p; 133 int modefound = 0; 134 135 if (isdigit((unsigned char)argv[0][0])) { 136 naptime = atoi(argv[0]); 137 if (naptime <= 0) 138 naptime = 5; 139 continue; 140 } 141 142 for (p = modes; p->c_name ; p++) { 143 if (strstr(p->c_name, argv[0]) == p->c_name) { 144 curmode = p; 145 modefound++; 146 break; 147 } 148 149 if (strstr(all, argv[0]) == all) { 150 allcounter=0; 151 allflag=1; 152 } 153 } 154 155 156 if (!modefound && !allflag) 157 error("%s: Unknown command.", argv[0]); 158 } 159 160 /* 161 * Discard setgid privileges. If not the running kernel, we toss 162 * them away totally so that bad guys can't print interesting stuff 163 * from kernel memory, otherwise switch back to kmem for the 164 * duration of the kvm_openfiles() call. 165 */ 166 if (nlistf != NULL || memf != NULL) 167 (void)setgid(getgid()); 168 else 169 (void)setegid(egid); 170 171 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 172 if (kd == NULL) { 173 error("%s", errbuf); 174 exit(1); 175 } 176 177 /* Get rid of privs for now. */ 178 if (nlistf == NULL && memf == NULL) 179 (void)setegid(getgid()); 180 181 signal(SIGINT, die); 182 signal(SIGQUIT, die); 183 signal(SIGTERM, die); 184 sv_stop_handler = signal(SIGTSTP, stop); 185 186 /* 187 * Initialize display. Load average appears in a one line 188 * window of its own. Current command's display appears in 189 * an overlapping sub-window of stdscr configured by the display 190 * routines to minimize update work by curses. 191 */ 192 if (initscr() == NULL) 193 { 194 warnx("couldn't initialize screen"); 195 exit(0); 196 } 197 198 CMDLINE = LINES - 1; 199 wnd = (*curmode->c_open)(); 200 if (wnd == NULL) { 201 warnx("couldn't initialize display"); 202 die(0); 203 } 204 wload = newwin(1, 0, 3, 20); 205 if (wload == NULL) { 206 warnx("couldn't set up load average window"); 207 die(0); 208 } 209 gethostname(hostname, sizeof (hostname)); 210 hostname[sizeof(hostname) - 1] = '\0'; 211 212 len = sizeof(clk); 213 if (sysctlbyname("kern.clockrate", &clk, &len, NULL, 0)) 214 error("can't get \"kern.clockrate\": %s", strerror(errno)); 215 hz = clk.hz; 216 stathz = clk.stathz; 217 218 len = sizeof(maxslp); 219 if (sysctlbyname("vm.maxslp", &maxslp, &len, NULL, 0)) 220 error("can't get \"vm.maxslp\": %s", strerror(errno)); 221 222 (*curmode->c_init)(); 223 curmode->c_flags |= CF_INIT; 224 labels(); 225 226 dellave = 0.0; 227 228 display(0); 229 noecho(); 230 cbreak(); 231 keyboard(); 232 /*NOTREACHED*/ 233 } 234 235 static void 236 usage(void) 237 { 238 fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] " 239 "[-t turns]\n\t\t[display] [refresh-interval]\n"); 240 exit(1); 241 } 242 243 244 void 245 labels(void) 246 { 247 if (curmode->c_flags & CF_LOADAV) { 248 mvaddstr(2, 20, 249 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 250 mvaddstr(3, 5, "Load Average"); 251 } 252 (*curmode->c_label)(); 253 #ifdef notdef 254 mvprintw(21, 25, "CPU usage on %s", hostname); 255 #endif 256 refresh(); 257 } 258 259 void 260 display(int signo) 261 { 262 static int skip; 263 int j; 264 struct mode *p; 265 int ms_delay; 266 267 if (signo == SIGALRM && skip-- > 0) 268 /* Don't display on this timeout */ 269 return; 270 271 /* Get the load average over the last minute. */ 272 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); 273 (*curmode->c_fetch)(); 274 if (curmode->c_flags & CF_LOADAV) { 275 j = 5.0*avenrun[0] + 0.5; 276 dellave -= avenrun[0]; 277 if (dellave >= 0.0) 278 c = '<'; 279 else { 280 c = '>'; 281 dellave = -dellave; 282 } 283 if (dellave < 0.1) 284 c = '|'; 285 dellave = avenrun[0]; 286 wmove(wload, 0, 0); 287 wclrtoeol(wload); 288 whline(wload, c, (j > 50) ? 50 : j); 289 if (j > 50) 290 wprintw(wload, " %4.1f", avenrun[0]); 291 } 292 (*curmode->c_refresh)(); 293 if (curmode->c_flags & CF_LOADAV) 294 wrefresh(wload); 295 wrefresh(wnd); 296 move(CMDLINE, col); 297 refresh(); 298 299 if (allflag && signo==SIGALRM) { 300 if (allcounter >= turns){ 301 p = curmode; 302 p++; 303 if (p->c_name == NULL) 304 p = modes; 305 switch_mode(p); 306 allcounter=0; 307 } else 308 allcounter++; 309 } 310 311 /* curses timeout() uses VTIME, limited to 255 1/10th secs */ 312 ms_delay = naptime * 1000; 313 if (ms_delay < 25500) { 314 timeout(ms_delay); 315 skip = 0; 316 } else { 317 skip = ms_delay / 25500; 318 timeout(ms_delay / (skip + 1)); 319 } 320 } 321 322 void 323 redraw(void) 324 { 325 resizeterm(LINES, COLS); 326 CMDLINE = LINES - 1; 327 labels(); 328 329 display(0); 330 needsredraw = 0; 331 } 332 333 static void 334 stop(int signo) 335 { 336 sigset_t set; 337 338 signal(SIGTSTP, sv_stop_handler); 339 /* unblock SIGTSTP */ 340 sigemptyset(&set); 341 sigaddset(&set, SIGTSTP); 342 sigprocmask(SIG_UNBLOCK, &set, NULL); 343 /* stop ourselves */ 344 kill(0, SIGTSTP); 345 /* must have been restarted */ 346 signal(SIGTSTP, stop); 347 needsredraw = signo; 348 } 349 350 void 351 die(int signo) 352 { 353 move(CMDLINE, 0); 354 clrtoeol(); 355 refresh(); 356 endwin(); 357 exit(0); 358 } 359 360 void 361 error(const char *fmt, ...) 362 { 363 va_list ap; 364 char buf[255]; 365 int oy, ox; 366 367 va_start(ap, fmt); 368 369 if (wnd) { 370 getyx(stdscr, oy, ox); 371 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 372 clrtoeol(); 373 standout(); 374 mvaddstr(CMDLINE, 0, buf); 375 standend(); 376 move(oy, ox); 377 refresh(); 378 } else { 379 (void) vfprintf(stderr, fmt, ap); 380 fprintf(stderr, "\n"); 381 } 382 va_end(ap); 383 } 384 385 void 386 nlisterr(struct nlist name_list[]) 387 { 388 int i, n; 389 390 n = 0; 391 clear(); 392 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 393 for (i = 0; 394 name_list[i].n_name != NULL && *name_list[i].n_name != '\0'; i++) 395 if (name_list[i].n_value == 0) 396 mvprintw(2 + ++n, 10, "%s", name_list[i].n_name); 397 move(CMDLINE, 0); 398 clrtoeol(); 399 refresh(); 400 sleep(5); 401 endwin(); 402 exit(1); 403 } 404