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