Annotation of src/usr.bin/systat/main.c, Revision 1.27
1.27 ! deraadt 1: /* $OpenBSD: main.c,v 1.26 2003/06/03 02:56:17 millert Exp $ */
1.3 deraadt 2: /* $NetBSD: main.c,v 1.8 1996/05/10 23:16:36 thorpej Exp $ */
1.1 deraadt 3:
4: /*-
5: * Copyright (c) 1980, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.26 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1980, 1992, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
42: #endif
1.27 ! deraadt 43: static char rcsid[] = "$OpenBSD: main.c,v 1.26 2003/06/03 02:56:17 millert Exp $";
1.1 deraadt 44: #endif /* not lint */
45:
46: #include <sys/param.h>
1.21 deraadt 47: #include <sys/sysctl.h>
1.1 deraadt 48:
49: #include <err.h>
50: #include <nlist.h>
51: #include <signal.h>
1.11 millert 52: #include <ctype.h>
1.1 deraadt 53: #include <stdio.h>
54: #include <string.h>
1.3 deraadt 55: #include <unistd.h>
1.11 millert 56: #include <stdlib.h>
1.6 deraadt 57: #include <limits.h>
1.23 millert 58: #include <stdarg.h>
1.1 deraadt 59:
60: #include "systat.h"
61: #include "extern.h"
62:
1.21 deraadt 63: double dellave;
1.1 deraadt 64:
1.21 deraadt 65: kvm_t *kd;
66: char *nlistf = NULL;
1.20 pvalchev 67: char *memf = NULL;
1.1 deraadt 68: sig_t sigtstpdfl;
1.21 deraadt 69: double avenrun[3];
70: int col;
1.1 deraadt 71: int naptime = 5;
1.21 deraadt 72: int verbose = 1; /* to report kvm read errs */
73: int hz, stathz;
1.1 deraadt 74: char c;
75: char *namp;
76: char hostname[MAXHOSTNAMELEN];
77: WINDOW *wnd;
1.11 millert 78: long CMDLINE;
1.1 deraadt 79:
1.21 deraadt 80: WINDOW *wload; /* one line window for load average */
1.1 deraadt 81:
1.22 millert 82: static void usage(void);
1.3 deraadt 83:
1.2 deraadt 84: int
1.24 deraadt 85: main(int argc, char *argv[])
1.1 deraadt 86: {
1.21 deraadt 87: int ch;
1.6 deraadt 88: char errbuf[_POSIX2_LINE_MAX];
1.1 deraadt 89:
1.25 deraadt 90: kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
91: if (kd == NULL) {
92: error("%s", errbuf);
93: exit(1);
94: }
95:
96: setegid(getgid());
97: setgid(getgid());
98:
1.21 deraadt 99: while ((ch = getopt(argc, argv, "w:")) != -1)
1.24 deraadt 100: switch (ch) {
1.21 deraadt 101: case 'w':
102: if ((naptime = atoi(optarg)) <= 0)
103: errx(1, "interval <= 0.");
104: break;
105: default:
106: usage();
107: }
108: argc -= optind;
109: argv += optind;
1.3 deraadt 110:
1.1 deraadt 111: while (argc > 0) {
1.3 deraadt 112: if (isdigit(argv[0][0])) {
113: naptime = atoi(argv[0]);
114: if (naptime <= 0)
115: naptime = 5;
116: } else {
1.1 deraadt 117: struct cmdtab *p;
118:
1.3 deraadt 119: p = lookup(&argv[0][0]);
1.1 deraadt 120: if (p == (struct cmdtab *)-1)
1.3 deraadt 121: errx(1, "ambiguous request: %s", &argv[0][0]);
1.1 deraadt 122: if (p == 0)
1.3 deraadt 123: errx(1, "unknown request: %s", &argv[0][0]);
1.1 deraadt 124: curcmd = p;
125: }
1.21 deraadt 126: argc--;
127: argv++;
1.1 deraadt 128: }
1.24 deraadt 129:
1.21 deraadt 130: signal(SIGINT, sigdie);
131: siginterrupt(SIGINT, 1);
132: signal(SIGQUIT, sigdie);
133: siginterrupt(SIGQUIT, 1);
134: signal(SIGTERM, sigdie);
135: siginterrupt(SIGTERM, 1);
1.1 deraadt 136:
137: /*
138: * Initialize display. Load average appears in a one line
139: * window of its own. Current command's display appears in
140: * an overlapping sub-window of stdscr configured by the display
141: * routines to minimize update work by curses.
142: */
1.24 deraadt 143: if (initscr() == NULL) {
1.1 deraadt 144: warnx("couldn't initialize screen");
145: exit(0);
146: }
147:
148: CMDLINE = LINES - 1;
149: wnd = (*curcmd->c_open)();
150: if (wnd == NULL) {
151: warnx("couldn't initialize display");
1.21 deraadt 152: die();
1.1 deraadt 153: }
154: wload = newwin(1, 0, 3, 20);
155: if (wload == NULL) {
156: warnx("couldn't set up load average window");
1.21 deraadt 157: die();
1.1 deraadt 158: }
159: gethostname(hostname, sizeof (hostname));
1.21 deraadt 160: gethz();
1.1 deraadt 161: (*curcmd->c_init)();
162: curcmd->c_flags |= CF_INIT;
163: labels();
164:
165: dellave = 0.0;
166:
1.21 deraadt 167: signal(SIGALRM, sigdisplay);
168: siginterrupt(SIGALRM, 1);
169: signal(SIGWINCH, sigwinch);
170: siginterrupt(SIGWINCH, 1);
171: gotdisplay = 1;
1.1 deraadt 172: noecho();
173: crmode();
174: keyboard();
175: /*NOTREACHED*/
176: }
1.3 deraadt 177:
1.21 deraadt 178: void
1.24 deraadt 179: gethz(void)
1.21 deraadt 180: {
181: struct clockinfo cinf;
182: size_t size = sizeof(cinf);
183: int mib[2];
184:
185: mib[0] = CTL_KERN;
186: mib[1] = KERN_CLOCKRATE;
187: if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
188: return;
189: stathz = cinf.stathz;
190: hz = cinf.hz;
191: }
192:
1.3 deraadt 193: static void
1.24 deraadt 194: usage(void)
1.3 deraadt 195: {
1.7 niklas 196: fprintf(stderr,
1.27 ! deraadt 197: "usage: systat [-w wait] [iostat|mbufs|netstat|pigs|swap|vmstat]\n");
1.3 deraadt 198: exit(1);
199: }
200:
1.1 deraadt 201:
202: void
1.24 deraadt 203: labels(void)
1.1 deraadt 204: {
205: if (curcmd->c_flags & CF_LOADAV) {
206: mvaddstr(2, 20,
207: "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
208: mvaddstr(3, 5, "Load Average");
209: }
210: (*curcmd->c_label)();
211: #ifdef notdef
212: mvprintw(21, 25, "CPU usage on %s", hostname);
213: #endif
214: refresh();
215: }
216:
217: void
1.21 deraadt 218: sigdisplay(signo)
1.1 deraadt 219: int signo;
220: {
1.21 deraadt 221: gotdisplay = 1;
222: }
223:
224: void
225: display(void)
226: {
1.17 mpech 227: int i, j;
1.1 deraadt 228:
229: /* Get the load average over the last minute. */
230: (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
231: (*curcmd->c_fetch)();
232: if (curcmd->c_flags & CF_LOADAV) {
233: j = 5.0*avenrun[0] + 0.5;
234: dellave -= avenrun[0];
235: if (dellave >= 0.0)
236: c = '<';
237: else {
238: c = '>';
239: dellave = -dellave;
240: }
1.10 kstailey 241: if (dellave < 0.05)
1.1 deraadt 242: c = '|';
243: dellave = avenrun[0];
1.21 deraadt 244: wmove(wload, 0, 0);
245: wclrtoeol(wload);
1.1 deraadt 246: for (i = (j > 50) ? 50 : j; i > 0; i--)
247: waddch(wload, c);
248: if (j > 50)
249: wprintw(wload, " %4.1f", avenrun[0]);
250: }
251: (*curcmd->c_refresh)();
252: if (curcmd->c_flags & CF_LOADAV)
253: wrefresh(wload);
254: wrefresh(wnd);
255: move(CMDLINE, col);
256: refresh();
257: alarm(naptime);
258: }
259:
260: void
1.24 deraadt 261: load(void)
1.1 deraadt 262: {
263:
264: (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
265: mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
266: avenrun[0], avenrun[1], avenrun[2]);
267: clrtoeol();
268: }
269:
1.21 deraadt 270: volatile sig_atomic_t gotdie;
271: volatile sig_atomic_t gotdisplay;
272: volatile sig_atomic_t gotwinch;
273:
1.1 deraadt 274: void
1.21 deraadt 275: sigdie(signo)
1.1 deraadt 276: int signo;
277: {
1.21 deraadt 278: gotdie = 1;
279: }
280:
281: void
1.24 deraadt 282: die(void)
1.21 deraadt 283: {
1.13 deraadt 284: if (wnd) {
285: move(CMDLINE, 0);
286: clrtoeol();
287: refresh();
288: endwin();
289: }
1.1 deraadt 290: exit(0);
291: }
1.14 kstailey 292:
293: void
1.24 deraadt 294: sigwinch(int signo)
1.14 kstailey 295: {
1.21 deraadt 296: gotwinch = 1;
1.14 kstailey 297: }
298:
1.1 deraadt 299: void
300: error(const char *fmt, ...)
301: {
302: va_list ap;
303: char buf[255];
304: int oy, ox;
1.23 millert 305:
1.1 deraadt 306: va_start(ap, fmt);
307: if (wnd) {
308: getyx(stdscr, oy, ox);
1.5 deraadt 309: (void) vsnprintf(buf, sizeof buf, fmt, ap);
1.1 deraadt 310: clrtoeol();
311: standout();
312: mvaddstr(CMDLINE, 0, buf);
313: standend();
314: move(oy, ox);
315: refresh();
316: } else {
317: (void) vfprintf(stderr, fmt, ap);
318: fprintf(stderr, "\n");
319: }
320: va_end(ap);
321: }
322:
323: void
1.24 deraadt 324: nlisterr(struct nlist namelist[])
1.1 deraadt 325: {
326: int i, n;
327:
328: n = 0;
329: clear();
330: mvprintw(2, 10, "systat: nlist: can't find following symbols:");
331: for (i = 0;
332: namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
333: if (namelist[i].n_value == 0)
334: mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
335: move(CMDLINE, 0);
336: clrtoeol();
337: refresh();
338: endwin();
339: exit(1);
340: }