xref: /freebsd/usr.bin/systat/main.c (revision 991554f2c46fdbc7e9acbf87fc8da089618c3a19)
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 unsigned int	delay = 5000000;	/* in microseconds */
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 	double t;
86 
87 #ifdef USE_WIDECHAR
88 	(void) setlocale(LC_ALL, "");
89 #else
90 	(void) setlocale(LC_TIME, "");
91 #endif
92 
93 	argc--, argv++;
94 	while (argc > 0) {
95 		if (argv[0][0] == '-') {
96 			struct cmdtab *p;
97 
98 			p = lookup(&argv[0][1]);
99 			if (p == (struct cmdtab *)-1)
100 				errx(1, "%s: ambiguous request", &argv[0][1]);
101 			if (p == (struct cmdtab *)0)
102 				errx(1, "%s: unknown request", &argv[0][1]);
103 			curcmd = p;
104 		} else {
105 			t = strtod(argv[0], NULL) * 1000000.0;
106 			if (t > 0 && t < (double)UINT_MAX)
107 				delay = (unsigned int)t;
108 		}
109 		argc--, argv++;
110 	}
111 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
112 	if (kd != NULL) {
113 		/*
114 		 * Try to actually read something, we may be in a jail, and
115 		 * have /dev/null opened as /dev/mem.
116 		 */
117 		if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 ||
118 		    kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) !=
119 		    sizeof(dummy)) {
120 			kvm_close(kd);
121 			kd = NULL;
122 		}
123 	}
124 	if (kd == NULL) {
125 		/*
126 		 * Maybe we are lacking permissions? Retry, this time with bogus
127 		 * devices. We can now use sysctl only.
128 		 */
129 		use_kvm = 0;
130 		kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null",
131 		    O_RDONLY, errbuf);
132 		if (kd == NULL) {
133 			error("%s", errbuf);
134 			exit(1);
135 		}
136 	}
137 	signal(SIGHUP, die);
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, 1, 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 	if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0)
163 	    || 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 	display();
175 	noecho();
176 	crmode();
177 	keyboard();
178 	/*NOTREACHED*/
179 
180 	return EXIT_SUCCESS;
181 }
182 
183 void
184 labels(void)
185 {
186 	if (curcmd->c_flags & CF_LOADAV) {
187 		mvaddstr(0, 20,
188 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
189 		mvaddstr(1, 5, "Load Average");
190 	}
191 	(*curcmd->c_label)();
192 #ifdef notdef
193 	mvprintw(21, 25, "CPU usage on %s", hostname);
194 #endif
195 	refresh();
196 }
197 
198 void
199 display(void)
200 {
201 	int i, j;
202 
203 	/* Get the load average over the last minute. */
204 	(void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
205 	(*curcmd->c_fetch)();
206 	if (curcmd->c_flags & CF_LOADAV) {
207 		j = 5.0*avenrun[0] + 0.5;
208 		dellave -= avenrun[0];
209 		if (dellave >= 0.0)
210 			c = '<';
211 		else {
212 			c = '>';
213 			dellave = -dellave;
214 		}
215 		if (dellave < 0.1)
216 			c = '|';
217 		dellave = avenrun[0];
218 		wmove(wload, 0, 0); wclrtoeol(wload);
219 		for (i = (j > 50) ? 50 : j; i > 0; i--)
220 			waddch(wload, c);
221 		if (j > 50)
222 			wprintw(wload, " %4.1f", avenrun[0]);
223 	}
224 	(*curcmd->c_refresh)();
225 	if (curcmd->c_flags & CF_LOADAV)
226 		wrefresh(wload);
227 	wrefresh(wnd);
228 	move(CMDLINE, col);
229 	refresh();
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