Annotation of src/usr.bin/systat/main.c, Revision 1.29
1.29 ! deraadt 1: /* $OpenBSD: main.c,v 1.28 2003/12/17 20:58:39 jmc 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.29 ! deraadt 43: static char rcsid[] = "$OpenBSD: main.c,v 1.28 2003/12/17 20:58:39 jmc 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.29 ! deraadt 136: signal(SIGTSTP, sigtstp);
! 137: siginterrupt(SIGTSTP, 1);
1.1 deraadt 138:
139: /*
140: * Initialize display. Load average appears in a one line
141: * window of its own. Current command's display appears in
142: * an overlapping sub-window of stdscr configured by the display
143: * routines to minimize update work by curses.
144: */
1.24 deraadt 145: if (initscr() == NULL) {
1.1 deraadt 146: warnx("couldn't initialize screen");
147: exit(0);
148: }
149:
150: CMDLINE = LINES - 1;
151: wnd = (*curcmd->c_open)();
152: if (wnd == NULL) {
153: warnx("couldn't initialize display");
1.21 deraadt 154: die();
1.1 deraadt 155: }
156: wload = newwin(1, 0, 3, 20);
157: if (wload == NULL) {
158: warnx("couldn't set up load average window");
1.21 deraadt 159: die();
1.1 deraadt 160: }
161: gethostname(hostname, sizeof (hostname));
1.21 deraadt 162: gethz();
1.1 deraadt 163: (*curcmd->c_init)();
164: curcmd->c_flags |= CF_INIT;
165: labels();
166:
167: dellave = 0.0;
168:
1.21 deraadt 169: signal(SIGALRM, sigdisplay);
170: siginterrupt(SIGALRM, 1);
171: signal(SIGWINCH, sigwinch);
172: siginterrupt(SIGWINCH, 1);
173: gotdisplay = 1;
1.1 deraadt 174: noecho();
175: crmode();
176: keyboard();
177: /*NOTREACHED*/
178: }
1.3 deraadt 179:
1.21 deraadt 180: void
1.24 deraadt 181: gethz(void)
1.21 deraadt 182: {
183: struct clockinfo cinf;
184: size_t size = sizeof(cinf);
185: int mib[2];
186:
187: mib[0] = CTL_KERN;
188: mib[1] = KERN_CLOCKRATE;
189: if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
190: return;
191: stathz = cinf.stathz;
192: hz = cinf.hz;
193: }
194:
1.3 deraadt 195: static void
1.24 deraadt 196: usage(void)
1.3 deraadt 197: {
1.28 jmc 198: fprintf(stderr, "usage: systat [-w wait] [display] [refresh-interval]\n");
1.3 deraadt 199: exit(1);
200: }
201:
1.1 deraadt 202:
203: void
1.24 deraadt 204: labels(void)
1.1 deraadt 205: {
206: if (curcmd->c_flags & CF_LOADAV) {
207: mvaddstr(2, 20,
208: "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
209: mvaddstr(3, 5, "Load Average");
210: }
211: (*curcmd->c_label)();
212: #ifdef notdef
213: mvprintw(21, 25, "CPU usage on %s", hostname);
214: #endif
215: refresh();
216: }
217:
218: void
1.21 deraadt 219: sigdisplay(signo)
1.1 deraadt 220: int signo;
221: {
1.21 deraadt 222: gotdisplay = 1;
223: }
224:
225: void
226: display(void)
227: {
1.17 mpech 228: int i, j;
1.1 deraadt 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];
1.21 deraadt 245: wmove(wload, 0, 0);
246: wclrtoeol(wload);
1.1 deraadt 247: for (i = (j > 50) ? 50 : j; i > 0; i--)
248: waddch(wload, c);
249: if (j > 50)
250: wprintw(wload, " %4.1f", avenrun[0]);
251: }
252: (*curcmd->c_refresh)();
253: if (curcmd->c_flags & CF_LOADAV)
254: wrefresh(wload);
255: wrefresh(wnd);
256: move(CMDLINE, col);
257: refresh();
258: alarm(naptime);
259: }
260:
261: void
1.24 deraadt 262: load(void)
1.1 deraadt 263: {
264:
265: (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
266: mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
267: avenrun[0], avenrun[1], avenrun[2]);
268: clrtoeol();
269: }
270:
1.21 deraadt 271: volatile sig_atomic_t gotdie;
272: volatile sig_atomic_t gotdisplay;
273: volatile sig_atomic_t gotwinch;
1.29 ! deraadt 274: volatile sig_atomic_t gottstp;
1.21 deraadt 275:
1.1 deraadt 276: void
1.29 ! deraadt 277: sigdie(int signo)
1.1 deraadt 278: {
1.21 deraadt 279: gotdie = 1;
1.29 ! deraadt 280: }
! 281:
! 282: void
! 283: sigtstp(int signo)
! 284: {
! 285: gottstp = 1;
1.21 deraadt 286: }
287:
288: void
1.24 deraadt 289: die(void)
1.21 deraadt 290: {
1.13 deraadt 291: if (wnd) {
292: move(CMDLINE, 0);
293: clrtoeol();
294: refresh();
295: endwin();
296: }
1.1 deraadt 297: exit(0);
298: }
1.14 kstailey 299:
300: void
1.24 deraadt 301: sigwinch(int signo)
1.14 kstailey 302: {
1.21 deraadt 303: gotwinch = 1;
1.14 kstailey 304: }
305:
1.1 deraadt 306: void
307: error(const char *fmt, ...)
308: {
309: va_list ap;
310: char buf[255];
311: int oy, ox;
1.23 millert 312:
1.1 deraadt 313: va_start(ap, fmt);
314: if (wnd) {
315: getyx(stdscr, oy, ox);
1.5 deraadt 316: (void) vsnprintf(buf, sizeof buf, fmt, ap);
1.1 deraadt 317: clrtoeol();
318: standout();
319: mvaddstr(CMDLINE, 0, buf);
320: standend();
321: move(oy, ox);
322: refresh();
323: } else {
324: (void) vfprintf(stderr, fmt, ap);
325: fprintf(stderr, "\n");
326: }
327: va_end(ap);
328: }
329:
330: void
1.24 deraadt 331: nlisterr(struct nlist namelist[])
1.1 deraadt 332: {
333: int i, n;
334:
335: n = 0;
336: clear();
337: mvprintw(2, 10, "systat: nlist: can't find following symbols:");
338: for (i = 0;
339: namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
340: if (namelist[i].n_value == 0)
341: mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
342: move(CMDLINE, 0);
343: clrtoeol();
344: refresh();
345: endwin();
346: exit(1);
347: }