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 #include <sys/time.h> 50 51 #include <err.h> 52 #include <locale.h> 53 #include <nlist.h> 54 #include <paths.h> 55 #include <signal.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <unistd.h> 59 #include "systat.h" 60 #include "extern.h" 61 62 static int dellave; 63 64 kvm_t *kd; 65 sig_t sigtstpdfl; 66 double avenrun[3]; 67 int col; 68 int naptime = 5; 69 int verbose = 1; /* to report kvm read errs */ 70 struct clockinfo clkinfo; 71 double hertz; 72 char c; 73 char *namp; 74 char hostname[MAXHOSTNAMELEN]; 75 WINDOW *wnd; 76 int CMDLINE; 77 78 static WINDOW *wload; /* one line window for load average */ 79 80 int 81 main(argc, argv) 82 int argc; 83 char **argv; 84 { 85 char errbuf[80]; 86 size_t size; 87 int err; 88 89 (void) setlocale(LC_TIME, ""); 90 91 argc--, argv++; 92 while (argc > 0) { 93 if (argv[0][0] == '-') { 94 struct cmdtab *p; 95 96 p = lookup(&argv[0][1]); 97 if (p == (struct cmdtab *)-1) 98 errx(1, "%s: ambiguous request", &argv[0][1]); 99 if (p == (struct cmdtab *)0) 100 errx(1, "%s: unknown request", &argv[0][1]); 101 curcmd = p; 102 } else { 103 naptime = atoi(argv[0]); 104 if (naptime <= 0) 105 naptime = 5; 106 } 107 argc--, argv++; 108 } 109 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 110 if (kd == NULL) { 111 error("%s", errbuf); 112 exit(1); 113 } 114 signal(SIGINT, die); 115 signal(SIGQUIT, die); 116 signal(SIGTERM, die); 117 118 /* 119 * Initialize display. Load average appears in a one line 120 * window of its own. Current command's display appears in 121 * an overlapping sub-window of stdscr configured by the display 122 * routines to minimize update work by curses. 123 */ 124 initscr(); 125 CMDLINE = LINES - 1; 126 wnd = (*curcmd->c_open)(); 127 if (wnd == NULL) { 128 warnx("couldn't initialize display"); 129 die(0); 130 } 131 wload = newwin(1, 0, 3, 20); 132 if (wload == NULL) { 133 warnx("couldn't set up load average window"); 134 die(0); 135 } 136 gethostname(hostname, sizeof (hostname)); 137 size = sizeof(clkinfo); 138 err = sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0); 139 if (err != 0 || size != sizeof(clkinfo)) { 140 error("kern.clockrate"); 141 die(0); 142 } 143 hertz = clkinfo.stathz; 144 (*curcmd->c_init)(); 145 curcmd->c_flags |= CF_INIT; 146 labels(); 147 148 dellave = 0.0; 149 150 signal(SIGALRM, display); 151 display(0); 152 noecho(); 153 crmode(); 154 keyboard(); 155 /*NOTREACHED*/ 156 157 return EXIT_SUCCESS; 158 } 159 160 void 161 labels() 162 { 163 if (curcmd->c_flags & CF_LOADAV) { 164 mvaddstr(2, 20, 165 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 166 mvaddstr(3, 5, "Load Average"); 167 } 168 (*curcmd->c_label)(); 169 #ifdef notdef 170 mvprintw(21, 25, "CPU usage on %s", hostname); 171 #endif 172 refresh(); 173 } 174 175 void 176 display(signo) 177 int signo; 178 { 179 register int i, j; 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 alarm(naptime); 209 } 210 211 void 212 load() 213 { 214 215 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 216 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 217 avenrun[0], avenrun[1], avenrun[2]); 218 clrtoeol(); 219 } 220 221 void 222 die(signo) 223 int signo; 224 { 225 move(CMDLINE, 0); 226 clrtoeol(); 227 refresh(); 228 endwin(); 229 exit(0); 230 } 231 232 #if __STDC__ 233 #include <stdarg.h> 234 #else 235 #include <varargs.h> 236 #endif 237 238 #if __STDC__ 239 void 240 error(const char *fmt, ...) 241 #else 242 void 243 error(fmt, va_alist) 244 char *fmt; 245 va_dcl 246 #endif 247 { 248 va_list ap; 249 char buf[255]; 250 int oy, ox; 251 #if __STDC__ 252 va_start(ap, fmt); 253 #else 254 va_start(ap); 255 #endif 256 257 if (wnd) { 258 getyx(stdscr, oy, ox); 259 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 260 clrtoeol(); 261 standout(); 262 mvaddstr(CMDLINE, 0, buf); 263 standend(); 264 move(oy, ox); 265 refresh(); 266 } else { 267 (void) vfprintf(stderr, fmt, ap); 268 fprintf(stderr, "\n"); 269 } 270 va_end(ap); 271 } 272 273 void 274 nlisterr(namelist) 275 struct nlist namelist[]; 276 { 277 int i, n; 278 279 n = 0; 280 clear(); 281 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 282 for (i = 0; 283 namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++) 284 if (namelist[i].n_value == 0) 285 mvprintw(2 + ++n, 10, "%s", namelist[i].n_name); 286 move(CMDLINE, 0); 287 clrtoeol(); 288 refresh(); 289 endwin(); 290 exit(1); 291 } 292