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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)main.c 8.1 (Berkeley) 6/6/93 31 * $FreeBSD: src/usr.bin/systat/main.c,v 1.11.2.1 2001/06/06 20:26:01 tmm Exp $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/time.h> 36 37 #include <err.h> 38 #include <limits.h> 39 #include <locale.h> 40 #include <nlist.h> 41 #include <signal.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 #include "systat.h" 46 #include "extern.h" 47 48 static struct nlist namelist[] = { 49 #define X_FIRST 0 50 #define X_HZ 0 51 { .n_name = "_hz" }, 52 #define X_STATHZ 1 53 { .n_name = "_stathz" }, 54 { .n_name = "" } 55 }; 56 static int dellave; 57 58 kvm_t *kd; 59 sig_t sigtstpdfl; 60 double avenrun[3]; 61 int col; 62 double naptime = 5.0; 63 int verbose = 1; /* to report kvm read errs */ 64 int hz, stathz; 65 double hertz; 66 char c; 67 char *namp; 68 char hostname[MAXHOSTNAMELEN]; 69 WINDOW *wnd; 70 int CMDLINE; 71 72 static WINDOW *wload; /* one line window for load average */ 73 74 int 75 main(int argc, char **argv) 76 { 77 char errbuf[_POSIX2_LINE_MAX]; 78 79 (void) setlocale(LC_TIME, ""); 80 81 argc--, argv++; 82 while (argc > 0) { 83 if (argv[0][0] == '-') { 84 struct cmdtab *p; 85 86 p = lookup(&argv[0][1]); 87 if (p == (struct cmdtab *)-1) 88 errx(1, "%s: ambiguous request", &argv[0][1]); 89 if (p == NULL) 90 errx(1, "%s: unknown request", &argv[0][1]); 91 curcmd = p; 92 } else { 93 naptime = strtod(argv[0], NULL); 94 if (naptime <= 0.0) 95 naptime = 5.0; 96 } 97 argc--, argv++; 98 } 99 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 100 if (kd == NULL) { 101 error("%s", errbuf); 102 exit(1); 103 } 104 signal(SIGINT, die); 105 signal(SIGQUIT, die); 106 signal(SIGTERM, die); 107 108 /* 109 * Initialize display. Load average appears in a one line 110 * window of its own. Current command's display appears in 111 * an overlapping sub-window of stdscr configured by the display 112 * routines to minimize update work by curses. 113 */ 114 initscr(); 115 CMDLINE = LINES - 1; 116 wnd = (*curcmd->c_open)(); 117 if (wnd == NULL) { 118 warnx("couldn't initialize display"); 119 die(0); 120 } 121 wload = newwin(1, 0, 3, 20); 122 if (wload == NULL) { 123 warnx("couldn't set up load average window"); 124 die(0); 125 } 126 if (kvm_nlist(kd, namelist)) { 127 nlisterr(namelist); 128 exit(1); 129 } 130 if (namelist[X_FIRST].n_type == 0) 131 errx(1, "couldn't read namelist"); 132 gethostname(hostname, sizeof (hostname)); 133 NREAD(X_HZ, &hz, sizeof(hz)); 134 NREAD(X_STATHZ, &stathz, sizeof(stathz)); 135 hertz = stathz ? stathz : hz; 136 (*curcmd->c_init)(); 137 curcmd->c_flags |= CF_INIT; 138 labels(); 139 140 dellave = 0.0; 141 142 signal(SIGALRM, display); 143 display(0); 144 noecho(); 145 crmode(); 146 keyboard(); 147 /*NOTREACHED*/ 148 149 return EXIT_SUCCESS; 150 } 151 152 void 153 labels(void) 154 { 155 if (curcmd->c_flags & CF_LOADAV) { 156 mvaddstr(2, 20, 157 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 158 mvaddstr(3, 5, "Load Average"); 159 } 160 (*curcmd->c_label)(); 161 #ifdef notdef 162 mvprintw(21, 25, "CPU usage on %s", hostname); 163 #endif 164 refresh(); 165 } 166 167 void 168 display(int signo __unused) 169 { 170 int i, j; 171 struct itimerval ctv; 172 173 /* Get the load average over the last minute. */ 174 (void) getloadavg(avenrun, NELEM(avenrun)); 175 (*curcmd->c_fetch)(); 176 if (curcmd->c_flags & CF_LOADAV) { 177 j = 5.0*avenrun[0] + 0.5; 178 dellave = avenrun[0]; 179 c = '|'; 180 wmove(wload, 0, 0); wclrtoeol(wload); 181 for (i = (j > 50) ? 50 : j; i > 0; i--) 182 waddch(wload, c); 183 if (j > 50) 184 wprintw(wload, " %4.1f", avenrun[0]); 185 } 186 (*curcmd->c_refresh)(); 187 if (curcmd->c_flags & CF_LOADAV) 188 wrefresh(wload); 189 wrefresh(wnd); 190 move(CMDLINE, col); 191 refresh(); 192 ctv.it_interval.tv_sec = 0; 193 ctv.it_interval.tv_usec = 0; 194 ctv.it_value.tv_sec = (int)naptime; 195 ctv.it_value.tv_usec = (naptime - (double)(int)naptime) * 1000000.0; 196 setitimer(ITIMER_REAL, &ctv, NULL); 197 } 198 199 void 200 load(void) 201 { 202 203 (void) getloadavg(avenrun, NELEM(avenrun)); 204 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 205 avenrun[0], avenrun[1], avenrun[2]); 206 clrtoeol(); 207 } 208 209 void 210 die(int signo __unused) 211 { 212 move(CMDLINE, 0); 213 clrtoeol(); 214 refresh(); 215 endwin(); 216 exit(0); 217 } 218 219 #include <stdarg.h> 220 221 void 222 error(const char *fmt, ...) 223 { 224 va_list ap; 225 char buf[255]; 226 int oy, ox; 227 va_start(ap, fmt); 228 229 if (wnd) { 230 getyx(stdscr, oy, ox); 231 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 232 clrtoeol(); 233 standout(); 234 mvaddstr(CMDLINE, 0, buf); 235 standend(); 236 move(oy, ox); 237 refresh(); 238 } else { 239 (void) vfprintf(stderr, fmt, ap); 240 fprintf(stderr, "\n"); 241 } 242 va_end(ap); 243 } 244 245 void 246 nlisterr(struct nlist *n_list) 247 { 248 int i, n; 249 250 n = 0; 251 clear(); 252 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 253 for (i = 0; 254 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++) 255 if (n_list[i].n_value == 0) 256 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name); 257 move(CMDLINE, 0); 258 clrtoeol(); 259 refresh(); 260 endwin(); 261 exit(1); 262 } 263