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