Annotation of src/usr.bin/systat/main.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: main.c,v 1.8 1996/05/10 23:16:36 thorpej 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.4 ! deraadt 47: static char rcsid[] = "$OpenBSD: main.c,v 1.8 1996/05/10 23:16:36 thorpej Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/param.h>
51:
52: #include <err.h>
53: #include <nlist.h>
54: #include <signal.h>
55: #include <stdio.h>
56: #include <string.h>
1.3 deraadt 57: #include <unistd.h>
1.1 deraadt 58:
59: #include "systat.h"
60: #include "extern.h"
61:
62: static struct nlist namelist[] = {
63: #define X_FIRST 0
64: #define X_HZ 0
65: { "_hz" },
66: #define X_STATHZ 1
67: { "_stathz" },
68: { "" }
69: };
70: static int dellave;
71:
72: kvm_t *kd;
1.3 deraadt 73: char *memf = NULL;
74: char *nlistf = NULL;
1.1 deraadt 75: sig_t sigtstpdfl;
76: double avenrun[3];
77: int col;
78: int naptime = 5;
79: int verbose = 1; /* to report kvm read errs */
80: int hz, stathz;
81: char c;
82: char *namp;
83: char hostname[MAXHOSTNAMELEN];
84: WINDOW *wnd;
85: int CMDLINE;
86:
87: static WINDOW *wload; /* one line window for load average */
88:
1.3 deraadt 89: static void usage();
90:
1.2 deraadt 91: int
1.1 deraadt 92: main(argc, argv)
93: int argc;
94: char **argv;
95: {
1.3 deraadt 96: int ch;
1.1 deraadt 97: char errbuf[80];
98:
1.3 deraadt 99: while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
100: switch(ch) {
101: case 'M':
102: memf = optarg;
103: break;
104: case 'N':
105: nlistf = optarg;
106: break;
107: case 'w':
108: if ((naptime = atoi(optarg)) <= 0)
109: errx(1, "interval <= 0.");
110: break;
111: case '?':
112: default:
113: usage();
114: }
115: argc -= optind;
116: argv += optind;
117: /*
118: * Discard setgid privileges if not the running kernel so that bad
119: * guys can't print interesting stuff from kernel memory.
120: */
121: if (nlistf != NULL || memf != NULL)
122: setgid(getgid());
123:
1.1 deraadt 124: while (argc > 0) {
1.3 deraadt 125: if (isdigit(argv[0][0])) {
126: naptime = atoi(argv[0]);
127: if (naptime <= 0)
128: naptime = 5;
129: } else {
1.1 deraadt 130: struct cmdtab *p;
131:
1.3 deraadt 132: p = lookup(&argv[0][0]);
1.1 deraadt 133: if (p == (struct cmdtab *)-1)
1.3 deraadt 134: errx(1, "ambiguous request: %s", &argv[0][0]);
1.1 deraadt 135: if (p == 0)
1.3 deraadt 136: errx(1, "unknown request: %s", &argv[0][0]);
1.1 deraadt 137: curcmd = p;
138: }
139: argc--, argv++;
140: }
1.3 deraadt 141: kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
1.1 deraadt 142: if (kd == NULL) {
143: error("%s", errbuf);
144: exit(1);
145: }
146: if (kvm_nlist(kd, namelist)) {
147: nlisterr(namelist);
148: exit(1);
149: }
150: if (namelist[X_FIRST].n_type == 0)
151: errx(1, "couldn't read namelist");
152: signal(SIGINT, die);
153: signal(SIGQUIT, die);
154: signal(SIGTERM, die);
155:
156: /*
157: * Initialize display. Load average appears in a one line
158: * window of its own. Current command's display appears in
159: * an overlapping sub-window of stdscr configured by the display
160: * routines to minimize update work by curses.
161: */
162: if (initscr() == NULL)
163: {
164: warnx("couldn't initialize screen");
165: exit(0);
166: }
167:
168: CMDLINE = LINES - 1;
169: wnd = (*curcmd->c_open)();
170: if (wnd == NULL) {
171: warnx("couldn't initialize display");
172: die(0);
173: }
174: wload = newwin(1, 0, 3, 20);
175: if (wload == NULL) {
176: warnx("couldn't set up load average window");
177: die(0);
178: }
179: gethostname(hostname, sizeof (hostname));
180: NREAD(X_HZ, &hz, LONG);
181: NREAD(X_STATHZ, &stathz, LONG);
182: (*curcmd->c_init)();
183: curcmd->c_flags |= CF_INIT;
184: labels();
185:
186: dellave = 0.0;
187:
188: signal(SIGALRM, display);
189: display(0);
190: noecho();
191: crmode();
192: keyboard();
193: /*NOTREACHED*/
194: }
1.3 deraadt 195:
196: static void
197: usage()
198: {
199: fprintf(stderr, "usage: systat [-M core] [-N system] [-w wait]\n");
200: exit(1);
201: }
202:
1.1 deraadt 203:
204: void
205: labels()
206: {
207: if (curcmd->c_flags & CF_LOADAV) {
208: mvaddstr(2, 20,
209: "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
210: mvaddstr(3, 5, "Load Average");
211: }
212: (*curcmd->c_label)();
213: #ifdef notdef
214: mvprintw(21, 25, "CPU usage on %s", hostname);
215: #endif
216: refresh();
217: }
218:
219: void
220: display(signo)
221: int signo;
222: {
223: register int i, j;
224:
225: /* Get the load average over the last minute. */
226: (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
227: (*curcmd->c_fetch)();
228: if (curcmd->c_flags & CF_LOADAV) {
229: j = 5.0*avenrun[0] + 0.5;
230: dellave -= avenrun[0];
231: if (dellave >= 0.0)
232: c = '<';
233: else {
234: c = '>';
235: dellave = -dellave;
236: }
237: if (dellave < 0.1)
238: c = '|';
239: dellave = avenrun[0];
240: wmove(wload, 0, 0); wclrtoeol(wload);
241: for (i = (j > 50) ? 50 : j; i > 0; i--)
242: waddch(wload, c);
243: if (j > 50)
244: wprintw(wload, " %4.1f", avenrun[0]);
245: }
246: (*curcmd->c_refresh)();
247: if (curcmd->c_flags & CF_LOADAV)
248: wrefresh(wload);
249: wrefresh(wnd);
250: move(CMDLINE, col);
251: refresh();
252: alarm(naptime);
253: }
254:
255: void
256: load()
257: {
258:
259: (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
260: mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
261: avenrun[0], avenrun[1], avenrun[2]);
262: clrtoeol();
263: }
264:
265: void
266: die(signo)
267: int signo;
268: {
269: move(CMDLINE, 0);
270: clrtoeol();
271: refresh();
272: endwin();
273: exit(0);
274: }
275:
276: #if __STDC__
277: #include <stdarg.h>
278: #else
279: #include <varargs.h>
280: #endif
281:
282: #if __STDC__
283: void
284: error(const char *fmt, ...)
285: #else
286: void
287: error(fmt, va_alist)
288: char *fmt;
289: va_dcl
290: #endif
291: {
292: va_list ap;
293: char buf[255];
294: int oy, ox;
295: #if __STDC__
296: va_start(ap, fmt);
297: #else
298: va_start(ap);
299: #endif
300:
301: if (wnd) {
302: getyx(stdscr, oy, ox);
303: (void) vsprintf(buf, fmt, ap);
304: clrtoeol();
305: standout();
306: mvaddstr(CMDLINE, 0, buf);
307: standend();
308: move(oy, ox);
309: refresh();
310: } else {
311: (void) vfprintf(stderr, fmt, ap);
312: fprintf(stderr, "\n");
313: }
314: va_end(ap);
315: }
316:
317: void
318: nlisterr(namelist)
319: struct nlist namelist[];
320: {
321: int i, n;
322:
323: n = 0;
324: clear();
325: mvprintw(2, 10, "systat: nlist: can't find following symbols:");
326: for (i = 0;
327: namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
328: if (namelist[i].n_value == 0)
329: mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
330: move(CMDLINE, 0);
331: clrtoeol();
332: refresh();
333: endwin();
334: exit(1);
335: }