Annotation of src/usr.bin/systat/main.c, Revision 1.37
1.37 ! cnst 1: /* $OpenBSD: main.c,v 1.36 2007/04/24 06:32:08 tedu 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.37 ! cnst 43: static char rcsid[] = "$OpenBSD: main.c,v 1.36 2007/04/24 06:32:08 tedu 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.35 deraadt 56: #include <utmp.h>
1.11 millert 57: #include <stdlib.h>
1.6 deraadt 58: #include <limits.h>
1.23 millert 59: #include <stdarg.h>
1.1 deraadt 60:
61: #include "systat.h"
62: #include "extern.h"
63:
1.21 deraadt 64: double dellave;
1.1 deraadt 65:
1.21 deraadt 66: kvm_t *kd;
67: char *nlistf = NULL;
1.20 pvalchev 68: char *memf = NULL;
1.21 deraadt 69: double avenrun[3];
1.36 tedu 70: double naptime = 5.0;
1.21 deraadt 71: int verbose = 1; /* to report kvm read errs */
1.30 itojun 72: int nflag = 0;
1.35 deraadt 73: int ut, hz, stathz;
1.1 deraadt 74: char hostname[MAXHOSTNAMELEN];
75: WINDOW *wnd;
1.32 deraadt 76: int CMDLINE;
1.1 deraadt 77:
1.21 deraadt 78: WINDOW *wload; /* one line window for load average */
1.1 deraadt 79:
1.22 millert 80: static void usage(void);
1.3 deraadt 81:
1.2 deraadt 82: int
1.24 deraadt 83: main(int argc, char *argv[])
1.1 deraadt 84: {
1.6 deraadt 85: char errbuf[_POSIX2_LINE_MAX];
1.31 djm 86: gid_t gid;
1.35 deraadt 87: int ch;
88:
89: ut = open(_PATH_UTMP, O_RDONLY);
90: if (ut < 0) {
91: error("No utmp");
92: exit(1);
93: }
1.1 deraadt 94:
1.25 deraadt 95: kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
96: if (kd == NULL) {
97: error("%s", errbuf);
98: exit(1);
99: }
100:
1.31 djm 101: gid = getgid();
102: if (setresgid(gid, gid, gid) == -1)
103: err(1, "setresgid");
1.25 deraadt 104:
1.30 itojun 105: while ((ch = getopt(argc, argv, "nw:")) != -1)
1.24 deraadt 106: switch (ch) {
1.30 itojun 107: case 'n':
108: nflag = 1;
109: break;
1.21 deraadt 110: case 'w':
1.36 tedu 111:
112: naptime = strtod(optarg, NULL);
113: if (naptime < 0.09 || naptime > 1000.0)
114: errx(1, "invalid interval: %s", optarg);
1.21 deraadt 115: break;
116: default:
117: usage();
118: }
119: argc -= optind;
120: argv += optind;
1.3 deraadt 121:
1.1 deraadt 122: while (argc > 0) {
1.3 deraadt 123: if (isdigit(argv[0][0])) {
1.36 tedu 124: naptime = strtod(argv[0], NULL);
125: if (naptime < 0.09 || naptime > 1000.0)
126: naptime = 5.0;
1.3 deraadt 127: } else {
1.1 deraadt 128: struct cmdtab *p;
129:
1.3 deraadt 130: p = lookup(&argv[0][0]);
1.1 deraadt 131: if (p == (struct cmdtab *)-1)
1.3 deraadt 132: errx(1, "ambiguous request: %s", &argv[0][0]);
1.1 deraadt 133: if (p == 0)
1.3 deraadt 134: errx(1, "unknown request: %s", &argv[0][0]);
1.1 deraadt 135: curcmd = p;
136: }
1.21 deraadt 137: argc--;
138: argv++;
1.1 deraadt 139: }
1.24 deraadt 140:
1.21 deraadt 141: signal(SIGINT, sigdie);
142: siginterrupt(SIGINT, 1);
143: signal(SIGQUIT, sigdie);
144: siginterrupt(SIGQUIT, 1);
145: signal(SIGTERM, sigdie);
146: siginterrupt(SIGTERM, 1);
1.29 deraadt 147: signal(SIGTSTP, sigtstp);
148: siginterrupt(SIGTSTP, 1);
1.1 deraadt 149:
150: /*
151: * Initialize display. Load average appears in a one line
152: * window of its own. Current command's display appears in
153: * an overlapping sub-window of stdscr configured by the display
154: * routines to minimize update work by curses.
155: */
1.24 deraadt 156: if (initscr() == NULL) {
1.1 deraadt 157: warnx("couldn't initialize screen");
158: exit(0);
159: }
160:
161: CMDLINE = LINES - 1;
162: wnd = (*curcmd->c_open)();
163: if (wnd == NULL) {
164: warnx("couldn't initialize display");
1.21 deraadt 165: die();
1.1 deraadt 166: }
1.35 deraadt 167: wload = newwin(1, 0, 1, 20);
1.1 deraadt 168: if (wload == NULL) {
169: warnx("couldn't set up load average window");
1.21 deraadt 170: die();
1.1 deraadt 171: }
172: gethostname(hostname, sizeof (hostname));
1.21 deraadt 173: gethz();
1.1 deraadt 174: (*curcmd->c_init)();
175: curcmd->c_flags |= CF_INIT;
176: labels();
177:
178: dellave = 0.0;
179:
1.21 deraadt 180: signal(SIGALRM, sigdisplay);
181: siginterrupt(SIGALRM, 1);
182: signal(SIGWINCH, sigwinch);
183: siginterrupt(SIGWINCH, 1);
184: gotdisplay = 1;
1.1 deraadt 185: noecho();
186: crmode();
187: keyboard();
188: /*NOTREACHED*/
189: }
1.3 deraadt 190:
1.21 deraadt 191: void
1.24 deraadt 192: gethz(void)
1.21 deraadt 193: {
194: struct clockinfo cinf;
195: size_t size = sizeof(cinf);
196: int mib[2];
197:
198: mib[0] = CTL_KERN;
199: mib[1] = KERN_CLOCKRATE;
200: if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
201: return;
202: stathz = cinf.stathz;
203: hz = cinf.hz;
204: }
205:
1.3 deraadt 206: static void
1.24 deraadt 207: usage(void)
1.3 deraadt 208: {
1.30 itojun 209: fprintf(stderr, "usage: systat [-n] [-w wait] [display] [refresh-interval]\n");
1.3 deraadt 210: exit(1);
211: }
212:
1.1 deraadt 213:
214: void
1.24 deraadt 215: labels(void)
1.1 deraadt 216: {
1.35 deraadt 217: if (curcmd->c_flags & CF_LOADAV)
218: mvprintw(0, 2 + 4, "users Load");
1.1 deraadt 219: (*curcmd->c_label)();
220: #ifdef notdef
221: mvprintw(21, 25, "CPU usage on %s", hostname);
222: #endif
223: refresh();
224: }
225:
1.32 deraadt 226: /*ARGSUSED*/
1.1 deraadt 227: void
1.32 deraadt 228: sigdisplay(int signo)
1.1 deraadt 229: {
1.21 deraadt 230: gotdisplay = 1;
231: }
232:
233: void
234: display(void)
235: {
1.1 deraadt 236: /* Get the load average over the last minute. */
237: (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
238: (*curcmd->c_fetch)();
239: if (curcmd->c_flags & CF_LOADAV) {
1.35 deraadt 240: extern int ucount();
241: char tbuf[26];
242: time_t now;
243:
244: time(&now);
245: strlcpy(tbuf, ctime(&now), sizeof tbuf);
246:
247: putint(ucount(), 0, 2, 3);
248: putfloat(avenrun[0], 0, 2 + 17, 6, 2, 0);
249: putfloat(avenrun[1], 0, 2 + 23, 6, 2, 0);
250: putfloat(avenrun[2], 0, 2 + 29, 6, 2, 0);
251: mvaddstr(0, 2 + 53, tbuf);
1.1 deraadt 252: }
253: (*curcmd->c_refresh)();
254: if (curcmd->c_flags & CF_LOADAV)
255: wrefresh(wload);
256: wrefresh(wnd);
1.32 deraadt 257: move(CMDLINE, 0);
1.1 deraadt 258: refresh();
1.36 tedu 259: ualarm(naptime * 1000000, 0);
1.1 deraadt 260: }
261:
262: void
1.24 deraadt 263: load(void)
1.1 deraadt 264: {
265:
266: (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
267: mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
268: avenrun[0], avenrun[1], avenrun[2]);
269: clrtoeol();
270: }
271:
1.21 deraadt 272: volatile sig_atomic_t gotdie;
273: volatile sig_atomic_t gotdisplay;
274: volatile sig_atomic_t gotwinch;
1.29 deraadt 275: volatile sig_atomic_t gottstp;
1.21 deraadt 276:
1.32 deraadt 277: /*ARGSUSED*/
1.1 deraadt 278: void
1.29 deraadt 279: sigdie(int signo)
1.1 deraadt 280: {
1.21 deraadt 281: gotdie = 1;
1.29 deraadt 282: }
283:
1.32 deraadt 284: /*ARGSUSED*/
1.29 deraadt 285: void
286: sigtstp(int signo)
287: {
288: gottstp = 1;
1.21 deraadt 289: }
290:
291: void
1.24 deraadt 292: die(void)
1.21 deraadt 293: {
1.13 deraadt 294: if (wnd) {
295: move(CMDLINE, 0);
296: clrtoeol();
297: refresh();
298: endwin();
299: }
1.1 deraadt 300: exit(0);
301: }
1.14 kstailey 302:
1.32 deraadt 303: /*ARGSUSED*/
1.14 kstailey 304: void
1.24 deraadt 305: sigwinch(int signo)
1.14 kstailey 306: {
1.21 deraadt 307: gotwinch = 1;
1.14 kstailey 308: }
309:
1.1 deraadt 310: void
311: error(const char *fmt, ...)
312: {
313: va_list ap;
314: char buf[255];
315: int oy, ox;
1.23 millert 316:
1.1 deraadt 317: va_start(ap, fmt);
318: if (wnd) {
319: getyx(stdscr, oy, ox);
1.5 deraadt 320: (void) vsnprintf(buf, sizeof buf, fmt, ap);
1.1 deraadt 321: clrtoeol();
322: standout();
323: mvaddstr(CMDLINE, 0, buf);
324: standend();
325: move(oy, ox);
326: refresh();
327: } else {
328: (void) vfprintf(stderr, fmt, ap);
329: fprintf(stderr, "\n");
330: }
331: va_end(ap);
332: }
333:
334: void
1.24 deraadt 335: nlisterr(struct nlist namelist[])
1.1 deraadt 336: {
337: int i, n;
338:
339: n = 0;
340: clear();
341: mvprintw(2, 10, "systat: nlist: can't find following symbols:");
342: for (i = 0;
343: namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
344: if (namelist[i].n_value == 0)
345: mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
346: move(CMDLINE, 0);
347: clrtoeol();
348: refresh();
349: endwin();
350: exit(1);
1.35 deraadt 351: }
352:
353: /* calculate number of users on the system */
354: int
355: ucount(void)
356: {
357: int nusers = 0;
358: struct utmp utmp;
359:
360: if (ut < 0)
361: return (0);
362: lseek(ut, (off_t)0, SEEK_SET);
363: while (read(ut, &utmp, sizeof(utmp)))
364: if (utmp.ut_name[0] != '\0')
365: nusers++;
366:
367: return (nusers);
1.1 deraadt 368: }