xref: /freebsd/usr.bin/systat/main.c (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
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 	(void) setlocale(LC_ALL, "");
88 
89 	argc--, argv++;
90 	while (argc > 0) {
91 		if (argv[0][0] == '-') {
92 			struct cmdtab *p;
93 
94 			p = lookup(&argv[0][1]);
95 			if (p == (struct cmdtab *)-1)
96 				errx(1, "%s: ambiguous request", &argv[0][1]);
97 			if (p == (struct cmdtab *)0)
98 				errx(1, "%s: unknown request", &argv[0][1]);
99 			curcmd = p;
100 		} else {
101 			t = strtod(argv[0], NULL) * 1000000.0;
102 			if (t > 0 && t < (double)UINT_MAX)
103 				delay = (unsigned int)t;
104 		}
105 		argc--, argv++;
106 	}
107 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
108 	if (kd != NULL) {
109 		/*
110 		 * Try to actually read something, we may be in a jail, and
111 		 * have /dev/null opened as /dev/mem.
112 		 */
113 		if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 ||
114 		    kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) !=
115 		    sizeof(dummy)) {
116 			kvm_close(kd);
117 			kd = NULL;
118 		}
119 	}
120 	if (kd == NULL) {
121 		/*
122 		 * Maybe we are lacking permissions? Retry, this time with bogus
123 		 * devices. We can now use sysctl only.
124 		 */
125 		use_kvm = 0;
126 		kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null",
127 		    O_RDONLY, errbuf);
128 		if (kd == NULL) {
129 			error("%s", errbuf);
130 			exit(1);
131 		}
132 	}
133 	signal(SIGHUP, die);
134 	signal(SIGINT, die);
135 	signal(SIGQUIT, die);
136 	signal(SIGTERM, die);
137 
138 	/*
139 	 * Initialize display.  Load average appears in a one line
140 	 * window of its own.  Current command's display appears in
141 	 * an overlapping sub-window of stdscr configured by the display
142 	 * routines to minimize update work by curses.
143 	 */
144 	initscr();
145 	CMDLINE = LINES - 1;
146 	wnd = (*curcmd->c_open)();
147 	if (wnd == NULL) {
148 		warnx("couldn't initialize display");
149 		die(0);
150 	}
151 	wload = newwin(1, 0, 1, 20);
152 	if (wload == NULL) {
153 		warnx("couldn't set up load average window");
154 		die(0);
155 	}
156 	gethostname(hostname, sizeof (hostname));
157 	size = sizeof(clkinfo);
158 	if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0)
159 	    || size != sizeof(clkinfo)) {
160 		error("kern.clockrate");
161 		die(0);
162 	}
163 	hertz = clkinfo.stathz;
164 	(*curcmd->c_init)();
165 	curcmd->c_flags |= CF_INIT;
166 	labels();
167 
168 	dellave = 0.0;
169 
170 	display();
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(void)
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 }
227 
228 void
229 load(void)
230 {
231 
232 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
233 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
234 	    avenrun[0], avenrun[1], avenrun[2]);
235 	clrtoeol();
236 }
237 
238 void
239 die(int signo __unused)
240 {
241 	move(CMDLINE, 0);
242 	clrtoeol();
243 	refresh();
244 	endwin();
245 	exit(0);
246 }
247 
248 #include <stdarg.h>
249 
250 void
251 error(const char *fmt, ...)
252 {
253 	va_list ap;
254 	char buf[255];
255 	int oy, ox;
256 
257 	va_start(ap, fmt);
258 	if (wnd) {
259 		getyx(stdscr, oy, ox);
260 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
261 		clrtoeol();
262 		standout();
263 		mvaddstr(CMDLINE, 0, buf);
264 		standend();
265 		move(oy, ox);
266 		refresh();
267 	} else {
268 		(void) vfprintf(stderr, fmt, ap);
269 		fprintf(stderr, "\n");
270 	}
271 	va_end(ap);
272 }
273 
274 void
275 nlisterr(struct nlist n_list[])
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 	    n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
284 		if (n_list[i].n_value == 0)
285 			mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);
286 	move(CMDLINE, 0);
287 	clrtoeol();
288 	refresh();
289 	endwin();
290 	exit(1);
291 }
292