main.c revision 77205
1251843Sbapt/*-
2251843Sbapt * Copyright (c) 1980, 1992, 1993
3217309Snwhitehorn *	The Regents of the University of California.  All rights reserved.
4217309Snwhitehorn *
5220749Snwhitehorn * Redistribution and use in source and binary forms, with or without
6220749Snwhitehorn * modification, are permitted provided that the following conditions
7220749Snwhitehorn * are met:
8220749Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9217309Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
11217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
12217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
13217309Snwhitehorn * 3. All advertising materials mentioning features or use of this software
14217309Snwhitehorn *    must display the following acknowledgement:
15217309Snwhitehorn *	This product includes software developed by the University of
16217309Snwhitehorn *	California, Berkeley and its contributors.
17251843Sbapt * 4. Neither the name of the University nor the names of its contributors
18251843Sbapt *    may be used to endorse or promote products derived from this software
19251843Sbapt *    without specific prior written permission.
20251843Sbapt *
21251843Sbapt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22251843Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23251843Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1980, 1992, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/usr.bin/systat/main.c 77205 2001-05-25 23:10:27Z tmm $";
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/time.h>
50#include <sys/sysctl.h>
51
52#include <err.h>
53#include <limits.h>
54#include <locale.h>
55#include <nlist.h>
56#include <paths.h>
57#include <signal.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <unistd.h>
61#include "systat.h"
62#include "extern.h"
63
64static int     dellave;
65
66kvm_t *kd;
67sig_t	sigtstpdfl;
68double avenrun[3];
69int     col;
70int	naptime = 5;
71int     verbose = 1;                    /* to report kvm read errs */
72struct	clockinfo clkinfo;
73double	hertz;
74char    c;
75char    *namp;
76char    hostname[MAXHOSTNAMELEN];
77WINDOW  *wnd;
78int     CMDLINE;
79int     use_kvm = 1;
80
81static	WINDOW *wload;			/* one line window for load average */
82
83int
84main(argc, argv)
85	int argc;
86	char **argv;
87{
88	char errbuf[_POSIX2_LINE_MAX], dummy;
89	size_t	size;
90	int	err;
91
92	(void) setlocale(LC_TIME, "");
93
94	argc--, argv++;
95	while (argc > 0) {
96		if (argv[0][0] == '-') {
97			struct cmdtab *p;
98
99			p = lookup(&argv[0][1]);
100			if (p == (struct cmdtab *)-1)
101				errx(1, "%s: ambiguous request", &argv[0][1]);
102			if (p == (struct cmdtab *)0)
103				errx(1, "%s: unknown request", &argv[0][1]);
104			curcmd = p;
105		} else {
106			naptime = atoi(argv[0]);
107			if (naptime <= 0)
108				naptime = 5;
109		}
110		argc--, argv++;
111	}
112	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
113	if (kd != NULL) {
114		/*
115		 * Try to actually read something, we may be in a jail, and
116		 * have /dev/null opened as /dev/mem.
117		 */
118		if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 ||
119		    kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) !=
120		    sizeof(dummy)) {
121			kvm_close(kd);
122			kd = NULL;
123		}
124	}
125	if (kd == NULL) {
126		/*
127		 * Maybe we are lacking permissions? Retry, this time with bogus
128		 * devices. We can now use sysctl only.
129		 */
130		use_kvm = 0;
131		kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null",
132		    O_RDONLY, errbuf);
133		if (kd == NULL) {
134			error("%s", errbuf);
135			exit(1);
136		}
137	}
138	signal(SIGINT, die);
139	signal(SIGQUIT, die);
140	signal(SIGTERM, die);
141
142	/*
143	 * Initialize display.  Load average appears in a one line
144	 * window of its own.  Current command's display appears in
145	 * an overlapping sub-window of stdscr configured by the display
146	 * routines to minimize update work by curses.
147	 */
148	initscr();
149	CMDLINE = LINES - 1;
150	wnd = (*curcmd->c_open)();
151	if (wnd == NULL) {
152		warnx("couldn't initialize display");
153		die(0);
154	}
155	wload = newwin(1, 0, 3, 20);
156	if (wload == NULL) {
157		warnx("couldn't set up load average window");
158		die(0);
159	}
160	gethostname(hostname, sizeof (hostname));
161	size = sizeof(clkinfo);
162	err = sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0);
163	if (err != 0 || size != sizeof(clkinfo)) {
164		error("kern.clockrate");
165		die(0);
166	}
167	hertz = clkinfo.stathz;
168	(*curcmd->c_init)();
169	curcmd->c_flags |= CF_INIT;
170	labels();
171
172	dellave = 0.0;
173
174	signal(SIGALRM, display);
175	display(0);
176	noecho();
177	crmode();
178	keyboard();
179	/*NOTREACHED*/
180
181	return EXIT_SUCCESS;
182}
183
184void
185labels()
186{
187	if (curcmd->c_flags & CF_LOADAV) {
188		mvaddstr(2, 20,
189		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
190		mvaddstr(3, 5, "Load Average");
191	}
192	(*curcmd->c_label)();
193#ifdef notdef
194	mvprintw(21, 25, "CPU usage on %s", hostname);
195#endif
196	refresh();
197}
198
199void
200display(signo)
201	int signo;
202{
203	register int i, j;
204
205	/* Get the load average over the last minute. */
206	(void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
207	(*curcmd->c_fetch)();
208	if (curcmd->c_flags & CF_LOADAV) {
209		j = 5.0*avenrun[0] + 0.5;
210		dellave -= avenrun[0];
211		if (dellave >= 0.0)
212			c = '<';
213		else {
214			c = '>';
215			dellave = -dellave;
216		}
217		if (dellave < 0.1)
218			c = '|';
219		dellave = avenrun[0];
220		wmove(wload, 0, 0); wclrtoeol(wload);
221		for (i = (j > 50) ? 50 : j; i > 0; i--)
222			waddch(wload, c);
223		if (j > 50)
224			wprintw(wload, " %4.1f", avenrun[0]);
225	}
226	(*curcmd->c_refresh)();
227	if (curcmd->c_flags & CF_LOADAV)
228		wrefresh(wload);
229	wrefresh(wnd);
230	move(CMDLINE, col);
231	refresh();
232	alarm(naptime);
233}
234
235void
236load()
237{
238
239	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
240	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
241	    avenrun[0], avenrun[1], avenrun[2]);
242	clrtoeol();
243}
244
245void
246die(signo)
247	int signo;
248{
249	move(CMDLINE, 0);
250	clrtoeol();
251	refresh();
252	endwin();
253	exit(0);
254}
255
256#if __STDC__
257#include <stdarg.h>
258#else
259#include <varargs.h>
260#endif
261
262#if __STDC__
263void
264error(const char *fmt, ...)
265#else
266void
267error(fmt, va_alist)
268	char *fmt;
269	va_dcl
270#endif
271{
272	va_list ap;
273	char buf[255];
274	int oy, ox;
275#if __STDC__
276	va_start(ap, fmt);
277#else
278	va_start(ap);
279#endif
280
281	if (wnd) {
282		getyx(stdscr, oy, ox);
283		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
284		clrtoeol();
285		standout();
286		mvaddstr(CMDLINE, 0, buf);
287		standend();
288		move(oy, ox);
289		refresh();
290	} else {
291		(void) vfprintf(stderr, fmt, ap);
292		fprintf(stderr, "\n");
293	}
294	va_end(ap);
295}
296
297void
298nlisterr(namelist)
299	struct nlist namelist[];
300{
301	int i, n;
302
303	n = 0;
304	clear();
305	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
306	for (i = 0;
307	    namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
308		if (namelist[i].n_value == 0)
309			mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
310	move(CMDLINE, 0);
311	clrtoeol();
312	refresh();
313	endwin();
314	exit(1);
315}
316