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