Home | History | Annotate | Line # | Download | only in systat
main.c revision 1.34
      1 /*	$NetBSD: main.c,v 1.34 2003/08/03 12:14:58 jdolecek 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.34 2003/08/03 12:14:58 jdolecek 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 		if (nlistf)
    191 			errx(1, "%s: no namelist", nlistf);
    192 		else
    193 			errx(1, "no namelist");
    194 	}
    195 
    196 	signal(SIGINT, die);
    197 	signal(SIGQUIT, die);
    198 	signal(SIGTERM, die);
    199 	signal(SIGWINCH, redraw);
    200 	sv_stop_handler = signal(SIGTSTP, stop);
    201 
    202 	/*
    203 	 * Initialize display.  Load average appears in a one line
    204 	 * window of its own.  Current command's display appears in
    205 	 * an overlapping sub-window of stdscr configured by the display
    206 	 * routines to minimize update work by curses.
    207 	 */
    208 	if (initscr() == NULL)
    209 	{
    210 		warnx("couldn't initialize screen");
    211 		exit(0);
    212 	}
    213 
    214 	CMDLINE = LINES - 1;
    215 	wnd = (*curmode->c_open)();
    216 	if (wnd == NULL) {
    217 		warnx("couldn't initialize display");
    218 		die(0);
    219 	}
    220 	wload = newwin(1, 0, 3, 20);
    221 	if (wload == NULL) {
    222 		warnx("couldn't set up load average window");
    223 		die(0);
    224 	}
    225 	gethostname(hostname, sizeof (hostname));
    226 	hostname[sizeof(hostname) - 1] = '\0';
    227 	NREAD(X_HZ, &hz, sizeof hz);
    228 	NREAD(X_STATHZ, &stathz, sizeof stathz);
    229 	NREAD(X_MAXSLP, &maxslp, sizeof maxslp);
    230 	(*curmode->c_init)();
    231 	curmode->c_flags |= CF_INIT;
    232 	labels();
    233 
    234 	dellave = 0.0;
    235 
    236 	signal(SIGALRM, display);
    237 	display(0);
    238 	noecho();
    239 	cbreak();
    240 	keyboard();
    241 	/*NOTREACHED*/
    242 }
    243 
    244 static void
    245 usage(void)
    246 {
    247 	fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] "
    248 		"[-t turns]\n\t\t[display] [refresh-interval]\n");
    249 	exit(1);
    250 }
    251 
    252 
    253 void
    254 labels(void)
    255 {
    256 	if (curmode->c_flags & CF_LOADAV) {
    257 		mvaddstr(2, 20,
    258 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
    259 		mvaddstr(3, 5, "Load Average");
    260 	}
    261 	(*curmode->c_label)();
    262 #ifdef notdef
    263 	mvprintw(21, 25, "CPU usage on %s", hostname);
    264 #endif
    265 	refresh();
    266 }
    267 
    268 void
    269 display(int signo)
    270 {
    271 	int j;
    272 	sigset_t set;
    273 	struct mode *p;
    274 
    275 	sigemptyset(&set);
    276 	sigaddset(&set, SIGALRM);
    277 	sigprocmask(SIG_BLOCK, &set, NULL);
    278 
    279 	/* Get the load average over the last minute. */
    280 	(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
    281 	(*curmode->c_fetch)();
    282 	if (curmode->c_flags & CF_LOADAV) {
    283 		j = 5.0*avenrun[0] + 0.5;
    284 		dellave -= avenrun[0];
    285 		if (dellave >= 0.0)
    286 			c = '<';
    287 		else {
    288 			c = '>';
    289 			dellave = -dellave;
    290 		}
    291 		if (dellave < 0.1)
    292 			c = '|';
    293 		dellave = avenrun[0];
    294 		wmove(wload, 0, 0);
    295 		wclrtoeol(wload);
    296 		whline(wload, c, (j > 50) ? 50 : j);
    297 		if (j > 50)
    298 			wprintw(wload, " %4.1f", avenrun[0]);
    299 	}
    300 	(*curmode->c_refresh)();
    301 	if (curmode->c_flags & CF_LOADAV)
    302 		wrefresh(wload);
    303 	wrefresh(wnd);
    304 	move(CMDLINE, col);
    305 	refresh();
    306 
    307 	if (allflag && signo==SIGALRM) {
    308 		if (allcounter >= turns){
    309 			p = curmode;
    310 			p++;
    311 			if (p->c_name == NULL)
    312 				p = modes;
    313 			switch_mode(p);
    314 			allcounter=0;
    315 		} else
    316 			allcounter++;
    317        }
    318 
    319 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    320 	alarm(naptime);
    321 }
    322 
    323 void
    324 redraw(int signo)
    325 {
    326 	sigset_t set;
    327 	struct winsize win;
    328 
    329 	sigemptyset(&set);
    330 	sigaddset(&set, SIGALRM);
    331 	sigprocmask(SIG_BLOCK, &set, NULL);
    332 
    333 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
    334 	    (win.ws_row != LINES || win.ws_col != COLS)) {
    335 		resizeterm(win.ws_row, win.ws_col);
    336 		CMDLINE = LINES - 1;
    337 		labels();
    338 	}
    339 
    340 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    341 	display(0);
    342 }
    343 
    344 static void
    345 stop(int signo)
    346 {
    347 	sigset_t set;
    348 
    349 	signal(SIGTSTP, sv_stop_handler);
    350 	/* unblock SIGTSTP */
    351 	sigemptyset(&set);
    352 	sigaddset(&set, SIGTSTP);
    353 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    354 	/* stop ourselves */
    355 	kill(0, SIGTSTP);
    356 	/* must have been restarted */
    357 	signal(SIGTSTP, stop);
    358 	redraw(signo);
    359 }
    360 
    361 void
    362 die(int signo)
    363 {
    364 	move(CMDLINE, 0);
    365 	clrtoeol();
    366 	refresh();
    367 	endwin();
    368 	exit(0);
    369 }
    370 
    371 void
    372 error(const char *fmt, ...)
    373 {
    374 	va_list ap;
    375 	char buf[255];
    376 	int oy, ox;
    377 
    378 	va_start(ap, fmt);
    379 
    380 	if (wnd) {
    381 		getyx(stdscr, oy, ox);
    382 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
    383 		clrtoeol();
    384 		standout();
    385 		mvaddstr(CMDLINE, 0, buf);
    386 		standend();
    387 		move(oy, ox);
    388 		refresh();
    389 	} else {
    390 		(void) vfprintf(stderr, fmt, ap);
    391 		fprintf(stderr, "\n");
    392 	}
    393 	va_end(ap);
    394 }
    395 
    396 void
    397 nlisterr(struct nlist namelist[])
    398 {
    399 	int i, n;
    400 
    401 	n = 0;
    402 	clear();
    403 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
    404 	for (i = 0;
    405 	    namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
    406 		if (namelist[i].n_value == 0)
    407 			mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
    408 	move(CMDLINE, 0);
    409 	clrtoeol();
    410 	refresh();
    411 	sleep(5);
    412 	endwin();
    413 	exit(1);
    414 }
    415