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