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