1 /* $NetBSD: main.c,v 1.44 2009/07/14 21:08:31 apb 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.44 2009/07/14 21:08:31 apb 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 int j; 271 struct mode *p; 272 273 /* Get the load average over the last minute. */ 274 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); 275 (*curmode->c_fetch)(); 276 if (curmode->c_flags & CF_LOADAV) { 277 j = 5.0*avenrun[0] + 0.5; 278 dellave -= avenrun[0]; 279 if (dellave >= 0.0) 280 c = '<'; 281 else { 282 c = '>'; 283 dellave = -dellave; 284 } 285 if (dellave < 0.1) 286 c = '|'; 287 dellave = avenrun[0]; 288 wmove(wload, 0, 0); 289 wclrtoeol(wload); 290 whline(wload, c, (j > 50) ? 50 : j); 291 if (j > 50) 292 wprintw(wload, " %4.1f", avenrun[0]); 293 } 294 (*curmode->c_refresh)(); 295 if (curmode->c_flags & CF_LOADAV) 296 wrefresh(wload); 297 wrefresh(wnd); 298 move(CMDLINE, col); 299 refresh(); 300 301 if (allflag && signo==SIGALRM) { 302 if (allcounter >= turns){ 303 p = curmode; 304 p++; 305 if (p->c_name == NULL) 306 p = modes; 307 switch_mode(p); 308 allcounter=0; 309 } else 310 allcounter++; 311 } 312 313 timeout(naptime * 1000); 314 } 315 316 void 317 redraw(void) 318 { 319 resizeterm(LINES, COLS); 320 CMDLINE = LINES - 1; 321 labels(); 322 323 display(0); 324 needsredraw = 0; 325 } 326 327 static void 328 stop(int signo) 329 { 330 sigset_t set; 331 332 signal(SIGTSTP, sv_stop_handler); 333 /* unblock SIGTSTP */ 334 sigemptyset(&set); 335 sigaddset(&set, SIGTSTP); 336 sigprocmask(SIG_UNBLOCK, &set, NULL); 337 /* stop ourselves */ 338 kill(0, SIGTSTP); 339 /* must have been restarted */ 340 signal(SIGTSTP, stop); 341 needsredraw = signo; 342 } 343 344 void 345 die(int signo) 346 { 347 move(CMDLINE, 0); 348 clrtoeol(); 349 refresh(); 350 endwin(); 351 exit(0); 352 } 353 354 void 355 error(const char *fmt, ...) 356 { 357 va_list ap; 358 char buf[255]; 359 int oy, ox; 360 361 va_start(ap, fmt); 362 363 if (wnd) { 364 getyx(stdscr, oy, ox); 365 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 366 clrtoeol(); 367 standout(); 368 mvaddstr(CMDLINE, 0, buf); 369 standend(); 370 move(oy, ox); 371 refresh(); 372 } else { 373 (void) vfprintf(stderr, fmt, ap); 374 fprintf(stderr, "\n"); 375 } 376 va_end(ap); 377 } 378 379 void 380 nlisterr(struct nlist name_list[]) 381 { 382 int i, n; 383 384 n = 0; 385 clear(); 386 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 387 for (i = 0; 388 name_list[i].n_name != NULL && *name_list[i].n_name != '\0'; i++) 389 if (name_list[i].n_value == 0) 390 mvprintw(2 + ++n, 10, "%s", name_list[i].n_name); 391 move(CMDLINE, 0); 392 clrtoeol(); 393 refresh(); 394 sleep(5); 395 endwin(); 396 exit(1); 397 } 398