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