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