xref: /freebsd/usr.bin/systat/main.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
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 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifdef lint
39 static const char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 
42 #ifndef lint
43 static const char copyright[] =
44 "@(#) Copyright (c) 1980, 1992, 1993\n\
45 	The Regents of the University of California.  All rights reserved.\n";
46 #endif
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 
62 #include "systat.h"
63 #include "extern.h"
64 
65 static int     dellave;
66 
67 kvm_t *kd;
68 sig_t	sigtstpdfl;
69 double avenrun[3];
70 int     col;
71 int	naptime = 5;
72 int     verbose = 1;                    /* to report kvm read errs */
73 struct	clockinfo clkinfo;
74 double	hertz;
75 char    c;
76 char    *namp;
77 char    hostname[MAXHOSTNAMELEN];
78 WINDOW  *wnd;
79 int     CMDLINE;
80 int     use_kvm = 1;
81 
82 static	WINDOW *wload;			/* one line window for load average */
83 
84 int
85 main(int argc, char **argv)
86 {
87 	char errbuf[_POSIX2_LINE_MAX], dummy;
88 	size_t	size;
89 
90 	(void) setlocale(LC_TIME, "");
91 
92 	argc--, argv++;
93 	while (argc > 0) {
94 		if (argv[0][0] == '-') {
95 			struct cmdtab *p;
96 
97 			p = lookup(&argv[0][1]);
98 			if (p == (struct cmdtab *)-1)
99 				errx(1, "%s: ambiguous request", &argv[0][1]);
100 			if (p == (struct cmdtab *)0)
101 				errx(1, "%s: unknown request", &argv[0][1]);
102 			curcmd = p;
103 		} else {
104 			naptime = atoi(argv[0]);
105 			if (naptime <= 0)
106 				naptime = 5;
107 		}
108 		argc--, argv++;
109 	}
110 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
111 	if (kd != NULL) {
112 		/*
113 		 * Try to actually read something, we may be in a jail, and
114 		 * have /dev/null opened as /dev/mem.
115 		 */
116 		if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 ||
117 		    kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) !=
118 		    sizeof(dummy)) {
119 			kvm_close(kd);
120 			kd = NULL;
121 		}
122 	}
123 	if (kd == NULL) {
124 		/*
125 		 * Maybe we are lacking permissions? Retry, this time with bogus
126 		 * devices. We can now use sysctl only.
127 		 */
128 		use_kvm = 0;
129 		kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null",
130 		    O_RDONLY, errbuf);
131 		if (kd == NULL) {
132 			error("%s", errbuf);
133 			exit(1);
134 		}
135 	}
136 	signal(SIGINT, die);
137 	signal(SIGQUIT, die);
138 	signal(SIGTERM, die);
139 
140 	/*
141 	 * Initialize display.  Load average appears in a one line
142 	 * window of its own.  Current command's display appears in
143 	 * an overlapping sub-window of stdscr configured by the display
144 	 * routines to minimize update work by curses.
145 	 */
146 	initscr();
147 	CMDLINE = LINES - 1;
148 	wnd = (*curcmd->c_open)();
149 	if (wnd == NULL) {
150 		warnx("couldn't initialize display");
151 		die(0);
152 	}
153 	wload = newwin(1, 0, 1, 20);
154 	if (wload == NULL) {
155 		warnx("couldn't set up load average window");
156 		die(0);
157 	}
158 	gethostname(hostname, sizeof (hostname));
159 	size = sizeof(clkinfo);
160 	if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0)
161 	    || size != sizeof(clkinfo)) {
162 		error("kern.clockrate");
163 		die(0);
164 	}
165 	hertz = clkinfo.stathz;
166 	(*curcmd->c_init)();
167 	curcmd->c_flags |= CF_INIT;
168 	labels();
169 
170 	dellave = 0.0;
171 
172 	signal(SIGALRM, display);
173 	display(0);
174 	noecho();
175 	crmode();
176 	keyboard();
177 	/*NOTREACHED*/
178 
179 	return EXIT_SUCCESS;
180 }
181 
182 void
183 labels(void)
184 {
185 	if (curcmd->c_flags & CF_LOADAV) {
186 		mvaddstr(0, 20,
187 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
188 		mvaddstr(1, 5, "Load Average");
189 	}
190 	(*curcmd->c_label)();
191 #ifdef notdef
192 	mvprintw(21, 25, "CPU usage on %s", hostname);
193 #endif
194 	refresh();
195 }
196 
197 void
198 display(int signo __unused)
199 {
200 	int i, j;
201 
202 	/* Get the load average over the last minute. */
203 	(void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
204 	(*curcmd->c_fetch)();
205 	if (curcmd->c_flags & CF_LOADAV) {
206 		j = 5.0*avenrun[0] + 0.5;
207 		dellave -= avenrun[0];
208 		if (dellave >= 0.0)
209 			c = '<';
210 		else {
211 			c = '>';
212 			dellave = -dellave;
213 		}
214 		if (dellave < 0.1)
215 			c = '|';
216 		dellave = avenrun[0];
217 		wmove(wload, 0, 0); wclrtoeol(wload);
218 		for (i = (j > 50) ? 50 : j; i > 0; i--)
219 			waddch(wload, c);
220 		if (j > 50)
221 			wprintw(wload, " %4.1f", avenrun[0]);
222 	}
223 	(*curcmd->c_refresh)();
224 	if (curcmd->c_flags & CF_LOADAV)
225 		wrefresh(wload);
226 	wrefresh(wnd);
227 	move(CMDLINE, col);
228 	refresh();
229 	alarm(naptime);
230 }
231 
232 void
233 load(void)
234 {
235 
236 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
237 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
238 	    avenrun[0], avenrun[1], avenrun[2]);
239 	clrtoeol();
240 }
241 
242 void
243 die(int signo __unused)
244 {
245 	move(CMDLINE, 0);
246 	clrtoeol();
247 	refresh();
248 	endwin();
249 	exit(0);
250 }
251 
252 #include <stdarg.h>
253 
254 void
255 error(const char *fmt, ...)
256 {
257 	va_list ap;
258 	char buf[255];
259 	int oy, ox;
260 
261 	va_start(ap, fmt);
262 	if (wnd) {
263 		getyx(stdscr, oy, ox);
264 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
265 		clrtoeol();
266 		standout();
267 		mvaddstr(CMDLINE, 0, buf);
268 		standend();
269 		move(oy, ox);
270 		refresh();
271 	} else {
272 		(void) vfprintf(stderr, fmt, ap);
273 		fprintf(stderr, "\n");
274 	}
275 	va_end(ap);
276 }
277 
278 void
279 nlisterr(struct nlist n_list[])
280 {
281 	int i, n;
282 
283 	n = 0;
284 	clear();
285 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
286 	for (i = 0;
287 	    n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
288 		if (n_list[i].n_value == 0)
289 			mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);
290 	move(CMDLINE, 0);
291 	clrtoeol();
292 	refresh();
293 	endwin();
294 	exit(1);
295 }
296