xref: /freebsd/usr.bin/systat/main.c (revision dc5c110e4a9f03493c2cf4b2b2d0658d5a38f70d)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * 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
35 static 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 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 
46 #include <nlist.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include "systat.h"
50 #include "extern.h"
51 
52 static struct nlist namelist[] = {
53 #define X_FIRST		0
54 #define	X_HZ		0
55 	{ "_hz" },
56 #define	X_STATHZ		1
57 	{ "_stathz" },
58 	{ "" }
59 };
60 static int     dellave;
61 
62 kvm_t *kd;
63 sig_t	sigtstpdfl;
64 double avenrun[3];
65 int     col;
66 int	naptime = 5;
67 int     verbose = 1;                    /* to report kvm read errs */
68 int     hz, stathz;
69 char    c;
70 char    *namp;
71 char    hostname[MAXHOSTNAMELEN];
72 WINDOW  *wnd;
73 int     CMDLINE;
74 
75 static	WINDOW *wload;			/* one line window for load average */
76 
77 void
78 main(argc, argv)
79 	int argc;
80 	char **argv;
81 {
82 	char errbuf[80];
83 
84 	argc--, argv++;
85 	while (argc > 0) {
86 		if (argv[0][0] == '-') {
87 			struct cmdtab *p;
88 
89 			p = lookup(&argv[0][1]);
90 			if (p == (struct cmdtab *)-1) {
91 				fprintf(stderr, "%s: ambiguous request\n",
92 				    &argv[0][1]);
93 				exit(1);
94 			}
95 			if (p == (struct cmdtab *)0) {
96 				fprintf(stderr, "%s: unknown request\n",
97 				    &argv[0][1]);
98 				exit(1);
99 			}
100 			curcmd = p;
101 		} else {
102 			naptime = atoi(argv[0]);
103 			if (naptime <= 0)
104 				naptime = 5;
105 		}
106 		argc--, argv++;
107 	}
108 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
109 	if (kd == NULL) {
110 		error("%s", errbuf);
111 		exit(1);
112 	}
113 	if (kvm_nlist(kd, namelist)) {
114 		nlisterr(namelist);
115 		exit(1);
116 	}
117 	if (namelist[X_FIRST].n_type == 0) {
118 		fprintf(stderr, "couldn't read namelist.\n");
119 		exit(1);
120 	}
121 	signal(SIGINT, die);
122 	signal(SIGQUIT, die);
123 	signal(SIGTERM, die);
124 
125 	/*
126 	 * Initialize display.  Load average appears in a one line
127 	 * window of its own.  Current command's display appears in
128 	 * an overlapping sub-window of stdscr configured by the display
129 	 * routines to minimize update work by curses.
130 	 */
131 	initscr();
132 	CMDLINE = LINES - 1;
133 	wnd = (*curcmd->c_open)();
134 	if (wnd == NULL) {
135 		fprintf(stderr, "Couldn't initialize display.\n");
136 		die(0);
137 	}
138 	wload = newwin(1, 0, 3, 20);
139 	if (wload == NULL) {
140 		fprintf(stderr, "Couldn't set up load average window.\n");
141 		die(0);
142 	}
143 	gethostname(hostname, sizeof (hostname));
144 	NREAD(X_HZ, &hz, LONG);
145 	NREAD(X_STATHZ, &stathz, LONG);
146 	(*curcmd->c_init)();
147 	curcmd->c_flags |= CF_INIT;
148 	labels();
149 
150 	dellave = 0.0;
151 
152 	signal(SIGALRM, display);
153 	display(0);
154 	noecho();
155 	crmode();
156 	keyboard();
157 	/*NOTREACHED*/
158 }
159 
160 void
161 labels()
162 {
163 	if (curcmd->c_flags & CF_LOADAV) {
164 		mvaddstr(2, 20,
165 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
166 		mvaddstr(3, 5, "Load Average");
167 	}
168 	(*curcmd->c_label)();
169 #ifdef notdef
170 	mvprintw(21, 25, "CPU usage on %s", hostname);
171 #endif
172 	refresh();
173 }
174 
175 void
176 display(signo)
177 	int signo;
178 {
179 	register int i, j;
180 
181 	/* Get the load average over the last minute. */
182 	(void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
183 	(*curcmd->c_fetch)();
184 	if (curcmd->c_flags & CF_LOADAV) {
185 		j = 5.0*avenrun[0] + 0.5;
186 		dellave -= avenrun[0];
187 		if (dellave >= 0.0)
188 			c = '<';
189 		else {
190 			c = '>';
191 			dellave = -dellave;
192 		}
193 		if (dellave < 0.1)
194 			c = '|';
195 		dellave = avenrun[0];
196 		wmove(wload, 0, 0); wclrtoeol(wload);
197 		for (i = (j > 50) ? 50 : j; i > 0; i--)
198 			waddch(wload, c);
199 		if (j > 50)
200 			wprintw(wload, " %4.1f", avenrun[0]);
201 	}
202 	(*curcmd->c_refresh)();
203 	if (curcmd->c_flags & CF_LOADAV)
204 		wrefresh(wload);
205 	wrefresh(wnd);
206 	move(CMDLINE, col);
207 	refresh();
208 	alarm(naptime);
209 }
210 
211 void
212 load()
213 {
214 
215 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
216 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
217 	    avenrun[0], avenrun[1], avenrun[2]);
218 	clrtoeol();
219 }
220 
221 void
222 die(signo)
223 	int signo;
224 {
225 	move(CMDLINE, 0);
226 	clrtoeol();
227 	refresh();
228 	endwin();
229 	exit(0);
230 }
231 
232 #if __STDC__
233 #include <stdarg.h>
234 #else
235 #include <varargs.h>
236 #endif
237 
238 #if __STDC__
239 void
240 error(const char *fmt, ...)
241 #else
242 void
243 error(fmt, va_alist)
244 	char *fmt;
245 	va_dcl
246 #endif
247 {
248 	va_list ap;
249 	char buf[255];
250 	int oy, ox;
251 #if __STDC__
252 	va_start(ap, fmt);
253 #else
254 	va_start(ap);
255 #endif
256 
257 	if (wnd) {
258 		getyx(stdscr, oy, ox);
259 		(void) vsprintf(buf, fmt, ap);
260 		clrtoeol();
261 		standout();
262 		mvaddstr(CMDLINE, 0, buf);
263 		standend();
264 		move(oy, ox);
265 		refresh();
266 	} else {
267 		(void) vfprintf(stderr, fmt, ap);
268 		fprintf(stderr, "\n");
269 	}
270 	va_end(ap);
271 }
272 
273 void
274 nlisterr(namelist)
275 	struct nlist namelist[];
276 {
277 	int i, n;
278 
279 	n = 0;
280 	clear();
281 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
282 	for (i = 0;
283 	    namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
284 		if (namelist[i].n_value == 0)
285 			mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
286 	move(CMDLINE, 0);
287 	clrtoeol();
288 	refresh();
289 	endwin();
290 	exit(1);
291 }
292