xref: /netbsd-src/usr.bin/systat/main.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: main.c,v 1.33 2003/07/05 08:05:51 dsl Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #if 0
41 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
42 #endif
43 __RCSID("$NetBSD: main.c,v 1.33 2003/07/05 08:05:51 dsl Exp $");
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <termios.h>
58 #include <sys/ioctl.h>
59 
60 #include "systat.h"
61 #include "extern.h"
62 
63 static struct nlist namelist[] = {
64 #define X_FIRST		0
65 #define	X_HZ		0
66 	{ "_hz" },
67 #define	X_STATHZ		1
68 	{ "_stathz" },
69 #define	X_MAXSLP		2
70 	{ "_maxslp" },
71 	{ "" }
72 };
73 static int     dellave;
74 
75 kvm_t *kd;
76 char	*memf = NULL;
77 char	*nlistf = NULL;
78 sig_t	sigtstpdfl;
79 double avenrun[3];
80 int     col;
81 int	naptime = 5;
82 int     verbose = 1;                    /* to report kvm read errs */
83 int     hz, stathz, maxslp;
84 char    c;
85 char    *namp;
86 char    hostname[MAXHOSTNAMELEN + 1];
87 WINDOW  *wnd;
88 int     CMDLINE;
89 int     turns = 2;	/* stay how many refresh-turns in 'all' mode? */
90 int     allflag;
91 int     allcounter;
92 
93 static	WINDOW *wload;			/* one line window for load average */
94 
95 static void (*sv_stop_handler)(int);
96 
97 static void stop(int);
98 static void usage(void);
99 int main(int, char **);
100 
101 gid_t egid; /* XXX needed by initiostat() and initkre() */
102 
103 int
104 main(int argc, char **argv)
105 {
106 	int ch;
107 	char errbuf[_POSIX2_LINE_MAX];
108 
109 	egid = getegid();
110 	(void)setegid(getgid());
111 
112 	while ((ch = getopt(argc, argv, "M:N:nw:t:")) != -1)
113 		switch(ch) {
114 		case 'M':
115 			memf = optarg;
116 			break;
117 		case 'N':
118 			nlistf = optarg;
119 			break;
120 		case 'n':
121 			nflag = !nflag;
122 			break;
123 		case 'w':
124 			if ((naptime = atoi(optarg)) <= 0)
125 				errx(1, "interval <= 0.");
126 			break;
127 		case 't':
128 			if ((turns = atoi(optarg)) <= 0)
129 				errx(1, "turns <= 0.");
130 			break;
131 		case '?':
132 		default:
133 			usage();
134 		}
135 	argc -= optind;
136 	argv += optind;
137 
138 
139 	for ( ; argc > 0; argc--, argv++) {
140 		struct mode *p;
141 		int modefound = 0;
142 
143 		if (isdigit(argv[0][0])) {
144 			naptime = atoi(argv[0]);
145 			if (naptime <= 0)
146 				naptime = 5;
147 			continue;
148 		}
149 
150 		for (p = modes; p->c_name ; p++) {
151 			if (strstr(p->c_name, argv[0]) == p->c_name) {
152 				curmode = p;
153 				modefound++;
154 				break;
155 			}
156 
157 			if(strstr("all",argv[0]) == "all"){
158 				allcounter=0;
159 				allflag=1;
160 			}
161 		}
162 
163 
164 		if (!modefound && !allflag)
165 			error("%s: Unknown command.", argv[0]);
166 	}
167 
168 	/*
169 	 * Discard setgid privileges.  If not the running kernel, we toss
170 	 * them away totally so that bad guys can't print interesting stuff
171 	 * from kernel memory, otherwise switch back to kmem for the
172 	 * duration of the kvm_openfiles() call.
173 	 */
174 	if (nlistf != NULL || memf != NULL)
175 		(void)setgid(getgid());
176 	else
177 		(void)setegid(egid);
178 
179 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
180 	if (kd == NULL) {
181 		error("%s", errbuf);
182 		exit(1);
183 	}
184 
185 	/* Get rid of privs for now. */
186 	if (nlistf == NULL && memf == NULL)
187 		(void)setegid(getgid());
188 
189 	if (kvm_nlist(kd, namelist)) {
190 		nlisterr(namelist);
191 		exit(1);
192 	}
193 	if (namelist[X_FIRST].n_type == 0)
194 		errx(1, "couldn't read namelist");
195 	signal(SIGINT, die);
196 	signal(SIGQUIT, die);
197 	signal(SIGTERM, die);
198 	signal(SIGWINCH, redraw);
199 	sv_stop_handler = signal(SIGTSTP, stop);
200 
201 	/*
202 	 * Initialize display.  Load average appears in a one line
203 	 * window of its own.  Current command's display appears in
204 	 * an overlapping sub-window of stdscr configured by the display
205 	 * routines to minimize update work by curses.
206 	 */
207 	if (initscr() == NULL)
208 	{
209 		warnx("couldn't initialize screen");
210 		exit(0);
211 	}
212 
213 	CMDLINE = LINES - 1;
214 	wnd = (*curmode->c_open)();
215 	if (wnd == NULL) {
216 		warnx("couldn't initialize display");
217 		die(0);
218 	}
219 	wload = newwin(1, 0, 3, 20);
220 	if (wload == NULL) {
221 		warnx("couldn't set up load average window");
222 		die(0);
223 	}
224 	gethostname(hostname, sizeof (hostname));
225 	hostname[sizeof(hostname) - 1] = '\0';
226 	NREAD(X_HZ, &hz, sizeof hz);
227 	NREAD(X_STATHZ, &stathz, sizeof stathz);
228 	NREAD(X_MAXSLP, &maxslp, sizeof maxslp);
229 	(*curmode->c_init)();
230 	curmode->c_flags |= CF_INIT;
231 	labels();
232 
233 	dellave = 0.0;
234 
235 	signal(SIGALRM, display);
236 	display(0);
237 	noecho();
238 	cbreak();
239 	keyboard();
240 	/*NOTREACHED*/
241 }
242 
243 static void
244 usage(void)
245 {
246 	fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] "
247 		"[-t turns]\n\t\t[display] [refresh-interval]\n");
248 	exit(1);
249 }
250 
251 
252 void
253 labels(void)
254 {
255 	if (curmode->c_flags & CF_LOADAV) {
256 		mvaddstr(2, 20,
257 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
258 		mvaddstr(3, 5, "Load Average");
259 	}
260 	(*curmode->c_label)();
261 #ifdef notdef
262 	mvprintw(21, 25, "CPU usage on %s", hostname);
263 #endif
264 	refresh();
265 }
266 
267 void
268 display(int signo)
269 {
270 	int j;
271 	sigset_t set;
272 	struct mode *p;
273 
274 	sigemptyset(&set);
275 	sigaddset(&set, SIGALRM);
276 	sigprocmask(SIG_BLOCK, &set, NULL);
277 
278 	/* Get the load average over the last minute. */
279 	(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
280 	(*curmode->c_fetch)();
281 	if (curmode->c_flags & CF_LOADAV) {
282 		j = 5.0*avenrun[0] + 0.5;
283 		dellave -= avenrun[0];
284 		if (dellave >= 0.0)
285 			c = '<';
286 		else {
287 			c = '>';
288 			dellave = -dellave;
289 		}
290 		if (dellave < 0.1)
291 			c = '|';
292 		dellave = avenrun[0];
293 		wmove(wload, 0, 0);
294 		wclrtoeol(wload);
295 		whline(wload, c, (j > 50) ? 50 : j);
296 		if (j > 50)
297 			wprintw(wload, " %4.1f", avenrun[0]);
298 	}
299 	(*curmode->c_refresh)();
300 	if (curmode->c_flags & CF_LOADAV)
301 		wrefresh(wload);
302 	wrefresh(wnd);
303 	move(CMDLINE, col);
304 	refresh();
305 
306 	if (allflag && signo==SIGALRM) {
307 		if (allcounter >= turns){
308 			p = curmode;
309 			p++;
310 			if (p->c_name == NULL)
311 				p = modes;
312 			switch_mode(p);
313 			allcounter=0;
314 		} else
315 			allcounter++;
316        }
317 
318 	sigprocmask(SIG_UNBLOCK, &set, NULL);
319 	alarm(naptime);
320 }
321 
322 void
323 redraw(int signo)
324 {
325 	sigset_t set;
326 	struct winsize win;
327 
328 	sigemptyset(&set);
329 	sigaddset(&set, SIGALRM);
330 	sigprocmask(SIG_BLOCK, &set, NULL);
331 
332 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
333 	    (win.ws_row != LINES || win.ws_col != COLS)) {
334 		resizeterm(win.ws_row, win.ws_col);
335 		CMDLINE = LINES - 1;
336 		labels();
337 	}
338 
339 	sigprocmask(SIG_UNBLOCK, &set, NULL);
340 	display(0);
341 }
342 
343 static void
344 stop(int signo)
345 {
346 	sigset_t set;
347 
348 	signal(SIGTSTP, sv_stop_handler);
349 	/* unblock SIGTSTP */
350 	sigemptyset(&set);
351 	sigaddset(&set, SIGTSTP);
352 	sigprocmask(SIG_UNBLOCK, &set, NULL);
353 	/* stop ourselves */
354 	kill(0, SIGTSTP);
355 	/* must have been restarted */
356 	signal(SIGTSTP, stop);
357 	redraw(signo);
358 }
359 
360 void
361 die(int signo)
362 {
363 	move(CMDLINE, 0);
364 	clrtoeol();
365 	refresh();
366 	endwin();
367 	exit(0);
368 }
369 
370 void
371 error(const char *fmt, ...)
372 {
373 	va_list ap;
374 	char buf[255];
375 	int oy, ox;
376 
377 	va_start(ap, fmt);
378 
379 	if (wnd) {
380 		getyx(stdscr, oy, ox);
381 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
382 		clrtoeol();
383 		standout();
384 		mvaddstr(CMDLINE, 0, buf);
385 		standend();
386 		move(oy, ox);
387 		refresh();
388 	} else {
389 		(void) vfprintf(stderr, fmt, ap);
390 		fprintf(stderr, "\n");
391 	}
392 	va_end(ap);
393 }
394 
395 void
396 nlisterr(struct nlist namelist[])
397 {
398 	int i, n;
399 
400 	n = 0;
401 	clear();
402 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
403 	for (i = 0;
404 	    namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
405 		if (namelist[i].n_value == 0)
406 			mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
407 	move(CMDLINE, 0);
408 	clrtoeol();
409 	refresh();
410 	endwin();
411 	exit(1);
412 }
413